1------------------------------------------------------------------------------
2--                                                                          --
3--                GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                  --
4--                                                                          --
5--                SYSTEM.TASKING.PROTECTED_OBJECTS.ENTRIES                  --
6--                                                                          --
7--                                  S p e c                                 --
8--                                                                          --
9--          Copyright (C) 1992-2018, 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 contains all simple primitives related to Protected_Objects
33--  with entries (i.e init, lock, unlock).
34
35--  The handling of protected objects with no entries is done in
36--  System.Tasking.Protected_Objects, the complex routines for protected
37--  objects with entries in System.Tasking.Protected_Objects.Operations.
38
39--  The split between Entries and Operations is needed to break circular
40--  dependencies inside the run time.
41
42--  Note: the compiler generates direct calls to this interface, via Rtsfind.
43--  Any changes to this interface may require corresponding compiler changes.
44
45with Ada.Finalization;
46with Ada.Unchecked_Conversion;
47
48package System.Tasking.Protected_Objects.Entries is
49   pragma Elaborate_Body;
50
51   subtype Positive_Protected_Entry_Index is
52     Protected_Entry_Index range  1 .. Protected_Entry_Index'Last;
53   --  Index of the entry (and in some cases of the queue)
54
55   type Find_Body_Index_Access is access
56     function
57       (O : System.Address;
58        E : Protected_Entry_Index)
59        return Protected_Entry_Index;
60   --  Convert a queue index to an entry index (an entry family has one entry
61   --  index for several queue indexes).
62
63   type Protected_Entry_Body_Array is
64     array (Positive_Protected_Entry_Index range <>) of Entry_Body;
65   --  Contains executable code for all entry bodies of a protected type
66
67   type Protected_Entry_Body_Access is
68     access constant Protected_Entry_Body_Array;
69
70   type Protected_Entry_Queue_Array is
71     array (Protected_Entry_Index range <>) of Entry_Queue;
72
73   type Protected_Entry_Queue_Max_Array is
74     array (Positive_Protected_Entry_Index range <>) of Natural;
75
76   type Protected_Entry_Queue_Max_Access is
77     access constant Protected_Entry_Queue_Max_Array;
78
79   --  The following type contains the GNARL state of a protected object.
80   --  The application-defined portion of the state (i.e. private objects)
81   --  is maintained by the compiler-generated code. Note that there is a
82   --  simplified version of this type declared in System.Tasking.PO_Simple
83   --  that handle the simple case (no entries).
84
85   type Protection_Entries (Num_Entries : Protected_Entry_Index) is new
86     Ada.Finalization.Limited_Controlled
87   with record
88      L : aliased Task_Primitives.Lock;
89      --  The underlying lock associated with a Protection_Entries. Note
90      --  that you should never (un)lock Object.L directly, but instead
91      --  use Lock_Entries/Unlock_Entries.
92
93      Compiler_Info : System.Address;
94      --  Pointer to compiler-generated record representing protected object
95
96      Call_In_Progress : Entry_Call_Link;
97      --  Pointer to the entry call being executed (if any)
98
99      Ceiling : System.Any_Priority;
100      --  Ceiling priority associated with the protected object
101
102      New_Ceiling : System.Any_Priority;
103      --  New ceiling priority associated to the protected object. In case
104      --  of assignment of a new ceiling priority to the protected object the
105      --  frontend generates a call to set_ceiling to save the new value in
106      --  this field. After such assignment this value can be read by means
107      --  of the 'Priority attribute, which generates a call to get_ceiling.
108      --  However, the ceiling of the protected object will not be changed
109      --  until completion of the protected action in which the assignment
110      --  has been executed (AARM D.5.2 (10/2)).
111
112      Owner : Task_Id;
113      --  This field contains the protected object's owner. Null_Task
114      --  indicates that the protected object is not currently being used.
115      --  This information is used for detecting the type of potentially
116      --  blocking operations described in the ARM 9.5.1, par. 15 (external
117      --  calls on a protected subprogram with the same target object as that
118      --  of the protected action).
119
120      Old_Base_Priority : System.Any_Priority;
121      --  Task's base priority when the protected operation was called
122
123      Pending_Action : Boolean;
124      --  Flag indicating that priority has been dipped temporarily in order
125      --  to avoid violating the priority ceiling of the lock associated with
126      --  this protected object, in Lock_Server. The flag tells Unlock_Server
127      --  or Unlock_And_Update_Server to restore the old priority to
128      --  Old_Base_Priority. This is needed because of situations (bad
129      --  language design?) where one needs to lock a PO but to do so would
130      --  violate the priority ceiling. For example, this can happen when an
131      --  entry call has been requeued to a lower-priority object, and the
132      --  caller then tries to cancel the call while its own priority is
133      --  higher than the ceiling of the new PO.
134
135      Finalized : Boolean := False;
136      --  Set to True by Finalize to make this routine idempotent
137
138      Entry_Bodies : Protected_Entry_Body_Access;
139      --  Pointer to an array containing the executable code for all entry
140      --  bodies of a protected type.
141
142      Find_Body_Index : Find_Body_Index_Access;
143      --  A function which maps the entry index in a call (which denotes the
144      --  queue of the proper entry) into the body of the entry.
145
146      Entry_Queue_Maxes : Protected_Entry_Queue_Max_Access;
147      --  Access to an array of naturals representing the max value for each
148      --  entry's queue length. A value of 0 signifies no max.
149
150      Entry_Queues : Protected_Entry_Queue_Array (1 .. Num_Entries);
151      --  Action and barrier subprograms for the protected type.
152   end record;
153
154   --  No default initial values for this type, since call records will need to
155   --  be re-initialized before every use.
156
157   type Protection_Entries_Access is access all Protection_Entries'Class;
158   --  See comments in s-tassta.adb about the implicit call to Current_Master
159   --  generated by this declaration.
160
161   function To_Address is
162     new Ada.Unchecked_Conversion (Protection_Entries_Access, System.Address);
163   function To_Protection is
164     new Ada.Unchecked_Conversion (System.Address, Protection_Entries_Access);
165
166   function Get_Ceiling
167     (Object : Protection_Entries_Access) return System.Any_Priority;
168   --  Returns the new ceiling priority of the protected object
169
170   function Has_Interrupt_Or_Attach_Handler
171     (Object : Protection_Entries_Access) return Boolean;
172   --  Returns True if an Interrupt_Handler or Attach_Handler pragma applies
173   --  to the protected object. That is to say this primitive returns False for
174   --  Protection, but is overridden to return True when interrupt handlers are
175   --  declared so the check required by C.3.1(11) can be implemented in
176   --  System.Tasking.Protected_Objects.Initialize_Protection.
177
178   procedure Initialize_Protection_Entries
179     (Object            : Protection_Entries_Access;
180      Ceiling_Priority  : Integer;
181      Compiler_Info     : System.Address;
182      Entry_Queue_Maxes : Protected_Entry_Queue_Max_Access;
183      Entry_Bodies      : Protected_Entry_Body_Access;
184      Find_Body_Index   : Find_Body_Index_Access);
185   --  Initialize the Object parameter so that it can be used by the runtime
186   --  to keep track of the runtime state of a protected object.
187
188   procedure Lock_Entries (Object : Protection_Entries_Access);
189   --  Lock a protected object for write access. Upon return, the caller owns
190   --  the lock to this object, and no other call to Lock or Lock_Read_Only
191   --  with the same argument will return until the corresponding call to
192   --  Unlock has been made by the caller. Program_Error is raised in case of
193   --  ceiling violation.
194
195   procedure Lock_Entries_With_Status
196     (Object            : Protection_Entries_Access;
197      Ceiling_Violation : out Boolean);
198   --  Same as above, but return the ceiling violation status instead of
199   --  raising Program_Error.
200
201   procedure Lock_Read_Only_Entries (Object : Protection_Entries_Access);
202   --  Lock a protected object for read access. Upon return, the caller owns
203   --  the lock for read access, and no other calls to Lock with the same
204   --  argument will return until the corresponding call to Unlock has been
205   --  made by the caller. Other calls to Lock_Read_Only may (but need not)
206   --  return before the call to Unlock, and the corresponding callers will
207   --  also own the lock for read access.
208   --
209   --  Note: we are not currently using this interface, it is provided for
210   --  possible future use. At the current time, everyone uses Lock for both
211   --  read and write locks.
212
213   function Number_Of_Entries
214     (Object : Protection_Entries_Access) return Entry_Index;
215   --  Return the number of entries of a protected object
216
217   procedure Set_Ceiling
218     (Object : Protection_Entries_Access;
219      Prio   : System.Any_Priority);
220   --  Sets the new ceiling priority of the protected object
221
222   procedure Unlock_Entries (Object : Protection_Entries_Access);
223   --  Relinquish ownership of the lock for the object represented by the
224   --  Object parameter. If this ownership was for write access, or if it was
225   --  for read access where there are no other read access locks outstanding,
226   --  one (or more, in the case of Lock_Read_Only) of the tasks waiting on
227   --  this lock (if any) will be given the lock and allowed to return from
228   --  the Lock or Lock_Read_Only call.
229
230private
231
232   overriding procedure Finalize (Object : in out Protection_Entries);
233   --  Clean up a Protection object; in particular, finalize the associated
234   --  Lock object.
235
236end System.Tasking.Protected_Objects.Entries;
237