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.
14 func TestCondSignal(t
*testing
.T
) {
18 running
:= make(chan bool, n
)
19 awake
:= make(chan bool, n
)
20 for i
:= 0; i
< n
; i
++ {
29 for i
:= 0; i
< n
; i
++ {
30 <-running
// Wait for everyone to run.
35 t
.Fatal("goroutine not asleep")
41 <-awake
// Will deadlock if no goroutine wakes up
44 t
.Fatal("too many goroutines awake")
52 func TestCondSignalGenerations(t
*testing
.T
) {
56 running
:= make(chan bool, n
)
57 awake
:= make(chan int, n
)
58 for i
:= 0; i
< n
; i
++ {
69 t
.Fatalf("wrong goroutine woke up: want %d, got %d", i
-1, a
)
79 func TestCondBroadcast(t
*testing
.T
) {
83 running
:= make(chan int, n
)
84 awake
:= make(chan int, n
)
86 for i
:= 0; i
< n
; i
++ {
97 for i
:= 0; i
< n
; i
++ {
98 for i
:= 0; i
< n
; i
++ {
99 <-running
// Will deadlock unless n are running.
108 t
.Fatal("goroutine not asleep")
114 seen
:= make([]bool, n
)
115 for i
:= 0; i
< n
; i
++ {
118 t
.Fatal("goroutine woke up twice")
125 t
.Fatal("goroutine did not exit")
131 func TestRace(t
*testing
.T
) {
133 c
:= NewCond(&Mutex
{})
134 done
:= make(chan bool)
187 func TestCondSignalStealing(t
*testing
.T
) {
188 for iters
:= 0; iters
< 1000; iters
++ {
193 ch
:= make(chan struct{})
207 // We know that the waiter is in the cond.Wait() call because we
208 // synchronized with it, then acquired/released the mutex it was
209 // holding when we synchronized.
211 // Start two goroutines that will race: one will broadcast on
212 // the cond var, the other will wait on it.
214 // The new waiter may or may not get notified, but the first one
215 // has to be notified.
229 // Check that the first waiter does get signaled.
232 case <-time
.After(2 * time
.Second
):
233 t
.Fatalf("First waiter didn't get broadcast.")
236 // Release the second waiter in case it didn't get the
245 func TestCondCopy(t
*testing
.T
) {
248 if err
== nil || err
.(string) != "sync.Cond is copied" {
249 t
.Fatalf("got %v, expect sync.Cond is copied", err
)
252 c
:= Cond
{L
: &Mutex
{}}
258 func BenchmarkCond1(b
*testing
.B
) {
262 func BenchmarkCond2(b
*testing
.B
) {
266 func BenchmarkCond4(b
*testing
.B
) {
270 func BenchmarkCond8(b
*testing
.B
) {
274 func BenchmarkCond16(b
*testing
.B
) {
278 func BenchmarkCond32(b
*testing
.B
) {
282 func benchmarkCond(b
*testing
.B
, waiters
int) {
283 c
:= NewCond(&Mutex
{})
284 done
:= make(chan bool)
287 for routine
:= 0; routine
< waiters
+1; routine
++ {
289 for i
:= 0; i
< b
.N
; i
++ {
311 for routine
:= 0; routine
< waiters
+1; routine
++ {