X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Control%2FConcurrent.hs;h=8df665e37cae5166202a7cd46ebac4a89ef1d8c8;hb=9846058c21cf4eae3d5b4b3b977db2b85342771e;hp=f3e00827d769519459194b6df3cea7bad0fd449a;hpb=e816bd912de53222ae9baf9343236e9bd1462d23;p=ghc-base.git diff --git a/Control/Concurrent.hs b/Control/Concurrent.hs index f3e0082..8df665e 100644 --- a/Control/Concurrent.hs +++ b/Control/Concurrent.hs @@ -230,8 +230,8 @@ real_handler ex = AsyncException ThreadKilled -> return () -- report all others: - AsyncException StackOverflow -> reportStackOverflow False - other -> reportError False other + AsyncException StackOverflow -> reportStackOverflow + other -> reportError other #endif /* __GLASGOW_HASKELL__ */ @@ -478,8 +478,8 @@ runInUnboundThread action = do > myForkIO :: IO () -> IO (MVar ()) > myForkIO io = do -> mvar \<- newEmptyMVar -> forkIO (io \`finally\` putMVar mvar ()) +> mvar <- newEmptyMVar +> forkIO (io `finally` putMVar mvar ()) > return mvar Note that we use 'finally' from the @@ -490,25 +490,26 @@ runInUnboundThread action = do A better method is to keep a global list of all child threads which we should wait for at the end of the program: -> children :: MVar [MVar ()] -> children = unsafePerformIO (newMVar []) -> -> waitForChildren :: IO () -> waitForChildren = do -> (mvar:mvars) \<- takeMVar children -> putMVar children mvars -> takeMVar mvar -> waitForChildren -> -> forkChild :: IO () -> IO () -> forkChild io = do -> mvar \<- newEmptyMVar -> forkIO (p \`finally\` putMVar mvar ()) -> childs \<- takeMVar children -> putMVar children (mvar:childs) -> -> later = flip finally -> +> children :: MVar [MVar ()] +> children = unsafePerformIO (newMVar []) +> +> waitForChildren :: IO () +> waitForChildren = do +> cs <- takeMVar children +> case cs of +> [] -> return () +> m:ms -> do +> putMVar children ms +> takeMVar m +> waitForChildren +> +> forkChild :: IO () -> IO () +> forkChild io = do +> mvar <- newEmptyMVar +> childs <- takeMVar children +> putMVar children (mvar:childs) +> forkIO (io `finally` putMVar mvar ()) +> > main = > later waitForChildren $ > ...