Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
sync::Once: Use Acquire on the hot path, and explain why we don't use…
… it elsewhere
  • Loading branch information
RalfJung committed Jul 17, 2018
1 parent 1c84d81 commit 3e1254d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/libstd/sync/once.rs
Expand Up @@ -220,7 +220,11 @@ impl Once {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn call_once<F>(&self, f: F) where F: FnOnce() {
// Fast path, just see if we've completed initialization.
if self.state.load(Ordering::SeqCst) == COMPLETE {
// An `Acquire` load is enough because that makes all the initialization
// operations visible to us. The cold path uses SeqCst consistently
// because the performance difference really does not matter there,
// and SeqCst minimizes the chances of something going wrong.
if self.state.load(Ordering::Acquire) == COMPLETE {
return
}

Expand Down Expand Up @@ -277,7 +281,11 @@ impl Once {
#[unstable(feature = "once_poison", issue = "33577")]
pub fn call_once_force<F>(&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 {
// An `Acquire` load is enough because that makes all the initialization
// operations visible to us. The cold path uses SeqCst consistently
// because the performance difference really does not matter there,
// and SeqCst minimizes the chances of something going wrong.
if self.state.load(Ordering::Acquire) == COMPLETE {
return
}

Expand Down

0 comments on commit 3e1254d

Please sign in to comment.