@@ -6,12 +6,12 @@ CONSTANTS
66 N
77
88(* *
9- --algorithm Barrier {
9+ --algorithm Barriers {
1010 variables
11- lock = 1 ,
12- gate_1 = 0 ,
13- gate_2 = 0 ,
14- rdv = 0 ;
11+ lock = 1 , \* lock variable used for critical sections
12+ gate_1 = 0 , \* semaphore for the first chamber
13+ gate_2 = 0 , \* semaphore for the second chamber
14+ rdv = 0 ; \* counts the processes entering/leaving the barrier
1515
1616 macro Lock ( l ) {
1717 await l = 1 ;
@@ -31,28 +31,38 @@ CONSTANTS
3131 s := s + N ;
3232 }
3333
34+ \* The algorithm uses two waiting chambers which wait for all processes to
35+ \* to enter before allowing them to continue.
36+ \* The usage of two chambers ensures no process can leave the barrier and
37+ \* pass through the whole barrier again while blocking others inside.
3438 process ( proc \in 1 .. N ) {
3539a0 : while ( TRUE ) {
3640 skip ; \* Some code
41+ \* first waiting chamber a1-a6
3742a1 : Lock ( lock ) ;
38- a2 : rdv := rdv + 1 ;
43+ a2 : rdv := rdv + 1 ; \* protect read/write of shared variable with a lock
3944a3 : if ( rdv = N ) {
45+ \* when all processes are in the first chamber
4046a4 : Signal ( gate_1 ) ;
47+ \* open the chamber
4148 } ;
4249a5 : Unlock ( lock ) ;
4350a6 : Wait ( gate_1 ) ;
51+ \* second waiting chamber a7-a12
4452a7 : Lock ( lock ) ;
45- a8 : rdv := rdv - 1 ;
53+ a8 : rdv := rdv - 1 ; \* protect read/write of shared variable with a lock
4654a9 : if ( rdv = 0 ) {
55+ \* when all processes are in the second chamber
4756a10 : Signal ( gate_2 ) ;
57+ \* open the chamber
4858 } ;
4959a11 : Unlock ( lock ) ;
5060a12 : Wait ( gate_2 ) ;
5161 }
5262 }
5363}
5464* *)
55- \* BEGIN TRANSLATION (chksum(pcal) = "fa4cafb2 " /\ chksum(tla) = "4cd2e9fd")
65+ \* BEGIN TRANSLATION (chksum(pcal) = "7a2331f4 " /\ chksum(tla) = "4cd2e9fd")
5666VARIABLES pc , lock , gate_1 , gate_2 , rdv
5767
5868vars == << pc , lock , gate_1 , gate_2 , rdv >>
@@ -239,6 +249,7 @@ pc_translation(self) ==
239249 ELSE "b1"
240250
241251B == INSTANCE Barrier WITH pc <- [ p \in ProcSet |-> pc_translation ( p ) ]
252+ BSpec == B ! Spec
242253
243254-------------------------------------------------------------------------------
244255
0 commit comments