Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add yield to MonadFork #3713

Merged
merged 1 commit into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ instance MonadFork m => MonadFork (WithEarlyExit m) where
unmask' = earlyExit . unmask . withEarlyExit
in collapse <$> withEarlyExit (f unmask')
throwTo = lift .: throwTo
yield = lift yield

instance MonadST m => MonadST (WithEarlyExit m) where
withLiftST f = lowerLiftST $ \(_proxy :: Proxy s) liftST ->
Expand Down