static bool chansend ( ChanType *t , Hchan *c , byte *ep , bool block , void *pc ) { SudoG *sg; SudoG mysg; G* gp; int64 t0; #line 82 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) runtime·racereadobjectpc ( ep , t->elem , runtime·getcallerpc ( &t ) , chansend ) ; #line 85 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( c == nil ) { USED ( t ) ; if ( !block ) return false; runtime·park ( nil , nil , "chan send (nil chan)" ) ; return false; } #line 93 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( debug ) { runtime·printf ( "chansend: chan=%p; elem=" , c ) ; c->elemtype->alg->print ( c->elemsize , ep ) ; runtime·prints ( "\n" ) ; } #line 99 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" t0 = 0; mysg.releasetime = 0; if ( runtime·blockprofilerate > 0 ) { t0 = runtime·cputicks ( ) ; mysg.releasetime = -1; } #line 106 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; if ( raceenabled ) runtime·racereadpc ( c , pc , chansend ) ; if ( c->closed ) goto closed; #line 112 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( c->dataqsiz > 0 ) goto asynch; #line 115 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sg = dequeue ( &c->recvq ) ; if ( sg != nil ) { if ( raceenabled ) racesync ( c , sg ) ; runtime·unlock ( c ) ; #line 121 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" gp = sg->g; gp->param = sg; if ( sg->elem != nil ) c->elemtype->alg->copy ( c->elemsize , sg->elem , ep ) ; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; return true; } #line 131 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( !block ) { runtime·unlock ( c ) ; return false; } #line 136 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" mysg.elem = ep; mysg.g = g; mysg.selectdone = nil; g->param = nil; enqueue ( &c->sendq , &mysg ) ; runtime·parkunlock ( c , "chan send" ) ; #line 143 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( g->param == nil ) { runtime·lock ( c ) ; if ( !c->closed ) runtime·throw ( "chansend: spurious wakeup" ) ; goto closed; } #line 150 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; #line 153 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" return true; #line 155 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" asynch: if ( c->closed ) goto closed; #line 159 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( c->qcount >= c->dataqsiz ) { if ( !block ) { runtime·unlock ( c ) ; return false; } mysg.g = g; mysg.elem = nil; mysg.selectdone = nil; enqueue ( &c->sendq , &mysg ) ; runtime·parkunlock ( c , "chan send" ) ; #line 170 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; goto asynch; } #line 174 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { runtime·raceacquire ( chanbuf ( c , c->sendx ) ) ; runtime·racerelease ( chanbuf ( c , c->sendx ) ) ; } #line 179 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" c->elemtype->alg->copy ( c->elemsize , chanbuf ( c , c->sendx ) , ep ) ; if ( ++c->sendx == c->dataqsiz ) c->sendx = 0; c->qcount++; #line 184 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" 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 ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; #line 197 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" closed: runtime·unlock ( c ) ; runtime·panicstring ( "send on closed channel" ) ; return false; }
static bool chanrecv ( ChanType *t , Hchan* c , byte *ep , bool block , bool *received ) { SudoG *sg; SudoG mysg; G *gp; int64 t0; #line 214 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( debug ) runtime·printf ( "chanrecv: chan=%p\n" , c ) ; #line 217 "/tmp/makerelease402042453/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 225 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" t0 = 0; mysg.releasetime = 0; if ( runtime·blockprofilerate > 0 ) { t0 = runtime·cputicks ( ) ; mysg.releasetime = -1; } #line 232 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; if ( c->dataqsiz > 0 ) goto asynch; #line 236 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( c->closed ) goto closed; #line 239 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sg = dequeue ( &c->sendq ) ; if ( sg != nil ) { if ( raceenabled ) racesync ( c , sg ) ; runtime·unlock ( c ) ; #line 245 "/tmp/makerelease402042453/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 253 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; return true; } #line 258 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( !block ) { runtime·unlock ( c ) ; return false; } #line 263 "/tmp/makerelease402042453/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 270 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( g->param == nil ) { runtime·lock ( c ) ; if ( !c->closed ) runtime·throw ( "chanrecv: spurious wakeup" ) ; goto closed; } #line 277 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; #line 283 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" asynch: if ( c->qcount <= 0 ) { if ( c->closed ) goto closed; #line 288 "/tmp/makerelease402042453/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 300 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" runtime·lock ( c ) ; goto asynch; } #line 304 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { runtime·raceacquire ( chanbuf ( c , c->recvx ) ) ; runtime·racerelease ( chanbuf ( c , c->recvx ) ) ; } #line 309 "/tmp/makerelease402042453/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 316 "/tmp/makerelease402042453/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 326 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( received != nil ) *received = true; if ( mysg.releasetime > 0 ) runtime·blockevent ( mysg.releasetime - t0 , 2 ) ; return true; #line 332 "/tmp/makerelease402042453/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 , done; int64 t0; Scase *cas , *dfl; Hchan *c; SudoG *sg; G *gp; byte *as; void *pc; #line 663 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sel = *selp; #line 665 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( debug ) runtime·printf ( "select: sel=%p\n" , sel ) ; #line 668 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" t0 = 0; if ( runtime·blockprofilerate > 0 ) { t0 = runtime·cputicks ( ) ; for ( i=0; i<sel->ncase; i++ ) sel->scase[i].sg.releasetime = -1; } #line 684 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" 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; } #line 695 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" 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; } #line 730 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sellock ( sel ) ; #line 732 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" loop: #line 734 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" dfl = nil; for ( i=0; i<sel->ncase; i++ ) { o = sel->pollorder[i]; cas = &sel->scase[o]; c = cas->chan; #line 740 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" 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; #line 754 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" case CaseSend: if ( raceenabled ) runtime·racereadpc ( c , cas->pc , chansend ) ; 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; #line 769 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" case CaseDefault: dfl = cas; break; } } #line 775 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( dfl != nil ) { selunlock ( sel ) ; cas = dfl; goto retc; } #line 783 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" done = 0; 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->selectdone = &done; #line 792 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" switch ( cas->kind ) { case CaseRecv: enqueue ( &c->recvq , sg ) ; break; #line 797 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" case CaseSend: enqueue ( &c->sendq , sg ) ; break; } } #line 803 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" g->param = nil; runtime·park ( selparkcommit , sel , "select" ) ; #line 806 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sellock ( sel ) ; sg = g->param; #line 811 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" 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 ) ; } } #line 822 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( sg == nil ) goto loop; #line 825 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" cas = ( Scase* ) sg; c = cas->chan; #line 828 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( c->dataqsiz > 0 ) runtime·throw ( "selectgo: shouldn't happen" ) ; #line 831 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( debug ) runtime·printf ( "wait-return: sel=%p c=%p cas=%p kind=%d\n" , sel , c , cas , cas->kind ) ; #line 835 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( cas->kind == CaseRecv ) { if ( cas->receivedp != nil ) *cas->receivedp = true; } #line 840 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { if ( cas->kind == CaseRecv && cas->sg.elem != nil ) runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; else if ( cas->kind == CaseSend ) runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; } #line 847 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" selunlock ( sel ) ; goto retc; #line 850 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" asyncrecv: #line 852 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { if ( cas->sg.elem != nil ) runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; runtime·raceacquire ( chanbuf ( c , c->recvx ) ) ; runtime·racerelease ( chanbuf ( c , c->recvx ) ) ; } if ( cas->receivedp != nil ) *cas->receivedp = true; if ( cas->sg.elem != nil ) c->elemtype->alg->copy ( c->elemsize , cas->sg.elem , 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--; sg = dequeue ( &c->sendq ) ; if ( sg != nil ) { gp = sg->g; selunlock ( sel ) ; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; } else { selunlock ( sel ) ; } goto retc; #line 878 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" asyncsend: #line 880 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { runtime·raceacquire ( chanbuf ( c , c->sendx ) ) ; runtime·racerelease ( chanbuf ( c , c->sendx ) ) ; runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; } c->elemtype->alg->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 ) ; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; } else { selunlock ( sel ) ; } goto retc; #line 901 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" syncrecv: #line 903 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { if ( cas->sg.elem != nil ) runtime·racewriteobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chanrecv ) ; 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->elemtype->alg->copy ( c->elemsize , cas->sg.elem , sg->elem ) ; gp = sg->g; gp->param = sg; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; goto retc; #line 922 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" rclose: #line 924 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" selunlock ( sel ) ; if ( cas->receivedp != nil ) *cas->receivedp = false; if ( cas->sg.elem != nil ) c->elemtype->alg->copy ( c->elemsize , cas->sg.elem , nil ) ; if ( raceenabled ) runtime·raceacquire ( c ) ; goto retc; #line 933 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" syncsend: #line 935 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" if ( raceenabled ) { runtime·racereadobjectpc ( cas->sg.elem , c->elemtype , cas->pc , chansend ) ; 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->elemtype->alg->copy ( c->elemsize , sg->elem , cas->sg.elem ) ; gp = sg->g; gp->param = sg; if ( sg->releasetime ) sg->releasetime = runtime·cputicks ( ) ; runtime·ready ( gp ) ; #line 950 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" retc: #line 956 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" pc = cas->pc; if ( cas->so > 0 ) { as = ( byte* ) selp + cas->so; *as = true; } if ( cas->sg.releasetime > 0 ) runtime·blockevent ( cas->sg.releasetime - t0 , 2 ) ; runtime·free ( sel ) ; return pc; #line 966 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" sclose: #line 968 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" selunlock ( sel ) ; runtime·panicstring ( "send on closed channel" ) ; return nil; }
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·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"); }
static bool chanrecv(ChanType *t, Hchan* c, byte *ep, bool block, bool *received) { SudoG *sg; SudoG mysg; G *gp; int64 t0; G *g; if(runtime_gcwaiting()) runtime_gosched(); // raceenabled: don't need to check ep, as it is always on the stack. if(debug) runtime_printf("chanrecv: chan=%p\n", c); g = runtime_g(); if(c == nil) { USED(t); if(!block) return false; runtime_park(nil, nil, "chan receive (nil chan)"); return false; // 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) runtime_memmove(ep, sg->elem, c->elemsize); gp = sg->g; gp->param = sg; if(sg->releasetime) sg->releasetime = runtime_cputicks(); runtime_ready(gp); if(received != nil) *received = true; return true; } if(!block) { runtime_unlock(c); return false; } mysg.elem = ep; mysg.g = g; mysg.selectdone = nil; g->param = nil; enqueue(&c->recvq, &mysg); runtime_parkunlock(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 true; asynch: if(c->qcount <= 0) { if(c->closed) goto closed; 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"); runtime_lock(c); goto asynch; } if(raceenabled) runtime_raceacquire(chanbuf(c, c->recvx)); if(ep != nil) runtime_memmove(ep, chanbuf(c, c->recvx), c->elemsize); runtime_memclr(chanbuf(c, c->recvx), c->elemsize); 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(received != nil) *received = true; if(mysg.releasetime > 0) runtime_blockevent(mysg.releasetime - t0, 2); return true; closed: if(ep != nil) runtime_memclr(ep, c->elemsize); 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; }
/* * 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. */ static bool chansend(ChanType *t, Hchan *c, byte *ep, bool block, void *pc) { SudoG *sg; SudoG mysg; G* gp; int64 t0; G* g; g = runtime_g(); if(raceenabled) runtime_racereadobjectpc(ep, t->__element_type, runtime_getcallerpc(&t), chansend); if(c == nil) { USED(t); if(!block) return false; runtime_park(nil, nil, "chan send (nil chan)"); return false; // not reached } if(runtime_gcwaiting()) runtime_gosched(); if(debug) { runtime_printf("chansend: chan=%p\n", c); } t0 = 0; mysg.releasetime = 0; if(runtime_blockprofilerate > 0) { t0 = runtime_cputicks(); mysg.releasetime = -1; } runtime_lock(c); if(raceenabled) runtime_racereadpc(c, pc, 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) runtime_memmove(sg->elem, ep, c->elemsize); if(sg->releasetime) sg->releasetime = runtime_cputicks(); runtime_ready(gp); return true; } if(!block) { runtime_unlock(c); return false; } mysg.elem = ep; mysg.g = g; mysg.selectdone = nil; g->param = nil; enqueue(&c->sendq, &mysg); runtime_parkunlock(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 true; asynch: if(c->closed) goto closed; if(c->qcount >= c->dataqsiz) { if(!block) { runtime_unlock(c); return false; } mysg.g = g; mysg.elem = nil; mysg.selectdone = nil; enqueue(&c->sendq, &mysg); runtime_parkunlock(c, "chan send"); runtime_lock(c); goto asynch; } if(raceenabled) runtime_racerelease(chanbuf(c, c->sendx)); runtime_memmove(chanbuf(c, c->sendx), ep, c->elemsize); 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(mysg.releasetime > 0) runtime_blockevent(mysg.releasetime - t0, 2); return true; closed: runtime_unlock(c); runtime_panicstring("send on closed channel"); return false; // not reached }