1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 type Tintptr
*int // assignable to *int
16 type Tint
int // *Tint implements Tinter, interface{}
20 type Tinter
interface {
24 func TestFinalizerType(t
*testing
.T
) {
25 if runtime
.GOARCH
!= "amd64" {
26 t
.Skipf("Skipping on non-amd64 machine")
29 ch
:= make(chan bool, 10)
30 finalize
:= func(x
*int) {
32 t
.Errorf("finalizer %d, want %d", *x
, 97531)
37 var finalizerTests
= []struct {
38 convert
func(*int) interface{}
41 {func(x
*int) interface{} { return x
}, func(v
*int) { finalize(v
) }},
42 {func(x
*int) interface{} { return Tintptr(x
) }, func(v Tintptr
) { finalize(v
) }},
43 {func(x
*int) interface{} { return Tintptr(x
) }, func(v
*int) { finalize(v
) }},
44 {func(x
*int) interface{} { return (*Tint
)(x
) }, func(v
*Tint
) { finalize((*int)(v
)) }},
45 {func(x
*int) interface{} { return (*Tint
)(x
) }, func(v Tinter
) { finalize((*int)(v
.(*Tint
))) }},
48 for _
, tt
:= range finalizerTests
{
52 runtime
.SetFinalizer(tt
.convert(v
), tt
.finalizer
)
55 time
.Sleep(1 * time
.Second
)
59 case <-time
.After(time
.Second
* 4):
60 t
.Errorf("finalizer for type %T didn't run", tt
.finalizer
)
65 type bigValue
struct {
71 func TestFinalizerInterfaceBig(t
*testing
.T
) {
72 if runtime
.GOARCH
!= "amd64" {
73 t
.Skipf("Skipping on non-amd64 machine")
77 v
:= &bigValue
{0xDEADBEEFDEADBEEF, true, "It matters not how strait the gate"}
79 runtime
.SetFinalizer(v
, func(v
interface{}) {
80 i
, ok
:= v
.(*bigValue
)
82 t
.Errorf("finalizer called with type %T, want *bigValue", v
)
85 t
.Errorf("finalizer called with %+v, want %+v", *i
, old
)
91 time
.Sleep(1 * time
.Second
)
95 case <-time
.After(4 * time
.Second
):
96 t
.Errorf("finalizer for type *bigValue didn't run")
103 func BenchmarkFinalizer(b
*testing
.B
) {
104 const CallsPerSched
= 1000
105 procs
:= runtime
.GOMAXPROCS(-1)
106 N
:= int32(b
.N
/ CallsPerSched
)
107 var wg sync
.WaitGroup
109 for p
:= 0; p
< procs
; p
++ {
111 var data
[CallsPerSched
]*int
112 for i
:= 0; i
< CallsPerSched
; i
++ {
115 for atomic
.AddInt32(&N
, -1) >= 0 {
117 for i
:= 0; i
< CallsPerSched
; i
++ {
118 runtime
.SetFinalizer(data
[i
], fin
)
120 for i
:= 0; i
< CallsPerSched
; i
++ {
121 runtime
.SetFinalizer(data
[i
], nil)
130 func BenchmarkFinalizerRun(b
*testing
.B
) {
131 const CallsPerSched
= 1000
132 procs
:= runtime
.GOMAXPROCS(-1)
133 N
:= int32(b
.N
/ CallsPerSched
)
134 var wg sync
.WaitGroup
136 for p
:= 0; p
< procs
; p
++ {
138 for atomic
.AddInt32(&N
, -1) >= 0 {
140 for i
:= 0; i
< CallsPerSched
; i
++ {
142 runtime
.SetFinalizer(v
, fin
)