|
@@ -1,28 +1,45 @@
|
|
| 1 |
procedure main()
|
| 2 |
// How many threads have arrived to the barrier
|
| 3 |
shared count := 0
|
| 4 |
// Protects the increment of the count
|
| 5 |
shared can_access_count := create_semaphore(1)
|
| 6 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 7 |
-
shared
|
|
|
|
| 8 |
// Read thread count from standard input
|
| 9 |
input shared const thread_count
|
| 10 |
// Create a thread team running secondary
|
| 11 |
create_threads(thread_count, secondary)
|
| 12 |
end procedure
|
| 13 |
|
| 14 |
procedure secondary()
|
|
|
|
| 15 |
Statement A
|
|
|
|
| 16 |
// Adapt rendezvous solution here
|
| 17 |
wait(can_access_count)
|
| 18 |
count := count + 1
|
| 19 |
if count = thread_count then
|
| 20 |
-
|
| 21 |
-
signal(
|
| 22 |
-
end for
|
| 23 |
end if
|
| 24 |
signal(can_access_count)
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
// Statement B can be only executed until all threads have run Statement A
|
| 27 |
Statement B
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
end procedure
|
| 1 |
procedure main()
|
| 2 |
// How many threads have arrived to the barrier
|
| 3 |
shared count := 0
|
| 4 |
// Protects the increment of the count
|
| 5 |
shared can_access_count := create_semaphore(1)
|
| 6 |
// Locked (0) until all threads arrive, then it is unlocked (1)
|
| 7 |
+
shared turnstile1 := create_semaphore(0)
|
| 8 |
+
shared turnstile2 := create_semaphore(1)
|
| 9 |
// Read thread count from standard input
|
| 10 |
input shared const thread_count
|
| 11 |
// Create a thread team running secondary
|
| 12 |
create_threads(thread_count, secondary)
|
| 13 |
end procedure
|
| 14 |
|
| 15 |
procedure secondary()
|
| 16 |
+
while true do
|
| 17 |
Statement A
|
| 18 |
+
|
| 19 |
// Adapt rendezvous solution here
|
| 20 |
wait(can_access_count)
|
| 21 |
count := count + 1
|
| 22 |
if count = thread_count then
|
| 23 |
+
wait(turnstile2)
|
| 24 |
+
signal(turnstile1)
|
|
|
|
| 25 |
end if
|
| 26 |
signal(can_access_count)
|
| 27 |
+
wait(turnstile1)
|
| 28 |
+
signal(turnstile1)
|
| 29 |
+
|
| 30 |
// Statement B can be only executed until all threads have run Statement A
|
| 31 |
Statement B
|
| 32 |
+
|
| 33 |
+
// Adapt rendezvous solution here
|
| 34 |
+
wait(can_access_count)
|
| 35 |
+
count := count - 1
|
| 36 |
+
if count = 0 then
|
| 37 |
+
wait(turnstile1)
|
| 38 |
+
signal(turnstile2)
|
| 39 |
+
end if
|
| 40 |
+
signal(can_access_count)
|
| 41 |
+
wait(turnstile2)
|
| 42 |
+
signal(turnstile2)
|
| 43 |
+
|
| 44 |
+
end while
|
| 45 |
end procedure
|