Skip to content

Commit

Permalink
Merge #3713
Browse files Browse the repository at this point in the history
3713: Add `yield` to `MonadFork` r=amesgen a=amesgen



Co-authored-by: Alexander Esgen <[email protected]>
  • Loading branch information
iohk-bors[bot] and amesgen authored Apr 19, 2022
2 parents 69f5e18 + df918cf commit 0e37994
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions io-classes/src/Control/Monad/Class/MonadFork.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class MonadThread m => MonadFork m where
killThread :: ThreadId m -> m ()
killThread tid = throwTo tid ThreadKilled

yield :: m ()

fork :: MonadFork m => m () -> m (ThreadId m)
fork = forkIO
{-# DEPRECATED fork "use forkIO" #-}
Expand All @@ -57,6 +59,7 @@ instance MonadFork IO where
forkIOWithUnmask = IO.forkIOWithUnmask
throwTo = IO.throwTo
killThread = IO.killThread
yield = IO.yield

instance MonadThread m => MonadThread (ReaderT r m) where
type ThreadId (ReaderT r m) = ThreadId m
Expand All @@ -70,6 +73,7 @@ instance MonadFork m => MonadFork (ReaderT e m) where
restore' (ReaderT f) = ReaderT $ restore . f
in runReaderT (k restore') e
throwTo e t = lift (throwTo e t)
yield = lift yield

-- | Apply the label to the current thread
labelThisThread :: MonadThread m => String -> m ()
Expand Down
4 changes: 4 additions & 0 deletions io-sim/src/Control/Monad/IOSim/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ schedule !thread@Thread{
return $ SimTrace time tid tlbl (EventThrowTo e tid')
$ trace

YieldSim k -> do
let thread' = thread { threadControl = ThreadControl k ctl }
deschedule Yield thread' simstate

-- ExploreRaces is ignored by this simulator
ExploreRaces k ->
{-# SCC "schedule.ExploreRaces" #-}
Expand Down
3 changes: 3 additions & 0 deletions io-sim/src/Control/Monad/IOSim/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ data SimA s a where
SetMaskState :: MaskingState -> IOSim s a -> (a -> SimA s b) -> SimA s b
GetMaskState :: (MaskingState -> SimA s b) -> SimA s b

YieldSim :: SimA s a -> SimA s a

ExploreRaces :: SimA s b -> SimA s b

Fix :: (x -> IOSim s x) -> (x -> SimA s r) -> SimA s r
Expand Down Expand Up @@ -371,6 +373,7 @@ instance MonadFork (IOSim s) where
forkIO task = IOSim $ oneShot $ \k -> Fork task k
forkIOWithUnmask f = forkIO (f unblock)
throwTo tid e = IOSim $ oneShot $ \k -> ThrowTo (toException e) tid (k ())
yield = IOSim $ oneShot $ \k -> YieldSim (k ())

instance MonadTest (IOSim s) where
exploreRaces = IOSim $ oneShot $ \k -> ExploreRaces (k ())
Expand Down
5 changes: 5 additions & 0 deletions io-sim/src/Control/Monad/IOSimPOR/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,11 @@ schedule thread@Thread{
return $ SimPORTrace time tid tstep tlbl (EventThrowTo e tid')
$ trace

-- intentionally a no-op (at least for now)
YieldSim k -> do
let thread' = thread { threadControl = ThreadControl k ctl }
schedule thread' simstate


threadInterruptible :: Thread s a -> Bool
threadInterruptible thread =
Expand Down
6 changes: 0 additions & 6 deletions io-sim/test/Test/IOSim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,6 @@ unit_async_10 =
)
===
["child 1", "child 2", "child 1 running", "parent done"]
where
yield :: IOSim s ()
yield = atomically (return ()) -- yield, go to end of runqueue


unit_async_11 =
Expand Down Expand Up @@ -934,9 +931,6 @@ unit_async_11 =
)
===
["child 1", "child 2", "child 1 running", "parent done"]
where
yield :: IOSim s ()
yield = atomically (return ()) -- yield, go to end of runqueue


unit_async_12 =
Expand Down

0 comments on commit 0e37994

Please sign in to comment.