Blob


1 #include "threadimpl.h"
3 /*
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
10 * same time.
11 *
12 * For Plan 9 apps, it's just not worth the extra effort.
13 */
14 static QLock chanlock;
16 Channel*
17 chancreate(int elemsize, int bufsize)
18 {
19 Channel *c;
21 c = malloc(sizeof *c+bufsize*elemsize);
22 if(c == nil)
23 sysfatal("chancreate malloc: %r");
24 memset(c, 0, sizeof *c);
25 c->elemsize = elemsize;
26 c->bufsize = bufsize;
27 c->nbuf = 0;
28 c->buf = (uchar*)(c+1);
29 return c;
30 }
32 void
33 chansetname(Channel *c, char *fmt, ...)
34 {
35 char *name;
36 va_list arg;
38 va_start(arg, fmt);
39 name = vsmprint(fmt, arg);
40 va_end(arg);
41 free(c->name);
42 c->name = name;
43 }
45 /* bug - work out races */
46 void
47 chanfree(Channel *c)
48 {
49 if(c == nil)
50 return;
51 free(c->name);
52 free(c->arecv.a);
53 free(c->asend.a);
54 free(c);
55 }
57 static void
58 addarray(_Altarray *a, Alt *alt)
59 {
60 if(a->n == a->m){
61 a->m += 16;
62 a->a = realloc(a->a, a->m*sizeof a->a[0]);
63 }
64 a->a[a->n++] = alt;
65 }
67 static void
68 delarray(_Altarray *a, int i)
69 {
70 --a->n;
71 a->a[i] = a->a[a->n];
72 }
74 /*
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
77 */
78 #define otherop(op) (CHANSND+CHANRCV-(op))
80 static _Altarray*
81 chanarray(Channel *c, uint op)
82 {
83 switch(op){
84 default:
85 return nil;
86 case CHANSND:
87 return &c->asend;
88 case CHANRCV:
89 return &c->arecv;
90 }
91 }
93 static int
94 altcanexec(Alt *a)
95 {
96 _Altarray *ar;
97 Channel *c;
99 if(a->op == CHANNOP)
100 return 0;
101 c = a->c;
102 if(c->bufsize == 0){
103 ar = chanarray(c, otherop(a->op));
104 return ar && ar->n;
105 }else{
106 switch(a->op){
107 default:
108 return 0;
109 case CHANSND:
110 return c->nbuf < c->bufsize;
111 case CHANRCV:
112 return c->nbuf > 0;
117 static void
118 altqueue(Alt *a)
120 _Altarray *ar;
122 ar = chanarray(a->c, a->op);
123 addarray(ar, a);
126 static void
127 altdequeue(Alt *a)
129 int i;
130 _Altarray *ar;
132 ar = chanarray(a->c, a->op);
133 if(ar == nil){
134 fprint(2, "bad use of altdequeue op=%d\n", a->op);
135 abort();
138 for(i=0; i<ar->n; i++)
139 if(ar->a[i] == a){
140 delarray(ar, i);
141 return;
143 fprint(2, "cannot find self in altdq\n");
144 abort();
147 static void
148 altalldequeue(Alt *a)
150 int i;
152 for(i=0; a[i].op!=CHANEND && a[i].op!=CHANNOBLK; i++)
153 if(a[i].op != CHANNOP)
154 altdequeue(&a[i]);
157 static void
158 amove(void *dst, void *src, uint n)
160 if(dst){
161 if(src == nil)
162 memset(dst, 0, n);
163 else
164 memmove(dst, src, n);
168 /*
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.
175 */
176 static void
177 altcopy(Alt *s, Alt *r)
179 Alt *t;
180 Channel *c;
181 uchar *cp;
183 /*
184 * Work out who is sender and who is receiver
185 */
186 if(s == nil && r == nil)
187 return;
188 assert(s != nil);
189 c = s->c;
190 if(s->op == CHANRCV){
191 t = s;
192 s = r;
193 r = t;
195 assert(s==nil || s->op == CHANSND);
196 assert(r==nil || r->op == CHANRCV);
198 /*
199 * Channel is empty (or unbuffered) - copy directly.
200 */
201 if(s && r && c->nbuf == 0){
202 amove(r->v, s->v, c->elemsize);
203 return;
206 /*
207 * Otherwise it's always okay to receive and then send.
208 */
209 if(r){
210 cp = c->buf + c->off*c->elemsize;
211 amove(r->v, cp, c->elemsize);
212 --c->nbuf;
213 if(++c->off == c->bufsize)
214 c->off = 0;
216 if(s){
217 cp = c->buf + (c->off+c->nbuf)%c->bufsize*c->elemsize;
218 amove(cp, s->v, c->elemsize);
219 ++c->nbuf;
223 static void
224 altexec(Alt *a)
226 int i;
227 _Altarray *ar;
228 Alt *other;
229 Channel *c;
231 c = a->c;
232 ar = chanarray(c, otherop(a->op));
233 if(ar && ar->n){
234 i = rand()%ar->n;
235 other = ar->a[i];
236 altcopy(a, other);
237 altalldequeue(other->xalt);
238 other->xalt[0].xalt = other;
239 _threadready(other->thread);
240 }else
241 altcopy(a, nil);
244 #define dbgalt 0
245 int
246 chanalt(Alt *a)
248 int i, j, ncan, n, canblock;
249 Channel *c;
250 _Thread *t;
252 needstack(512);
253 for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++)
255 n = i;
256 canblock = a[i].op == CHANEND;
258 t = proc()->thread;
259 for(i=0; i<n; i++){
260 a[i].thread = t;
261 a[i].xalt = a;
263 qlock(&chanlock);
264 if(dbgalt) print("alt ");
265 ncan = 0;
266 for(i=0; i<n; i++){
267 c = a[i].c;
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("*");
272 ncan++;
275 if(ncan){
276 j = rand()%ncan;
277 for(i=0; i<n; i++){
278 if(altcanexec(&a[i])){
279 if(j-- == 0){
280 if(dbgalt){
281 c = a[i].c;
282 print(" => %c:", "esrnb"[a[i].op]);
283 if(c->name) print("%s", c->name); else print("%p", c);
284 print("\n");
286 altexec(&a[i]);
287 qunlock(&chanlock);
288 return i;
293 if(dbgalt)print("\n");
295 if(!canblock){
296 qunlock(&chanlock);
297 return -1;
300 for(i=0; i<n; i++){
301 if(a[i].op != CHANNOP)
302 altqueue(&a[i]);
304 qunlock(&chanlock);
306 _threadswitch();
308 /*
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.
311 */
312 return a[0].xalt - a;
315 static int
316 _chanop(Channel *c, int op, void *p, int canblock)
318 Alt a[2];
320 a[0].c = c;
321 a[0].op = op;
322 a[0].v = p;
323 a[1].op = canblock ? CHANEND : CHANNOBLK;
324 if(chanalt(a) < 0)
325 return -1;
326 return 1;
329 int
330 chansend(Channel *c, void *v)
332 return _chanop(c, CHANSND, v, 1);
335 int
336 channbsend(Channel *c, void *v)
338 return _chanop(c, CHANSND, v, 0);
341 int
342 chanrecv(Channel *c, void *v)
344 return _chanop(c, CHANRCV, v, 1);
347 int
348 channbrecv(Channel *c, void *v)
350 return _chanop(c, CHANRCV, v, 0);
353 int
354 chansendp(Channel *c, void *v)
356 return _chanop(c, CHANSND, (void*)&v, 1);
359 void*
360 chanrecvp(Channel *c)
362 void *v;
364 _chanop(c, CHANRCV, (void*)&v, 1);
365 return v;
368 int
369 channbsendp(Channel *c, void *v)
371 return _chanop(c, CHANSND, (void*)&v, 0);
374 void*
375 channbrecvp(Channel *c)
377 void *v;
379 _chanop(c, CHANRCV, (void*)&v, 0);
380 return v;
383 int
384 chansendul(Channel *c, ulong val)
386 return _chanop(c, CHANSND, &val, 1);
389 ulong
390 chanrecvul(Channel *c)
392 ulong val;
394 _chanop(c, CHANRCV, &val, 1);
395 return val;
398 int
399 channbsendul(Channel *c, ulong val)
401 return _chanop(c, CHANSND, &val, 0);
404 ulong
405 channbrecvul(Channel *c)
407 ulong val;
409 _chanop(c, CHANRCV, &val, 0);
410 return val;