1
0
mirror of https://github.com/golang/go synced 2024-11-26 17:07:09 -07:00

runtime: improve synchronization in TestFinalizerRegisterABI

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>
This commit is contained in:
Austin Clements 2021-04-15 17:23:16 -04:00
parent c8fb0ec5a0
commit bdddfd10ec

View File

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