From 0e9b79042c1056a583af7fd3e499987b8a8d171b Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Mon, 2 Dec 2024 11:05:43 +0330 Subject: [PATCH] fix: switch to OnceLock Signed-off-by: Amirhossein Akhlaghpour --- Cargo.toml | 2 +- src/tid.rs | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a43d3b9..a609942 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ name = "bench" harness = false [dependencies] -lazy_static = "1" +lazy_static = "1.5.0" [dev-dependencies] proptest = "1" diff --git a/src/tid.rs b/src/tid.rs index f2cb7e0..9d7731e 100644 --- a/src/tid.rs +++ b/src/tid.rs @@ -3,7 +3,7 @@ use crate::{ page, sync::{ atomic::{AtomicUsize, Ordering}, - lazy_static, thread_local, Mutex, + thread_local, Mutex, }, Pack, }; @@ -12,6 +12,7 @@ use std::{ collections::VecDeque, fmt, marker::PhantomData, + sync::OnceLock, }; /// Uniquely identifies a thread. @@ -29,12 +30,7 @@ struct Registry { free: Mutex>, } -lazy_static! { - static ref REGISTRY: Registry = Registry { - next: AtomicUsize::new(0), - free: Mutex::new(VecDeque::new()), - }; -} +static REGISTRY: OnceLock = OnceLock::new(); thread_local! { static REGISTRATION: Registration = Registration::new(); @@ -147,6 +143,8 @@ impl Registration { #[cold] fn register(&self) -> Tid { let id = REGISTRY + .get() + .expect("msg") .free .lock() .ok() @@ -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::::BITS { panic_in_drop!( "creating a new thread ID ({}) would exceed the \ @@ -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(tid: usize, f: impl FnOnce() -> R) -> R { struct Guard(Option);