{-# LANGUAGE PatternSynonyms #-}
module Ki.Implicit
(
Context,
withGlobalContext,
Scope,
scoped,
Scope.wait,
Scope.waitSTM,
waitFor,
Thread,
fork,
fork_,
forkWithUnmask,
forkWithUnmask_,
async,
asyncWithUnmask,
await,
awaitSTM,
awaitFor,
Scope.cancelScope,
cancelled,
cancelledSTM,
CancelToken,
Duration,
Duration.microseconds,
Duration.milliseconds,
Duration.seconds,
timeoutSTM,
sleep,
ThreadFailed (..),
)
where
import Ki.CancelToken (CancelToken)
import qualified Ki.Context as Context
import Ki.Duration (Duration)
import qualified Ki.Duration as Duration
import Ki.Prelude
import Ki.Scope (Scope)
import qualified Ki.Scope as Scope
import Ki.Thread (Thread)
import qualified Ki.Thread as Thread
import Ki.ThreadFailed (ThreadFailed (..))
import Ki.Timeout (timeoutSTM)
type Context =
?context :: Context.Context
async :: Scope -> (Context => IO a) -> IO (Thread (Either ThreadFailed a))
async :: Scope -> (Context => IO a) -> IO (Thread (Either ThreadFailed a))
async Scope
scope Context => IO a
action =
Scope -> IO a -> IO (Thread (Either ThreadFailed a))
forall a. Scope -> IO a -> IO (Thread (Either ThreadFailed a))
Thread.async Scope
scope (Scope -> (Context => IO a) -> IO a
forall a. Scope -> (Context => a) -> a
with Scope
scope Context => IO a
action)
asyncWithUnmask :: Scope -> (Context => (forall x. IO x -> IO x) -> IO a) -> IO (Thread (Either ThreadFailed a))
asyncWithUnmask :: Scope
-> (Context => (forall x. IO x -> IO x) -> IO a)
-> IO (Thread (Either ThreadFailed a))
asyncWithUnmask Scope
scope Context => (forall x. IO x -> IO x) -> IO a
action =
Scope
-> ((forall x. IO x -> IO x) -> IO a)
-> IO (Thread (Either ThreadFailed a))
forall a.
Scope
-> ((forall x. IO x -> IO x) -> IO a)
-> IO (Thread (Either ThreadFailed a))
Thread.asyncWithUnmask Scope
scope (let ?context = Scope.context scope in Context => (forall x. IO x -> IO x) -> IO a
(forall x. IO x -> IO x) -> IO a
action)
await :: Thread a -> IO a
await :: Thread a -> IO a
await =
Thread a -> IO a
forall a. Thread a -> IO a
Thread.await
awaitSTM :: Thread a -> STM a
awaitSTM :: Thread a -> STM a
awaitSTM =
Thread a -> STM a
forall a. Thread a -> STM a
Thread.awaitSTM
awaitFor :: Thread a -> Duration -> IO (Maybe a)
awaitFor :: Thread a -> Duration -> IO (Maybe a)
awaitFor =
Thread a -> Duration -> IO (Maybe a)
forall a. Thread a -> Duration -> IO (Maybe a)
Thread.awaitFor
cancelled :: Context => IO (Maybe CancelToken)
cancelled :: IO (Maybe CancelToken)
cancelled =
STM (Maybe CancelToken) -> IO (Maybe CancelToken)
forall a. STM a -> IO a
atomically (STM CancelToken -> STM (Maybe CancelToken)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional STM CancelToken
Context => STM CancelToken
cancelledSTM)
cancelledSTM :: Context => STM CancelToken
cancelledSTM :: STM CancelToken
cancelledSTM =
Context -> STM CancelToken
Context.contextCancelTokenSTM Context
Context
?context
fork :: Scope -> (Context => IO a) -> IO (Thread a)
fork :: Scope -> (Context => IO a) -> IO (Thread a)
fork Scope
scope Context => IO a
action =
Scope -> IO a -> IO (Thread a)
forall a. Scope -> IO a -> IO (Thread a)
Thread.fork Scope
scope (Scope -> (Context => IO a) -> IO a
forall a. Scope -> (Context => a) -> a
with Scope
scope Context => IO a
action)
fork_ :: Scope -> (Context => IO ()) -> IO ()
fork_ :: Scope -> (Context => IO ()) -> IO ()
fork_ Scope
scope Context => IO ()
action =
Scope -> IO () -> IO ()
Thread.fork_ Scope
scope (Scope -> (Context => IO ()) -> IO ()
forall a. Scope -> (Context => a) -> a
with Scope
scope Context => IO ()
action)
forkWithUnmask :: Scope -> (Context => (forall x. IO x -> IO x) -> IO a) -> IO (Thread a)
forkWithUnmask :: Scope
-> (Context => (forall x. IO x -> IO x) -> IO a) -> IO (Thread a)
forkWithUnmask Scope
scope Context => (forall x. IO x -> IO x) -> IO a
action =
Scope -> ((forall x. IO x -> IO x) -> IO a) -> IO (Thread a)
forall a.
Scope -> ((forall x. IO x -> IO x) -> IO a) -> IO (Thread a)
Thread.forkWithUnmask Scope
scope (let ?context = Scope.context scope in Context => (forall x. IO x -> IO x) -> IO a
(forall x. IO x -> IO x) -> IO a
action)
forkWithUnmask_ :: Scope -> (Context => (forall x. IO x -> IO x) -> IO ()) -> IO ()
forkWithUnmask_ :: Scope -> (Context => (forall x. IO x -> IO x) -> IO ()) -> IO ()
forkWithUnmask_ Scope
scope Context => (forall x. IO x -> IO x) -> IO ()
action =
Scope -> ((forall x. IO x -> IO x) -> IO ()) -> IO ()
Thread.forkWithUnmask_ Scope
scope (let ?context = Scope.context scope in Context => (forall x. IO x -> IO x) -> IO ()
(forall x. IO x -> IO x) -> IO ()
action)
withGlobalContext :: (Context => IO a) -> IO a
withGlobalContext :: (Context => IO a) -> IO a
withGlobalContext Context => IO a
action =
let ?context = Context.globalContext in IO a
Context => IO a
action
scoped :: Context => (Context => Scope -> IO a) -> IO a
scoped :: (Context => Scope -> IO a) -> IO a
scoped Context => Scope -> IO a
action =
Context -> (Scope -> IO a) -> IO a
forall a. Context -> (Scope -> IO a) -> IO a
Scope.scoped Context
Context
?context \Scope
scope -> Scope -> (Context => IO a) -> IO a
forall a. Scope -> (Context => a) -> a
with Scope
scope (Context => Scope -> IO a
Scope -> IO a
action Scope
scope)
sleep :: Context => Duration -> IO ()
sleep :: Duration -> IO ()
sleep Duration
duration =
Duration -> STM (IO ()) -> IO () -> IO ()
forall a. Duration -> STM (IO a) -> IO a -> IO a
timeoutSTM Duration
duration (STM CancelToken
Context => STM CancelToken
cancelledSTM STM CancelToken -> (CancelToken -> STM (IO ())) -> STM (IO ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CancelToken -> STM (IO ())
forall e a. Exception e => e -> STM a
throwSTM) (() -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
waitFor :: Scope -> Duration -> IO ()
waitFor :: Scope -> Duration -> IO ()
waitFor =
Scope -> Duration -> IO ()
Scope.waitFor
with :: Scope -> (Context => a) -> a
with :: Scope -> (Context => a) -> a
with Scope
scope Context => a
action =
let ?context = Scope.context scope in a
Context => a
action