From d1017940d77f35f841008c3e108e3da5e48a592f Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Tue, 30 Jun 2020 18:27:21 +1000 Subject: remove inlined lazy::Waiter in favor of sync::Once --- src/libstd/sync/once.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'src/libstd/sync') diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 7dc822db3d0..1fce5dc0352 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -132,6 +132,7 @@ unsafe impl Send for Once {} #[derive(Debug)] pub struct OnceState { poisoned: bool, + set_state_on_drop_to: Cell, } /// Initialization value for static [`Once`] values. @@ -321,7 +322,7 @@ impl Once { } let mut f = Some(f); - self.call_inner(true, &mut |p| f.take().unwrap()(&OnceState { poisoned: p })); + self.call_inner(true, &mut |p| f.take().unwrap()(p)); } /// Returns `true` if some `call_once` call has completed @@ -385,7 +386,7 @@ impl Once { // currently no way to take an `FnOnce` and call it via virtual dispatch // without some allocation overhead. #[cold] - fn call_inner(&self, ignore_poisoning: bool, init: &mut dyn FnMut(bool)) { + fn call_inner(&self, ignore_poisoning: bool, init: &mut dyn FnMut(&OnceState)) { let mut state_and_queue = self.state_and_queue.load(Ordering::Acquire); loop { match state_and_queue { @@ -413,8 +414,9 @@ impl Once { }; // Run the initialization function, letting it know if we're // poisoned or not. - init(state_and_queue == POISONED); - waiter_queue.set_state_on_drop_to = COMPLETE; + let init_state = OnceState { poisoned: state_and_queue == POISONED, set_state_on_drop_to: Cell::new(COMPLETE) }; + init(&init_state); + waiter_queue.set_state_on_drop_to = init_state.set_state_on_drop_to.get(); break; } _ => { @@ -554,6 +556,14 @@ impl OnceState { pub fn poisoned(&self) -> bool { self.poisoned } + + /// Poison the associated [`Once`] without explicitly panicking. + /// + /// [`Once`]: struct.Once.html + // NOTE: This is currently only exposed for the `lazy` module + pub(crate) fn poison(&self) { + self.set_state_on_drop_to.set(POISONED); + } } #[cfg(all(test, not(target_os = "emscripten")))] -- cgit 1.4.1-3-g733a5 From 1f1cda65d9a5c88855d3fbcb3912095474e557de Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Tue, 30 Jun 2020 18:36:10 +1000 Subject: appease tidy --- src/libstd/lazy.rs | 5 +---- src/libstd/sync/once.rs | 5 ++++- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/libstd/sync') diff --git a/src/libstd/lazy.rs b/src/libstd/lazy.rs index 761cc2b439f..094eff17f89 100644 --- a/src/libstd/lazy.rs +++ b/src/libstd/lazy.rs @@ -116,10 +116,7 @@ impl SyncOnceCell { /// Creates a new empty cell. #[unstable(feature = "once_cell", issue = "68198")] pub const fn new() -> SyncOnceCell { - SyncOnceCell { - once: Once::new(), - value: UnsafeCell::new(MaybeUninit::uninit()), - } + SyncOnceCell { once: Once::new(), value: UnsafeCell::new(MaybeUninit::uninit()) } } /// Gets the reference to the underlying value. diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 1fce5dc0352..64260990824 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -414,7 +414,10 @@ impl Once { }; // Run the initialization function, letting it know if we're // poisoned or not. - let init_state = OnceState { poisoned: state_and_queue == POISONED, set_state_on_drop_to: Cell::new(COMPLETE) }; + let init_state = OnceState { + poisoned: state_and_queue == POISONED, + set_state_on_drop_to: Cell::new(COMPLETE), + }; init(&init_state); waiter_queue.set_state_on_drop_to = init_state.set_state_on_drop_to.get(); break; -- cgit 1.4.1-3-g733a5