diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-02-12 20:17:16 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-02-22 04:08:12 +0000 |
commit | 5912f4fc376013897998747bd81602dce962072b (patch) | |
tree | ab5f4bb0bfd50126f4cbd6acee7821ea12c0cd75 /src/runtime/sema_test.go | |
parent | af686da46ffeb463c113607bac3ec5e8f53acaf9 (diff) | |
download | go-5912f4fc376013897998747bd81602dce962072b.tar.gz go-5912f4fc376013897998747bd81602dce962072b.zip |
runtime: really wait for goroutines in testSemaHandoff
The code has a comment saying that it waited for the goroutines,
but it didn't actually do so.
Change-Id: Icaeb40613711053a9f443cc34143835560427dda
Reviewed-on: https://go-review.googlesource.com/c/go/+/219277
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/runtime/sema_test.go')
-rw-r--r-- | src/runtime/sema_test.go | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/src/runtime/sema_test.go b/src/runtime/sema_test.go index 8bd5d4ce57..cf3de0a190 100644 --- a/src/runtime/sema_test.go +++ b/src/runtime/sema_test.go @@ -6,6 +6,7 @@ package runtime_test import ( . "runtime" + "sync" "sync/atomic" "testing" ) @@ -61,8 +62,11 @@ func testSemaHandoff() bool { // 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: @@ -74,7 +78,9 @@ func testSemaHandoff() bool { }() } + wg.Add(1) go func() { + defer wg.Done() Semacquire(&sema) atomic.CompareAndSwapUint32(&res, 0, 1) @@ -91,7 +97,7 @@ func testSemaHandoff() bool { 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? } |