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
#![deny(missing_docs)]
pub mod noinline;
pub mod race_cell;
use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
Barrier,
},
thread,
time::{Duration, Instant},
};
pub fn concurrent_test_2(f1: impl FnOnce() + Send + 'static,
f2: impl FnOnce() + Send + 'static) {
let barrier1 = Arc::new(Barrier::new(2));
let barrier2 = barrier1.clone();
let thread1 = thread::spawn(move || {
barrier1.wait();
noinline::call_once(f1);
});
barrier2.wait();
noinline::call_once(f2);
thread1.join().unwrap();
}
pub fn concurrent_test_3(f1: impl FnOnce() + Send + 'static,
f2: impl FnOnce() + Send + 'static,
f3: impl FnOnce() + Send + 'static) {
let barrier1 = Arc::new(Barrier::new(3));
let barrier2 = barrier1.clone();
let barrier3 = barrier1.clone();
let thread1 = thread::spawn(move || {
barrier1.wait();
noinline::call_once(f1);
});
let thread2 = thread::spawn(move || {
barrier2.wait();
noinline::call_once(f2);
});
barrier3.wait();
noinline::call_once(f3);
thread1.join().unwrap();
thread2.join().unwrap();
}
pub fn benchmark(num_iterations: u32,
mut iteration: impl FnMut()) {
let start_time = Instant::now();
for _ in 0..num_iterations {
noinline::call_mut(&mut iteration);
}
let total_duration = start_time.elapsed();
let total_ms = (total_duration.as_secs() as u32) * 1000 +
total_duration.subsec_nanos() / 1_000_000;
let hundred_iter_duration = total_duration * 100 / num_iterations;
let iter_duration = hundred_iter_duration / 100;
assert_eq!(iter_duration.as_secs(), 0);
let iter_ns = iter_duration.subsec_nanos();
let iter_ns_fraction = hundred_iter_duration.subsec_nanos() % 100;
print!("{} ms ({} iters, ~{}.{} ns/iter): ",
total_ms,
num_iterations,
iter_ns,
iter_ns_fraction);
}
pub fn concurrent_benchmark(
num_iterations: u32,
iteration_func: impl FnMut(),
mut antagonist_func: impl FnMut() + Send + 'static
) {
let barrier = Arc::new(Barrier::new(2));
let run_flag = Arc::new(AtomicBool::new(true));
let (antag_barrier, antag_run_flag) = (barrier.clone(), run_flag.clone());
let antagonist = thread::spawn(
move || {
antag_barrier.wait();
while antag_run_flag.load(Ordering::Relaxed) {
noinline::call_mut(&mut antagonist_func);
}
}
);
barrier.wait();
thread::sleep(Duration::from_millis(10));
benchmark(num_iterations, iteration_func);
run_flag.store(false, Ordering::Relaxed);
antagonist.join().unwrap();
}
#[cfg(test)]
mod tests {
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
#[test]
fn swap_and_fetch_add() {
const ATOMIC_OPS_COUNT: usize = 100_000_000;
let atom = Arc::new(AtomicUsize::new(0));
let atom2 = atom.clone();
let mut last_value = 0;
::concurrent_test_2(
move || {
for _ in 1..(ATOMIC_OPS_COUNT + 1) {
let former_atom = atom.fetch_add(1, Ordering::Relaxed);
assert!((former_atom == 0) || (former_atom == last_value));
last_value = former_atom+1;
}
},
move || {
for _ in 1..(ATOMIC_OPS_COUNT + 1) {
let former_atom = atom2.swap(0, Ordering::Relaxed);
assert!(former_atom <= ATOMIC_OPS_COUNT);
}
}
);
}
#[test]
fn fetch_and_or_xor() {
const ATOMIC_OPS_COUNT: usize = 30_000_000;
let atom = Arc::new(AtomicUsize::new(0));
let atom2 = atom.clone();
let atom3 = atom.clone();
const AND_MASK: usize = 0b0000_0000_0000_0000;
const XOR_MASK: usize = 0b0000_1111_0000_1111;
const OR_MASK: usize = 0b1111_0000_1111_0000;
::concurrent_test_3(
move || {
for _ in 1..(ATOMIC_OPS_COUNT + 1) {
let old_val = atom.fetch_and(AND_MASK, Ordering::Relaxed);
assert_eq!(old_val & 0b1111_1111_1111_1111, old_val);
assert!((old_val & XOR_MASK == XOR_MASK) ||
(old_val & XOR_MASK == 0));
assert!((old_val & OR_MASK == OR_MASK) ||
(old_val & OR_MASK == 0));
}
},
move || {
for _ in 1..(ATOMIC_OPS_COUNT + 1) {
let old_val = atom2.fetch_or(OR_MASK, Ordering::Relaxed);
assert_eq!(old_val & 0b1111_1111_1111_1111, old_val);
assert!((old_val & XOR_MASK == XOR_MASK) ||
(old_val & XOR_MASK == 0));
assert!((old_val & OR_MASK == OR_MASK) ||
(old_val & OR_MASK == 0));
}
},
move || {
for _ in 1..(ATOMIC_OPS_COUNT + 1) {
let old_val = atom3.fetch_xor(XOR_MASK, Ordering::Relaxed);
assert_eq!(old_val & 0b1111_1111_1111_1111, old_val);
assert!((old_val & XOR_MASK == XOR_MASK) ||
(old_val & XOR_MASK == 0));
assert!((old_val & OR_MASK == OR_MASK) ||
(old_val & OR_MASK == 0));
}
}
);
}
}
#[cfg(test)]
mod benchs {
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
};
#[test]
#[ignore]
fn bench_relaxed() {
let atom = AtomicUsize::new(0);
::benchmark(930_000_000, || {
atom.fetch_add(1, Ordering::Relaxed);
});
}
#[test]
#[ignore]
fn bench_seqcst() {
let atom = Arc::new(AtomicUsize::new(0));
let atom2 = atom.clone();
::concurrent_benchmark(
110_000_000,
move || { atom.fetch_add(1, Ordering::SeqCst); },
move || { atom2.fetch_add(1, Ordering::SeqCst); }
);
}
}