Skip to content

Commit

Permalink
std/sync: update memory order of atomicity for Once
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Dec 3, 2024
1 parent c286b88 commit 6fba559
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions std/sync/once.jule
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Once {
fn Do(self, f: fn()) {
// Note: Here is an incorrect implementation of do:
//
// if self.done.compare_swap(0, 1) {
// if self.done.CompareSwap(0, 1) {
// f()
// }
//
Expand All @@ -90,17 +90,17 @@ impl Once {
// This is why the slow path falls back to a mutex, and why
// the self.done.store must be delayed until after f returns.

if self.done.Load(atomic::Relaxed) == 0 {
if self.done.Load(atomic::SeqCst) == 0 {
// Outlined slow-path to allow inlining of the fast-path.
self.doSlow(f)
}
}

fn doSlow(self, f: fn()) {
self.m.Lock()
if self.done.Load(atomic::Relaxed) == 0 {
if self.done.Load(atomic::SeqCst) == 0 {
f()
self.done.Store(1, atomic::Relaxed)
self.done.Store(1, atomic::SeqCst)
}
self.m.Unlock()
}
Expand Down

0 comments on commit 6fba559

Please sign in to comment.