From 8507e28f89916662d2f61af823993483169d912c Mon Sep 17 00:00:00 2001 From: Alphyr <47725341+a1phyr@users.noreply.github.com> Date: Tue, 11 Mar 2025 08:15:26 +0100 Subject: [PATCH] Remove an old custom `OnceCell` implementation in favor of `std` (#7208) --- .../loom/std/atomic_u64_static_once_cell.rs | 8 +-- tokio/src/process/unix/mod.rs | 6 +- tokio/src/signal/registry.rs | 6 +- tokio/src/util/mod.rs | 3 - tokio/src/util/once_cell.rs | 70 ------------------- 5 files changed, 10 insertions(+), 83 deletions(-) delete mode 100644 tokio/src/util/once_cell.rs diff --git a/tokio/src/loom/std/atomic_u64_static_once_cell.rs b/tokio/src/loom/std/atomic_u64_static_once_cell.rs index 40c6172a5..e1fceb872 100644 --- a/tokio/src/loom/std/atomic_u64_static_once_cell.rs +++ b/tokio/src/loom/std/atomic_u64_static_once_cell.rs @@ -1,10 +1,10 @@ use super::AtomicU64; use crate::loom::sync::{atomic::Ordering, Mutex}; -use crate::util::once_cell::OnceCell; +use std::sync::OnceLock; pub(crate) struct StaticAtomicU64 { init: u64, - cell: OnceCell>, + cell: OnceLock>, } impl AtomicU64 { @@ -19,7 +19,7 @@ impl StaticAtomicU64 { pub(crate) const fn new(val: u64) -> StaticAtomicU64 { StaticAtomicU64 { init: val, - cell: OnceCell::new(), + cell: OnceLock::new(), } } @@ -52,6 +52,6 @@ impl StaticAtomicU64 { } fn inner(&self) -> &Mutex { - self.cell.get(|| Mutex::new(self.init)) + self.cell.get_or_init(|| Mutex::new(self.init)) } } diff --git a/tokio/src/process/unix/mod.rs b/tokio/src/process/unix/mod.rs index c9d1035f5..75a6c23b2 100644 --- a/tokio/src/process/unix/mod.rs +++ b/tokio/src/process/unix/mod.rs @@ -66,11 +66,11 @@ impl Kill for StdChild { cfg_not_has_const_mutex_new! { fn get_orphan_queue() -> &'static OrphanQueueImpl { - use crate::util::once_cell::OnceCell; + use std::sync::OnceLock; - static ORPHAN_QUEUE: OnceCell> = OnceCell::new(); + static ORPHAN_QUEUE: OnceLock> = OnceLock::new(); - ORPHAN_QUEUE.get(OrphanQueueImpl::new) + ORPHAN_QUEUE.get_or_init(OrphanQueueImpl::new) } } diff --git a/tokio/src/signal/registry.rs b/tokio/src/signal/registry.rs index e5358cae3..7ebdc028d 100644 --- a/tokio/src/signal/registry.rs +++ b/tokio/src/signal/registry.rs @@ -1,9 +1,9 @@ use crate::signal::os::{OsExtraData, OsStorage}; use crate::sync::watch; -use crate::util::once_cell::OnceCell; use std::ops; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::OnceLock; pub(crate) type EventId = usize; @@ -164,9 +164,9 @@ where OsExtraData: 'static + Send + Sync + Init, OsStorage: 'static + Send + Sync + Init, { - static GLOBALS: OnceCell = OnceCell::new(); + static GLOBALS: OnceLock = OnceLock::new(); - GLOBALS.get(globals_init) + GLOBALS.get_or_init(globals_init) } #[cfg(all(test, not(loom)))] diff --git a/tokio/src/util/mod.rs b/tokio/src/util/mod.rs index f41062440..6dce66f25 100644 --- a/tokio/src/util/mod.rs +++ b/tokio/src/util/mod.rs @@ -13,9 +13,6 @@ pub(crate) use blocking_check::check_socket_for_blocking; pub(crate) mod metric_atomics; -#[cfg(any(feature = "rt", feature = "signal", feature = "process"))] -pub(crate) mod once_cell; - #[cfg(any( // io driver uses `WakeList` directly feature = "net", diff --git a/tokio/src/util/once_cell.rs b/tokio/src/util/once_cell.rs deleted file mode 100644 index 71fc00758..000000000 --- a/tokio/src/util/once_cell.rs +++ /dev/null @@ -1,70 +0,0 @@ -#![allow(dead_code)] -use std::cell::UnsafeCell; -use std::mem::MaybeUninit; -use std::sync::Once; - -pub(crate) struct OnceCell { - once: Once, - value: UnsafeCell>, -} - -unsafe impl Send for OnceCell {} -unsafe impl Sync for OnceCell {} - -impl OnceCell { - pub(crate) const fn new() -> Self { - Self { - once: Once::new(), - value: UnsafeCell::new(MaybeUninit::uninit()), - } - } - - /// Get the value inside this cell, initializing it using the provided - /// function if necessary. - /// - /// If the `init` closure panics, then the `OnceCell` is poisoned and all - /// future calls to `get` will panic. - #[inline] - pub(crate) fn get(&self, init: impl FnOnce() -> T) -> &T { - if !self.once.is_completed() { - self.do_init(init); - } - - // Safety: The `std::sync::Once` guarantees that we can only reach this - // line if a `call_once` closure has been run exactly once and without - // panicking. Thus, the value is not uninitialized. - // - // There is also no race because the only `&self` method that modifies - // `value` is `do_init`, but if the `call_once` closure is still - // running, then no thread has gotten past the `call_once`. - unsafe { &*(self.value.get() as *const T) } - } - - #[cold] - fn do_init(&self, init: impl FnOnce() -> T) { - let value_ptr = self.value.get() as *mut T; - - self.once.call_once(|| { - let set_to = init(); - - // Safety: The `std::sync::Once` guarantees that this initialization - // will run at most once, and that no thread can get past the - // `call_once` until it has run exactly once. Thus, we have - // exclusive access to `value`. - unsafe { - std::ptr::write(value_ptr, set_to); - } - }); - } -} - -impl Drop for OnceCell { - fn drop(&mut self) { - if self.once.is_completed() { - let value_ptr = self.value.get() as *mut T; - unsafe { - std::ptr::drop_in_place(value_ptr); - } - } - } -}