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)

-- | A thread.
--
-- ==== __👉 Details__
--
-- * A thread's lifetime is delimited by the scope in which it was created.
--
-- * The thread that creates a scope is considered the parent of all threads created within it.
--
-- * If an exception is raised in a child thread, the child either propagates the exception to its parent (see
--   'Ki.fork'), or returns the exception as a value (see 'Ki.forkTry').
--
-- * All threads created within a scope are terminated when the scope closes.
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,
      -- If *they* are deadlocked, we will *both* will be delivered a wakeup from the RTS. We want to shrug this
      -- exception off, because afterwards they'll have put to the result var. But don't shield indefinitely, once will
      -- cover this use case and prevent any accidental infinite loops.
      $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
    }

-- | Wait for a thread to terminate.
await :: Thread a -> STM a
await :: forall a. Thread a -> STM a
await =
  Thread a -> STM a
forall a. Thread a -> STM a
await_