-------------------------------------------------------------------------------
-- Mutual exclusion protocol 4b_c_p2
-- (4 bits, complex conditions, deadlock-free, single writer)
-- generated automatically in [Bar-David-Taubenfeld-03]
-------------------------------------------------------------------------------

module mutex_4b_c_p2 (ARCH_4B) is

!nat_bits 2 -- use 2 bits to store natural numbers

(*
 * Process P (i) competing for the access to critical section
 * where i = 0..1, j = 1 - i
 *
 *   loop
 *      non critical section ;
 *      A[i] := 1 ;
 *      while C[0] != A[j] or C[i] = j do
 *         C[i] := A[1]
 *      end while ;
 *      critical section ;
 *      A[i] := 0
 *   end loop
 *)

process P [NCS:Pid, CS:Access, A, C:Operation] (i: Pid) is
   var j: Pid, a_1, a_j, c_0, c_i:Nat in
   j := 1 - i;
   loop
      NCS (i);
      A (Write, i, 1 of Nat, i);
      loop L in
         C (Read, 0 of Pid, ?c_0, i);
         C (Read, i, ?c_i, i);
         A (Read, j, ?a_j, i);
         if not ((c_0 != a_j) or (c_i == j)) then break L end if;
         A (Read, 1 of Pid, ?a_1, i);
         C (Write, i, a_1, i)
      end loop;
      CS (Enter, i); CS (Leave, i);
      A (Write, i, 0 of Nat, i)
   end loop
   end var
end process

-------------------------------------------------------------------------------

process Main [NCS:Pid, CS:Access, A, C:Operation, MU:Latency] is
   Arch_4b [NCS, CS, A, C, MU]
end process

end module