Skip to content

Commit

Permalink
task: use NonZeroU64 for task::Id (#6733)
Browse files Browse the repository at this point in the history
  • Loading branch information
mox692 authored Aug 1, 2024
1 parent 1077b0b commit 338e13b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions tokio/src/runtime/task/id.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
///
Expand Down Expand Up @@ -78,21 +78,22 @@ impl Id {
use crate::loom::sync::atomic::StaticAtomicU64;

#[cfg(all(test, loom))]
{
crate::loom::lazy_static! {
static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1);
}
Self(NEXT_ID.fetch_add(1, Relaxed))
crate::loom::lazy_static! {
static ref NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1);
}

#[cfg(not(all(test, loom)))]
{
static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1);
Self(NEXT_ID.fetch_add(1, Relaxed))
static NEXT_ID: StaticAtomicU64 = StaticAtomicU64::new(1);

loop {
let id = NEXT_ID.fetch_add(1, Relaxed);
if let Some(id) = NonZeroU64::new(id) {
return Self(id);
}
}
}

pub(crate) fn as_u64(&self) -> u64 {
self.0
self.0.get()
}
}
2 changes: 1 addition & 1 deletion tokio/src/runtime/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,6 @@ unsafe impl<S> sharded_list::ShardedListItem for Task<S> {
unsafe fn get_shard_id(target: NonNull<Self::Target>) -> 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
}
}

0 comments on commit 338e13b

Please sign in to comment.