Skip to content

Commit c2b84fb

Browse files
committed
change LazyLock implementation to use nonpoison::Once
Additionally moves `ExclusiveState` into `sys::sync::once` since it is no longer needed in the `poison` module.
1 parent 5ed1b4e commit c2b84fb

File tree

7 files changed

+24
-38
lines changed

7 files changed

+24
-38
lines changed

library/std/src/sync/lazy_lock.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
use super::poison::once::ExclusiveState;
21
use crate::cell::UnsafeCell;
32
use crate::mem::ManuallyDrop;
43
use crate::ops::{Deref, DerefMut};
54
use crate::panic::{RefUnwindSafe, UnwindSafe};
6-
use crate::sync::Once;
5+
use crate::sync::nonpoison::once::Once;
6+
use crate::sys::sync::once::ExclusiveState;
77
use crate::{fmt, ptr};
88

9-
// We use the state of a Once as discriminant value. Upon creation, the state is
10-
// "incomplete" and `f` contains the initialization closure. In the first call to
11-
// `call_once`, `f` is taken and run. If it succeeds, `value` is set and the state
12-
// is changed to "complete". If it panics, the Once is poisoned, so none of the
13-
// two fields is initialized.
9+
/// We use the state of a Once (the [`ExclusiveState`] enum) as discriminant value.
10+
///
11+
/// Upon creation, the state is "incomplete" and `f` contains the initialization closure. In the
12+
/// first call to `call_once()`, `f` is taken and run. If it succeeds, `value` is set and the state
13+
/// is changed to "complete". If it panics, the [`Once`] is poisoned, so none of the two fields is
14+
/// initialized.
15+
///
16+
/// [`call_once()`]: Once::call_once
1417
union Data<T, F> {
1518
value: ManuallyDrop<T>,
1619
f: ManuallyDrop<F>,
@@ -63,7 +66,6 @@ union Data<T, F> {
6366
/// ```
6467
#[stable(feature = "lazy_cell", since = "1.80.0")]
6568
pub struct LazyLock<T, F = fn() -> T> {
66-
// FIXME(nonpoison_once): if possible, switch to nonpoison version once it is available
6769
once: Once,
6870
data: UnsafeCell<Data<T, F>>,
6971
}

library/std/src/sync/poison/once.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ pub struct OnceState {
4949
pub(crate) inner: sys::OnceState,
5050
}
5151

52-
pub(crate) enum ExclusiveState {
53-
Incomplete,
54-
Poisoned,
55-
Complete,
56-
}
57-
5852
/// Initialization value for static [`Once`] values.
5953
///
6054
/// # Examples
@@ -299,26 +293,6 @@ impl Once {
299293
self.inner.wait(true);
300294
}
301295
}
302-
303-
/// Returns the current state of the `Once` instance.
304-
///
305-
/// Since this takes a mutable reference, no initialization can currently
306-
/// be running, so the state must be either "incomplete", "poisoned" or
307-
/// "complete".
308-
#[inline]
309-
pub(crate) fn state(&mut self) -> ExclusiveState {
310-
self.inner.state()
311-
}
312-
313-
/// Sets current state of the `Once` instance.
314-
///
315-
/// Since this takes a mutable reference, no initialization can currently
316-
/// be running, so the state must be either "incomplete", "poisoned" or
317-
/// "complete".
318-
#[inline]
319-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
320-
self.inner.set_state(new_state);
321-
}
322296
}
323297

324298
#[stable(feature = "std_debug", since = "1.16.0")]

library/std/src/sys/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod condvar;
22
mod mutex;
3-
mod once;
3+
pub(crate) mod once; // Note that this is exposed for `LazyLock`.
44
mod once_box;
55
mod rwlock;
66
mod thread_parking;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use super::ExclusiveState;
12
use crate::cell::Cell;
23
use crate::sync as public;
34
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
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.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
// This also gives us the opportunity to optimize the implementation a bit which
88
// should help the fast path on call sites.
99

10+
// FIXME(nonpoison_once): Is this a good place to put this?
11+
/// The mode that the `Once` is in.
12+
///
13+
/// This type is used in the implementation of [`LazyLock`](crate::sync::LazyLock)
14+
pub(crate) enum ExclusiveState {
15+
Incomplete,
16+
Poisoned,
17+
Complete,
18+
}
19+
1020
cfg_if::cfg_if! {
1121
if #[cfg(any(
1222
all(target_os = "windows", not(target_vendor="win7")),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use super::ExclusiveState;
12
use crate::cell::Cell;
23
use crate::sync as public;
3-
use crate::sync::poison::once::ExclusiveState;
44

55
pub struct Once {
66
state: Cell<State>,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
// processor. Because both use acquire ordering such a reordering is not
5656
// allowed, so no need for `SeqCst`.
5757

58+
use super::ExclusiveState;
5859
use crate::cell::Cell;
5960
use crate::sync::atomic::Ordering::{AcqRel, Acquire, Release};
6061
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr};
61-
use crate::sync::poison::once::ExclusiveState;
6262
use crate::thread::{self, Thread};
6363
use crate::{fmt, ptr, sync as public};
6464

0 commit comments

Comments
 (0)