{-# LANGUAGE RankNTypes #-}
module UnliftIO.Concurrent
(
ThreadId,
myThreadId, forkIO, forkWithUnmask, forkIOWithUnmask, forkFinally, killThread, throwTo,
forkOn, forkOnWithUnmask, getNumCapabilities, setNumCapabilities,
threadCapability,
yield,
threadDelay, threadWaitRead, threadWaitWrite,
module UnliftIO.MVar, module UnliftIO.Chan,
C.rtsSupportsBoundThreads, forkOS, isCurrentThreadBound, runInBoundThread,
runInUnboundThread,
mkWeakThreadId
) where
import Control.Monad.IO.Class (MonadIO, liftIO)
import System.Posix.Types (Fd)
import System.Mem.Weak (Weak)
import Control.Concurrent (ThreadId)
import qualified Control.Concurrent as C
import Control.Monad.IO.Unlift
import UnliftIO.MVar
import UnliftIO.Chan
import UnliftIO.Exception (throwTo, SomeException)
myThreadId :: MonadIO m => m ThreadId
myThreadId = liftIO C.myThreadId
{-# INLINABLE myThreadId #-}
forkIO :: MonadUnliftIO m => m () -> m ThreadId
forkIO m = withRunInIO $ \run -> C.forkIO $ run m
{-# INLINABLE forkIO #-}
forkIOWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask m =
withRunInIO $ \run -> C.forkIOWithUnmask $ \unmask -> run $ m $ liftIO . unmask . run
{-# INLINABLE forkIOWithUnmask #-}
forkWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkWithUnmask = forkIOWithUnmask
{-# INLINABLE forkWithUnmask #-}
{-# DEPRECATED forkWithUnmask "forkWithUnmask has been renamed to forkIOWithUnmask" #-}
forkFinally :: MonadUnliftIO m => m a -> (Either SomeException a -> m ()) -> m ThreadId
forkFinally m1 m2 = withRunInIO $ \run -> C.forkFinally (run m1) $ run . m2
{-# INLINABLE forkFinally #-}
killThread :: MonadIO m => ThreadId -> m ()
killThread = liftIO . C.killThread
{-# INLINABLE killThread #-}
forkOn :: MonadUnliftIO m => Int -> m () -> m ThreadId
forkOn i m = withRunInIO $ \run -> C.forkOn i $ run m
{-# INLINABLE forkOn #-}
forkOnWithUnmask :: MonadUnliftIO m => Int -> ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkOnWithUnmask i m =
withRunInIO $ \run -> C.forkOnWithUnmask i $ \unmask -> run $ m $ liftIO . unmask . run
{-# INLINABLE forkOnWithUnmask #-}
getNumCapabilities :: MonadIO m => m Int
getNumCapabilities = liftIO C.getNumCapabilities
{-# INLINABLE getNumCapabilities #-}
setNumCapabilities :: MonadIO m => Int -> m ()
setNumCapabilities = liftIO . C.setNumCapabilities
{-# INLINABLE setNumCapabilities #-}
threadCapability :: MonadIO m => ThreadId -> m (Int, Bool)
threadCapability = liftIO . C.threadCapability
{-# INLINABLE threadCapability #-}
yield :: MonadIO m => m ()
yield = liftIO C.yield
{-# INLINABLE yield #-}
threadDelay :: MonadIO m => Int -> m ()
threadDelay = liftIO . C.threadDelay
{-# INLINABLE threadDelay #-}
threadWaitRead :: MonadIO m => Fd -> m ()
threadWaitRead = liftIO . C.threadWaitRead
{-# INLINABLE threadWaitRead #-}
threadWaitWrite :: MonadIO m => Fd -> m ()
threadWaitWrite = liftIO . C.threadWaitWrite
{-# INLINABLE threadWaitWrite #-}
forkOS :: MonadUnliftIO m => m () -> m ThreadId
forkOS m = withRunInIO $ \run -> C.forkOS $ run m
{-# INLINABLE forkOS #-}
isCurrentThreadBound :: MonadIO m => m Bool
isCurrentThreadBound = liftIO C.isCurrentThreadBound
{-# INLINABLE isCurrentThreadBound #-}
runInBoundThread :: MonadUnliftIO m => m a -> m a
runInBoundThread m = withRunInIO $ \run -> C.runInBoundThread $ run m
{-# INLINABLE runInBoundThread #-}
runInUnboundThread :: MonadUnliftIO m => m a -> m a
runInUnboundThread m = withRunInIO $ \run -> C.runInUnboundThread $ run m
{-# INLINABLE runInUnboundThread #-}
mkWeakThreadId :: MonadIO m => ThreadId -> m (Weak ThreadId)
mkWeakThreadId = liftIO . C.mkWeakThreadId
{-# INLINABLE mkWeakThreadId #-}