{-# 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 :: m ThreadId
myThreadId = IO ThreadId -> m ThreadId
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ThreadId
C.myThreadId
{-# INLINABLE myThreadId #-}
forkIO :: MonadUnliftIO m => m () -> m ThreadId
forkIO :: m () -> m ThreadId
forkIO m ()
m = ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO () -> IO ThreadId
C.forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ m () -> IO ()
forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkIO #-}
forkIOWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask :: ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask (forall a. m a -> m a) -> m ()
m =
((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
C.forkIOWithUnmask (((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId)
-> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> m () -> IO ()
forall a. m a -> IO a
run (m () -> IO ()) -> m () -> IO ()
forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m ()
m ((forall a. m a -> m a) -> m ()) -> (forall a. m a -> m a) -> m ()
forall a b. (a -> b) -> a -> b
$ IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (m a -> IO a) -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> IO a
forall a. IO a -> IO a
unmask (IO a -> IO a) -> (m a -> IO a) -> m a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> IO a
forall a. m a -> IO a
run
{-# INLINABLE forkIOWithUnmask #-}
forkWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkWithUnmask :: ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkWithUnmask = ((forall a. m a -> m a) -> m ()) -> m ThreadId
forall (m :: * -> *).
MonadUnliftIO m =>
((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask
{-# INLINABLE forkWithUnmask #-}
{-# DEPRECATED forkWithUnmask "forkWithUnmask has been renamed to forkIOWithUnmask" #-}
forkFinally :: MonadUnliftIO m => m a -> (Either SomeException a -> m ()) -> m ThreadId
forkFinally :: m a -> (Either SomeException a -> m ()) -> m ThreadId
forkFinally m a
m1 Either SomeException a -> m ()
m2 = ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
forall a. IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
C.forkFinally (m a -> IO a
forall a. m a -> IO a
run m a
m1) ((Either SomeException a -> IO ()) -> IO ThreadId)
-> (Either SomeException a -> IO ()) -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ m () -> IO ()
forall a. m a -> IO a
run (m () -> IO ())
-> (Either SomeException a -> m ())
-> Either SomeException a
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either SomeException a -> m ()
m2
{-# INLINABLE forkFinally #-}
killThread :: MonadIO m => ThreadId -> m ()
killThread :: ThreadId -> m ()
killThread = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (ThreadId -> IO ()) -> ThreadId -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO ()
C.killThread
{-# INLINABLE killThread #-}
forkOn :: MonadUnliftIO m => Int -> m () -> m ThreadId
forkOn :: Int -> m () -> m ThreadId
forkOn Int
i m ()
m = ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> Int -> IO () -> IO ThreadId
C.forkOn Int
i (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ m () -> IO ()
forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkOn #-}
forkOnWithUnmask :: MonadUnliftIO m => Int -> ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkOnWithUnmask :: Int -> ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkOnWithUnmask Int
i (forall a. m a -> m a) -> m ()
m =
((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> Int -> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
C.forkOnWithUnmask Int
i (((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId)
-> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> m () -> IO ()
forall a. m a -> IO a
run (m () -> IO ()) -> m () -> IO ()
forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m ()
m ((forall a. m a -> m a) -> m ()) -> (forall a. m a -> m a) -> m ()
forall a b. (a -> b) -> a -> b
$ IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (m a -> IO a) -> m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> IO a
forall a. IO a -> IO a
unmask (IO a -> IO a) -> (m a -> IO a) -> m a -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m a -> IO a
forall a. m a -> IO a
run
{-# INLINABLE forkOnWithUnmask #-}
getNumCapabilities :: MonadIO m => m Int
getNumCapabilities :: m Int
getNumCapabilities = IO Int -> m Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Int
C.getNumCapabilities
{-# INLINABLE getNumCapabilities #-}
setNumCapabilities :: MonadIO m => Int -> m ()
setNumCapabilities :: Int -> m ()
setNumCapabilities = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Int -> IO ()) -> Int -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IO ()
C.setNumCapabilities
{-# INLINABLE setNumCapabilities #-}
threadCapability :: MonadIO m => ThreadId -> m (Int, Bool)
threadCapability :: ThreadId -> m (Int, Bool)
threadCapability = IO (Int, Bool) -> m (Int, Bool)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Int, Bool) -> m (Int, Bool))
-> (ThreadId -> IO (Int, Bool)) -> ThreadId -> m (Int, Bool)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO (Int, Bool)
C.threadCapability
{-# INLINABLE threadCapability #-}
yield :: MonadIO m => m ()
yield :: m ()
yield = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
C.yield
{-# INLINABLE yield #-}
threadDelay :: MonadIO m => Int -> m ()
threadDelay :: Int -> m ()
threadDelay = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Int -> IO ()) -> Int -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IO ()
C.threadDelay
{-# INLINABLE threadDelay #-}
threadWaitRead :: MonadIO m => Fd -> m ()
threadWaitRead :: Fd -> m ()
threadWaitRead = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Fd -> IO ()) -> Fd -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fd -> IO ()
C.threadWaitRead
{-# INLINABLE threadWaitRead #-}
threadWaitWrite :: MonadIO m => Fd -> m ()
threadWaitWrite :: Fd -> m ()
threadWaitWrite = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Fd -> IO ()) -> Fd -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fd -> IO ()
C.threadWaitWrite
{-# INLINABLE threadWaitWrite #-}
forkOS :: MonadUnliftIO m => m () -> m ThreadId
forkOS :: m () -> m ThreadId
forkOS m ()
m = ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId)
-> ((forall a. m a -> IO a) -> IO ThreadId) -> m ThreadId
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO () -> IO ThreadId
C.forkOS (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ m () -> IO ()
forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkOS #-}
isCurrentThreadBound :: MonadIO m => m Bool
isCurrentThreadBound :: m Bool
isCurrentThreadBound = IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Bool
C.isCurrentThreadBound
{-# INLINABLE isCurrentThreadBound #-}
runInBoundThread :: MonadUnliftIO m => m a -> m a
runInBoundThread :: m a -> m a
runInBoundThread m a
m = ((forall a. m a -> IO a) -> IO a) -> m a
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO a) -> m a)
-> ((forall a. m a -> IO a) -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO a -> IO a
forall a. IO a -> IO a
C.runInBoundThread (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ m a -> IO a
forall a. m a -> IO a
run m a
m
{-# INLINABLE runInBoundThread #-}
runInUnboundThread :: MonadUnliftIO m => m a -> m a
runInUnboundThread :: m a -> m a
runInUnboundThread m a
m = ((forall a. m a -> IO a) -> IO a) -> m a
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. m a -> IO a) -> IO a) -> m a)
-> ((forall a. m a -> IO a) -> IO a) -> m a
forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO a -> IO a
forall a. IO a -> IO a
C.runInUnboundThread (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ m a -> IO a
forall a. m a -> IO a
run m a
m
{-# INLINABLE runInUnboundThread #-}
mkWeakThreadId :: MonadIO m => ThreadId -> m (Weak ThreadId)
mkWeakThreadId :: ThreadId -> m (Weak ThreadId)
mkWeakThreadId = IO (Weak ThreadId) -> m (Weak ThreadId)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Weak ThreadId) -> m (Weak ThreadId))
-> (ThreadId -> IO (Weak ThreadId))
-> ThreadId
-> m (Weak ThreadId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO (Weak ThreadId)
C.mkWeakThreadId
{-# INLINABLE mkWeakThreadId #-}