Skip to content

Commit 8830b00

Browse files
committed
io-sim-por: shuffle module structure
The following order makes things easier to read: * Effects * Steps * Races * Schedule control * Schedule modifications
1 parent 8ace872 commit 8830b00

File tree

1 file changed

+58
-46
lines changed

1 file changed

+58
-46
lines changed

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

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

108+
threadStepId :: Thread s a -> (ThreadId, Int)
109+
threadStepId Thread{ threadId, threadStep } = (threadId, threadStep)
110+
108111
isRacyThreadId :: ThreadId -> Bool
109112
isRacyThreadId (RacyThreadId _) = True
110113
isRacyThreadId _ = True
@@ -1215,7 +1218,9 @@ ordNub = go Set.empty
12151218
| x `Set.member` s = go s xs
12161219
| otherwise = x : go (Set.insert x s) xs
12171220

1221+
--
12181222
-- Effects
1223+
--
12191224

12201225
data Effect = Effect {
12211226
effectReads :: !(Set TVarId),
@@ -1276,7 +1281,9 @@ racingEffects e e' =
12761281
effectLiftST eff
12771282
|| not (Set.null (effectReads eff) && Set.null (effectWrites eff))
12781283

1284+
--
12791285
-- Steps
1286+
--
12801287

12811288
data Step = Step {
12821289
stepThreadId :: !ThreadId,
@@ -1336,6 +1343,10 @@ data StepInfo = StepInfo {
13361343
}
13371344
deriving Show
13381345

1346+
--
1347+
-- Races
1348+
--
1349+
13391350
data Races = Races { -- These steps may still race with future steps
13401351
activeRaces :: ![StepInfo],
13411352
-- These steps cannot be concurrent with future steps
@@ -1467,55 +1478,24 @@ traceRaces r = r
14671478
-- traceRaces r@Races{activeRaces,completeRaces} =
14681479
-- Debug.trace ("Tracking "++show (length (concatMap stepInfoRaces activeRaces)) ++" races") r
14691480

1470-
-- Schedule modifications
1471-
1472-
stepStepId :: Step -> (ThreadId, Int)
1473-
stepStepId Step{ stepThreadId = tid, stepStep = n } = (tid,n)
1474-
1475-
threadStepId :: Thread s a -> (ThreadId, Int)
1476-
threadStepId Thread{ threadId, threadStep } = (threadId, threadStep)
1477-
1478-
stepInfoToScheduleMods :: StepInfo -> [ScheduleMod]
1479-
stepInfoToScheduleMods
1480-
StepInfo{ stepInfoStep = step,
1481-
stepInfoControl = control,
1482-
stepInfoNonDep = nondep,
1483-
stepInfoRaces = races
1484-
} =
1485-
-- It is actually possible for a later step that races with an earlier one
1486-
-- not to *depend* on it in a happens-before sense. But we don't want to try
1487-
-- to follow any steps *after* the later one.
1488-
[ ScheduleMod
1489-
{ scheduleModTarget = stepStepId step
1490-
, scheduleModControl = control
1491-
, scheduleModInsertion = takeWhile (/=stepStepId step')
1492-
(map stepStepId (reverse nondep))
1493-
++ [stepStepId step']
1494-
-- It should be unnecessary to include the delayed
1495-
-- step in the insertion, since the default
1496-
-- scheduling should run it anyway. Removing it may
1497-
-- help avoid redundant schedules.
1498-
-- ++ [stepStepId step]
1499-
}
1500-
| step' <- races ]
1501-
1502-
traceFinalRacesFound :: SimState s a -> SimTrace a -> SimTrace a
1503-
traceFinalRacesFound simstate@SimState{ control0 = control } =
1504-
TraceRacesFound [extendScheduleControl control m | m <- scheduleMods]
1505-
where SimState{ races } =
1506-
quiescentRacesInSimState simstate
1507-
scheduleMods =
1508-
concatMap stepInfoToScheduleMods $ completeRaces races
1509-
15101481

1482+
--
15111483
-- Schedule control
1484+
--
15121485

15131486
controlTargets :: StepId -> ScheduleControl -> Bool
15141487
controlTargets stepId
15151488
(ControlAwait (ScheduleMod{ scheduleModTarget }:_)) =
15161489
stepId == scheduleModTarget
15171490
controlTargets _stepId _ = False
15181491

1492+
followControl :: ScheduleControl -> ScheduleControl
1493+
followControl (ControlAwait (ScheduleMod { scheduleModInsertion } : mods)) =
1494+
ControlFollow scheduleModInsertion mods
1495+
followControl (ControlAwait []) = error "Impossible: followControl (ControlAwait [])"
1496+
followControl ControlDefault{} = error "Impossible: followControl ControlDefault{}"
1497+
followControl ControlFollow{} = error "Impossible: followControl ControlFollow{}"
1498+
15191499
controlFollows :: StepId -> ScheduleControl -> Bool
15201500
controlFollows _stepId ControlDefault = True
15211501
controlFollows _stepId (ControlFollow [] _) = True
@@ -1545,12 +1525,44 @@ advanceControl stepId control =
15451525
assert (not $ controlTargets stepId control) $
15461526
control
15471527

1548-
followControl :: ScheduleControl -> ScheduleControl
1549-
followControl (ControlAwait (ScheduleMod { scheduleModInsertion } : mods)) =
1550-
ControlFollow scheduleModInsertion mods
1551-
followControl (ControlAwait []) = error "Impossible: followControl (ControlAwait [])"
1552-
followControl ControlDefault{} = error "Impossible: followControl ControlDefault{}"
1553-
followControl ControlFollow{} = error "Impossible: followControl ControlFollow{}"
1528+
--
1529+
-- Schedule modifications
1530+
--
1531+
1532+
stepStepId :: Step -> (ThreadId, Int)
1533+
stepStepId Step{ stepThreadId = tid, stepStep = n } = (tid,n)
1534+
1535+
stepInfoToScheduleMods :: StepInfo -> [ScheduleMod]
1536+
stepInfoToScheduleMods
1537+
StepInfo{ stepInfoStep = step,
1538+
stepInfoControl = control,
1539+
stepInfoNonDep = nondep,
1540+
stepInfoRaces = races
1541+
} =
1542+
-- It is actually possible for a later step that races with an earlier one
1543+
-- not to *depend* on it in a happens-before sense. But we don't want to try
1544+
-- to follow any steps *after* the later one.
1545+
[ ScheduleMod
1546+
{ scheduleModTarget = stepStepId step
1547+
, scheduleModControl = control
1548+
, scheduleModInsertion = takeWhile (/=stepStepId step')
1549+
(map stepStepId (reverse nondep))
1550+
++ [stepStepId step']
1551+
-- It should be unnecessary to include the delayed
1552+
-- step in the insertion, since the default
1553+
-- scheduling should run it anyway. Removing it may
1554+
-- help avoid redundant schedules.
1555+
-- ++ [stepStepId step]
1556+
}
1557+
| step' <- races ]
1558+
1559+
traceFinalRacesFound :: SimState s a -> SimTrace a -> SimTrace a
1560+
traceFinalRacesFound simstate@SimState{ control0 = control } =
1561+
TraceRacesFound [extendScheduleControl control m | m <- scheduleMods]
1562+
where SimState{ races } =
1563+
quiescentRacesInSimState simstate
1564+
scheduleMods =
1565+
concatMap stepInfoToScheduleMods $ completeRaces races
15541566

15551567
-- Extend an existing schedule control with a newly discovered schedule mod
15561568
extendScheduleControl' :: ScheduleControl -> ScheduleMod -> ScheduleControl

0 commit comments

Comments
 (0)