]> Cypherpunks repositories - gostls13.git/commitdiff
sync: use runtime.AddCleanup instead of runtime.SetFinalizer
authorCarlos Amedee <carlos@golang.org>
Mon, 6 Jan 2025 17:59:07 +0000 (12:59 -0500)
committerCarlos Amedee <carlos@golang.org>
Fri, 14 Feb 2025 15:29:34 +0000 (07:29 -0800)
This changes the use of finalizers to the cleanup implementation in
tests.

Updates #70907

Change-Id: I7d7289999a83fa53f538698f34294f7d9651c921
Reviewed-on: https://go-review.googlesource.com/c/go/+/640735
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/sync/map_test.go
src/sync/oncefunc_test.go
src/sync/pool_test.go

index f12c43a28fc95f075d556730f316315e67ec6030..05c81354c87b67edef0a3661f26466727277cd7c 100644 (file)
@@ -225,15 +225,13 @@ func TestIssue40999(t *testing.T) {
        // add an initial entry to bias len(m.dirty) above the miss count.
        m.Store(nil, struct{}{})
 
-       var finalized uint32
+       var cleanedUp uint32
 
-       // Set finalizers that count for collected keys. A non-zero count
+       // Add cleanups that count for collected keys. A non-zero count
        // indicates that keys have not been leaked.
-       for atomic.LoadUint32(&finalized) == 0 {
+       for atomic.LoadUint32(&cleanedUp) == 0 {
                p := new(int)
-               runtime.SetFinalizer(p, func(*int) {
-                       atomic.AddUint32(&finalized, 1)
-               })
+               runtime.AddCleanup(p, func(c *uint32) { atomic.AddUint32(c, 1) }, &cleanedUp)
                m.Store(p, struct{}{})
                m.Delete(p)
                runtime.GC()
index 5f0d5640631dd0fdc2122c90f74ee1291d24587c..daf094571f054b2a03b5a865da5a39fdb9d2a89f 100644 (file)
@@ -203,9 +203,7 @@ func TestOnceXGC(t *testing.T) {
                t.Run(n, func(t *testing.T) {
                        buf := make([]byte, 1024)
                        var gc atomic.Bool
-                       runtime.SetFinalizer(&buf[0], func(_ *byte) {
-                               gc.Store(true)
-                       })
+                       runtime.AddCleanup(&buf[0], func(g *atomic.Bool) { g.Store(true) }, &gc)
                        f := fn(buf)
                        gcwaitfin()
                        if gc.Load() != false {
index b6ee983c2989b2190cbc1d829d1a17cb41b0c960..286dcacf3ecc84366d0b780e8c85a25d0506c7ef 100644 (file)
@@ -109,12 +109,10 @@ loop:
                if try == 1 && testing.Short() {
                        break
                }
-               var fin, fin1 uint32
+               var cln, cln1 uint32
                for i := 0; i < N; i++ {
                        v := new(string)
-                       runtime.SetFinalizer(v, func(vv *string) {
-                               atomic.AddUint32(&fin, 1)
-                       })
+                       runtime.AddCleanup(v, func(f *uint32) { atomic.AddUint32(f, 1) }, &cln)
                        p.Put(v)
                }
                if drain {
@@ -126,11 +124,11 @@ loop:
                        runtime.GC()
                        time.Sleep(time.Duration(i*100+10) * time.Millisecond)
                        // 1 pointer can remain on stack or elsewhere
-                       if fin1 = atomic.LoadUint32(&fin); fin1 >= N-1 {
+                       if cln1 = atomic.LoadUint32(&cln); cln1 >= N-1 {
                                continue loop
                        }
                }
-               t.Fatalf("only %v out of %v resources are finalized on try %v", fin1, N, try)
+               t.Fatalf("only %v out of %v resources are cleaned up on try %v", cln1, N, try)
        }
 }