Skip to content

Commit df918cf

Browse files
committed
Add yield to MonadFork
1 parent 69f5e18 commit df918cf

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

io-classes/src/Control/Monad/Class/MonadFork.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class MonadThread m => MonadFork m where
3838
killThread :: ThreadId m -> m ()
3939
killThread tid = throwTo tid ThreadKilled
4040

41+
yield :: m ()
42+
4143
fork :: MonadFork m => m () -> m (ThreadId m)
4244
fork = forkIO
4345
{-# DEPRECATED fork "use forkIO" #-}
@@ -57,6 +59,7 @@ instance MonadFork IO where
5759
forkIOWithUnmask = IO.forkIOWithUnmask
5860
throwTo = IO.throwTo
5961
killThread = IO.killThread
62+
yield = IO.yield
6063

6164
instance MonadThread m => MonadThread (ReaderT r m) where
6265
type ThreadId (ReaderT r m) = ThreadId m
@@ -70,6 +73,7 @@ instance MonadFork m => MonadFork (ReaderT e m) where
7073
restore' (ReaderT f) = ReaderT $ restore . f
7174
in runReaderT (k restore') e
7275
throwTo e t = lift (throwTo e t)
76+
yield = lift yield
7377

7478
-- | Apply the label to the current thread
7579
labelThisThread :: MonadThread m => String -> m ()

io-sim/src/Control/Monad/IOSim/Internal.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ schedule !thread@Thread{
562562
return $ SimTrace time tid tlbl (EventThrowTo e tid')
563563
$ trace
564564

565+
YieldSim k -> do
566+
let thread' = thread { threadControl = ThreadControl k ctl }
567+
deschedule Yield thread' simstate
568+
565569
-- ExploreRaces is ignored by this simulator
566570
ExploreRaces k ->
567571
{-# SCC "schedule.ExploreRaces" #-}

io-sim/src/Control/Monad/IOSim/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ data SimA s a where
164164
SetMaskState :: MaskingState -> IOSim s a -> (a -> SimA s b) -> SimA s b
165165
GetMaskState :: (MaskingState -> SimA s b) -> SimA s b
166166

167+
YieldSim :: SimA s a -> SimA s a
168+
167169
ExploreRaces :: SimA s b -> SimA s b
168170

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

375378
instance MonadTest (IOSim s) where
376379
exploreRaces = IOSim $ oneShot $ \k -> ExploreRaces (k ())

io-sim/src/Control/Monad/IOSimPOR/Internal.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ schedule thread@Thread{
696696
return $ SimPORTrace time tid tstep tlbl (EventThrowTo e tid')
697697
$ trace
698698

699+
-- intentionally a no-op (at least for now)
700+
YieldSim k -> do
701+
let thread' = thread { threadControl = ThreadControl k ctl }
702+
schedule thread' simstate
703+
699704

700705
threadInterruptible :: Thread s a -> Bool
701706
threadInterruptible thread =

io-sim/test/Test/IOSim.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,6 @@ unit_async_10 =
899899
)
900900
===
901901
["child 1", "child 2", "child 1 running", "parent done"]
902-
where
903-
yield :: IOSim s ()
904-
yield = atomically (return ()) -- yield, go to end of runqueue
905902

906903

907904
unit_async_11 =
@@ -934,9 +931,6 @@ unit_async_11 =
934931
)
935932
===
936933
["child 1", "child 2", "child 1 running", "parent done"]
937-
where
938-
yield :: IOSim s ()
939-
yield = atomically (return ()) -- yield, go to end of runqueue
940934

941935

942936
unit_async_12 =

0 commit comments

Comments
 (0)