2008-09-19 21:52:59 -06:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
import "os"
|
|
|
|
import "runtime"
|
2008-12-18 21:06:28 -07:00
|
|
|
|
|
|
|
var randx int;
|
|
|
|
|
|
|
|
func
|
|
|
|
nrand(n int) int
|
|
|
|
{
|
|
|
|
randx += 10007;
|
|
|
|
if randx >= 1000000 {
|
|
|
|
randx -= 1000000;
|
|
|
|
}
|
|
|
|
return randx%n;
|
|
|
|
}
|
2008-09-19 21:52:59 -06:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
type Chan
|
2008-09-20 20:56:40 -06:00
|
|
|
struct
|
|
|
|
{
|
2008-12-19 04:05:37 -07:00
|
|
|
sc,rc chan int; // send and recv chan
|
2008-09-20 20:56:40 -06:00
|
|
|
sv,rv int; // send and recv seq
|
|
|
|
}
|
|
|
|
|
2008-09-19 21:52:59 -06:00
|
|
|
var
|
|
|
|
(
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc int;
|
|
|
|
cval int;
|
2009-01-16 17:12:14 -07:00
|
|
|
end int = 10000;
|
2008-09-20 20:56:40 -06:00
|
|
|
totr,tots int;
|
|
|
|
nc *Chan;
|
2008-09-19 21:52:59 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
init()
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2009-01-06 16:19:02 -07:00
|
|
|
nc = new(Chan);
|
2008-09-20 20:56:40 -06:00
|
|
|
}
|
2008-09-19 21:52:59 -06:00
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
func
|
2008-12-18 21:06:28 -07:00
|
|
|
mkchan(c,n int) []*Chan
|
2008-09-20 20:56:40 -06:00
|
|
|
{
|
2009-01-06 16:19:02 -07:00
|
|
|
ca := make([]*Chan, n);
|
2008-09-20 20:56:40 -06:00
|
|
|
for i:=0; i<n; i++ {
|
|
|
|
cval = cval+100;
|
2009-01-06 16:19:02 -07:00
|
|
|
ch := new(Chan);
|
|
|
|
ch.sc = make(chan int, c);
|
2008-09-20 20:56:40 -06:00
|
|
|
ch.rc = ch.sc;
|
|
|
|
ch.sv = cval;
|
|
|
|
ch.rv = cval;
|
|
|
|
ca[i] = ch;
|
|
|
|
}
|
|
|
|
return ca;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func
|
|
|
|
expect(v, v0 int) (newv int)
|
|
|
|
{
|
|
|
|
if v == v0 {
|
|
|
|
if v%100 == 75 {
|
2009-01-16 17:12:14 -07:00
|
|
|
return end;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
return v+1;
|
|
|
|
}
|
|
|
|
panic("got ", v, " expected ", v0+1, "\n");
|
|
|
|
}
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
func (c *Chan)
|
|
|
|
send() bool
|
|
|
|
{
|
|
|
|
// print("send ", c.sv, "\n");
|
|
|
|
tots++;
|
|
|
|
c.sv = expect(c.sv, c.sv);
|
2009-01-16 17:12:14 -07:00
|
|
|
if c.sv == end {
|
2008-10-07 13:31:31 -06:00
|
|
|
c.sc = nil;
|
2008-09-20 20:56:40 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-19 21:52:59 -06:00
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
send(c *Chan)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc++; // total goroutines running
|
2008-09-19 21:52:59 -06:00
|
|
|
for {
|
2008-12-18 21:06:28 -07:00
|
|
|
for r:=nrand(10); r>=0; r-- {
|
2009-05-08 16:21:41 -06:00
|
|
|
runtime.Gosched();
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
c.sc <- c.sv;
|
|
|
|
if c.send() {
|
2008-09-19 21:52:59 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc--;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
func (c *Chan)
|
|
|
|
recv(v int) bool
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
// print("recv ", v, "\n");
|
|
|
|
totr++;
|
|
|
|
c.rv = expect(c.rv, v);
|
2009-01-16 17:12:14 -07:00
|
|
|
if c.rv == end {
|
2008-09-20 20:56:40 -06:00
|
|
|
c.rc = nil;
|
|
|
|
return true;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
return false;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
recv(c *Chan)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
|
|
|
var v int;
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc++; // total goroutines running
|
|
|
|
for {
|
2008-12-18 21:06:28 -07:00
|
|
|
for r:=nrand(10); r>=0; r-- {
|
2009-05-08 16:21:41 -06:00
|
|
|
runtime.Gosched();
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
v = <-c.rc;
|
|
|
|
if c.recv(v) {
|
2008-09-19 21:52:59 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc--;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
sel(r0,r1,r2,r3, s0,s1,s2,s3 *Chan)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
|
|
|
var v int;
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc++; // total goroutines running
|
|
|
|
a := 0; // local chans running
|
|
|
|
|
|
|
|
if r0.rc != nil { a++ }
|
|
|
|
if r1.rc != nil { a++ }
|
|
|
|
if r2.rc != nil { a++ }
|
|
|
|
if r3.rc != nil { a++ }
|
|
|
|
if s0.sc != nil { a++ }
|
|
|
|
if s1.sc != nil { a++ }
|
|
|
|
if s2.sc != nil { a++ }
|
|
|
|
if s3.sc != nil { a++ }
|
2008-09-19 21:52:59 -06:00
|
|
|
|
|
|
|
for {
|
2008-12-18 21:06:28 -07:00
|
|
|
for r:=nrand(5); r>=0; r-- {
|
2009-05-08 16:21:41 -06:00
|
|
|
runtime.Gosched();
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2008-09-20 20:56:40 -06:00
|
|
|
case v = <-r0.rc:
|
|
|
|
if r0.recv(v) {
|
2008-09-19 21:52:59 -06:00
|
|
|
a--;
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
case v = <-r1.rc:
|
|
|
|
if r1.recv(v) {
|
2008-09-19 21:52:59 -06:00
|
|
|
a--;
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
case v = <-r2.rc:
|
|
|
|
if r2.recv(v) {
|
2008-09-19 21:52:59 -06:00
|
|
|
a--;
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
case v = <-r3.rc:
|
|
|
|
if r3.recv(v) {
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
case s0.sc <- s0.sv:
|
|
|
|
if s0.send() {
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
case s1.sc <- s1.sv:
|
|
|
|
if s1.send() {
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
case s2.sc <- s2.sv:
|
|
|
|
if s2.send() {
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
case s3.sc <- s3.sv:
|
|
|
|
if s3.send() {
|
2008-09-19 21:52:59 -06:00
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-09-20 20:56:40 -06:00
|
|
|
nproc--;
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// direct send to direct recv
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
test1(c *Chan)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
go send(c);
|
|
|
|
go recv(c);
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// direct send to select recv
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
test2(c int)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
ca := mkchan(c,4);
|
|
|
|
|
|
|
|
go send(ca[0]);
|
|
|
|
go send(ca[1]);
|
|
|
|
go send(ca[2]);
|
|
|
|
go send(ca[3]);
|
|
|
|
|
|
|
|
go sel(ca[0],ca[1],ca[2],ca[3], nc,nc,nc,nc);
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// select send to direct recv
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
test3(c int)
|
|
|
|
{
|
|
|
|
ca := mkchan(c,4);
|
|
|
|
|
|
|
|
go recv(ca[0]);
|
|
|
|
go recv(ca[1]);
|
|
|
|
go recv(ca[2]);
|
|
|
|
go recv(ca[3]);
|
|
|
|
|
|
|
|
go sel(nc,nc,nc,nc, ca[0],ca[1],ca[2],ca[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// select send to select recv
|
|
|
|
func
|
|
|
|
test4(c int)
|
|
|
|
{
|
|
|
|
ca := mkchan(c,4);
|
|
|
|
|
|
|
|
go sel(nc,nc,nc,nc, ca[0],ca[1],ca[2],ca[3]);
|
|
|
|
go sel(ca[0],ca[1],ca[2],ca[3], nc,nc,nc,nc);
|
|
|
|
}
|
|
|
|
|
|
|
|
func
|
|
|
|
test5(c int)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
ca := mkchan(c,8);
|
|
|
|
|
|
|
|
go sel(ca[4],ca[5],ca[6],ca[7], ca[0],ca[1],ca[2],ca[3]);
|
|
|
|
go sel(ca[0],ca[1],ca[2],ca[3], ca[4],ca[5],ca[6],ca[7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
func
|
|
|
|
test6(c int)
|
|
|
|
{
|
|
|
|
ca := mkchan(c,12);
|
|
|
|
|
|
|
|
go send(ca[4]);
|
|
|
|
go send(ca[5]);
|
|
|
|
go send(ca[6]);
|
|
|
|
go send(ca[7]);
|
|
|
|
|
|
|
|
go recv(ca[8]);
|
|
|
|
go recv(ca[9]);
|
|
|
|
go recv(ca[10]);
|
|
|
|
go recv(ca[11]);
|
|
|
|
|
|
|
|
go sel(ca[4],ca[5],ca[6],ca[7], ca[0],ca[1],ca[2],ca[3]);
|
|
|
|
go sel(ca[0],ca[1],ca[2],ca[3], ca[8],ca[9],ca[10],ca[11]);
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for outstanding tests to finish
|
|
|
|
func
|
|
|
|
wait()
|
|
|
|
{
|
2009-05-08 16:21:41 -06:00
|
|
|
runtime.Gosched();
|
2008-09-20 20:56:40 -06:00
|
|
|
for nproc != 0 {
|
2009-05-08 16:21:41 -06:00
|
|
|
runtime.Gosched();
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run all tests with specified buffer size
|
|
|
|
func
|
2008-09-20 20:56:40 -06:00
|
|
|
tests(c int)
|
2008-09-19 21:52:59 -06:00
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
ca := mkchan(c,4);
|
|
|
|
test1(ca[0]);
|
|
|
|
test1(ca[1]);
|
|
|
|
test1(ca[2]);
|
|
|
|
test1(ca[3]);
|
|
|
|
wait();
|
|
|
|
|
|
|
|
test2(c);
|
|
|
|
wait();
|
|
|
|
|
|
|
|
test3(c);
|
2008-09-19 21:52:59 -06:00
|
|
|
wait();
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
test4(c);
|
2008-09-19 21:52:59 -06:00
|
|
|
wait();
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
test5(c);
|
|
|
|
wait();
|
|
|
|
|
|
|
|
test6(c);
|
2008-09-19 21:52:59 -06:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
// run all test with 4 buffser sizes
|
|
|
|
func
|
|
|
|
main()
|
|
|
|
{
|
2008-09-20 20:56:40 -06:00
|
|
|
|
2008-09-19 21:52:59 -06:00
|
|
|
tests(0);
|
|
|
|
tests(1);
|
|
|
|
tests(10);
|
|
|
|
tests(100);
|
|
|
|
|
2008-09-20 20:56:40 -06:00
|
|
|
t := 4 // buffer sizes
|
|
|
|
* ( 4*4 // tests 1,2,3,4 channels
|
|
|
|
+ 8 // test 5 channels
|
|
|
|
+ 12 // test 6 channels
|
|
|
|
) * 76; // sends/recvs on a channel
|
|
|
|
|
|
|
|
if tots != t || totr != t {
|
|
|
|
print("tots=", tots, " totr=", totr, " sb=", t, "\n");
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(1);
|
2008-09-20 16:16:48 -06:00
|
|
|
}
|
2009-05-08 16:21:41 -06:00
|
|
|
os.Exit(0);
|
2008-09-19 21:52:59 -06:00
|
|
|
}
|