Skip to content

Commit

Permalink
use integer ID for collector uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Jul 1, 2024
1 parent 90b9e7b commit 9f80055
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
20 changes: 8 additions & 12 deletions src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::tls::Thread;
use crate::{raw, LocalGuard, OwnedGuard};

use std::cell::UnsafeCell;
use std::fmt;
use std::num::NonZeroU64;
use std::sync::atomic::Ordering;
use std::{fmt, ptr};
use std::sync::atomic::{AtomicUsize, Ordering};

/// Fast, efficient, and robust memory reclamation.
///
Expand All @@ -13,8 +13,8 @@ use std::{fmt, ptr};
/// the [`enter`](Collector::enter) or [`enter_owned`](Collector::enter_owned)
/// methods.
pub struct Collector {
id: usize,
pub(crate) raw: raw::Collector,
unique: *mut u8,
}

unsafe impl Send for Collector {}
Expand All @@ -26,13 +26,15 @@ impl Collector {

/// Creates a new collector.
pub fn new() -> Self {
static ID: AtomicUsize = AtomicUsize::new(0);

let cpus = std::thread::available_parallelism()
.map(Into::into)
.unwrap_or(1);

Self {
raw: raw::Collector::new(cpus, Self::DEFAULT_EPOCH_TICK, Self::DEFAULT_RETIRE_TICK),
unique: Box::into_raw(Box::new(0)),
id: ID.fetch_add(1, Ordering::Relaxed),
}
}

Expand Down Expand Up @@ -284,14 +286,8 @@ impl Collector {
unsafe { self.raw.reclaim_all() };
}

pub(crate) fn ptr_eq(this: &Collector, other: &Collector) -> bool {
ptr::eq(this.unique, other.unique)
}
}

impl Drop for Collector {
fn drop(&mut self) {
let _ = unsafe { Box::from_raw(self.unique) };
pub(crate) fn id_eq(this: &Collector, other: &Collector) -> bool {
this.id == other.id
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl Guard for LocalGuard<'_> {

/// Returns `true` if this guard belongs to the given collector.
fn belongs_to(&self, collector: &Collector) -> bool {
Collector::ptr_eq(self.collector, collector)
Collector::id_eq(self.collector, collector)
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ impl Guard for OwnedGuard<'_> {

/// Returns `true` if this guard belongs to the given collector.
fn belongs_to(&self, collector: &Collector) -> bool {
Collector::ptr_eq(self.collector, collector)
Collector::id_eq(self.collector, collector)
}
}

Expand Down

0 comments on commit 9f80055

Please sign in to comment.