4 #include "threadimpl.h"
7 * One can go through a lot of effort to avoid this global lock.
8 * You have to put locks in all the channels and all the Alt
9 * structures. At the beginning of an alt you have to lock all
10 * the channels, but then to try to actually exec an op you
11 * have to lock the other guy's alt structure, so that other
12 * people aren't trying to use him in some other op at the
15 * For Plan 9 apps, it's just not worth the extra effort.
17 static QLock chanlock;
20 chancreate(int elemsize, int bufsize)
24 c = malloc(sizeof *c+bufsize*elemsize);
26 sysfatal("chancreate malloc: %r");
27 memset(c, 0, sizeof *c);
28 c->elemsize = elemsize;
31 c->buf = (uchar*)(c+1);
36 chansetname(Channel *c, char *fmt, ...)
42 name = vsmprint(fmt, arg);
48 /* bug - work out races */
61 addarray(_Altarray *a, Alt *alt)
65 a->a = realloc(a->a, a->m*sizeof a->a[0]);
71 delarray(_Altarray *a, int i)
78 * doesn't really work for things other than CHANSND and CHANRCV
79 * but is only used as arg to chanarray, which can handle it
81 #define otherop(op) (CHANSND+CHANRCV-(op))
84 chanarray(Channel *c, uint op)
106 ar = chanarray(c, otherop(a->op));
113 return c->nbuf < c->bufsize;
125 ar = chanarray(a->c, a->op);
135 ar = chanarray(a->c, a->op);
137 fprint(2, "bad use of altdequeue op=%d\n", a->op);
141 for(i=0; i<ar->n; i++)
146 fprint(2, "cannot find self in altdq\n");
151 altalldequeue(Alt *a)
155 for(i=0; a[i].op!=CHANEND && a[i].op!=CHANNOBLK; i++)
156 if(a[i].op != CHANNOP)
161 amove(void *dst, void *src, uint n)
167 memmove(dst, src, n);
172 * Actually move the data around. There are up to three
173 * players: the sender, the receiver, and the channel itself.
174 * If the channel is unbuffered or the buffer is empty,
175 * data goes from sender to receiver. If the channel is full,
176 * the receiver removes some from the channel and the sender
177 * gets to put some in.
180 altcopy(Alt *s, Alt *r)
187 * Work out who is sender and who is receiver
189 if(s == nil && r == nil)
193 if(s->op == CHANRCV){
198 assert(s==nil || s->op == CHANSND);
199 assert(r==nil || r->op == CHANRCV);
202 * Channel is empty (or unbuffered) - copy directly.
204 if(s && r && c->nbuf == 0){
205 amove(r->v, s->v, c->elemsize);
210 * Otherwise it's always okay to receive and then send.
213 cp = c->buf + c->off*c->elemsize;
214 amove(r->v, cp, c->elemsize);
216 if(++c->off == c->bufsize)
220 cp = c->buf + (c->off+c->nbuf)%c->bufsize*c->elemsize;
221 amove(cp, s->v, c->elemsize);
235 ar = chanarray(c, otherop(a->op));
240 altalldequeue(other->xalt);
241 other->xalt[0].xalt = other;
242 _threadready(other->thread);
251 int i, j, ncan, n, canblock;
255 for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++)
258 canblock = a[i].op == CHANEND;
266 if(dbgalt) print("alt ");
270 if(dbgalt) print(" %c:", "esrnb"[a[i].op]);
271 if(dbgalt) if(c->name) print("%s", c->name); else print("%p", c);
272 if(altcanexec(&a[i])){
273 if(dbgalt) print("*");
280 if(altcanexec(&a[i])){
284 print(" => %c:", "esrnb"[a[i].op]);
285 if(c->name) print("%s", c->name); else print("%p", c);
295 if(dbgalt)print("\n");
303 if(a[i].op != CHANNOP)
311 * the guy who ran the op took care of dequeueing us
312 * and then set a[0].alt to the one that was executed.
314 return a[0].xalt - a;
318 _chanop(Channel *c, int op, void *p, int canblock)
325 a[1].op = canblock ? CHANEND : CHANNOBLK;
332 chansend(Channel *c, void *v)
334 return _chanop(c, CHANSND, v, 1);
338 channbsend(Channel *c, void *v)
340 return _chanop(c, CHANSND, v, 0);
344 chanrecv(Channel *c, void *v)
346 return _chanop(c, CHANRCV, v, 1);
350 channbrecv(Channel *c, void *v)
352 return _chanop(c, CHANRCV, v, 0);
356 chansendp(Channel *c, void *v)
358 return _chanop(c, CHANSND, (void*)&v, 1);
362 chanrecvp(Channel *c)
366 _chanop(c, CHANRCV, (void*)&v, 1);
371 channbsendp(Channel *c, void *v)
373 return _chanop(c, CHANSND, (void*)&v, 0);
377 channbrecvp(Channel *c)
381 _chanop(c, CHANRCV, (void*)&v, 0);
386 chansendul(Channel *c, ulong val)
388 return _chanop(c, CHANSND, &val, 1);
392 chanrecvul(Channel *c)
396 _chanop(c, CHANRCV, &val, 1);
401 channbsendul(Channel *c, ulong val)
403 return _chanop(c, CHANSND, &val, 0);
407 channbrecvul(Channel *c)
411 _chanop(c, CHANRCV, &val, 0);