/* Test: * get/put/read * get!/put!/read! * select */ #include typedef struct { int n; char result[100]; } res; int gen_res(res *a, int seq, int test) { res b; sprintf(b.result, "%9x : A string number %d\n",a,seq); b.n = seq; if (test) { if (b.n != a->n) return 1; return strcmp(a->result+10, b.result+10) ? 1 : 0; } else { strcpy(a->result, b.result); a->n = b.n; return 0; } } @process receiver(@share in1, @share in2, @share @stream ok) { res *ptr, actual, extra; int errors = 0; int i; for (i=0;i<20;i++) { @select { @get(in1, actual) { @put(ok, actual); } @get!(in2, ptr) { @put!(ok, ptr); } } } actual.n = -1; sprintf(actual.result, "receiver got %d duds\n", errors); @write(ok, actual); } void paws(@share empty, int start) { int i; while (start > 0) { @select { @get(empty,i) { ; } @else { ; } } start -= 34; } } @process sender(@share out, @share @stream ok, int start, @share empty) { res a, *b; int i; for (i=0;i<5;i++) { paws(empty, start); gen_res(&a, i+start, 0); @write(out, a); } sprintf(a.result, "%d: Wrote first 5 successfully\n", start); @write(ok, a); for (; i< 10;i++) { paws(empty, start); b = (res *)malloc (sizeof *b); gen_res(b, i+start, 0); @put!(out , b); } b = (res *)malloc (sizeof *b); sprintf(a.result, "%d: Wrote second 5 successfully\n", start); @put(ok, a); } eMain() { res *a; int i; @share bag1, bag2; @share @stream ok; @share empty; @init(ok); @init(bag1); @init(bag2); @init(empty); @subordinate receiver(bag1, bag2, ok); @subordinate sender(bag2, ok, 0, empty); @subordinate sender(bag1, ok, 100, empty); while (1) { @get!(ok, a); printf("%8x : %s", a, a->result); if (a->n < 0) break; free(a); } }