Skip to content

Commit

Permalink
change owned_id to u64 back
Browse files Browse the repository at this point in the history
  • Loading branch information
wathenjiang committed Sep 28, 2023
1 parent df4ab61 commit 63a7679
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
8 changes: 4 additions & 4 deletions tokio/src/runtime/task/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::runtime::task::state::State;
use crate::runtime::task::{Id, Schedule};
use crate::util::linked_list;

use std::num::NonZeroU32;
use std::num::NonZeroU64;
use std::pin::Pin;
use std::ptr::NonNull;
use std::task::{Context, Poll, Waker};
Expand Down Expand Up @@ -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<Option<NonZeroU32>>,
pub(super) owner_id: UnsafeCell<Option<NonZeroU64>>,

/// The tracing ID for this instrumented task.
#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down Expand Up @@ -391,11 +391,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: NonZeroU32) {
pub(super) unsafe fn set_owner_id(&self, owner: NonZeroU64) {
self.owner_id.with_mut(|ptr| *ptr = Some(owner));
}

pub(super) fn get_owner_id(&self) -> Option<NonZeroU32> {
pub(super) fn get_owner_id(&self) -> Option<NonZeroU64> {
// 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) }
Expand Down
39 changes: 29 additions & 10 deletions tokio/src/runtime/task/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::util::linked_list::{Link, LinkedList};

use crate::loom::sync::atomic::{AtomicBool, Ordering};
use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::num::NonZeroU64;

use super::core::Header;

Expand All @@ -28,20 +28,39 @@ use super::core::Header;
// 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.

static NEXT_OWNED_TASKS_ID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(1);
cfg_has_atomic_u64! {
use std::sync::atomic::AtomicU64;

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;
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;

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;
}
}
}
}

pub(crate) struct OwnedTasks<S: 'static> {
lists: Box<[Mutex<ListSement<S>>]>,
pub(crate) id: NonZeroU32,
pub(crate) id: NonZeroU64,
closed: AtomicBool,
pub(crate) segment_size: u32,
segment_mask: u32,
Expand All @@ -52,7 +71,7 @@ type ListSement<S> = LinkedList<Task<S>, <Task<S> as Link>::Target>;

pub(crate) struct LocalOwnedTasks<S: 'static> {
inner: UnsafeCell<OwnedTasksInner<S>>,
pub(crate) id: NonZeroU32,
pub(crate) id: NonZeroU64,
_not_send_or_sync: PhantomData<*const ()>,
}
struct OwnedTasksInner<S: 'static> {
Expand Down Expand Up @@ -194,7 +213,7 @@ impl<S: 'static> OwnedTasks<S> {
#[inline]
unsafe fn remove_inner(&self, task: &Task<S>) -> Option<Task<S>> {
// Safety: it is safe, because every task has one task_id
let task_id = unsafe{Header::get_id(task.header_ptr())};
let task_id = unsafe { Header::get_id(task.header_ptr()) };
let mut lock = self.segment_inner(task_id.0 as usize);
let task = lock.remove(task.header_ptr())?;
drop(lock);
Expand Down

0 comments on commit 63a7679

Please sign in to comment.