{-# LANGUAGE RankNTypes #-}
-- | Unlifted "Control.Concurrent".
--
-- This module is not reexported by "UnliftIO",
-- use it only if "UnliftIO.Async" is not enough.
--
-- @since 0.1.1.0
module UnliftIO.Concurrent
  (
    -- * Concurrent Haskell
    ThreadId,

    -- * Basic concurrency operations
    myThreadId, forkIO, forkWithUnmask, forkIOWithUnmask,  forkFinally, killThread, throwTo,

    -- ** Threads with affinity
    forkOn, forkOnWithUnmask, getNumCapabilities, setNumCapabilities,
    threadCapability,

    -- * Scheduling
    yield,

    -- ** Waiting
    threadDelay, threadWaitRead, threadWaitWrite,

    -- * Communication abstractions
    module UnliftIO.MVar, module UnliftIO.Chan,

    -- * Bound Threads
    C.rtsSupportsBoundThreads, forkOS, isCurrentThreadBound, runInBoundThread,
    runInUnboundThread,

    -- * Weak references to ThreadIds
    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)

-- | Lifted version of 'C.myThreadId'.
--
-- @since 0.1.1.0
myThreadId :: MonadIO m => m ThreadId
myThreadId :: forall (m :: * -> *). MonadIO m => m ThreadId
myThreadId = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ThreadId
C.myThreadId
{-# INLINABLE myThreadId #-}

-- | Unlifted version of 'C.forkIO'.
--
-- @since 0.1.1.0
forkIO :: MonadUnliftIO m => m () -> m ThreadId
forkIO :: forall (m :: * -> *). MonadUnliftIO m => m () -> m ThreadId
forkIO m ()
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO () -> IO ThreadId
C.forkIO forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkIO #-}

-- | Unlifted version of 'C.forkIOWithUnmask'.
--
-- @since 0.2.11
forkIOWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask :: forall (m :: * -> *).
MonadUnliftIO m =>
((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask (forall a. m a -> m a) -> m ()
m =
  forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO 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 b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m ()
m forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
unmask forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. m a -> IO a
run
{-# INLINABLE forkIOWithUnmask #-}

-- | Please use 'forkIOWithUnmask' instead. This function has been deprecated
-- in release 0.2.11 and will be removed in the next major release.
--
-- @since 0.1.1.0
forkWithUnmask :: MonadUnliftIO m => ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkWithUnmask :: forall (m :: * -> *).
MonadUnliftIO m =>
((forall a. m a -> m a) -> m ()) -> m ThreadId
forkWithUnmask = forall (m :: * -> *).
MonadUnliftIO m =>
((forall a. m a -> m a) -> m ()) -> m ThreadId
forkIOWithUnmask
{-# INLINABLE forkWithUnmask #-}
{-# DEPRECATED forkWithUnmask "forkWithUnmask has been renamed to forkIOWithUnmask" #-}

-- | Unlifted version of 'C.forkFinally'.
--
-- @since 0.1.1.0
forkFinally :: MonadUnliftIO m => m a -> (Either SomeException a -> m ()) -> m ThreadId
forkFinally :: forall (m :: * -> *) a.
MonadUnliftIO m =>
m a -> (Either SomeException a -> m ()) -> m ThreadId
forkFinally m a
m1 Either SomeException a -> m ()
m2 = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
C.forkFinally (forall a. m a -> IO a
run m a
m1) forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either SomeException a -> m ()
m2
{-# INLINABLE forkFinally #-}

-- | Lifted version of 'C.killThread'.
--
-- @since 0.1.1.0
killThread :: MonadIO m => ThreadId -> m ()
killThread :: forall (m :: * -> *). MonadIO m => ThreadId -> m ()
killThread = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO ()
C.killThread
{-# INLINABLE  killThread #-}

-- | Unlifted version of 'C.forkOn'.
--
-- @since 0.1.1.0
forkOn :: MonadUnliftIO m => Int -> m () -> m ThreadId
forkOn :: forall (m :: * -> *). MonadUnliftIO m => Int -> m () -> m ThreadId
forkOn Int
i m ()
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> Int -> IO () -> IO ThreadId
C.forkOn Int
i forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkOn #-}

-- | Unlifted version of 'C.forkOnWithUnmask'.
--
-- @since 0.1.1.0
forkOnWithUnmask :: MonadUnliftIO m => Int -> ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkOnWithUnmask :: forall (m :: * -> *).
MonadUnliftIO m =>
Int -> ((forall a. m a -> m a) -> m ()) -> m ThreadId
forkOnWithUnmask Int
i (forall a. m a -> m a) -> m ()
m =
  forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO 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 b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> forall a. m a -> IO a
run forall a b. (a -> b) -> a -> b
$ (forall a. m a -> m a) -> m ()
m forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> IO a
unmask forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. m a -> IO a
run
{-# INLINABLE forkOnWithUnmask #-}

-- | Lifted version of 'C.getNumCapabilities'.
--
-- @since 0.1.1.0
getNumCapabilities :: MonadIO m => m Int
getNumCapabilities :: forall (m :: * -> *). MonadIO m => m Int
getNumCapabilities = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Int
C.getNumCapabilities
{-# INLINABLE getNumCapabilities #-}

-- | Lifted version of 'C.setNumCapabilities'.
--
-- @since 0.1.1.0
setNumCapabilities :: MonadIO m => Int -> m ()
setNumCapabilities :: forall (m :: * -> *). MonadIO m => Int -> m ()
setNumCapabilities = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> IO ()
C.setNumCapabilities
{-# INLINABLE setNumCapabilities #-}

-- | Lifted version of 'C.threadCapability'.
--
-- @since 0.1.1.0
threadCapability :: MonadIO m => ThreadId -> m (Int, Bool)
threadCapability :: forall (m :: * -> *). MonadIO m => ThreadId -> m (Int, Bool)
threadCapability = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO (Int, Bool)
C.threadCapability
{-# INLINABLE threadCapability #-}

-- | Lifted version of 'C.yield'.
--
-- @since 0.1.1.0
yield :: MonadIO m => m ()
yield :: forall (m :: * -> *). MonadIO m => m ()
yield = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
C.yield
{-# INLINABLE yield #-}

-- | Lifted version of 'C.threadDelay'.
--
-- @since 0.1.1.0
threadDelay :: MonadIO m => Int -> m ()
threadDelay :: forall (m :: * -> *). MonadIO m => Int -> m ()
threadDelay = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
.  Int -> IO ()
C.threadDelay
{-# INLINABLE threadDelay #-}

-- | Lifted version of 'C.threadWaitRead'.
--
-- @since 0.1.1.0
threadWaitRead :: MonadIO m => Fd -> m ()
threadWaitRead :: forall (m :: * -> *). MonadIO m => Fd -> m ()
threadWaitRead = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fd -> IO ()
C.threadWaitRead
{-# INLINABLE threadWaitRead #-}

-- | Lifted version of 'C.threadWaitWrite'.
--
-- @since 0.1.1.0
threadWaitWrite :: MonadIO m => Fd -> m ()
threadWaitWrite :: forall (m :: * -> *). MonadIO m => Fd -> m ()
threadWaitWrite = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. Fd -> IO ()
C.threadWaitWrite
{-# INLINABLE threadWaitWrite #-}

-- | Unflifted version of 'C.forkOS'.
--
-- @since 0.1.1.0
forkOS :: MonadUnliftIO m => m () -> m ThreadId
forkOS :: forall (m :: * -> *). MonadUnliftIO m => m () -> m ThreadId
forkOS m ()
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> IO () -> IO ThreadId
C.forkOS forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m ()
m
{-# INLINABLE forkOS #-}

-- | Lifted version of 'C.isCurrentThreadBound'.
--
-- @since 0.1.1.0
isCurrentThreadBound :: MonadIO m => m Bool
isCurrentThreadBound :: forall (m :: * -> *). MonadIO m => m Bool
isCurrentThreadBound = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Bool
C.isCurrentThreadBound
{-# INLINABLE isCurrentThreadBound #-}

-- | Unlifted version of 'C.runInBoundThread'.
--
-- @since 0.1.1.0
runInBoundThread :: MonadUnliftIO m => m a -> m a
runInBoundThread :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
runInBoundThread m a
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. IO a -> IO a
C.runInBoundThread forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m a
m
{-# INLINABLE runInBoundThread #-}

-- | Unlifted version of 'C.runInUnboundThread'.
--
-- @since 0.1.1.0
runInUnboundThread :: MonadUnliftIO m => m a -> m a
runInUnboundThread :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
runInUnboundThread m a
m = forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO a
run -> forall a. IO a -> IO a
C.runInUnboundThread forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO a
run m a
m
{-# INLINABLE runInUnboundThread #-}

-- | Lifted version of 'C.mkWeakThreadId'.
--
-- @since 0.1.1.0
mkWeakThreadId :: MonadIO m => ThreadId -> m (Weak ThreadId)
mkWeakThreadId :: forall (m :: * -> *). MonadIO m => ThreadId -> m (Weak ThreadId)
mkWeakThreadId = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThreadId -> IO (Weak ThreadId)
C.mkWeakThreadId
{-# INLINABLE mkWeakThreadId #-}