git-annex/Utility/Parallel.hs
Joey Hess 2edb5d145c rewrote to not use forkProcess
That can make the threaded runtime stall.. But it can use threads now!
2012-07-18 19:25:46 -04:00

35 lines
957 B
Haskell

{- parallel processing via threads
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.Parallel where
import Common
import Control.Concurrent
import Control.Exception
{- Runs an action in parallel with a set of values, in a set of threads.
- In order for the actions to truely run in parallel, requires GHC's
- threaded runtime,
-
- Returns the values partitioned into ones with which the action succeeded,
- and ones with which it failed. -}
inParallel :: (v -> IO ()) -> [v] -> IO ([v], [v])
inParallel a l = do
mvars <- mapM thread l
statuses <- mapM takeMVar mvars
return $ reduce $ partition snd $ zip l statuses
where
reduce (x,y) = (map fst x, map fst y)
thread v = do
mvar <- newEmptyMVar
_ <- forkIO $ do
r <- try (a v) :: IO (Either SomeException ())
case r of
Left _ -> putMVar mvar False
Right _ -> putMVar mvar True
return mvar