4.1.11. GNATCOLL.Locks

package GNATCOLL.Locks is

   subtype Mutual_Exclusion is Binary_Semaphore
     (Initially_Available => True,
      Ceiling             => Default_Ceiling);
  --  This subtype represents a Mutual exclusion primitive, which is a common
  --  use case for Semaphores.

   type Scoped_Lock (Lock : access Mutual_Exclusion)
   is limited private;
   --  This type represents a scoped lock for the Mutual_Exclusion object
   --  passed as discriminant. It will hold the Mutex as long as the object is
   --  alive, and release it when it is finalized.
   --
   --  It provides a useful idiom to protect a subprogram or a part of a
   --  subprogram from data races via a critical section. Here is code without
   --  a scoped lock::
   --
   --     A : Integer := 0;
   --     M : Mutual_Exclusion;
   --
   --     procedure Modify_State is
   --     begin
   --        M.Seize;
   --        A := A + 1;
   --        if A > 12 then
   --           M.Release;
   --           return;
   --        end if;
   --
   --        A := A + 2;
   --        M.Release;
   --     exception  -- Addition can overflow!
   --        when others =>
   --        M.Release;
   --        raise;
   --     end Modify_State;
   --
   --  And here is the code with the scoped lock idiom::
   --
   --     A : Integer := 0;
   --     M : Mutual_Exclusion;
   --
   --     procedure Modify_State is
   --        Lock : Scoped_Lock (M);
   --     begin
   --        A := A + 1;
   --        if A > 12 then
   --           return;
   --        end if;
   --
   --        A := A + 2;
   --     end Modify_State;

end GNATCOLL.Locks;