Skip to content

Commit 163443f

Browse files
committed
io-sim-por: stylistic changes
1 parent 204d5c0 commit 163443f

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,21 @@ ppEvents events =
338338
runSimTrace :: forall a. (forall s. IOSim s a) -> SimTrace a
339339
runSimTrace mainAction = runST (runSimTraceST mainAction)
340340

341-
controlSimTrace :: forall a. Maybe Int -> ScheduleControl -> (forall s. IOSim s a) -> SimTrace a
342-
controlSimTrace limit control mainAction = runST (controlSimTraceST limit control mainAction)
343-
344-
exploreSimTrace ::
345-
forall a test. (Testable test) =>
346-
(ExplorationOptions->ExplorationOptions) ->
347-
(forall s. IOSim s a) -> (Maybe (SimTrace a) -> SimTrace a -> test) -> Property
341+
controlSimTrace :: forall a.
342+
Maybe Int
343+
-> ScheduleControl
344+
-- ^ note: must be either `ControlDefault` or `ControlAwait`.
345+
-> (forall s. IOSim s a)
346+
-> SimTrace a
347+
controlSimTrace limit control mainAction =
348+
runST (controlSimTraceST limit control mainAction)
349+
350+
exploreSimTrace
351+
:: forall a test. Testable test
352+
=> (ExplorationOptions -> ExplorationOptions)
353+
-> (forall s. IOSim s a)
354+
-> (Maybe (SimTrace a) -> SimTrace a -> test)
355+
-> Property
348356
exploreSimTrace optsf mainAction k =
349357
case explorationReplay opts of
350358
Nothing ->

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

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ happensBeforeStep step step' =
130130
(getVectorClock $ stepVClock step')
131131

132132
labelledTVarId :: TVar s a -> ST s (Labelled TVarId)
133-
labelledTVarId TVar { tvarId, tvarLabel } = (Labelled tvarId) <$> readSTRef tvarLabel
133+
labelledTVarId TVar { tvarId, tvarLabel } = Labelled tvarId <$> readSTRef tvarLabel
134134

135135
labelledThreads :: Map ThreadId (Thread s a) -> [Labelled ThreadId]
136136
labelledThreads threadMap =
@@ -892,7 +892,7 @@ removeMinimums = \psq ->
892892

893893
traceMany :: [(Time, ThreadId, Maybe ThreadLabel, SimEventType)]
894894
-> SimTrace a -> SimTrace a
895-
traceMany [] trace = trace
895+
traceMany [] trace = trace
896896
traceMany ((time, tid, tlbl, event):ts) trace =
897897
SimTrace time tid tlbl event (traceMany ts trace)
898898

@@ -910,7 +910,10 @@ runSimTraceST mainAction = controlSimTraceST Nothing ControlDefault mainAction
910910

911911
controlSimTraceST :: Maybe Int -> ScheduleControl -> IOSim s a -> ST s (SimTrace a)
912912
controlSimTraceST limit control mainAction =
913-
schedule mainThread initialState{ control = control, control0 = control, perStepTimeLimit = limit }
913+
schedule mainThread initialState { control = control,
914+
control0 = control,
915+
perStepTimeLimit = limit
916+
}
914917
where
915918
mainThread =
916919
Thread {
@@ -1242,7 +1245,7 @@ someTvarId :: SomeTVar s -> TVarId
12421245
someTvarId (SomeTVar r) = tvarId r
12431246

12441247
onlyReadEffect :: Effect -> Bool
1245-
onlyReadEffect e@Effect { effectReads } = e == mempty { effectReads }
1248+
onlyReadEffect e = e { effectReads = effectReads mempty } == mempty
12461249

12471250
racingEffects :: Effect -> Effect -> Bool
12481251
racingEffects e e' =
@@ -1267,11 +1270,13 @@ data Step = Step {
12671270
deriving Show
12681271

12691272
-- steps race if they can be reordered with a possibly different outcome
1270-
racingSteps :: Step -> Step -> Bool
1273+
racingSteps :: Step -- ^ an earlier step
1274+
-> Step -- ^ a later step
1275+
-> Bool
12711276
racingSteps s s' =
12721277
stepThreadId s /= stepThreadId s'
12731278
&& not (stepThreadId s' `elem` effectWakeup (stepEffect s))
1274-
&& (racingEffects (stepEffect s) (stepEffect s')
1279+
&& (stepEffect s `racingEffects` stepEffect s'
12751280
|| throwsTo s s'
12761281
|| throwsTo s' s)
12771282
where throwsTo s1 s2 =
@@ -1333,6 +1338,9 @@ updateRacesInSimState thread SimState{ control, threads, races } =
13331338
(Map.keysSet (Map.filter (not . threadDone) threads))
13341339
races
13351340

1341+
-- | 'updateRaces' turns a current 'Step' into 'StepInfo', and updates all
1342+
-- 'activeRaces'.
1343+
--
13361344
-- We take care that steps can only race against threads in their
13371345
-- concurrent set. When this becomes empty, a step can be retired into
13381346
-- the "complete" category, but only if there are some steps racing
@@ -1343,9 +1351,13 @@ updateRaces newStep@Step{ stepThreadId = tid, stepEffect = newEffect }
13431351
control
13441352
newConcurrent0
13451353
races@Races{ activeRaces } =
1346-
let -- a new step cannot race with any threads that it just woke up
1347-
newConcurrent = foldr Set.delete newConcurrent0 (effectWakeup newEffect)
1348-
new | isTestThreadId tid = [] -- test threads do not race
1354+
1355+
let justBlocking :: Bool
1356+
justBlocking = blocking && onlyReadEffect newEffect
1357+
1358+
-- a new step cannot race with any threads that it just woke up
1359+
new :: [StepInfo]
1360+
new | isTestThreadId tid = [] -- test threads do not race
13491361
| Set.null newConcurrent = [] -- cannot race with anything
13501362
| justBlocking = [] -- no need to defer a blocking transaction
13511363
| otherwise =
@@ -1355,14 +1367,18 @@ updateRaces newStep@Step{ stepThreadId = tid, stepEffect = newEffect }
13551367
stepInfoNonDep = [],
13561368
stepInfoRaces = []
13571369
}]
1358-
justBlocking = blocking && onlyReadEffect newEffect
1359-
updateActive =
1370+
where
1371+
newConcurrent :: Set ThreadId
1372+
newConcurrent = foldr Set.delete newConcurrent0 (effectWakeup newEffect)
1373+
1374+
activeRaces' :: [StepInfo]
1375+
activeRaces' =
13601376
[ -- if this step depends on the previous step, or is not concurrent,
13611377
-- then any threads that it wakes up become non-concurrent also.
13621378
let lessConcurrent = foldr Set.delete concurrent (effectWakeup newEffect) in
13631379
if tid `elem` concurrent then
13641380
let theseStepsRace = not (isTestThreadId tid) && racingSteps step newStep
1365-
happensBefore = happensBeforeStep step newStep
1381+
happensBefore = step `happensBeforeStep` newStep
13661382
nondep' | happensBefore = nondep
13671383
| otherwise = newStep : nondep
13681384
-- We will only record the first race with each thread---reversing
@@ -1378,18 +1394,22 @@ updateRaces newStep@Step{ stepThreadId = tid, stepEffect = newEffect }
13781394
control == ControlFollow [] []) &&
13791395
theseStepsRace = newStep : stepRaces
13801396
| otherwise = stepRaces
1381-
in stepInfo { stepInfoConcurrent = effectForks newEffect `Set.union` concurrent',
1397+
1398+
in stepInfo { stepInfoConcurrent = effectForks newEffect
1399+
`Set.union` concurrent',
13821400
stepInfoNonDep = nondep',
13831401
stepInfoRaces = stepRaces'
13841402
}
1403+
13851404
else stepInfo { stepInfoConcurrent = lessConcurrent }
1405+
13861406
| stepInfo@StepInfo { stepInfoStep = step,
13871407
stepInfoConcurrent = concurrent,
13881408
stepInfoNonDep = nondep,
13891409
stepInfoRaces = stepRaces
13901410
}
13911411
<- activeRaces ]
1392-
in normalizeRaces $ races { activeRaces = new ++ updateActive }
1412+
in normalizeRaces $ races { activeRaces = new ++ activeRaces' }
13931413

13941414
-- When a thread terminates, we remove it from the concurrent thread
13951415
-- sets of active races.
@@ -1420,10 +1440,10 @@ quiescentRacesInSimState simstate@SimState{ races } =
14201440
quiescentRaces :: Races -> Races
14211441
quiescentRaces Races{ activeRaces, completeRaces } =
14221442
Races{ activeRaces = [],
1423-
completeRaces = [s{stepInfoConcurrent = Set.empty} |
1424-
s <- activeRaces,
1425-
not (null (stepInfoRaces s))] ++
1426-
completeRaces }
1443+
completeRaces = [ s{stepInfoConcurrent = Set.empty}
1444+
| s <- activeRaces
1445+
, not (null (stepInfoRaces s))
1446+
] ++ completeRaces }
14271447

14281448
traceRaces :: Races -> Races
14291449
traceRaces r = r
@@ -1448,15 +1468,18 @@ stepInfoToScheduleMods
14481468
-- It is actually possible for a later step that races with an earlier one
14491469
-- not to *depend* on it in a happens-before sense. But we don't want to try
14501470
-- to follow any steps *after* the later one.
1451-
[ ScheduleMod (stepStepId step)
1452-
control
1453-
(takeWhile (/=stepStepId step')
1454-
(map stepStepId (reverse nondep))
1455-
++ [stepStepId step'])
1456-
-- It should be unnecessary to include the delayed step in the insertion,
1457-
-- since the default scheduling should run it anyway. Removing it may
1458-
-- help avoid redundant schedules.
1459-
-- ++ [stepStepId step])
1471+
[ ScheduleMod
1472+
{ scheduleModTarget = stepStepId step
1473+
, scheduleModControl = control
1474+
, scheduleModInsertion = takeWhile (/=stepStepId step')
1475+
(map stepStepId (reverse nondep))
1476+
++ [stepStepId step']
1477+
-- It should be unnecessary to include the delayed
1478+
-- step in the insertion, since the default
1479+
-- scheduling should run it anyway. Removing it may
1480+
-- help avoid redundant schedules.
1481+
-- ++ [stepStepId step]
1482+
}
14601483
| step' <- races ]
14611484

14621485
traceFinalRacesFound :: SimState s a -> SimTrace a -> SimTrace a
@@ -1502,10 +1525,8 @@ advanceControl stepId c =
15021525
c
15031526

15041527
followControl :: ScheduleControl -> ScheduleControl
1505-
followControl (ControlAwait
1506-
(ScheduleMod{scheduleModTarget,
1507-
scheduleModInsertion} : mods)) =
1508-
ControlFollow scheduleModInsertion mods
1528+
followControl (ControlAwait (ScheduleMod { scheduleModInsertion } : mods)) =
1529+
ControlFollow scheduleModInsertion mods
15091530
followControl (ControlAwait []) = error "Impossible: followControl (ControlAwait [])"
15101531
followControl ControlDefault{} = error "Impossible: followControl ControlDefault{}"
15111532
followControl ControlFollow{} = error "Impossible: followControl ControlFollow{}"

0 commit comments

Comments
 (0)