#![forbid(unsafe_code)]
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
static PAUSES: AtomicUsize = AtomicUsize::new(0);
#[must_use = "garbage collection resumes as soon as the pause is dropped"]
pub struct GarbageCollectionPause {
_private: (),
}
impl GarbageCollectionPause {
pub fn new() -> Self {
PAUSES.fetch_add(1, Ordering::Relaxed);
Self { _private: () }
}
}
impl Default for GarbageCollectionPause {
fn default() -> Self {
Self::new()
}
}
impl Drop for GarbageCollectionPause {
fn drop(&mut self) {
PAUSES.fetch_sub(1, Ordering::Relaxed);
}
}
pub(crate) fn is_garbage_collection_paused() -> bool {
PAUSES.load(Ordering::Relaxed) != 0
}
#[cfg(test)]
mod tests {
use super::GarbageCollectionPause;
use super::is_garbage_collection_paused;
#[test]
fn test_pauses_nest() {
let outer = GarbageCollectionPause::new();
let inner = GarbageCollectionPause::new();
drop(inner);
assert!(is_garbage_collection_paused());
drop(outer);
}
}