-
Notifications
You must be signed in to change notification settings - Fork 3
/
Locks2.tla
148 lines (122 loc) · 4.46 KB
/
Locks2.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
------------------------------- MODULE Locks2 -------------------------------
EXTENDS Naturals, TLC
(* --algorithm DeadlockEmpire
variables mutex = 0; mutex2 = 0;
mutexOwner = 0; mutex2Owner = 0;
process Left = 1
begin
L1: await (mutex = 0 \/ mutexOwner = 1); \* Monitor.Enter(mutex);
mutex := mutex + 1;
mutexOwner := 1;
L2: await (mutex2 = 0 \/ mutex2Owner = 1); \* Monitor.Enter(mutex2);
mutex2 := mutex2 + 1;
mutex2Owner := 1;
L3: skip;
L4: mutex := mutex - 1; \* Monitor.Exit(mutex);
if (mutex = 0) then mutexOwner := 0; end if;
L5: mutex2 := mutex2 - 1; \* Monitor.Exit(mutex2);
if (mutex2 = 0) then mutex2Owner := 0; end if;
end process;
process Right = 2
begin
R1: await (mutex2 = 0 \/ mutex2Owner = 2); \* Monitor.Enter(mutex2);
mutex2 := mutex2 + 1;
mutex2Owner := 2;
R2: await (mutex = 0 \/ mutexOwner = 2); \* Monitor.Enter(mutex);
mutex := mutex + 1;
mutexOwner := 2;
R3: skip;
R4: await (mutex2 = 0 \/ mutex2Owner = 2); \* Monitor.Enter(mutex2);
mutex2 := mutex2 + 1;
mutex2Owner := 2;
R5: mutex:= mutex - 1; \* Monitor.Exit(mutex);
if (mutex = 0) then mutexOwner := 0; end if;
end process;
end algorithm *)
\* BEGIN TRANSLATION
VARIABLES mutex, mutex2, mutexOwner, mutex2Owner, pc
vars == << mutex, mutex2, mutexOwner, mutex2Owner, pc >>
ProcSet == {1} \cup {2}
Init == (* Global variables *)
/\ mutex = 0
/\ mutex2 = 0
/\ mutexOwner = 0
/\ mutex2Owner = 0
/\ pc = [self \in ProcSet |-> CASE self = 1 -> "L1"
[] self = 2 -> "R1"]
L1 == /\ pc[1] = "L1"
/\ (mutex = 0 \/ mutexOwner = 1)
/\ mutex' = mutex + 1
/\ mutexOwner' = 1
/\ pc' = [pc EXCEPT ![1] = "L2"]
/\ UNCHANGED << mutex2, mutex2Owner >>
L2 == /\ pc[1] = "L2"
/\ (mutex2 = 0 \/ mutex2Owner = 1)
/\ mutex2' = mutex2 + 1
/\ mutex2Owner' = 1
/\ pc' = [pc EXCEPT ![1] = "L3"]
/\ UNCHANGED << mutex, mutexOwner >>
L3 == /\ pc[1] = "L3"
/\ TRUE
/\ pc' = [pc EXCEPT ![1] = "L4"]
/\ UNCHANGED << mutex, mutex2, mutexOwner, mutex2Owner >>
L4 == /\ pc[1] = "L4"
/\ mutex' = mutex - 1
/\ IF (mutex' = 0)
THEN /\ mutexOwner' = 0
ELSE /\ TRUE
/\ UNCHANGED mutexOwner
/\ pc' = [pc EXCEPT ![1] = "L5"]
/\ UNCHANGED << mutex2, mutex2Owner >>
L5 == /\ pc[1] = "L5"
/\ mutex2' = mutex2 - 1
/\ IF (mutex2' = 0)
THEN /\ mutex2Owner' = 0
ELSE /\ TRUE
/\ UNCHANGED mutex2Owner
/\ pc' = [pc EXCEPT ![1] = "Done"]
/\ UNCHANGED << mutex, mutexOwner >>
Left == L1 \/ L2 \/ L3 \/ L4 \/ L5
R1 == /\ pc[2] = "R1"
/\ (mutex2 = 0 \/ mutex2Owner = 2)
/\ mutex2' = mutex2 + 1
/\ mutex2Owner' = 2
/\ pc' = [pc EXCEPT ![2] = "R2"]
/\ UNCHANGED << mutex, mutexOwner >>
R2 == /\ pc[2] = "R2"
/\ (mutex = 0 \/ mutexOwner = 2)
/\ mutex' = mutex + 1
/\ mutexOwner' = 2
/\ pc' = [pc EXCEPT ![2] = "R3"]
/\ UNCHANGED << mutex2, mutex2Owner >>
R3 == /\ pc[2] = "R3"
/\ TRUE
/\ pc' = [pc EXCEPT ![2] = "R4"]
/\ UNCHANGED << mutex, mutex2, mutexOwner, mutex2Owner >>
R4 == /\ pc[2] = "R4"
/\ (mutex2 = 0 \/ mutex2Owner = 2)
/\ mutex2' = mutex2 + 1
/\ mutex2Owner' = 2
/\ pc' = [pc EXCEPT ![2] = "R5"]
/\ UNCHANGED << mutex, mutexOwner >>
R5 == /\ pc[2] = "R5"
/\ mutex' = mutex - 1
/\ IF (mutex' = 0)
THEN /\ mutexOwner' = 0
ELSE /\ TRUE
/\ UNCHANGED mutexOwner
/\ pc' = [pc EXCEPT ![2] = "Done"]
/\ UNCHANGED << mutex2, mutex2Owner >>
Right == R1 \/ R2 \/ R3 \/ R4 \/ R5
Next == Left \/ Right
\/ (* Disjunct to prevent deadlock on termination *)
((\A self \in ProcSet: pc[self] = "Done") /\ UNCHANGED vars)
Spec == Init /\ [][Next]_vars
Termination == <>(\A self \in ProcSet: pc[self] = "Done")
\* END TRANSLATION
Assertions == mutex >= 0 /\ mutex2 >= 0
CriticalSections == {pc[1],pc[2]} /= {"L3", "R3"}
=============================================================================
\* Modification History
\* Last modified Tue Apr 02 01:13:13 PDT 2019 by ejacobus
\* Created Mon Apr 01 01:15:27 PDT 2019 by ejacobus