Skip to content

Commit 924b326

Browse files
committed
io-sim-por: use OrdPSQ for runqueue
1 parent 508d0fb commit 924b326

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ labelledThreads threadMap =
162162
--
163163
data TimerVars s = TimerVars !(TVar s TimeoutState) !(TVar s Bool)
164164

165+
type RunQueue = OrdPSQ (Down ThreadId) (Down ThreadId) ()
165166

166167
-- | Internal state.
167168
--
168169
data SimState s a = SimState {
169-
runqueue :: ![ThreadId],
170+
runqueue :: !RunQueue,
170171
-- | All threads other than the currently running thread: both running
171172
-- and blocked threads.
172173
threads :: !(Map ThreadId (Thread s a)),
@@ -193,7 +194,7 @@ data SimState s a = SimState {
193194
initialState :: SimState s a
194195
initialState =
195196
SimState {
196-
runqueue = [],
197+
runqueue = PSQ.empty,
197198
threads = Map.empty,
198199
curTime = Time 0,
199200
timers = PSQ.empty,
@@ -213,15 +214,17 @@ invariant :: Maybe (Thread s a) -> SimState s a -> x -> x
213214
invariant (Just running) simstate@SimState{runqueue,threads,clocks} =
214215
assert (not (threadBlocked running))
215216
. assert (threadId running `Map.notMember` threads)
216-
. assert (threadId running `List.notElem` runqueue)
217+
. assert (not (Down (threadId running) `PSQ.member` runqueue))
217218
. assert (threadClockId running `Map.member` clocks)
218219
. invariant Nothing simstate
219220

220221
invariant Nothing SimState{runqueue,threads,clocks} =
221-
assert (all (`Map.member` threads) runqueue)
222-
. assert (and [ (threadBlocked t || threadDone t) == (threadId t `notElem` runqueue)
222+
assert (PSQ.fold' (\(Down tid) _ _ a -> tid `Map.member` threads && a) True runqueue)
223+
. assert (and [ (threadBlocked t || threadDone t) == not (Down (threadId t) `PSQ.member` runqueue)
223224
| t <- Map.elems threads ])
224-
. assert (and (zipWith (>) runqueue (drop 1 runqueue)))
225+
. assert (and (zipWith (\(Down tid, _, _) (Down tid', _, _) -> tid > tid')
226+
(PSQ.toList runqueue)
227+
(drop 1 (PSQ.toList runqueue))))
225228
. assert (and [ threadClockId t `Map.member` clocks
226229
| t <- Map.elems threads ])
227230

@@ -233,8 +236,8 @@ timeSinceEpoch (Time t) = fromRational (toRational t)
233236

234237
-- | Insert thread into `runqueue`.
235238
--
236-
insertThread :: Thread s a -> [ThreadId] -> [ThreadId]
237-
insertThread t = List.insertBy (comparing Down) (threadId t)
239+
insertThread :: Thread s a -> RunQueue -> RunQueue
240+
insertThread Thread { threadId } = PSQ.insert (Down threadId) (Down threadId) ()
238241

239242

240243
-- | Schedule / run a thread.
@@ -818,7 +821,7 @@ reschedule simstate@SimState{ runqueue, threads,
818821
curTime=time
819822
} =
820823
fmap (SimPORTrace time tid tstep Nothing (EventReschedule control)) $
821-
assert (tid `elem` runqueue) $
824+
assert (Down tid `PSQ.member` runqueue) $
822825
assert (tid `Map.member` threads) $
823826
invariant Nothing simstate $
824827
let thread = threads Map.! tid in
@@ -831,12 +834,13 @@ reschedule simstate@SimState{ runqueue, threads,
831834
++ " actual step: "++show (threadStep thread)++"\n"
832835
++ "Thread:\n" ++ show thread ++ "\n"
833836
else
834-
schedule thread simstate { runqueue = List.delete tid runqueue
837+
schedule thread simstate { runqueue = PSQ.delete (Down tid) runqueue
835838
, threads = Map.delete tid threads }
836839

837840
-- When there is no current running thread but the runqueue is non-empty then
838841
-- schedule the next one to run.
839-
reschedule simstate@SimState{ runqueue = tid:runqueue', threads } =
842+
reschedule simstate@SimState{ runqueue, threads }
843+
| Just (Down tid, _, _, runqueue') <- PSQ.minView runqueue =
840844
invariant Nothing simstate $
841845

842846
let thread = threads Map.! tid in
@@ -845,7 +849,7 @@ reschedule simstate@SimState{ runqueue = tid:runqueue', threads } =
845849

846850
-- But when there are no runnable threads, we advance the time to the next
847851
-- timer event, or stop.
848-
reschedule simstate@SimState{ runqueue = [], threads, timers, curTime = time, races } =
852+
reschedule simstate@SimState{ threads, timers, curTime = time, races } =
849853
invariant Nothing simstate $
850854

851855
-- time is moving on

0 commit comments

Comments
 (0)