extra-0.3: Extra functions I use.

Safe HaskellSafe-Inferred



Extra functions for Control.Concurrent. These functions manipulate the number of capabilities and new types of lock.



withNumCapabilities :: Int -> IO a -> IO aSource

On GHC 7.6 and above with the -threaded flag, brackets a call to setNumCapabilities. On lower versions (which lack setNumCapabilities) this function just runs the argument action.

setNumCapabilities :: Int -> IO ()

Set the number of Haskell threads that can run truly simultaneously (on separate physical processors) at any given time. The number passed to forkOn is interpreted modulo this value. The initial value is given by the +RTS -N runtime flag.

This is also the number of threads that will participate in parallel garbage collection. It is strongly recommended that the number of capabilities is not set larger than the number of physical processor cores, and it may often be beneficial to leave one or more cores free to avoid contention with other processes in the machine.

forkFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId

fork a thread and call the supplied function when the thread is about to terminate, with an exception or a returned value. The function is called with asynchronous exceptions masked.

 forkFinally action and_then =
   mask $ \restore ->
     forkIO $ try (restore action) >>= and_then

This function is useful for informing the parent when a child terminates, for example.

data Lock Source

Like an MVar, but has no value

withLock :: Lock -> IO a -> IO aSource

data Var a Source

Like an MVar, but must always be full

newVar :: a -> IO (Var a)Source

readVar :: Var a -> IO aSource

modifyVar :: Var a -> (a -> IO (a, b)) -> IO bSource

modifyVar_ :: Var a -> (a -> IO a) -> IO ()Source

withVar :: Var a -> (a -> IO b) -> IO bSource

data Barrier a Source

Starts out empty, then is filled exactly once