Skip to content

Commit

Permalink
reduce the size of header
Browse files Browse the repository at this point in the history
  • Loading branch information
wathenjiang committed Sep 12, 2023
1 parent ab2452c commit 445d79f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 41 deletions.
8 changes: 7 additions & 1 deletion 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.
Expand Down Expand Up @@ -39,6 +39,12 @@ impl From<NonZeroU64> for Id {
}
}

impl From<NonZeroU32> 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)
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/current_thread/mod.rs
Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/multi_thread/handle.rs
Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/runtime/scheduler/multi_thread_alt/handle.rs
Expand Up @@ -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
}
}
Expand Down
8 changes: 4 additions & 4 deletions tokio/src/runtime/task/core.rs
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::NonZeroU64;
use std::num::NonZeroU32;
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<NonZeroU64>>,
pub(super) owner_id: UnsafeCell<Option<NonZeroU32>>,

/// The tracing ID for this instrumented task.
#[cfg(all(tokio_unstable, feature = "tracing"))]
Expand Down Expand Up @@ -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<NonZeroU64> {
pub(super) fn get_owner_id(&self) -> Option<NonZeroU32> {
// 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
42 changes: 12 additions & 30 deletions tokio/src/runtime/task/list.rs
Expand Up @@ -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
Expand All @@ -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<S: 'static> {
lists: Vec<Mutex<CountedOwnedTasksInner<S>>>,
pub(crate) id: NonZeroU64,
pub(crate) id: NonZeroU32,
closed: AtomicBool,
grain: usize,
count: AtomicUsize,
Expand All @@ -68,7 +50,7 @@ struct CountedOwnedTasksInner<S: 'static> {
}
pub(crate) struct LocalOwnedTasks<S: 'static> {
inner: UnsafeCell<OwnedTasksInner<S>>,
pub(crate) id: NonZeroU64,
pub(crate) id: NonZeroU32,
_not_send_or_sync: PhantomData<*const ()>,
}
struct OwnedTasksInner<S: 'static> {
Expand Down Expand Up @@ -233,8 +215,8 @@ cfg_taskdump! {
F: FnMut(&Task<S>)
{
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);
}
}
}
Expand Down

0 comments on commit 445d79f

Please sign in to comment.