OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [sync/] [cond_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
package sync_test
5
 
6
import (
7
        . "sync"
8
        "testing"
9
)
10
 
11
func TestCondSignal(t *testing.T) {
12
        var m Mutex
13
        c := NewCond(&m)
14
        n := 2
15
        running := make(chan bool, n)
16
        awake := make(chan bool, n)
17
        for i := 0; i < n; i++ {
18
                go func() {
19
                        m.Lock()
20
                        running <- true
21
                        c.Wait()
22
                        awake <- true
23
                        m.Unlock()
24
                }()
25
        }
26
        for i := 0; i < n; i++ {
27
                <-running // Wait for everyone to run.
28
        }
29
        for n > 0 {
30
                select {
31
                case <-awake:
32
                        t.Fatal("goroutine not asleep")
33
                default:
34
                }
35
                m.Lock()
36
                c.Signal()
37
                m.Unlock()
38
                <-awake // Will deadlock if no goroutine wakes up
39
                select {
40
                case <-awake:
41
                        t.Fatal("too many goroutines awake")
42
                default:
43
                }
44
                n--
45
        }
46
        c.Signal()
47
}
48
 
49
func TestCondSignalGenerations(t *testing.T) {
50
        var m Mutex
51
        c := NewCond(&m)
52
        n := 100
53
        running := make(chan bool, n)
54
        awake := make(chan int, n)
55
        for i := 0; i < n; i++ {
56
                go func(i int) {
57
                        m.Lock()
58
                        running <- true
59
                        c.Wait()
60
                        awake <- i
61
                        m.Unlock()
62
                }(i)
63
                if i > 0 {
64
                        a := <-awake
65
                        if a != i-1 {
66
                                t.Fatalf("wrong goroutine woke up: want %d, got %d", i-1, a)
67
                        }
68
                }
69
                <-running
70
                m.Lock()
71
                c.Signal()
72
                m.Unlock()
73
        }
74
}
75
 
76
func TestCondBroadcast(t *testing.T) {
77
        var m Mutex
78
        c := NewCond(&m)
79
        n := 200
80
        running := make(chan int, n)
81
        awake := make(chan int, n)
82
        exit := false
83
        for i := 0; i < n; i++ {
84
                go func(g int) {
85
                        m.Lock()
86
                        for !exit {
87
                                running <- g
88
                                c.Wait()
89
                                awake <- g
90
                        }
91
                        m.Unlock()
92
                }(i)
93
        }
94
        for i := 0; i < n; i++ {
95
                for i := 0; i < n; i++ {
96
                        <-running // Will deadlock unless n are running.
97
                }
98
                if i == n-1 {
99
                        m.Lock()
100
                        exit = true
101
                        m.Unlock()
102
                }
103
                select {
104
                case <-awake:
105
                        t.Fatal("goroutine not asleep")
106
                default:
107
                }
108
                m.Lock()
109
                c.Broadcast()
110
                m.Unlock()
111
                seen := make([]bool, n)
112
                for i := 0; i < n; i++ {
113
                        g := <-awake
114
                        if seen[g] {
115
                                t.Fatal("goroutine woke up twice")
116
                        }
117
                        seen[g] = true
118
                }
119
        }
120
        select {
121
        case <-running:
122
                t.Fatal("goroutine did not exit")
123
        default:
124
        }
125
        c.Broadcast()
126
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.