Skip to content

Commit 326d1e0

Browse files
committed
Move some things to std::sync::poison and reexport them in std::sync
1 parent 85c3989 commit 326d1e0

File tree

14 files changed

+82
-25
lines changed

14 files changed

+82
-25
lines changed

library/std/src/sync/barrier.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use crate::sync::{Condvar, Mutex};
3333
/// ```
3434
#[stable(feature = "rust1", since = "1.0.0")]
3535
pub struct Barrier {
36+
// FIXME(sync_nonpoison): switch to nonpoison version once it is available
3637
lock: Mutex<BarrierState>,
38+
// FIXME(sync_nonpoison): switch to nonpoison version once it is available
3739
cvar: Condvar,
3840
num_threads: usize,
3941
}

library/std/src/sync/lazy_lock.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::once::ExclusiveState;
1+
use super::poison::once::ExclusiveState;
22
use crate::cell::UnsafeCell;
33
use crate::mem::ManuallyDrop;
44
use crate::ops::Deref;
@@ -63,6 +63,7 @@ union Data<T, F> {
6363
/// ```
6464
#[stable(feature = "lazy_cell", since = "1.80.0")]
6565
pub struct LazyLock<T, F = fn() -> T> {
66+
// FIXME(sync_nonpoison): if possible, switch to nonpoison version once it is available
6667
once: Once,
6768
data: UnsafeCell<Data<T, F>>,
6869
}

library/std/src/sync/mod.rs

+47-19
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167
168168
#![stable(feature = "rust1", since = "1.0.0")]
169169

170+
// No formatting: this file is just re-exports, and their order is worth preserving.
171+
#![cfg_attr(rustfmt, rustfmt::skip)]
172+
173+
174+
// These come from `core` & `alloc` and only in one flavor: no poisoning.
170175
#[unstable(feature = "exclusive_wrapper", issue = "98407")]
171176
pub use core::sync::Exclusive;
172177
#[stable(feature = "rust1", since = "1.0.0")]
@@ -175,40 +180,63 @@ pub use core::sync::atomic;
175180
#[stable(feature = "rust1", since = "1.0.0")]
176181
pub use alloc_crate::sync::{Arc, Weak};
177182

183+
184+
// These exist only in one flavor: no poisoning.
185+
// FIXME(sync_nonpoison): should we move these to `sync::nonpoison` and unconditionally reexport them here?
178186
#[stable(feature = "rust1", since = "1.0.0")]
179187
pub use self::barrier::{Barrier, BarrierWaitResult};
180-
#[stable(feature = "rust1", since = "1.0.0")]
181-
pub use self::condvar::{Condvar, WaitTimeoutResult};
182188
#[stable(feature = "lazy_cell", since = "1.80.0")]
183189
pub use self::lazy_lock::LazyLock;
184-
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
185-
pub use self::mutex::MappedMutexGuard;
186-
#[stable(feature = "rust1", since = "1.0.0")]
187-
pub use self::mutex::{Mutex, MutexGuard};
188-
#[stable(feature = "rust1", since = "1.0.0")]
189-
#[allow(deprecated)]
190-
pub use self::once::{ONCE_INIT, Once, OnceState};
191190
#[stable(feature = "once_cell", since = "1.70.0")]
192191
pub use self::once_lock::OnceLock;
192+
193+
194+
// These make sense and exist only with poisoning.
193195
#[stable(feature = "rust1", since = "1.0.0")]
194-
pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
196+
pub use self::poison::{LockResult, PoisonError};
197+
198+
199+
// These (should) exist in both flavors: with and without poisoning.
200+
// FIXME(sync_nonpoison): implement nonpoison versions:
201+
// * Mutex
202+
// * Condvar
203+
// * Once
204+
// * RwLock
205+
206+
// FIXME(sync_nonpoison): conditionally select the default flavor based on edition(?).
207+
use self::poison as default_flavor;
208+
209+
#[stable(feature = "rust1", since = "1.0.0")]
210+
pub use default_flavor::{
211+
Mutex, MutexGuard, TryLockError, TryLockResult,
212+
Condvar, WaitTimeoutResult,
213+
Once, OnceState,
214+
RwLock, RwLockReadGuard, RwLockWriteGuard,
215+
};
216+
#[stable(feature = "rust1", since = "1.0.0")]
217+
#[expect(deprecated)]
218+
pub use default_flavor::ONCE_INIT;
219+
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
220+
pub use default_flavor::{MappedMutexGuard, MappedRwLockReadGuard, MappedRwLockWriteGuard};
221+
222+
223+
// FIXME(reentrant_lock): current implementation does not support poisoning.
224+
// Either move it to `sync::nonpoison`,
225+
// or split into two implementations (with and without poisoning)
226+
// and move them to `sync::poison` and `sync::nonpoison` respectively.
195227
#[unstable(feature = "reentrant_lock", issue = "121440")]
196228
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
197-
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
198-
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
199-
#[stable(feature = "rust1", since = "1.0.0")]
200-
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
229+
201230

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

235+
#[unstable(feature = "sync_poison_mod", issue = "134646")]
236+
pub mod poison;
237+
206238
mod barrier;
207-
mod condvar;
208239
mod lazy_lock;
209-
mod mutex;
210-
pub(crate) mod once;
211240
mod once_lock;
212-
mod poison;
241+
213242
mod reentrant_lock;
214-
mod rwlock;

library/std/src/sync/once_lock.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ use crate::sync::Once;
101101
/// ```
102102
#[stable(feature = "once_cell", since = "1.70.0")]
103103
pub struct OnceLock<T> {
104+
// FIXME(sync_nonpoison): if possible, switch to nonpoison version once it is available
104105
once: Once,
105106
// Whether or not the value is initialized is tracked by `once.is_completed()`.
106107
value: UnsafeCell<MaybeUninit<T>>,

library/std/src/sync/poison.rs

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
//! TODO
2+
3+
#[stable(feature = "rust1", since = "1.0.0")]
4+
pub use self::condvar::{Condvar, WaitTimeoutResult};
5+
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
6+
pub use self::mutex::MappedMutexGuard;
7+
#[stable(feature = "rust1", since = "1.0.0")]
8+
pub use self::mutex::{Mutex, MutexGuard};
9+
#[stable(feature = "rust1", since = "1.0.0")]
10+
#[expect(deprecated)]
11+
pub use self::once::ONCE_INIT;
12+
#[stable(feature = "rust1", since = "1.0.0")]
13+
pub use self::once::{Once, OnceState};
14+
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
15+
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
16+
#[stable(feature = "rust1", since = "1.0.0")]
17+
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
118
use crate::error::Error;
219
use crate::fmt;
320
#[cfg(panic = "unwind")]
421
use crate::sync::atomic::{AtomicBool, Ordering};
522
#[cfg(panic = "unwind")]
623
use crate::thread;
724

8-
pub struct Flag {
25+
mod condvar;
26+
#[stable(feature = "rust1", since = "1.0.0")]
27+
mod mutex;
28+
pub(crate) mod once;
29+
mod rwlock;
30+
31+
pub(crate) struct Flag {
932
#[cfg(panic = "unwind")]
1033
failed: AtomicBool,
1134
}
@@ -78,7 +101,7 @@ impl Flag {
78101
}
79102

80103
#[derive(Clone)]
81-
pub struct Guard {
104+
pub(crate) struct Guard {
82105
#[cfg(panic = "unwind")]
83106
panicking: bool,
84107
}
@@ -316,7 +339,7 @@ impl<T> Error for TryLockError<T> {
316339
}
317340
}
318341

319-
pub fn map_result<T, U, F>(result: LockResult<T>, f: F) -> LockResult<U>
342+
pub(crate) fn map_result<T, U, F>(result: LockResult<T>, f: F) -> LockResult<U>
320343
where
321344
F: FnOnce(T) -> U,
322345
{

library/std/src/sync/condvar.rs renamed to library/std/src/sync/poison/condvar.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
mod tests;
33

44
use crate::fmt;
5-
use crate::sync::{LockResult, MutexGuard, PoisonError, mutex, poison};
5+
use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex};
66
use crate::sys::sync as sys;
77
use crate::time::{Duration, Instant};
88

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

19+
// FIXME(sync_nonpoison): `WaitTimeoutResult` is actually poisoning-agnostic, it seems.
20+
// Should we take advantage of this fact?
1921
impl WaitTimeoutResult {
2022
/// Returns `true` if the wait was known to have timed out.
2123
///
File renamed without changes.
File renamed without changes.

library/std/src/sys/sync/once/futex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cell::Cell;
22
use crate::sync as public;
33
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
4-
use crate::sync::once::ExclusiveState;
4+
use crate::sync::poison::once::ExclusiveState;
55
use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all};
66

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

0 commit comments

Comments
 (0)