compiler,runtime: implement non-blocking selects

Blocking selects are much more complicated, so let's do non-blocking
ones first.
This commit is contained in:
Ayke van Laethem
2019-06-08 19:21:29 +02:00
committed by Ron Evans
parent 8890a0f3c8
commit b7197bcaae
5 changed files with 291 additions and 25 deletions
+55 -2
View File
@@ -48,10 +48,63 @@ func main() {
}
println("sum(100):", sum)
// Test select
// Test simple selects.
go selectDeadlock()
go selectNoOp()
// Test select with a single send operation (transformed into chan send).
ch = make(chan int)
go fastreceiver(ch)
select {
case ch <- 5:
println("select one sent")
}
close(ch)
// Test select with a single recv operation (transformed into chan recv).
select {
case n := <-ch:
println("select one n:", n)
}
// Test select recv with channel that has one entry.
ch = make(chan int)
go func(ch chan int) {
ch <- 55
}(ch)
time.Sleep(time.Millisecond)
select {
case make(chan int) <- 3:
println("unreachable")
case n := <-ch:
println("select n from chan:", n)
case n := <-make(chan int):
println("unreachable:", n)
}
// Test select recv with closed channel.
close(ch)
select {
case make(chan int) <- 3:
println("unreachable")
case n := <-ch:
println("select n from closed chan:", n)
case n := <-make(chan int):
println("unreachable:", n)
}
// Test select send.
ch = make(chan int)
go fastreceiver(ch)
time.Sleep(time.Millisecond)
select {
case ch <- 235:
println("select send")
case n := <-make(chan int):
println("unreachable:", n)
}
close(ch)
// Allow goroutines to exit.
time.Sleep(time.Microsecond)
}
@@ -68,7 +121,7 @@ func sender(ch chan int) {
}
func sendComplex(ch chan complex128) {
ch <- 7+10.5i
ch <- 7 + 10.5i
}
func fastsender(ch chan int) {
+7
View File
@@ -23,3 +23,10 @@ sum(100): 4950
deadlocking
select no-op
after no-op
select one sent
sum: 5
select one n: 0
select n from chan: 55
select n from closed chan: 0
select send
sum: 235