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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [ken/] [chan.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $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
package main
8
 
9
import "os"
10
import "runtime"
11
import "sync"
12
 
13
var randx int
14
 
15
func nrand(n int) int {
16
        randx += 10007
17
        if randx >= 1000000 {
18
                randx -= 1000000
19
        }
20
        return randx % n
21
}
22
 
23
type Chan struct {
24
        sc, rc chan int // send and recv chan
25
        sv, rv int      // send and recv seq
26
}
27
 
28
var (
29
        nproc      int
30
        nprocLock  sync.Mutex
31
        cval       int
32
        end        int = 10000
33
        totr, tots int
34
        totLock    sync.Mutex
35
        nc         *Chan
36
)
37
 
38
func init() {
39
        nc = new(Chan)
40
}
41
 
42
func changeNproc(adjust int) int {
43
        nprocLock.Lock()
44
        nproc += adjust
45
        ret := nproc
46
        nprocLock.Unlock()
47
        return ret
48
}
49
 
50
func mkchan(c, n int) []*Chan {
51
        ca := make([]*Chan, n)
52
        for i := 0; i < n; i++ {
53
                cval = cval + 100
54
                ch := new(Chan)
55
                ch.sc = make(chan int, c)
56
                ch.rc = ch.sc
57
                ch.sv = cval
58
                ch.rv = cval
59
                ca[i] = ch
60
        }
61
        return ca
62
}
63
 
64
func expect(v, v0 int) (newv int) {
65
        if v == v0 {
66
                if v%100 == 75 {
67
                        return end
68
                }
69
                return v + 1
70
        }
71
        print("got ", v, " expected ", v0+1, "\n")
72
        panic("fail")
73
}
74
 
75
func (c *Chan) send() bool {
76
        //      print("send ", c.sv, "\n");
77
        totLock.Lock()
78
        tots++
79
        totLock.Unlock()
80
        c.sv = expect(c.sv, c.sv)
81
        if c.sv == end {
82
                c.sc = nil
83
                return true
84
        }
85
        return false
86
}
87
 
88
func send(c *Chan) {
89
        for {
90
                for r := nrand(10); r >= 0; r-- {
91
                        runtime.Gosched()
92
                }
93
                c.sc <- c.sv
94
                if c.send() {
95
                        break
96
                }
97
        }
98
        changeNproc(-1)
99
}
100
 
101
func (c *Chan) recv(v int) bool {
102
        //      print("recv ", v, "\n");
103
        totLock.Lock()
104
        totr++
105
        totLock.Unlock()
106
        c.rv = expect(c.rv, v)
107
        if c.rv == end {
108
                c.rc = nil
109
                return true
110
        }
111
        return false
112
}
113
 
114
func recv(c *Chan) {
115
        var v int
116
 
117
        for {
118
                for r := nrand(10); r >= 0; r-- {
119
                        runtime.Gosched()
120
                }
121
                v = <-c.rc
122
                if c.recv(v) {
123
                        break
124
                }
125
        }
126
        changeNproc(-1)
127
}
128
 
129
func sel(r0, r1, r2, r3, s0, s1, s2, s3 *Chan) {
130
        var v int
131
 
132
        a := 0 // local chans running
133
 
134
        if r0.rc != nil {
135
                a++
136
        }
137
        if r1.rc != nil {
138
                a++
139
        }
140
        if r2.rc != nil {
141
                a++
142
        }
143
        if r3.rc != nil {
144
                a++
145
        }
146
        if s0.sc != nil {
147
                a++
148
        }
149
        if s1.sc != nil {
150
                a++
151
        }
152
        if s2.sc != nil {
153
                a++
154
        }
155
        if s3.sc != nil {
156
                a++
157
        }
158
 
159
        for {
160
                for r := nrand(5); r >= 0; r-- {
161
                        runtime.Gosched()
162
                }
163
 
164
                select {
165
                case v = <-r0.rc:
166
                        if r0.recv(v) {
167
                                a--
168
                        }
169
                case v = <-r1.rc:
170
                        if r1.recv(v) {
171
                                a--
172
                        }
173
                case v = <-r2.rc:
174
                        if r2.recv(v) {
175
                                a--
176
                        }
177
                case v = <-r3.rc:
178
                        if r3.recv(v) {
179
                                a--
180
                        }
181
                case s0.sc <- s0.sv:
182
                        if s0.send() {
183
                                a--
184
                        }
185
                case s1.sc <- s1.sv:
186
                        if s1.send() {
187
                                a--
188
                        }
189
                case s2.sc <- s2.sv:
190
                        if s2.send() {
191
                                a--
192
                        }
193
                case s3.sc <- s3.sv:
194
                        if s3.send() {
195
                                a--
196
                        }
197
                }
198
                if a == 0 {
199
                        break
200
                }
201
        }
202
        changeNproc(-1)
203
}
204
 
205
// direct send to direct recv
206
func test1(c *Chan) {
207
        changeNproc(2)
208
        go send(c)
209
        go recv(c)
210
}
211
 
212
// direct send to select recv
213
func test2(c int) {
214
        ca := mkchan(c, 4)
215
 
216
        changeNproc(4)
217
        go send(ca[0])
218
        go send(ca[1])
219
        go send(ca[2])
220
        go send(ca[3])
221
 
222
        changeNproc(1)
223
        go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc)
224
}
225
 
