resource worker const int n = 10 const int p = 10 const int infinity = 1000000 type weightrow = [n]int type work = rec(int source; int distance ) #type workpool = [p]cap(int) #optype resultchan(int,int) body worker(int me, weightrow myweight, cap (work) pool [0:p], cap (int) answer) int mindist int newdist int ackcount, parent bool active op getwork(var int distance) op putwork(int vertex, int distance) proc getwork(distance) { int source; work inwork, ackwork inwork.source = -2; inwork.distance = 0 ackwork.source = -2; ackwork.distance = 0 while (inwork.source == -2) { if (active and (ackcount == 0) and ?pool[me] == 0) { active = false; send pool[parent](ackwork) write("Worker ",me," announces termination to worker ", parent) } write("Worker ",me," waits for new work"); receive pool[me](inwork) write("Worker ",me," got new work ", inwork.distance," from ", inwork.source) if (inwork.source == -2) { ackcount = ackcount-1 } } if (active) { send pool[inwork.source](ackwork) } else { active = true; parent = inwork.source } distance = inwork.distance } proc putwork(vertex,distance) { work outwork ackcount = ackcount + 1; outwork = work(me,distance) send pool[vertex](outwork) } ackcount = 0 active = false mindist = infinity getwork(newdist); write("Worker ",me," got distance ", newdist) while (newdist != -1) { if (newdist < mindist) { mindist = newdist; for [w=1 to n] { if (w != me) { if (myweight[w] < infinity) { putwork(w,mindist+myweight[w]) write("Worker ",me," puts work ", w, " ",mindist+myweight[w]) } } } } getwork(newdist); write("Worker ",me,"got distance", newdist) } send answer(mindist); end worker