Skip to content

Commit

Permalink
fix: switch to OnceLock
Browse files Browse the repository at this point in the history
Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
  • Loading branch information
Mehrbod2002 committed Dec 2, 2024
1 parent ee45b4c commit 0e9b790
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ name = "bench"
harness = false

[dependencies]
lazy_static = "1"
lazy_static = "1.5.0"

[dev-dependencies]
proptest = "1"
Expand Down
29 changes: 18 additions & 11 deletions src/tid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
page,
sync::{
atomic::{AtomicUsize, Ordering},
lazy_static, thread_local, Mutex,
thread_local, Mutex,
},
Pack,
};
Expand All @@ -12,6 +12,7 @@ use std::{
collections::VecDeque,
fmt,
marker::PhantomData,
sync::OnceLock,

Check failure on line 15 in src/tid.rs

View workflow job for this annotation

GitHub Actions / Build (MSRV)

unresolved import `std::sync::OnceLock`
};

/// Uniquely identifies a thread.
Expand All @@ -29,12 +30,7 @@ struct Registry {
free: Mutex<VecDeque<usize>>,
}

lazy_static! {
static ref REGISTRY: Registry = Registry {
next: AtomicUsize::new(0),
free: Mutex::new(VecDeque::new()),
};
}
static REGISTRY: OnceLock<Registry> = OnceLock::new();

thread_local! {
static REGISTRATION: Registration = Registration::new();
Expand Down Expand Up @@ -147,6 +143,8 @@ impl Registration {
#[cold]
fn register<C: cfg::Config>(&self) -> Tid<C> {
let id = REGISTRY
.get()
.expect("msg")
.free
.lock()
.ok()
Expand All @@ -158,7 +156,11 @@ impl Registration {
}
})
.unwrap_or_else(|| {
let id = REGISTRY.next.fetch_add(1, Ordering::AcqRel);
let id = REGISTRY
.get()
.expect("")
.next
.fetch_add(1, Ordering::AcqRel);
if id > Tid::<C>::BITS {
panic_in_drop!(
"creating a new thread ID ({}) would exceed the \
Expand All @@ -182,19 +184,24 @@ impl Registration {
// panics. T_T
// Just skip TID reuse and use loom's lazy_static macro to ensure we have a
// clean initial TID on every iteration, instead.
#[cfg(not(all(loom, any(feature = "loom", test))))]
#[cfg(not(all(feature = "loom", any(feature = "loom", test))))]
impl Drop for Registration {
fn drop(&mut self) {
use std::sync::PoisonError;

if let Some(id) = self.0.get() {
let mut free_list = REGISTRY.free.lock().unwrap_or_else(PoisonError::into_inner);
let mut free_list = REGISTRY
.get()
.expect("msg")
.free
.lock()
.unwrap_or_else(PoisonError::into_inner);
free_list.push_back(id);
}
}
}

#[cfg(all(test, not(loom)))]
#[cfg(all(test, not(feature = "loom")))]
pub(crate) fn with<R>(tid: usize, f: impl FnOnce() -> R) -> R {
struct Guard(Option<usize>);

Expand Down

0 comments on commit 0e9b790

Please sign in to comment.