Created
July 13, 2014 13:27
-
-
Save ssadler/6289a356f01c6a376855 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Actors where | |
import Control.Concurrent.MVar | |
import Control.Concurrent (forkIO) | |
import Control.Monad (forever, replicateM) | |
type PoolQuery a b = a -> IO b | |
type PoolActor a = MVar (PoolQuery a ()) | |
forkPoolActors :: (IO a) -> Int -> IO (PoolActor a) | |
forkPoolActors construct n = do | |
qbox <- newEmptyMVar | |
replicateM n $ do | |
forkIO $ do | |
res <- construct | |
forever $ do | |
query <- takeMVar qbox | |
query res | |
return () | |
return qbox | |
withResource :: PoolActor a -> PoolQuery a b -> IO b | |
withResource qbox query = do | |
abox <- newEmptyMVar | |
putMVar qbox $ \res -> query res >>= putMVar abox | |
takeMVar abox |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment