Skip to main content

chaud_hot/
cycle.rs

1use core::sync::atomic::{AtomicU32, Ordering::Relaxed};
2use parking_lot::{Condvar, Mutex};
3
4static EPOCH: AtomicU32 = AtomicU32::new(0);
5
6static WAIT: (Mutex<()>, Condvar) = (Mutex::new(()), Condvar::new());
7
8#[inline]
9pub fn current() -> u32 {
10    EPOCH.load(Relaxed)
11}
12
13#[inline]
14pub fn check(epoch: &mut u32) -> bool {
15    let prev = *epoch;
16    *epoch = EPOCH.load(Relaxed);
17    prev != *epoch
18}
19
20#[inline]
21pub fn wait(epoch: &mut u32) {
22    if check(epoch) {
23        return;
24    }
25
26    WAIT.1.wait_while(&mut WAIT.0.lock(), |_| !check(epoch));
27}
28
29/// Utility to track "init done" in Chaud integration tests.
30pub fn track_init() {
31    EPOCH.store(u32::MAX, Relaxed);
32}
33
34/// Utility to track "init done" in Chaud integration tests.
35pub(crate) fn init_done() {
36    EPOCH.store(0, Relaxed);
37    WAIT.1.notify_all();
38}
39
40pub(crate) fn did_reload() {
41    EPOCH.fetch_add(1, Relaxed);
42    WAIT.1.notify_all();
43}