import (
. "runtime"
+ "sync"
"sync/atomic"
"testing"
)
// to another goroutine. Stop the current goroutine from migrating to
// another CPU where it can win the race (and appear to have not yielded) by
// keeping the CPUs slightly busy.
+ var wg sync.WaitGroup
for i := 0; i < GOMAXPROCS(-1); i++ {
+ wg.Add(1)
go func() {
+ defer wg.Done()
for {
select {
case <-done:
}()
}
+ wg.Add(1)
go func() {
+ defer wg.Done()
Semacquire(&sema)
atomic.CompareAndSwapUint32(&res, 0, 1)
Semrelease1(&sema, true, 0)
atomic.CompareAndSwapUint32(&res, 0, 2)
- <-done // wait for goroutines to finish to avoid data races
+ wg.Wait() // wait for goroutines to finish to avoid data races
return res == 1 // did the waiter run first?
}