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
use crate::sem::{Semaphore, Sem};
use core::ptr;
use core::sync::atomic::{AtomicU8, AtomicU32, AtomicPtr, Ordering};
const INCOMPLETE: u8 = 0x0;
const RUNNING: u8 = 0x1;
const COMPLETE: u8 = 0x2;
const FAIL: u8 = 0x4;
const FAIL_MSG: &str = "Unable to initialize semaphore";
struct StateGuard<'a> {
state: &'a AtomicU8,
}
impl<'a> Drop for StateGuard<'a> {
fn drop(&mut self) {
self.state.store(COMPLETE, Ordering::Release);
}
}
struct SemGuard {
sem: Sem,
waiting: AtomicU32,
}
impl SemGuard {
fn wait(&self) {
self.waiting.fetch_add(1, Ordering::Release);
self.sem.wait();
}
}
impl Drop for SemGuard {
fn drop(&mut self) {
for _ in 0..self.waiting.load(Ordering::Acquire) {
self.sem.signal();
}
}
}
pub struct Once {
sem: AtomicPtr<SemGuard>,
state: AtomicU8,
}
impl Once {
pub const fn new() -> Self {
Self {
sem: AtomicPtr::new(ptr::null_mut()),
state: AtomicU8::new(INCOMPLETE),
}
}
pub fn call_once<F: FnOnce()>(&self, cb: F) {
if self.is_completed() {
return;
}
let mut cb = Some(cb);
self.call_inner(move || match cb.take() {
Some(cb) => cb(),
None => unreach!()
});
}
#[inline]
pub fn is_completed(&self) -> bool {
self.state.load(Ordering::Acquire) == COMPLETE
}
#[cold]
fn call_inner_fail(&self) -> ! {
self.state.store(FAIL, Ordering::Acquire);
panic!(FAIL_MSG)
}
#[cold]
fn call_inner<F: FnMut()>(&self, mut cb: F) {
loop {
match self.state.load(Ordering::Acquire) {
COMPLETE => break,
FAIL => panic!(FAIL_MSG),
INCOMPLETE => {
if INCOMPLETE != self.state.compare_and_swap(INCOMPLETE, RUNNING, Ordering::Acquire) {
continue;
}
let sem = match Sem::new(0) {
Some(sem) => sem,
None => self.call_inner_fail(),
};
let mut sem_guard = SemGuard {
sem,
waiting: AtomicU32::new(0),
};
self.sem.store(&mut sem_guard as *mut _, Ordering::Release);
let _state_guard = StateGuard {
state: &self.state
};
cb();
},
state => {
debug_assert_eq!(state, RUNNING);
let mut sem = self.sem.load(Ordering::Acquire);
while sem.is_null() {
if self.state.load(Ordering::Acquire) == FAIL {
panic!(FAIL_MSG);
}
sem = self.sem.load(Ordering::Acquire);
core::sync::atomic::spin_loop_hint();
}
if self.state.load(Ordering::Acquire) != RUNNING {
unsafe {
(*sem).wait()
}
}
},
}
}
}
}