
Sam
By: a guest on Oct 27th, 2007 | syntax:
Ada | size: 0.91 KB | hits: 37 | expires: Never
package P is
Shared : Natural := 0;
procedure Maybe_Increment;
end P;
package body P is
protected Lock is
entry Maybe_Lock (Lock_Acquired : out Boolean);
entry Maybe_Unlock (Was_Locked : Boolean);
private
Is_Locked : Boolean := False;
end Lock;
protected body Lock is
entry Maybe_Lock (Lock_Acquired : out Boolean) when True is
begin
Lock_Acquired := not Is_Locked;
Is_Locked := True;
end Maybe_Lock;
entry Maybe_Unlock (Was_Locked : Boolean) when True is
begin
if Was_Locked then
Is_Locked := False;
end if;
end Maybe_Unlock;
end Lock;
procedure Maybe_Increment is
L : Boolean;
begin
Lock.Maybe_Lock (L);
if L then
Shared := Shared + 1;
end if;
Lock.Maybe_Unlock (L);
end Maybe_Increment;
end P;