1------------------------------------------------------------------------------
2--                                                                          --
3--                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4--                                                                          --
5--      S Y S T E M . T A S K I N G . P R O T E C T E D _ O B J E C T S     --
6--                                                                          --
7--                                  S p e c                                 --
8--                                                                          --
9--          Copyright (C) 1992-2011, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNARL is free software; you can  redistribute it  and/or modify it under --
12-- terms of the  GNU General Public License as published  by the Free Soft- --
13-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNARL was developed by the GNARL team at Florida State University.       --
28-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This package provides necessary definitions to handle simple (i.e without
33--  entries) protected objects.
34
35--  All the routines that handle protected objects with entries have been moved
36--  to two children: Entries and Operations. Note that Entries only contains
37--  the type declaration and the OO primitives. This is needed to avoid
38--  circular dependency.
39
40--  This package is part of the high level tasking interface used by the
41--  compiler to expand Ada 95 tasking constructs into simpler run time calls
42--  (aka GNARLI, GNU Ada Run-time Library Interface)
43
44--  Note: the compiler generates direct calls to this interface, via Rtsfind.
45--  Any changes to this interface may require corresponding compiler changes
46--  in exp_ch9.adb and possibly exp_ch7.adb and exp_attr.adb
47
48package System.Tasking.Protected_Objects is
49   pragma Elaborate_Body;
50
51   ---------------------------------
52   -- Compiler Interface (GNARLI) --
53   ---------------------------------
54
55   --  The compiler will expand in the GNAT tree the following construct:
56
57   --  protected PO is
58   --     procedure P;
59   --  private
60   --     open : boolean := false;
61   --  end PO;
62
63   --  protected body PO is
64   --     procedure P is
65   --        ...variable declarations...
66   --     begin
67   --        ...B...
68   --     end P;
69   --  end PO;
70
71   --  as follows:
72
73   --  protected type poT is
74   --     procedure p;
75   --  private
76   --     open : boolean := false;
77   --  end poT;
78   --  type poTV is limited record
79   --     open : boolean := false;
80   --     _object : aliased protection;
81   --  end record;
82   --  procedure poPT__pN (_object : in out poTV);
83   --  procedure poPT__pP (_object : in out poTV);
84   --  freeze poTV [
85   --     procedure poTVI (_init : in out poTV) is
86   --     begin
87   --        _init.open := false;
88   --        object-init-proc (_init._object);
89   --        initialize_protection (_init._object'unchecked_access,
90   --          unspecified_priority);
91   --        return;
92   --     end _init_proc;
93   --  ]
94   --  po : poT;
95   --  poTVI (poTV!(po));
96
97   --  procedure poPT__pN (_object : in out poTV) is
98   --     poR : protection renames _object._object;
99   --     openP : boolean renames _object.open;
100   --     ...variable declarations...
101   --  begin
102   --     ...B...
103   --     return;
104   --  end poPT__pN;
105
106   --  procedure poPT__pP (_object : in out poTV) is
107   --     procedure _clean is
108   --     begin
109   --        unlock (_object._object'unchecked_access);
110   --        return;
111   --     end _clean;
112   --  begin
113   --     lock (_object._object'unchecked_access);
114   --     B2b : begin
115   --        poPT__pN (_object);
116   --     at end
117   --        _clean;
118   --     end B2b;
119   --     return;
120   --  end poPT__pP;
121
122   Null_Protected_Entry : constant := Null_Entry;
123
124   Max_Protected_Entry : constant := Max_Entry;
125
126   type Protected_Entry_Index is new Entry_Index
127     range Null_Protected_Entry .. Max_Protected_Entry;
128
129   type Barrier_Function_Pointer is access
130     function
131       (O    : System.Address;
132        E    : Protected_Entry_Index)
133        return Boolean;
134   --  Pointer to a function which evaluates the barrier of a protected
135   --  entry body. O is a pointer to the compiler-generated record
136   --  representing the protected object, and E is the index of the
137   --  entry serviced by the body.
138
139   type Entry_Action_Pointer is access
140     procedure
141       (O : System.Address;
142        P : System.Address;
143        E : Protected_Entry_Index);
144   --  Pointer to a procedure which executes the sequence of statements
145   --  of a protected entry body. O is a pointer to the compiler-generated
146   --  record representing the protected object, P is a pointer to the
147   --  record of entry parameters, and E is the index of the
148   --  entry serviced by the body.
149
150   type Entry_Body is record
151      Barrier : Barrier_Function_Pointer;
152      Action  : Entry_Action_Pointer;
153   end record;
154   --  The compiler-generated code passes objects of this type to the GNARL
155   --  to allow it to access the executable code of an entry body.
156
157   type Entry_Body_Access is access all Entry_Body;
158
159   type Protection is limited private;
160   --  This type contains the GNARL state of a protected object. The
161   --  application-defined portion of the state (i.e. private objects)
162   --  is maintained by the compiler-generated code.
163   --  Note that there are now 2 Protection types. One for the simple
164   --  case (no entries) and one for the general case that needs the whole
165   --  Finalization mechanism.
166   --  This split helps in the case of restricted run time where we want to
167   --  minimize the size of the code.
168
169   type Protection_Access is access all Protection;
170
171   Null_PO : constant Protection_Access := null;
172
173   function Get_Ceiling
174     (Object : Protection_Access) return System.Any_Priority;
175   --  Returns the new ceiling priority of the protected object
176
177   procedure Initialize_Protection
178     (Object           : Protection_Access;
179      Ceiling_Priority : Integer);
180   --  Initialize the Object parameter so that it can be used by the runtime
181   --  to keep track of the runtime state of a protected object.
182
183   procedure Lock (Object : Protection_Access);
184   --  Lock a protected object for write access. Upon return, the caller
185   --  owns the lock to this object, and no other call to Lock or
186   --  Lock_Read_Only with the same argument will return until the
187   --  corresponding call to Unlock has been made by the caller.
188
189   procedure Lock_Read_Only (Object : Protection_Access);
190   --  Lock a protected object for read access. Upon return, the caller
191   --  owns the lock for read access, and no other calls to Lock with the
192   --  same argument will return until the corresponding call to Unlock
193   --  has been made by the caller. Other calls to Lock_Read_Only may (but
194   --  need not) return before the call to Unlock, and the corresponding
195   --  callers will also own the lock for read access.
196
197   procedure Set_Ceiling
198     (Object : Protection_Access;
199      Prio   : System.Any_Priority);
200   --  Sets the new ceiling priority of the protected object
201
202   procedure Unlock (Object : Protection_Access);
203   --  Relinquish ownership of the lock for the object represented by
204   --  the Object parameter. If this ownership was for write access, or
205   --  if it was for read access where there are no other read access
206   --  locks outstanding, one (or more, in the case of Lock_Read_Only)
207   --  of the tasks waiting on this lock (if any) will be given the
208   --  lock and allowed to return from the Lock or Lock_Read_Only call.
209
210private
211   type Protection is record
212      L : aliased Task_Primitives.Lock;
213      --  Lock used to ensure mutual exclusive access to the protected object
214
215      Ceiling : System.Any_Priority;
216      --  Ceiling priority associated to the protected object
217
218      New_Ceiling : System.Any_Priority;
219      --  New ceiling priority associated to the protected object. In case
220      --  of assignment of a new ceiling priority to the protected object the
221      --  frontend generates a call to set_ceiling to save the new value in
222      --  this field. After such assignment this value can be read by means
223      --  of the 'Priority attribute, which generates a call to get_ceiling.
224      --  However, the ceiling of the protected object will not be changed
225      --  until completion of the protected action in which the assignment
226      --  has been executed (AARM D.5.2 (10/2)).
227
228      Owner : Task_Id;
229      --  This field contains the protected object's owner. Null_Task
230      --  indicates that the protected object is not currently being used.
231      --  This information is used for detecting the type of potentially
232      --  blocking operations described in the ARM 9.5.1, par. 15 (external
233      --  calls on a protected subprogram with the same target object as that
234      --  of the protected action).
235   end record;
236
237   procedure Finalize_Protection (Object : in out Protection);
238   --  Clean up a Protection object (in particular, finalize the associated
239   --  Lock object). The compiler generates calls automatically to this
240   --  procedure
241
242end System.Tasking.Protected_Objects;
243