diff options
-rw-r--r-- | lore/Control/Concurrent/Go.hs | 13 | ||||
-rw-r--r-- | lore/Control/Concurrent/Sima.hs | 10 |
2 files changed, 12 insertions, 11 deletions
diff --git a/lore/Control/Concurrent/Go.hs b/lore/Control/Concurrent/Go.hs index 05626d7..69ce9ed 100644 --- a/lore/Control/Concurrent/Go.hs +++ b/lore/Control/Concurrent/Go.hs @@ -70,6 +70,7 @@ go = forkIO {- Example: (TODO: move to module-level docs) +-- can I just implement forM/Traversable over the channel? forRange :: TChan (Maybe a) -> (a -> IO b) -> IO [b] forRange ch fn = helper fn [] where -- I could not get Intero or ghc to accept a type-annotation for the helper function, @@ -83,19 +84,19 @@ forRange ch fn = helper fn [] where b <- fn v helper fn (b : acc) -generate :: (Num a, Enum a) => TChan (Maybe a) -> IO () -generate ch = do +feedData :: (Num a, Enum a) => TChan (Maybe a) -> IO () +feedData ch = do forM_ [1 .. 9999] (\x -> writeCh ch (Just x)) writeQ ch Nothing -- EOF value -process :: TChan (Maybe Int) -> IO () -process c = do +printData :: TChan (Maybe Int) -> IO () +printData c = do forRange c (print :: Int -> IO ()) return () main :: IO () main = do ch <- chan - go $ generate ch - process ch + go $ feedData ch + printData ch -} diff --git a/lore/Control/Concurrent/Sima.hs b/lore/Control/Concurrent/Sima.hs index 3588a2a..b69c0bb 100644 --- a/lore/Control/Concurrent/Sima.hs +++ b/lore/Control/Concurrent/Sima.hs @@ -1,12 +1,12 @@ -module Control.Concurrent.Simaphore +module Control.Concurrent.Sima ( mapPool ) where -import Control.Concurrent.MSem +import qualified Control.Concurrent.MSem as Sem -- | Simaphore-based throttled 'mapConcurrently'. mapPool :: Traversable t => Int -> (a -> IO b) -> t a -> IO (t b) -mapPool lim f xs = do - sima <- new lim - mapConcurrently (with sima . f) xs +mapPool n f xs = do + sima <- Sem.new n + mapConcurrently (Sem.with sima . f) xs |