]> Cypherpunks repositories - gostls13.git/commitdiff
context: convert goroutines to atomic type
authorcuiweixie <cuiweixie@gmail.com>
Sat, 27 Aug 2022 03:58:12 +0000 (11:58 +0800)
committerDamien Neil <dneil@google.com>
Mon, 29 Aug 2022 20:21:48 +0000 (20:21 +0000)
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>
src/context/context.go
src/context/context_test.go

index 1070111efa59384393944e629a411ffcec3765b4..7eace57893fc6dd2b5599f7364efd9f8b62d540e 100644 (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():
index 8673c0fdeaa82af7c78a27b27ae0df76a4ae8f50..099188090767d9a573d2fbeeeaaa522085dc9d7d 100644 (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)
                }