Skip to content

Commit

Permalink
Make sure WaitGroup updates and checks atomically
Browse files Browse the repository at this point in the history
  • Loading branch information
JothamWong committed Apr 20, 2024
1 parent d30edb5 commit 97dd1f0
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions std/ooga-std.ooga
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ func (s *Sync) NewWaitGroup(counter int) WaitGroup {
}

func (wg *WaitGroup) Add(delta int) {
startAtomic();
wg.counter += delta
endAtomic();
}

func (wg *WaitGroup) Done() {
// update expressions are atomic
wg.counter--
}

func (wg *WaitGroup) Wait() {
for wg.counter > 0 {
for {
startAtomic();
if wg.counter <= 0 {
endAtomic();
break;
}
endAtomic();
yieldThread(); // yield to avoid wasteful loop
"YIELD";
}
}

Expand Down

0 comments on commit 97dd1f0

Please sign in to comment.