1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
10 static int32 debug = 0;
12 typedef struct WaitQ WaitQ;
13 typedef struct SudoG SudoG;
14 typedef struct Select Select;
15 typedef struct Scase Scase;
17 typedef struct __go_type_descriptor Type;
18 typedef struct __go_channel_type ChanType;
22 G* g; // g and selgen constitute
23 uint32 selgen; // a weak pointer to g
25 byte* elem; // data element
36 uint32 qcount; // total data in the q
37 uint32 dataqsiz; // size of the circular q
41 uint32 sendx; // send index
42 uint32 recvx; // receive index
43 WaitQ recvq; // list of recv waiters
44 WaitQ sendq; // list of send waiters
48 // Buffer follows Hchan immediately in memory.
49 // chanbuf(c, i) is pointer to the i'th slot in the buffer.
50 #define chanbuf(c, i) ((byte*)((c)+1)+(uintptr)(c)->elemsize*(i))
62 SudoG sg; // must be first member (cast to Scase)
65 uint16 index; // index to return
66 bool* receivedp; // pointer to received bool (recv2)
71 uint16 tcase; // total count of scase[]
72 uint16 ncase; // currently filled scase[]
73 uint16* pollorder; // case poll order
74 Hchan** lockorder; // channel lock order
75 Scase scase[1]; // one per case (in order of appearance)
78 static void dequeueg(WaitQ*);
79 static SudoG* dequeue(WaitQ*);
80 static void enqueue(WaitQ*, SudoG*);
83 runtime_makechan_c(ChanType *t, int64 hint)
89 elem = t->__element_type;
91 if(hint < 0 || (int32)hint != hint || (elem->__size > 0 && (uintptr)hint > ((uintptr)-1) / elem->__size))
92 runtime_panicstring("makechan: size out of range");
96 // allocate memory in one call
97 c = (Hchan*)runtime_mal(n + hint*elem->__size);
98 c->elemsize = elem->__size;
99 c->elemalign = elem->__align;
103 runtime_printf("makechan: chan=%p; elemsize=%lld; elemalign=%d; dataqsiz=%d\n",
104 c, (long long)elem->__size, elem->__align, c->dataqsiz);
110 // func makechan(typ *ChanType, size uint32) (chan)
111 uintptr reflect_makechan(ChanType *, uint32)
112 asm ("libgo_reflect.reflect.makechan");
115 reflect_makechan(ChanType *t, uint32 size)
120 c = runtime_makechan_c(t, size);
121 ret = runtime_mal(sizeof(void*));
122 __builtin_memcpy(ret, &c, sizeof(void*));
126 // makechan(t *ChanType, hint int64) (hchan *chan any);
128 __go_new_channel(ChanType *t, uintptr hint)
130 return runtime_makechan_c(t, hint);
134 * generic single channel send/recv
135 * if the bool pointer is nil,
136 * then the full exchange will
137 * occur. if pres is not nil,
138 * then the protocol will not
139 * sleep but return if it could
142 * sleep can wake up with g->param == nil
143 * when a channel involved in the sleep has
144 * been closed. it is easiest to loop and re-run
145 * the operation; we'll see that it's now closed.
148 runtime_chansend(ChanType *t, Hchan *c, byte *ep, bool *pres)
163 g->status = Gwaiting;
164 g->waitreason = "chan send (nil chan)";
166 return; // not reached
169 if(runtime_gcwaiting)
173 runtime_printf("chansend: chan=%p\n", c);
183 sg = dequeue(&c->recvq);
190 runtime_memmove(sg->elem, ep, c->elemsize);
206 mysg.selgen = NOSELGEN;
208 g->status = Gwaiting;
209 g->waitreason = "chan send";
210 enqueue(&c->sendq, &mysg);
214 if(g->param == nil) {
217 runtime_throw("chansend: spurious wakeup");
227 if(c->qcount >= c->dataqsiz) {
235 mysg.selgen = NOSELGEN;
236 g->status = Gwaiting;
237 g->waitreason = "chan send";
238 enqueue(&c->sendq, &mysg);
245 runtime_memmove(chanbuf(c, c->sendx), ep, c->elemsize);
246 if(++c->sendx == c->dataqsiz)
250 sg = dequeue(&c->recvq);
263 runtime_panicstring("send on closed channel");
268 runtime_chanrecv(ChanType *t, Hchan* c, byte *ep, bool *selected, bool *received)
275 if(runtime_gcwaiting)
279 runtime_printf("chanrecv: chan=%p\n", c);
285 if(selected != nil) {
289 g->status = Gwaiting;
290 g->waitreason = "chan receive (nil chan)";
292 return; // not reached
302 sg = dequeue(&c->sendq);
307 runtime_memmove(ep, sg->elem, c->elemsize);
319 if(selected != nil) {
327 mysg.selgen = NOSELGEN;
329 g->status = Gwaiting;
330 g->waitreason = "chan receive";
331 enqueue(&c->recvq, &mysg);
335 if(g->param == nil) {
338 runtime_throw("chanrecv: spurious wakeup");
351 if(selected != nil) {
360 mysg.selgen = NOSELGEN;
361 g->status = Gwaiting;
362 g->waitreason = "chan receive";
363 enqueue(&c->recvq, &mysg);
371 runtime_memmove(ep, chanbuf(c, c->recvx), c->elemsize);
372 runtime_memclr(chanbuf(c, c->recvx), c->elemsize);
373 if(++c->recvx == c->dataqsiz)
377 sg = dequeue(&c->sendq);
393 runtime_memclr(ep, c->elemsize);
401 // The compiler generates a call to __go_send_small to send a value 8
404 __go_send_small(ChanType *t, Hchan* c, uint64 val)
406 byte b[sizeof(uint64)];
408 runtime_memclr(b, sizeof(uint64));
409 __builtin_memcpy(b, &val, t->__element_type->__size);
410 runtime_chansend(t, c, b, nil);
413 // The compiler generates a call to __go_send_big to send a value
414 // larger than 8 bytes or smaller.
416 __go_send_big(ChanType *t, Hchan* c, byte* p)
418 runtime_chansend(t, c, p, nil);
421 // The compiler generates a call to __go_receive_small to receive a
422 // value 8 bytes or smaller.
424 __go_receive_small(ChanType *t, Hchan* c)
427 byte b[sizeof(uint64)];
432 runtime_chanrecv(t, c, u.b, nil, nil);
436 // The compiler generates a call to __go_receive_big to receive a
437 // value larger than 8 bytes.
439 __go_receive_big(ChanType *t, Hchan* c, byte* p)
441 runtime_chanrecv(t, c, p, nil, nil);
444 _Bool runtime_chanrecv2(ChanType *t, Hchan* c, byte* p)
445 __asm__("runtime.chanrecv2");
448 runtime_chanrecv2(ChanType *t, Hchan* c, byte* p)
452 runtime_chanrecv(t, c, p, nil, &received);
456 // func selectnbsend(c chan any, elem any) bool
458 // compiler implements
469 // if selectnbsend(c, v) {
476 runtime_selectnbsend(ChanType *t, Hchan *c, byte *p)
480 runtime_chansend(t, c, p, &res);
484 // func selectnbrecv(elem *any, c chan any) bool
486 // compiler implements
497 // if selectnbrecv(&v, c) {
504 runtime_selectnbrecv(ChanType *t, byte *v, Hchan *c)
508 runtime_chanrecv(t, c, v, &selected, nil);
512 // func selectnbrecv2(elem *any, ok *bool, c chan any) bool
514 // compiler implements
525 // if c != nil && selectnbrecv2(&v, &ok, c) {
532 runtime_selectnbrecv2(ChanType *t, byte *v, _Bool *received, Hchan *c)
538 runtime_chanrecv(t, c, v, &selected, received == nil ? nil : &r);
545 // func chansend(c chan, val iword, nb bool) (selected bool)
546 // where an iword is the same word an interface value would use:
547 // the actual data if it fits, or else a pointer to the data.
549 _Bool reflect_chansend(ChanType *, Hchan *, uintptr, _Bool)
550 __asm__("libgo_reflect.reflect.chansend");
553 reflect_chansend(ChanType *t, Hchan *c, uintptr val, _Bool nb)
561 sp = (bool*)&selected;
566 if(__go_is_pointer_type(t->__element_type))
570 runtime_chansend(t, c, vp, sp);
575 // func chanrecv(c chan, nb bool) (val iword, selected, received bool)
576 // where an iword is the same word an interface value would use:
577 // the actual data if it fits, or else a pointer to the data.
586 struct chanrecv_ret reflect_chanrecv(ChanType *, Hchan *, _Bool)
587 __asm__("libgo_reflect.reflect.chanrecv");
590 reflect_chanrecv(ChanType *t, Hchan *c, _Bool nb)
592 struct chanrecv_ret ret;
606 if(__go_is_pointer_type(t->__element_type)) {
607 vp = (byte*)&ret.val;
609 vp = runtime_mal(t->__element_type->__size);
610 ret.val = (uintptr)vp;
612 runtime_chanrecv(t, c, vp, sp, &received);
614 ret.selected = selected;
615 ret.received = received;
619 static void newselect(int32, Select**);
621 // newselect(size uint32) (sel *byte);
623 void* runtime_newselect(int) __asm__("runtime.newselect");
626 runtime_newselect(int size)
630 newselect(size, &sel);
635 newselect(int32 size, Select **selp)
644 sel = runtime_mal(sizeof(*sel) +
645 n*sizeof(sel->scase[0]) +
646 size*sizeof(sel->lockorder[0]) +
647 size*sizeof(sel->pollorder[0]));
651 sel->pollorder = (void*)(sel->scase + size);
652 sel->lockorder = (void*)(sel->pollorder + size);
656 runtime_printf("newselect s=%p size=%d\n", sel, size);
659 // cut in half to give stack a chance to split
660 static void selectsend(Select *sel, Hchan *c, int index, void *elem);
662 // selectsend(sel *byte, hchan *chan any, elem *any) (selected bool);
664 void runtime_selectsend(Select *, Hchan *, void *, int)
665 __asm__("runtime.selectsend");
668 runtime_selectsend(Select *sel, Hchan *c, void *elem, int index)
670 // nil cases do not compete
674 selectsend(sel, c, index, elem);
678 selectsend(Select *sel, Hchan *c, int index, void *elem)
685 runtime_throw("selectsend: too many cases");
687 cas = &sel->scase[i];
691 cas->kind = CaseSend;
695 runtime_printf("selectsend s=%p index=%d chan=%p\n",
696 sel, cas->index, cas->chan);
699 // cut in half to give stack a chance to split
700 static void selectrecv(Select *sel, Hchan *c, int index, void *elem, bool*);
702 // selectrecv(sel *byte, hchan *chan any, elem *any) (selected bool);
704 void runtime_selectrecv(Select *, Hchan *, void *, int)
705 __asm__("runtime.selectrecv");
708 runtime_selectrecv(Select *sel, Hchan *c, void *elem, int index)
710 // nil cases do not compete
714 selectrecv(sel, c, index, elem, nil);
717 // selectrecv2(sel *byte, hchan *chan any, elem *any, received *bool) (selected bool);
719 void runtime_selectrecv2(Select *, Hchan *, void *, bool *, int)
720 __asm__("runtime.selectrecv2");
723 runtime_selectrecv2(Select *sel, Hchan *c, void *elem, bool *received, int index)
725 // nil cases do not compete
729 selectrecv(sel, c, index, elem, received);
733 selectrecv(Select *sel, Hchan *c, int index, void *elem, bool *received)
740 runtime_throw("selectrecv: too many cases");
742 cas = &sel->scase[i];
746 cas->kind = CaseRecv;
748 cas->receivedp = received;
751 runtime_printf("selectrecv s=%p index=%d chan=%p\n",
752 sel, cas->index, cas->chan);
755 // cut in half to give stack a chance to split
756 static void selectdefault(Select*, int);
758 // selectdefault(sel *byte) (selected bool);
760 void runtime_selectdefault(Select *, int) __asm__("runtime.selectdefault");
763 runtime_selectdefault(Select *sel, int index)
765 selectdefault(sel, index);
769 selectdefault(Select *sel, int index)
776 runtime_throw("selectdefault: too many cases");
778 cas = &sel->scase[i];
782 cas->kind = CaseDefault;
785 runtime_printf("selectdefault s=%p index=%d\n",
796 for(i=0; i<sel->ncase; i++) {
797 c0 = sel->lockorder[i];
799 c = sel->lockorder[i];
806 selunlock(Select *sel)
812 for(i=sel->ncase; i-->0;) {
813 c0 = sel->lockorder[i];
827 g->status = Gwaiting; // forever
828 g->waitreason = "select (no cases)";
832 static int selectgo(Select**);
834 // selectgo(sel *byte);
836 int runtime_selectgo(Select *) __asm__("runtime.selectgo");
839 runtime_selectgo(Select *sel)
841 return selectgo(&sel);
845 selectgo(Select **selp)
857 if(runtime_gcwaiting)
861 runtime_printf("select: sel=%p\n", sel);
865 // The compiler rewrites selects that statically have
866 // only 0 or 1 cases plus default into simpler constructs.
867 // The only way we can end up with such small sel->ncase
868 // values here is for a larger select in which most channels
869 // have been nilled out. The general code handles those
870 // cases correctly, and they are rare enough not to bother
871 // optimizing (and needing to test).
873 // generate permuted order
874 for(i=0; i<sel->ncase; i++)
875 sel->pollorder[i] = i;
876 for(i=1; i<sel->ncase; i++) {
877 o = sel->pollorder[i];
878 j = runtime_fastrand1()%(i+1);
879 sel->pollorder[i] = sel->pollorder[j];
880 sel->pollorder[j] = o;
883 // sort the cases by Hchan address to get the locking order.
884 for(i=0; i<sel->ncase; i++) {
885 c = sel->scase[i].chan;
886 for(j=i; j>0 && sel->lockorder[j-1] >= c; j--)
887 sel->lockorder[j] = sel->lockorder[j-1];
888 sel->lockorder[j] = c;
893 // pass 1 - look for something already waiting
895 for(i=0; i<sel->ncase; i++) {
896 o = sel->pollorder[i];
897 cas = &sel->scase[o];
902 if(c->dataqsiz > 0) {
906 sg = dequeue(&c->sendq);
917 if(c->dataqsiz > 0) {
918 if(c->qcount < c->dataqsiz)
921 sg = dequeue(&c->recvq);
940 // pass 2 - enqueue on all chans
941 for(i=0; i<sel->ncase; i++) {
942 o = sel->pollorder[i];
943 cas = &sel->scase[o];
947 sg->selgen = g->selgen;
951 enqueue(&c->recvq, sg);
955 enqueue(&c->sendq, sg);
961 g->status = Gwaiting;
962 g->waitreason = "select";
969 // pass 3 - dequeue from unsuccessful chans
970 // otherwise they stack up on quiet channels
971 for(i=0; i<sel->ncase; i++) {
972 cas = &sel->scase[i];
973 if(cas != (Scase*)sg) {
975 if(cas->kind == CaseSend)
989 runtime_throw("selectgo: shouldnt happen");
992 runtime_printf("wait-return: sel=%p c=%p cas=%p kind=%d\n",
993 sel, c, cas, cas->kind);
995 if(cas->kind == CaseRecv) {
996 if(cas->receivedp != nil)
997 *cas->receivedp = true;
1004 // can receive from buffer
1005 if(cas->receivedp != nil)
1006 *cas->receivedp = true;
1007 if(cas->sg.elem != nil)
1008 runtime_memmove(cas->sg.elem, chanbuf(c, c->recvx), c->elemsize);
1009 runtime_memclr(chanbuf(c, c->recvx), c->elemsize);
1010 if(++c->recvx == c->dataqsiz)
1013 sg = dequeue(&c->sendq);
1024 // can send to buffer
1025 runtime_memmove(chanbuf(c, c->sendx), cas->sg.elem, c->elemsize);
1026 if(++c->sendx == c->dataqsiz)
1029 sg = dequeue(&c->recvq);
1040 // can receive from sleeping sender (sg)
1043 runtime_printf("syncrecv: sel=%p c=%p o=%d\n", sel, c, o);
1044 if(cas->receivedp != nil)
1045 *cas->receivedp = true;
1046 if(cas->sg.elem != nil)
1047 runtime_memmove(cas->sg.elem, sg->elem, c->elemsize);
1054 // read at end of closed channel
1056 if(cas->receivedp != nil)
1057 *cas->receivedp = false;
1058 if(cas->sg.elem != nil)
1059 runtime_memclr(cas->sg.elem, c->elemsize);
1063 // can send to sleeping receiver (sg)
1066 runtime_printf("syncsend: sel=%p c=%p o=%d\n", sel, c, o);
1068 runtime_memmove(sg->elem, cas->sg.elem, c->elemsize);
1074 // return index corresponding to chosen case
1080 // send on closed channel
1082 runtime_panicstring("send on closed channel");
1083 return 0; // not reached
1086 // closechan(sel *byte);
1088 runtime_closechan(Hchan *c)
1094 runtime_panicstring("close of nil channel");
1096 if(runtime_gcwaiting)
1102 runtime_panicstring("close of closed channel");
1107 // release all readers
1109 sg = dequeue(&c->recvq);
1117 // release all writers
1119 sg = dequeue(&c->sendq);
1131 __go_builtin_close(Hchan *c)
1133 runtime_closechan(c);
1137 // func chanclose(c chan)
1139 void reflect_chanclose(uintptr) __asm__("libgo_reflect.reflect.chanclose");
1142 reflect_chanclose(uintptr c)
1144 runtime_closechan((Hchan*)c);
1148 // func chanlen(c chan) (len int32)
1150 int32 reflect_chanlen(uintptr) __asm__("libgo_reflect.reflect.chanlen");
1153 reflect_chanlen(uintptr ca)
1167 __go_chan_len(Hchan *c)
1169 return reflect_chanlen((uintptr)c);
1173 // func chancap(c chan) (cap int32)
1175 int32 reflect_chancap(uintptr) __asm__("libgo_reflect.reflect.chancap");
1178 reflect_chancap(uintptr ca)
1192 __go_chan_cap(Hchan *c)
1194 return reflect_chancap((uintptr)c);
1206 q->first = sgp->link;
1208 // if sgp is stale, ignore it
1209 if(sgp->selgen != NOSELGEN &&
1210 (sgp->selgen != sgp->g->selgen ||
1211 !runtime_cas(&sgp->g->selgen, sgp->selgen, sgp->selgen + 2))) {
1212 //prints("INVALID PSEUDOG POINTER\n");
1222 SudoG **l, *sgp, *prevsgp;
1227 for(l=&q->first; (sgp=*l) != nil; l=&sgp->link, prevsgp=sgp) {
1238 enqueue(WaitQ *q, SudoG *sgp)
1241 if(q->first == nil) {
1246 q->last->link = sgp;