Skip to content

Commit 2091a97

Browse files
committed
io-sim: trace deschedule events
Also use a single 'Deschedule' type.
1 parent 611f62e commit 2091a97

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

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

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import Control.Monad.Class.MonadThrow hiding (getMaskingState)
7979
import Control.Monad.Class.MonadTime
8080
import Control.Monad.Class.MonadTimer
8181

82-
import Control.Monad.IOSim.Types hiding (Deschedule (..))
82+
import Control.Monad.IOSim.Types
8383
import Control.Monad.IOSim.InternalTypes
8484

8585
--
@@ -201,15 +201,19 @@ schedule thread@Thread{
201201
ForkFrame -> do
202202
-- this thread is done
203203
trace <- deschedule Terminated thread simstate
204-
return $ SimTrace time tid tlbl EventThreadFinished trace
204+
return $ SimTrace time tid tlbl EventThreadFinished
205+
$ SimTrace time tid tlbl (EventDeschedule Terminated)
206+
$ trace
205207

206208
MaskFrame k maskst' ctl' -> do
207209
-- pop the control stack, restore thread-local state
208210
let thread' = thread { threadControl = ThreadControl (k x) ctl'
209211
, threadMasking = maskst' }
210212
-- but if we're now unmasked, check for any pending async exceptions
211213
trace <- deschedule Interruptable thread' simstate
212-
return (SimTrace time tid tlbl (EventMask maskst') trace)
214+
return $ SimTrace time tid tlbl (EventMask maskst')
215+
$ SimTrace time tid tlbl (EventDeschedule Interruptable)
216+
$ trace
213217

214218
CatchFrame _handler k ctl' -> do
215219
-- pop the control stack and continue
@@ -235,8 +239,10 @@ schedule thread@Thread{
235239
| otherwise -> do
236240
-- An unhandled exception in any other thread terminates the thread
237241
trace <- deschedule Terminated thread simstate
238-
return (SimTrace time tid tlbl (EventThrow e) $
239-
SimTrace time tid tlbl (EventThreadUnhandled e) trace)
242+
return $ SimTrace time tid tlbl (EventThrow e)
243+
$ SimTrace time tid tlbl (EventThreadUnhandled e)
244+
$ SimTrace time tid tlbl (EventDeschedule Terminated)
245+
$ trace
240246

241247
Catch action' handler k -> do
242248
-- push the failure and success continuations onto the control stack
@@ -412,19 +418,18 @@ schedule thread@Thread{
412418
-- that algorithms are not sensitive to the exact policy, so long
413419
-- as it is a fair policy (all runnable threads eventually run).
414420
trace <- deschedule Yield thread' simstate' { nextVid = nextVid' }
415-
return $
416-
SimTrace time tid tlbl (EventTxCommitted vids [nextVid..pred nextVid']
417-
Nothing) $
418-
traceMany
419-
[ (time, tid', tlbl', EventTxWakeup vids')
420-
| tid' <- unblocked
421-
, let tlbl' = lookupThreadLabel tid' threads
422-
, let Just vids' = Set.toList <$> Map.lookup tid' wokeby ] $
423-
traceMany
424-
[ (time, tid, tlbl, EventLog tr)
425-
| tr <- tvarTraces
426-
]
427-
trace
421+
return $ SimTrace time tid tlbl (EventTxCommitted
422+
vids [nextVid..pred nextVid'] Nothing)
423+
$ traceMany
424+
[ (time, tid', tlbl', EventTxWakeup vids')
425+
| tid' <- unblocked
426+
, let tlbl' = lookupThreadLabel tid' threads
427+
, let Just vids' = Set.toList <$> Map.lookup tid' wokeby ]
428+
$ traceMany
429+
[ (time, tid, tlbl, EventLog tr)
430+
| tr <- tvarTraces ]
431+
$ SimTrace time tid tlbl (EventDeschedule Yield)
432+
$ trace
428433

429434
StmTxAborted _read e -> do
430435
-- schedule this thread to immediately raise the exception
@@ -436,7 +441,9 @@ schedule thread@Thread{
436441
mapM_ (\(SomeTVar tvar) -> blockThreadOnTVar tid tvar) read
437442
vids <- traverse (\(SomeTVar tvar) -> labelledTVarId tvar) read
438443
trace <- deschedule Blocked thread simstate
439-
return $ SimTrace time tid tlbl (EventTxBlocked vids Nothing) trace
444+
return $ SimTrace time tid tlbl (EventTxBlocked vids Nothing)
445+
$ SimTrace time tid tlbl (EventDeschedule Blocked)
446+
$ trace
440447

441448
GetThreadId k -> do
442449
let thread' = thread { threadControl = ThreadControl (k tid) ctl }
@@ -464,9 +471,11 @@ schedule thread@Thread{
464471
trace <-
465472
case maskst' of
466473
-- If we're now unmasked then check for any pending async exceptions
467-
Unmasked -> deschedule Interruptable thread' simstate
474+
Unmasked -> SimTrace time tid tlbl (EventDeschedule Interruptable)
475+
<$> deschedule Interruptable thread' simstate
468476
_ -> schedule thread' simstate
469-
return (SimTrace time tid tlbl (EventMask maskst') trace)
477+
return $ SimTrace time tid tlbl (EventMask maskst')
478+
$ trace
470479

471480
ThrowTo e tid' _ | tid' == tid -> do
472481
-- Throw to ourself is equivalent to a synchronous throw,
@@ -489,6 +498,7 @@ schedule thread@Thread{
489498
trace <- deschedule Blocked thread' simstate { threads = threads' }
490499
return $ SimTrace time tid tlbl (EventThrowTo e tid')
491500
$ SimTrace time tid tlbl EventThrowToBlocked
501+
$ SimTrace time tid tlbl (EventDeschedule Blocked)
492502
$ trace
493503
else do
494504
-- The target thread has async exceptions unmasked, or is masked but
@@ -525,8 +535,6 @@ threadInterruptible thread =
525535
| otherwise -> False
526536
MaskedUninterruptible -> False
527537

528-
data Deschedule = Yield | Interruptable | Blocked | Terminated
529-
530538
deschedule :: Deschedule -> Thread s a -> SimState s a -> ST s (SimTrace a)
531539
deschedule Yield thread simstate@SimState{runqueue, threads} =
532540

@@ -598,6 +606,9 @@ deschedule Terminated thread simstate@SimState{ curTime = time, threads } = do
598606
, let tlbl' = lookupThreadLabel tid' threads ]
599607
trace
600608

609+
deschedule Sleep _thread _simstate =
610+
error "IOSim: impossible happend"
611+
601612
-- When there is no current running thread but the runqueue is non-empty then
602613
-- schedule the next one to run.
603614
reschedule :: SimState s a -> ST s (SimTrace a)

0 commit comments

Comments
 (0)