Skip to content

Relax orderings in std::sync::once #44331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/libstd/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl Once {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn call_once<F>(&'static self, f: F) where F: FnOnce() {
// Fast path, just see if we've completed initialization.
if self.state.load(Ordering::SeqCst) == COMPLETE {
if self.state.load(Ordering::Acquire) == COMPLETE {
return
}

Expand All @@ -243,7 +243,7 @@ impl Once {
#[unstable(feature = "once_poison", issue = "33577")]
pub fn call_once_force<F>(&'static self, f: F) where F: FnOnce(&OnceState) {
// same as above, just with a different parameter to `call_inner`.
if self.state.load(Ordering::SeqCst) == COMPLETE {
if self.state.load(Ordering::Acquire) == COMPLETE {
return
}

Expand All @@ -268,7 +268,7 @@ impl Once {
fn call_inner(&'static self,
ignore_poisoning: bool,
init: &mut FnMut(bool)) {
let mut state = self.state.load(Ordering::SeqCst);
let mut state = self.state.load(Ordering::Acquire);

'outer: loop {
match state {
Expand All @@ -289,7 +289,7 @@ impl Once {
POISONED |
INCOMPLETE => {
let old = self.state.compare_and_swap(state, RUNNING,
Ordering::SeqCst);
Ordering::Acquire);
if old != state {
state = old;
continue
Expand Down Expand Up @@ -328,7 +328,7 @@ impl Once {
node.next = (state & !STATE_MASK) as *mut Waiter;
let old = self.state.compare_and_swap(state,
me | RUNNING,
Ordering::SeqCst);
Ordering::AcqRel);
if old != state {
state = old;
continue
Expand All @@ -337,10 +337,10 @@ impl Once {
// Once we've enqueued ourselves, wait in a loop.
// Afterwards reload the state and continue with what we
// were doing from before.
while !node.signaled.load(Ordering::SeqCst) {
while !node.signaled.load(Ordering::Acquire) {
thread::park();
}
state = self.state.load(Ordering::SeqCst);
state = self.state.load(Ordering::Acquire);
continue 'outer
}
}
Expand All @@ -361,9 +361,9 @@ impl Drop for Finish {
// Swap out our state with however we finished. We should only ever see
// an old state which was RUNNING.
let queue = if self.panicked {
self.me.state.swap(POISONED, Ordering::SeqCst)
self.me.state.swap(POISONED, Ordering::Release)
} else {
self.me.state.swap(COMPLETE, Ordering::SeqCst)
self.me.state.swap(COMPLETE, Ordering::Release)
};
assert_eq!(queue & STATE_MASK, RUNNING);

Expand All @@ -376,7 +376,7 @@ impl Drop for Finish {
while !queue.is_null() {
let next = (*queue).next;
let thread = (*queue).thread.take().unwrap();
(*queue).signaled.store(true, Ordering::SeqCst);
(*queue).signaled.store(true, Ordering::Release);
thread.unpark();
queue = next;
}
Expand Down