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
use memory::*;
use std::fmt::{self, Debug};
use std::sync::{self, Arc};
use std::sync::atomic::{self, AtomicI32};
pub const STEPS_CAP: usize = 15;
#[derive(Copy, Clone, Debug)]
pub struct Schedule {
pub count: u8,
pub steps: [u8; STEPS_CAP],
}
impl Schedule {
pub fn empty() -> Schedule { Schedule { count: 0, steps: [0; STEPS_CAP] } }
pub fn append(self, x: u8) -> Schedule {
assert_ne!(self.count, STEPS_CAP as u8);
let mut s = self;
s.steps[s.count as usize] = x;
s.count += 1;
s
}
pub fn without_last(self) -> Schedule {
let mut s = self;
s.count -= 1;
s
}
pub fn get(&self, idx: u32) -> u8 {
assert!(self.has(idx));
self.steps[idx as usize]
}
pub fn get_or_inf(&self, idx: u32) -> u32 {
if self.has(idx) {
self.get(idx) as u32
} else {
u32::max_value()
}
}
pub fn get_last(&self) -> u8 { self.get(self.count as u32 - 1) }
pub fn is_empty(&self) -> bool { self.count == 0 }
pub fn is_full(&self) -> bool { self.count == STEPS_CAP as u8 }
pub fn count(&self) -> u32 { self.count as u32 }
pub fn has(&self, idx: u32) -> bool { idx < self.count as u32 }
}
impl fmt::Display for Schedule {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@")?;
self.steps[0..(self.count as usize)].fmt(f)
}
}
impl From<&[i32]> for Schedule {
fn from(nums: &[i32]) -> Self {
let mut s = Schedule::empty();
for n in nums {
s.append(*n as u8);
}
s
}
}
#[derive(Clone, Debug)]
pub struct Info {
pub thread_num: u32,
pub schedule: Schedule,
pub turn: Arc<AtomicI32>,
}
impl Info {
pub fn new(schedule: Schedule) -> (Info, Info) {
let i = Info { thread_num: 0, schedule, turn: Arc::new(AtomicI32::new(0))};
let mut j = i.clone();
j.thread_num = 1;
(i, j)
}
}
use std::cell::{Cell, RefCell};
thread_local! {
pub static SCHEDULE_INFO: RefCell<Option<Info>> = RefCell::new(None);
pub static STEP_COUNT_LIMIT: Cell<(u32, u32)> = Cell::new((0, 0));
}
pub fn is_scheduled() -> bool {
SCHEDULE_INFO.with(|c| c.borrow().is_some())
}
pub fn set_schedule(i: Info) {
SCHEDULE_INFO.with(|c| c.replace(Some(i)));
}
pub fn get_step_count_limit() -> (u32, u32) {
STEP_COUNT_LIMIT.with(|c| c.get())
}
pub fn is_step_at_limit() -> bool {
let (count, limit) = get_step_count_limit();
count == limit
}
pub fn inc_step_count() {
STEP_COUNT_LIMIT.with(|c| {
let (count, limit) = c.get();
c.set((count + 1, limit));
})
}
pub fn reset_count_limit(limit: u32) {
STEP_COUNT_LIMIT.with(|c| {
c.set((1, limit));
})
}
pub fn inc_turn() {
SCHEDULE_INFO.with(|c| {
c.borrow().as_ref().unwrap().turn.fetch_add(1, atomic::Ordering::SeqCst);
});
}
pub fn poison_turn() {
SCHEDULE_INFO.with(|c| {
c.borrow().as_ref().unwrap().turn.store(-7, atomic::Ordering::SeqCst);
});
}
pub fn step() {
if !is_scheduled() { return; }
let (count, limit) = get_step_count_limit();
if count < limit {
inc_step_count();
return;
}
println!("Preempt. Passing turn");
inc_turn();
wait_for_turn();
}
pub fn wait_for_turn() {
SCHEDULE_INFO.with(|c| {
let borrowed = c.borrow();
let info = borrowed.as_ref().unwrap();
let turn_mod = info.thread_num;
let mut tries = 1_000;
loop {
tries -= 1;
if tries < 0 {
tries = 1_000;
use std::thread;
use std::time::Duration;
thread::park_timeout(Duration::from_micros(10));
}
let t = info.turn.load(atomic::Ordering::SeqCst);
assert!(t >= 0);
let turn = t as u32;
if turn & 1 == turn_mod {
let lim = info.schedule.get_or_inf(turn);
if lim > 0 {
reset_count_limit(lim);
break;
}
inc_turn();
}
}
});
}
pub fn finished() -> u32 {
inc_turn();
let (count, limit) = get_step_count_limit();
count
}
#[cfg(test)]
mod test {
use super::*;
}