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.
13 func TestCondSignal(t
*testing
.T
) {
17 running
:= make(chan bool, n
)
18 awake
:= make(chan bool, n
)
19 for i
:= 0; i
< n
; i
++ {
28 for i
:= 0; i
< n
; i
++ {
29 <-running
// Wait for everyone to run.
34 t
.Fatal("goroutine not asleep")
40 <-awake
// Will deadlock if no goroutine wakes up
43 t
.Fatal("too many goroutines awake")
51 func TestCondSignalGenerations(t
*testing
.T
) {
55 running
:= make(chan bool, n
)
56 awake
:= make(chan int, n
)
57 for i
:= 0; i
< n
; i
++ {
68 t
.Fatalf("wrong goroutine woke up: want %d, got %d", i
-1, a
)
78 func TestCondBroadcast(t
*testing
.T
) {
82 running
:= make(chan int, n
)
83 awake
:= make(chan int, n
)
85 for i
:= 0; i
< n
; i
++ {
96 for i
:= 0; i
< n
; i
++ {
97 for i
:= 0; i
< n
; i
++ {
98 <-running
// Will deadlock unless n are running.
107 t
.Fatal("goroutine not asleep")
113 seen
:= make([]bool, n
)
114 for i
:= 0; i
< n
; i
++ {
117 t
.Fatal("goroutine woke up twice")
124 t
.Fatal("goroutine did not exit")
130 func TestRace(t
*testing
.T
) {
132 c
:= NewCond(&Mutex
{})
133 done
:= make(chan bool)
186 func TestCondCopy(t
*testing
.T
) {
189 if err
== nil || err
.(string) != "sync.Cond is copied" {
190 t
.Fatalf("got %v, expect sync.Cond is copied", err
)
193 c
:= Cond
{L
: &Mutex
{}}
199 func BenchmarkCond1(b
*testing
.B
) {
203 func BenchmarkCond2(b
*testing
.B
) {
207 func BenchmarkCond4(b
*testing
.B
) {
211 func BenchmarkCond8(b
*testing
.B
) {
215 func BenchmarkCond16(b
*testing
.B
) {
219 func BenchmarkCond32(b
*testing
.B
) {
223 func benchmarkCond(b
*testing
.B
, waiters
int) {
224 c
:= NewCond(&Mutex
{})
225 done
:= make(chan bool)
228 for routine
:= 0; routine
< waiters
+1; routine
++ {
230 for i
:= 0; i
< b
.N
; i
++ {
252 for routine
:= 0; routine
< waiters
+1; routine
++ {