module Game.LambdaHack.Utils.Thread
( forkChild, waitForChildren
) where
import Control.Concurrent (ThreadId, forkIO)
import Control.Concurrent.MVar
import Control.Exception (finally)
forkChild :: MVar [MVar ()] -> IO () -> IO ThreadId
forkChild children io = do
mvar <- newEmptyMVar
childs <- takeMVar children
putMVar children (mvar : childs)
forkIO (io `finally` putMVar mvar ())
waitForChildren :: MVar [MVar ()] -> IO ()
waitForChildren children = do
cs <- takeMVar children
case cs of
[] -> return ()
m : ms -> do
putMVar children ms
takeMVar m
waitForChildren children