|
1 | 1 | {-# LANGUAGE CPP, NamedFieldPuns, RecordWildCards, ScopedTypeVariables, RankNTypes, DeriveDataTypeable #-} |
2 | 2 |
|
3 | | -#if MIN_VERSION_monad_control(0,3,0) |
4 | | -{-# LANGUAGE FlexibleContexts #-} |
5 | | -#endif |
6 | | - |
7 | 3 | #if !MIN_VERSION_base(4,3,0) |
8 | 4 | {-# LANGUAGE RankNTypes #-} |
9 | 5 | #endif |
@@ -54,24 +50,7 @@ import Data.Typeable (Typeable) |
54 | 50 | import GHC.Conc.Sync (labelThread) |
55 | 51 | import qualified Control.Exception as E |
56 | 52 | 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) |
75 | 54 |
|
76 | 55 | -- | A single resource pool entry. |
77 | 56 | data Entry a = Entry { |
@@ -247,15 +226,9 @@ purgeLocalPool destroy LocalPool{..} = do |
247 | 226 | -- destroy a pooled resource, as doing so will almost certainly cause |
248 | 227 | -- a subsequent user (who expects the resource to be valid) to throw |
249 | 228 | -- 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 |
257 | 230 | {-# 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 |
259 | 232 | (resource, local) <- takeResource pool |
260 | 233 | ret <- restore (runInIO (act resource)) `onException` |
261 | 234 | destroyResource pool local resource |
@@ -295,14 +268,8 @@ takeResource pool@Pool{..} = do |
295 | 268 | -- returns immediately with 'Nothing' (ie. the action function is /not/ called). |
296 | 269 | -- Conversely, if a resource can be borrowed from the pool without blocking, the |
297 | 270 | -- 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 |
306 | 273 | res <- tryTakeResource pool |
307 | 274 | case res of |
308 | 275 | Just (resource, local) -> do |
|
0 commit comments