agpl_1.0.0_b5da3320/src/agpl-monitor.ads

 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
--  Mutex with counter. A task may safely request it multiple times,
--  as long as it releases it the same times

with Ada.Finalization;
with Ada.Task_Identification;

package Agpl.Monitor is

   pragma Preelaborate;

   use Ada;
   use Ada.Task_Identification;

   Use_Error : exception; -- Should never happen.

   type Semaphore is synchronized interface;

   procedure P (This : in out Semaphore) is abstract;
   procedure V (This : in out Semaphore) is abstract;

   --  Standard Counting semaphore:
   protected type Counting_Semaphore is new Semaphore with
      entry P;
      entry V;
   private
      entry Safe_P;

      Caller : Task_Id := Null_Task_Id;           -- Requester
      In_Use : Natural := 0;                      -- Times requested
   end Counting_Semaphore;

   --  Fake for testing:
   protected type Fake_Semaphore is new Semaphore with
      procedure P;
      procedure V;
   end Fake_Semaphore;

   type Semaphore_Access is access all Semaphore'Class;

   --  The following object is defined for conveniently usage of semaphores.
   --  Use:
   --  S : aliased Semaphore;
   --  declare
   --    M : Object (S'access);
   --  begin
   --    Exclusive_work;
   --  end;
   type Object (S : access Semaphore'Class) is new
     Finalization.Limited_Controlled with null record;
   --  Note that for generality purposes, if S in null no error will happen

   procedure Initialize (This : in out Object);
   procedure Finalize   (This : in out Object);

private

   pragma Inline (Initialize, Finalize);

end Agpl.Monitor;