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,10 +94,19 @@ func (wg *WaitGroup) Add(delta int) {
fatal("sync: WaitGroup.Add called from multiple synctest bubbles")
case synctest.CurrentBubble:
bubbled = true
state := wg.state.Or(waitGroupBubbleFlag)
if state != 0 && state&waitGroupBubbleFlag == 0 {
// Add has been called from outside this bubble.
fatal("sync: WaitGroup.Add called from inside and outside synctest bubble")
// 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 {
// Add has been called from outside this bubble.
fatal("sync: WaitGroup.Add called from inside and outside synctest bubble")
}
break
}
}
}
}