Replace atomic Or with compare-and-swap loop

This commit is contained in:
Vorapol Rinsatitnon
2025-09-18 12:13:44 +08:00
parent 57388d3e2f
commit f3a584b498

View File

@@ -94,11 +94,20 @@ func (wg *WaitGroup) Add(delta int) {
fatal("sync: WaitGroup.Add called from multiple synctest bubbles") fatal("sync: WaitGroup.Add called from multiple synctest bubbles")
case synctest.CurrentBubble: case synctest.CurrentBubble:
bubbled = true bubbled = true
state := wg.state.Or(waitGroupBubbleFlag) // Use compare-and-swap loop to implement atomic Or operation
// since race detector doesn't have __tsan_go_atomic64_fetch_or
for {
old := wg.state.Load()
new := old | waitGroupBubbleFlag
if wg.state.CompareAndSwap(old, new) {
state := old
if state != 0 && state&waitGroupBubbleFlag == 0 { if state != 0 && state&waitGroupBubbleFlag == 0 {
// Add has been called from outside this bubble. // Add has been called from outside this bubble.
fatal("sync: WaitGroup.Add called from inside and outside synctest bubble") fatal("sync: WaitGroup.Add called from inside and outside synctest bubble")
} }
break
}
}
} }
} }
state := wg.state.Add(uint64(delta) << 32) state := wg.state.Add(uint64(delta) << 32)