1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use crate::rt::{execution, thread};
#[cfg(feature = "checkpoint")]
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
/// An execution path
#[derive(Debug)]
#[cfg_attr(feature = "checkpoint", derive(Serialize, Deserialize))]
pub(crate) struct Path {
preemption_bound: Option<usize>,
/// Current execution's position in the branch index.
///
/// When the execution starts, this is zero, but `branches` might not be
/// empty.
///
/// In order to perform an exhaustive search, the execution is seeded with a
/// set of branches.
pos: usize,
/// Sequence of all decisions in a loom execution that can be permuted.
///
/// This vec tracks the branch kind and index into one of the vecs below.
/// Each branch kind is tracked separately to make backtracking algorithms
/// simpler.
branches: Vec<Branch>,
/// Tracks threads to be scheduled
schedules: Vec<Schedule>,
/// Atomic writes
writes: Vec<VecDeque<usize>>,
/// Tracks spurious notifications
spurious: Vec<VecDeque<bool>>,
/// Maximum number of branches to explore
max_branches: usize,
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "checkpoint", derive(Serialize, Deserialize))]
enum Branch {
Schedule(usize),
Write(usize),
Spurious(usize),
}
#[derive(Debug)]
#[cfg_attr(feature = "checkpoint", derive(Serialize, Deserialize))]
pub(crate) struct Schedule {
pub(crate) preemptions: usize,
pub(crate) initial_active: Option<usize>,
pub(crate) threads: Vec<Thread>,
init_threads: Vec<Thread>,
}
#[derive(Debug, Eq, PartialEq, Clone)]
#[cfg_attr(feature = "checkpoint", derive(Serialize, Deserialize))]
pub(crate) enum Thread {
/// The thread is currently disabled
Disabled,
/// The thread should not be explored
Skip,
/// The thread is in a yield state.
Yield,
/// The thread is waiting to be explored
Pending,
/// The thread is currently being explored
Active,
/// The thread has been explored
Visited,
}
impl Path {
/// New Path
pub(crate) fn new(max_branches: usize, preemption_bound: Option<usize>) -> Path {
Path {
preemption_bound,
branches: vec![],
pos: 0,
schedules: vec![],
writes: vec![],
spurious: vec![],
max_branches,
}
}
pub(crate) fn pos(&self) -> usize {
self.pos
}
/// Returns the atomic write to read
pub(crate) fn branch_write<I>(&mut self, seed: I) -> usize
where
I: Iterator<Item = usize>,
{
use self::Branch::Write;
assert!(
self.branches.len() < self.max_branches,
"actual = {}",
self.branches.len()
);
if self.pos == self.branches.len() {
let i = self.writes.len();
let writes: VecDeque<_> = seed.collect();
self.writes.push(writes);
self.branches.push(Branch::Write(i));
}
let i = match self.branches[self.pos] {
Write(i) => i,
_ => panic!("path entry {} is not a write", self.pos),
};
self.pos += 1;
self.writes[i][0]
}
/// Branch on spurious notifications
pub(crate) fn branch_spurious(&mut self) -> bool {
use self::Branch::Spurious;
assert!(
self.branches.len() < self.max_branches,
"actual = {}",
self.branches.len()
);
if self.pos == self.branches.len() {
let i = self.spurious.len();
let spurious: VecDeque<_> = vec![false, true].into();
self.spurious.push(spurious);
self.branches.push(Branch::Spurious(i));
}
let i = match self.branches[self.pos] {
Spurious(i) => i,
_ => panic!("path entry {} is not a spurious wait", self.pos),
};
self.pos += 1;
self.spurious[i][0]
}
/// Returns the thread identifier to schedule
pub(crate) fn branch_thread<I>(
&mut self,
execution_id: execution::Id,
seed: I,
) -> Option<thread::Id>
where
I: Iterator<Item = Thread>,
{
assert!(
self.branches.len() < self.max_branches,
"actual = {}",
self.branches.len()
);
if self.pos == self.branches.len() {
// Entering a new exploration space.
let i = self.schedules.len();
let mut threads: Vec<_> = seed.collect();
let num_active = threads.iter().filter(|th| th.is_active()).count();
assert!(num_active <= 1, "num_active = {}", num_active);
// Ensure at least one thread is active, otherwise toggle a yielded
// thread.
if num_active == 0 {
for th in &mut threads {
if *th == Thread::Yield {
*th = Thread::Active;
}
}
}
let curr_active = active(&threads);
let initial_active = if let Some(prev) = self.schedules.last() {
if curr_active == active(&prev.threads) {
curr_active
} else {
None
}
} else {
curr_active
};
let preemptions = if let Some(prev) = self.schedules.last() {
let mut preemptions = prev.preemptions;
if prev.initial_active.is_some() && prev.initial_active != active(&prev.threads) {
preemptions += 1;
}
preemptions
} else {
0
};
let threads_clone = threads.clone();
self.schedules.push(Schedule {
preemptions,
threads,
initial_active,
init_threads: threads_clone,
});
self.branches.push(Branch::Schedule(i));
}
let i = match self.branches[self.pos] {
Branch::Schedule(i) => i,
_ => panic!(),
};
self.pos += 1;
let threads = &mut self.schedules[i].threads;
threads
.iter_mut()
.enumerate()
.find(|&(_, ref th)| th.is_active())
.map(|(i, _)| thread::Id::new(execution_id, i))
}
pub(crate) fn backtrack(&mut self, point: usize, thread_id: thread::Id) {
let index = match self.branches[point] {
Branch::Schedule(index) => index,
_ => panic!(),
};
// Exhaustive DPOR only requires adding this backtrack point
self.schedules[index].backtrack(thread_id, self.preemption_bound);
if self.preemption_bound.is_some() {
if index > 0 {
for j in (1..index).rev() {
// Preemption bounded DPOR requires conservatively adding another
// backtrack point to cover cases missed by the bounds.
if active(&self.schedules[j].threads) != active(&self.schedules[j - 1].threads)
{
self.schedules[j].backtrack(thread_id, self.preemption_bound);
return;
}
}
self.schedules[0].backtrack(thread_id, self.preemption_bound);
}
}
}
/// Returns `false` if there are no more paths to explore
pub(crate) fn step(&mut self) -> bool {
use self::Branch::*;
self.pos = 0;
while self.branches.len() > 0 {
match self.branches.last().unwrap() {
&Schedule(i) => {
// Transition the active thread to visited.
self.schedules[i]
.threads
.iter_mut()
.find(|th| th.is_active())
.map(|th| *th = Thread::Visited);
// Find a pending thread and transition it to active
let rem = self.schedules[i]
.threads
.iter_mut()
.find(|th| th.is_pending())
.map(|th| {
*th = Thread::Active;
})
.is_some();
if !rem {
self.branches.pop();
self.schedules.pop();
continue;
}
}
&Write(i) => {
self.writes[i].pop_front();
if self.writes[i].is_empty() {
self.branches.pop();
self.writes.pop();
continue;
}
}
&Spurious(i) => {
self.spurious[i].pop_front();
if self.spurious[i].is_empty() {
self.branches.pop();
self.spurious.pop();
continue;
}
}
}
return true;
}
false
}
}
impl Schedule {
fn backtrack(&mut self, thread_id: thread::Id, preemption_bound: Option<usize>) {
if let Some(bound) = preemption_bound {
assert!(self.preemptions <= bound, "actual = {}", self.preemptions);
if self.preemptions == bound {
return;
}
}
let thread_id = thread_id.as_usize();
if thread_id >= self.threads.len() {
return;
}
if self.threads[thread_id].is_enabled() {
self.threads[thread_id].explore();
} else {
for th in &mut self.threads {
th.explore();
}
}
}
}
impl Thread {
fn explore(&mut self) {
match *self {
Thread::Skip => {
*self = Thread::Pending;
}
_ => {}
}
}
fn is_pending(&self) -> bool {
match *self {
Thread::Pending => true,
_ => false,
}
}
fn is_active(&self) -> bool {
match *self {
Thread::Active => true,
_ => false,
}
}
fn is_enabled(&self) -> bool {
!self.is_disabled()
}
fn is_disabled(&self) -> bool {
*self == Thread::Disabled
}
}
fn active(threads: &[Thread]) -> Option<usize> {
// Get the index of the currently active thread
threads
.iter()
.enumerate()
.find(|(_, th)| th.is_active())
.map(|(index, _)| index)
}