Skip to content

Commit 18eb0fc

Browse files
committed
io-sim-por: renamed ThreadId type to IOSimThreadId
The original type clashes with `ThreadId` type class from `io-classes` which is in particular annoying when pasting `ControlSchedules` in `ghci` or counterexamples.
1 parent 56ce016 commit 18eb0fc

File tree

7 files changed

+124
-108
lines changed

7 files changed

+124
-108
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Control.Monad.IOSim
4646
, SimEvent (..)
4747
, SimEventType (..)
4848
, ThreadLabel
49+
, IOSimThreadId (..)
4950
, Labelled (..)
5051
-- ** Dynamic Tracing
5152
, traceM
@@ -324,12 +325,12 @@ data Failure =
324325
FailureException SomeException
325326

326327
-- | The threads all deadlocked.
327-
| FailureDeadlock ![Labelled ThreadId]
328+
| FailureDeadlock ![Labelled IOSimThreadId]
328329

329330
-- | The main thread terminated normally but other threads were still
330331
-- alive, and strict shutdown checking was requested.
331332
-- See 'runSimStrictShutdown'.
332-
| FailureSloppyShutdown [Labelled ThreadId]
333+
| FailureSloppyShutdown [Labelled IOSimThreadId]
333334

334335
-- | An exception was thrown while evaluation the trace.
335336
-- This could be an internal assertion failure of `io-sim` or an
@@ -404,7 +405,7 @@ traceResult strict = unsafePerformIO . eval
404405

