2008-07-13 15:29:46 -06: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.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
|
2008-07-14 15:33:39 -06:00
|
|
|
static int32 debug = 0;
|
2008-09-09 12:50:14 -06:00
|
|
|
static Lock chanlock;
|
2008-07-13 15:29:46 -06:00
|
|
|
|
2009-03-12 18:55:11 -06:00
|
|
|
enum
|
|
|
|
{
|
2009-03-13 17:47:54 -06:00
|
|
|
Wclosed = 0x0001, // writer has closed
|
|
|
|
Rclosed = 0x0002, // reader has seen close
|
|
|
|
Eincr = 0x0004, // increment errors
|
|
|
|
Emax = 0x0800, // error limit before throw
|
2009-03-12 18:55:11 -06:00
|
|
|
};
|
|
|
|
|
2008-07-14 15:33:39 -06:00
|
|
|
typedef struct Link Link;
|
2008-07-20 21:13:07 -06:00
|
|
|
typedef struct WaitQ WaitQ;
|
|
|
|
typedef struct SudoG SudoG;
|
2008-07-24 16:57:30 -06:00
|
|
|
typedef struct Select Select;
|
|
|
|
typedef struct Scase Scase;
|
2008-07-20 21:13:07 -06:00
|
|
|
|
|
|
|
struct SudoG
|
|
|
|
{
|
|
|
|
G* g; // g and selgen constitute
|
2008-07-25 16:55:12 -06:00
|
|
|
int32 selgen; // a weak pointer to g
|
2009-01-24 16:58:44 -07:00
|
|
|
int16 offset; // offset of case number
|
|
|
|
int8 isfree; // offset of case number
|
2008-07-20 21:13:07 -06:00
|
|
|
SudoG* link;
|
2008-12-09 17:16:07 -07:00
|
|
|
byte elem[8]; // synch data element (+ more)
|
2008-07-20 21:13:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct WaitQ
|
|
|
|
{
|
|
|
|
SudoG* first;
|
|
|
|
SudoG* last;
|
|
|
|
};
|
2008-07-13 15:29:46 -06:00
|
|
|
|
|
|
|
struct Hchan
|
|
|
|
{
|
2009-08-20 12:12:04 -06:00
|
|
|
uint32 qcount; // total data in the q
|
|
|
|
uint32 dataqsiz; // size of the circular q
|
2009-03-12 18:55:11 -06:00
|
|
|
uint16 elemsize;
|
2009-03-13 17:47:54 -06:00
|
|
|
uint16 closed; // Wclosed Rclosed errorcount
|
2008-07-14 15:33:39 -06:00
|
|
|
Alg* elemalg; // interface for element type
|
|
|
|
Link* senddataq; // pointer for sender
|
|
|
|
Link* recvdataq; // pointer for receiver
|
|
|
|
WaitQ recvq; // list of recv waiters
|
|
|
|
WaitQ sendq; // list of send waiters
|
2008-07-20 21:13:07 -06:00
|
|
|
SudoG* free; // freelist
|
2008-07-14 15:33:39 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Link
|
|
|
|
{
|
2008-07-24 16:57:30 -06:00
|
|
|
Link* link; // asynch queue circular linked list
|
2008-12-09 17:16:07 -07:00
|
|
|
byte elem[8]; // asynch queue data element (+ more)
|
2008-07-13 15:29:46 -06:00
|
|
|
};
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
struct Scase
|
|
|
|
{
|
|
|
|
Hchan* chan; // chan
|
|
|
|
byte* pc; // return pc
|
2008-11-05 18:57:18 -07:00
|
|
|
uint16 send; // 0-recv 1-send 2-default
|
2008-07-24 16:57:30 -06:00
|
|
|
uint16 so; // vararg of selected bool
|
2008-07-25 12:58:26 -06:00
|
|
|
union {
|
|
|
|
byte elem[8]; // element (send)
|
|
|
|
byte* elemp; // pointer to element (recv)
|
|
|
|
} u;
|
2008-07-24 16:57:30 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Select
|
|
|
|
{
|
|
|
|
uint16 tcase; // total count of scase[]
|
|
|
|
uint16 ncase; // currently filled scase[]
|
2008-07-26 15:21:21 -06:00
|
|
|
Select* link; // for freelist
|
2008-12-09 17:16:07 -07:00
|
|
|
Scase* scase[1]; // one per case
|
2008-07-24 16:57:30 -06:00
|
|
|
};
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
static Select* selfree[20];
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
static SudoG* dequeue(WaitQ*, Hchan*);
|
|
|
|
static void enqueue(WaitQ*, SudoG*);
|
|
|
|
static SudoG* allocsg(Hchan*);
|
|
|
|
static void freesg(Hchan*, SudoG*);
|
|
|
|
static uint32 gcd(uint32, uint32);
|
|
|
|
static uint32 fastrand1(void);
|
|
|
|
static uint32 fastrand2(void);
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2009-07-08 16:00:54 -06:00
|
|
|
Hchan*
|
|
|
|
makechan(uint32 elemsize, uint32 elemalg, uint32 hint)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
|
|
|
Hchan *c;
|
2008-07-14 15:33:39 -06:00
|
|
|
int32 i;
|
2008-07-13 15:29:46 -06:00
|
|
|
|
2009-01-26 10:56:42 -07:00
|
|
|
if(elemalg >= nelem(algarray)) {
|
2008-12-19 13:05:22 -07:00
|
|
|
printf("chan(alg=%d)\n", elemalg);
|
2009-08-25 16:54:25 -06:00
|
|
|
throw("sys·makechan: unsupported elem type");
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
c = mal(sizeof(*c));
|
|
|
|
|
|
|
|
c->elemsize = elemsize;
|
|
|
|
c->elemalg = &algarray[elemalg];
|
2008-07-14 15:33:39 -06:00
|
|
|
|
|
|
|
if(hint > 0) {
|
|
|
|
Link *d, *b, *e;
|
|
|
|
|
|
|
|
// make a circular q
|
|
|
|
b = nil;
|
|
|
|
e = nil;
|
|
|
|
for(i=0; i<hint; i++) {
|
2008-12-09 17:16:07 -07:00
|
|
|
d = mal(sizeof(*d) + c->elemsize - sizeof(d->elem));
|
2008-07-14 15:33:39 -06:00
|
|
|
if(e == nil)
|
|
|
|
e = d;
|
|
|
|
d->link = b;
|
|
|
|
b = d;
|
|
|
|
}
|
|
|
|
e->link = b;
|
|
|
|
c->recvdataq = b;
|
|
|
|
c->senddataq = b;
|
|
|
|
c->qcount = 0;
|
|
|
|
c->dataqsiz = hint;
|
|
|
|
}
|
2008-07-13 15:29:46 -06:00
|
|
|
|
|
|
|
if(debug) {
|
2009-08-25 16:54:25 -06:00
|
|
|
prints("makechan: chan=");
|
2008-07-13 15:29:46 -06:00
|
|
|
sys·printpointer(c);
|
|
|
|
prints("; elemsize=");
|
|
|
|
sys·printint(elemsize);
|
|
|
|
prints("; elemalg=");
|
|
|
|
sys·printint(elemalg);
|
2008-07-14 15:33:39 -06:00
|
|
|
prints("; dataqsiz=");
|
|
|
|
sys·printint(c->dataqsiz);
|
2008-07-13 15:29:46 -06:00
|
|
|
prints("\n");
|
|
|
|
}
|
2009-07-08 16:00:54 -06:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2009-08-25 16:54:25 -06:00
|
|
|
// makechan(elemsize uint32, elemalg uint32, hint uint32) (hchan *chan any);
|
2009-07-08 16:00:54 -06:00
|
|
|
void
|
2009-08-25 16:54:25 -06:00
|
|
|
sys·makechan(uint32 elemsize, uint32 elemalg, uint32 hint, Hchan *ret)
|
2009-07-08 16:00:54 -06:00
|
|
|
{
|
|
|
|
ret = makechan(elemsize, elemalg, hint);
|
|
|
|
FLUSH(&ret);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
2009-03-13 17:47:54 -06:00
|
|
|
static void
|
|
|
|
incerr(Hchan* c)
|
|
|
|
{
|
|
|
|
c->closed += Eincr;
|
|
|
|
if(c->closed & Emax) {
|
|
|
|
unlock(&chanlock);
|
|
|
|
throw("too many operations on a closed channel");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:21:21 -06: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
|
|
|
|
*/
|
2008-07-13 15:29:46 -06:00
|
|
|
void
|
2009-07-08 16:00:54 -06:00
|
|
|
chansend(Hchan *c, byte *ep, bool *pres)
|
2008-07-13 15:29:46 -06:00
|
|
|
{
|
2008-07-26 15:21:21 -06:00
|
|
|
SudoG *sg;
|
|
|
|
G* gp;
|
2008-07-13 15:29:46 -06:00
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
prints("chansend: chan=");
|
|
|
|
sys·printpointer(c);
|
|
|
|
prints("; elem=");
|
2008-07-26 15:21:21 -06:00
|
|
|
c->elemalg->print(c->elemsize, ep);
|
2008-07-13 15:29:46 -06:00
|
|
|
prints("\n");
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2009-03-23 19:50:35 -06:00
|
|
|
loop:
|
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto closed;
|
2009-03-13 17:47:54 -06:00
|
|
|
|
2008-07-14 15:33:39 -06:00
|
|
|
if(c->dataqsiz > 0)
|
|
|
|
goto asynch;
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = dequeue(&c->recvq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
if(ep != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, sg->elem, ep);
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2008-07-26 15:21:21 -06:00
|
|
|
|
|
|
|
if(pres != nil)
|
|
|
|
*pres = true;
|
2008-07-14 15:33:39 -06:00
|
|
|
return;
|
|
|
|
}
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
if(pres != nil) {
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
*pres = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sg = allocsg(c);
|
|
|
|
if(ep != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, sg->elem, ep);
|
|
|
|
g->param = nil;
|
2008-07-14 15:33:39 -06:00
|
|
|
g->status = Gwaiting;
|
2008-07-26 15:21:21 -06:00
|
|
|
enqueue(&c->sendq, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2009-05-08 16:21:41 -06:00
|
|
|
gosched();
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = g->param;
|
2009-03-23 19:50:35 -06:00
|
|
|
if(sg == nil)
|
|
|
|
goto loop;
|
2008-07-26 15:21:21 -06:00
|
|
|
freesg(c, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-14 15:33:39 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
asynch:
|
2009-03-13 17:47:54 -06:00
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto closed;
|
|
|
|
|
|
|
|
if(c->qcount >= c->dataqsiz) {
|
2008-09-26 12:47:04 -06:00
|
|
|
if(pres != nil) {
|
|
|
|
unlock(&chanlock);
|
|
|
|
*pres = false;
|
|
|
|
return;
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = allocsg(c);
|
2008-07-14 18:41:38 -06:00
|
|
|
g->status = Gwaiting;
|
2008-07-26 15:21:21 -06:00
|
|
|
enqueue(&c->sendq, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2009-05-08 16:21:41 -06:00
|
|
|
gosched();
|
2008-09-19 21:43:30 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2009-03-13 17:47:54 -06:00
|
|
|
goto asynch;
|
2008-07-14 18:41:38 -06:00
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
if(ep != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, c->senddataq->elem, ep);
|
2008-07-14 18:41:38 -06:00
|
|
|
c->senddataq = c->senddataq->link;
|
|
|
|
c->qcount++;
|
2008-07-26 15:21:21 -06:00
|
|
|
|
|
|
|
sg = dequeue(&c->recvq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
freesg(c, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2008-09-19 21:43:30 -06:00
|
|
|
} else
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-09-26 12:47:04 -06:00
|
|
|
if(pres != nil)
|
|
|
|
*pres = true;
|
2009-03-13 17:47:54 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
closed:
|
|
|
|
incerr(c);
|
|
|
|
if(pres != nil)
|
2009-03-23 19:50:35 -06:00
|
|
|
*pres = true;
|
2009-03-13 17:47:54 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-13 15:29:46 -06:00
|
|
|
}
|
2008-07-13 17:20:27 -06:00
|
|
|
|
2009-07-08 16:00:54 -06:00
|
|
|
void
|
2008-07-26 15:21:21 -06:00
|
|
|
chanrecv(Hchan* c, byte *ep, bool* pres)
|
2008-07-15 22:07:59 -06:00
|
|
|
{
|
2008-07-26 15:21:21 -06:00
|
|
|
SudoG *sg;
|
|
|
|
G *gp;
|
2008-07-16 12:46:33 -06:00
|
|
|
|
2008-07-15 22:07:59 -06:00
|
|
|
if(debug) {
|
2008-07-26 15:21:21 -06:00
|
|
|
prints("chanrecv: chan=");
|
2008-07-15 22:07:59 -06:00
|
|
|
sys·printpointer(c);
|
|
|
|
prints("\n");
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2009-03-23 19:50:35 -06:00
|
|
|
loop:
|
2008-07-15 22:07:59 -06:00
|
|
|
if(c->dataqsiz > 0)
|
|
|
|
goto asynch;
|
|
|
|
|
2009-03-13 17:47:54 -06:00
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto closed;
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
c->elemalg->copy(c->elemsize, ep, sg->elem);
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2008-07-15 22:07:59 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
if(pres != nil)
|
|
|
|
*pres = true;
|
2008-07-15 22:07:59 -06:00
|
|
|
return;
|
|
|
|
}
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
if(pres != nil) {
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
*pres = false;
|
2008-07-14 15:33:39 -06:00
|
|
|
return;
|
|
|
|
}
|
2008-08-05 15:21:42 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = allocsg(c);
|
|
|
|
g->param = nil;
|
2008-07-14 15:33:39 -06:00
|
|
|
g->status = Gwaiting;
|
2008-07-26 15:21:21 -06:00
|
|
|
enqueue(&c->recvq, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2009-05-08 16:21:41 -06:00
|
|
|
gosched();
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = g->param;
|
2009-03-23 19:50:35 -06:00
|
|
|
if(sg == nil)
|
|
|
|
goto loop;
|
2009-03-13 17:47:54 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
c->elemalg->copy(c->elemsize, ep, sg->elem);
|
|
|
|
freesg(c, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-14 15:33:39 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
asynch:
|
2009-03-13 17:47:54 -06:00
|
|
|
if(c->qcount <= 0) {
|
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto closed;
|
|
|
|
|
2008-09-26 12:47:04 -06:00
|
|
|
if(pres != nil) {
|
|
|
|
unlock(&chanlock);
|
|
|
|
*pres = false;
|
|
|
|
return;
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = allocsg(c);
|
2008-07-14 18:41:38 -06:00
|
|
|
g->status = Gwaiting;
|
2008-07-26 15:21:21 -06:00
|
|
|
enqueue(&c->recvq, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2009-05-08 16:21:41 -06:00
|
|
|
gosched();
|
2009-03-13 17:47:54 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2009-03-13 17:47:54 -06:00
|
|
|
goto asynch;
|
2008-07-14 18:41:38 -06:00
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
c->elemalg->copy(c->elemsize, ep, c->recvdataq->elem);
|
2008-07-14 18:41:38 -06:00
|
|
|
c->recvdataq = c->recvdataq->link;
|
|
|
|
c->qcount--;
|
2008-07-26 15:21:21 -06:00
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
freesg(c, sg);
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2009-03-13 17:47:54 -06:00
|
|
|
if(pres != nil)
|
|
|
|
*pres = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock(&chanlock);
|
2008-09-26 12:47:04 -06:00
|
|
|
if(pres != nil)
|
|
|
|
*pres = true;
|
2009-03-13 17:47:54 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
closed:
|
|
|
|
c->elemalg->copy(c->elemsize, ep, nil);
|
|
|
|
c->closed |= Rclosed;
|
|
|
|
incerr(c);
|
|
|
|
if(pres != nil)
|
2009-03-23 19:50:35 -06:00
|
|
|
*pres = true;
|
2009-03-13 17:47:54 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-13 17:20:27 -06:00
|
|
|
}
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
// chansend1(hchan *chan any, elem any);
|
2008-07-13 17:20:27 -06:00
|
|
|
void
|
2008-07-26 15:21:21 -06:00
|
|
|
sys·chansend1(Hchan* c, ...)
|
|
|
|
{
|
|
|
|
int32 o;
|
|
|
|
byte *ae;
|
|
|
|
|
|
|
|
o = rnd(sizeof(c), c->elemsize);
|
|
|
|
ae = (byte*)&c + o;
|
2009-07-08 16:00:54 -06:00
|
|
|
chansend(c, ae, nil);
|
2008-07-26 15:21:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// chansend2(hchan *chan any, elem any) (pres bool);
|
|
|
|
void
|
|
|
|
sys·chansend2(Hchan* c, ...)
|
2008-07-13 17:20:27 -06:00
|
|
|
{
|
2008-07-26 15:21:21 -06:00
|
|
|
int32 o;
|
2008-07-14 15:33:39 -06:00
|
|
|
byte *ae, *ap;
|
2008-07-13 17:20:27 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
o = rnd(sizeof(c), c->elemsize);
|
|
|
|
ae = (byte*)&c + o;
|
2009-06-30 21:02:07 -06:00
|
|
|
o = rnd(o+c->elemsize, Structrnd);
|
2008-07-26 15:21:21 -06:00
|
|
|
ap = (byte*)&c + o;
|
2008-07-16 12:46:33 -06:00
|
|
|
|
2009-07-08 16:00:54 -06:00
|
|
|
chansend(c, ae, ap);
|
2008-07-26 15:21:21 -06:00
|
|
|
}
|
2008-07-14 18:41:38 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
// chanrecv1(hchan *chan any) (elem any);
|
|
|
|
void
|
|
|
|
sys·chanrecv1(Hchan* c, ...)
|
|
|
|
{
|
|
|
|
int32 o;
|
|
|
|
byte *ae;
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
o = rnd(sizeof(c), Structrnd);
|
2008-07-26 15:21:21 -06:00
|
|
|
ae = (byte*)&c + o;
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
chanrecv(c, ae, nil);
|
|
|
|
}
|
2008-07-14 15:33:39 -06:00
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
// chanrecv2(hchan *chan any) (elem any, pres bool);
|
|
|
|
void
|
|
|
|
sys·chanrecv2(Hchan* c, ...)
|
|
|
|
{
|
|
|
|
int32 o;
|
|
|
|
byte *ae, *ap;
|
2008-07-20 21:13:07 -06:00
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
o = rnd(sizeof(c), Structrnd);
|
2008-07-26 15:21:21 -06:00
|
|
|
ae = (byte*)&c + o;
|
|
|
|
o = rnd(o+c->elemsize, 1);
|
|
|
|
ap = (byte*)&c + o;
|
|
|
|
|
|
|
|
chanrecv(c, ae, ap);
|
|
|
|
}
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
// newselect(size uint32) (sel *byte);
|
|
|
|
void
|
2009-06-30 21:02:07 -06:00
|
|
|
sys·newselect(int32 size, ...)
|
2008-07-24 16:57:30 -06:00
|
|
|
{
|
2009-06-30 21:02:07 -06:00
|
|
|
int32 n, o;
|
|
|
|
Select **selp;
|
|
|
|
Select *sel;
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
o = rnd(sizeof(size), Structrnd);
|
|
|
|
selp = (Select**)((byte*)&size + o);
|
2008-07-24 16:57:30 -06:00
|
|
|
n = 0;
|
|
|
|
if(size > 1)
|
|
|
|
n = size-1;
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
sel = nil;
|
|
|
|
if(size >= 1 && size < nelem(selfree)) {
|
|
|
|
sel = selfree[size];
|
|
|
|
if(sel != nil)
|
|
|
|
selfree[size] = sel->link;
|
|
|
|
}
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
if(sel == nil)
|
|
|
|
sel = mal(sizeof(*sel) + n*sizeof(sel->scase[0]));
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
sel->tcase = size;
|
|
|
|
sel->ncase = 0;
|
2009-06-30 21:02:07 -06:00
|
|
|
*selp = sel;
|
2008-07-24 16:57:30 -06:00
|
|
|
if(debug) {
|
|
|
|
prints("newselect s=");
|
|
|
|
sys·printpointer(sel);
|
2009-01-24 16:58:44 -07:00
|
|
|
prints(" size=");
|
2008-11-06 18:50:28 -07:00
|
|
|
sys·printint(size);
|
2008-07-24 16:57:30 -06:00
|
|
|
prints("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectsend(sel *byte, hchan *chan any, elem any) (selected bool);
|
|
|
|
void
|
|
|
|
sys·selectsend(Select *sel, Hchan *c, ...)
|
|
|
|
{
|
|
|
|
int32 i, eo;
|
|
|
|
Scase *cas;
|
2008-07-25 18:03:27 -06:00
|
|
|
byte *ae;
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
// nil cases do not compete
|
2008-07-24 16:57:30 -06:00
|
|
|
if(c == nil)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
|
|
|
throw("selectsend: too many cases");
|
|
|
|
sel->ncase = i+1;
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[i];
|
|
|
|
if(cas == nil) {
|
|
|
|
cas = mal(sizeof *cas + c->elemsize - sizeof(cas->u.elem));
|
|
|
|
sel->scase[i] = cas;
|
|
|
|
}
|
2008-07-24 16:57:30 -06:00
|
|
|
|
|
|
|
cas->pc = sys·getcallerpc(&sel);
|
|
|
|
cas->chan = c;
|
|
|
|
|
|
|
|
eo = rnd(sizeof(sel), sizeof(c));
|
|
|
|
eo = rnd(eo+sizeof(c), c->elemsize);
|
2009-06-30 21:02:07 -06:00
|
|
|
cas->so = rnd(eo+c->elemsize, Structrnd);
|
2008-07-24 16:57:30 -06:00
|
|
|
cas->send = 1;
|
|
|
|
|
|
|
|
ae = (byte*)&sel + eo;
|
2008-07-25 12:58:26 -06:00
|
|
|
c->elemalg->copy(c->elemsize, cas->u.elem, ae);
|
2008-07-24 16:57:30 -06:00
|
|
|
|
|
|
|
if(debug) {
|
2009-01-24 16:58:44 -07:00
|
|
|
prints("selectsend s=");
|
2008-07-24 16:57:30 -06:00
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" pc=");
|
|
|
|
sys·printpointer(cas->pc);
|
|
|
|
prints(" chan=");
|
|
|
|
sys·printpointer(cas->chan);
|
|
|
|
prints(" po=");
|
|
|
|
sys·printint(cas->so);
|
|
|
|
prints(" send=");
|
|
|
|
sys·printint(cas->send);
|
|
|
|
prints("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
|
|
|
|
void
|
|
|
|
sys·selectrecv(Select *sel, Hchan *c, ...)
|
|
|
|
{
|
2008-07-26 15:21:21 -06:00
|
|
|
int32 i, eo;
|
2008-07-25 12:58:26 -06:00
|
|
|
Scase *cas;
|
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
// nil cases do not compete
|
2008-07-25 12:58:26 -06:00
|
|
|
if(c == nil)
|
|
|
|
return;
|
|
|
|
|
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
2008-11-05 19:04:24 -07:00
|
|
|
throw("selectrecv: too many cases");
|
2008-07-25 12:58:26 -06:00
|
|
|
sel->ncase = i+1;
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[i];
|
|
|
|
if(cas == nil) {
|
|
|
|
cas = mal(sizeof *cas);
|
|
|
|
sel->scase[i] = cas;
|
|
|
|
}
|
2008-07-25 12:58:26 -06:00
|
|
|
cas->pc = sys·getcallerpc(&sel);
|
|
|
|
cas->chan = c;
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
eo = rnd(sizeof(sel), sizeof(c));
|
|
|
|
eo = rnd(eo+sizeof(c), sizeof(byte*));
|
2009-06-30 21:02:07 -06:00
|
|
|
cas->so = rnd(eo+sizeof(byte*), Structrnd);
|
2008-07-25 12:58:26 -06:00
|
|
|
cas->send = 0;
|
2008-07-26 15:21:21 -06:00
|
|
|
cas->u.elemp = *(byte**)((byte*)&sel + eo);
|
2008-07-25 12:58:26 -06:00
|
|
|
|
|
|
|
if(debug) {
|
2009-01-24 16:58:44 -07:00
|
|
|
prints("selectrecv s=");
|
2008-07-25 12:58:26 -06:00
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" pc=");
|
|
|
|
sys·printpointer(cas->pc);
|
|
|
|
prints(" chan=");
|
|
|
|
sys·printpointer(cas->chan);
|
|
|
|
prints(" so=");
|
|
|
|
sys·printint(cas->so);
|
|
|
|
prints(" send=");
|
|
|
|
sys·printint(cas->send);
|
|
|
|
prints("\n");
|
|
|
|
}
|
2008-07-24 16:57:30 -06:00
|
|
|
}
|
|
|
|
|
2008-11-05 22:50:28 -07:00
|
|
|
|
2009-01-24 16:58:44 -07:00
|
|
|
// selectdefaul(sel *byte) (selected bool);
|
2008-11-05 19:04:24 -07:00
|
|
|
void
|
2008-11-05 22:50:28 -07:00
|
|
|
sys·selectdefault(Select *sel, ...)
|
2008-11-05 19:04:24 -07:00
|
|
|
{
|
2008-11-05 22:50:28 -07:00
|
|
|
int32 i;
|
2008-11-05 19:04:24 -07:00
|
|
|
Scase *cas;
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-11-05 19:04:24 -07:00
|
|
|
i = sel->ncase;
|
|
|
|
if(i >= sel->tcase)
|
|
|
|
throw("selectdefault: too many cases");
|
|
|
|
sel->ncase = i+1;
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[i];
|
|
|
|
if(cas == nil) {
|
|
|
|
cas = mal(sizeof *cas);
|
|
|
|
sel->scase[i] = cas;
|
|
|
|
}
|
2008-11-05 19:04:24 -07:00
|
|
|
cas->pc = sys·getcallerpc(&sel);
|
2008-11-05 22:50:28 -07:00
|
|
|
cas->chan = nil;
|
2008-11-05 19:04:24 -07:00
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
cas->so = rnd(sizeof(sel), Structrnd);
|
2008-11-05 19:04:24 -07:00
|
|
|
cas->send = 2;
|
2008-11-05 22:50:28 -07:00
|
|
|
cas->u.elemp = nil;
|
2008-11-05 19:04:24 -07:00
|
|
|
|
|
|
|
if(debug) {
|
2009-01-24 16:58:44 -07:00
|
|
|
prints("selectdefault s=");
|
2008-11-05 19:04:24 -07:00
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" pc=");
|
|
|
|
sys·printpointer(cas->pc);
|
|
|
|
prints(" so=");
|
|
|
|
sys·printint(cas->so);
|
|
|
|
prints(" send=");
|
|
|
|
sys·printint(cas->send);
|
|
|
|
prints("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
// selectgo(sel *byte);
|
|
|
|
void
|
|
|
|
sys·selectgo(Select *sel)
|
|
|
|
{
|
|
|
|
uint32 p, o, i;
|
2008-11-05 18:57:18 -07:00
|
|
|
Scase *cas, *dfl;
|
2008-07-24 16:57:30 -06:00
|
|
|
Hchan *c;
|
2008-07-25 16:55:12 -06:00
|
|
|
SudoG *sg;
|
|
|
|
G *gp;
|
2008-08-05 15:18:47 -06:00
|
|
|
byte *as;
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2009-01-27 14:23:28 -07:00
|
|
|
if(debug) {
|
2008-07-25 16:55:12 -06:00
|
|
|
prints("selectgo: sel=");
|
|
|
|
sys·printpointer(sel);
|
|
|
|
prints("\n");
|
|
|
|
}
|
2008-07-25 12:58:26 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
if(sel->ncase < 2) {
|
|
|
|
if(sel->ncase < 1)
|
|
|
|
throw("selectgo: no cases");
|
|
|
|
// make special case of one.
|
2008-07-24 16:57:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// select a (relative) prime
|
|
|
|
for(i=0;; i++) {
|
|
|
|
p = fastrand1();
|
|
|
|
if(gcd(p, sel->ncase) == 1)
|
|
|
|
break;
|
|
|
|
if(i > 1000) {
|
|
|
|
throw("selectgo: failed to select prime");
|
|
|
|
}
|
|
|
|
}
|
2008-07-25 16:55:12 -06:00
|
|
|
|
|
|
|
// select an initial offset
|
2008-07-24 16:57:30 -06:00
|
|
|
o = fastrand2();
|
|
|
|
|
|
|
|
p %= sel->ncase;
|
|
|
|
o %= sel->ncase;
|
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
|
|
|
|
2009-01-24 16:58:44 -07:00
|
|
|
loop:
|
2008-07-25 16:55:12 -06:00
|
|
|
// pass 1 - look for something already waiting
|
2008-11-05 18:57:18 -07:00
|
|
|
dfl = nil;
|
2008-07-24 16:57:30 -06:00
|
|
|
for(i=0; i<sel->ncase; i++) {
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[o];
|
2008-11-06 18:50:28 -07:00
|
|
|
|
2008-11-05 18:57:18 -07:00
|
|
|
if(cas->send == 2) { // default
|
|
|
|
dfl = cas;
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next1;
|
2008-11-05 18:57:18 -07:00
|
|
|
}
|
2008-11-06 18:50:28 -07:00
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
c = cas->chan;
|
2008-07-25 16:55:12 -06:00
|
|
|
if(c->dataqsiz > 0) {
|
2008-07-26 15:21:21 -06:00
|
|
|
if(cas->send) {
|
2009-03-23 19:50:35 -06:00
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto sclose;
|
2008-07-26 15:21:21 -06:00
|
|
|
if(c->qcount < c->dataqsiz)
|
|
|
|
goto asyns;
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next1;
|
2008-07-26 15:21:21 -06:00
|
|
|
}
|
2008-11-06 18:50:28 -07:00
|
|
|
if(c->qcount > 0)
|
|
|
|
goto asynr;
|
2009-03-13 17:47:54 -06:00
|
|
|
if(c->closed & Wclosed)
|
2009-03-23 19:50:35 -06:00
|
|
|
goto rclose;
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next1;
|
|
|
|
}
|
2008-07-25 12:58:26 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
if(cas->send) {
|
2009-03-23 19:50:35 -06:00
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto sclose;
|
2008-07-25 16:55:12 -06:00
|
|
|
sg = dequeue(&c->recvq, c);
|
|
|
|
if(sg != nil)
|
2008-07-26 15:21:21 -06:00
|
|
|
goto gots;
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next1;
|
2008-07-25 16:55:12 -06:00
|
|
|
}
|
2008-11-06 18:50:28 -07:00
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg != nil)
|
|
|
|
goto gotr;
|
2009-03-13 17:47:54 -06:00
|
|
|
if(c->closed & Wclosed)
|
2009-03-23 19:50:35 -06:00
|
|
|
goto rclose;
|
2008-07-25 12:58:26 -06:00
|
|
|
|
2008-11-06 18:50:28 -07:00
|
|
|
next1:
|
2008-07-25 16:55:12 -06:00
|
|
|
o += p;
|
|
|
|
if(o >= sel->ncase)
|
|
|
|
o -= sel->ncase;
|
|
|
|
}
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-11-05 18:57:18 -07:00
|
|
|
if(dfl != nil) {
|
|
|
|
cas = dfl;
|
|
|
|
goto retc;
|
|
|
|
}
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-07-25 12:58:26 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
// pass 2 - enqueue on all chans
|
|
|
|
for(i=0; i<sel->ncase; i++) {
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[o];
|
2008-07-25 16:55:12 -06:00
|
|
|
c = cas->chan;
|
2008-07-26 15:21:21 -06:00
|
|
|
|
|
|
|
if(c->dataqsiz > 0) {
|
|
|
|
if(cas->send) {
|
|
|
|
if(c->qcount < c->dataqsiz) {
|
2008-09-19 21:43:30 -06:00
|
|
|
prints("selectgo: pass 2 async send\n");
|
2008-07-26 15:21:21 -06:00
|
|
|
goto asyns;
|
|
|
|
}
|
2008-09-19 21:43:30 -06:00
|
|
|
sg = allocsg(c);
|
|
|
|
sg->offset = o;
|
|
|
|
enqueue(&c->sendq, sg);
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next2;
|
|
|
|
}
|
|
|
|
if(c->qcount > 0) {
|
|
|
|
prints("selectgo: pass 2 async recv\n");
|
|
|
|
goto asynr;
|
2008-07-26 15:21:21 -06:00
|
|
|
}
|
2008-11-06 18:50:28 -07:00
|
|
|
sg = allocsg(c);
|
|
|
|
sg->offset = o;
|
|
|
|
enqueue(&c->recvq, sg);
|
|
|
|
goto next2;
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
if(cas->send) {
|
|
|
|
sg = dequeue(&c->recvq, c);
|
2008-07-26 15:21:21 -06:00
|
|
|
if(sg != nil) {
|
2008-09-19 21:43:30 -06:00
|
|
|
prints("selectgo: pass 2 sync send\n");
|
2008-07-26 15:21:21 -06:00
|
|
|
g->selgen++;
|
2008-09-19 21:43:30 -06:00
|
|
|
goto gots;
|
2008-07-26 15:21:21 -06:00
|
|
|
}
|
2008-07-25 16:55:12 -06:00
|
|
|
sg = allocsg(c);
|
|
|
|
sg->offset = o;
|
|
|
|
c->elemalg->copy(c->elemsize, sg->elem, cas->u.elem);
|
|
|
|
enqueue(&c->sendq, sg);
|
2008-11-06 18:50:28 -07:00
|
|
|
goto next2;
|
2008-07-24 16:57:30 -06:00
|
|
|
}
|
2008-11-06 18:50:28 -07:00
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
prints("selectgo: pass 2 sync recv\n");
|
|
|
|
g->selgen++;
|
|
|
|
goto gotr;
|
|
|
|
}
|
|
|
|
sg = allocsg(c);
|
|
|
|
sg->offset = o;
|
|
|
|
enqueue(&c->recvq, sg);
|
2008-07-24 16:57:30 -06:00
|
|
|
|
2008-11-06 18:50:28 -07:00
|
|
|
next2:
|
2008-07-24 16:57:30 -06:00
|
|
|
o += p;
|
|
|
|
if(o >= sel->ncase)
|
|
|
|
o -= sel->ncase;
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:58:44 -07:00
|
|
|
g->param = nil;
|
2008-07-25 16:55:12 -06:00
|
|
|
g->status = Gwaiting;
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2009-05-08 16:21:41 -06:00
|
|
|
gosched();
|
2008-07-25 16:55:12 -06:00
|
|
|
|
2008-09-09 12:50:14 -06:00
|
|
|
lock(&chanlock);
|
2008-07-25 16:55:12 -06:00
|
|
|
sg = g->param;
|
2009-01-24 16:58:44 -07:00
|
|
|
if(sg == nil)
|
|
|
|
goto loop;
|
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
o = sg->offset;
|
2008-12-09 17:16:07 -07:00
|
|
|
cas = sel->scase[o];
|
2008-07-25 16:55:12 -06:00
|
|
|
c = cas->chan;
|
|
|
|
|
2009-01-24 16:58:44 -07:00
|
|
|
if(c->dataqsiz > 0) {
|
|
|
|
// prints("shouldnt happen\n");
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
2009-01-27 14:23:28 -07:00
|
|
|
if(debug) {
|
2008-07-26 15:21:21 -06:00
|
|
|
prints("wait-return: sel=");
|
2008-07-25 16:55:12 -06:00
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" c=");
|
|
|
|
sys·printpointer(c);
|
2008-07-26 15:21:21 -06:00
|
|
|
prints(" cas=");
|
|
|
|
sys·printpointer(cas);
|
|
|
|
prints(" send=");
|
|
|
|
sys·printint(cas->send);
|
2008-07-25 16:55:12 -06:00
|
|
|
prints(" o=");
|
|
|
|
sys·printint(o);
|
|
|
|
prints("\n");
|
|
|
|
}
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
if(!cas->send) {
|
|
|
|
if(cas->u.elemp != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, cas->u.elemp, sg->elem);
|
|
|
|
}
|
|
|
|
|
|
|
|
freesg(c, sg);
|
|
|
|
goto retc;
|
|
|
|
|
|
|
|
asynr:
|
2008-09-19 21:43:30 -06:00
|
|
|
if(cas->u.elemp != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, cas->u.elemp, c->recvdataq->elem);
|
|
|
|
c->recvdataq = c->recvdataq->link;
|
|
|
|
c->qcount--;
|
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
freesg(c, sg);
|
|
|
|
ready(gp);
|
|
|
|
}
|
|
|
|
goto retc;
|
|
|
|
|
2008-07-26 15:21:21 -06:00
|
|
|
asyns:
|
2008-09-19 21:43:30 -06:00
|
|
|
if(cas->u.elem != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, c->senddataq->elem, cas->u.elem);
|
|
|
|
c->senddataq = c->senddataq->link;
|
|
|
|
c->qcount++;
|
|
|
|
sg = dequeue(&c->recvq, c);
|
|
|
|
if(sg != nil) {
|
|
|
|
gp = sg->g;
|
|
|
|
freesg(c, sg);
|
|
|
|
ready(gp);
|
|
|
|
}
|
|
|
|
goto retc;
|
2008-08-05 15:18:47 -06:00
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
gotr:
|
2008-07-26 15:21:21 -06:00
|
|
|
// recv path to wakeup the sender (sg)
|
2009-01-27 14:23:28 -07:00
|
|
|
if(debug) {
|
2008-07-25 16:55:12 -06:00
|
|
|
prints("gotr: sel=");
|
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" c=");
|
|
|
|
sys·printpointer(c);
|
|
|
|
prints(" o=");
|
|
|
|
sys·printint(o);
|
|
|
|
prints("\n");
|
|
|
|
}
|
2008-07-26 15:21:21 -06:00
|
|
|
if(cas->u.elemp != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, cas->u.elemp, sg->elem);
|
2008-07-25 16:55:12 -06:00
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2008-07-25 16:55:12 -06:00
|
|
|
goto retc;
|
|
|
|
|
2009-03-23 19:50:35 -06:00
|
|
|
rclose:
|
|
|
|
if(cas->u.elemp != nil)
|
|
|
|
c->elemalg->copy(c->elemsize, cas->u.elemp, nil);
|
|
|
|
c->closed |= Rclosed;
|
|
|
|
incerr(c);
|
|
|
|
goto retc;
|
|
|
|
|
2008-07-25 16:55:12 -06:00
|
|
|
gots:
|
2008-07-26 15:21:21 -06:00
|
|
|
// send path to wakeup the receiver (sg)
|
2009-01-27 14:23:28 -07:00
|
|
|
if(debug) {
|
2008-07-25 16:55:12 -06:00
|
|
|
prints("gots: sel=");
|
|
|
|
sys·printpointer(sel);
|
|
|
|
prints(" c=");
|
|
|
|
sys·printpointer(c);
|
|
|
|
prints(" o=");
|
|
|
|
sys·printint(o);
|
|
|
|
prints("\n");
|
|
|
|
}
|
2009-03-23 19:50:35 -06:00
|
|
|
if(c->closed & Wclosed)
|
|
|
|
goto sclose;
|
2008-07-26 15:21:21 -06:00
|
|
|
c->elemalg->copy(c->elemsize, sg->elem, cas->u.elem);
|
2008-07-25 16:55:12 -06:00
|
|
|
gp = sg->g;
|
|
|
|
gp->param = sg;
|
2008-08-04 17:43:49 -06:00
|
|
|
ready(gp);
|
2009-03-23 19:50:35 -06:00
|
|
|
goto retc;
|
|
|
|
|
|
|
|
sclose:
|
|
|
|
incerr(c);
|
|
|
|
goto retc;
|
2008-07-24 16:57:30 -06:00
|
|
|
|
|
|
|
retc:
|
2008-07-26 15:21:21 -06:00
|
|
|
if(sel->ncase >= 1 && sel->ncase < nelem(selfree)) {
|
|
|
|
sel->link = selfree[sel->ncase];
|
|
|
|
selfree[sel->ncase] = sel;
|
|
|
|
}
|
2008-09-09 12:50:14 -06:00
|
|
|
unlock(&chanlock);
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
sys·setcallerpc(&sel, cas->pc);
|
|
|
|
as = (byte*)&sel + cas->so;
|
|
|
|
*as = true;
|
|
|
|
}
|
|
|
|
|
2009-03-12 18:55:11 -06:00
|
|
|
// closechan(sel *byte);
|
|
|
|
void
|
|
|
|
sys·closechan(Hchan *c)
|
|
|
|
{
|
2009-03-13 17:47:54 -06:00
|
|
|
SudoG *sg;
|
|
|
|
G* gp;
|
2009-03-12 18:55:11 -06:00
|
|
|
|
2009-03-13 17:47:54 -06:00
|
|
|
lock(&chanlock);
|
|
|
|
incerr(c);
|
2009-03-12 18:55:11 -06:00
|
|
|
c->closed |= Wclosed;
|
2009-03-13 17:47:54 -06:00
|
|
|
|
|
|
|
// release all readers
|
|
|
|
for(;;) {
|
|
|
|
sg = dequeue(&c->recvq, c);
|
|
|
|
if(sg == nil)
|
|
|
|
break;
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = nil;
|
|
|
|
freesg(c, sg);
|
|
|
|
ready(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// release all writers
|
|
|
|
for(;;) {
|
|
|
|
sg = dequeue(&c->sendq, c);
|
|
|
|
if(sg == nil)
|
|
|
|
break;
|
|
|
|
gp = sg->g;
|
|
|
|
gp->param = nil;
|
|
|
|
freesg(c, sg);
|
|
|
|
ready(gp);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock(&chanlock);
|
2009-03-12 18:55:11 -06:00
|
|
|
}
|
|
|
|
|
2009-08-26 11:47:18 -06:00
|
|
|
void
|
|
|
|
chanclose(Hchan *c)
|
|
|
|
{
|
|
|
|
sys·closechan(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
chanclosed(Hchan *c)
|
|
|
|
{
|
|
|
|
return (c->closed & Rclosed) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-12 18:55:11 -06:00
|
|
|
// closedchan(sel *byte) bool;
|
|
|
|
void
|
|
|
|
sys·closedchan(Hchan *c, bool closed)
|
|
|
|
{
|
2009-08-26 11:47:18 -06:00
|
|
|
closed = chanclosed(c);
|
2009-03-12 18:55:11 -06:00
|
|
|
FLUSH(&closed);
|
|
|
|
}
|
|
|
|
|
2008-07-20 21:13:07 -06:00
|
|
|
static SudoG*
|
|
|
|
dequeue(WaitQ *q, Hchan *c)
|
|
|
|
{
|
|
|
|
SudoG *sgp;
|
|
|
|
|
|
|
|
loop:
|
|
|
|
sgp = q->first;
|
|
|
|
if(sgp == nil)
|
|
|
|
return nil;
|
|
|
|
q->first = sgp->link;
|
|
|
|
|
|
|
|
// if sgp is stale, ignore it
|
|
|
|
if(sgp->selgen != sgp->g->selgen) {
|
2008-07-25 16:55:12 -06:00
|
|
|
//prints("INVALID PSEUDOG POINTER\n");
|
2008-07-20 21:13:07 -06:00
|
|
|
freesg(c, sgp);
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalidate any others
|
|
|
|
sgp->g->selgen++;
|
|
|
|
return sgp;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SudoG*
|
|
|
|
allocsg(Hchan *c)
|
|
|
|
{
|
|
|
|
SudoG* sg;
|
|
|
|
|
|
|
|
sg = c->free;
|
|
|
|
if(sg != nil) {
|
|
|
|
c->free = sg->link;
|
|
|
|
} else
|
2008-12-09 17:16:07 -07:00
|
|
|
sg = mal(sizeof(*sg) + c->elemsize - sizeof(sg->elem));
|
2008-07-20 21:13:07 -06:00
|
|
|
sg->selgen = g->selgen;
|
|
|
|
sg->g = g;
|
2008-07-26 15:21:21 -06:00
|
|
|
sg->offset = 0;
|
2009-01-24 16:58:44 -07:00
|
|
|
sg->isfree = 0;
|
2008-07-26 15:21:21 -06:00
|
|
|
|
2008-07-20 21:13:07 -06:00
|
|
|
return sg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
freesg(Hchan *c, SudoG *sg)
|
|
|
|
{
|
2009-03-13 17:47:54 -06:00
|
|
|
if(sg != nil) {
|
|
|
|
if(sg->isfree)
|
|
|
|
throw("chan.freesg: already free");
|
|
|
|
sg->isfree = 1;
|
|
|
|
sg->link = c->free;
|
|
|
|
c->free = sg;
|
|
|
|
}
|
2008-07-20 21:13:07 -06:00
|
|
|
}
|
2008-07-24 16:57:30 -06:00
|
|
|
|
|
|
|
static uint32
|
|
|
|
gcd(uint32 u, uint32 v)
|
|
|
|
{
|
|
|
|
for(;;) {
|
|
|
|
if(u > v) {
|
|
|
|
if(v == 0)
|
|
|
|
return u;
|
|
|
|
u = u%v;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(u == 0)
|
|
|
|
return v;
|
|
|
|
v = v%u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
fastrand1(void)
|
|
|
|
{
|
|
|
|
static uint32 x = 0x49f6428aUL;
|
|
|
|
|
|
|
|
x += x;
|
|
|
|
if(x & 0x80000000L)
|
|
|
|
x ^= 0x88888eefUL;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
fastrand2(void)
|
|
|
|
{
|
|
|
|
static uint32 x = 0x49f6428aUL;
|
|
|
|
|
|
|
|
x += x;
|
|
|
|
if(x & 0x80000000L)
|
|
|
|
x ^= 0xfafd871bUL;
|
|
|
|
return x;
|
|
|
|
}
|