Skip to content

Commit

Permalink
Only shuffle at most the first half of the runqueue
Browse files Browse the repository at this point in the history
  • Loading branch information
bolt12 committed Dec 15, 2023
1 parent ad93f94 commit 6a781b9
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions io-sim/src/Control/Monad/IOSim/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -853,13 +853,17 @@ unblockThreads !onlySTM !wakeup !simstate@SimState {runqueue, threads, stdGen} =
-- To preserve our invariants (that threadBlocked is correct)
-- we update the runqueue and threads together here
(unblocked, simstate {
runqueue = Deque.fromList shuffledRunqueue,
runqueue = Deque.fromList (shuffledRunqueue ++ rest),
threads = threads',
stdGen = stdGen'
stdGen = stdGen''
})
where
!(shuffledRunqueue, stdGen') = fisherYatesShuffle stdGen runqueue'
!runqueue' = Deque.toList $ runqueue <> Deque.fromList unblocked
!(shuffledRunqueue, stdGen'') = fisherYatesShuffle stdGen' toShuffle
!((toShuffle, rest), stdGen') =
let runqueueList = Deque.toList $ runqueue <> Deque.fromList unblocked
runqueueListLength = max 1 (length runqueueList)
(ix, newGen) = randomR (0, runqueueListLength `div` 2) stdGen
in (splitAt ix runqueueList, newGen)
-- can only unblock if the thread exists and is blocked (not running)
!unblocked = [ tid
| tid <- wakeup
Expand Down

0 comments on commit 6a781b9

Please sign in to comment.