@@ -61,6 +61,8 @@ import Data.Maybe (mapMaybe)
6161import Data.Ord
6262import Data.OrdPSQ (OrdPSQ )
6363import Data.OrdPSQ qualified as PSQ
64+ import Data.IntPSQ (IntPSQ )
65+ import Data.IntPSQ qualified as IPSQ
6466import Data.Set (Set )
6567import Data.Set qualified as Set
6668import Data.Time (UTCTime (.. ), fromGregorian )
@@ -85,6 +87,7 @@ import Control.Monad.IOSim.Types hiding (SimEvent (SimEvent), Trace (SimTrace))
8587import Control.Monad.IOSim.Types (SimEvent )
8688import Control.Monad.IOSimPOR.Timeout (unsafeTimeout )
8789import Control.Monad.IOSimPOR.Types
90+ import Data.Coerce (coerce , Coercible )
8891
8992--
9093-- Simulation interpreter
@@ -179,7 +182,7 @@ data TimerCompletionInfo s =
179182 -- `TimeoutId` (only used to report in a trace).
180183
181184type RunQueue = OrdPSQ (Down IOSimThreadId ) (Down IOSimThreadId ) ()
182- type Timeouts s = OrdPSQ TimeoutId Time (TimerCompletionInfo s )
185+ type Timeouts s = IntPSQ Time (TimerCompletionInfo s )
183186
184187-- | Internal state.
185188--
@@ -215,7 +218,7 @@ initialState =
215218 runqueue = PSQ. empty,
216219 threads = Map. empty,
217220 curTime = Time 0 ,
218- timers = PSQ . empty,
221+ timers = IPSQ . empty,
219222 clocks = Map. singleton (ClockId [] ) epoch1970,
220223 nextVid = 0 ,
221224 nextTmid = TimeoutId 0 ,
@@ -372,7 +375,7 @@ schedule thread@Thread{
372375
373376 DelayFrame tmid k ctl' -> do
374377 let thread' = thread { threadControl = ThreadControl k ctl' }
375- timers' = PSQ . delete tmid timers
378+ timers' = IPSQ . delete (coerce tmid) timers
376379 schedule thread' simstate { timers = timers' }
377380
378381 Throw e -> case unwindControlStack e thread timers of
@@ -482,7 +485,7 @@ schedule thread@Thread{
482485 StartTimeout d action' k -> do
483486 lock <- TMVar <$> execNewTVar (TMVarId nextVid) (Just $! " lock-" ++ show nextTmid) Nothing
484487 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
486489 thread' = thread { threadControl =
487490 ThreadControl action'
488491 (TimeoutFrame nextTmid lock k ctl)
@@ -493,7 +496,7 @@ schedule thread@Thread{
493496
494497 UnregisterTimeout tmid k -> do
495498 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 }
497500
498501 RegisterDelay d k | d < 0 -> do
499502 tvar <- execNewTVar (TVarId nextVid)
@@ -513,7 +516,7 @@ schedule thread@Thread{
513516 False
514517 modifySTRef (tvarVClock tvar) (leastUpperBoundVClock vClock)
515518 let ! expiry = d `addTime` time
516- ! timers' = PSQ . insert nextTmid expiry (TimerRegisterDelay tvar) timers
519+ ! timers' = IPSQ . insert (coerce nextTmid) expiry (TimerRegisterDelay tvar) timers
517520 ! thread' = thread { threadControl = ThreadControl (k tvar) ctl }
518521 trace <- schedule thread' simstate { timers = timers'
519522 , nextVid = succ nextVid
@@ -532,7 +535,7 @@ schedule thread@Thread{
532535
533536 ThreadDelay d k -> do
534537 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
536539 thread' = thread { threadControl = ThreadControl (Return () ) (DelayFrame nextTmid k ctl) }
537540 trace <- deschedule (Blocked BlockedOnDelay ) thread'
538541 simstate { timers = timers',
@@ -558,15 +561,15 @@ schedule thread@Thread{
558561 modifySTRef (tvarVClock tvar) (leastUpperBoundVClock vClock)
559562 let expiry = d `addTime` time
560563 t = Timeout tvar nextTmid
561- timers' = PSQ . insert nextTmid expiry (Timer tvar) timers
564+ timers' = IPSQ . insert (coerce nextTmid) expiry (Timer tvar) timers
562565 thread' = thread { threadControl = ThreadControl (k t) ctl }
563566 trace <- schedule thread' simstate { timers = timers'
564567 , nextVid = succ (succ nextVid)
565568 , nextTmid = succ nextTmid }
566569 return (SimPORTrace time tid tstep tlbl (EventTimerCreated nextTmid (TVarId nextVid) expiry) trace)
567570
568571 CancelTimeout (Timeout tvar tmid) k -> do
569- let timers' = PSQ . delete tmid timers
572+ let timers' = IPSQ . delete (coerce tmid) timers
570573 written <- execAtomically' (runSTM $ writeTVar tvar TimeoutCancelled )
571574 written' <- mapM someTVarToLabelled written
572575 (wakeup, wokeby) <- threadsUnblockedByWrites written
@@ -1291,29 +1294,29 @@ unwindControlStack e thread = \timeouts ->
12911294 _ -> unwind maskst ctl timers'
12921295 where
12931296 -- Remove the timeout associated with the 'TimeoutFrame'.
1294- timers' = PSQ . delete tmid timers
1297+ timers' = IPSQ . delete (coerce tmid) timers
12951298
12961299 unwind maskst (DelayFrame tmid _k ctl) timers =
12971300 unwind maskst ctl timers'
12981301 where
12991302 -- Remove the timeout associated with the 'DelayFrame'.
1300- timers' = PSQ . delete tmid timers
1303+ timers' = IPSQ . delete (coerce tmid) timers
13011304
13021305 atLeastInterruptibleMask :: MaskingState -> MaskingState
13031306 atLeastInterruptibleMask Unmasked = MaskedInterruptible
13041307 atLeastInterruptibleMask ms = ms
13051308
13061309
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
13121315 Nothing -> Nothing
13131316 Just (k, p, x, psq') -> Just (collectAll [k] p [x] psq')
13141317 where
13151318 collectAll ks p xs psq =
1316- case PSQ . minView psq of
1319+ case IPSQ . minView psq of
13171320 Just (k, p', x, psq')
13181321 | p == p' -> collectAll (k: ks) p (x: xs) psq'
13191322 _ -> (reverse ks, p, reverse xs, psq)
0 commit comments