git-annex/Utility/Parallel.hs
Joey Hess 67c8ef7de2 use a TMVar
SampleMVar won't work; between getting the current value and changing
it, another thread could made a change, which would get lost.

TMVar works well; this update situation is handled by atomic transactions.
2012-06-26 19:21:44 -04:00

22 lines
659 B
Haskell

{- parallel processes
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.Parallel where
import Common
{- Runs an action in parallel with a set of values.
- 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
pids <- mapM (forkProcess . a) l
statuses <- mapM (getProcessStatus True False) pids
return $ reduce $ partition (succeeded . snd) $ zip l statuses
where
succeeded v = v == Just (Exited ExitSuccess)
reduce (x,y) = (map fst x, map fst y)