@@ -48,17 +48,18 @@ module Control.Monad.IOSim.Internal
48
48
49
49
import Prelude hiding (read )
50
50
51
+ import Data.Coerce
51
52
import Data.Deque.Strict (Deque )
52
53
import Data.Deque.Strict qualified as Deque
53
54
import Data.Dynamic
54
55
import Data.Foldable (foldlM , toList , traverse_ )
56
+ import Data.IntPSQ (IntPSQ )
57
+ import Data.IntPSQ qualified as PSQ
55
58
import Data.List qualified as List
56
59
import Data.List.Trace qualified as Trace
57
60
import Data.Map.Strict (Map )
58
61
import Data.Map.Strict qualified as Map
59
62
import Data.Maybe (mapMaybe )
60
- import Data.OrdPSQ (OrdPSQ )
61
- import Data.OrdPSQ qualified as PSQ
62
63
import Data.Set (Set )
63
64
import Data.Set qualified as Set
64
65
import Data.Time (UTCTime (.. ), fromGregorian )
@@ -134,7 +135,7 @@ data TimerCompletionInfo s =
134
135
-- `TimeoutId` (only used to report in a trace).
135
136
136
137
137
- type Timeouts s = OrdPSQ TimeoutId Time (TimerCompletionInfo s )
138
+ type Timeouts s = IntPSQ Time (TimerCompletionInfo s )
138
139
139
140
-- | Internal state.
140
141
--
@@ -263,7 +264,7 @@ schedule !thread@Thread{
263
264
264
265
DelayFrame tmid k ctl' -> do
265
266
let thread' = thread { threadControl = ThreadControl k ctl' }
266
- timers' = PSQ. delete tmid timers
267
+ timers' = ( PSQ. delete . coerce) tmid timers
267
268
schedule thread' simstate { timers = timers' }
268
269
269
270
Throw e -> case unwindControlStack e thread timers of
@@ -360,7 +361,7 @@ schedule !thread@Thread{
360
361
StartTimeout d action' k -> do
361
362
! lock <- TMVar <$> execNewTVar (TMVarId nextVid) (Just $! " lock-" ++ show nextTmid) Nothing
362
363
let ! expiry = d `addTime` time
363
- ! timers' = PSQ. insert nextTmid expiry (TimerTimeout tid nextTmid lock) timers
364
+ ! timers' = ( PSQ. insert . coerce) nextTmid expiry (TimerTimeout tid nextTmid lock) timers
364
365
! thread' = thread { threadControl =
365
366
ThreadControl action'
366
367
(TimeoutFrame nextTmid lock k ctl)
@@ -373,7 +374,7 @@ schedule !thread@Thread{
373
374
374
375
UnregisterTimeout tmid k -> do
375
376
let thread' = thread { threadControl = ThreadControl k ctl }
376
- schedule thread' simstate { timers = PSQ. delete tmid timers }
377
+ schedule thread' simstate { timers = ( PSQ. delete . coerce) tmid timers }
377
378
378
379
RegisterDelay d k | d < 0 -> do
379
380
! tvar <- execNewTVar (TVarId nextVid)
@@ -391,7 +392,7 @@ schedule !thread@Thread{
391
392
(Just $! " <<timeout " ++ show (unTimeoutId nextTmid) ++ " >>" )
392
393
False
393
394
let ! expiry = d `addTime` time
394
- ! timers' = PSQ. insert nextTmid expiry (TimerRegisterDelay tvar) timers
395
+ ! timers' = ( PSQ. insert . coerce) nextTmid expiry (TimerRegisterDelay tvar) timers
395
396
! thread' = thread { threadControl = ThreadControl (k tvar) ctl }
396
397
trace <- schedule thread' simstate { timers = timers'
397
398
, nextVid = succ nextVid
@@ -410,7 +411,7 @@ schedule !thread@Thread{
410
411
411
412
ThreadDelay d k -> do
412
413
let ! expiry = d `addTime` time
413
- ! timers' = PSQ. insert nextTmid expiry (TimerThreadDelay tid nextTmid) timers
414
+ ! timers' = ( PSQ. insert . coerce) nextTmid expiry (TimerThreadDelay tid nextTmid) timers
414
415
! thread' = thread { threadControl = ThreadControl (Return () ) (DelayFrame nextTmid k ctl) }
415
416
! trace <- deschedule (Blocked BlockedOnDelay ) thread' simstate { timers = timers'
416
417
, nextTmid = succ nextTmid }
@@ -434,15 +435,15 @@ schedule !thread@Thread{
434
435
TimeoutPending
435
436
let ! expiry = d `addTime` time
436
437
! t = Timeout tvar nextTmid
437
- ! timers' = PSQ. insert nextTmid expiry (Timer tvar) timers
438
+ ! timers' = ( PSQ. insert . coerce) nextTmid expiry (Timer tvar) timers
438
439
! thread' = thread { threadControl = ThreadControl (k t) ctl }
439
440
trace <- schedule thread' simstate { timers = timers'
440
441
, nextVid = succ nextVid
441
442
, nextTmid = succ nextTmid }
442
443
return (SimTrace time tid tlbl (EventTimerCreated nextTmid (TVarId nextVid) expiry) trace)
443
444
444
445
CancelTimeout (Timeout tvar tmid) k -> do
445
- let ! timers' = PSQ. delete tmid timers
446
+ let ! timers' = ( PSQ. delete . coerce) tmid timers
446
447
! thread' = thread { threadControl = ThreadControl k ctl }
447
448
! written <- execAtomically' (runSTM $ writeTVar tvar TimeoutCancelled )
448
449
-- note: we are not running traceTVar on 'tvar', since its not exposed to
@@ -925,8 +926,8 @@ unwindControlStack e thread = \timers ->
925
926
where
926
927
unwind :: forall s' c . MaskingState
927
928
-> ControlStack s' c a
928
- -> OrdPSQ TimeoutId Time (TimerCompletionInfo s )
929
- -> (Either Bool (Thread s' a ), OrdPSQ TimeoutId Time (TimerCompletionInfo s ))
929
+ -> IntPSQ Time (TimerCompletionInfo s )
930
+ -> (Either Bool (Thread s' a ), IntPSQ Time (TimerCompletionInfo s ))
930
931
unwind _ MainFrame timers = (Left True , timers)
931
932
unwind _ ForkFrame timers = (Left False , timers)
932
933
unwind _ (MaskFrame _k maskst' ctl) timers = unwind maskst' ctl timers
@@ -962,24 +963,24 @@ unwindControlStack e thread = \timers ->
962
963
_ -> unwind maskst ctl timers'
963
964
where
964
965
-- Remove the timeout associated with the 'TimeoutFrame'.
965
- timers' = PSQ. delete tmid timers
966
+ timers' = ( PSQ. delete . coerce) tmid timers
966
967
967
968
unwind maskst (DelayFrame tmid _k ctl) timers =
968
969
unwind maskst ctl timers'
969
970
where
970
971
-- Remove the timeout associated with the 'DelayFrame'.
971
- timers' = PSQ. delete tmid timers
972
+ timers' = ( PSQ. delete . coerce) tmid timers
972
973
973
974
974
975
atLeastInterruptibleMask :: MaskingState -> MaskingState
975
976
atLeastInterruptibleMask Unmasked = MaskedInterruptible
976
977
atLeastInterruptibleMask ms = ms
977
978
978
979
979
- removeMinimums :: (Ord k , Ord p )
980
- => OrdPSQ k p a
981
- -> Maybe ([k ], p , [a ], OrdPSQ k p a )
982
- removeMinimums = \ psq ->
980
+ removeMinimums :: (Coercible k Int , Ord p )
981
+ => IntPSQ p a
982
+ -> Maybe ([k ], p , [a ], IntPSQ p a )
983
+ removeMinimums = \ psq -> coerce $
983
984
case PSQ. minView psq of
984
985
Nothing -> Nothing
985
986
Just (k, p, x, psq') -> Just (collectAll [k] p [x] psq')
0 commit comments