From 74d3643991e6b2fc5ee8adc8b4b524597fe5f29e Mon Sep 17 00:00:00 2001 From: mox692 Date: Fri, 26 Jul 2024 21:10:01 +0900 Subject: [PATCH 1/5] task: use `NonZeroU64` for `task::Id` --- tokio/src/runtime/task/id.rs | 16 +++++++++++----- tokio/src/runtime/task/mod.rs | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tokio/src/runtime/task/id.rs b/tokio/src/runtime/task/id.rs index 82c8a7e7e90..04e7dcbb5fa 100644 --- a/tokio/src/runtime/task/id.rs +++ b/tokio/src/runtime/task/id.rs @@ -1,6 +1,6 @@ use crate::runtime::context; -use std::fmt; +use std::{fmt, num::NonZeroU64}; /// An opaque ID that uniquely identifies a task relative to all other currently /// running tasks. @@ -24,7 +24,7 @@ use std::fmt; #[cfg_attr(docsrs, doc(cfg(all(feature = "rt", tokio_unstable))))] #[cfg_attr(not(tokio_unstable), allow(unreachable_pub))] #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)] -pub struct Id(pub(crate) u64); +pub struct Id(pub(crate) NonZeroU64); /// Returns the [`Id`] of the currently running task. /// @@ -82,17 +82,23 @@ impl Id { crate::loom::lazy_static! { static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); } - Self(NEXT_ID.fetch_add(1, Relaxed)) + let id = NonZeroU64::new(NEXT_ID.fetch_add(1, Relaxed)) + .unwrap_or_else(|| NonZeroU64::new(1).unwrap()); + + Self(id) } #[cfg(not(all(test, loom)))] { static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); - Self(NEXT_ID.fetch_add(1, Relaxed)) + let id = NonZeroU64::new(NEXT_ID.fetch_add(1, Relaxed)) + .unwrap_or_else(|| NonZeroU64::new(1).unwrap()); + + Self(id) } } pub(crate) fn as_u64(&self) -> u64 { - self.0 + self.0.get() } } diff --git a/tokio/src/runtime/task/mod.rs b/tokio/src/runtime/task/mod.rs index aa799bf2be1..8248fc0b591 100644 --- a/tokio/src/runtime/task/mod.rs +++ b/tokio/src/runtime/task/mod.rs @@ -532,6 +532,6 @@ unsafe impl sharded_list::ShardedListItem for Task { unsafe fn get_shard_id(target: NonNull) -> usize { // SAFETY: The caller guarantees that `target` points at a valid task. let task_id = unsafe { Header::get_id(target) }; - task_id.0 as usize + task_id.0.get() as usize } } From 1f560961e26e53f230162c9efd87501b2e938dd3 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 1 Aug 2024 19:50:26 +0900 Subject: [PATCH 2/5] apply review: use loop --- tokio/src/runtime/task/id.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tokio/src/runtime/task/id.rs b/tokio/src/runtime/task/id.rs index 04e7dcbb5fa..7a8c32f56b2 100644 --- a/tokio/src/runtime/task/id.rs +++ b/tokio/src/runtime/task/id.rs @@ -82,19 +82,23 @@ impl Id { crate::loom::lazy_static! { static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); } - let id = NonZeroU64::new(NEXT_ID.fetch_add(1, Relaxed)) - .unwrap_or_else(|| NonZeroU64::new(1).unwrap()); - - Self(id) + loop { + let id = NEXT_ID.fetch_add(1, Relaxed); + if let Some(id) = NonZeroU64::new(u64::from(id)) { + return Self(id); + } + } } #[cfg(not(all(test, loom)))] { static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); - let id = NonZeroU64::new(NEXT_ID.fetch_add(1, Relaxed)) - .unwrap_or_else(|| NonZeroU64::new(1).unwrap()); - - Self(id) + loop { + let id = NEXT_ID.fetch_add(1, Relaxed); + if let Some(id) = NonZeroU64::new(u64::from(id)) { + return Self(id); + } + } } } From 5cf547c0a48c22e7278b45b2baeb00a487ac3e88 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 1 Aug 2024 21:38:40 +0900 Subject: [PATCH 3/5] small cleanup --- tokio/src/runtime/task/id.rs | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/tokio/src/runtime/task/id.rs b/tokio/src/runtime/task/id.rs index 7a8c32f56b2..fcd58031907 100644 --- a/tokio/src/runtime/task/id.rs +++ b/tokio/src/runtime/task/id.rs @@ -78,26 +78,16 @@ impl Id { use crate::loom::sync::atomic::StaticAtomicU64; #[cfg(all(test, loom))] - { - crate::loom::lazy_static! { - static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); - } - loop { - let id = NEXT_ID.fetch_add(1, Relaxed); - if let Some(id) = NonZeroU64::new(u64::from(id)) { - return Self(id); - } - } + crate::loom::lazy_static! { + static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); } - #[cfg(not(all(test, loom)))] - { - static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); - loop { - let id = NEXT_ID.fetch_add(1, Relaxed); - if let Some(id) = NonZeroU64::new(u64::from(id)) { - return Self(id); - } + static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); + + loop { + let id = NEXT_ID.fetch_add(1, Relaxed); + if let Some(id) = NonZeroU64::new(u64::from(id)) { + return Self(id); } } } From d91a5b2f766bbb335794bf2c3ebbfe82376964c6 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 1 Aug 2024 23:06:55 +0900 Subject: [PATCH 4/5] remove unrelated diff --- tokio/src/runtime/task/id.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tokio/src/runtime/task/id.rs b/tokio/src/runtime/task/id.rs index fcd58031907..8eece673ef4 100644 --- a/tokio/src/runtime/task/id.rs +++ b/tokio/src/runtime/task/id.rs @@ -81,6 +81,7 @@ impl Id { crate::loom::lazy_static! { static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); } + #[cfg(not(all(test, loom)))] static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1); From 0844ced9c76cfcf3b098f858003b5296d38ef549 Mon Sep 17 00:00:00 2001 From: mox692 Date: Thu, 1 Aug 2024 23:32:50 +0900 Subject: [PATCH 5/5] fix useless conversion --- tokio/src/runtime/task/id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/src/runtime/task/id.rs b/tokio/src/runtime/task/id.rs index 8eece673ef4..9c3b1403ec2 100644 --- a/tokio/src/runtime/task/id.rs +++ b/tokio/src/runtime/task/id.rs @@ -87,7 +87,7 @@ impl Id { loop { let id = NEXT_ID.fetch_add(1, Relaxed); - if let Some(id) = NonZeroU64::new(u64::from(id)) { + if let Some(id) = NonZeroU64::new(id) { return Self(id); } }