-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathatomic.rs
More file actions
64 lines (58 loc) · 2.43 KB
/
Copy pathatomic.rs
File metadata and controls
64 lines (58 loc) · 2.43 KB
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
use std::sync::atomic::{AtomicBool, Ordering};
/// RAII guard that clears an [`AtomicBool`] flag to `false` on drop.
///
/// Callers set the flag to `true` before constructing the guard (typically
/// via a `compare_exchange`); the guard resets it on every exit path,
/// including panics, so a panicked holder can never leave the flag wedged.
///
/// **Panic-strategy caveat:** the clear-on-panic guarantee relies on
/// destructors running while the stack unwinds, so it holds under
/// `panic = "unwind"` (the default). Under `panic = "abort"` — e.g. the
/// iOS release profiles — a panic aborts the process immediately and no
/// `Drop` runs; there is simply no "after" left for the flag to gate.
#[must_use = "AtomicFlagGuard clears the flag on drop; binding to `_` or using as a statement drops it immediately"]
pub struct AtomicFlagGuard<'a>(&'a AtomicBool);
impl<'a> AtomicFlagGuard<'a> {
/// Wrap `flag`. Does **not** set it to `true` — the caller is
/// responsible for doing that before constructing the guard.
pub fn new(flag: &'a AtomicBool) -> Self {
Self(flag)
}
}
impl Drop for AtomicFlagGuard<'_> {
fn drop(&mut self) {
self.0.store(false, Ordering::Release);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::panic::{catch_unwind, AssertUnwindSafe};
/// A guard constructed over a `true` flag holds it while in scope and
/// clears it to `false` on a normal scope exit.
#[test]
fn clears_flag_on_normal_drop() {
let flag = AtomicBool::new(true);
{
let _guard = AtomicFlagGuard::new(&flag);
assert!(flag.load(Ordering::Acquire), "flag stays set while held");
}
assert!(!flag.load(Ordering::Acquire), "flag cleared on drop");
}
/// The clear also runs while unwinding a panic — the load-bearing
/// property the sync coordinators lean on so a panicked pass can't
/// leave `is_syncing` latched and wedge `quiesce()`'s drain.
#[test]
fn clears_flag_while_unwinding_panic() {
let flag = AtomicBool::new(true);
let result = catch_unwind(AssertUnwindSafe(|| {
let _guard = AtomicFlagGuard::new(&flag);
panic!("boom while holding the guard");
}));
assert!(result.is_err(), "the panic propagated out of catch_unwind");
assert!(
!flag.load(Ordering::Acquire),
"Drop ran during unwinding and cleared the flag"
);
}
}