{-# LANGUAGE RecordWildCards, RankNTypes #-}
module Control.Concurrent.Thread.BoundedThreadGroup where
import Control.Concurrent.Thread.Group (ThreadGroup)
import qualified Control.Concurrent.Thread as Thread
import qualified Control.Concurrent.Thread.Group as ThreadGroup
import Control.Concurrent
import Control.Concurrent.STM
data BoundedThreadGroup = BoundedThreadGroup
{ maxSize :: Int
, threadGroup :: ThreadGroup
}
new :: Int -> IO BoundedThreadGroup
new maxSize = do
threadGroup <- ThreadGroup.new
return BoundedThreadGroup {..}
nrOfRunning :: BoundedThreadGroup -> STM Int
nrOfRunning BoundedThreadGroup {..} = ThreadGroup.nrOfRunning threadGroup
wait :: BoundedThreadGroup -> IO ()
wait BoundedThreadGroup {..} = ThreadGroup.wait threadGroup
waitN :: Int -> BoundedThreadGroup -> IO ()
waitN i BoundedThreadGroup {..} = ThreadGroup.waitN i threadGroup
forkIO :: BoundedThreadGroup -> IO a -> IO (ThreadId, IO (Thread.Result a))
forkIO BoundedThreadGroup {..} action = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkIO threadGroup action
forkOS :: BoundedThreadGroup -> IO a -> IO (ThreadId, IO (Thread.Result a))
forkOS BoundedThreadGroup {..} action = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkOS threadGroup action
forkOn :: Int
-> BoundedThreadGroup
-> IO a
-> IO (ThreadId, IO (Thread.Result a))
forkOn i BoundedThreadGroup {..} action = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkOn i threadGroup action
forkIOWithUnmask :: BoundedThreadGroup
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Thread.Result a))
forkIOWithUnmask BoundedThreadGroup {..} f = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkIOWithUnmask threadGroup f
forkOnWithUnmask :: Int
-> BoundedThreadGroup
-> ((forall b. IO b -> IO b) -> IO a)
-> IO (ThreadId, IO (Thread.Result a))
forkOnWithUnmask i BoundedThreadGroup {..} f = do
ThreadGroup.waitN maxSize threadGroup
ThreadGroup.forkOnWithUnmask i threadGroup f