405406
-- | Turn 'SimTrace' into a list of timestamped events.
406407
--
407-
traceEvents :: SimTrace a -> [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
408+
traceEvents :: SimTrace a -> [(Time, IOSimThreadId, Maybe ThreadLabel, SimEventType)]
408409
traceEvents (SimTrace time tid tlbl event t) = (time, tid, tlbl, event)
409410
: traceEvents t
410411
traceEvents (SimPORTrace time tid _ tlbl event t) = (time, tid, tlbl, event)
@@ -414,7 +415,7 @@ traceEvents _ = []
414415

415416
-- | Pretty print a timestamped event.
416417
--
417-
ppEvents :: [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
418+
ppEvents :: [(Time, IOSimThreadId, Maybe ThreadLabel, SimEventType)]
418419
-> String
419420
ppEvents events =
420421
intercalate "\n"
@@ -547,7 +548,7 @@ exploreSimTrace optsf mainAction k =
547548
[ n `div` k + if i < n `mod` k then 1 else 0
548549
| i <- [0..k-1] ]
549550

550-
showThread :: (ThreadId,Maybe ThreadLabel) -> String
551+
showThread :: (IOSimThreadId,Maybe ThreadLabel) -> String
551552
showThread (tid,lab) =
552553
ppIOSimThreadId tid ++ (case lab of Nothing -> ""
553554
Just l -> " ("++l++")")
@@ -661,8 +662,8 @@ raceReversals ControlFollow{} = error "Impossible: raceReversals ControlFoll
661662
compareTracesST :: forall a b s.
662663
Maybe (SimTrace a) -- ^ passing
663664
-> SimTrace b -- ^ failing
664-
-> ST s ( ST s (Maybe ( (Time, ThreadId, Maybe ThreadLabel)
665-
, Set.Set (ThreadId, Maybe ThreadLabel)
665+
-> ST s ( ST s (Maybe ( (Time, IOSimThreadId, Maybe ThreadLabel)
666+
, Set.Set (IOSimThreadId, Maybe ThreadLabel)
666667
))
667668
, SimTrace b
668669
)
@@ -674,8 +675,8 @@ compareTracesST (Just passing) trace = do
674675
, trace'
675676
)
676677
where
677-
go :: STRef s (Maybe ( (Time, ThreadId, Maybe ThreadLabel)
678-
, Set.Set (ThreadId, Maybe ThreadLabel)
678+
go :: STRef s (Maybe ( (Time, IOSimThreadId, Maybe ThreadLabel)
679+
, Set.Set (IOSimThreadId, Maybe ThreadLabel)
679680
))
680681
-> SimTrace a -- ^ passing
681682
-> SimTrace b -- ^ failing
@@ -693,10 +694,10 @@ compareTracesST (Just passing) trace = do
693694
go _ _ SimTrace {} = error "compareTracesST: invariant violation"
694695
go _ _ fail = return fail
695696

696-
wakeup :: STRef s (Maybe ( (Time, ThreadId, Maybe ThreadLabel)
697-
, Set.Set (ThreadId, Maybe ThreadLabel)
697+
wakeup :: STRef s (Maybe ( (Time, IOSimThreadId, Maybe ThreadLabel)
698+
, Set.Set (IOSimThreadId, Maybe ThreadLabel)
698699
))
699-
-> ThreadId
700+
-> IOSimThreadId
700701
-> SimTrace b
701702
-> ST s (SimTrace b)
702703
wakeup sleeper tidpass

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@
77

88
-- | Common types shared between `IOSim` and `IOSimPOR`.
99
--
10-
module Control.Monad.IOSim.CommonTypes where
10+
module Control.Monad.IOSim.CommonTypes
11+
( IOSimThreadId (..)
12+
, childThreadId
13+
, setRacyThread
14+
, TVarId (..)
15+
, TimeoutId (..)
16+
, ClockId (..)
17+
, VectorClock (..)
18+
, unTimeoutId
19+
, ThreadLabel
20+
, TVarLabel
21+
, TVar (..)
22+
, SomeTVar (..)
23+
, Deschedule (..)
24+
, ThreadStatus (..)
25+
, BlockedReason (..)
26+
) where
1127

1228
import Control.DeepSeq (NFData (..))
1329
import Control.Monad.Class.MonadSTM (TraceValue)
@@ -28,26 +44,26 @@ import GHC.Generics
2844
-- `Control.Monad.Class.MonadTest.exploreRaces` was
2945
-- executed in it or it's a thread forked by a racy thread.
3046
--
31-
data ThreadId = RacyThreadId [Int]
32-
| ThreadId [Int] -- non racy threads have higher priority
47+
data IOSimThreadId = RacyThreadId [Int]
48+
| ThreadId [Int] -- non racy threads have higher priority
3349
deriving stock (Eq, Ord, Show, Generic)
3450
deriving anyclass NFData
3551
deriving anyclass NoThunks
3652

3753

38-
childThreadId :: ThreadId -> Int -> ThreadId
54+
childThreadId :: IOSimThreadId -> Int -> IOSimThreadId
3955
childThreadId (RacyThreadId is) i = RacyThreadId (is ++ [i])
4056
childThreadId (ThreadId is) i = ThreadId (is ++ [i])
4157

42-
setRacyThread :: ThreadId -> ThreadId
58+
setRacyThread :: IOSimThreadId -> IOSimThreadId
4359
setRacyThread (ThreadId is) = RacyThreadId is
4460
setRacyThread tid@RacyThreadId{} = tid
4561

4662

4763
newtype TVarId = TVarId Int deriving (Eq, Ord, Enum, Show)
4864
newtype TimeoutId = TimeoutId Int deriving (Eq, Ord, Enum, Show)
4965
newtype ClockId = ClockId [Int] deriving (Eq, Ord, Show)
50-
newtype VectorClock = VectorClock { getVectorClock :: Map ThreadId Int }
66+
newtype VectorClock = VectorClock { getVectorClock :: Map IOSimThreadId Int }
5167
deriving Show
5268

5369
unTimeoutId :: TimeoutId -> Int
@@ -80,7 +96,7 @@ data TVar s a = TVar {
8096
-- To avoid duplicates efficiently, the operations rely on a copy of the
8197
-- thread Ids represented as a set.
8298
--
83-
tvarBlocked :: !(STRef s ([ThreadId], Set ThreadId)),
99+
tvarBlocked :: !(STRef s ([IOSimThreadId], Set IOSimThreadId)),
84100

85101
-- | The vector clock of the current value.
86102
--

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Control.Monad.IOSim.Internal
2626
, TimeoutException (..)
2727
, EventlogEvent (..)
2828
, EventlogMarker (..)
29-
, ThreadId
29+
, IOSimThreadId
3030
, ThreadLabel
3131
, Labelled (..)
3232
, SimTrace
@@ -83,12 +83,12 @@ import Control.Monad.IOSim.Types (SimEvent)
8383
--
8484

8585
data Thread s a = Thread {
86-
threadId :: !ThreadId,
86+
threadId :: !IOSimThreadId,
8787
threadControl :: !(ThreadControl s a),
8888
threadStatus :: !ThreadStatus,
8989
threadMasking :: !MaskingState,
9090
-- other threads blocked in a ThrowTo to us because we are or were masked
91-
threadThrowTo :: ![(SomeException, Labelled ThreadId)],
91+
threadThrowTo :: ![(SomeException, Labelled IOSimThreadId)],
9292
threadClockId :: !ClockId,
9393
threadLabel :: Maybe ThreadLabel,
9494
threadNextTId :: !Int
@@ -102,7 +102,7 @@ isThreadBlocked t = case threadStatus t of
102102
labelledTVarId :: TVar s a -> ST s (Labelled TVarId)
103103
labelledTVarId TVar { tvarId, tvarLabel } = (Labelled tvarId) <$> readSTRef tvarLabel
104104

105-
labelledThreads :: Map ThreadId (Thread s a) -> [Labelled ThreadId]
105+
labelledThreads :: Map IOSimThreadId (Thread s a) -> [Labelled IOSimThreadId]
106106
labelledThreads threadMap =
107107
-- @Map.foldr'@ (and alikes) are not strict enough, to not ratain the
108108
-- original thread map we need to evaluate the spine of the list.
@@ -121,11 +121,11 @@ data TimerCompletionInfo s =
121121
-- ^ `newTimeout` timer.
122122
| TimerRegisterDelay !(TVar s Bool)
123123
-- ^ `registerDelay` timer.
124-
| TimerThreadDelay !ThreadId !TimeoutId
125-
-- ^ `threadDelay` timer run by `ThreadId` which was assigned the given
124+
| TimerThreadDelay !IOSimThreadId !TimeoutId
125+
-- ^ `threadDelay` timer run by `IOSimThreadId` which was assigned the given
126126
-- `TimeoutId` (only used to report in a trace).
127-
| TimerTimeout !ThreadId !TimeoutId !(TMVar (IOSim s) ThreadId)
128-
-- ^ `timeout` timer run by `ThreadId` which was assigned the given
127+
| TimerTimeout !IOSimThreadId !TimeoutId !(TMVar (IOSim s) IOSimThreadId)
128+
-- ^ `timeout` timer run by `IOSimThreadId` which was assigned the given
129129
-- `TimeoutId` (only used to report in a trace).
130130

131131

@@ -134,10 +134,10 @@ type Timeouts s = OrdPSQ TimeoutId Time (TimerCompletionInfo s)
134134
-- | Internal state.
135135
--
136136
data SimState s a = SimState {
137-
runqueue :: !(Deque ThreadId),
137+
runqueue :: !(Deque IOSimThreadId),
138138
-- | All threads other than the currently running thread: both running
139139
-- and blocked threads.
140-
threads :: !(Map ThreadId (Thread s a)),
140+
threads :: !(Map IOSimThreadId (Thread s a)),
141141
-- | current time
142142
curTime :: !Time,
143143
-- | ordered list of timers and timeouts
@@ -821,7 +821,7 @@ reschedule !simstate@SimState{ threads, timers, curTime = time } =
821821
timeoutSTMAction TimerThreadDelay{} = return ()
822822
timeoutSTMAction TimerTimeout{} = return ()
823823

824-
unblockThreads :: Bool -> [ThreadId] -> SimState s a -> ([ThreadId], SimState s a)
824+
unblockThreads :: Bool -> [IOSimThreadId] -> SimState s a -> ([IOSimThreadId], SimState s a)
825825
unblockThreads !onlySTM !wakeup !simstate@SimState {runqueue, threads} =
826826
-- To preserve our invariants (that threadBlocked is correct)
827827
-- we update the runqueue and threads together here
@@ -863,7 +863,7 @@ unblockThreads !onlySTM !wakeup !simstate@SimState {runqueue, threads} =
863863
-- receive a 'ThreadKilled' exception.
864864
--
865865
forkTimeoutInterruptThreads :: forall s a.
866-
[(ThreadId, TimeoutId, TMVar (IOSim s) ThreadId)]
866+
[(IOSimThreadId, TimeoutId, TMVar (IOSim s) IOSimThreadId)]
867867
-> SimState s a
868868
-> ST s (SimState s a)
869869
forkTimeoutInterruptThreads timeoutExpired simState =
@@ -883,13 +883,13 @@ forkTimeoutInterruptThreads timeoutExpired simState =
883883
where
884884
-- we launch a thread responsible for throwing an AsyncCancelled exception
885885
-- to the thread which timeout expired
886-
throwToThread :: [(Thread s a, TMVar (IOSim s) ThreadId)]
886+
throwToThread :: [(Thread s a, TMVar (IOSim s) IOSimThreadId)]
887887

888888
(simState', throwToThread) = List.mapAccumR fn simState timeoutExpired
889889
where
890890
fn :: SimState s a
891-
-> (ThreadId, TimeoutId, TMVar (IOSim s) ThreadId)
892-
-> (SimState s a, (Thread s a, TMVar (IOSim s) ThreadId))
891+
-> (IOSimThreadId, TimeoutId, TMVar (IOSim s) IOSimThreadId)
892+
-> (SimState s a, (Thread s a, TMVar (IOSim s) IOSimThreadId))
893893
fn state@SimState { threads } (tid, tmid, lock) =
894894
let t = case tid `Map.lookup` threads of
895895
Just t' -> t'
@@ -1003,13 +1003,13 @@ removeMinimums = \psq ->
10031003
| p == p' -> collectAll (k:ks) p (x:xs) psq'
10041004
_ -> (reverse ks, p, reverse xs, psq)
10051005

1006-
traceMany :: [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
1006+
traceMany :: [(Time, IOSimThreadId, Maybe ThreadLabel, SimEventType)]
10071007
-> SimTrace a -> SimTrace a
10081008
traceMany [] trace = trace
10091009
traceMany ((time, tid, tlbl, event):ts) trace =
10101010
SimTrace time tid tlbl event (traceMany ts trace)
10111011

1012-
lookupThreadLabel :: ThreadId -> Map ThreadId (Thread s a) -> Maybe ThreadLabel
1012+
lookupThreadLabel :: IOSimThreadId -> Map IOSimThreadId (Thread s a) -> Maybe ThreadLabel
10131013
lookupThreadLabel tid threads = join (threadLabel <$> Map.lookup tid threads)
10141014

10151015

@@ -1041,7 +1041,7 @@ runSimTraceST mainAction = schedule mainThread initialState
10411041

10421042
execAtomically :: forall s a c.
10431043
Time
1044-
-> ThreadId
1044+
-> IOSimThreadId
10451045
-> Maybe ThreadLabel
10461046
-> TVarId
10471047
-> StmA s a
@@ -1341,10 +1341,10 @@ traceTVarST TVar{tvarCurrent, tvarUndo, tvarTrace} new = do
13411341
-- Blocking and unblocking on TVars
13421342
--
13431343

1344-
readTVarBlockedThreads :: TVar s a -> ST s [ThreadId]
1344+
readTVarBlockedThreads :: TVar s a -> ST s [IOSimThreadId]
13451345
readTVarBlockedThreads TVar{tvarBlocked} = fst <$> readSTRef tvarBlocked
13461346

1347-
blockThreadOnTVar :: ThreadId -> TVar s a -> ST s ()
1347+
blockThreadOnTVar :: IOSimThreadId -> TVar s a -> ST s ()
13481348
blockThreadOnTVar tid TVar{tvarBlocked} = do
13491349
(tids, tidsSet) <- readSTRef tvarBlocked
13501350
when (tid `Set.notMember` tidsSet) $ do
@@ -1365,7 +1365,7 @@ unblockAllThreadsFromTVar TVar{tvarBlocked} = do
13651365
-- the var writes that woke them.
13661366
--
13671367
threadsUnblockedByWrites :: [SomeTVar s]
1368-
-> ST s ([ThreadId], Map ThreadId (Set (Labelled TVarId)))
1368+
-> ST s ([IOSimThreadId], Map IOSimThreadId (Set (Labelled TVarId)))
13691369
threadsUnblockedByWrites written = do
13701370
!tidss <- sequence
13711371
[ (,) <$> labelledTVarId tvar <*> readTVarBlockedThreads tvar

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Control.Exception (Exception)
1515
import Control.Concurrent.Class.MonadSTM
1616
import Control.Monad.Class.MonadThrow (MaskingState (..))
1717

18-
import Control.Monad.IOSim.Types (IOSim (..), SimA (..), ThreadId, TimeoutId)
18+
import Control.Monad.IOSim.Types (IOSim (..), SimA (..), IOSimThreadId, TimeoutId)
1919

2020
import GHC.Exts (oneShot)
2121

@@ -43,7 +43,7 @@ data ControlStack s b a where
4343
-> !(ControlStack s c a)
4444
-> ControlStack s b a
4545
TimeoutFrame :: TimeoutId
46-
-> TMVar (IOSim s) ThreadId
46+
-> TMVar (IOSim s) IOSimThreadId
4747
-> (Maybe b -> SimA s c)
4848
-> !(ControlStack s c a)
4949
-> ControlStack s b a
@@ -74,7 +74,7 @@ data ControlStackDash =
7474
| DelayFrame' TimeoutId ControlStackDash
7575
deriving Show
7676

77-
data IsLocked = NotLocked | Locked !ThreadId
77+
data IsLocked = NotLocked | Locked !IOSimThreadId
7878
deriving (Eq, Show)
7979

8080
-- | Unsafe method which removes a timeout.

0 commit comments

Comments
 (0)