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