1 #include "threadimpl.h"
4 * One can go through a lot of effort to avoid this global lock.
5 * You have to put locks in all the channels and all the Alt
6 * structures. At the beginning of an alt you have to lock all
7 * the channels, but then to try to actually exec an op you
8 * have to lock the other guy's alt structure, so that other
9 * people aren't trying to use him in some other op at the
12 * For Plan 9 apps, it's just not worth the extra effort.
14 static QLock chanlock;
17 chancreate(int elemsize, int bufsize)
21 c = malloc(sizeof *c+bufsize*elemsize);
23 sysfatal("chancreate malloc: %r");
24 memset(c, 0, sizeof *c);
25 c->elemsize = elemsize;
28 c->buf = (uchar*)(c+1);
33 chansetname(Channel *c, char *fmt, ...)
39 name = vsmprint(fmt, arg);
45 /* bug - work out races */
58 addarray(_Altarray *a, Alt *alt)
62 a->a = realloc(a->a, a->m*sizeof a->a[0]);
68 delarray(_Altarray *a, int i)
75 * doesn't really work for things other than CHANSND and CHANRCV
76 * but is only used as arg to chanarray, which can handle it
78 #define otherop(op) (CHANSND+CHANRCV-(op))
81 chanarray(Channel *c, uint op)
103 ar = chanarray(c, otherop(a->op));
110 return c->nbuf < c->bufsize;
122 ar = chanarray(a->c, a->op);
132 ar = chanarray(a->c, a->op);
134 fprint(2, "bad use of altdequeue op=%d\n", a->op);
138 for(i=0; i<ar->n; i++)
143 fprint(2, "cannot find self in altdq\n");
148 altalldequeue(Alt *a)
152 for(i=0; a[i].op!=CHANEND && a[i].op!=CHANNOBLK; i++)
153 if(a[i].op != CHANNOP)
158 amove(void *dst, void *src, uint n)
164 memmove(dst, src, n);
169 * Actually move the data around. There are up to three
170 * players: the sender, the receiver, and the channel itself.
171 * If the channel is unbuffered or the buffer is empty,
172 * data goes from sender to receiver. If the channel is full,
173 * the receiver removes some from the channel and the sender
174 * gets to put some in.
177 altcopy(Alt *s, Alt *r)
184 * Work out who is sender and who is receiver
186 if(s == nil && r == nil)
190 if(s->op == CHANRCV){
195 assert(s==nil || s->op == CHANSND);
196 assert(r==nil || r->op == CHANRCV);
199 * Channel is empty (or unbuffered) - copy directly.
201 if(s && r && c->nbuf == 0){
202 amove(r->v, s->v, c->elemsize);
207 * Otherwise it's always okay to receive and then send.
210 cp = c->buf + c->off*c->elemsize;
211 amove(r->v, cp, c->elemsize);
213 if(++c->off == c->bufsize)
217 cp = c->buf + (c->off+c->nbuf)%c->bufsize*c->elemsize;
218 amove(cp, s->v, c->elemsize);
232 ar = chanarray(c, otherop(a->op));
237 altalldequeue(other->xalt);
238 other->xalt[0].xalt = other;
239 _threadready(other->thread);
248 int i, j, ncan, n, canblock;
253 for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++)
256 canblock = a[i].op == CHANEND;
264 if(dbgalt) print("alt ");
268 if(dbgalt) print(" %c:", "esrnb"[a[i].op]);
269 if(dbgalt) if(c->name) print("%s", c->name); else print("%p", c);
270 if(altcanexec(&a[i])){
271 if(dbgalt) print("*");
278 if(altcanexec(&a[i])){
282 print(" => %c:", "esrnb"[a[i].op]);
283 if(c->name) print("%s", c->name); else print("%p", c);
293 if(dbgalt)print("\n");
301 if(a[i].op != CHANNOP)
309 * the guy who ran the op took care of dequeueing us
310 * and then set a[0].alt to the one that was executed.
312 return a[0].xalt - a;
316 _chanop(Channel *c, int op, void *p, int canblock)
323 a[1].op = canblock ? CHANEND : CHANNOBLK;
330 chansend(Channel *c, void *v)
332 return _chanop(c, CHANSND, v, 1);
336 channbsend(Channel *c, void *v)
338 return _chanop(c, CHANSND, v, 0);
342 chanrecv(Channel *c, void *v)
344 return _chanop(c, CHANRCV, v, 1);
348 channbrecv(Channel *c, void *v)
350 return _chanop(c, CHANRCV, v, 0);
354 chansendp(Channel *c, void *v)
356 return _chanop(c, CHANSND, (void*)&v, 1);
360 chanrecvp(Channel *c)
364 _chanop(c, CHANRCV, (void*)&v, 1);
369 channbsendp(Channel *c, void *v)
371 return _chanop(c, CHANSND, (void*)&v, 0);
375 channbrecvp(Channel *c)
379 _chanop(c, CHANRCV, (void*)&v, 0);
384 chansendul(Channel *c, ulong val)
386 return _chanop(c, CHANSND, &val, 1);
390 chanrecvul(Channel *c)
394 _chanop(c, CHANRCV, &val, 1);
399 channbsendul(Channel *c, ulong val)
401 return _chanop(c, CHANSND, &val, 0);
405 channbrecvul(Channel *c)
409 _chanop(c, CHANRCV, &val, 0);