diff options
| author | Ralf Jung <post@ralfj.de> | 2018-07-17 20:51:31 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2018-07-17 20:51:31 +0200 |
| commit | 3e1254d956d944a2909f61f733427350cd96f410 (patch) | |
| tree | 806215f64765419d8d139a2c247c323c6f8bdbf2 /src/libstd | |
| parent | 1c84d8187395b641616649a0f16f15a78ea8bbce (diff) | |
sync::Once: Use Acquire on the hot path, and explain why we don't use it elsewhere
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/sync/once.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/libstd/sync/once.rs b/src/libstd/sync/once.rs index 51c42995d5e..443e2a6980d 100644 --- a/src/libstd/sync/once.rs +++ b/src/libstd/sync/once.rs @@ -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 } @@ -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 } |
