Skip to content

Commit c09661b

Browse files
committed
use IntPSQ instead of OrdPSQ
1 parent 1679cc2 commit c09661b

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module Control.Monad.IOSim.Internal
4848

4949
import Prelude hiding (read)
5050

51+
import Data.Coerce
5152
import Data.Deque.Strict (Deque)
5253
import Data.Deque.Strict qualified as Deque
5354
import Data.Dynamic
@@ -57,8 +58,8 @@ import Data.List.Trace qualified as Trace
5758
import Data.Map.Strict (Map)
5859
import Data.Map.Strict qualified as Map
5960
import Data.Maybe (mapMaybe)
60-
import Data.OrdPSQ (OrdPSQ)
61-
import Data.OrdPSQ qualified as PSQ
61+
import Data.IntPSQ (IntPSQ)
62+
import Data.IntPSQ qualified as PSQ
6263
import Data.Set (Set)
6364
import Data.Set qualified as Set
6465
import Data.Time (UTCTime (..), fromGregorian)
@@ -134,7 +135,7 @@ data TimerCompletionInfo s =
134135
-- `TimeoutId` (only used to report in a trace).
135136

136137

137-
type Timeouts s = OrdPSQ TimeoutId Time (TimerCompletionInfo s)
138+
type Timeouts s = IntPSQ Time (TimerCompletionInfo s)
138139

139140
-- | Internal state.
140141
--
@@ -263,7 +264,7 @@ schedule !thread@Thread{
263264

264265
DelayFrame tmid k ctl' -> do
265266
let thread' = thread { threadControl = ThreadControl k ctl' }
266-
timers' = PSQ.delete tmid timers
267+
timers' = (PSQ.delete . coerce) tmid timers
267268
schedule thread' simstate { timers = timers' }
268269

269270
Throw e -> case unwindControlStack e thread timers of
@@ -360,7 +361,7 @@ schedule !thread@Thread{
360361
StartTimeout d action' k -> do
361362
!lock <- TMVar <$> execNewTVar (TMVarId nextVid) (Just $! "lock-" ++ show nextTmid) Nothing
362363
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
364365
!thread' = thread { threadControl =
365366
ThreadControl action'
366367
(TimeoutFrame nextTmid lock k ctl)
@@ -373,7 +374,7 @@ schedule !thread@Thread{
373374

374375
UnregisterTimeout tmid k -> do
375376
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 }
377378

378379
RegisterDelay d k | d < 0 -> do
379380
!tvar <- execNewTVar (TVarId nextVid)
@@ -391,7 +392,7 @@ schedule !thread@Thread{
391392
(Just $! "<<timeout " ++ show (unTimeoutId nextTmid) ++ ">>")
392393
False
393394
let !expiry = d `addTime` time
394-
!timers' = PSQ.insert nextTmid expiry (TimerRegisterDelay tvar) timers
395+
!timers' = (PSQ.insert . coerce) nextTmid expiry (TimerRegisterDelay tvar) timers
395396
!thread' = thread { threadControl = ThreadControl (k tvar) ctl }
396397
trace <- schedule thread' simstate { timers = timers'
397398
, nextVid = succ nextVid
@@ -410,7 +411,7 @@ schedule !thread@Thread{
410411

411412
ThreadDelay d k -> do
412413
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
414415
!thread' = thread { threadControl = ThreadControl (Return ()) (DelayFrame nextTmid k ctl) }
415416
!trace <- deschedule (Blocked BlockedOnDelay) thread' simstate { timers = timers'
416417
, nextTmid = succ nextTmid }
@@ -434,15 +435,15 @@ schedule !thread@Thread{
434435
TimeoutPending
435436
let !expiry = d `addTime` time
436437
!t = Timeout tvar nextTmid
437-
!timers' = PSQ.insert nextTmid expiry (Timer tvar) timers
438+
!timers' = (PSQ.insert . coerce) nextTmid expiry (Timer tvar) timers
438439
!thread' = thread { threadControl = ThreadControl (k t) ctl }
439440
trace <- schedule thread' simstate { timers = timers'
440441
, nextVid = succ nextVid
441442
, nextTmid = succ nextTmid }
442443
return (SimTrace time tid tlbl (EventTimerCreated nextTmid (TVarId nextVid) expiry) trace)
443444

444445
CancelTimeout (Timeout tvar tmid) k -> do
445-
let !timers' = PSQ.delete tmid timers
446+
let !timers' = (PSQ.delete . coerce) tmid timers
446447
!thread' = thread { threadControl = ThreadControl k ctl }
447448
!written <- execAtomically' (runSTM $ writeTVar tvar TimeoutCancelled)
448449
-- note: we are not running traceTVar on 'tvar', since its not exposed to
@@ -925,8 +926,8 @@ unwindControlStack e thread = \timers ->
925926
where
926927
unwind :: forall s' c. MaskingState
927928
-> 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))
930931
unwind _ MainFrame timers = (Left True, timers)
931932
unwind _ ForkFrame timers = (Left False, timers)
932933
unwind _ (MaskFrame _k maskst' ctl) timers = unwind maskst' ctl timers
@@ -962,24 +963,24 @@ unwindControlStack e thread = \timers ->
962963
_ -> unwind maskst ctl timers'
963964
where
964965
-- Remove the timeout associated with the 'TimeoutFrame'.
965-
timers' = PSQ.delete tmid timers
966+
timers' = (PSQ.delete . coerce) tmid timers
966967

967968
unwind maskst (DelayFrame tmid _k ctl) timers =
968969
unwind maskst ctl timers'
969970
where
970971
-- Remove the timeout associated with the 'DelayFrame'.
971-
timers' = PSQ.delete tmid timers
972+
timers' = (PSQ.delete . coerce) tmid timers
972973

973974

974975
atLeastInterruptibleMask :: MaskingState -> MaskingState
975976
atLeastInterruptibleMask Unmasked = MaskedInterruptible
976977
atLeastInterruptibleMask ms = ms
977978

978979

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 $
983984
case PSQ.minView psq of
984985
Nothing -> Nothing
985986
Just (k, p, x, psq') -> Just (collectAll [k] p [x] psq')

0 commit comments

Comments
 (0)