Skip to content

Commit

Permalink
std/sync: update memory order of atomicity for WaitGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Dec 3, 2024
1 parent 24dc68c commit c286b88
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions std/sync/waitgroup.jule
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl WaitGroup {
// and unblocks any wait() calls if task count becomes zero.
// Panics if task count reaches below zero.
fn Add(mut self, delta: int) {
oldTask := int(self.taskN.Add(u32(delta), atomic::Relaxed))
oldTask := int(self.taskN.Add(u32(delta), atomic::SeqCst))
nTask := oldTask + delta
if nTask < 0 {
panic("sync: WaitGroup.Add: negative number of tasks")
Expand All @@ -42,12 +42,12 @@ impl WaitGroup {

// Number of tasks reaches to zero, therefore clear waiters.
for {
nWaiters := self.waitN.Load(atomic::Relaxed)
nWaiters := self.waitN.Load(atomic::SeqCst)
if nWaiters == 0 {
ret
}

if self.waitN.CompareSwap(nWaiters, 0, atomic::Relaxed) {
if self.waitN.CompareSwap(nWaiters, 0, atomic::SeqCst) {
ret
}
}
Expand All @@ -58,17 +58,17 @@ impl WaitGroup {

// Blocks until all tasks are done (task count becomes zero)
fn Wait(mut self) {
nTask := self.taskN.Load(atomic::Relaxed)
nTask := self.taskN.Load(atomic::SeqCst)
if nTask == 0 {
// No task, no need to wait.
ret
}

// Register this wait call to waiters.
self.waitN.Add(1, atomic::Relaxed)
self.waitN.Add(1, atomic::SeqCst)

// Wait for clearing waiters.
for self.waitN.Load(atomic::Relaxed) != 0 {
for self.waitN.Load(atomic::SeqCst) != 0 {
}
}
}

0 comments on commit c286b88

Please sign in to comment.