-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
74 lines (64 loc) · 1.7 KB
/
main.rs
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
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
// uncomment for incorrect (relaxed)
const ACK: Ordering = Ordering::Relaxed;
const REL: Ordering = Ordering::Relaxed;
// uncomment for correct (acquire/release)
//const ACK: Ordering = Ordering::Acquire;
//const REL: Ordering = Ordering::Release;
static FLAG: AtomicBool = AtomicBool::new(false);
static mut SHARED_VALUE: u32 = 0;
#[inline(never)]
fn do_busy_work(v: *mut i32) {
loop {
let i: i32 = rand::random();
unsafe {
std::ptr::write_volatile(v, i);
}
if (i & 7) == 0 {
return;
}
}
}
const INCREMENTS: usize = 10_000_000;
#[inline(never)]
fn increment_shared_value() {
let mut count = 0;
while count < INCREMENTS {
let mut v = 0;
do_busy_work(&mut v as _);
if FLAG
.compare_exchange(false, true, ACK, Ordering::Relaxed)
.is_ok()
{
// increment
unsafe {
SHARED_VALUE += 1;
}
// store
FLAG.store(false, REL);
// counter
count += 1;
}
}
}
pub fn main() {
let threads_count = std::env::args()
.nth(1)
.unwrap_or_else(|| "2".into())
.parse()
.expect("the first argument to be a thread count");
let mut threads = Vec::with_capacity(threads_count);
loop {
unsafe {
SHARED_VALUE = 0;
}
for _ in 0..threads_count {
threads.push(thread::spawn(increment_shared_value));
}
for t in threads.drain(..) {
t.join().unwrap();
}
println!("shared value = {}", unsafe { SHARED_VALUE });
}
}