]> Cypherpunks.ru repositories - gostls13.git/commit
runtime: fix race condition
authorAdam Langley <agl@golang.org>
Fri, 18 Dec 2009 20:25:53 +0000 (12:25 -0800)
committerAdam Langley <agl@golang.org>
Fri, 18 Dec 2009 20:25:53 +0000 (12:25 -0800)
commit50d6c81d4ae1810ad129ef6074607098cbce955b
tree22926a17ad5c0b3c6247a205fee273c8eef9aefd
parent057e7d9faee72daa8e0de6cdf3dcff974e9b6e0f
runtime: fix race condition

(Thanks to ken and rsc for pointing this out)

rsc:
ken pointed out that there's a race in the new
one-lock-per-channel code.  the issue is that
if one goroutine has gone to sleep doing

select {
case <-c1:
case <-c2:
}

and then two more goroutines try to send
on c1 and c2 simultaneously, the way that
the code makes sure only one wins is the
selgen field manipulation in dequeue:

       // if sgp is stale, ignore it
       if(sgp->selgen != sgp->g->selgen) {
       //prints("INVALID PSEUDOG POINTER\n");
       freesg(c, sgp);
       goto loop;
       }

       // invalidate any others
       sgp->g->selgen++;

but because the global lock is gone both
goroutines will be fiddling with sgp->g->selgen
at the same time.

This results in a 7% slowdown in the single threaded case for a
ping-pong microbenchmark.

Since the cas predominantly succeeds, adding a simple check first
didn't make any difference.

R=rsc
CC=golang-dev
https://golang.org/cl/180068
src/pkg/runtime/chan.c
src/pkg/runtime/runtime.h
test/chan/doubleselect.go [new file with mode: 0644]
test/golden.out