diff --git a/tokio/src/runtime/id.rs b/tokio/src/runtime/id.rs index 58551d49989..f2d03300781 100644 --- a/tokio/src/runtime/id.rs +++ b/tokio/src/runtime/id.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::num::NonZeroU64; +use std::num::{NonZeroU64, NonZeroU32}; /// An opaque ID that uniquely identifies a runtime relative to all other currently /// running runtimes. @@ -39,6 +39,12 @@ impl From for Id { } } +impl From for Id { + fn from(value: NonZeroU32) -> Self { + Id(value.into()) + } +} + impl fmt::Display for Id { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) diff --git a/tokio/src/runtime/scheduler/current_thread/mod.rs b/tokio/src/runtime/scheduler/current_thread/mod.rs index 3e57aa8a374..c913e6e0b35 100644 --- a/tokio/src/runtime/scheduler/current_thread/mod.rs +++ b/tokio/src/runtime/scheduler/current_thread/mod.rs @@ -546,10 +546,10 @@ cfg_metrics! { } cfg_unstable! { - use std::num::NonZeroU64; + use std::num::NonZeroU32; impl Handle { - pub(crate) fn owned_id(&self) -> NonZeroU64 { + pub(crate) fn owned_id(&self) -> NonZeroU32 { self.shared.owned.id } } diff --git a/tokio/src/runtime/scheduler/multi_thread/handle.rs b/tokio/src/runtime/scheduler/multi_thread/handle.rs index 568eb80af8b..957673d749f 100644 --- a/tokio/src/runtime/scheduler/multi_thread/handle.rs +++ b/tokio/src/runtime/scheduler/multi_thread/handle.rs @@ -60,10 +60,10 @@ impl Handle { } cfg_unstable! { - use std::num::NonZeroU64; + use std::num::NonZeroU32; impl Handle { - pub(crate) fn owned_id(&self) -> NonZeroU64 { + pub(crate) fn owned_id(&self) -> NonZeroU32 { self.shared.owned.id } } diff --git a/tokio/src/runtime/scheduler/multi_thread_alt/handle.rs b/tokio/src/runtime/scheduler/multi_thread_alt/handle.rs index d746bca1a18..b7288fe2302 100644 --- a/tokio/src/runtime/scheduler/multi_thread_alt/handle.rs +++ b/tokio/src/runtime/scheduler/multi_thread_alt/handle.rs @@ -59,10 +59,10 @@ impl Handle { } cfg_unstable! { - use std::num::NonZeroU64; + use std::num::NonZeroU32; impl Handle { - pub(crate) fn owned_id(&self) -> NonZeroU64 { + pub(crate) fn owned_id(&self) -> NonZeroU32 { self.shared.owned.id } } diff --git a/tokio/src/runtime/task/core.rs b/tokio/src/runtime/task/core.rs index 4c274e3a0e3..c198badf3d4 100644 --- a/tokio/src/runtime/task/core.rs +++ b/tokio/src/runtime/task/core.rs @@ -17,7 +17,7 @@ use crate::runtime::task::state::State; use crate::runtime::task::{Id, Schedule}; use crate::util::linked_list; -use std::num::NonZeroU64; +use std::num::NonZeroU32; use std::pin::Pin; use std::ptr::NonNull; use std::task::{Context, Poll, Waker}; @@ -168,7 +168,7 @@ pub(crate) struct Header { /// The id is not unset when removed from a list because we want to be able /// to read the id without synchronization, even if it is concurrently being /// removed from the list. - pub(super) owner_id: UnsafeCell>, + pub(super) owner_id: UnsafeCell>, /// The tracing ID for this instrumented task. #[cfg(all(tokio_unstable, feature = "tracing"))] @@ -397,11 +397,11 @@ impl Header { // safety: The caller must guarantee exclusive access to this field, and // must ensure that the id is either `None` or the id of the OwnedTasks // containing this task. - pub(super) unsafe fn set_owner_id(&self, owner: NonZeroU64) { + pub(super) unsafe fn set_owner_id(&self, owner: NonZeroU32) { self.owner_id.with_mut(|ptr| *ptr = Some(owner)); } - pub(super) fn get_owner_id(&self) -> Option { + pub(super) fn get_owner_id(&self) -> Option { // safety: If there are concurrent writes, then that write has violated // the safety requirements on `set_owner_id`. unsafe { self.owner_id.with(|ptr| *ptr) } diff --git a/tokio/src/runtime/task/list.rs b/tokio/src/runtime/task/list.rs index d8ca1d5c023..e1abcc1533a 100644 --- a/tokio/src/runtime/task/list.rs +++ b/tokio/src/runtime/task/list.rs @@ -14,8 +14,9 @@ use crate::runtime::task::{JoinHandle, LocalNotified, Notified, Schedule, Task}; use crate::util::linked_list::{Link, LinkedList}; use std::marker::PhantomData; -use std::num::NonZeroU64; +use std::num::NonZeroU32; use std::sync::atomic::AtomicBool; +use std::sync::atomic::{AtomicU32, Ordering}; // The id from the module below is used to verify whether a given task is stored // in this OwnedTasks, or some other task. The counter starts at one so we can @@ -26,39 +27,20 @@ use std::sync::atomic::AtomicBool; // bug in Tokio, so we accept that certain bugs would not be caught if the two // mixed up runtimes happen to have the same id. -cfg_has_atomic_u64! { - use std::sync::atomic::{AtomicU64, Ordering}; +static NEXT_OWNED_TASKS_ID: AtomicU32 = AtomicU32::new(1); - static NEXT_OWNED_TASKS_ID: AtomicU64 = AtomicU64::new(1); - - fn get_next_id() -> NonZeroU64 { - loop { - let id = NEXT_OWNED_TASKS_ID.fetch_add(1, Ordering::Relaxed); - if let Some(id) = NonZeroU64::new(id) { - return id; - } - } - } -} - -cfg_not_has_atomic_u64! { - use std::sync::atomic::{AtomicU32, Ordering}; - - static NEXT_OWNED_TASKS_ID: AtomicU32 = AtomicU32::new(1); - - fn get_next_id() -> NonZeroU64 { - loop { - let id = NEXT_OWNED_TASKS_ID.fetch_add(1, Ordering::Relaxed); - if let Some(id) = NonZeroU64::new(u64::from(id)) { - return id; - } +fn get_next_id() -> NonZeroU32 { + loop { + let id = NEXT_OWNED_TASKS_ID.fetch_add(1, Ordering::Relaxed); + if let Some(id) = NonZeroU32::new(id) { + return id; } } } pub(crate) struct OwnedTasks { lists: Vec>>, - pub(crate) id: NonZeroU64, + pub(crate) id: NonZeroU32, closed: AtomicBool, grain: usize, count: AtomicUsize, @@ -68,7 +50,7 @@ struct CountedOwnedTasksInner { } pub(crate) struct LocalOwnedTasks { inner: UnsafeCell>, - pub(crate) id: NonZeroU64, + pub(crate) id: NonZeroU32, _not_send_or_sync: PhantomData<*const ()>, } struct OwnedTasksInner { @@ -233,8 +215,8 @@ cfg_taskdump! { F: FnMut(&Task) { let mut f = f; - for list in &self.lists{ - f = list.lock().list.for_each(f); + for inner in &self.lists{ + f = inner.lock().list.for_each(f); } } }