2012-02-17 04:50:37 +00:00
|
|
|
// run
|
2010-02-04 00:31:34 +00:00
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-23 07:47:26 +00:00
|
|
|
// Test basic operation of finalizers.
|
2010-02-04 00:31:34 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2010-03-26 21:15:30 +00:00
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
)
|
2010-02-04 00:31:34 +00:00
|
|
|
|
|
|
|
const N = 250
|
|
|
|
|
|
|
|
type A struct {
|
|
|
|
b *B
|
|
|
|
n int
|
|
|
|
}
|
|
|
|
|
|
|
|
type B struct {
|
|
|
|
n int
|
|
|
|
}
|
|
|
|
|
|
|
|
var i int
|
|
|
|
var nfinal int
|
|
|
|
var final [N]int
|
|
|
|
|
2010-03-26 21:15:30 +00:00
|
|
|
// the unused return is to test finalizers with return values
|
|
|
|
func finalA(a *A) (unused [N]int) {
|
2010-02-04 00:31:34 +00:00
|
|
|
if final[a.n] != 0 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("finalA", a.n, final[a.n])
|
|
|
|
panic("fail")
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
|
|
|
final[a.n] = 1
|
2010-03-26 21:15:30 +00:00
|
|
|
return
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func finalB(b *B) {
|
|
|
|
if final[b.n] != 1 {
|
2010-03-24 23:46:53 +00:00
|
|
|
println("finalB", b.n, final[b.n])
|
|
|
|
panic("fail")
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
|
|
|
final[b.n] = 2
|
|
|
|
nfinal++
|
|
|
|
}
|
|
|
|
|
2011-10-06 15:42:51 +00:00
|
|
|
func nofinalB(b *B) {
|
|
|
|
panic("nofinalB run")
|
|
|
|
}
|
|
|
|
|
2010-02-04 00:31:34 +00:00
|
|
|
func main() {
|
|
|
|
runtime.GOMAXPROCS(4)
|
|
|
|
for i = 0; i < N; i++ {
|
|
|
|
b := &B{i}
|
|
|
|
a := &A{b, i}
|
2011-10-06 15:42:51 +00:00
|
|
|
c := new(B)
|
|
|
|
runtime.SetFinalizer(c, nofinalB)
|
2010-02-04 00:31:34 +00:00
|
|
|
runtime.SetFinalizer(b, finalB)
|
|
|
|
runtime.SetFinalizer(a, finalA)
|
2011-10-06 15:42:51 +00:00
|
|
|
runtime.SetFinalizer(c, nil)
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
runtime.GC()
|
|
|
|
runtime.Gosched()
|
2010-03-30 17:34:57 +00:00
|
|
|
time.Sleep(1e6)
|
2011-10-06 15:42:51 +00:00
|
|
|
if nfinal >= N*8/10 {
|
|
|
|
break
|
|
|
|
}
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
2010-03-25 02:52:07 +00:00
|
|
|
if nfinal < N*8/10 {
|
2010-03-30 17:34:57 +00:00
|
|
|
println("not enough finalizing:", nfinal, "/", N)
|
|
|
|
panic("fail")
|
2010-02-04 00:31:34 +00:00
|
|
|
}
|
|
|
|
}
|