module Ki.Internal.Thread
( Thread,
makeThread,
await,
)
where
import Control.Concurrent (ThreadId)
import Control.Exception (BlockedIndefinitelyOnSTM (..))
import GHC.Conc (STM)
import Ki.Internal.IO (tryEitherSTM)
data Thread a = Thread
{ forall a. Thread a -> ThreadId
threadId :: {-# UNPACK #-} !ThreadId,
forall a. Thread a -> STM a
await_ :: !(STM a)
}
deriving stock ((forall a b. (a -> b) -> Thread a -> Thread b)
-> (forall a b. a -> Thread b -> Thread a) -> Functor Thread
forall a b. a -> Thread b -> Thread a
forall a b. (a -> b) -> Thread a -> Thread b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Thread a -> Thread b
fmap :: forall a b. (a -> b) -> Thread a -> Thread b
$c<$ :: forall a b. a -> Thread b -> Thread a
<$ :: forall a b. a -> Thread b -> Thread a
Functor)
instance Eq (Thread a) where
Thread ThreadId
ix STM a
_ == :: Thread a -> Thread a -> Bool
== Thread ThreadId
iy STM a
_ =
ThreadId
ix ThreadId -> ThreadId -> Bool
forall a. Eq a => a -> a -> Bool
== ThreadId
iy
instance Ord (Thread a) where
compare :: Thread a -> Thread a -> Ordering
compare (Thread ThreadId
ix STM a
_) (Thread ThreadId
iy STM a
_) =
ThreadId -> ThreadId -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ThreadId
ix ThreadId
iy
makeThread :: ThreadId -> STM a -> Thread a
makeThread :: forall a. ThreadId -> STM a -> Thread a
makeThread ThreadId
threadId STM a
action =
Thread
{ ThreadId
$sel:threadId:Thread :: ThreadId
threadId :: ThreadId
threadId,
$sel:await_:Thread :: STM a
await_ = (BlockedIndefinitelyOnSTM -> STM a)
-> (a -> STM a) -> STM a -> STM a
forall e b a.
Exception e =>
(e -> STM b) -> (a -> STM b) -> STM a -> STM b
tryEitherSTM (\BlockedIndefinitelyOnSTM
BlockedIndefinitelyOnSTM -> STM a
action) a -> STM a
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure STM a
action
}
await :: Thread a -> STM a
await :: forall a. Thread a -> STM a
await =
Thread a -> STM a
forall a. Thread a -> STM a
await_