@@ -222,6 +222,12 @@ timeSinceEpoch :: Time -> NominalDiffTime
222
222
timeSinceEpoch (Time t) = fromRational (toRational t)
223
223
224
224
225
+ -- | Insert thread into `runqueue`.
226
+ --
227
+ insertThread :: Thread s a -> [ThreadId ] -> [ThreadId ]
228
+ insertThread t = List. insertBy (comparing Down ) (threadId t)
229
+
230
+
225
231
-- | Schedule / run a thread.
226
232
--
227
233
schedule :: Thread s a -> SimState s a -> ST s (SimTrace a )
@@ -477,7 +483,7 @@ schedule thread@Thread{
477
483
threads' = Map. insert tid' thread'' threads
478
484
-- A newly forked thread may have a higher priority, so we deschedule this one.
479
485
trace <- deschedule Yield thread'
480
- simstate { runqueue = List. insertBy (comparing Down ) tid ' runqueue
486
+ simstate { runqueue = insertThread thread' ' runqueue
481
487
, threads = threads' }
482
488
return (SimTrace time tid tlbl (EventThreadForked tid') trace)
483
489
@@ -640,7 +646,7 @@ deschedule Yield thread@Thread { threadId = tid }
640
646
-- We do it here by inserting the current thread into the runqueue in priority order.
641
647
642
648
let thread' = stepThread thread
643
- runqueue' = List. insertBy (comparing Down ) tid runqueue
649
+ runqueue' = insertThread thread' runqueue
644
650
threads' = Map. insert tid thread' threads
645
651
control' = advanceControl (threadStepId thread) control in
646
652
reschedule simstate { runqueue = runqueue', threads = threads',
@@ -728,7 +734,7 @@ deschedule Sleep thread@Thread { threadId = tid }
728
734
-- Schedule control says we should run a different thread. Put
729
735
-- this one to sleep without recording a step.
730
736
731
- let runqueue' = List. insertBy (comparing Down ) tid runqueue
737
+ let runqueue' = insertThread thread runqueue
732
738
threads' = Map. insert tid thread threads in
733
739
reschedule simstate { runqueue = runqueue', threads = threads' }
734
740
@@ -813,29 +819,39 @@ reschedule simstate@SimState{ runqueue = [], threads, timers, curTime = time, ra
813
819
TimeoutFired -> error " MonadTimer(Sim): invariant violation"
814
820
TimeoutCancelled -> return ()
815
821
816
- unblockThreads :: VectorClock -> [ThreadId ] -> SimState s a -> ([ThreadId ], SimState s a )
822
+ unblockThreads :: forall s a .
823
+ VectorClock
824
+ -> [ThreadId ]
825
+ -> SimState s a
826
+ -> ([ThreadId ], SimState s a )
817
827
unblockThreads vClock wakeup simstate@ SimState {runqueue, threads} =
818
828
-- To preserve our invariants (that threadBlocked is correct)
819
829
-- we update the runqueue and threads together here
820
- (unblocked, simstate {
821
- runqueue = foldr ( List. insertBy (comparing Down )) runqueue unblocked,
822
- threads = threads'
823
- })
830
+ ( unblockedIds
831
+ , simstate { runqueue = foldr insertThread runqueue unblocked,
832
+ threads = threads'
833
+ })
824
834
where
825
835
-- can only unblock if the thread exists and is blocked (not running)
826
- unblocked = [ tid
836
+ unblocked :: [Thread s a ]
837
+ unblocked = [ thread
827
838
| tid <- wakeup
828
- , case Map. lookup tid threads of
829
- Just Thread { threadDone = True } -> False
830
- Just Thread { threadBlocked = True } -> True
831
- _ -> False
839
+ , thread <-
840
+ case Map. lookup tid threads of
841
+ Just Thread { threadDone = True } -> [ ]
842
+ Just t@ Thread { threadBlocked = True } -> [t]
843
+ _ -> [ ]
832
844
]
845
+
846
+ unblockedIds :: [ThreadId ]
847
+ unblockedIds = map threadId unblocked
848
+
833
849
-- and in which case we mark them as now running
834
850
threads' = List. foldl'
835
851
(flip (Map. adjust
836
852
(\ t -> t { threadBlocked = False ,
837
853
threadVClock = vClock `leastUpperBoundVClock` threadVClock t })))
838
- threads unblocked
854
+ threads unblockedIds
839
855
840
856
841
857
-- | Iterate through the control stack to find an enclosing exception handler
0 commit comments