From 2113fefe7d4ac5634a5b0597ef1b465684c73076 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Sat, 27 Aug 2022 11:58:12 +0800 Subject: [PATCH] context: convert goroutines to atomic type Change-Id: I021fbc9786a3e3f858770fe3e109a0de487390d8 Reviewed-on: https://go-review.googlesource.com/c/go/+/426089 TryBot-Result: Gopher Robot Reviewed-by: Heschi Kreinick Run-TryBot: xie cui <523516579@qq.com> Reviewed-by: Damien Neil --- src/context/context.go | 4 ++-- src/context/context_test.go | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/context/context.go b/src/context/context.go index 1070111efa5..7eace57893f 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -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(): diff --git a/src/context/context_test.go b/src/context/context_test.go index 8673c0fdeaa..09918809076 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -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) }