Download pseudo source code

 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
record barrier of
  limit as unsigned integer
  count as unsigned integer
  can_access_record as mutex
  turnstile1 as semaphore
  turnstile2 as semaphore
end record

procedure create_barrier(limit)
  declare new_barrier as barrier
  new_barrier.limit := limit
  new_barrier.count := 0
  new_barrier.can_access_record := create_semaphore(1)
  new_barrier.turnstile1 := create_semaphore(0)
  new_barrier.turnstile2 := create_semaphore(1)
  return new_barrier
end procedure

procedure wait(barrier)
  barrier_phase1(barrier)
  barrier_phase2(barrier)
end procedure

procedure barrier_phase1(barrier)
  wait(barrier.can_access_record)
  if ++barrier.count == barrier.limit then
    wait(barrier.turnstile2)
    signal(barrier.turnstile1)
  end if
  signal(barrier.can_access_record)

  wait(barrier.turnstile1)
  signal(barrier.turnstile1)
end procedure

procedure barrier_phase2(barrier)
  wait(barrier.can_access_record)
  if --barrier.count == 0 then
    wait(barrier.turnstile1)
    signal(barrier.turnstile2)
  end if
  signal(barrier.can_access_record)

  wait(barrier.turnstile2)
  signal(barrier.turnstile2)
end procedure

procedure main()
  shared constant thread_count = read_integer()
  shared my_barrier := create_barrier(thread_count)
  create_threads(thread_count, secondary)
end procedure

procedure secondary()
  while true do
    Sentence A
    barrier_phase1(my_barrier)
    Sentence B
    barrier_phase2(my_barrier)
  end while
end procedure

procedure third(row_count)
  for row := 0 to row_count do
    process_row(row)
    wait(my_barrier)
  end for
end procedure