Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE PackageImports #-}
- module EasyPool
- ( EasyPool
- , withEasyPool
- , newEasyPool
- ) where
- import Pool
- import Control.Monad.STM
- import Control.Concurrent.STM.TVar
- import Control.Monad.IO.Class
- import "MonadCatchIO-transformers" Control.Monad.CatchIO
- data EasyPool r m = EasyPool
- { epPool :: Pool r
- , epMake :: m (Maybe r)
- }
- withEasyPool :: MonadCatchIO m => EasyPool r m -> (r -> m a) -> m (Maybe a)
- withEasyPool (EasyPool pool mk) = withPool pool mk
- newEasyPool :: MonadCatchIO m => Int -> m r -> m (EasyPool r m)
- newEasyPool count mk = do
- pool <- new
- texist <- liftIO $ newTVarIO 0
- return $ EasyPool pool $ mk' texist
- where
- mk' texist = do
- exist <- liftIO $ atomically $ readTVar texist
- if exist >= count
- then return Nothing
- else do
- r <- mk
- liftIO $ atomically $ do
- exist <- readTVar texist
- if exist >= count
- then return Nothing
- else return $ Just r
Add Comment
Please, Sign In to add comment