Skip to content

Commit

Permalink
Use a strict version of putMVar.
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav committed Apr 5, 2013
1 parent 50a76c9 commit 998a7b5
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pl5/msort.hs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ concatBS size =
-- Utilities
------------------------------------------------------------------------------

-- | A strict version of @putMVar@ that accepts a monad operation that
-- produces the output.
putMVarM' :: MonadIO m => Conc.MVar a -> m a -> m ()
putMVarM' v m = do !x <- m
liftIO $! Conc.putMVar v x

-- | Get the current system time in milliseconds.
now :: IO Int
now = truncate . (* 1000) <$> getPOSIXTime
Expand Down Expand Up @@ -500,14 +506,13 @@ main = do
-- sort it on its own.

-- Sort each run concurrently and get the final run info
finalRunMVars <- forM sortedRuns $ \runInfo -> do
!finalRunMVars <- forM sortedRuns $ \runInfo -> do
final <- liftIO Conc.newEmptyMVar
Res.resourceForkIO $
msort runInfo runLength bufSize k >>=
liftIO . Conc.putMVar final
Res.resourceForkIO $ putMVarM' final $
msort runInfo runLength bufSize k
return final

finalRuns <- mapM (liftIO . Conc.takeMVar) finalRunMVars
!finalRuns <- mapM (liftIO . Conc.takeMVar) finalRunMVars

-- Now have @numCapabilities@ files that are fully sorted. Can merge
-- them all in a single pass.
Expand Down

0 comments on commit 998a7b5

Please sign in to comment.