void test_grpwait(int chsz, int grpsz) { lwt_chan_t cs[grpsz]; lwt_t ts[grpsz]; int i; lwt_cgrp_t g; printf("[TEST] group wait (channel buffer size %d, grpsz %d)\n", chsz, grpsz); g = lwt_cgrp(); assert(g); for (i = 0 ; i < grpsz ; i++) { cs[i] = lwt_chan(chsz); assert(cs[i]); lwt_chan_grant(cs[i]); lwt_chan_t c = lwt_chan(0); ts[i] = lwt_create(fn_grpwait, cs[i], 0, c); lwt_chan_mark_set(cs[i], (void*)lwt_id(ts[i])); lwt_cgrp_add(g, cs[i], LWT_CHAN_RCV); } assert(lwt_cgrp_free(g) == -1); /** * Q: why don't we iterate through all of the data here? * * A: We need to fix 1) cevt_wait to be level triggered, or 2) * provide a function to detect if there is data available on * a channel. Either of these would allows us to iterate on a * channel while there is more data pending. */ for (i = 0 ; i < ((ITER * grpsz)-(grpsz*chsz)) ; i++) { lwt_chan_t c; int r; lwt_chan_dir_t direction; c = lwt_cgrp_wait(g, &direction); assert(c); assert(direction == LWT_CHAN_RCV); r = (int)lwt_rcv(c); assert(r == (int)lwt_chan_mark_get(c)); } for (i = 0 ; i < grpsz ; i++) { lwt_cgrp_rem(g, cs[i]); lwt_join(ts[i]); lwt_chan_deref(cs[i]); } assert(!lwt_cgrp_free(g)); return; }
void * fn_grpwait(lwt_chan_t c) { int i; for (i = 0 ; i < ITER ; i++) { if ((i % 7) == 0) { int j; for (j = 0 ; j < (i % 8) ; j++) lwt_yield(LWT_NULL); } lwt_snd(c, (void*)lwt_id(lwt_current())); } lwt_chan_deref(c); }