module Ki.Internal.Scope
( Scope,
scoped,
awaitAll,
fork,
forkWith,
forkWith_,
fork_,
forkTry,
forkTryWith,
)
where
import Control.Concurrent (ThreadId, myThreadId, throwTo)
import Control.Concurrent.MVar (MVar, newEmptyMVar, tryPutMVar, tryTakeMVar)
import Control.Exception
( Exception (fromException, toException),
MaskingState (..),
SomeException,
assert,
asyncExceptionFromException,
asyncExceptionToException,
throwIO,
try,
uninterruptibleMask,
pattern ErrorCall,
)
import Control.Monad (when, guard)
import Data.Foldable (for_)
import Data.Functor (void)
import Data.IntMap (IntMap)
import qualified Data.IntMap.Lazy as IntMap.Lazy
import Data.Void (Void, absurd)
import GHC.Conc
( STM,
TVar,
atomically,
enableAllocationLimit,
labelThread,
newTVarIO,
readTVar,
retry,
setAllocationCounter,
throwSTM,
writeTVar,
)
import GHC.Conc.Sync (readTVarIO)
import GHC.IO (unsafeUnmask)
import Ki.Internal.ByteCount
import Ki.Internal.Counter
import Ki.Internal.IO
( IOResult (..),
UnexceptionalIO (..),
interruptiblyMasked,
isAsyncException,
unexceptionalTry,
unexceptionalTryEither,
uninterruptiblyMasked,
)
import Ki.Internal.Thread
import Data.Maybe (isJust)
data Scope = Scope
{
Scope -> MVar SomeException
childExceptionVar :: {-# UNPACK #-} !(MVar SomeException),
Scope -> TVar (IntMap ThreadId)
childrenVar :: {-# UNPACK #-} !(TVar (IntMap ThreadId)),
Scope -> Counter
nextChildIdCounter :: {-# UNPACK #-} !Counter,
Scope -> ThreadId
parentThreadId :: {-# UNPACK #-} !ThreadId,
Scope -> TVar ScopeStatus
statusVar :: {-# UNPACK #-} !(TVar ScopeStatus)
}
type ScopeStatus = Int
pattern Open :: Int
pattern $mOpen :: forall {r}. ScopeStatus -> ((# #) -> r) -> ((# #) -> r) -> r
Open <- ((>= 0) -> True)
pattern Closing :: Int
pattern $mClosing :: forall {r}. ScopeStatus -> ((# #) -> r) -> ((# #) -> r) -> r
$bClosing :: ScopeStatus
Closing = -1
pattern Closed :: Int
pattern $mClosed :: forall {r}. ScopeStatus -> ((# #) -> r) -> ((# #) -> r) -> r
$bClosed :: ScopeStatus
Closed = -2
{-# COMPLETE Open, Closing, Closed #-}
data ScopeClosing
= ScopeClosing
instance Show ScopeClosing where
show :: ScopeClosing -> String
show ScopeClosing
_ = String
"ScopeClosing"
instance Exception ScopeClosing where
toException :: ScopeClosing -> SomeException
toException = ScopeClosing -> SomeException
forall e. Exception e => e -> SomeException
asyncExceptionToException
fromException :: SomeException -> Maybe ScopeClosing
fromException = SomeException -> Maybe ScopeClosing
forall e. Exception e => SomeException -> Maybe e
asyncExceptionFromException
isScopeClosingException :: SomeException -> Bool
isScopeClosingException :: SomeException -> Bool
isScopeClosingException SomeException
exception =
Maybe ScopeClosing -> Bool
forall a. Maybe a -> Bool
isJust (forall e. Exception e => SomeException -> Maybe e
fromException @ScopeClosing SomeException
exception)
pattern IsScopeClosingException :: SomeException
pattern $mIsScopeClosingException :: forall {r}. SomeException -> ((# #) -> r) -> ((# #) -> r) -> r
IsScopeClosingException <- (isScopeClosingException -> True)
scoped :: (Scope -> IO a) -> IO a
scoped :: forall a. (Scope -> IO a) -> IO a
scoped Scope -> IO a
action = do
scope :: Scope
scope@Scope {MVar SomeException
$sel:childExceptionVar:Scope :: Scope -> MVar SomeException
childExceptionVar :: MVar SomeException
childExceptionVar, TVar (IntMap ThreadId)
$sel:childrenVar:Scope :: Scope -> TVar (IntMap ThreadId)
childrenVar :: TVar (IntMap ThreadId)
childrenVar, TVar ScopeStatus
$sel:statusVar:Scope :: Scope -> TVar ScopeStatus
statusVar :: TVar ScopeStatus
statusVar} <- IO Scope
allocateScope
((forall a. IO a -> IO a) -> IO a) -> IO a
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
uninterruptibleMask \forall a. IO a -> IO a
restore -> do
Either SomeException a
result <- IO a -> IO (Either SomeException a)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO a -> IO a
forall a. IO a -> IO a
restore (Scope -> IO a
action Scope
scope))
!IntMap ThreadId
livingChildren <- do
IntMap ThreadId
livingChildren0 <-
STM (IntMap ThreadId) -> IO (IntMap ThreadId)
forall a. STM a -> IO a
atomically do
ScopeStatus
n <- TVar ScopeStatus -> STM ScopeStatus
forall a. TVar a -> STM a
readTVar TVar ScopeStatus
statusVar
Bool -> STM () -> STM ()
forall a. HasCallStack => Bool -> a -> a
assert (ScopeStatus
n ScopeStatus -> ScopeStatus -> Bool
forall a. Ord a => a -> a -> Bool
>= ScopeStatus
0) (Bool -> STM ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (ScopeStatus
n ScopeStatus -> ScopeStatus -> Bool
forall a. Eq a => a -> a -> Bool
== ScopeStatus
0))
TVar ScopeStatus -> ScopeStatus -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar ScopeStatus
statusVar ScopeStatus
Closing
TVar (IntMap ThreadId) -> STM (IntMap ThreadId)
forall a. TVar a -> STM a
readTVar TVar (IntMap ThreadId)
childrenVar
IntMap ThreadId -> IO (IntMap ThreadId)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure case Either SomeException a
result of
Left (SomeException -> Maybe ThreadFailed
forall e. Exception e => SomeException -> Maybe e
fromException -> Just ThreadFailed {ScopeStatus
childId :: ScopeStatus
$sel:childId:ThreadFailed :: ThreadFailed -> ScopeStatus
childId}) -> ScopeStatus -> IntMap ThreadId -> IntMap ThreadId
forall a. ScopeStatus -> IntMap a -> IntMap a
IntMap.Lazy.delete ScopeStatus
childId IntMap ThreadId
livingChildren0
Either SomeException a
_ -> IntMap ThreadId
livingChildren0
[ThreadId] -> (ThreadId -> IO ()) -> IO ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (IntMap ThreadId -> [ThreadId]
forall a. IntMap a -> [a]
IntMap.Lazy.elems IntMap ThreadId
livingChildren) \ThreadId
livingChild -> ThreadId -> ScopeClosing -> IO ()
forall e. Exception e => ThreadId -> e -> IO ()
throwTo ThreadId
livingChild ScopeClosing
ScopeClosing
STM () -> IO ()
forall a. STM a -> IO a
atomically do
TVar (IntMap ThreadId) -> STM ()
forall a. TVar (IntMap a) -> STM ()
blockUntilEmpty TVar (IntMap ThreadId)
childrenVar
TVar ScopeStatus -> ScopeStatus -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar ScopeStatus
statusVar ScopeStatus
Closed
case Either SomeException a
result of
Left SomeException
exception -> SomeException -> IO a
forall e a. Exception e => e -> IO a
throwIO (SomeException -> SomeException
unwrapThreadFailed SomeException
exception)
Right a
value ->
MVar SomeException -> IO (Maybe SomeException)
forall a. MVar a -> IO (Maybe a)
tryTakeMVar MVar SomeException
childExceptionVar IO (Maybe SomeException) -> (Maybe SomeException -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe SomeException
Nothing -> a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value
Just SomeException
exception -> SomeException -> IO a
forall e a. Exception e => e -> IO a
throwIO SomeException
exception
allocateScope :: IO Scope
allocateScope :: IO Scope
allocateScope = do
MVar SomeException
childExceptionVar <- IO (MVar SomeException)
forall a. IO (MVar a)
newEmptyMVar
TVar (IntMap ThreadId)
childrenVar <- IntMap ThreadId -> IO (TVar (IntMap ThreadId))
forall a. a -> IO (TVar a)
newTVarIO IntMap ThreadId
forall a. IntMap a
IntMap.Lazy.empty
Counter
nextChildIdCounter <- IO Counter
newCounter
ThreadId
parentThreadId <- IO ThreadId
myThreadId
TVar ScopeStatus
statusVar <- ScopeStatus -> IO (TVar ScopeStatus)
forall a. a -> IO (TVar a)
newTVarIO ScopeStatus
0
Scope -> IO Scope
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Scope {MVar SomeException
$sel:childExceptionVar:Scope :: MVar SomeException
childExceptionVar :: MVar SomeException
childExceptionVar, TVar (IntMap ThreadId)
$sel:childrenVar:Scope :: TVar (IntMap ThreadId)
childrenVar :: TVar (IntMap ThreadId)
childrenVar, Counter
$sel:nextChildIdCounter:Scope :: Counter
nextChildIdCounter :: Counter
nextChildIdCounter, ThreadId
$sel:parentThreadId:Scope :: ThreadId
parentThreadId :: ThreadId
parentThreadId, TVar ScopeStatus
$sel:statusVar:Scope :: TVar ScopeStatus
statusVar :: TVar ScopeStatus
statusVar}
spawn :: Scope -> ThreadOptions -> (Tid -> (forall x. IO x -> IO x) -> UnexceptionalIO ()) -> IO ThreadId
spawn :: Scope
-> ThreadOptions
-> (ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ())
-> IO ThreadId
spawn
Scope {TVar (IntMap ThreadId)
$sel:childrenVar:Scope :: Scope -> TVar (IntMap ThreadId)
childrenVar :: TVar (IntMap ThreadId)
childrenVar, Counter
$sel:nextChildIdCounter:Scope :: Scope -> Counter
nextChildIdCounter :: Counter
nextChildIdCounter, TVar ScopeStatus
$sel:statusVar:Scope :: Scope -> TVar ScopeStatus
statusVar :: TVar ScopeStatus
statusVar}
ThreadOptions {ThreadAffinity
affinity :: ThreadAffinity
$sel:affinity:ThreadOptions :: ThreadOptions -> ThreadAffinity
affinity, Maybe ByteCount
allocationLimit :: Maybe ByteCount
$sel:allocationLimit:ThreadOptions :: ThreadOptions -> Maybe ByteCount
allocationLimit, String
label :: String
$sel:label:ThreadOptions :: ThreadOptions -> String
label, $sel:maskingState:ThreadOptions :: ThreadOptions -> MaskingState
maskingState = MaskingState
requestedChildMaskingState}
ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ()
action = do
IO ThreadId -> IO ThreadId
forall a. IO a -> IO a
interruptiblyMasked do
STM () -> IO ()
forall a. STM a -> IO a
atomically do
ScopeStatus
n <- TVar ScopeStatus -> STM ScopeStatus
forall a. TVar a -> STM a
readTVar TVar ScopeStatus
statusVar
Bool -> STM () -> STM ()
forall a. HasCallStack => Bool -> a -> a
assert (ScopeStatus
n ScopeStatus -> ScopeStatus -> Bool
forall a. Ord a => a -> a -> Bool
>= -ScopeStatus
2) do
case ScopeStatus
n of
ScopeStatus
Open -> TVar ScopeStatus -> ScopeStatus -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar ScopeStatus
statusVar (ScopeStatus -> STM ()) -> ScopeStatus -> STM ()
forall a b. (a -> b) -> a -> b
$! ScopeStatus
n ScopeStatus -> ScopeStatus -> ScopeStatus
forall a. Num a => a -> a -> a
+ ScopeStatus
1
ScopeStatus
Closing -> ScopeClosing -> STM ()
forall e a. Exception e => e -> STM a
throwSTM ScopeClosing
ScopeClosing
ScopeStatus
Closed -> ErrorCall -> STM ()
forall e a. Exception e => e -> STM a
throwSTM (String -> ErrorCall
ErrorCall String
"ki: scope closed")
ScopeStatus
childId <- Counter -> IO ScopeStatus
incrCounter Counter
nextChildIdCounter
ThreadId
childThreadId <-
ThreadAffinity -> IO () -> IO ThreadId
forkWithAffinity ThreadAffinity
affinity do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
label)) do
ThreadId
childThreadId <- IO ThreadId
myThreadId
ThreadId -> String -> IO ()
labelThread ThreadId
childThreadId String
label
case Maybe ByteCount
allocationLimit of
Maybe ByteCount
Nothing -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Just ByteCount
bytes -> do
Int64 -> IO ()
setAllocationCounter (ByteCount -> Int64
byteCountToInt64 ByteCount
bytes)
IO ()
enableAllocationLimit
let
atRequestedMaskingState :: IO a -> IO a
atRequestedMaskingState :: forall a. IO a -> IO a
atRequestedMaskingState =
case MaskingState
requestedChildMaskingState of
MaskingState
Unmasked -> IO a -> IO a
forall a. IO a -> IO a
unsafeUnmask
MaskingState
MaskedInterruptible -> IO a -> IO a
forall a. a -> a
id
MaskingState
MaskedUninterruptible -> IO a -> IO a
forall a. IO a -> IO a
uninterruptiblyMasked
UnexceptionalIO () -> IO ()
forall a. UnexceptionalIO a -> IO a
runUnexceptionalIO (ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ()
action ScopeStatus
childId IO x -> IO x
forall a. IO a -> IO a
atRequestedMaskingState)
STM () -> IO ()
forall a. STM a -> IO a
atomically (TVar (IntMap ThreadId) -> ScopeStatus -> STM ()
unrecordChild TVar (IntMap ThreadId)
childrenVar ScopeStatus
childId)
STM () -> IO ()
forall a. STM a -> IO a
atomically do
ScopeStatus
n <- TVar ScopeStatus -> STM ScopeStatus
forall a. TVar a -> STM a
readTVar TVar ScopeStatus
statusVar
TVar ScopeStatus -> ScopeStatus -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar ScopeStatus
statusVar (ScopeStatus -> STM ()) -> ScopeStatus -> STM ()
forall a b. (a -> b) -> a -> b
$! ScopeStatus
n ScopeStatus -> ScopeStatus -> ScopeStatus
forall a. Num a => a -> a -> a
- ScopeStatus
1
TVar (IntMap ThreadId) -> ScopeStatus -> ThreadId -> STM ()
recordChild TVar (IntMap ThreadId)
childrenVar ScopeStatus
childId ThreadId
childThreadId
ThreadId -> IO ThreadId
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ThreadId
childThreadId
recordChild :: TVar (IntMap ThreadId) -> Tid -> ThreadId -> STM ()
recordChild :: TVar (IntMap ThreadId) -> ScopeStatus -> ThreadId -> STM ()
recordChild TVar (IntMap ThreadId)
childrenVar ScopeStatus
childId ThreadId
childThreadId = do
IntMap ThreadId
children <- TVar (IntMap ThreadId) -> STM (IntMap ThreadId)
forall a. TVar a -> STM a
readTVar TVar (IntMap ThreadId)
childrenVar
TVar (IntMap ThreadId) -> IntMap ThreadId -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (IntMap ThreadId)
childrenVar (IntMap ThreadId -> STM ()) -> IntMap ThreadId -> STM ()
forall a b. (a -> b) -> a -> b
$! (Maybe ThreadId -> Maybe ThreadId)
-> ScopeStatus -> IntMap ThreadId -> IntMap ThreadId
forall a.
(Maybe a -> Maybe a) -> ScopeStatus -> IntMap a -> IntMap a
IntMap.Lazy.alter (Maybe ThreadId
-> (ThreadId -> Maybe ThreadId) -> Maybe ThreadId -> Maybe ThreadId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ThreadId -> Maybe ThreadId
forall a. a -> Maybe a
Just ThreadId
childThreadId) (Maybe ThreadId -> ThreadId -> Maybe ThreadId
forall a b. a -> b -> a
const Maybe ThreadId
forall a. Maybe a
Nothing)) ScopeStatus
childId IntMap ThreadId
children
unrecordChild :: TVar (IntMap ThreadId) -> Tid -> STM ()
unrecordChild :: TVar (IntMap ThreadId) -> ScopeStatus -> STM ()
unrecordChild TVar (IntMap ThreadId)
childrenVar ScopeStatus
childId = do
IntMap ThreadId
children <- TVar (IntMap ThreadId) -> STM (IntMap ThreadId)
forall a. TVar a -> STM a
readTVar TVar (IntMap ThreadId)
childrenVar
TVar (IntMap ThreadId) -> IntMap ThreadId -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (IntMap ThreadId)
childrenVar (IntMap ThreadId -> STM ()) -> IntMap ThreadId -> STM ()
forall a b. (a -> b) -> a -> b
$! (Maybe ThreadId -> Maybe ThreadId)
-> ScopeStatus -> IntMap ThreadId -> IntMap ThreadId
forall a.
(Maybe a -> Maybe a) -> ScopeStatus -> IntMap a -> IntMap a
IntMap.Lazy.alter (Maybe ThreadId
-> (ThreadId -> Maybe ThreadId) -> Maybe ThreadId -> Maybe ThreadId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ThreadId -> Maybe ThreadId
forall a. a -> Maybe a
Just ThreadId
forall a. HasCallStack => a
undefined) (Maybe ThreadId -> ThreadId -> Maybe ThreadId
forall a b. a -> b -> a
const Maybe ThreadId
forall a. Maybe a
Nothing)) ScopeStatus
childId IntMap ThreadId
children
awaitAll :: Scope -> STM ()
awaitAll :: Scope -> STM ()
awaitAll Scope {TVar (IntMap ThreadId)
$sel:childrenVar:Scope :: Scope -> TVar (IntMap ThreadId)
childrenVar :: TVar (IntMap ThreadId)
childrenVar, TVar ScopeStatus
$sel:statusVar:Scope :: Scope -> TVar ScopeStatus
statusVar :: TVar ScopeStatus
statusVar} = do
TVar (IntMap ThreadId) -> STM ()
forall a. TVar (IntMap a) -> STM ()
blockUntilEmpty TVar (IntMap ThreadId)
childrenVar
ScopeStatus
n <- TVar ScopeStatus -> STM ScopeStatus
forall a. TVar a -> STM a
readTVar TVar ScopeStatus
statusVar
case ScopeStatus
n of
ScopeStatus
Open -> Bool -> STM ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (ScopeStatus
n ScopeStatus -> ScopeStatus -> Bool
forall a. Eq a => a -> a -> Bool
== ScopeStatus
0)
ScopeStatus
Closing -> STM ()
forall a. STM a
retry
ScopeStatus
Closed -> () -> STM ()
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
blockUntilEmpty :: TVar (IntMap a) -> STM ()
blockUntilEmpty :: forall a. TVar (IntMap a) -> STM ()
blockUntilEmpty TVar (IntMap a)
var = do
IntMap a
x <- TVar (IntMap a) -> STM (IntMap a)
forall a. TVar a -> STM a
readTVar TVar (IntMap a)
var
Bool -> STM ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (IntMap a -> Bool
forall a. IntMap a -> Bool
IntMap.Lazy.null IntMap a
x)
fork :: Scope -> IO a -> IO (Thread a)
fork :: forall a. Scope -> IO a -> IO (Thread a)
fork Scope
scope =
Scope -> ThreadOptions -> IO a -> IO (Thread a)
forall a. Scope -> ThreadOptions -> IO a -> IO (Thread a)
forkWith Scope
scope ThreadOptions
defaultThreadOptions
fork_ :: Scope -> IO Void -> IO ()
fork_ :: Scope -> IO Void -> IO ()
fork_ Scope
scope =
Scope -> ThreadOptions -> IO Void -> IO ()
forkWith_ Scope
scope ThreadOptions
defaultThreadOptions
forkWith :: Scope -> ThreadOptions -> IO a -> IO (Thread a)
forkWith :: forall a. Scope -> ThreadOptions -> IO a -> IO (Thread a)
forkWith Scope
scope ThreadOptions
opts IO a
action = do
TVar (Result a)
resultVar <- Result a -> IO (TVar (Result a))
forall a. a -> IO (TVar a)
newTVarIO Result a
forall a. Result a
NoResultYet
let done :: Result a -> UnexceptionalIO ()
done Result a
result = IO () -> UnexceptionalIO ()
forall a. IO a -> UnexceptionalIO a
UnexceptionalIO (STM () -> IO ()
forall a. STM a -> IO a
atomically (TVar (Result a) -> Result a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Result a)
resultVar Result a
result))
ThreadId
ident <-
Scope
-> ThreadOptions
-> (ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ())
-> IO ThreadId
spawn Scope
scope ThreadOptions
opts \ScopeStatus
childId forall a. IO a -> IO a
masking -> do
IOResult a
result <- IO a -> UnexceptionalIO (IOResult a)
forall a. IO a -> UnexceptionalIO (IOResult a)
unexceptionalTry (IO a -> IO a
forall a. IO a -> IO a
masking IO a
action)
case IOResult a
result of
Failure SomeException
exception -> do
Bool -> UnexceptionalIO () -> UnexceptionalIO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when
(Bool -> Bool
not (SomeException -> Bool
isScopeClosingException SomeException
exception))
(Scope -> ScopeStatus -> SomeException -> UnexceptionalIO ()
propagateException Scope
scope ScopeStatus
childId SomeException
exception)
Result a -> UnexceptionalIO ()
done (SomeException -> Result a
forall a. SomeException -> Result a
BadResult SomeException
exception)
Success a
value -> Result a -> UnexceptionalIO ()
done (a -> Result a
forall a. a -> Result a
GoodResult a
value)
let doAwait :: STM a
doAwait =
TVar (Result a) -> STM (Result a)
forall a. TVar a -> STM a
readTVar TVar (Result a)
resultVar STM (Result a) -> (Result a -> STM a) -> STM a
forall a b. STM a -> (a -> STM b) -> STM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Result a
NoResultYet -> STM a
forall a. STM a
retry
BadResult SomeException
exception -> SomeException -> STM a
forall e a. Exception e => e -> STM a
throwSTM SomeException
exception
GoodResult a
value -> a -> STM a
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value
Thread a -> IO (Thread a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ThreadId -> STM a -> Thread a
forall a. ThreadId -> STM a -> Thread a
makeThread ThreadId
ident STM a
doAwait)
forkWith_ :: Scope -> ThreadOptions -> IO Void -> IO ()
forkWith_ :: Scope -> ThreadOptions -> IO Void -> IO ()
forkWith_ Scope
scope ThreadOptions
opts IO Void
action = do
ThreadId
_childThreadId <-
Scope
-> ThreadOptions
-> (ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ())
-> IO ThreadId
spawn Scope
scope ThreadOptions
opts \ScopeStatus
childId forall a. IO a -> IO a
masking ->
(SomeException -> UnexceptionalIO ())
-> (Void -> UnexceptionalIO ()) -> IO Void -> UnexceptionalIO ()
forall a b.
(SomeException -> UnexceptionalIO b)
-> (a -> UnexceptionalIO b) -> IO a -> UnexceptionalIO b
unexceptionalTryEither
(\SomeException
exception -> Bool -> UnexceptionalIO () -> UnexceptionalIO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (SomeException -> Bool
isScopeClosingException SomeException
exception)) (Scope -> ScopeStatus -> SomeException -> UnexceptionalIO ()
propagateException Scope
scope ScopeStatus
childId SomeException
exception))
Void -> UnexceptionalIO ()
forall a. Void -> a
absurd
(IO Void -> IO Void
forall a. IO a -> IO a
masking IO Void
action)
() -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
forkTry :: forall e a. Exception e => Scope -> IO a -> IO (Thread (Either e a))
forkTry :: forall e a.
Exception e =>
Scope -> IO a -> IO (Thread (Either e a))
forkTry Scope
scope =
Scope -> ThreadOptions -> IO a -> IO (Thread (Either e a))
forall e a.
Exception e =>
Scope -> ThreadOptions -> IO a -> IO (Thread (Either e a))
forkTryWith Scope
scope ThreadOptions
defaultThreadOptions
data Result a
= NoResultYet
| BadResult !SomeException
| GoodResult a
forkTryWith :: forall e a. Exception e => Scope -> ThreadOptions -> IO a -> IO (Thread (Either e a))
forkTryWith :: forall e a.
Exception e =>
Scope -> ThreadOptions -> IO a -> IO (Thread (Either e a))
forkTryWith Scope
scope ThreadOptions
opts IO a
action = do
TVar (Result a)
resultVar <- Result a -> IO (TVar (Result a))
forall a. a -> IO (TVar a)
newTVarIO Result a
forall a. Result a
NoResultYet
let done :: Result a -> UnexceptionalIO ()
done Result a
result = IO () -> UnexceptionalIO ()
forall a. IO a -> UnexceptionalIO a
UnexceptionalIO (STM () -> IO ()
forall a. STM a -> IO a
atomically (TVar (Result a) -> Result a -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar TVar (Result a)
resultVar Result a
result))
ThreadId
childThreadId <-
Scope
-> ThreadOptions
-> (ScopeStatus -> (forall a. IO a -> IO a) -> UnexceptionalIO ())
-> IO ThreadId
spawn Scope
scope ThreadOptions
opts \ScopeStatus
childId forall a. IO a -> IO a
masking -> do
IOResult a
result <- IO a -> UnexceptionalIO (IOResult a)
forall a. IO a -> UnexceptionalIO (IOResult a)
unexceptionalTry (IO a -> IO a
forall a. IO a -> IO a
masking IO a
action)
case IOResult a
result of
Failure SomeException
exception -> do
let shouldPropagate :: Bool
shouldPropagate =
if SomeException -> Bool
isScopeClosingException SomeException
exception
then Bool
False
else case forall e. Exception e => SomeException -> Maybe e
fromException @e SomeException
exception of
Maybe e
Nothing -> Bool
True
Just e
_ -> SomeException -> Bool
isAsyncException SomeException
exception
Bool -> UnexceptionalIO () -> UnexceptionalIO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
shouldPropagate (Scope -> ScopeStatus -> SomeException -> UnexceptionalIO ()
propagateException Scope
scope ScopeStatus
childId SomeException
exception)
Result a -> UnexceptionalIO ()
done (SomeException -> Result a
forall a. SomeException -> Result a
BadResult SomeException
exception)
Success a
value -> Result a -> UnexceptionalIO ()
done (a -> Result a
forall a. a -> Result a
GoodResult a
value)
let doAwait :: STM (Either e a)
doAwait =
TVar (Result a) -> STM (Result a)
forall a. TVar a -> STM a
readTVar TVar (Result a)
resultVar STM (Result a)
-> (Result a -> STM (Either e a)) -> STM (Either e a)
forall a b. STM a -> (a -> STM b) -> STM b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Result a
NoResultYet -> STM (Either e a)
forall a. STM a
retry
BadResult SomeException
exception ->
case forall e. Exception e => SomeException -> Maybe e
fromException @e SomeException
exception of
Maybe e
Nothing -> SomeException -> STM (Either e a)
forall e a. Exception e => e -> STM a
throwSTM SomeException
exception
Just e
expectedException -> Either e a -> STM (Either e a)
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (e -> Either e a
forall a b. a -> Either a b
Left e
expectedException)
GoodResult a
value -> Either e a -> STM (Either e a)
forall a. a -> STM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Either e a
forall a b. b -> Either a b
Right a
value)
Thread (Either e a) -> IO (Thread (Either e a))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ThreadId -> STM (Either e a) -> Thread (Either e a)
forall a. ThreadId -> STM a -> Thread a
makeThread ThreadId
childThreadId STM (Either e a)
doAwait)
propagateException :: Scope -> Tid -> SomeException -> UnexceptionalIO ()
propagateException :: Scope -> ScopeStatus -> SomeException -> UnexceptionalIO ()
propagateException Scope {MVar SomeException
$sel:childExceptionVar:Scope :: Scope -> MVar SomeException
childExceptionVar :: MVar SomeException
childExceptionVar, ThreadId
$sel:parentThreadId:Scope :: Scope -> ThreadId
parentThreadId :: ThreadId
parentThreadId, TVar ScopeStatus
$sel:statusVar:Scope :: Scope -> TVar ScopeStatus
statusVar :: TVar ScopeStatus
statusVar} ScopeStatus
childId SomeException
exception =
IO ScopeStatus -> UnexceptionalIO ScopeStatus
forall a. IO a -> UnexceptionalIO a
UnexceptionalIO (TVar ScopeStatus -> IO ScopeStatus
forall a. TVar a -> IO a
readTVarIO TVar ScopeStatus
statusVar) UnexceptionalIO ScopeStatus
-> (ScopeStatus -> UnexceptionalIO ()) -> UnexceptionalIO ()
forall a b.
UnexceptionalIO a -> (a -> UnexceptionalIO b) -> UnexceptionalIO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
ScopeStatus
Closing -> UnexceptionalIO ()
tryPutChildExceptionVar
ScopeStatus
_ -> UnexceptionalIO ()
loop
where
loop :: UnexceptionalIO ()
loop :: UnexceptionalIO ()
loop =
IO () -> UnexceptionalIO (IOResult ())
forall a. IO a -> UnexceptionalIO (IOResult a)
unexceptionalTry (ThreadId -> ThreadFailed -> IO ()
forall e. Exception e => ThreadId -> e -> IO ()
throwTo ThreadId
parentThreadId ThreadFailed {ScopeStatus
$sel:childId:ThreadFailed :: ScopeStatus
childId :: ScopeStatus
childId, SomeException
exception :: SomeException
$sel:exception:ThreadFailed :: SomeException
exception}) UnexceptionalIO (IOResult ())
-> (IOResult () -> UnexceptionalIO ()) -> UnexceptionalIO ()
forall a b.
UnexceptionalIO a -> (a -> UnexceptionalIO b) -> UnexceptionalIO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Failure SomeException
IsScopeClosingException -> UnexceptionalIO ()
tryPutChildExceptionVar
Failure SomeException
_ -> UnexceptionalIO ()
loop
Success ()
_ -> () -> UnexceptionalIO ()
forall a. a -> UnexceptionalIO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
tryPutChildExceptionVar :: UnexceptionalIO ()
tryPutChildExceptionVar :: UnexceptionalIO ()
tryPutChildExceptionVar =
IO () -> UnexceptionalIO ()
forall a. IO a -> UnexceptionalIO a
UnexceptionalIO (IO Bool -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (MVar SomeException -> SomeException -> IO Bool
forall a. MVar a -> a -> IO Bool
tryPutMVar MVar SomeException
childExceptionVar SomeException
exception))