#![allow(unsafe_code)]
#[cfg(unix)]
pub(crate) use unix::generation;
#[cfg(not(unix))]
pub(crate) fn generation() -> Option<u64> {
None
}
#[cfg(unix)]
mod unix {
use core::sync::atomic::{AtomicU64, Ordering};
use std::sync::OnceLock;
static FORK_GENERATION: AtomicU64 = AtomicU64::new(0);
extern "C" fn bump_generation_in_child() {
FORK_GENERATION.fetch_add(1, Ordering::SeqCst);
}
pub(crate) fn generation() -> Option<u64> {
static INSTALLED: OnceLock<bool> = OnceLock::new();
let installed = *INSTALLED.get_or_init(|| {
unsafe { libc::pthread_atfork(None, None, Some(bump_generation_in_child)) == 0 }
});
installed.then(|| FORK_GENERATION.load(Ordering::Acquire))
}
}