I am trying to implement a barrier in Ada that has similar functions to C pthread_barrier_wait. Ada 2012 has Ada.Synchronous_Barriers but not available on my system (gnu-gnat on debian lenny).
In particular, how can I make all wait tasks go out of the barrier at the same time and, ideally, complete one of these tasks without using Ada 2012? The following is a very poor implementation. What could be a better approach?
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure foobar is
protected Synchronizer is
entry Ready_For_Action;
entry Wait_For_Release;
entry Done;
entry Wait_For_Others;
private
ready, active: Natural := 0;
end Synchronizer;
NUM_OBJECTS: constant := 3;
protected body Synchronizer is
entry Ready_For_Action when active = 0 is
begin
ready := ready + 1;
end Ready_For_Action;
entry Wait_For_Release when ready = NUM_OBJECTS is
begin
active := active + 1;
end Wait_For_Release;
entry Done when active = NUM_OBJECTS is
begin
ready := ready - 1;
end Done;
entry Wait_For_Others when ready = 0 is
begin
active := active - 1;
end wait_for_others;
end Synchronizer;
task type Foo(N: Natural);
task body Foo is
id: Natural := N;
begin
for iter in 1..3 loop
Synchronizer.Ready_For_Action;
Synchronizer.Wait_For_Release;
if id = 1 then new_line; end if;
delay 0.1;
put(id); new_line;
Synchronizer.Done;
Synchronizer.Wait_For_Others;
end loop;
end Foo;
Task1: Foo(1);
Task2: Foo(2);
Task3: Foo(3);
begin
Null;
end foobar;
Program output:
$ ./foobar
3
1
2
3
1
2
3
2
1
source
share