Implement -freuse-stack= option
[official-gcc.git] / libgo / go / sync / once_test.go
blob37075af171b5d14129b82ec14890e39cc875d99d
1 // Copyright 2009 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.
5 package sync_test
7 import (
8 "runtime"
9 . "sync"
10 "sync/atomic"
11 "testing"
14 type one int
16 func (o *one) Increment() {
17 *o++
20 func run(once *Once, o *one, c chan bool) {
21 once.Do(func() { o.Increment() })
22 c <- true
25 func TestOnce(t *testing.T) {
26 o := new(one)
27 once := new(Once)
28 c := make(chan bool)
29 const N = 10
30 for i := 0; i < N; i++ {
31 go run(once, o, c)
33 for i := 0; i < N; i++ {
34 <-c
36 if *o != 1 {
37 t.Errorf("once failed: %d is not 1", *o)
41 func BenchmarkOnce(b *testing.B) {
42 const CallsPerSched = 1000
43 procs := runtime.GOMAXPROCS(-1)
44 N := int32(b.N / CallsPerSched)
45 var once Once
46 f := func() {}
47 c := make(chan bool, procs)
48 for p := 0; p < procs; p++ {
49 go func() {
50 for atomic.AddInt32(&N, -1) >= 0 {
51 runtime.Gosched()
52 for g := 0; g < CallsPerSched; g++ {
53 once.Do(f)
56 c <- true
57 }()
59 for p := 0; p < procs; p++ {
60 <-c