@@ -1,27 +1,42 @@
|
|
1 |
procedure main(argc, argv[]):
|
2 |
if argc = 4 then
|
3 |
shared team_count := integer(argv[1])
|
4 |
shared stage1_delay := integer(argv[2])
|
5 |
shared stage2_delay := integer(argv[3])
|
6 |
shared position := 0
|
7 |
|
|
|
|
|
|
|
|
|
8 |
for team := 0 to team_count do
|
9 |
create_thread(run_stage1, team)
|
10 |
create_thread(run_stage2, team)
|
11 |
end for
|
12 |
else
|
13 |
print "usage: relay_race team_count stage1_delay stage2_delay"
|
14 |
end if
|
15 |
end procedure
|
16 |
|
17 |
procedure run_stage1(team_number):
|
|
|
|
|
|
|
18 |
delay(stage1_delay)
|
|
|
|
|
19 |
end function
|
20 |
|
21 |
procedure run_stage2(team_number):
|
|
|
|
|
|
|
22 |
delay(stage2_delay)
|
|
|
|
|
23 |
declare constant my_team_position = ++position
|
24 |
if my_team_position <= 3 then
|
25 |
print('Place ', my_team_position, ': team ', team_number)
|
26 |
end if
|
|
|
27 |
end procedure
|
1 |
procedure main(argc, argv[]):
|
2 |
if argc = 4 then
|
3 |
shared team_count := integer(argv[1])
|
4 |
shared stage1_delay := integer(argv[2])
|
5 |
shared stage2_delay := integer(argv[3])
|
6 |
shared position := 0
|
7 |
|
8 |
+
shared start_barrier := create_barrier(team_count)
|
9 |
+
shared batons := create_semaphores(team_count, 0)
|
10 |
+
shared finish_mutex := create_mutexes(1)
|
11 |
+
|
12 |
for team := 0 to team_count do
|
13 |
create_thread(run_stage1, team)
|
14 |
create_thread(run_stage2, team)
|
15 |
end for
|
16 |
else
|
17 |
print "usage: relay_race team_count stage1_delay stage2_delay"
|
18 |
end if
|
19 |
end procedure
|
20 |
|
21 |
procedure run_stage1(team_number):
|
22 |
+
// Wait until all teams are ready
|
23 |
+
wait(start_barrier)
|
24 |
+
// Run the first stage
|
25 |
delay(stage1_delay)
|
26 |
+
// Relay the baton to its partner
|
27 |
+
signal(baton[team_number])
|
28 |
end function
|
29 |
|
30 |
procedure run_stage2(team_number):
|
31 |
+
// Wait until my peer relays the baton to me
|
32 |
+
wait(baton[team_number])
|
33 |
+
// Run the second stage
|
34 |
delay(stage2_delay)
|
35 |
+
// Claim our prize
|
36 |
+
wait(finish_mutex)
|
37 |
declare constant my_team_position = ++position
|
38 |
if my_team_position <= 3 then
|
39 |
print('Place ', my_team_position, ': team ', team_number)
|
40 |
end if
|
41 |
+
signal(finish_mutex)
|
42 |
end procedure
|