Skip to content

Commit

Permalink
Move some things to std::sync::poison and reexport them in std::sync
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigorenkoPV committed Dec 23, 2024
1 parent 85c3989 commit 326d1e0
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 25 deletions.
2 changes: 2 additions & 0 deletions library/std/src/sync/barrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ use crate::sync::{Condvar, Mutex};
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Barrier {
// FIXME(sync_nonpoison): switch to nonpoison version once it is available
lock: Mutex<BarrierState>,
// FIXME(sync_nonpoison): switch to nonpoison version once it is available
cvar: Condvar,
num_threads: usize,
}
Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sync/lazy_lock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::once::ExclusiveState;
use super::poison::once::ExclusiveState;
use crate::cell::UnsafeCell;
use crate::mem::ManuallyDrop;
use crate::ops::Deref;
Expand Down Expand Up @@ -63,6 +63,7 @@ union Data<T, F> {
/// ```
#[stable(feature = "lazy_cell", since = "1.80.0")]
pub struct LazyLock<T, F = fn() -> T> {
// FIXME(sync_nonpoison): if possible, switch to nonpoison version once it is available
once: Once,
data: UnsafeCell<Data<T, F>>,
}
Expand Down
66 changes: 47 additions & 19 deletions library/std/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@
#![stable(feature = "rust1", since = "1.0.0")]

// No formatting: this file is just re-exports, and their order is worth preserving.
#![cfg_attr(rustfmt, rustfmt::skip)]


// These come from `core` & `alloc` and only in one flavor: no poisoning.
#[unstable(feature = "exclusive_wrapper", issue = "98407")]
pub use core::sync::Exclusive;
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -175,40 +180,63 @@ pub use core::sync::atomic;
#[stable(feature = "rust1", since = "1.0.0")]
pub use alloc_crate::sync::{Arc, Weak};


// These exist only in one flavor: no poisoning.
// FIXME(sync_nonpoison): should we move these to `sync::nonpoison` and unconditionally reexport them here?
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::barrier::{Barrier, BarrierWaitResult};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::condvar::{Condvar, WaitTimeoutResult};
#[stable(feature = "lazy_cell", since = "1.80.0")]
pub use self::lazy_lock::LazyLock;
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
pub use self::mutex::MappedMutexGuard;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::mutex::{Mutex, MutexGuard};
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated)]
pub use self::once::{ONCE_INIT, Once, OnceState};
#[stable(feature = "once_cell", since = "1.70.0")]
pub use self::once_lock::OnceLock;


// These make sense and exist only with poisoning.
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
pub use self::poison::{LockResult, PoisonError};


// These (should) exist in both flavors: with and without poisoning.
// FIXME(sync_nonpoison): implement nonpoison versions:
// * Mutex
// * Condvar
// * Once
// * RwLock

// FIXME(sync_nonpoison): conditionally select the default flavor based on edition(?).
use self::poison as default_flavor;

#[stable(feature = "rust1", since = "1.0.0")]
pub use default_flavor::{
Mutex, MutexGuard, TryLockError, TryLockResult,
Condvar, WaitTimeoutResult,
Once, OnceState,
RwLock, RwLockReadGuard, RwLockWriteGuard,
};
#[stable(feature = "rust1", since = "1.0.0")]
#[expect(deprecated)]
pub use default_flavor::ONCE_INIT;
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
pub use default_flavor::{MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard};


// FIXME(reentrant_lock): current implementation does not support poisoning.
// Either move it to `sync::nonpoison`,
// or split into two implementations (with and without poisoning)
// and move them to `sync::poison` and `sync::nonpoison` respectively.
#[unstable(feature = "reentrant_lock", issue = "121440")]
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};


#[unstable(feature = "mpmc_channel", issue = "126840")]
pub mod mpmc;
pub mod mpsc;

#[unstable(feature = "sync_poison_mod", issue = "134646")]
pub mod poison;

mod barrier;
mod condvar;
mod lazy_lock;
mod mutex;
pub(crate) mod once;
mod once_lock;
mod poison;

mod reentrant_lock;
mod rwlock;
1 change: 1 addition & 0 deletions library/std/src/sync/once_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ use crate::sync::Once;
/// ```
#[stable(feature = "once_cell", since = "1.70.0")]
pub struct OnceLock<T> {
// FIXME(sync_nonpoison): if possible, switch to nonpoison version once it is available
once: Once,
// Whether or not the value is initialized is tracked by `once.is_completed()`.
value: UnsafeCell<MaybeUninit<T>>,
Expand Down
29 changes: 26 additions & 3 deletions library/std/src/sync/poison.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
//! TODO
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::condvar::{Condvar, WaitTimeoutResult};
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
pub use self::mutex::MappedMutexGuard;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::mutex::{Mutex, MutexGuard};
#[stable(feature = "rust1", since = "1.0.0")]
#[expect(deprecated)]
pub use self::once::ONCE_INIT;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::once::{Once, OnceState};
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::error::Error;
use crate::fmt;
#[cfg(panic = "unwind")]
use crate::sync::atomic::{AtomicBool, Ordering};
#[cfg(panic = "unwind")]
use crate::thread;

pub struct Flag {
mod condvar;
#[stable(feature = "rust1", since = "1.0.0")]
mod mutex;
pub(crate) mod once;
mod rwlock;

pub(crate) struct Flag {
#[cfg(panic = "unwind")]
failed: AtomicBool,
}
Expand Down Expand Up @@ -78,7 +101,7 @@ impl Flag {
}

#[derive(Clone)]
pub struct Guard {
pub(crate) struct Guard {
#[cfg(panic = "unwind")]
panicking: bool,
}
Expand Down Expand Up @@ -316,7 +339,7 @@ impl<T> Error for TryLockError<T> {
}
}

pub fn map_result<T, U, F>(result: LockResult<T>, f: F) -> LockResult<U>
pub(crate) fn map_result<T, U, F>(result: LockResult<T>, f: F) -> LockResult<U>
where
F: FnOnce(T) -> U,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod tests;

use crate::fmt;
use crate::sync::{LockResult, MutexGuard, PoisonError, mutex, poison};
use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
use crate::sys::sync as sys;
use crate::time::{Duration, Instant};

Expand All @@ -16,6 +16,8 @@ use crate::time::{Duration, Instant};
#[stable(feature = "wait_timeout", since = "1.5.0")]
pub struct WaitTimeoutResult(bool);

// FIXME(sync_nonpoison): `WaitTimeoutResult` is actually poisoning-agnostic, it seems.
// Should we take advantage of this fact?
impl WaitTimeoutResult {
/// Returns `true` if the wait was known to have timed out.
///
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion library/std/src/sys/sync/once/futex.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cell::Cell;
use crate::sync as public;
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
use crate::sync::once::ExclusiveState;
use crate::sync::poison::once::ExclusiveState;
use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all};

// On some platforms, the OS is very nice and handles the waiter queue for us.
Expand Down

0 comments on commit 326d1e0

Please sign in to comment.