]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: improve synchronization in TestFinalizerRegisterABI
authorAustin Clements <austin@google.com>
Thu, 15 Apr 2021 21:23:16 +0000 (17:23 -0400)
committerAustin Clements <austin@google.com>
Fri, 16 Apr 2021 00:01:58 +0000 (00:01 +0000)
Replace busy-wait with a channel.

Change-Id: I51ddfd5dbde15fff56c62c618570e66930902cbb
Reviewed-on: https://go-review.googlesource.com/c/go/+/310630
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/runtime/abi_test.go

index b82acfe32594f0497f93a89ddca97d07f64ee7e4..f69d3a9d5012d6c3ce79968533348f2cabe05ddc 100644 (file)
@@ -17,36 +17,21 @@ import (
        "os/exec"
        "runtime"
        "strings"
-       "sync"
        "testing"
        "time"
 )
 
-var regConfirmRunIface int
-var regConfirmRunPtr int
-var regConfirmMU sync.Mutex
-
-func guardedRead(p *int) int {
-       regConfirmMU.Lock()
-       defer regConfirmMU.Unlock()
-       return *p
-}
-
-func guardedWrite(p *int, v int) {
-       regConfirmMU.Lock()
-       defer regConfirmMU.Unlock()
-       *p = v
-}
+var regConfirmRun chan int
 
 //go:registerparams
 func regFinalizerPointer(v *Tint) (int, float32, [10]byte) {
-       guardedWrite(&regConfirmRunPtr, *(*int)(v))
+       regConfirmRun <- *(*int)(v)
        return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 }
 
 //go:registerparams
 func regFinalizerIface(v Tinter) (int, float32, [10]byte) {
-       guardedWrite(&regConfirmRunIface, *(*int)(v.(*Tint)))
+       regConfirmRun <- *(*int)(v.(*Tint))
        return 5151, 4.0, [10]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 }
 
@@ -96,15 +81,14 @@ func TestFinalizerRegisterABI(t *testing.T) {
                name         string
                fin          interface{}
                confirmValue int
-               confirmRun   *int
        }{
-               {"Pointer", regFinalizerPointer, -1, &regConfirmRunPtr},
-               {"Interface", regFinalizerIface, -2, &regConfirmRunIface},
+               {"Pointer", regFinalizerPointer, -1},
+               {"Interface", regFinalizerIface, -2},
        }
        for i := range tests {
                test := &tests[i]
                t.Run(test.name, func(t *testing.T) {
-                       guardedWrite(test.confirmRun, 0)
+                       regConfirmRun = make(chan int)
 
                        x := new(Tint)
                        *x = (Tint)(test.confirmValue)
@@ -116,17 +100,13 @@ func TestFinalizerRegisterABI(t *testing.T) {
                        runtime.GC()
                        runtime.GC()
 
-                       for i := 0; i < 100; i++ {
-                               time.Sleep(10 * time.Millisecond)
-                               if guardedRead(test.confirmRun) != 0 {
-                                       break
-                               }
-                       }
-                       v := guardedRead(test.confirmRun)
-                       if v == 0 {
+                       select {
+                       case <-time.After(time.Second):
                                t.Fatal("finalizer failed to execute")
-                       } else if v != test.confirmValue {
-                               t.Fatalf("wrong finalizer executed? regConfirmRun = %d", v)
+                       case gotVal := <-regConfirmRun:
+                               if gotVal != test.confirmValue {
+                                       t.Fatalf("wrong finalizer executed? got %d, want %d", gotVal, test.confirmValue)
+                               }
                        }
                })
        }