2008-11-06 23:27:32 -07: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
|
|
|
|
|
|
|
|
var counter uint
|
|
|
|
var shift uint
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func GetValue() uint {
|
2010-03-24 17:46:53 -06:00
|
|
|
counter++
|
2009-08-17 14:30:22 -06:00
|
|
|
return 1 << shift
|
2008-11-06 23:27:32 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func Send(a, b chan uint) int {
|
2010-03-24 17:46:53 -06:00
|
|
|
var i int
|
2009-08-17 14:30:22 -06:00
|
|
|
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case a <- GetValue():
|
2010-03-24 17:46:53 -06:00
|
|
|
i++
|
|
|
|
a = nil
|
2009-08-17 14:30:22 -06:00
|
|
|
case b <- GetValue():
|
2010-03-24 17:46:53 -06:00
|
|
|
i++
|
|
|
|
b = nil
|
2009-08-17 14:30:22 -06:00
|
|
|
default:
|
2010-03-24 17:46:53 -06:00
|
|
|
break LOOP
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
shift++
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
return i
|
2008-11-06 23:27:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-03-24 17:46:53 -06:00
|
|
|
a := make(chan uint, 1)
|
|
|
|
b := make(chan uint, 1)
|
2009-08-17 14:30:22 -06:00
|
|
|
if v := Send(a, b); v != 2 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("Send returned", v, "!= 2")
|
|
|
|
panic("fail")
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
if av, bv := <-a, <-b; av|bv != 3 {
|
|
|
|
println("bad values", av, bv)
|
|
|
|
panic("fail")
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
|
|
|
if v := Send(a, nil); v != 1 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("Send returned", v, "!= 1")
|
|
|
|
panic("fail")
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
|
|
|
if counter != 10 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("counter is", counter, "!= 10")
|
|
|
|
panic("fail")
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
2008-11-06 23:27:32 -07:00
|
|
|
}
|