Skip to content

Commit 362fa0e

Browse files
committed
io-sim-por: introduced insertThread
Introduced `insertThread :: Thread s a -> [ThreadId] -> [ThreadId]` which inserts a thread in `runqueue`.
1 parent e50acc0 commit 362fa0e

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ timeSinceEpoch :: Time -> NominalDiffTime
222222
timeSinceEpoch (Time t) = fromRational (toRational t)
223223

224224

225+
-- | Insert thread into `runqueue`.
226+
--
227+
insertThread :: Thread s a -> [ThreadId] -> [ThreadId]
228+
insertThread t = List.insertBy (comparing Down) (threadId t)
229+
230+
225231
-- | Schedule / run a thread.
226232
--
227233
schedule :: Thread s a -> SimState s a -> ST s (SimTrace a)
@@ -477,7 +483,7 @@ schedule thread@Thread{
477483
threads' = Map.insert tid' thread'' threads
478484
-- A newly forked thread may have a higher priority, so we deschedule this one.
479485
trace <- deschedule Yield thread'
480-
simstate { runqueue = List.insertBy (comparing Down) tid' runqueue
486+
simstate { runqueue = insertThread thread'' runqueue
481487
, threads = threads' }
482488
return (SimTrace time tid tlbl (EventThreadForked tid') trace)
483489

@@ -640,7 +646,7 @@ deschedule Yield thread@Thread { threadId = tid }
640646
-- We do it here by inserting the current thread into the runqueue in priority order.
641647

642648
let thread' = stepThread thread
643-
runqueue' = List.insertBy (comparing Down) tid runqueue
649+
runqueue' = insertThread thread' runqueue
644650
threads' = Map.insert tid thread' threads
645651
control' = advanceControl (threadStepId thread) control in
646652
reschedule simstate { runqueue = runqueue', threads = threads',
@@ -728,7 +734,7 @@ deschedule Sleep thread@Thread { threadId = tid }
728734
-- Schedule control says we should run a different thread. Put
729735
-- this one to sleep without recording a step.
730736

731-
let runqueue' = List.insertBy (comparing Down) tid runqueue
737+
let runqueue' = insertThread thread runqueue
732738
threads' = Map.insert tid thread threads in
733739
reschedule simstate { runqueue = runqueue', threads = threads' }
734740

@@ -813,29 +819,39 @@ reschedule simstate@SimState{ runqueue = [], threads, timers, curTime = time, ra
813819
TimeoutFired -> error "MonadTimer(Sim): invariant violation"
814820
TimeoutCancelled -> return ()
815821

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)
817827
unblockThreads vClock wakeup simstate@SimState {runqueue, threads} =
818828
-- To preserve our invariants (that threadBlocked is correct)
819829
-- 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+
})
824834
where
825835
-- can only unblock if the thread exists and is blocked (not running)
826-
unblocked = [ tid
836+
unblocked :: [Thread s a]
837+
unblocked = [ thread
827838
| 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+
_ -> [ ]
832844
]
845+
846+
unblockedIds :: [ThreadId]
847+
unblockedIds = map threadId unblocked
848+
833849
-- and in which case we mark them as now running
834850
threads' = List.foldl'
835851
(flip (Map.adjust
836852
(\t -> t { threadBlocked = False,
837853
threadVClock = vClock `leastUpperBoundVClock` threadVClock t })))
838-
threads unblocked
854+
threads unblockedIds
839855

840856

841857
-- | Iterate through the control stack to find an enclosing exception handler

0 commit comments

Comments
 (0)