static void* selectgo(Select **selp) { Select *sel; uint32 o, i, j; 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. for(i=0; i<sel->ncase; i++) { c = sel->scase[i].chan; for(j=i; j>0 && sel->lockorder[j-1] >= c; j--) sel->lockorder[j] = sel->lockorder[j-1]; sel->lockorder[j] = c; } 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; g->status = Gwaiting; g->waitreason = "select"; selunlock(sel); runtime·gosched(); 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: shouldnt 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(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 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) 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); goto retc; syncsend: // can send to sleeping receiver (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 to pc corresponding to chosen case pc = cas->pc; as = (byte*)selp + cas->so; runtime·free(sel); *as = true; return pc; sclose: // send on closed channel selunlock(sel); runtime·panicstring("send on closed channel"); return nil; // not reached }
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 }