Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 1 | // $G $D/$F.go && $L $F.$A && ./$A.out |
| 2 | |
| 3 | // Copyright 2009 The Go Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | // trivial finalizer test |
| 8 | |
| 9 | package main |
| 10 | |
| 11 | import "runtime" |
| 12 | |
| 13 | const N = 250 |
| 14 | |
| 15 | type A struct { |
| 16 | b *B |
| 17 | n int |
| 18 | } |
| 19 | |
| 20 | type B struct { |
| 21 | n int |
| 22 | } |
| 23 | |
| 24 | var i int |
| 25 | var nfinal int |
| 26 | var final [N]int |
| 27 | |
| 28 | func finalA(a *A) { |
| 29 | if final[a.n] != 0 { |
| 30 | panicln("finalA", a.n, final[a.n]) |
| 31 | } |
| 32 | final[a.n] = 1 |
| 33 | } |
| 34 | |
| 35 | func finalB(b *B) { |
| 36 | if final[b.n] != 1 { |
| 37 | panicln("finalB", b.n, final[b.n]) |
| 38 | } |
| 39 | final[b.n] = 2 |
| 40 | nfinal++ |
| 41 | } |
| 42 | |
| 43 | func main() { |
| 44 | runtime.GOMAXPROCS(4) |
| 45 | for i = 0; i < N; i++ { |
| 46 | b := &B{i} |
| 47 | a := &A{b, i} |
| 48 | runtime.SetFinalizer(b, finalB) |
| 49 | runtime.SetFinalizer(a, finalA) |
| 50 | } |
| 51 | for i := 0; i < N; i++ { |
| 52 | runtime.GC() |
| 53 | runtime.Gosched() |
| 54 | } |
| 55 | if nfinal < N*9/10 { |
| 56 | panic("not enough finalizing:", nfinal, "/", N) |
| 57 | } |
| 58 | } |