#![deny(unsafe_op_in_unsafe_fn, clippy::undocumented_unsafe_blocks)]
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::rc::Rc;
use std::sync::atomic::{AtomicU64, Ordering};
pub struct Guard<'m> {
pub(crate) inner: crossbeam_epoch::Guard,
_map: PhantomData<&'m ()>,
}
impl Guard<'_> {
pub(crate) fn new() -> Self {
Self {
inner: pin(),
_map: PhantomData,
}
}
#[inline]
pub fn repin(&mut self) {
self.inner.repin();
}
}
impl std::fmt::Debug for Guard<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Guard").finish_non_exhaustive()
}
}
struct Participant {
handle: crossbeam_epoch::LocalHandle,
id: u64,
}
fn next_participant_id() -> u64 {
static NEXT: AtomicU64 = AtomicU64::new(1);
NEXT.fetch_add(1, Ordering::Relaxed)
}
thread_local! {
static PARTICIPANT: Participant = Participant {
handle: crossbeam_epoch::default_collector().register(),
id: next_participant_id(),
};
}
#[inline]
pub(crate) fn pin_tagged() -> (crossbeam_epoch::Guard, u64) {
PARTICIPANT
.try_with(|p| (p.handle.pin(), p.id))
.unwrap_or_else(|_| (crossbeam_epoch::default_collector().register().pin(), 0))
}
#[inline]
pub(crate) fn pin() -> crossbeam_epoch::Guard {
pin_tagged().0
}
#[inline]
pub(crate) fn nested_pin(id: u64) -> crossbeam_epoch::Guard {
PARTICIPANT
.try_with(|p| (id != 0 && p.id == id).then(|| p.handle.pin()))
.ok()
.flatten()
.expect("artmap: cannot extend an epoch guard during thread-local destruction")
}
pub(crate) enum GuardHandle<'a> {
Owned(crossbeam_epoch::Guard, u64),
Shared(Rc<crossbeam_epoch::Guard>, u64),
Borrowed(&'a crossbeam_epoch::Guard),
}
impl GuardHandle<'_> {
#[inline]
pub(crate) fn owned() -> Self {
let (g, id) = pin_tagged();
GuardHandle::Owned(g, id)
}
#[inline]
pub(crate) fn guard(&self) -> &crossbeam_epoch::Guard {
match self {
GuardHandle::Owned(g, _) => g,
GuardHandle::Shared(g, _) => g,
GuardHandle::Borrowed(g) => g,
}
}
#[inline(always)]
pub(crate) fn duplicate(&self) -> Self {
match self {
GuardHandle::Owned(_, id) => GuardHandle::Owned(nested_pin(*id), *id),
GuardHandle::Shared(rc, id) => GuardHandle::Shared(Rc::clone(rc), *id),
GuardHandle::Borrowed(g) => GuardHandle::Borrowed(g),
}
}
}
pub(crate) struct Retired<T>(NonNull<T>);
unsafe impl<T: Send> Send for Retired<T> {}
impl<T> Retired<T> {
#[inline]
pub(crate) unsafe fn from_non_null(ptr: NonNull<T>) -> Self {
Self(ptr)
}
#[inline]
unsafe fn into_box(self) -> Box<T> {
unsafe { Box::from_raw(self.0.as_ptr()) }
}
}
#[inline]
pub(crate) fn retire<T: Send + 'static>(guard: &crossbeam_epoch::Guard, r: Retired<T>) {
#[cfg(loom)]
{
let _ = guard;
std::mem::forget(r);
}
#[cfg(not(loom))]
guard.defer(move || unsafe { drop(r.into_box()) });
}
#[cfg(all(test, not(loom)))]
mod tests {
use super::*;
#[test]
fn nested_pin_matches_participant() {
let (g, id) = pin_tagged();
assert_ne!(id, 0);
let n = nested_pin(id);
drop(g);
drop(n);
}
#[test]
#[should_panic(expected = "thread-local destruction")]
fn nested_pin_rejects_unknown_participant() {
drop(nested_pin(0));
}
#[test]
fn nested_pin_rejects_other_thread_participant() {
let (_g, id) = pin_tagged();
let other =
std::thread::spawn(move || std::panic::catch_unwind(|| nested_pin(id)).is_err())
.join()
.unwrap();
assert!(other, "a participant id from another thread is rejected");
}
#[test]
fn retire_runs_destructor_once() {
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
struct D(Arc<AtomicUsize>);
impl Drop for D {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::Relaxed);
}
}
let n = Arc::new(AtomicUsize::new(0));
let p = crate::raw::boxed(D(Arc::clone(&n)));
{
let g = pin();
retire(&g, unsafe { Retired::from_non_null(p) });
}
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
while n.load(Ordering::Relaxed) == 0 && std::time::Instant::now() < deadline {
pin().flush();
}
assert_eq!(n.load(Ordering::Relaxed), 1);
}
}