-
Notifications
You must be signed in to change notification settings - Fork 2
/
BoundedBuffer2.tla
131 lines (104 loc) · 3.85 KB
/
BoundedBuffer2.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
-------------------------------- MODULE BoundedBuffer2 --------------------------------
EXTENDS Naturals, Integers, FiniteSets, Bags, Sequences, TLC
CONSTANTS producers, \* set of producers
consumers, \* set of consumers
BUF_SIZE \* size of the buffer
ASSUME /\ producers # {}
/\ consumers # {}
/\ producers \intersect consumers = {}
/\ BUF_SIZE > 0
participants == producers \union consumers
(*
--algorithm BoundedBuffer2 {
variables running_threads=participants,
wait_set={}, \* producers or consumers waiting on a full/empty buffer
buf = <<>>, \* buffer
accesses=0; \* total number of gets and puts (used for termination)
\* define {running_threads == participants \ wait_set}
macro wait(p) {
wait_set := wait_set \union {p};
running_threads := running_threads \ {p};
}
macro notify() {
if(wait_set # {}) {
with(m \in wait_set) {
wait_set := wait_set \ {m};
running_threads := running_threads \union {m};
}
}
}
macro notifyAll() {
if(wait_set # {}) {
running_threads := running_threads \union wait_set;
wait_set := wait_set \ wait_set;
}
}
macro put(p) {
if(Len(buf) < BUF_SIZE) {
buf := Append(buf, accesses);
accesses := accesses +1;
notifyAll();
}
else
wait(p);
}
macro get(p) {
if(Len(buf) > 0) {
buf := Tail(buf);
notifyAll();
}
else {
wait(p);
}
}
{
while(TRUE) {
with(p \in running_threads) {
if(p \in producers) {
put(p);
}
else {
get(p);
}
}
}
}
}
*)
\* BEGIN TRANSLATION
VARIABLES running_threads, wait_set, buf, accesses
vars == << running_threads, wait_set, buf, accesses >>
Init == (* Global variables *)
/\ running_threads = participants
/\ wait_set = {}
/\ buf = <<>>
/\ accesses = 0
Next == \E p \in running_threads:
IF p \in producers
THEN /\ IF Len(buf) < BUF_SIZE
THEN /\ buf' = Append(buf, accesses)
/\ accesses' = accesses +1
/\ IF wait_set # {}
THEN /\ running_threads' = (running_threads \union wait_set)
/\ wait_set' = wait_set \ wait_set
ELSE /\ TRUE
/\ UNCHANGED << running_threads,
wait_set >>
ELSE /\ wait_set' = (wait_set \union {p})
/\ running_threads' = running_threads \ {p}
/\ UNCHANGED << buf, accesses >>
ELSE /\ IF Len(buf) > 0
THEN /\ buf' = Tail(buf)
/\ IF wait_set # {}
THEN /\ running_threads' = (running_threads \union wait_set)
/\ wait_set' = wait_set \ wait_set
ELSE /\ TRUE
/\ UNCHANGED << running_threads,
wait_set >>
ELSE /\ wait_set' = (wait_set \union {p})
/\ running_threads' = running_threads \ {p}
/\ buf' = buf
/\ UNCHANGED accesses
Spec == Init /\ [][Next]_vars
\* END TRANSLATION
=============================================================================