use super::collectible::DeferredClosure;
use super::collector::Collector;
use super::Epoch;
use std::panic::UnwindSafe;
pub struct Guard {
collector_ptr: *mut Collector,
}
impl Guard {
#[inline]
#[must_use]
pub fn new() -> Self {
let collector_ptr = Collector::current();
unsafe {
Collector::new_guard(collector_ptr, true);
}
Self { collector_ptr }
}
#[inline]
#[must_use]
pub fn epoch(&self) -> Epoch {
Collector::current_epoch()
}
#[inline]
pub fn accelerate(&self) {
unsafe {
(*self.collector_ptr).accelerate();
}
}
#[inline]
pub fn defer_execute<F: 'static + FnOnce()>(&self, f: F) {
unsafe {
Collector::collect(
self.collector_ptr,
Box::into_raw(Box::new(DeferredClosure::new(f))),
);
}
}
}
impl Default for Guard {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl Drop for Guard {
#[inline]
fn drop(&mut self) {
unsafe {
Collector::end_guard(self.collector_ptr);
}
}
}
impl UnwindSafe for Guard {}