runtime: eliminate scase.kind field

Currently, we include a "kind" field on scase to distinguish the three
kinds of cases in a select statement: sends, receives, and defaults.

This commit removes by kind field by instead arranging for the
compiler to always place sends before receives, and to provide their
counts separately. It also passes an explicit "block bool" parameter
to avoid needing to include a default case in the array.

It's safe to shuffle cases like this because the runtime will
randomize the order they're polled in anyway.

Fixes #40410.

Change-Id: Iaeaed4cf7bddd576d78f2c863bd91a03a5c82df2
Reviewed-on: https://go-review.googlesource.com/c/go/+/245125
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Matthew Dempsky 2020-07-27 16:19:15 -07:00
parent d36bc7d78a
commit fe23ba4a14
5 changed files with 125 additions and 103 deletions

View file

@ -1725,6 +1725,14 @@ func TestSelectMaxCases(t *testing.T) {
_, _, _ = Select(sCases)
}
func TestSelectNop(t *testing.T) {
// "select { default: }" should always return the default case.
chosen, _, _ := Select([]SelectCase{{Dir: SelectDefault}})
if chosen != 0 {
t.Fatalf("expected Select to return 0, but got %#v", chosen)
}
}
func BenchmarkSelect(b *testing.B) {
channel := make(chan int)
close(channel)