Skip to content

Commit e50acc0

Browse files
committed
io-sim-por: renamed ThreadId constructors
* `RacyThreadId`, previously `ThreadId` * `ThreadId`, previously `TestThreadId`.
1 parent 4206f34 commit e50acc0

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

io-sim/how-to-use-IOSimPOR.txt

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,27 +200,26 @@ small number of tests.
200200
IOSimPOR Scheduling
201201
-------------------
202202

203-
IOSimPOR distinguishes between test threads and system threads; test
204-
threads are intended to be part of the test itself, whereas system
205-
threads are part of the system under test. The main thread in a test
206-
is always a test thread; test threads can fork system threads, but not
207-
vice versa. Test threads always take priority over system threads, and
208-
IOSimPOR does not attempt to reverse races involving test threads. So
209-
when writing a test, one can assume that whatever the test threads do
210-
takes place first (at each simulated time point), and then the system
211-
threads run and any races between them are explored. Likewise, if a
212-
blocked test thread responds to an event in a system thread, then the
213-
test thread's response will take place before the system threads
214-
continue execution. Distinguishing test threads from system threads
215-
drastically reduces the set of races that IOSimPOR needs to consider.
216-
217-
To start a system thread, a test thread may call
203+
IOSimPOR distinguishes between non-racy threads and system threads; test
204+
threads are intended to be part of the test itself, whereas system threads are
205+
part of the system under test. The main thread in a test is always a non-racy
206+
thread; non-racy threads can fork system threads, but not vice versa. Test
207+
threads always take priority over system threads, and IOSimPOR does not
208+
attempt to reverse races involving test threads. So when writing a test, one
209+
can assume that whatever the test threads do takes place first (at each
210+
simulated time point), and then the system threads run and any races between
211+
them are explored. Likewise, if a blocked non-racy thread responds to an event
212+
in a system thread, then the test thread's response will take place before the
213+
system threads continue execution. Distinguishing non-racy threads from system
214+
threads drastically reduces the set of races that IOSimPOR needs to consider.
215+
216+
To start a system thread, a non-racy thread may call
218217

219218
exploreRaces :: MonadTest m => m ()
220219

221220
All threads forked (using forkIO) after such a call will be system
222221
threads. If there is no call to exploreRaces in a test, then all the
223-
threads forked will be test threads, and IOSimPOR will not reverse any
222+
threads forked will be non-racy threads, and IOSimPOR will not reverse any
224223
races---so it is necessary to include a call to exploreRaces somewhere
225224
in a test.
226225

@@ -234,7 +233,7 @@ easy to see which thread forked which other, and relatively easy to
234233
map thread ids in debugging output to threads in the source code.
235234

236235
Except when reversing races, IOSimPOR schedules threads using
237-
priorities. Test threads take priority over system threads, but
236+
priorities. Non-racy threads take priority over system threads, but
238237
otherwise priorities are determined by the thread ids: the thread with
239238
the lexicographically greatest thread Id has the highest priority. As
240239
a result, threads behave in a very predictable manner: newly forked

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,17 +627,17 @@ data SimEventType
627627
type TraceEvent = SimEventType
628628
{-# DEPRECATED TraceEvent "Use 'SimEventType' instead." #-}
629629

630-
data ThreadId = ThreadId [Int]
631-
| TestThreadId [Int] -- test threads have higher priority
630+
data ThreadId = RacyThreadId [Int]
631+
| ThreadId [Int] -- non racy threads have higher priority
632632
deriving (Eq, Ord, Show)
633633

634634
childThreadId :: ThreadId -> Int -> ThreadId
635+
childThreadId (RacyThreadId is) i = RacyThreadId (is ++ [i])
635636
childThreadId (ThreadId is) i = ThreadId (is ++ [i])
636-
childThreadId (TestThreadId is) i = TestThreadId (is ++ [i])
637637

638-
setNonTestThread :: ThreadId -> ThreadId
639-
setNonTestThread (TestThreadId is) = ThreadId is
640-
setNonTestThread tid@ThreadId{} = tid
638+
setRacyThread :: ThreadId -> ThreadId
639+
setRacyThread (ThreadId is) = RacyThreadId is
640+
setRacyThread tid@RacyThreadId{} = tid
641641

642642
newtype TVarId = TVarId Int deriving (Eq, Ord, Enum, Show)
643643
newtype TimeoutId = TimeoutId Int deriving (Eq, Ord, Enum, Show)

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,14 @@ data Thread s a = Thread {
105105
}
106106
deriving Show
107107

108-
isTestThreadId :: ThreadId -> Bool
109-
isTestThreadId (TestThreadId _) = True
110-
isTestThreadId _ = False
108+
isRacyThreadId :: ThreadId -> Bool
109+
isRacyThreadId (RacyThreadId _) = True
110+
isRacyThreadId _ = True
111+
112+
isNotRacyThreadId :: ThreadId -> Bool
113+
isNotRacyThreadId (ThreadId _) = True
114+
isNotRacyThreadId _ = False
115+
111116

112117
bottomVClock :: VectorClock
113118
bottomVClock = VectorClock Map.empty
@@ -449,7 +454,7 @@ schedule thread@Thread{
449454

450455
Fork a k -> do
451456
let nextTId = threadNextTId thread
452-
tid' | threadRacy thread = setNonTestThread $ childThreadId tid nextTId
457+
tid' | threadRacy thread = setRacyThread $ childThreadId tid nextTId
453458
| otherwise = childThreadId tid nextTId
454459
thread' = thread { threadControl = ThreadControl (k tid') ctl,
455460
threadNextTId = nextTId + 1,
@@ -913,7 +918,7 @@ controlSimTraceST limit control mainAction =
913918
where
914919
mainThread =
915920
Thread {
916-
threadId = TestThreadId [],
921+
threadId = ThreadId [],
917922
threadControl = ThreadControl (runIOSim mainAction) MainFrame,
918923
threadBlocked = False,
919924
threadDone = False,
@@ -923,7 +928,7 @@ controlSimTraceST limit control mainAction =
923928
threadLabel = Just "main",
924929
threadNextTId = 1,
925930
threadStep = 0,
926-
threadVClock = insertVClock (TestThreadId []) 0 bottomVClock,
931+
threadVClock = insertVClock (ThreadId []) 0 bottomVClock,
927932
threadEffect = mempty,
928933
threadRacy = False
929934
}
@@ -1353,7 +1358,7 @@ updateRaces newStep@Step{ stepThreadId = tid, stepEffect = newEffect }
13531358

13541359
-- a new step cannot race with any threads that it just woke up
13551360
new :: [StepInfo]
1356-
new | isTestThreadId tid = [] -- test threads do not race
1361+
new | isNotRacyThreadId tid = [] -- non-racy threads do not race
13571362
| Set.null newConcurrent = [] -- cannot race with anything
13581363
| justBlocking = [] -- no need to defer a blocking transaction
13591364
| otherwise =
@@ -1373,7 +1378,7 @@ updateRaces newStep@Step{ stepThreadId = tid, stepEffect = newEffect }
13731378
-- then any threads that it wakes up become non-concurrent also.
13741379
let lessConcurrent = foldr Set.delete concurrent (effectWakeup newEffect) in
13751380
if tid `elem` concurrent then
1376-
let theseStepsRace = not (isTestThreadId tid) && racingSteps step newStep
1381+
let theseStepsRace = isRacyThreadId tid && racingSteps step newStep
13771382
happensBefore = step `happensBeforeStep` newStep
13781383
nondep' | happensBefore = nondep
13791384
| otherwise = newStep : nondep

0 commit comments

Comments
 (0)