-
Notifications
You must be signed in to change notification settings - Fork 2
/
FastMutex.tla
153 lines (127 loc) · 5.06 KB
/
FastMutex.tla
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
----------------------------- MODULE FastMutex -----------------------------
EXTENDS Naturals, TLC
CONSTANT N
(* PlusCal options (-termination) *)
(*
--algorithm FastMutex {
(* shared memory *)
variables x = 0, y = 0, b = [i \in 1..N |-> FALSE];
fair process (Proc \in 1..N)
variable j;
{ ncs:- skip; (* the non-critical section *)
start: b[self] := TRUE;
l1: x := self;
l2: if (y # 0) { l3: b[self] := FALSE; (* change to TRUE to create deadlock *)
l4: await y = 0;
goto start };
l5: y := self;
l6: if (x # self) { l7: b[self] := FALSE;
j := 1;
l8: while (j <= N) { await ~b[j];
j := j + 1 };
l9: if (y # self) { l10: await y = 0;
goto start }};
cs:- skip; (* the critical section *)
l11: y := 0;
l12: b[self] := FALSE;
goto ncs }}
}
}
*)
\* BEGIN TRANSLATION
CONSTANT defaultInitValue
VARIABLES x, y, b, pc, j
vars == << x, y, b, pc, j >>
ProcSet == (1..N)
Init == (* Global variables *)
/\ x = 0
/\ y = 0
/\ b = [i \in 1..N |-> FALSE]
(* Process Proc *)
/\ j = [self \in 1..N |-> defaultInitValue]
/\ pc = [self \in ProcSet |-> "ncs"]
ncs(self) == /\ pc[self] = "ncs"
/\ TRUE
/\ pc' = [pc EXCEPT ![self] = "start"]
/\ UNCHANGED << x, y, b, j >>
start(self) == /\ pc[self] = "start"
/\ b' = [b EXCEPT ![self] = TRUE]
/\ pc' = [pc EXCEPT ![self] = "l1"]
/\ UNCHANGED << x, y, j >>
l1(self) == /\ pc[self] = "l1"
/\ x' = self
/\ pc' = [pc EXCEPT ![self] = "l2"]
/\ UNCHANGED << y, b, j >>
l2(self) == /\ pc[self] = "l2"
/\ IF y # 0
THEN /\ pc' = [pc EXCEPT ![self] = "l3"]
ELSE /\ pc' = [pc EXCEPT ![self] = "l5"]
/\ UNCHANGED << x, y, b, j >>
l3(self) == /\ pc[self] = "l3"
/\ b' = [b EXCEPT ![self] = FALSE]
/\ pc' = [pc EXCEPT ![self] = "l4"]
/\ UNCHANGED << x, y, j >>
l4(self) == /\ pc[self] = "l4"
/\ y = 0
/\ pc' = [pc EXCEPT ![self] = "start"]
/\ UNCHANGED << x, y, b, j >>
l5(self) == /\ pc[self] = "l5"
/\ y' = self
/\ pc' = [pc EXCEPT ![self] = "l6"]
/\ UNCHANGED << x, b, j >>
l6(self) == /\ pc[self] = "l6"
/\ IF x # self
THEN /\ pc' = [pc EXCEPT ![self] = "l7"]
ELSE /\ pc' = [pc EXCEPT ![self] = "cs"]
/\ UNCHANGED << x, y, b, j >>
l7(self) == /\ pc[self] = "l7"
/\ b' = [b EXCEPT ![self] = FALSE]
/\ j' = [j EXCEPT ![self] = 1]
/\ pc' = [pc EXCEPT ![self] = "l8"]
/\ UNCHANGED << x, y >>
l8(self) == /\ pc[self] = "l8"
/\ IF j[self] <= N
THEN /\ ~b[j[self]]
/\ j' = [j EXCEPT ![self] = j[self] + 1]
/\ pc' = [pc EXCEPT ![self] = "l8"]
ELSE /\ pc' = [pc EXCEPT ![self] = "l9"]
/\ j' = j
/\ UNCHANGED << x, y, b >>
l9(self) == /\ pc[self] = "l9"
/\ IF y # self
THEN /\ pc' = [pc EXCEPT ![self] = "l10"]
ELSE /\ pc' = [pc EXCEPT ![self] = "cs"]
/\ UNCHANGED << x, y, b, j >>
l10(self) == /\ pc[self] = "l10"
/\ y = 0
/\ pc' = [pc EXCEPT ![self] = "start"]
/\ UNCHANGED << x, y, b, j >>
cs(self) == /\ pc[self] = "cs"
/\ TRUE
/\ pc' = [pc EXCEPT ![self] = "l11"]
/\ UNCHANGED << x, y, b, j >>
l11(self) == /\ pc[self] = "l11"
/\ y' = 0
/\ pc' = [pc EXCEPT ![self] = "l12"]
/\ UNCHANGED << x, b, j >>
l12(self) == /\ pc[self] = "l12"
/\ b' = [b EXCEPT ![self] = FALSE]
/\ pc' = [pc EXCEPT ![self] = "ncs"]
/\ UNCHANGED << x, y, j >>
Proc(self) == ncs(self) \/ start(self) \/ l1(self) \/ l2(self) \/ l3(self)
\/ l4(self) \/ l5(self) \/ l6(self) \/ l7(self)
\/ l8(self) \/ l9(self) \/ l10(self) \/ cs(self)
\/ l11(self) \/ l12(self)
Next == (\E self \in 1..N: Proc(self))
\/ (* Disjunct to prevent deadlock on termination *)
((\A self \in ProcSet: pc[self] = "Done") /\ UNCHANGED vars)
Spec == /\ Init /\ [][Next]_vars
/\ \A self \in 1..N : WF_vars((pc[self] \notin {"ncs", "cs"}) /\ Proc(self))
Termination == <>(\A self \in ProcSet: pc[self] = "Done")
\* END TRANSLATION
Mutex == \A i,k \in 1..N : (i # k) => \neg ((pc[i] = "cs") /\ (pc[k] = "cs"))
Liveness == \E i \in 1..N : pc[i] \notin {"ncs", "cs", "l11", "l12"} ~> \E k \in 1..N : pc[k] = "cs"
=============================================================================
\* Modification History
\* Last modified Tue Feb 24 15:44:10 EST 2015 by nrla
\* Created Wed Feb 11 19:49:41 EST 2015 by nrla