2011-12-01 09:06:16 +01:00
|
|
|
// 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.
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
package runtime
|
2011-12-01 09:06:16 +01:00
|
|
|
#include "runtime.h"
|
2012-09-28 23:25:20 +02:00
|
|
|
#include "arch.h"
|
2011-12-01 09:06:16 +01:00
|
|
|
#include "go-type.h"
|
2012-10-23 06:31:11 +02:00
|
|
|
#include "race.h"
|
|
|
|
#include "malloc.h"
|
2014-07-12 02:01:09 +02:00
|
|
|
#include "chan.h"
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
uint32 runtime_Hchansize = sizeof(Hchan);
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
static void dequeueg(WaitQ*);
|
|
|
|
static SudoG* dequeue(WaitQ*);
|
|
|
|
static void enqueue(WaitQ*, SudoG*);
|
2012-10-23 06:31:11 +02:00
|
|
|
static void racesync(Hchan*, SudoG*);
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
static Hchan*
|
|
|
|
makechan(ChanType *t, int64 hint)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
Hchan *c;
|
2012-10-23 06:31:11 +02:00
|
|
|
uintptr n;
|
2011-12-01 09:06:16 +01:00
|
|
|
const Type *elem;
|
2012-10-03 07:27:36 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
elem = t->__element_type;
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// compiler checks this but be safe.
|
|
|
|
if(elem->__size >= (1<<16))
|
|
|
|
runtime_throw("makechan: invalid channel element type");
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(hint < 0 || (intgo)hint != hint || (elem->__size > 0 && (uintptr)hint > (MaxMem - sizeof(*c)) / elem->__size))
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_panicstring("makechan: size out of range");
|
|
|
|
|
|
|
|
n = sizeof(*c);
|
2013-11-06 20:49:01 +01:00
|
|
|
n = ROUND(n, elem->__align);
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
// allocate memory in one call
|
2014-06-07 00:37:27 +02:00
|
|
|
c = (Hchan*)runtime_mallocgc(sizeof(*c) + hint*elem->__size, (uintptr)t | TypeInfo_Chan, 0);
|
2011-12-01 09:06:16 +01:00
|
|
|
c->elemsize = elem->__size;
|
2014-06-07 00:37:27 +02:00
|
|
|
c->elemtype = elem;
|
2011-12-01 09:06:16 +01:00
|
|
|
c->dataqsiz = hint;
|
|
|
|
|
|
|
|
if(debug)
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_printf("makechan: chan=%p; elemsize=%D; dataqsiz=%D\n",
|
|
|
|
c, (int64)elem->__size, (int64)c->dataqsiz);
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.makechan(t *ChanType, size uint64) (c *Hchan) {
|
2014-06-07 00:37:27 +02:00
|
|
|
c = makechan(t, size);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Hchan*
|
|
|
|
__go_new_channel(ChanType *t, uintptr hint)
|
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
return makechan(t, hint);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2012-02-08 07:18:41 +01:00
|
|
|
Hchan*
|
|
|
|
__go_new_channel_big(ChanType *t, uint64 hint)
|
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
return makechan(t, hint);
|
2012-02-08 07:18:41 +01:00
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
/*
|
|
|
|
* generic single channel send/recv
|
|
|
|
* if the bool pointer is nil,
|
|
|
|
* then the full exchange will
|
|
|
|
* occur. if pres is not nil,
|
|
|
|
* then the protocol will not
|
|
|
|
* sleep but return if it could
|
|
|
|
* not complete.
|
|
|
|
*
|
|
|
|
* sleep can wake up with g->param == nil
|
|
|
|
* when a channel involved in the sleep has
|
|
|
|
* been closed. it is easiest to loop and re-run
|
|
|
|
* the operation; we'll see that it's now closed.
|
|
|
|
*/
|
2014-06-07 00:37:27 +02:00
|
|
|
static bool
|
|
|
|
chansend(ChanType *t, Hchan *c, byte *ep, bool block, void *pc)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
SudoG *sg;
|
|
|
|
SudoG mysg;
|
|
|
|
G* gp;
|
2012-10-23 06:31:11 +02:00
|
|
|
int64 t0;
|
2011-12-01 09:06:16 +01:00
|
|
|
G* g;
|
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled)
|
|
|
|
runtime_racereadobjectpc(ep, t->__element_type, runtime_getcallerpc(&t), chansend);
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
if(c == nil) {
|
|
|
|
USED(t);
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block)
|
|
|
|
return false;
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_park(nil, nil, "chan send (nil chan)");
|
2014-06-07 00:37:27 +02:00
|
|
|
return false; // not reached
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_gcwaiting())
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_gosched();
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
runtime_printf("chansend: chan=%p\n", c);
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
t0 = 0;
|
|
|
|
mysg.releasetime = 0;
|
|
|
|
if(runtime_blockprofilerate > 0) {
|
|
|
|
t0 = runtime_cputicks();
|
|
|
|
mysg.releasetime = -1;
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_lock(c);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_racereadpc(c, pc, chansend);
|
2011-12-01 09:06:16 +01:00
|
|
|
if(c->closed)
|
|
|
|
goto closed;
|
|
|
|
|
|
|
|
if(c->dataqsiz > 0)
|
|
|
|
goto asynch;
|
|
|
|
|
|
|
|
sg = dequeue(&c->recvq);
|
|
|
|
if(sg != nil) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
racesync(c, sg);
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
2012-10-03 07:27:36 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
|
|
|
if(sg->elem != nil)
|
|
|
|
runtime_memmove(sg->elem, ep, c->elemsize);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block) {
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
return false;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mysg.elem = ep;
|
|
|
|
mysg.g = g;
|
2014-06-07 00:37:27 +02:00
|
|
|
mysg.selectdone = nil;
|
2011-12-01 09:06:16 +01:00
|
|
|
g->param = nil;
|
|
|
|
enqueue(&c->sendq, &mysg);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(c, "chan send");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
if(g->param == nil) {
|
|
|
|
runtime_lock(c);
|
|
|
|
if(!c->closed)
|
|
|
|
runtime_throw("chansend: spurious wakeup");
|
|
|
|
goto closed;
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if(mysg.releasetime > 0)
|
|
|
|
runtime_blockevent(mysg.releasetime - t0, 2);
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
asynch:
|
|
|
|
if(c->closed)
|
|
|
|
goto closed;
|
|
|
|
|
|
|
|
if(c->qcount >= c->dataqsiz) {
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block) {
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
return false;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
mysg.g = g;
|
|
|
|
mysg.elem = nil;
|
2014-06-07 00:37:27 +02:00
|
|
|
mysg.selectdone = nil;
|
2011-12-01 09:06:16 +01:00
|
|
|
enqueue(&c->sendq, &mysg);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(c, "chan send");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
runtime_lock(c);
|
|
|
|
goto asynch;
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
if(raceenabled) {
|
|
|
|
runtime_raceacquire(chanbuf(c, c->sendx));
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_racerelease(chanbuf(c, c->sendx));
|
2014-07-19 10:53:52 +02:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_memmove(chanbuf(c, c->sendx), ep, c->elemsize);
|
|
|
|
if(++c->sendx == c->dataqsiz)
|
|
|
|
c->sendx = 0;
|
|
|
|
c->qcount++;
|
|
|
|
|
|
|
|
sg = dequeue(&c->recvq);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
runtime_unlock(c);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
} else
|
|
|
|
runtime_unlock(c);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(mysg.releasetime > 0)
|
|
|
|
runtime_blockevent(mysg.releasetime - t0, 2);
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
closed:
|
|
|
|
runtime_unlock(c);
|
|
|
|
runtime_panicstring("send on closed channel");
|
2014-06-07 00:37:27 +02:00
|
|
|
return false; // not reached
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
static bool
|
|
|
|
chanrecv(ChanType *t, Hchan* c, byte *ep, bool block, bool *received)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
SudoG *sg;
|
|
|
|
SudoG mysg;
|
|
|
|
G *gp;
|
2012-10-23 06:31:11 +02:00
|
|
|
int64 t0;
|
2011-12-01 09:06:16 +01:00
|
|
|
G *g;
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_gcwaiting())
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_gosched();
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// raceenabled: don't need to check ep, as it is always on the stack.
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
if(debug)
|
|
|
|
runtime_printf("chanrecv: chan=%p\n", c);
|
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
|
|
|
|
if(c == nil) {
|
|
|
|
USED(t);
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block)
|
|
|
|
return false;
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_park(nil, nil, "chan receive (nil chan)");
|
2014-06-07 00:37:27 +02:00
|
|
|
return false; // not reached
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
t0 = 0;
|
|
|
|
mysg.releasetime = 0;
|
|
|
|
if(runtime_blockprofilerate > 0) {
|
|
|
|
t0 = runtime_cputicks();
|
|
|
|
mysg.releasetime = -1;
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_lock(c);
|
|
|
|
if(c->dataqsiz > 0)
|
|
|
|
goto asynch;
|
|
|
|
|
|
|
|
if(c->closed)
|
|
|
|
goto closed;
|
|
|
|
|
|
|
|
sg = dequeue(&c->sendq);
|
|
|
|
if(sg != nil) {
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
racesync(c, sg);
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
|
|
|
|
|
|
|
if(ep != nil)
|
|
|
|
runtime_memmove(ep, sg->elem, c->elemsize);
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2012-10-23 06:31:11 +02:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
|
|
|
|
if(received != nil)
|
|
|
|
*received = true;
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block) {
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
2014-06-07 00:37:27 +02:00
|
|
|
return false;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mysg.elem = ep;
|
|
|
|
mysg.g = g;
|
2014-06-07 00:37:27 +02:00
|
|
|
mysg.selectdone = nil;
|
2011-12-01 09:06:16 +01:00
|
|
|
g->param = nil;
|
|
|
|
enqueue(&c->recvq, &mysg);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(c, "chan receive");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
if(g->param == nil) {
|
|
|
|
runtime_lock(c);
|
|
|
|
if(!c->closed)
|
|
|
|
runtime_throw("chanrecv: spurious wakeup");
|
|
|
|
goto closed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(received != nil)
|
|
|
|
*received = true;
|
2012-10-23 06:31:11 +02:00
|
|
|
if(mysg.releasetime > 0)
|
|
|
|
runtime_blockevent(mysg.releasetime - t0, 2);
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
asynch:
|
|
|
|
if(c->qcount <= 0) {
|
|
|
|
if(c->closed)
|
|
|
|
goto closed;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(!block) {
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
|
|
|
if(received != nil)
|
|
|
|
*received = false;
|
2014-06-07 00:37:27 +02:00
|
|
|
return false;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
mysg.g = g;
|
|
|
|
mysg.elem = nil;
|
2014-06-07 00:37:27 +02:00
|
|
|
mysg.selectdone = nil;
|
2011-12-01 09:06:16 +01:00
|
|
|
enqueue(&c->recvq, &mysg);
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_parkunlock(c, "chan receive");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
runtime_lock(c);
|
|
|
|
goto asynch;
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2014-07-19 10:53:52 +02:00
|
|
|
if(raceenabled) {
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_raceacquire(chanbuf(c, c->recvx));
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_racerelease(chanbuf(c, c->recvx));
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
if(ep != nil)
|
|
|
|
runtime_memmove(ep, chanbuf(c, c->recvx), c->elemsize);
|
|
|
|
runtime_memclr(chanbuf(c, c->recvx), c->elemsize);
|
|
|
|
if(++c->recvx == c->dataqsiz)
|
|
|
|
c->recvx = 0;
|
|
|
|
c->qcount--;
|
|
|
|
|
|
|
|
sg = dequeue(&c->sendq);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
runtime_unlock(c);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
} else
|
|
|
|
runtime_unlock(c);
|
|
|
|
|
|
|
|
if(received != nil)
|
|
|
|
*received = true;
|
2012-10-23 06:31:11 +02:00
|
|
|
if(mysg.releasetime > 0)
|
|
|
|
runtime_blockevent(mysg.releasetime - t0, 2);
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
closed:
|
|
|
|
if(ep != nil)
|
|
|
|
runtime_memclr(ep, c->elemsize);
|
|
|
|
if(received != nil)
|
|
|
|
*received = false;
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
runtime_raceacquire(c);
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_unlock(c);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(mysg.releasetime > 0)
|
|
|
|
runtime_blockevent(mysg.releasetime - t0, 2);
|
2014-06-07 00:37:27 +02:00
|
|
|
return true;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The compiler generates a call to __go_send_small to send a value 8
|
|
|
|
// bytes or smaller.
|
|
|
|
void
|
|
|
|
__go_send_small(ChanType *t, Hchan* c, uint64 val)
|
|
|
|
{
|
2012-02-11 01:03:10 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
byte b[sizeof(uint64)];
|
|
|
|
uint64 v;
|
|
|
|
} u;
|
2014-06-07 00:37:27 +02:00
|
|
|
byte *v;
|
2012-02-11 01:03:10 +01:00
|
|
|
|
|
|
|
u.v = val;
|
|
|
|
#ifndef WORDS_BIGENDIAN
|
2014-06-07 00:37:27 +02:00
|
|
|
v = u.b;
|
2012-02-11 01:03:10 +01:00
|
|
|
#else
|
2014-06-07 00:37:27 +02:00
|
|
|
v = u.b + sizeof(uint64) - t->__element_type->__size;
|
2012-02-11 01:03:10 +01:00
|
|
|
#endif
|
2014-06-07 00:37:27 +02:00
|
|
|
chansend(t, c, v, true, runtime_getcallerpc(&t));
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The compiler generates a call to __go_send_big to send a value
|
|
|
|
// larger than 8 bytes or smaller.
|
|
|
|
void
|
2014-06-07 00:37:27 +02:00
|
|
|
__go_send_big(ChanType *t, Hchan* c, byte* v)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
chansend(t, c, v, true, runtime_getcallerpc(&t));
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-04-15 00:43:47 +02:00
|
|
|
// The compiler generates a call to __go_receive to receive a
|
|
|
|
// value from a channel.
|
2011-12-01 09:06:16 +01:00
|
|
|
void
|
2014-06-07 00:37:27 +02:00
|
|
|
__go_receive(ChanType *t, Hchan* c, byte* v)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
2014-06-07 00:37:27 +02:00
|
|
|
chanrecv(t, c, v, true, nil);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
_Bool runtime_chanrecv2(ChanType *t, Hchan* c, byte* v)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "runtime.chanrecv2");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
_Bool
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_chanrecv2(ChanType *t, Hchan* c, byte* v)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
2014-06-10 02:36:38 +02:00
|
|
|
bool received = false;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
chanrecv(t, c, v, true, &received);
|
2011-12-01 09:06:16 +01:00
|
|
|
return received;
|
|
|
|
}
|
|
|
|
|
|
|
|
// compiler implements
|
|
|
|
//
|
|
|
|
// select {
|
|
|
|
// case c <- v:
|
|
|
|
// ... foo
|
|
|
|
// default:
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// as
|
|
|
|
//
|
|
|
|
// if selectnbsend(c, v) {
|
|
|
|
// ... foo
|
|
|
|
// } else {
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectnbsend(t *ChanType, c *Hchan, elem *byte) (selected bool) {
|
|
|
|
selected = chansend(t, c, elem, false, runtime_getcallerpc(&t));
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// compiler implements
|
|
|
|
//
|
|
|
|
// select {
|
|
|
|
// case v = <-c:
|
|
|
|
// ... foo
|
|
|
|
// default:
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// as
|
|
|
|
//
|
|
|
|
// if selectnbrecv(&v, c) {
|
|
|
|
// ... foo
|
|
|
|
// } else {
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectnbrecv(t *ChanType, elem *byte, c *Hchan) (selected bool) {
|
|
|
|
selected = chanrecv(t, c, elem, false, nil);
|
2012-10-03 07:27:36 +02:00
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
// compiler implements
|
|
|
|
//
|
|
|
|
// select {
|
|
|
|
// case v, ok = <-c:
|
|
|
|
// ... foo
|
|
|
|
// default:
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// as
|
|
|
|
//
|
|
|
|
// if c != nil && selectnbrecv2(&v, &ok, c) {
|
|
|
|
// ... foo
|
|
|
|
// } else {
|
|
|
|
// ... bar
|
|
|
|
// }
|
|
|
|
//
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectnbrecv2(t *ChanType, elem *byte, received *bool, c *Hchan) (selected bool) {
|
2011-12-01 09:06:16 +01:00
|
|
|
bool r;
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
selected = chanrecv(t, c, elem, false, received == nil ? nil : &r);
|
2011-12-01 09:06:16 +01:00
|
|
|
if(received != nil)
|
|
|
|
*received = r;
|
2012-10-03 07:27:36 +02:00
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.chansend(t *ChanType, c *Hchan, elem *byte, nb bool) (selected bool) {
|
|
|
|
selected = chansend(t, c, elem, !nb, runtime_getcallerpc(&t));
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.chanrecv(t *ChanType, c *Hchan, nb bool, elem *byte) (selected bool, received bool) {
|
2011-12-01 09:06:16 +01:00
|
|
|
received = false;
|
2014-07-12 02:01:09 +02:00
|
|
|
selected = chanrecv(t, c, elem, !nb, &received);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
static Select* newselect(int32);
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func newselect(size int32) (sel *byte) {
|
|
|
|
sel = (byte*)newselect(size);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
static Select*
|
|
|
|
newselect(int32 size)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
int32 n;
|
|
|
|
Select *sel;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if(size > 1)
|
|
|
|
n = size-1;
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// allocate all the memory we need in a single allocation
|
|
|
|
// start with Select with size cases
|
|
|
|
// then lockorder with size entries
|
|
|
|
// then pollorder with size entries
|
2011-12-01 09:06:16 +01:00
|
|
|
sel = runtime_mal(sizeof(*sel) +
|
|
|
|
n*sizeof(sel->scase[0]) +
|
|
|
|
size*sizeof(sel->lockorder[0]) +
|
|
|
|
size*sizeof(sel->pollorder[0]));
|
|
|
|
|
|
|
|
sel->tcase = size;
|
|
|
|
sel->ncase = 0;
|
2012-02-11 01:03:10 +01:00
|
|
|
sel->lockorder = (void*)(sel->scase + size);
|
|
|
|
sel->pollorder = (void*)(sel->lockorder + size);
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("newselect s=%p size=%d\n", sel, size);
|
2014-06-07 00:37:27 +02:00
|
|
|
return sel;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// cut in half to give stack a chance to split
|
|
|
|
static void selectsend(Select *sel, Hchan *c, int index, void *elem);
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectsend(sel *Select, c *Hchan, elem *byte, index int32) {
|
2011-12-01 09:06:16 +01:00
|
|
|
// nil cases do not compete
|
2014-07-12 02:01:09 +02:00
|
|
|
if(c != nil)
|
|
|
|
selectsend(sel, c, index, elem);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selectsend(Select *sel, Hchan *c, int index, void *elem)
|
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
Scase *cas;
|
2012-10-03 07:27:36 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
|
|
|
runtime_throw("selectsend: too many cases");
|
|
|
|
sel->ncase = i+1;
|
|
|
|
cas = &sel->scase[i];
|
|
|
|
|
|
|
|
cas->index = index;
|
|
|
|
cas->chan = c;
|
|
|
|
cas->kind = CaseSend;
|
|
|
|
cas->sg.elem = elem;
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("selectsend s=%p index=%d chan=%p\n",
|
|
|
|
sel, cas->index, cas->chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut in half to give stack a chance to split
|
|
|
|
static void selectrecv(Select *sel, Hchan *c, int index, void *elem, bool*);
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectrecv(sel *Select, c *Hchan, elem *byte, index int32) {
|
2011-12-01 09:06:16 +01:00
|
|
|
// nil cases do not compete
|
2014-07-12 02:01:09 +02:00
|
|
|
if(c != nil)
|
|
|
|
selectrecv(sel, c, index, elem, nil);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectrecv2(sel *Select, c *Hchan, elem *byte, received *bool, index int32) {
|
2011-12-01 09:06:16 +01:00
|
|
|
// nil cases do not compete
|
2014-07-12 02:01:09 +02:00
|
|
|
if(c != nil)
|
|
|
|
selectrecv(sel, c, index, elem, received);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selectrecv(Select *sel, Hchan *c, int index, void *elem, bool *received)
|
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
Scase *cas;
|
|
|
|
|
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
|
|
|
runtime_throw("selectrecv: too many cases");
|
|
|
|
sel->ncase = i+1;
|
|
|
|
cas = &sel->scase[i];
|
|
|
|
cas->index = index;
|
|
|
|
cas->chan = c;
|
|
|
|
|
|
|
|
cas->kind = CaseRecv;
|
|
|
|
cas->sg.elem = elem;
|
|
|
|
cas->receivedp = received;
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("selectrecv s=%p index=%d chan=%p\n",
|
|
|
|
sel, cas->index, cas->chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cut in half to give stack a chance to split
|
|
|
|
static void selectdefault(Select*, int);
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectdefault(sel *Select, index int32) {
|
2011-12-01 09:06:16 +01:00
|
|
|
selectdefault(sel, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-23 06:31:11 +02:00
|
|
|
selectdefault(Select *sel, int32 index)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
int32 i;
|
|
|
|
Scase *cas;
|
|
|
|
|
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
|
|
|
runtime_throw("selectdefault: too many cases");
|
|
|
|
sel->ncase = i+1;
|
|
|
|
cas = &sel->scase[i];
|
|
|
|
cas->index = index;
|
|
|
|
cas->chan = nil;
|
|
|
|
|
|
|
|
cas->kind = CaseDefault;
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("selectdefault s=%p index=%d\n",
|
|
|
|
sel, cas->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sellock(Select *sel)
|
|
|
|
{
|
|
|
|
uint32 i;
|
|
|
|
Hchan *c, *c0;
|
|
|
|
|
|
|
|
c = nil;
|
|
|
|
for(i=0; i<sel->ncase; i++) {
|
|
|
|
c0 = sel->lockorder[i];
|
|
|
|
if(c0 && c0 != c) {
|
|
|
|
c = sel->lockorder[i];
|
|
|
|
runtime_lock(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
selunlock(Select *sel)
|
|
|
|
{
|
2013-07-16 08:54:42 +02:00
|
|
|
int32 i, n, r;
|
|
|
|
Hchan *c;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
// We must be very careful here to not touch sel after we have unlocked
|
|
|
|
// the last lock, because sel can be freed right after the last unlock.
|
|
|
|
// Consider the following situation.
|
|
|
|
// First M calls runtime_park() in runtime_selectgo() passing the sel.
|
|
|
|
// Once runtime_park() has unlocked the last lock, another M makes
|
|
|
|
// the G that calls select runnable again and schedules it for execution.
|
|
|
|
// When the G runs on another M, it locks all the locks and frees sel.
|
|
|
|
// Now if the first M touches sel, it will access freed memory.
|
|
|
|
n = (int32)sel->ncase;
|
|
|
|
r = 0;
|
|
|
|
// skip the default case
|
|
|
|
if(n>0 && sel->lockorder[0] == nil)
|
|
|
|
r = 1;
|
|
|
|
for(i = n-1; i >= r; i--) {
|
|
|
|
c = sel->lockorder[i];
|
|
|
|
if(i>0 && sel->lockorder[i-1] == c)
|
|
|
|
continue; // will unlock it on the next iteration
|
|
|
|
runtime_unlock(c);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
static bool
|
|
|
|
selparkcommit(G *gp, void *sel)
|
|
|
|
{
|
|
|
|
USED(gp);
|
|
|
|
selunlock(sel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func block() {
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_park(nil, nil, "select (no cases)"); // forever
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int selectgo(Select**);
|
|
|
|
|
|
|
|
// selectgo(sel *byte);
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func selectgo(sel *Select) (ret int32) {
|
2011-12-01 09:06:16 +01:00
|
|
|
return selectgo(&sel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
selectgo(Select **selp)
|
|
|
|
{
|
|
|
|
Select *sel;
|
2014-06-07 00:37:27 +02:00
|
|
|
uint32 o, i, j, k, done;
|
2013-11-06 20:49:01 +01:00
|
|
|
int64 t0;
|
2011-12-01 09:06:16 +01:00
|
|
|
Scase *cas, *dfl;
|
|
|
|
Hchan *c;
|
|
|
|
SudoG *sg;
|
|
|
|
G *gp;
|
|
|
|
int index;
|
|
|
|
G *g;
|
|
|
|
|
|
|
|
sel = *selp;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_gcwaiting())
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_gosched();
|
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("select: sel=%p\n", sel);
|
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
t0 = 0;
|
|
|
|
if(runtime_blockprofilerate > 0) {
|
|
|
|
t0 = runtime_cputicks();
|
|
|
|
for(i=0; i<sel->ncase; i++)
|
|
|
|
sel->scase[i].sg.releasetime = -1;
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
// The compiler rewrites selects that statically have
|
|
|
|
// only 0 or 1 cases plus default into simpler constructs.
|
|
|
|
// The only way we can end up with such small sel->ncase
|
|
|
|
// values here is for a larger select in which most channels
|
|
|
|
// have been nilled out. The general code handles those
|
|
|
|
// cases correctly, and they are rare enough not to bother
|
|
|
|
// optimizing (and needing to test).
|
|
|
|
|
|
|
|
// generate permuted order
|
|
|
|
for(i=0; i<sel->ncase; i++)
|
|
|
|
sel->pollorder[i] = i;
|
|
|
|
for(i=1; i<sel->ncase; i++) {
|
|
|
|
o = sel->pollorder[i];
|
|
|
|
j = runtime_fastrand1()%(i+1);
|
|
|
|
sel->pollorder[i] = sel->pollorder[j];
|
|
|
|
sel->pollorder[j] = o;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the cases by Hchan address to get the locking order.
|
2013-07-16 08:54:42 +02:00
|
|
|
// simple heap sort, to guarantee n log n time and constant stack footprint.
|
2011-12-01 09:06:16 +01:00
|
|
|
for(i=0; i<sel->ncase; i++) {
|
2013-07-16 08:54:42 +02:00
|
|
|
j = i;
|
|
|
|
c = sel->scase[j].chan;
|
|
|
|
while(j > 0 && sel->lockorder[k=(j-1)/2] < c) {
|
|
|
|
sel->lockorder[j] = sel->lockorder[k];
|
|
|
|
j = k;
|
|
|
|
}
|
|
|
|
sel->lockorder[j] = c;
|
|
|
|
}
|
|
|
|
for(i=sel->ncase; i-->0; ) {
|
|
|
|
c = sel->lockorder[i];
|
|
|
|
sel->lockorder[i] = sel->lockorder[0];
|
|
|
|
j = 0;
|
|
|
|
for(;;) {
|
|
|
|
k = j*2+1;
|
|
|
|
if(k >= i)
|
|
|
|
break;
|
|
|
|
if(k+1 < i && sel->lockorder[k] < sel->lockorder[k+1])
|
|
|
|
k++;
|
|
|
|
if(c < sel->lockorder[k]) {
|
|
|
|
sel->lockorder[j] = sel->lockorder[k];
|
|
|
|
j = k;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
sel->lockorder[j] = c;
|
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
/*
|
|
|
|
for(i=0; i+1<sel->ncase; i++)
|
|
|
|
if(sel->lockorder[i] > sel->lockorder[i+1]) {
|
|
|
|
runtime_printf("i=%d %p %p\n", i, sel->lockorder[i], sel->lockorder[i+1]);
|
|
|
|
runtime_throw("select: broken sort");
|
|
|
|
}
|
|
|
|
*/
|
2011-12-01 09:06:16 +01:00
|
|
|
sellock(sel);
|
|
|
|
|
|
|
|
loop:
|
|
|
|
// pass 1 - look for something already waiting
|
|
|
|
dfl = nil;
|
|
|
|
for(i=0; i<sel->ncase; i++) {
|
|
|
|
o = sel->pollorder[i];
|
|
|
|
cas = &sel->scase[o];
|
|
|
|
c = cas->chan;
|
|
|
|
|
|
|
|
switch(cas->kind) {
|
|
|
|
case CaseRecv:
|
|
|
|
if(c->dataqsiz > 0) {
|
|
|
|
if(c->qcount > 0)
|
|
|
|
goto asyncrecv;
|
|
|
|
} else {
|
|
|
|
sg = dequeue(&c->sendq);
|
|
|
|
if(sg != nil)
|
|
|
|
goto syncrecv;
|
|
|
|
}
|
|
|
|
if(c->closed)
|
|
|
|
goto rclose;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CaseSend:
|
2013-11-06 20:49:01 +01:00
|
|
|
if(raceenabled)
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_racereadpc(c, runtime_selectgo, chansend);
|
2011-12-01 09:06:16 +01:00
|
|
|
if(c->closed)
|
|
|
|
goto sclose;
|
|
|
|
if(c->dataqsiz > 0) {
|
|
|
|
if(c->qcount < c->dataqsiz)
|
|
|
|
goto asyncsend;
|
|
|
|
} else {
|
|
|
|
sg = dequeue(&c->recvq);
|
|
|
|
if(sg != nil)
|
|
|
|
goto syncsend;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CaseDefault:
|
|
|
|
dfl = cas;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dfl != nil) {
|
|
|
|
selunlock(sel);
|
|
|
|
cas = dfl;
|
|
|
|
goto retc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pass 2 - enqueue on all chans
|
2014-06-07 00:37:27 +02:00
|
|
|
done = 0;
|
2011-12-01 09:06:16 +01:00
|
|
|
for(i=0; i<sel->ncase; i++) {
|
|
|
|
o = sel->pollorder[i];
|
|
|
|
cas = &sel->scase[o];
|
|
|
|
c = cas->chan;
|
|
|
|
sg = &cas->sg;
|
|
|
|
sg->g = g;
|
2014-06-07 00:37:27 +02:00
|
|
|
sg->selectdone = &done;
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
switch(cas->kind) {
|
|
|
|
case CaseRecv:
|
|
|
|
enqueue(&c->recvq, sg);
|
|
|
|
break;
|
2012-10-03 07:27:36 +02:00
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
case CaseSend:
|
|
|
|
enqueue(&c->sendq, sg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g->param = nil;
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_park(selparkcommit, sel, "select");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
sellock(sel);
|
|
|
|
sg = g->param;
|
|
|
|
|
|
|
|
// pass 3 - dequeue from unsuccessful chans
|
|
|
|
// otherwise they stack up on quiet channels
|
|
|
|
for(i=0; i<sel->ncase; i++) {
|
|
|
|
cas = &sel->scase[i];
|
|
|
|
if(cas != (Scase*)sg) {
|
|
|
|
c = cas->chan;
|
|
|
|
if(cas->kind == CaseSend)
|
|
|
|
dequeueg(&c->sendq);
|
|
|
|
else
|
|
|
|
dequeueg(&c->recvq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sg == nil)
|
|
|
|
goto loop;
|
|
|
|
|
|
|
|
cas = (Scase*)sg;
|
|
|
|
c = cas->chan;
|
|
|
|
|
|
|
|
if(c->dataqsiz > 0)
|
2013-07-16 08:54:42 +02:00
|
|
|
runtime_throw("selectgo: shouldn't happen");
|
2011-12-01 09:06:16 +01:00
|
|
|
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("wait-return: sel=%p c=%p cas=%p kind=%d\n",
|
|
|
|
sel, c, cas, cas->kind);
|
|
|
|
|
|
|
|
if(cas->kind == CaseRecv) {
|
|
|
|
if(cas->receivedp != nil)
|
|
|
|
*cas->receivedp = true;
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled) {
|
|
|
|
if(cas->kind == CaseRecv && cas->sg.elem != nil)
|
|
|
|
runtime_racewriteobjectpc(cas->sg.elem, c->elemtype, selectgo, chanrecv);
|
|
|
|
else if(cas->kind == CaseSend)
|
|
|
|
runtime_racereadobjectpc(cas->sg.elem, c->elemtype, selectgo, chansend);
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
selunlock(sel);
|
|
|
|
goto retc;
|
|
|
|
|
|
|
|
asyncrecv:
|
|
|
|
// can receive from buffer
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled) {
|
|
|
|
if(cas->sg.elem != nil)
|
|
|
|
runtime_racewriteobjectpc(cas->sg.elem, c->elemtype, selectgo, chanrecv);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_raceacquire(chanbuf(c, c->recvx));
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_racerelease(chanbuf(c, c->recvx));
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
if(cas->receivedp != nil)
|
|
|
|
*cas->receivedp = true;
|
|
|
|
if(cas->sg.elem != nil)
|
|
|
|
runtime_memmove(cas->sg.elem, chanbuf(c, c->recvx), c->elemsize);
|
|
|
|
runtime_memclr(chanbuf(c, c->recvx), c->elemsize);
|
|
|
|
if(++c->recvx == c->dataqsiz)
|
|
|
|
c->recvx = 0;
|
|
|
|
c->qcount--;
|
|
|
|
sg = dequeue(&c->sendq);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
selunlock(sel);
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
} else {
|
|
|
|
selunlock(sel);
|
|
|
|
}
|
|
|
|
goto retc;
|
|
|
|
|
|
|
|
asyncsend:
|
|
|
|
// can send to buffer
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled) {
|
2014-07-19 10:53:52 +02:00
|
|
|
runtime_raceacquire(chanbuf(c, c->sendx));
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_racerelease(chanbuf(c, c->sendx));
|
2014-06-07 00:37:27 +02:00
|
|
|
runtime_racereadobjectpc(cas->sg.elem, c->elemtype, selectgo, chansend);
|
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_memmove(chanbuf(c, c->sendx), cas->sg.elem, c->elemsize);
|
|
|
|
if(++c->sendx == c->dataqsiz)
|
|
|
|
c->sendx = 0;
|
|
|
|
c->qcount++;
|
|
|
|
sg = dequeue(&c->recvq);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
selunlock(sel);
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
} else {
|
|
|
|
selunlock(sel);
|
|
|
|
}
|
|
|
|
goto retc;
|
|
|
|
|
|
|
|
syncrecv:
|
|
|
|
// can receive from sleeping sender (sg)
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled) {
|
|
|
|
if(cas->sg.elem != nil)
|
|
|
|
runtime_racewriteobjectpc(cas->sg.elem, c->elemtype, selectgo, chanrecv);
|
2012-10-23 06:31:11 +02:00
|
|
|
racesync(c, sg);
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
selunlock(sel);
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("syncrecv: sel=%p c=%p o=%d\n", sel, c, o);
|
|
|
|
if(cas->receivedp != nil)
|
|
|
|
*cas->receivedp = true;
|
|
|
|
if(cas->sg.elem != nil)
|
|
|
|
runtime_memmove(cas->sg.elem, sg->elem, c->elemsize);
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
goto retc;
|
|
|
|
|
|
|
|
rclose:
|
|
|
|
// read at end of closed channel
|
|
|
|
selunlock(sel);
|
|
|
|
if(cas->receivedp != nil)
|
|
|
|
*cas->receivedp = false;
|
|
|
|
if(cas->sg.elem != nil)
|
|
|
|
runtime_memclr(cas->sg.elem, c->elemsize);
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled)
|
|
|
|
runtime_raceacquire(c);
|
2011-12-01 09:06:16 +01:00
|
|
|
goto retc;
|
|
|
|
|
|
|
|
syncsend:
|
|
|
|
// can send to sleeping receiver (sg)
|
2014-06-07 00:37:27 +02:00
|
|
|
if(raceenabled) {
|
|
|
|
runtime_racereadobjectpc(cas->sg.elem, c->elemtype, selectgo, chansend);
|
2012-10-23 06:31:11 +02:00
|
|
|
racesync(c, sg);
|
2014-06-07 00:37:27 +02:00
|
|
|
}
|
2011-12-01 09:06:16 +01:00
|
|
|
selunlock(sel);
|
|
|
|
if(debug)
|
|
|
|
runtime_printf("syncsend: sel=%p c=%p o=%d\n", sel, c, o);
|
|
|
|
if(sg->elem != nil)
|
|
|
|
runtime_memmove(sg->elem, cas->sg.elem, c->elemsize);
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
|
|
|
|
retc:
|
|
|
|
// return index corresponding to chosen case
|
|
|
|
index = cas->index;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(cas->sg.releasetime > 0)
|
|
|
|
runtime_blockevent(cas->sg.releasetime - t0, 2);
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_free(sel);
|
|
|
|
return index;
|
|
|
|
|
|
|
|
sclose:
|
|
|
|
// send on closed channel
|
|
|
|
selunlock(sel);
|
|
|
|
runtime_panicstring("send on closed channel");
|
|
|
|
return 0; // not reached
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// This struct must match ../reflect/value.go:/runtimeSelect.
|
|
|
|
typedef struct runtimeSelect runtimeSelect;
|
|
|
|
struct runtimeSelect
|
|
|
|
{
|
|
|
|
uintptr dir;
|
|
|
|
ChanType *typ;
|
|
|
|
Hchan *ch;
|
2014-06-07 00:37:27 +02:00
|
|
|
byte *val;
|
2012-10-23 06:31:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This enum must match ../reflect/value.go:/SelectDir.
|
|
|
|
enum SelectDir {
|
|
|
|
SelectSend = 1,
|
|
|
|
SelectRecv,
|
|
|
|
SelectDefault,
|
|
|
|
};
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.rselect(cases Slice) (chosen int, recvOK bool) {
|
2012-10-23 06:31:11 +02:00
|
|
|
int32 i;
|
|
|
|
Select *sel;
|
|
|
|
runtimeSelect* rcase, *rc;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
chosen = -1;
|
|
|
|
recvOK = false;
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
rcase = (runtimeSelect*)cases.__values;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
sel = newselect(cases.__count);
|
2012-10-23 06:31:11 +02:00
|
|
|
for(i=0; i<cases.__count; i++) {
|
|
|
|
rc = &rcase[i];
|
|
|
|
switch(rc->dir) {
|
|
|
|
case SelectDefault:
|
|
|
|
selectdefault(sel, i);
|
|
|
|
break;
|
|
|
|
case SelectSend:
|
|
|
|
if(rc->ch == nil)
|
|
|
|
break;
|
2014-06-07 00:37:27 +02:00
|
|
|
selectsend(sel, rc->ch, i, rc->val);
|
2012-10-23 06:31:11 +02:00
|
|
|
break;
|
|
|
|
case SelectRecv:
|
|
|
|
if(rc->ch == nil)
|
|
|
|
break;
|
2014-06-07 00:37:27 +02:00
|
|
|
selectrecv(sel, rc->ch, i, rc->val, &recvOK);
|
2012-10-23 06:31:11 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
chosen = (intgo)(uintptr)selectgo(&sel);
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
static void closechan(Hchan *c, void *pc);
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func closechan(c *Hchan) {
|
2013-11-06 20:49:01 +01:00
|
|
|
closechan(c, runtime_getcallerpc(&c));
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.chanclose(c *Hchan) {
|
2014-06-05 01:15:33 +02:00
|
|
|
closechan(c, runtime_getcallerpc(&c));
|
2013-11-06 20:49:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
closechan(Hchan *c, void *pc)
|
2011-12-01 09:06:16 +01:00
|
|
|
{
|
|
|
|
SudoG *sg;
|
|
|
|
G* gp;
|
|
|
|
|
|
|
|
if(c == nil)
|
|
|
|
runtime_panicstring("close of nil channel");
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
if(runtime_gcwaiting())
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_gosched();
|
|
|
|
|
|
|
|
runtime_lock(c);
|
|
|
|
if(c->closed) {
|
|
|
|
runtime_unlock(c);
|
|
|
|
runtime_panicstring("close of closed channel");
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if(raceenabled) {
|
2013-11-06 20:49:01 +01:00
|
|
|
runtime_racewritepc(c, pc, runtime_closechan);
|
2012-10-23 06:31:11 +02:00
|
|
|
runtime_racerelease(c);
|
|
|
|
}
|
|
|
|
|
2011-12-01 09:06:16 +01:00
|
|
|
c->closed = true;
|
|
|
|
|
|
|
|
// release all readers
|
|
|
|
for(;;) {
|
|
|
|
sg = dequeue(&c->recvq);
|
|
|
|
if(sg == nil)
|
|
|
|
break;
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = nil;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// release all writers
|
|
|
|
for(;;) {
|
|
|
|
sg = dequeue(&c->sendq);
|
|
|
|
if(sg == nil)
|
|
|
|
break;
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = nil;
|
2013-11-06 20:49:01 +01:00
|
|
|
if(sg->releasetime)
|
|
|
|
sg->releasetime = runtime_cputicks();
|
2011-12-01 09:06:16 +01:00
|
|
|
runtime_ready(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime_unlock(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__go_builtin_close(Hchan *c)
|
|
|
|
{
|
|
|
|
runtime_closechan(c);
|
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.chanlen(c *Hchan) (len int) {
|
2011-12-01 09:06:16 +01:00
|
|
|
if(c == nil)
|
|
|
|
len = 0;
|
|
|
|
else
|
|
|
|
len = c->qcount;
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
intgo
|
2011-12-01 09:06:16 +01:00
|
|
|
__go_chan_len(Hchan *c)
|
|
|
|
{
|
2014-06-05 01:15:33 +02:00
|
|
|
return reflect_chanlen(c);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
2014-07-12 02:01:09 +02:00
|
|
|
func reflect.chancap(c *Hchan) (cap int) {
|
2011-12-01 09:06:16 +01:00
|
|
|
if(c == nil)
|
|
|
|
cap = 0;
|
|
|
|
else
|
|
|
|
cap = c->dataqsiz;
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
intgo
|
2011-12-01 09:06:16 +01:00
|
|
|
__go_chan_cap(Hchan *c)
|
|
|
|
{
|
2014-06-05 01:15:33 +02:00
|
|
|
return reflect_chancap(c);
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static SudoG*
|
|
|
|
dequeue(WaitQ *q)
|
|
|
|
{
|
|
|
|
SudoG *sgp;
|
|
|
|
|
|
|
|
loop:
|
|
|
|
sgp = q->first;
|
|
|
|
if(sgp == nil)
|
|
|
|
return nil;
|
|
|
|
q->first = sgp->link;
|
|
|
|
|
2014-06-07 00:37:27 +02:00
|
|
|
// if sgp participates in a select and is already signaled, ignore it
|
|
|
|
if(sgp->selectdone != nil) {
|
|
|
|
// claim the right to signal
|
|
|
|
if(*sgp->selectdone != 0 || !runtime_cas(sgp->selectdone, 0, 1))
|
|
|
|
goto loop;
|
2011-12-01 09:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sgp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dequeueg(WaitQ *q)
|
|
|
|
{
|
|
|
|
SudoG **l, *sgp, *prevsgp;
|
|
|
|
G *g;
|
|
|
|
|
|
|
|
g = runtime_g();
|
|
|
|
prevsgp = nil;
|
|
|
|
for(l=&q->first; (sgp=*l) != nil; l=&sgp->link, prevsgp=sgp) {
|
|
|
|
if(sgp->g == g) {
|
|
|
|
*l = sgp->link;
|
|
|
|
if(q->last == sgp)
|
|
|
|
q->last = prevsgp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
enqueue(WaitQ *q, SudoG *sgp)
|
|
|
|
{
|
|
|
|
sgp->link = nil;
|
|
|
|
if(q->first == nil) {
|
|
|
|
q->first = sgp;
|
|
|
|
q->last = sgp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
q->last->link = sgp;
|
|
|
|
q->last = sgp;
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
racesync(Hchan *c, SudoG *sg)
|
|
|
|
{
|
|
|
|
runtime_racerelease(chanbuf(c, 0));
|
|
|
|
runtime_raceacquireg(sg->g, chanbuf(c, 0));
|
|
|
|
runtime_racereleaseg(sg->g, chanbuf(c, 0));
|
|
|
|
runtime_raceacquire(chanbuf(c, 0));
|
|
|
|
}
|