1
0
mirror of https://github.com/golang/go synced 2024-11-17 01:04:50 -07:00

context: convert goroutines to atomic type

Change-Id: I021fbc9786a3e3f858770fe3e109a0de487390d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/426089
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: xie cui <523516579@qq.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
cuiweixie 2022-08-27 11:58:12 +08:00 committed by Damien Neil
parent fd2ac5ef96
commit 2113fefe7d
2 changed files with 5 additions and 6 deletions

View File

@ -244,7 +244,7 @@ func newCancelCtx(parent Context) cancelCtx {
}
// goroutines counts the number of goroutines ever created; for testing.
var goroutines int32
var goroutines atomic.Int32
// propagateCancel arranges for child to be canceled when parent is.
func propagateCancel(parent Context, child canceler) {
@ -274,7 +274,7 @@ func propagateCancel(parent Context, child canceler) {
}
p.mu.Unlock()
} else {
atomic.AddInt32(&goroutines, +1)
goroutines.Add(1)
go func() {
select {
case <-parent.Done():

View File

@ -10,7 +10,6 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"time"
)
@ -723,17 +722,17 @@ func (d *myDoneCtx) Done() <-chan struct{} {
}
func XTestCustomContextGoroutines(t testingT) {
g := atomic.LoadInt32(&goroutines)
g := goroutines.Load()
checkNoGoroutine := func() {
t.Helper()
now := atomic.LoadInt32(&goroutines)
now := goroutines.Load()
if now != g {
t.Fatalf("%d goroutines created", now-g)
}
}
checkCreatedGoroutine := func() {
t.Helper()
now := atomic.LoadInt32(&goroutines)
now := goroutines.Load()
if now != g+1 {
t.Fatalf("%d goroutines created, want 1", now-g)
}