/** * LCR * -- compute a leader in a ring * from N. Lynch, p. 477 */ type Pid; type Status; const unique UNKNOWN, CHOSEN, REPORTED: Status; var u: [Pid] int; var status: [Pid] Status; const next: [Pid] Pid; procedure Main () { var candidate: Pid; var neighbor: Pid; // identifiers are unique assume (forall p, q: Pid :: p != q ==> u[p] != u[q]); assume (forall p: Pid :: status[p] == UNKNOWN); // any number of nodes can start the leader electing while (*) { havoc candidate; havoc neighbor; assume candidate != neighbor; assume next[candidate] == neighbor; call {:async} leader (neighbor, u[candidate]); } return; } procedure leader (this: Pid, max: int) { if (max > u[this]) { // pass the higher numbers call {:async} leader(next[this], max); } else if (max == u[this]) { // win when we get our number back status[this] := CHOSEN; } else { // do nothing } return; }