Skip to content

Commit

Permalink
Constify {Mutex, Notify, OnceCell, RwLock, Semaphore}::new
Browse files Browse the repository at this point in the history
By removing the tracing log in these functions.

Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed Jul 29, 2023
1 parent efe3ab6 commit 4fe5686
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 309 deletions.
4 changes: 2 additions & 2 deletions tokio/src/loom/std/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub(crate) struct Mutex<T: ?Sized>(sync::Mutex<T>);
#[allow(dead_code)]
impl<T> Mutex<T> {
#[inline]
pub(crate) fn new(t: T) -> Mutex<T> {
Mutex(sync::Mutex::new(t))
pub(crate) const fn new(t: T) -> Mutex<T> {
Self::const_new(t)
}

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/loom/std/parking_lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub(crate) struct RwLockWriteGuard<'a, T: ?Sized>(

impl<T> Mutex<T> {
#[inline]
pub(crate) fn new(t: T) -> Mutex<T> {
Mutex(PhantomData, parking_lot::Mutex::new(t))
pub(crate) const fn new(t: T) -> Mutex<T> {
Self::const_new(t)
}

#[inline]
Expand Down
12 changes: 12 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,18 @@ macro_rules! cfg_not_has_const_mutex_new {
}
}

macro_rules! cfg_const_if_has_const_mutex_new {
($(#[$attr:meta])* $vis:vis const fn $fn_name:ident $( $rest : tt )*) => {
#[cfg(not(all(loom, test)))]
$(#[$attr])*
$vis const fn $fn_name $( $rest )*

#[cfg(all(loom, test))]
$(#[$attr])*
$vis fn $fn_name $( $rest )*
}
}

macro_rules! cfg_not_wasi {
($($item:item)*) => {
$(
Expand Down
63 changes: 16 additions & 47 deletions tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,43 +135,22 @@ impl Semaphore {
// PERMIT_SHIFT is used to leave that bit for that purpose.
const PERMIT_SHIFT: usize = 1;

/// Creates a new semaphore with the initial number of permits
///
/// Maximum number of permits on 32-bit platforms is `1<<29`.
pub(crate) fn new(permits: usize) -> Self {
assert!(
permits <= Self::MAX_PERMITS,
"a semaphore may not have more than MAX_PERMITS permits ({})",
Self::MAX_PERMITS
);

#[cfg(all(tokio_unstable, feature = "tracing"))]
let resource_span = {
let resource_span = tracing::trace_span!(
"runtime.resource",
concrete_type = "Semaphore",
kind = "Sync",
is_internal = true
);

resource_span.in_scope(|| {
tracing::trace!(
target: "runtime::resource::state_update",
permits = permits,
permits.op = "override",
)
});
resource_span
};
cfg_const_if_has_const_mutex_new! {
/// Creates a new semaphore with the initial number of permits
///
/// Maximum number of permits on 32-bit platforms is `1<<29`.
pub(crate) const fn new(permits: usize) -> Self {
assert!(permits <= Self::MAX_PERMITS);

Self {
permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
waiters: Mutex::new(Waitlist {
queue: LinkedList::new(),
closed: false,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span,
Self {
permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
waiters: Mutex::new(Waitlist {
queue: LinkedList::new(),
closed: false,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
}
}

Expand All @@ -180,17 +159,7 @@ impl Semaphore {
/// Maximum number of permits on 32-bit platforms is `1<<29`.
#[cfg(not(all(loom, test)))]
pub(crate) const fn const_new(permits: usize) -> Self {
assert!(permits <= Self::MAX_PERMITS);

Self {
permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
waiters: Mutex::const_new(Waitlist {
queue: LinkedList::new(),
closed: false,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
Self::new(permits)
}

/// Creates a new closed semaphore with 0 permits.
Expand Down
73 changes: 22 additions & 51 deletions tokio/src/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,51 +321,27 @@ fn bounds() {
}

impl<T: ?Sized> Mutex<T> {
/// Creates a new lock in an unlocked state ready for use.
///
/// # Examples
///
/// ```
/// use tokio::sync::Mutex;
///
/// let lock = Mutex::new(5);
/// ```
#[track_caller]
pub fn new(t: T) -> Self
where
T: Sized,
{
#[cfg(all(tokio_unstable, feature = "tracing"))]
let resource_span = {
let location = std::panic::Location::caller();

tracing::trace_span!(
"runtime.resource",
concrete_type = "Mutex",
kind = "Sync",
loc.file = location.file(),
loc.line = location.line(),
loc.col = location.column(),
)
};

#[cfg(all(tokio_unstable, feature = "tracing"))]
let s = resource_span.in_scope(|| {
tracing::trace!(
target: "runtime::resource::state_update",
locked = false,
);
semaphore::Semaphore::new(1)
});

#[cfg(any(not(tokio_unstable), not(feature = "tracing")))]
let s = semaphore::Semaphore::new(1);

Self {
c: UnsafeCell::new(t),
s,
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span,
cfg_const_if_has_const_mutex_new! {
/// Creates a new lock in an unlocked state ready for use.
///
/// # Examples
///
/// ```
/// use tokio::sync::Mutex;
///
/// let lock = Mutex::new(5);
/// ```
#[track_caller]
pub const fn new(t: T) -> Self
where
T: Sized,
{
Self {
c: UnsafeCell::new(t),
s: semaphore::Semaphore::new(1),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
}
}

Expand All @@ -383,12 +359,7 @@ impl<T: ?Sized> Mutex<T> {
where
T: Sized,
{
Self {
c: UnsafeCell::new(t),
s: semaphore::Semaphore::const_new(1),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
Self::new(t)
}

/// Locks this mutex, causing the current task to yield until the lock has
Expand Down
33 changes: 16 additions & 17 deletions tokio/src/sync/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,19 +418,21 @@ fn atomic_inc_num_notify_waiters_calls(data: &AtomicUsize) {
}

impl Notify {
/// Create a new `Notify`, initialized without a permit.
///
/// # Examples
///
/// ```
/// use tokio::sync::Notify;
///
/// let notify = Notify::new();
/// ```
pub fn new() -> Notify {
Notify {
state: AtomicUsize::new(0),
waiters: Mutex::new(LinkedList::new()),
cfg_const_if_has_const_mutex_new! {
/// Create a new `Notify`, initialized without a permit.
///
/// # Examples
///
/// ```
/// use tokio::sync::Notify;
///
/// let notify = Notify::new();
/// ```
pub const fn new() -> Notify {
Notify {
state: AtomicUsize::new(0),
waiters: Mutex::new(LinkedList::new()),
}
}
}

Expand All @@ -445,10 +447,7 @@ impl Notify {
/// ```
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Notify {
Notify {
state: AtomicUsize::new(0),
waiters: Mutex::const_new(LinkedList::new()),
}
Self::const_new()
}

/// Wait for a notification.
Expand Down
20 changes: 9 additions & 11 deletions tokio/src/sync/once_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ impl<T> From<T> for OnceCell<T> {
}

impl<T> OnceCell<T> {
/// Creates a new empty `OnceCell` instance.
pub fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
cfg_const_if_has_const_mutex_new! {
/// Creates a new empty `OnceCell` instance.
pub const fn new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::new(1),
}
}
}

Expand Down Expand Up @@ -175,11 +177,7 @@ impl<T> OnceCell<T> {
/// ```
#[cfg(not(all(loom, test)))]
pub const fn const_new() -> Self {
OnceCell {
value_set: AtomicBool::new(false),
value: UnsafeCell::new(MaybeUninit::uninit()),
semaphore: Semaphore::const_new(1),
}
Self::new()
}

/// Returns `true` if the `OnceCell` currently contains a value, and `false`
Expand Down
Loading

0 comments on commit 4fe5686

Please sign in to comment.