static bool chanrecv ( ChanType *t , Hchan* c , byte *ep , bool block , bool *received ) { SudoG *sg; SudoG mysg; G *gp; int64 t0; #line 215 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( debug ) runtime·printf ( "chanrecv: chan=%p\n" , c ) ; #line 218 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( c == nil ) { USED ( t ) ; if ( !block ) return false; runtime·park ( nil , nil , "chan receive (nil chan)" ) ; return false; } #line 226 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" t0 = 0; mysg.releasetime = 0; if ( runtime·blockprofilerate > 0 ) { t0 = runtime·cputicks ( ) ; mysg.releasetime = -1; } #line 233 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; if ( c->dataqsiz > 0 ) goto asynch; #line 237 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( c->closed ) goto closed; #line 240 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" sg = dequeue ( &c->sendq ) ; if ( sg != nil ) { if ( raceenabled ) racesync ( c , sg ) ; runtime·unlock ( c ) ; #line 246 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( ep != nil ) c->elemtype->alg->copy ( c->elemsize , ep , sg->elem ) ; gp = sg->g; gp->param = sg; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; #line 254 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; return true; } #line 259 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( !block ) { runtime·unlock ( c ) ; return false; } #line 264 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" mysg.elem = ep; mysg.g = g; mysg.selectdone = nil; g->param = nil; enqueue ( &c->recvq , &mysg ) ; runtime·parkunlock ( c , "chan receive" ) ; #line 271 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( g->param == nil ) { runtime·lock ( c ) ; if ( !c->closed ) runtime·throw ( "chanrecv: spurious wakeup" ) ; goto closed; } #line 278 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; #line 284 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" asynch: if ( c->qcount <= 0 ) { if ( c->closed ) goto closed; #line 289 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( !block ) { runtime·unlock ( c ) ; if ( received != nil ) *received = false; return false; } mysg.g = g; mysg.elem = nil; mysg.selectdone = nil; enqueue ( &c->recvq , &mysg ) ; runtime·parkunlock ( c , "chan receive" ) ; #line 301 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; goto asynch; } #line 305 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { runtime·raceacquire ( chanbuf ( c , c->recvx ) ) ; if ( c->dataqsiz == 1 ) runtime·racerelease ( chanbuf ( c , c->recvx ) ) ; } #line 311 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( ep != nil ) c->elemtype->alg->copy ( c->elemsize , ep , chanbuf ( c , c->recvx ) ) ; c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->recvx ) , nil ) ; if ( ++c->recvx == c->dataqsiz ) c->recvx = 0; c->qcount--; #line 318 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" sg = dequeue ( &c->sendq ) ; if ( sg != nil ) { gp = sg->g; runtime·unlock ( c ) ; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; } else runtime·unlock ( c ) ; #line 328 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; #line 334 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" closed: if ( ep != nil ) c->elemtype->alg->copy ( c->elemsize , ep , nil ) ; if ( received != nil ) *received = false; if ( raceenabled ) runtime·raceacquire ( c ) ; runtime·unlock ( c ) ; if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; }
static void* selectgo(Select **selp) { Select *sel; uint32 o, i, j, k; Scase *cas, *dfl; Hchan *c; SudoG *sg; G *gp; byte *as; void *pc; sel = *selp; if(runtime·gcwaiting) runtime·gosched(); if(debug) runtime·printf("select: sel=%p\n", sel); // The compiler rewrites selects that statically have // only 0 or 1 cases plus default into simpler constructs. // The only way we can end up with such small sel->ncase // values here is for a larger select in which most channels // have been nilled out. The general code handles those // cases correctly, and they are rare enough not to bother // optimizing (and needing to test). // generate permuted order for(i=0; i<sel->ncase; i++) sel->pollorder[i] = i; for(i=1; i<sel->ncase; i++) { o = sel->pollorder[i]; j = runtime·fastrand1()%(i+1); sel->pollorder[i] = sel->pollorder[j]; sel->pollorder[j] = o; } // sort the cases by Hchan address to get the locking order. // simple heap sort, to guarantee n log n time and constant stack footprint. for(i=0; i<sel->ncase; i++) { j = i; c = sel->scase[j].chan; while(j > 0 && sel->lockorder[k=(j-1)/2] < c) { sel->lockorder[j] = sel->lockorder[k]; j = k; } sel->lockorder[j] = c; } for(i=sel->ncase; i-->0; ) { c = sel->lockorder[i]; sel->lockorder[i] = sel->lockorder[0]; j = 0; for(;;) { k = j*2+1; if(k >= i) break; if(k+1 < i && sel->lockorder[k] < sel->lockorder[k+1]) k++; if(c < sel->lockorder[k]) { sel->lockorder[j] = sel->lockorder[k]; j = k; continue; } break; } sel->lockorder[j] = c; } /* for(i=0; i+1<sel->ncase; i++) if(sel->lockorder[i] > sel->lockorder[i+1]) { runtime·printf("i=%d %p %p\n", i, sel->lockorder[i], sel->lockorder[i+1]); runtime·throw("select: broken sort"); } */ sellock(sel); loop: // pass 1 - look for something already waiting dfl = nil; for(i=0; i<sel->ncase; i++) { o = sel->pollorder[i]; cas = &sel->scase[o]; c = cas->chan; switch(cas->kind) { case CaseRecv: if(c->dataqsiz > 0) { if(c->qcount > 0) goto asyncrecv; } else { sg = dequeue(&c->sendq); if(sg != nil) goto syncrecv; } if(c->closed) goto rclose; break; case CaseSend: if(c->closed) goto sclose; if(c->dataqsiz > 0) { if(c->qcount < c->dataqsiz) goto asyncsend; } else { sg = dequeue(&c->recvq); if(sg != nil) goto syncsend; } break; case CaseDefault: dfl = cas; break; } } if(dfl != nil) { selunlock(sel); cas = dfl; goto retc; } // pass 2 - enqueue on all chans for(i=0; i<sel->ncase; i++) { o = sel->pollorder[i]; cas = &sel->scase[o]; c = cas->chan; sg = &cas->sg; sg->g = g; sg->selgen = g->selgen; switch(cas->kind) { case CaseRecv: enqueue(&c->recvq, sg); break; case CaseSend: enqueue(&c->sendq, sg); break; } } g->param = nil; runtime·park((void(*)(Lock*))selunlock, (Lock*)sel, "select"); sellock(sel); sg = g->param; // pass 3 - dequeue from unsuccessful chans // otherwise they stack up on quiet channels for(i=0; i<sel->ncase; i++) { cas = &sel->scase[i]; if(cas != (Scase*)sg) { c = cas->chan; if(cas->kind == CaseSend) dequeueg(&c->sendq); else dequeueg(&c->recvq); } } if(sg == nil) goto loop; cas = (Scase*)sg; c = cas->chan; if(c->dataqsiz > 0) runtime·throw("selectgo: shouldn't happen"); if(debug) runtime·printf("wait-return: sel=%p c=%p cas=%p kind=%d\n", sel, c, cas, cas->kind); if(cas->kind == CaseRecv) { if(cas->receivedp != nil) *cas->receivedp = true; } selunlock(sel); goto retc; asyncrecv: // can receive from buffer if(raceenabled) runtime·raceacquire(chanbuf(c, c->recvx)); if(cas->receivedp != nil) *cas->receivedp = true; if(cas->sg.elem != nil) c->elemalg->copy(c->elemsize, cas->sg.elem, chanbuf(c, c->recvx)); c->elemalg->copy(c->elemsize, chanbuf(c, c->recvx), nil); if(++c->recvx == c->dataqsiz) c->recvx = 0; c->qcount--; sg = dequeue(&c->sendq); if(sg != nil) { gp = sg->g; selunlock(sel); runtime·ready(gp); } else { selunlock(sel); } goto retc; asyncsend: // can send to buffer if(raceenabled) runtime·racerelease(chanbuf(c, c->sendx)); c->elemalg->copy(c->elemsize, chanbuf(c, c->sendx), cas->sg.elem); if(++c->sendx == c->dataqsiz) c->sendx = 0; c->qcount++; sg = dequeue(&c->recvq); if(sg != nil) { gp = sg->g; selunlock(sel); runtime·ready(gp); } else { selunlock(sel); } goto retc; syncrecv: // can receive from sleeping sender (sg) if(raceenabled) racesync(c, sg); selunlock(sel); if(debug) runtime·printf("syncrecv: sel=%p c=%p o=%d\n", sel, c, o); if(cas->receivedp != nil) *cas->receivedp = true; if(cas->sg.elem != nil) c->elemalg->copy(c->elemsize, cas->sg.elem, sg->elem); gp = sg->g; gp->param = sg; runtime·ready(gp); goto retc; rclose: // read at end of closed channel selunlock(sel); if(cas->receivedp != nil) *cas->receivedp = false; if(cas->sg.elem != nil) c->elemalg->copy(c->elemsize, cas->sg.elem, nil); if(raceenabled) runtime·raceacquire(c); goto retc; syncsend: // can send to sleeping receiver (sg) if(raceenabled) racesync(c, sg); selunlock(sel); if(debug) runtime·printf("syncsend: sel=%p c=%p o=%d\n", sel, c, o); if(sg->elem != nil) c->elemalg->copy(c->elemsize, sg->elem, cas->sg.elem); gp = sg->g; gp->param = sg; runtime·ready(gp); retc: // return pc corresponding to chosen case. // Set boolean passed during select creation // (at offset selp + cas->so) to true. // If cas->so == 0, this is a reflect-driven select and we // don't need to update the boolean. pc = cas->pc; if(cas->so > 0) { as = (byte*)selp + cas->so; *as = true; } runtime·free(sel); return pc; sclose: // send on closed channel selunlock(sel); runtime·panicstring("send on closed channel"); return nil; // not reached }
void runtime·block(void) { runtime·park(nil, nil, "select (no cases)"); // forever }
void runtime·chanrecv(ChanType *t, Hchan* c, byte *ep, bool *selected, bool *received) { SudoG *sg; SudoG mysg; G *gp; int64 t0; if(runtime·gcwaiting) runtime·gosched(); if(debug) runtime·printf("chanrecv: chan=%p\n", c); if(c == nil) { USED(t); if(selected != nil) { *selected = false; return; } runtime·park(nil, nil, "chan receive (nil chan)"); return; // not reached } t0 = 0; mysg.releasetime = 0; if(runtime·blockprofilerate > 0) { t0 = runtime·cputicks(); mysg.releasetime = -1; } runtime·lock(c); if(c->dataqsiz > 0) goto asynch; if(c->closed) goto closed; sg = dequeue(&c->sendq); if(sg != nil) { if(raceenabled) racesync(c, sg); runtime·unlock(c); if(ep != nil) c->elemalg->copy(c->elemsize, ep, sg->elem); gp = sg->g; gp->param = sg; if(sg->releasetime) sg->releasetime = runtime·cputicks(); runtime·ready(gp); if(selected != nil) *selected = true; if(received != nil) *received = true; return; } if(selected != nil) { runtime·unlock(c); *selected = false; return; } mysg.elem = ep; mysg.g = g; mysg.selgen = NOSELGEN; g->param = nil; enqueue(&c->recvq, &mysg); runtime·park(runtime·unlock, c, "chan receive"); if(g->param == nil) { runtime·lock(c); if(!c->closed) runtime·throw("chanrecv: spurious wakeup"); goto closed; } if(received != nil) *received = true; if(mysg.releasetime > 0) runtime·blockevent(mysg.releasetime - t0, 2); return; asynch: if(c->qcount <= 0) { if(c->closed) goto closed; if(selected != nil) { runtime·unlock(c); *selected = false; if(received != nil) *received = false; return; } mysg.g = g; mysg.elem = nil; mysg.selgen = NOSELGEN; enqueue(&c->recvq, &mysg); runtime·park(runtime·unlock, c, "chan receive"); runtime·lock(c); goto asynch; } if(raceenabled) runtime·raceacquire(chanbuf(c, c->recvx)); if(ep != nil) c->elemalg->copy(c->elemsize, ep, chanbuf(c, c->recvx)); c->elemalg->copy(c->elemsize, chanbuf(c, c->recvx), nil); if(++c->recvx == c->dataqsiz) c->recvx = 0; c->qcount--; sg = dequeue(&c->sendq); if(sg != nil) { gp = sg->g; runtime·unlock(c); if(sg->releasetime) sg->releasetime = runtime·cputicks(); runtime·ready(gp); } else runtime·unlock(c); if(selected != nil) *selected = true; if(received != nil) *received = true; if(mysg.releasetime > 0) runtime·blockevent(mysg.releasetime - t0, 2); return; closed: if(ep != nil) c->elemalg->copy(c->elemsize, ep, nil); if(selected != nil) *selected = true; if(received != nil) *received = false; if(raceenabled) runtime·raceacquire(c); runtime·unlock(c); if(mysg.releasetime > 0) runtime·blockevent(mysg.releasetime - t0, 2); }
/* * generic single channel send/recv * if the bool pointer is nil, * then the full exchange will * occur. if pres is not nil, * then the protocol will not * sleep but return if it could * not complete. * * sleep can wake up with g->param == nil * when a channel involved in the sleep has * been closed. it is easiest to loop and re-run * the operation; we'll see that it's now closed. */ void runtime·chansend(ChanType *t, Hchan *c, byte *ep, bool *pres, void *pc) { SudoG *sg; SudoG mysg; G* gp; int64 t0; if(c == nil) { USED(t); if(pres != nil) { *pres = false; return; } runtime·park(nil, nil, "chan send (nil chan)"); return; // not reached } if(runtime·gcwaiting) runtime·gosched(); if(debug) { runtime·printf("chansend: chan=%p; elem=", c); c->elemalg->print(c->elemsize, ep); runtime·prints("\n"); } t0 = 0; mysg.releasetime = 0; if(runtime·blockprofilerate > 0) { t0 = runtime·cputicks(); mysg.releasetime = -1; } runtime·lock(c); // TODO(dvyukov): add similar instrumentation to select. if(raceenabled) runtime·racereadpc(c, pc, runtime·chansend); if(c->closed) goto closed; if(c->dataqsiz > 0) goto asynch; sg = dequeue(&c->recvq); if(sg != nil) { if(raceenabled) racesync(c, sg); runtime·unlock(c); gp = sg->g; gp->param = sg; if(sg->elem != nil) c->elemalg->copy(c->elemsize, sg->elem, ep); if(sg->releasetime) sg->releasetime = runtime·cputicks(); runtime·ready(gp); if(pres != nil) *pres = true; return; } if(pres != nil) { runtime·unlock(c); *pres = false; return; } mysg.elem = ep; mysg.g = g; mysg.selgen = NOSELGEN; g->param = nil; enqueue(&c->sendq, &mysg); runtime·park(runtime·unlock, c, "chan send"); if(g->param == nil) { runtime·lock(c); if(!c->closed) runtime·throw("chansend: spurious wakeup"); goto closed; } if(mysg.releasetime > 0) runtime·blockevent(mysg.releasetime - t0, 2); return; asynch: if(c->closed) goto closed; if(c->qcount >= c->dataqsiz) { if(pres != nil) { runtime·unlock(c); *pres = false; return; } mysg.g = g; mysg.elem = nil; mysg.selgen = NOSELGEN; enqueue(&c->sendq, &mysg); runtime·park(runtime·unlock, c, "chan send"); runtime·lock(c); goto asynch; } if(raceenabled) runtime·racerelease(chanbuf(c, c->sendx)); c->elemalg->copy(c->elemsize, chanbuf(c, c->sendx), ep); if(++c->sendx == c->dataqsiz) c->sendx = 0; c->qcount++; sg = dequeue(&c->recvq); if(sg != nil) { gp = sg->g; runtime·unlock(c); if(sg->releasetime) sg->releasetime = runtime·cputicks(); runtime·ready(gp); } else runtime·unlock(c); if(pres != nil) *pres = true; if(mysg.releasetime > 0) runtime·blockevent(mysg.releasetime - t0, 2); return; closed: runtime·unlock(c); runtime·panicstring("send on closed channel"); }
void jerk() { forward100(); wait1Msec(100); park(); }