Skip to content

Commit 549efda

Browse files
Use unliftio over monad-control
1 parent f4be4fb commit 549efda

File tree

2 files changed

+6
-39
lines changed

2 files changed

+6
-39
lines changed

Data/Pool.hs

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{-# LANGUAGE CPP, NamedFieldPuns, RecordWildCards, ScopedTypeVariables, RankNTypes, DeriveDataTypeable #-}
22

3-
#if MIN_VERSION_monad_control(0,3,0)
4-
{-# LANGUAGE FlexibleContexts #-}
5-
#endif
6-
73
#if !MIN_VERSION_base(4,3,0)
84
{-# LANGUAGE RankNTypes #-}
95
#endif
@@ -54,24 +50,7 @@ import Data.Typeable (Typeable)
5450
import GHC.Conc.Sync (labelThread)
5551
import qualified Control.Exception as E
5652
import qualified Data.Vector as V
57-
58-
#if MIN_VERSION_monad_control(0,3,0)
59-
import Control.Monad.Trans.Control (MonadBaseControl, control)
60-
import Control.Monad.Base (liftBase)
61-
#else
62-
import Control.Monad.IO.Control (MonadControlIO, controlIO)
63-
import Control.Monad.IO.Class (liftIO)
64-
#define control controlIO
65-
#define liftBase liftIO
66-
#endif
67-
68-
#if MIN_VERSION_base(4,3,0)
69-
import Control.Exception (mask)
70-
#else
71-
-- Don't do any async exception protection for older GHCs.
72-
mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b
73-
mask f = f id
74-
#endif
53+
import UnliftIO (mask, withRunInIO)
7554

7655
-- | A single resource pool entry.
7756
data Entry a = Entry {
@@ -247,15 +226,9 @@ purgeLocalPool destroy LocalPool{..} = do
247226
-- destroy a pooled resource, as doing so will almost certainly cause
248227
-- a subsequent user (who expects the resource to be valid) to throw
249228
-- an exception.
250-
withResource ::
251-
#if MIN_VERSION_monad_control(0,3,0)
252-
(MonadBaseControl IO m)
253-
#else
254-
(MonadControlIO m)
255-
#endif
256-
=> Pool a -> (a -> m b) -> m b
229+
withResource :: MonadUnliftIO m => Pool a -> (a -> m b) -> m b
257230
{-# SPECIALIZE withResource :: Pool a -> (a -> IO b) -> IO b #-}
258-
withResource pool act = control $ \runInIO -> mask $ \restore -> do
231+
withResource pool act = withRunInIO $ \runInIO -> mask $ \restore -> do
259232
(resource, local) <- takeResource pool
260233
ret <- restore (runInIO (act resource)) `onException`
261234
destroyResource pool local resource
@@ -295,14 +268,8 @@ takeResource pool@Pool{..} = do
295268
-- returns immediately with 'Nothing' (ie. the action function is /not/ called).
296269
-- Conversely, if a resource can be borrowed from the pool without blocking, the
297270
-- action is performed and it's result is returned, wrapped in a 'Just'.
298-
tryWithResource :: forall m a b.
299-
#if MIN_VERSION_monad_control(0,3,0)
300-
(MonadBaseControl IO m)
301-
#else
302-
(MonadControlIO m)
303-
#endif
304-
=> Pool a -> (a -> m b) -> m (Maybe b)
305-
tryWithResource pool act = control $ \runInIO -> mask $ \restore -> do
271+
tryWithResource :: forall m a b. MonadUnliftIO m => Pool a -> (a -> m b) -> m (Maybe b)
272+
tryWithResource pool act = withRunInIO $ \runInIO -> mask $ \restore -> do
306273
res <- tryTakeResource pool
307274
case res of
308275
Just (resource, local) -> do

resource-pool.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ library
3232
build-depends:
3333
base >= 4.4 && < 5,
3434
hashable,
35-
monad-control >= 0.2.0.1,
3635
transformers,
3736
transformers-base >= 0.4,
3837
stm >= 2.3,
3938
time,
39+
unliftio,
4040
vector >= 0.7
4141

4242
if flag(developer)

0 commit comments

Comments
 (0)