1 619085f0 2004-12-25 devnull #include "threadimpl.h"
4 619085f0 2004-12-25 devnull * One can go through a lot of effort to avoid this global lock.
5 619085f0 2004-12-25 devnull * You have to put locks in all the channels and all the Alt
6 619085f0 2004-12-25 devnull * structures. At the beginning of an alt you have to lock all
7 619085f0 2004-12-25 devnull * the channels, but then to try to actually exec an op you
8 619085f0 2004-12-25 devnull * have to lock the other guy's alt structure, so that other
9 619085f0 2004-12-25 devnull * people aren't trying to use him in some other op at the
10 619085f0 2004-12-25 devnull * same time.
12 619085f0 2004-12-25 devnull * For Plan 9 apps, it's just not worth the extra effort.
14 619085f0 2004-12-25 devnull static QLock chanlock;
17 619085f0 2004-12-25 devnull chancreate(int elemsize, int bufsize)
19 619085f0 2004-12-25 devnull Channel *c;
21 619085f0 2004-12-25 devnull c = malloc(sizeof *c+bufsize*elemsize);
22 73722a8b 2004-12-27 devnull if(c == nil)
23 73722a8b 2004-12-27 devnull sysfatal("chancreate malloc: %r");
24 619085f0 2004-12-25 devnull memset(c, 0, sizeof *c);
25 619085f0 2004-12-25 devnull c->elemsize = elemsize;
26 619085f0 2004-12-25 devnull c->bufsize = bufsize;
27 619085f0 2004-12-25 devnull c->nbuf = 0;
28 619085f0 2004-12-25 devnull c->buf = (uchar*)(c+1);
29 619085f0 2004-12-25 devnull return c;
33 619085f0 2004-12-25 devnull chansetname(Channel *c, char *fmt, ...)
35 619085f0 2004-12-25 devnull char *name;
36 619085f0 2004-12-25 devnull va_list arg;
38 619085f0 2004-12-25 devnull va_start(arg, fmt);
39 619085f0 2004-12-25 devnull name = vsmprint(fmt, arg);
40 619085f0 2004-12-25 devnull va_end(arg);
41 619085f0 2004-12-25 devnull free(c->name);
42 619085f0 2004-12-25 devnull c->name = name;
45 619085f0 2004-12-25 devnull /* bug - work out races */
47 619085f0 2004-12-25 devnull chanfree(Channel *c)
49 619085f0 2004-12-25 devnull if(c == nil)
51 619085f0 2004-12-25 devnull free(c->name);
52 619085f0 2004-12-25 devnull free(c->arecv.a);
53 619085f0 2004-12-25 devnull free(c->asend.a);
57 619085f0 2004-12-25 devnull static void
58 619085f0 2004-12-25 devnull addarray(_Altarray *a, Alt *alt)
60 619085f0 2004-12-25 devnull if(a->n == a->m){
61 619085f0 2004-12-25 devnull a->m += 16;
62 619085f0 2004-12-25 devnull a->a = realloc(a->a, a->m*sizeof a->a[0]);
64 619085f0 2004-12-25 devnull a->a[a->n++] = alt;
67 619085f0 2004-12-25 devnull static void
68 619085f0 2004-12-25 devnull delarray(_Altarray *a, int i)
71 619085f0 2004-12-25 devnull a->a[i] = a->a[a->n];
75 619085f0 2004-12-25 devnull * doesn't really work for things other than CHANSND and CHANRCV
76 619085f0 2004-12-25 devnull * but is only used as arg to chanarray, which can handle it
78 619085f0 2004-12-25 devnull #define otherop(op) (CHANSND+CHANRCV-(op))
80 619085f0 2004-12-25 devnull static _Altarray*
81 619085f0 2004-12-25 devnull chanarray(Channel *c, uint op)
83 619085f0 2004-12-25 devnull switch(op){
85 619085f0 2004-12-25 devnull return nil;
86 619085f0 2004-12-25 devnull case CHANSND:
87 619085f0 2004-12-25 devnull return &c->asend;
88 619085f0 2004-12-25 devnull case CHANRCV:
89 619085f0 2004-12-25 devnull return &c->arecv;
93 619085f0 2004-12-25 devnull static int
94 619085f0 2004-12-25 devnull altcanexec(Alt *a)
96 619085f0 2004-12-25 devnull _Altarray *ar;
97 619085f0 2004-12-25 devnull Channel *c;
99 619085f0 2004-12-25 devnull if(a->op == CHANNOP)
100 619085f0 2004-12-25 devnull return 0;
101 619085f0 2004-12-25 devnull c = a->c;
102 619085f0 2004-12-25 devnull if(c->bufsize == 0){
103 619085f0 2004-12-25 devnull ar = chanarray(c, otherop(a->op));
104 619085f0 2004-12-25 devnull return ar && ar->n;
106 619085f0 2004-12-25 devnull switch(a->op){
107 619085f0 2004-12-25 devnull default:
108 619085f0 2004-12-25 devnull return 0;
109 619085f0 2004-12-25 devnull case CHANSND:
110 619085f0 2004-12-25 devnull return c->nbuf < c->bufsize;
111 619085f0 2004-12-25 devnull case CHANRCV:
112 619085f0 2004-12-25 devnull return c->nbuf > 0;
117 619085f0 2004-12-25 devnull static void
118 619085f0 2004-12-25 devnull altqueue(Alt *a)
120 619085f0 2004-12-25 devnull _Altarray *ar;
122 619085f0 2004-12-25 devnull ar = chanarray(a->c, a->op);
123 619085f0 2004-12-25 devnull addarray(ar, a);
126 619085f0 2004-12-25 devnull static void
127 619085f0 2004-12-25 devnull altdequeue(Alt *a)
130 619085f0 2004-12-25 devnull _Altarray *ar;
132 619085f0 2004-12-25 devnull ar = chanarray(a->c, a->op);
133 619085f0 2004-12-25 devnull if(ar == nil){
134 619085f0 2004-12-25 devnull fprint(2, "bad use of altdequeue op=%d\n", a->op);
135 619085f0 2004-12-25 devnull abort();
138 619085f0 2004-12-25 devnull for(i=0; i<ar->n; i++)
139 619085f0 2004-12-25 devnull if(ar->a[i] == a){
140 619085f0 2004-12-25 devnull delarray(ar, i);
143 619085f0 2004-12-25 devnull fprint(2, "cannot find self in altdq\n");
144 619085f0 2004-12-25 devnull abort();
147 619085f0 2004-12-25 devnull static void
148 619085f0 2004-12-25 devnull altalldequeue(Alt *a)
152 619085f0 2004-12-25 devnull for(i=0; a[i].op!=CHANEND && a[i].op!=CHANNOBLK; i++)
153 619085f0 2004-12-25 devnull if(a[i].op != CHANNOP)
154 619085f0 2004-12-25 devnull altdequeue(&a[i]);
157 619085f0 2004-12-25 devnull static void
158 619085f0 2004-12-25 devnull amove(void *dst, void *src, uint n)
160 619085f0 2004-12-25 devnull if(dst){
161 619085f0 2004-12-25 devnull if(src == nil)
162 619085f0 2004-12-25 devnull memset(dst, 0, n);
164 619085f0 2004-12-25 devnull memmove(dst, src, n);
169 619085f0 2004-12-25 devnull * Actually move the data around. There are up to three
170 619085f0 2004-12-25 devnull * players: the sender, the receiver, and the channel itself.
171 619085f0 2004-12-25 devnull * If the channel is unbuffered or the buffer is empty,
172 619085f0 2004-12-25 devnull * data goes from sender to receiver. If the channel is full,
173 619085f0 2004-12-25 devnull * the receiver removes some from the channel and the sender
174 619085f0 2004-12-25 devnull * gets to put some in.
176 619085f0 2004-12-25 devnull static void
177 619085f0 2004-12-25 devnull altcopy(Alt *s, Alt *r)
180 619085f0 2004-12-25 devnull Channel *c;
181 619085f0 2004-12-25 devnull uchar *cp;
184 619085f0 2004-12-25 devnull * Work out who is sender and who is receiver
186 619085f0 2004-12-25 devnull if(s == nil && r == nil)
188 619085f0 2004-12-25 devnull assert(s != nil);
189 619085f0 2004-12-25 devnull c = s->c;
190 619085f0 2004-12-25 devnull if(s->op == CHANRCV){
195 619085f0 2004-12-25 devnull assert(s==nil || s->op == CHANSND);
196 619085f0 2004-12-25 devnull assert(r==nil || r->op == CHANRCV);
199 619085f0 2004-12-25 devnull * Channel is empty (or unbuffered) - copy directly.
201 619085f0 2004-12-25 devnull if(s && r && c->nbuf == 0){
202 619085f0 2004-12-25 devnull amove(r->v, s->v, c->elemsize);
207 619085f0 2004-12-25 devnull * Otherwise it's always okay to receive and then send.
210 619085f0 2004-12-25 devnull cp = c->buf + c->off*c->elemsize;
211 619085f0 2004-12-25 devnull amove(r->v, cp, c->elemsize);
212 619085f0 2004-12-25 devnull --c->nbuf;
213 619085f0 2004-12-25 devnull if(++c->off == c->bufsize)
214 619085f0 2004-12-25 devnull c->off = 0;
217 619085f0 2004-12-25 devnull cp = c->buf + (c->off+c->nbuf)%c->bufsize*c->elemsize;
218 619085f0 2004-12-25 devnull amove(cp, s->v, c->elemsize);
219 619085f0 2004-12-25 devnull ++c->nbuf;
223 619085f0 2004-12-25 devnull static void
224 619085f0 2004-12-25 devnull altexec(Alt *a)
227 619085f0 2004-12-25 devnull _Altarray *ar;
228 619085f0 2004-12-25 devnull Alt *other;
229 619085f0 2004-12-25 devnull Channel *c;
231 619085f0 2004-12-25 devnull c = a->c;
232 619085f0 2004-12-25 devnull ar = chanarray(c, otherop(a->op));
233 619085f0 2004-12-25 devnull if(ar && ar->n){
234 619085f0 2004-12-25 devnull i = rand()%ar->n;
235 619085f0 2004-12-25 devnull other = ar->a[i];
236 619085f0 2004-12-25 devnull altcopy(a, other);
237 619085f0 2004-12-25 devnull altalldequeue(other->xalt);
238 619085f0 2004-12-25 devnull other->xalt[0].xalt = other;
239 619085f0 2004-12-25 devnull _threadready(other->thread);
241 619085f0 2004-12-25 devnull altcopy(a, nil);
244 619085f0 2004-12-25 devnull #define dbgalt 0
246 619085f0 2004-12-25 devnull chanalt(Alt *a)
248 619085f0 2004-12-25 devnull int i, j, ncan, n, canblock;
249 619085f0 2004-12-25 devnull Channel *c;
250 619085f0 2004-12-25 devnull _Thread *t;
252 c8b6342d 2005-01-13 devnull needstack(512);
253 619085f0 2004-12-25 devnull for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++)
256 619085f0 2004-12-25 devnull canblock = a[i].op == CHANEND;
258 619085f0 2004-12-25 devnull t = proc()->thread;
259 619085f0 2004-12-25 devnull for(i=0; i<n; i++){
260 619085f0 2004-12-25 devnull a[i].thread = t;
261 619085f0 2004-12-25 devnull a[i].xalt = a;
263 619085f0 2004-12-25 devnull qlock(&chanlock);
264 619085f0 2004-12-25 devnull if(dbgalt) print("alt ");
265 619085f0 2004-12-25 devnull ncan = 0;
266 619085f0 2004-12-25 devnull for(i=0; i<n; i++){
267 619085f0 2004-12-25 devnull c = a[i].c;
268 619085f0 2004-12-25 devnull if(dbgalt) print(" %c:", "esrnb"[a[i].op]);
269 619085f0 2004-12-25 devnull if(dbgalt) if(c->name) print("%s", c->name); else print("%p", c);
270 619085f0 2004-12-25 devnull if(altcanexec(&a[i])){
271 619085f0 2004-12-25 devnull if(dbgalt) print("*");
275 619085f0 2004-12-25 devnull if(ncan){
276 619085f0 2004-12-25 devnull j = rand()%ncan;
277 619085f0 2004-12-25 devnull for(i=0; i<n; i++){
278 619085f0 2004-12-25 devnull if(altcanexec(&a[i])){
279 619085f0 2004-12-25 devnull if(j-- == 0){
280 619085f0 2004-12-25 devnull if(dbgalt){
281 619085f0 2004-12-25 devnull c = a[i].c;
282 619085f0 2004-12-25 devnull print(" => %c:", "esrnb"[a[i].op]);
283 619085f0 2004-12-25 devnull if(c->name) print("%s", c->name); else print("%p", c);
284 619085f0 2004-12-25 devnull print("\n");
286 619085f0 2004-12-25 devnull altexec(&a[i]);
287 619085f0 2004-12-25 devnull qunlock(&chanlock);
288 619085f0 2004-12-25 devnull return i;
293 619085f0 2004-12-25 devnull if(dbgalt)print("\n");
295 619085f0 2004-12-25 devnull if(!canblock){
296 619085f0 2004-12-25 devnull qunlock(&chanlock);
297 619085f0 2004-12-25 devnull return -1;
300 619085f0 2004-12-25 devnull for(i=0; i<n; i++){
301 619085f0 2004-12-25 devnull if(a[i].op != CHANNOP)
302 619085f0 2004-12-25 devnull altqueue(&a[i]);
304 619085f0 2004-12-25 devnull qunlock(&chanlock);
306 619085f0 2004-12-25 devnull _threadswitch();
309 619085f0 2004-12-25 devnull * the guy who ran the op took care of dequeueing us
310 619085f0 2004-12-25 devnull * and then set a[0].alt to the one that was executed.
312 619085f0 2004-12-25 devnull return a[0].xalt - a;
315 619085f0 2004-12-25 devnull static int
316 619085f0 2004-12-25 devnull _chanop(Channel *c, int op, void *p, int canblock)
318 619085f0 2004-12-25 devnull Alt a[2];
320 619085f0 2004-12-25 devnull a[0].c = c;
321 619085f0 2004-12-25 devnull a[0].op = op;
322 619085f0 2004-12-25 devnull a[0].v = p;
323 619085f0 2004-12-25 devnull a[1].op = canblock ? CHANEND : CHANNOBLK;
324 619085f0 2004-12-25 devnull if(chanalt(a) < 0)
325 619085f0 2004-12-25 devnull return -1;
326 619085f0 2004-12-25 devnull return 1;
330 619085f0 2004-12-25 devnull chansend(Channel *c, void *v)
332 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, v, 1);
336 619085f0 2004-12-25 devnull channbsend(Channel *c, void *v)
338 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, v, 0);
342 619085f0 2004-12-25 devnull chanrecv(Channel *c, void *v)
344 619085f0 2004-12-25 devnull return _chanop(c, CHANRCV, v, 1);
348 619085f0 2004-12-25 devnull channbrecv(Channel *c, void *v)
350 619085f0 2004-12-25 devnull return _chanop(c, CHANRCV, v, 0);
354 619085f0 2004-12-25 devnull chansendp(Channel *c, void *v)
356 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, (void*)&v, 1);
360 619085f0 2004-12-25 devnull chanrecvp(Channel *c)
362 619085f0 2004-12-25 devnull void *v;
364 619085f0 2004-12-25 devnull _chanop(c, CHANRCV, (void*)&v, 1);
365 619085f0 2004-12-25 devnull return v;
369 619085f0 2004-12-25 devnull channbsendp(Channel *c, void *v)
371 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, (void*)&v, 0);
375 619085f0 2004-12-25 devnull channbrecvp(Channel *c)
377 619085f0 2004-12-25 devnull void *v;
379 619085f0 2004-12-25 devnull _chanop(c, CHANRCV, (void*)&v, 0);
380 619085f0 2004-12-25 devnull return v;
384 619085f0 2004-12-25 devnull chansendul(Channel *c, ulong val)
386 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, &val, 1);
390 619085f0 2004-12-25 devnull chanrecvul(Channel *c)
392 619085f0 2004-12-25 devnull ulong val;
394 619085f0 2004-12-25 devnull _chanop(c, CHANRCV, &val, 1);
395 619085f0 2004-12-25 devnull return val;
399 619085f0 2004-12-25 devnull channbsendul(Channel *c, ulong val)
401 619085f0 2004-12-25 devnull return _chanop(c, CHANSND, &val, 0);
405 619085f0 2004-12-25 devnull channbrecvul(Channel *c)
407 619085f0 2004-12-25 devnull ulong val;
409 619085f0 2004-12-25 devnull _chanop(c, CHANRCV, &val, 0);
410 619085f0 2004-12-25 devnull return val;