From d1b2d29528b7794b41e66fc2136e395a02f8529b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 05:59:35 +0200 Subject: Merging upstream version 1.73.0+dfsg1. Signed-off-by: Daniel Baumann --- library/std/src/sync/barrier.rs | 7 ++----- library/std/src/sync/condvar.rs | 18 ++++++++---------- library/std/src/sync/lazy_lock.rs | 37 ++++++++++++++++++++++++++++++++++--- library/std/src/sync/mpmc/utils.rs | 4 ++++ library/std/src/sync/mpsc/mod.rs | 5 ++--- library/std/src/sync/mutex.rs | 8 +------- library/std/src/sync/once_lock.rs | 8 +++++--- library/std/src/sync/rwlock.rs | 8 +------- 8 files changed, 57 insertions(+), 38 deletions(-) (limited to 'library/std/src/sync') diff --git a/library/std/src/sync/barrier.rs b/library/std/src/sync/barrier.rs index e39254aa4..ed3c55120 100644 --- a/library/std/src/sync/barrier.rs +++ b/library/std/src/sync/barrier.rs @@ -130,11 +130,8 @@ impl Barrier { let local_gen = lock.generation_id; lock.count += 1; if lock.count < self.num_threads { - // We need a while loop to guard against spurious wakeups. - // https://en.wikipedia.org/wiki/Spurious_wakeup - while local_gen == lock.generation_id { - lock = self.cvar.wait(lock).unwrap(); - } + let _guard = + self.cvar.wait_while(lock, |state| local_gen == state.generation_id).unwrap(); BarrierWaitResult(false) } else { lock.count = 0; diff --git a/library/std/src/sync/condvar.rs b/library/std/src/sync/condvar.rs index 76a1b4a2a..9c4b926b7 100644 --- a/library/std/src/sync/condvar.rs +++ b/library/std/src/sync/condvar.rs @@ -21,11 +21,11 @@ impl WaitTimeoutResult { /// /// # Examples /// - /// This example spawns a thread which will update the boolean value and - /// then wait 100 milliseconds before notifying the condvar. + /// This example spawns a thread which will sleep 20 milliseconds before + /// updating a boolean value and then notifying the condvar. /// - /// The main thread will wait with a timeout on the condvar and then leave - /// once the boolean has been updated and notified. + /// The main thread will wait with a 10 millisecond timeout on the condvar + /// and will leave the loop upon timeout. /// /// ``` /// use std::sync::{Arc, Condvar, Mutex}; @@ -49,14 +49,12 @@ impl WaitTimeoutResult { /// /// // Wait for the thread to start up. /// let (lock, cvar) = &*pair; - /// let mut started = lock.lock().unwrap(); /// loop { /// // Let's put a timeout on the condvar's wait. - /// let result = cvar.wait_timeout(started, Duration::from_millis(10)).unwrap(); - /// // 10 milliseconds have passed, or maybe the value changed! - /// started = result.0; - /// if *started == true { - /// // We received the notification and the value has been updated, we can leave. + /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap(); + /// // 10 milliseconds have passed. + /// if result.1.timed_out() { + /// // timed out now and we can leave. /// break /// } /// } diff --git a/library/std/src/sync/lazy_lock.rs b/library/std/src/sync/lazy_lock.rs index a6bc468b0..3598598cf 100644 --- a/library/std/src/sync/lazy_lock.rs +++ b/library/std/src/sync/lazy_lock.rs @@ -25,6 +25,8 @@ union Data { /// /// # Examples /// +/// Initialize static variables with `LazyLock`. +/// /// ``` /// #![feature(lazy_cell)] /// @@ -54,6 +56,24 @@ union Data { /// // Some("Hoyten") /// } /// ``` +/// Initialize fields with `LazyLock`. +/// ``` +/// #![feature(lazy_cell)] +/// +/// use std::sync::LazyLock; +/// +/// #[derive(Debug)] +/// struct UseCellLock { +/// number: LazyLock, +/// } +/// fn main() { +/// let lock: LazyLock = LazyLock::new(|| 0u32); +/// +/// let data = UseCellLock { number: lock }; +/// println!("{}", *data.number); +/// } +/// ``` + #[unstable(feature = "lazy_cell", issue = "109736")] pub struct LazyLock T> { once: Once, @@ -69,6 +89,15 @@ impl T> LazyLock { LazyLock { once: Once::new(), data: UnsafeCell::new(Data { f: ManuallyDrop::new(f) }) } } + /// Creates a new lazy value that is already initialized. + #[inline] + #[cfg(test)] + pub(crate) fn preinit(value: T) -> LazyLock { + let once = Once::new(); + once.call_once(|| {}); + LazyLock { once, data: UnsafeCell::new(Data { value: ManuallyDrop::new(value) }) } + } + /// Consumes this `LazyLock` returning the stored value. /// /// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise. @@ -193,10 +222,12 @@ impl Default for LazyLock { #[unstable(feature = "lazy_cell", issue = "109736")] impl fmt::Debug for LazyLock { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_tuple("LazyLock"); match self.get() { - Some(v) => f.debug_tuple("LazyLock").field(v).finish(), - None => f.write_str("LazyLock(Uninit)"), - } + Some(v) => d.field(v), + None => d.field(&format_args!("")), + }; + d.finish() } } diff --git a/library/std/src/sync/mpmc/utils.rs b/library/std/src/sync/mpmc/utils.rs index d053d69e2..0cbc61160 100644 --- a/library/std/src/sync/mpmc/utils.rs +++ b/library/std/src/sync/mpmc/utils.rs @@ -35,7 +35,9 @@ use crate::ops::{Deref, DerefMut}; any( target_arch = "arm", target_arch = "mips", + target_arch = "mips32r6", target_arch = "mips64", + target_arch = "mips64r6", target_arch = "riscv64", ), repr(align(32)) @@ -59,7 +61,9 @@ use crate::ops::{Deref, DerefMut}; target_arch = "powerpc64", target_arch = "arm", target_arch = "mips", + target_arch = "mips32r6", target_arch = "mips64", + target_arch = "mips64r6", target_arch = "riscv64", target_arch = "s390x", )), diff --git a/library/std/src/sync/mpsc/mod.rs b/library/std/src/sync/mpsc/mod.rs index c00134c8b..f92bb1a4b 100644 --- a/library/std/src/sync/mpsc/mod.rs +++ b/library/std/src/sync/mpsc/mod.rs @@ -303,12 +303,11 @@ pub struct IntoIter { rx: Receiver, } -/// The sending-half of Rust's asynchronous [`channel`] type. This half can only be -/// owned by one thread, but it can be cloned to send to other threads. +/// The sending-half of Rust's asynchronous [`channel`] type. /// /// Messages can be sent through this channel with [`send`]. /// -/// Note: all senders (the original and the clones) need to be dropped for the receiver +/// Note: all senders (the original and its clones) need to be dropped for the receiver /// to stop blocking to receive messages with [`Receiver::recv`]. /// /// [`send`]: Sender::send diff --git a/library/std/src/sync/mutex.rs b/library/std/src/sync/mutex.rs index b8fec6902..b4ae6b7e0 100644 --- a/library/std/src/sync/mutex.rs +++ b/library/std/src/sync/mutex.rs @@ -490,13 +490,7 @@ impl fmt::Debug for Mutex { d.field("data", &&**err.get_ref()); } Err(TryLockError::WouldBlock) => { - struct LockedPlaceholder; - impl fmt::Debug for LockedPlaceholder { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("") - } - } - d.field("data", &LockedPlaceholder); + d.field("data", &format_args!("")); } } d.field("poisoned", &self.poison.get()); diff --git a/library/std/src/sync/once_lock.rs b/library/std/src/sync/once_lock.rs index e83bc35ee..e2b7b893c 100644 --- a/library/std/src/sync/once_lock.rs +++ b/library/std/src/sync/once_lock.rs @@ -365,10 +365,12 @@ impl Default for OnceLock { #[stable(feature = "once_cell", since = "1.70.0")] impl fmt::Debug for OnceLock { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_tuple("OnceLock"); match self.get() { - Some(v) => f.debug_tuple("Once").field(v).finish(), - None => f.write_str("Once(Uninit)"), - } + Some(v) => d.field(v), + None => d.field(&format_args!("")), + }; + d.finish() } } diff --git a/library/std/src/sync/rwlock.rs b/library/std/src/sync/rwlock.rs index 7c409cb3e..26aaa2414 100644 --- a/library/std/src/sync/rwlock.rs +++ b/library/std/src/sync/rwlock.rs @@ -485,13 +485,7 @@ impl fmt::Debug for RwLock { d.field("data", &&**err.get_ref()); } Err(TryLockError::WouldBlock) => { - struct LockedPlaceholder; - impl fmt::Debug for LockedPlaceholder { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str("") - } - } - d.field("data", &LockedPlaceholder); + d.field("data", &format_args!("")); } } d.field("poisoned", &self.poison.get()); -- cgit v1.2.3