/* Test: * get/put/read * get!/put!/read! * select */ #include #include #include typedef struct { int n; char result[100]; } res; useDefaultMarshal(int); useCustomMarshal(res); mStream& operator<<(mStream &s, const res& d) { strcat((char *)d.result, "M"); s.add(&d, sizeof d); return s; } umStream& operator>>(umStream &s, res& d) { s.remove(&d, sizeof d); strcat(d.result, "U"); return s; } template class @share; template class @share @stream; template class @share @reply; declareCtxType(res); typedef @share bag; typedef @share @stream str; typedef @share @reply rep; useCtxMarshal(bag); useCtxMarshal(str); useCtxMarshal(rep); int gen_res(res *a, int seq, int test) { res b; sprintf(b.result, "%9x : A string number %d ",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(bag in1, bag in2, str ok, rep r) { 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); } @resource!(r, ptr) { i--; strcpy(actual.result, ptr->result); sprintf(ptr->result, "%x : %s X ",ptr,actual.result); } @reply! (r, ptr); } } actual.n = -1; sprintf(actual.result, "receiver got %d duds ", errors); @put(ok, actual); } void paws(bag empty, int start) { res i; while (start > 0) { @select { @get(empty,i) { ; } @else { ; } } start -= 50; } } @process sender(bag out, str ok, int start, bag empty, rep r) { res a, *b; int i; for (i=0;i<5;i++) { paws(empty, start); gen_res(&a, i+start, 0); @call(r, a, a); strcat(a.result," Y "); @put(out, a); } sprintf(a.result, "%d: Wrote first 5 successfully ", start); @put(ok, a); for (; i< 10;i++) { paws(empty, start); b = (res *)malloc (sizeof *b); gen_res(b, i+start, 0); @call!(r, b, b); strcat(b->result," Z "); @put!(out , b); } b = (res *)malloc (sizeof *b); sprintf(a.result, "%d: Wrote second 5 successfully ", start); @put(ok, a); @get(empty, a); } eMain() { res *a; int i; bag bag1, bag2; str ok; bag empty; rep r; @subordinate receiver(bag1, bag2, ok, r); @subordinate sender(bag2, ok, 0, empty, r); @subordinate sender(bag1, ok, 100, empty, r); while (1) { @get!(ok, a); printf("%8x : %s\n", a, a->result); if (a->n < 0) break; free(a); } }