Skip to content

Commit

Permalink
Async exception protection on withResource
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Jan 7, 2012
1 parent db5dbfc commit 318dab8
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Data/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
{-# LANGUAGE FlexibleContexts #-}
#endif

#if !MIN_VERSION_base(4,3,0)
{-# LANGUAGE RankNTypes #-}
#endif

-- |
-- Module: Data.Pool
-- Copyright: (c) 2011 MailRank, Inc.
Expand Down Expand Up @@ -55,6 +59,14 @@ import Control.Monad.IO.Class (liftIO)
#define liftBase liftIO
#endif

#if MIN_VERSION_base(4,3,0)
import Control.Exception (mask)
#else
-- Don't do any async exception protection for older GHCs.
mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b
mask f = f id
#endif

-- | A single resource pool entry.
data Entry a = Entry {
entry :: a
Expand Down Expand Up @@ -188,11 +200,11 @@ withResource ::
#endif
=> Pool a -> (a -> m b) -> m b
{-# SPECIALIZE withResource :: Pool a -> (a -> IO b) -> IO b #-}
withResource pool act = do
(resource, local) <- liftBase (takeResource pool)
ret <- control $ \runInIO -> runInIO (act resource) `onException`
withResource pool act = control $ \runInIO -> mask $ \restore -> do
(resource, local) <- takeResource pool
ret <- restore (runInIO (act resource)) `onException`
destroyResource pool local resource
liftBase (putResource local resource)
putResource local resource
return ret
#if __GLASGOW_HASKELL__ >= 700
{-# INLINABLE withResource #-}
Expand Down

0 comments on commit 318dab8

Please sign in to comment.