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 <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Jul 29, 2023
1 parent efe3ab6 commit e2f9a85
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 338 deletions.
2 changes: 1 addition & 1 deletion tokio/src/loom/std/atomic_u64_static_const_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub(crate) type StaticAtomicU64 = AtomicU64;
impl AtomicU64 {
pub(crate) const fn new(val: u64) -> Self {
Self {
inner: Mutex::const_new(val),
inner: Mutex::new(val),
}
}
}
7 changes: 1 addition & 6 deletions tokio/src/loom/std/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ 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))
}

#[inline]
pub(crate) const fn const_new(t: T) -> Mutex<T> {
pub(crate) const fn new(t: T) -> Mutex<T> {
Mutex(sync::Mutex::new(t))
}

Expand Down
9 changes: 1 addition & 8 deletions tokio/src/loom/std/parking_lot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ 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))
}

#[inline]
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "parking_lot",))))]
pub(crate) const fn const_new(t: T) -> Mutex<T> {
pub(crate) const fn new(t: T) -> Mutex<T> {
Mutex(PhantomData, parking_lot::const_mutex(t))
}

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
13 changes: 2 additions & 11 deletions tokio/src/process/unix/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,15 @@ pub(crate) struct OrphanQueueImpl<T> {
}

impl<T> OrphanQueueImpl<T> {
cfg_not_has_const_mutex_new! {
pub(crate) fn new() -> Self {
cfg_const_if_has_const_mutex_new! {
pub(crate) const fn new() -> Self {
Self {
sigchild: Mutex::new(None),
queue: Mutex::new(Vec::new()),
}
}
}

cfg_has_const_mutex_new! {
pub(crate) const fn new() -> Self {
Self {
sigchild: Mutex::const_new(None),
queue: Mutex::const_new(Vec::new()),
}
}
}

#[cfg(test)]
fn len(&self) -> usize {
self.queue.lock().len()
Expand Down
69 changes: 15 additions & 54 deletions tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,61 +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
};

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

/// Creates a new semaphore with the initial number of permits.
///
/// 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);
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::const_new(Waitlist {
queue: LinkedList::new(),
closed: false,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
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 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::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 e2f9a85

Please sign in to comment.