Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: add OnceCell::wait_initialized method #7161

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion spellcheck.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
300
302
&
+
<
Expand Down Expand Up @@ -139,6 +139,7 @@ implementers
implementor
implementors
incrementing
initializer
inlining
interoperate
invariants
Expand Down Expand Up @@ -299,3 +300,4 @@ Wakers
wakeup
wakeups
workstealing
ZSTs
35 changes: 35 additions & 0 deletions tokio/src/loom/std/atomic_u8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::cell::UnsafeCell;
use std::fmt;
use std::ops::Deref;
use std::panic;

/// `AtomicU8` providing an additional `with_mut` function.
pub(crate) struct AtomicU8 {
inner: std::sync::atomic::AtomicU8,
}

impl AtomicU8 {
pub(crate) const fn new(val: u8) -> AtomicU8 {
let inner = std::sync::atomic::AtomicU8::new(val);
AtomicU8 { inner }
}

/// Get access to a mutable reference to the inner value.
pub(crate) fn with_mut<R>(&mut self, f: impl FnOnce(&mut u8) -> R) -> R {
f(self.inner.get_mut())
}
}

impl Deref for AtomicU8 {
type Target = std::sync::atomic::AtomicU8;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl fmt::Debug for AtomicU8 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
self.deref().fmt(fmt)
}
}
4 changes: 3 additions & 1 deletion tokio/src/loom/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod atomic_u16;
mod atomic_u32;
mod atomic_u64;
mod atomic_u8;
mod atomic_usize;
mod barrier;
mod mutex;
Expand Down Expand Up @@ -77,9 +78,10 @@ pub(crate) mod sync {
pub(crate) use crate::loom::std::atomic_u16::AtomicU16;
pub(crate) use crate::loom::std::atomic_u32::AtomicU32;
pub(crate) use crate::loom::std::atomic_u64::{AtomicU64, StaticAtomicU64};
pub(crate) use crate::loom::std::atomic_u8::AtomicU8;
pub(crate) use crate::loom::std::atomic_usize::AtomicUsize;

pub(crate) use std::sync::atomic::{fence, AtomicBool, AtomicPtr, AtomicU8, Ordering};
pub(crate) use std::sync::atomic::{fence, AtomicBool, AtomicPtr, Ordering};
}

pub(crate) use super::barrier::Barrier;
Expand Down
14 changes: 14 additions & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ macro_rules! cfg_io_driver_impl {
}
}

macro_rules! cfg_io_driver_or_sync {
( $( $item:item )* ) => {
$(
#[cfg(any(
feature = "net",
all(unix, feature = "process"),
all(unix, feature = "signal"),
feature = "sync",
))]
$item
)*
}
}

macro_rules! cfg_not_io_driver {
($($item:item)*) => {
$(
Expand Down
27 changes: 0 additions & 27 deletions tokio/src/sync/batch_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,33 +193,6 @@ impl Semaphore {
}
}

/// Creates a new closed semaphore with 0 permits.
pub(crate) fn new_closed() -> Self {
Self {
permits: AtomicUsize::new(Self::CLOSED),
waiters: Mutex::new(Waitlist {
queue: LinkedList::new(),
closed: true,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
}

/// Creates a new closed semaphore with 0 permits.
#[cfg(not(all(loom, test)))]
pub(crate) const fn const_new_closed() -> Self {
Self {
permits: AtomicUsize::new(Self::CLOSED),
waiters: Mutex::const_new(Waitlist {
queue: LinkedList::new(),
closed: true,
}),
#[cfg(all(tokio_unstable, feature = "tracing"))]
resource_span: tracing::Span::none(),
}
}

/// Returns the current number of available permits.
pub(crate) fn available_permits(&self) -> usize {
self.permits.load(Acquire) >> Self::PERMIT_SHIFT
Expand Down
Loading