int chanalt(Alt *a) { int i, j, ncan, n, canblock; Channel *c; Task *t; needstack(512); for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++) ; n = i; canblock = a[i].op == CHANEND; t = taskrunning; //所有alt指向正在运行的task for(i=0; i<n; i++){ a[i].task = t; a[i].xalt = a; } ncan = 0; for(i=0; i<n; i++){ if(altcanexec(&a[i])){ ncan++; } } if(ncan){ j = rand()%ncan; for(i=0; i<n; i++){ if(altcanexec(&a[i])){ if(j-- == 0){ altexec(&a[i]); return i; } } } } if(!canblock) return -1; for(i=0; i<n; i++){ if(a[i].op != CHANNOP) altqueue(&a[i]); } taskswitch(); /* * the guy who ran the op took care of dequeueing us * and then set a[0].alt to the one that was executed. */ return a[0].xalt - a; }
int chanalt(Alt *a) { int i, j, ncan, n, canblock; Channel *c; Task *t; needstack(512); for(i=0; a[i].op != CHANEND && a[i].op != CHANNOBLK; i++) ; n = i; canblock = a[i].op == CHANEND; t = taskrunning; for(i=0; i<n; i++){ a[i].task = t; a[i].xalt = a; } if(dbgalt) print("alt "); ncan = 0; for(i=0; i<n; i++){ c = a[i].c; if(dbgalt) print(" %c:", "esrnb"[a[i].op]); if(dbgalt) { if(c->name) print("%s", c->name); else print("%p", c); } if(altcanexec(&a[i])){ if(dbgalt) print("*"); ncan++; } } if(ncan){ j = rand()%ncan; for(i=0; i<n; i++){ if(altcanexec(&a[i])){ if(j-- == 0){ if(dbgalt){ c = a[i].c; print(" => %c:", "esrnb"[a[i].op]); if(c->name) print("%s", c->name); else print("%p", c); print("\n"); } altexec(&a[i]); return i; } } } } if(dbgalt)print("\n"); if(!canblock) return -1; for(i=0; i<n; i++){ if(a[i].op != CHANNOP) altqueue(&a[i]); } taskswitch(); /* * the guy who ran the op took care of dequeueing us * and then set a[0].alt to the one that was executed. */ return a[0].xalt - a; }