226
// select send to direct recv
227
func test3(c int) {
228
        ca := mkchan(c, 4)
229
 
230
        changeNproc(4)
231
        go recv(ca[0])
232
        go recv(ca[1])
233
        go recv(ca[2])
234
        go recv(ca[3])
235
 
236
        changeNproc(1)
237
        go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3])
238
}
239
 
240
// select send to select recv
241
func test4(c int) {
242
        ca := mkchan(c, 4)
243
 
244
        changeNproc(2)
245
        go sel(nc, nc, nc, nc, ca[0], ca[1], ca[2], ca[3])
246
        go sel(ca[0], ca[1], ca[2], ca[3], nc, nc, nc, nc)
247
}
248
 
249
func test5(c int) {
250
        ca := mkchan(c, 8)
251
 
252
        changeNproc(2)
253
        go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3])
254
        go sel(ca[0], ca[1], ca[2], ca[3], ca[4], ca[5], ca[6], ca[7])
255
}
256
 
257
func test6(c int) {
258
        ca := mkchan(c, 12)
259
 
260
        changeNproc(4)
261
        go send(ca[4])
262
        go send(ca[5])
263
        go send(ca[6])
264
        go send(ca[7])
265
 
266
        changeNproc(4)
267
        go recv(ca[8])
268
        go recv(ca[9])
269
        go recv(ca[10])
270
        go recv(ca[11])
271
 
272
        changeNproc(2)
273
        go sel(ca[4], ca[5], ca[6], ca[7], ca[0], ca[1], ca[2], ca[3])
274
        go sel(ca[0], ca[1], ca[2], ca[3], ca[8], ca[9], ca[10], ca[11])
275
}
276
 
277
// wait for outstanding tests to finish
278
func wait() {
279
        runtime.Gosched()
280
        for changeNproc(0) != 0 {
281
                runtime.Gosched()
282
        }
283
}
284
 
285
// run all tests with specified buffer size
286
func tests(c int) {
287
        ca := mkchan(c, 4)
288
        test1(ca[0])
289
        test1(ca[1])
290
        test1(ca[2])
291
        test1(ca[3])
292
        wait()
293
 
294
        test2(c)
295
        wait()
296
 
297
        test3(c)
298
        wait()
299
 
300
        test4(c)
301
        wait()
302
 
303
        test5(c)
304
        wait()
305
 
306
        test6(c)
307
        wait()
308
}
309
 
310
// run all test with 4 buffser sizes
311
func main() {
312
 
313
        tests(0)
314
        tests(1)
315
        tests(10)
316
        tests(100)
317
 
318
        t := 4 * // buffer sizes
319
                (4*4 + // tests 1,2,3,4 channels
320
                        8 + // test 5 channels
321
                        12) * // test 6 channels
322
                76 // sends/recvs on a channel
323
 
324
        if tots != t || totr != t {
325
                print("tots=", tots, " totr=", totr, " sb=", t, "\n")
326
                os.Exit(1)
327
        }
328
        os.Exit(0)
329
}

powered by: WebSVN 2.1.0

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