@@ -61,6 +61,8 @@ import Data.Maybe (mapMaybe)
61
61
import Data.Ord
62
62
import Data.OrdPSQ (OrdPSQ )
63
63
import Data.OrdPSQ qualified as PSQ
64
+ import Data.IntPSQ (IntPSQ )
65
+ import Data.IntPSQ qualified as IPSQ
64
66
import Data.Set (Set )
65
67
import Data.Set qualified as Set
66
68
import Data.Time (UTCTime (.. ), fromGregorian )
@@ -85,6 +87,7 @@ import Control.Monad.IOSim.Types hiding (SimEvent (SimEvent), Trace (SimTrace))
85
87
import Control.Monad.IOSim.Types (SimEvent )
86
88
import Control.Monad.IOSimPOR.Timeout (unsafeTimeout )
87
89
import Control.Monad.IOSimPOR.Types
90
+ import Data.Coerce (coerce , Coercible )
88
91
89
92
--
90
93
-- Simulation interpreter
@@ -179,7 +182,7 @@ data TimerCompletionInfo s =
179
182
-- `TimeoutId` (only used to report in a trace).
180
183
181
184
type RunQueue = OrdPSQ (Down IOSimThreadId ) (Down IOSimThreadId ) ()
182
- type Timeouts s = OrdPSQ TimeoutId Time (TimerCompletionInfo s )
185
+ type Timeouts s = IntPSQ Time (TimerCompletionInfo s )
183
186
184
187
-- | Internal state.
185
188
--
@@ -215,7 +218,7 @@ initialState =
215
218
runqueue = PSQ. empty,
216
219
threads = Map. empty,
217
220
curTime = Time 0 ,
218
- timers = PSQ . empty,
221
+ timers = IPSQ . empty,
219
222
clocks = Map. singleton (ClockId [] ) epoch1970,
220
223
nextVid = 0 ,
221
224
nextTmid = TimeoutId 0 ,
@@ -372,7 +375,7 @@ schedule thread@Thread{
372
375
373
376
DelayFrame tmid k ctl' -> do
374
377
let thread' = thread { threadControl = ThreadControl k ctl' }
375
- timers' = PSQ . delete tmid timers
378
+ timers' = IPSQ . delete (coerce tmid) timers
376
379
schedule thread' simstate { timers = timers' }
377
380
378
381
Throw e -> case unwindControlStack e thread timers of
@@ -482,7 +485,7 @@ schedule thread@Thread{
482
485
StartTimeout d action' k -> do
483
486
lock <- TMVar <$> execNewTVar (TMVarId nextVid) (Just $! " lock-" ++ show nextTmid) Nothing
484
487
let expiry = d `addTime` time
485
- timers' = PSQ . insert nextTmid expiry (TimerTimeout tid nextTmid lock) timers
488
+ timers' = IPSQ . insert (coerce nextTmid) expiry (TimerTimeout tid nextTmid lock) timers
486
489
thread' = thread { threadControl =
487
490
ThreadControl action'
488
491
(TimeoutFrame nextTmid lock k ctl)
@@ -493,7 +496,7 @@ schedule thread@Thread{
493
496
494
497
UnregisterTimeout tmid k -> do
495
498
let thread' = thread { threadControl = ThreadControl k ctl }
496
- schedule thread' simstate { timers = PSQ . delete tmid timers }
499
+ schedule thread' simstate { timers = IPSQ . delete (coerce tmid) timers }
497
500
498
501
RegisterDelay d k | d < 0 -> do
499
502
tvar <- execNewTVar (TVarId nextVid)
@@ -513,7 +516,7 @@ schedule thread@Thread{
513
516
False
514
517
modifySTRef (tvarVClock tvar) (leastUpperBoundVClock vClock)
515
518
let ! expiry = d `addTime` time
516
- ! timers' = PSQ . insert nextTmid expiry (TimerRegisterDelay tvar) timers
519
+ ! timers' = IPSQ . insert (coerce nextTmid) expiry (TimerRegisterDelay tvar) timers
517
520
! thread' = thread { threadControl = ThreadControl (k tvar) ctl }
518
521
trace <- schedule thread' simstate { timers = timers'
519
522
, nextVid = succ nextVid
@@ -532,7 +535,7 @@ schedule thread@Thread{
532
535
533
536
ThreadDelay d k -> do
534
537
let expiry = d `addTime` time
535
- timers' = PSQ . insert nextTmid expiry (TimerThreadDelay tid nextTmid) timers
538
+ timers' = IPSQ . insert (coerce nextTmid) expiry (TimerThreadDelay tid nextTmid) timers
536
539
thread' = thread { threadControl = ThreadControl (Return () ) (DelayFrame nextTmid k ctl) }
537
540
trace <- deschedule (Blocked BlockedOnDelay ) thread'
538
541
simstate { timers = timers',
@@ -558,15 +561,15 @@ schedule thread@Thread{
558
561
modifySTRef (tvarVClock tvar) (leastUpperBoundVClock vClock)
559
562
let expiry = d `addTime` time
560
563
t = Timeout tvar nextTmid
561
- timers' = PSQ . insert nextTmid expiry (Timer tvar) timers
564
+ timers' = IPSQ . insert (coerce nextTmid) expiry (Timer tvar) timers
562
565
thread' = thread { threadControl = ThreadControl (k t) ctl }
563
566
trace <- schedule thread' simstate { timers = timers'
564
567
, nextVid = succ (succ nextVid)
565
568
, nextTmid = succ nextTmid }
566
569
return (SimPORTrace time tid tstep tlbl (EventTimerCreated nextTmid (TVarId nextVid) expiry) trace)
567
570
568
571
CancelTimeout (Timeout tvar tmid) k -> do
569
- let timers' = PSQ . delete tmid timers
572
+ let timers' = IPSQ . delete (coerce tmid) timers
570
573
written <- execAtomically' (runSTM $ writeTVar tvar TimeoutCancelled )
571
574
written' <- mapM someTVarToLabelled written
572
575
(wakeup, wokeby) <- threadsUnblockedByWrites written
@@ -1291,29 +1294,29 @@ unwindControlStack e thread = \timeouts ->
1291
1294
_ -> unwind maskst ctl timers'
1292
1295
where
1293
1296
-- Remove the timeout associated with the 'TimeoutFrame'.
1294
- timers' = PSQ . delete tmid timers
1297
+ timers' = IPSQ . delete (coerce tmid) timers
1295
1298
1296
1299
unwind maskst (DelayFrame tmid _k ctl) timers =
1297
1300
unwind maskst ctl timers'
1298
1301
where
1299
1302
-- Remove the timeout associated with the 'DelayFrame'.
1300
- timers' = PSQ . delete tmid timers
1303
+ timers' = IPSQ . delete (coerce tmid) timers
1301
1304
1302
1305
atLeastInterruptibleMask :: MaskingState -> MaskingState
1303
1306
atLeastInterruptibleMask Unmasked = MaskedInterruptible
1304
1307
atLeastInterruptibleMask ms = ms
1305
1308
1306
1309
1307
- removeMinimums :: (Ord k , Ord p )
1308
- => OrdPSQ k p a
1309
- -> Maybe ([k ], p , [a ], OrdPSQ k p a )
1310
- removeMinimums = \ psq ->
1311
- case PSQ . minView psq of
1310
+ removeMinimums :: (Coercible Int k , Ord p )
1311
+ => IntPSQ p a
1312
+ -> Maybe ([k ], p , [a ], IntPSQ p a )
1313
+ removeMinimums = \ psq -> coerce $
1314
+ case IPSQ . minView psq of
1312
1315
Nothing -> Nothing
1313
1316
Just (k, p, x, psq') -> Just (collectAll [k] p [x] psq')
1314
1317
where
1315
1318
collectAll ks p xs psq =
1316
- case PSQ . minView psq of
1319
+ case IPSQ . minView psq of
1317
1320
Just (k, p', x, psq')
1318
1321
| p == p' -> collectAll (k: ks) p (x: xs) psq'
1319
1322
_ -> (reverse ks, p, reverse xs, psq)
0 commit comments