OSDN Git Service

2008-04-08 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tpoben.ads
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-2008, 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 2,  or (at your option) any later ver- --
14 -- sion. GNARL 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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package contains all simple primitives related to Protected_Objects
35 --  with entries (i.e init, lock, unlock).
36
37 --  The handling of protected objects with no entries is done in
38 --  System.Tasking.Protected_Objects, the complex routines for protected
39 --  objects with entries in System.Tasking.Protected_Objects.Operations.
40
41 --  The split between Entries and Operations is needed to break circular
42 --  dependencies inside the run time.
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
47 with Ada.Finalization;
48 with Ada.Unchecked_Conversion;
49
50 package System.Tasking.Protected_Objects.Entries is
51    pragma Elaborate_Body;
52
53    subtype Positive_Protected_Entry_Index is
54      Protected_Entry_Index range  1 .. Protected_Entry_Index'Last;
55
56    type Find_Body_Index_Access is access
57      function
58        (O : System.Address;
59         E : Protected_Entry_Index)
60         return Protected_Entry_Index;
61
62    type Protected_Entry_Body_Array is
63      array (Positive_Protected_Entry_Index range <>) of Entry_Body;
64    --  This is an array of the executable code for all entry bodies of
65    --  a protected type.
66
67    type Protected_Entry_Body_Access is access all Protected_Entry_Body_Array;
68
69    type Protected_Entry_Queue_Array is
70      array (Protected_Entry_Index range <>) of Entry_Queue;
71
72    --  This type contains the GNARL state of a protected object. The
73    --  application-defined portion of the state (i.e. private objects)
74    --  is maintained by the compiler-generated code.
75    --  note that there is a simplified version of this type declared in
76    --  System.Tasking.PO_Simple that handle the simple case (no entries).
77
78    type Protection_Entries (Num_Entries : Protected_Entry_Index) is new
79      Ada.Finalization.Limited_Controlled
80    with record
81       L                 : aliased Task_Primitives.Lock;
82       --  The underlying lock associated with a Protection_Entries.
83       --  Note that you should never (un)lock Object.L directly, but instead
84       --  use Lock_Entries/Unlock_Entries.
85
86       Compiler_Info : System.Address;
87       --  Pointer to compiler-generated record representing protected object
88
89       Call_In_Progress : Entry_Call_Link;
90       --  Pointer to the entry call being executed (if any)
91
92       Ceiling : System.Any_Priority;
93       --  Ceiling priority associated with the protected object
94
95       New_Ceiling : System.Any_Priority;
96       --  New ceiling priority associated to the protected object. In case
97       --  of assignment of a new ceiling priority to the protected object the
98       --  frontend generates a call to set_ceiling to save the new value in
99       --  this field. After such assignment this value can be read by means
100       --  of the 'Priority attribute, which generates a call to get_ceiling.
101       --  However, the ceiling of the protected object will not be changed
102       --  until completion of the protected action in which the assignment
103       --  has been executed (AARM D.5.2 (10/2)).
104
105       Owner : Task_Id;
106       --  This field contains the protected object's owner. Null_Task
107       --  indicates that the protected object is not currently being used.
108       --  This information is used for detecting the type of potentially
109       --  blocking operations described in the ARM 9.5.1, par. 15 (external
110       --  calls on a protected subprogram with the same target object as that
111       --  of the protected action).
112
113       Old_Base_Priority : System.Any_Priority;
114       --  Task's base priority when the protected operation was called
115
116       Pending_Action  : Boolean;
117       --  Flag indicating that priority has been dipped temporarily in order
118       --  to avoid violating the priority ceiling of the lock associated with
119       --  this protected object, in Lock_Server. The flag tells Unlock_Server
120       --  or Unlock_And_Update_Server to restore the old priority to
121       --  Old_Base_Priority. This is needed because of situations (bad
122       --  language design?) where one needs to lock a PO but to do so would
123       --  violate the priority ceiling. For example, this can happen when an
124       --  entry call has been requeued to a lower-priority object, and the
125       --  caller then tries to cancel the call while its own priority is
126       --  higher than the ceiling of the new PO.
127
128       Finalized : Boolean := False;
129       --  Set to True by Finalize to make this routine idempotent
130
131       Entry_Bodies : Protected_Entry_Body_Access;
132       --  Pointer to an array containing the executable code for all entry
133       --  bodies of a protected type.
134
135       --  The following function maps the entry index in a call (which denotes
136       --  the queue to the proper entry) into the body of the entry.
137
138       Find_Body_Index : Find_Body_Index_Access;
139       Entry_Queues      : Protected_Entry_Queue_Array (1 .. Num_Entries);
140    end record;
141
142    --  No default initial values for this type, since call records
143    --  will need to be re-initialized before every use.
144
145    type Protection_Entries_Access is access all Protection_Entries'Class;
146    --  See comments in s-tassta.adb about the implicit call to Current_Master
147    --  generated by this declaration.
148
149    function To_Address is
150      new Ada.Unchecked_Conversion (Protection_Entries_Access, System.Address);
151    function To_Protection is
152      new Ada.Unchecked_Conversion (System.Address, Protection_Entries_Access);
153
154    function Get_Ceiling
155      (Object : Protection_Entries_Access) return System.Any_Priority;
156    --  Returns the new ceiling priority of the protected object
157
158    function Has_Interrupt_Or_Attach_Handler
159      (Object : Protection_Entries_Access) return Boolean;
160    --  Returns True if an Interrupt_Handler or Attach_Handler pragma applies
161    --  to the protected object. That is to say this primitive returns False for
162    --  Protection, but is overriden to return True when interrupt handlers are
163    --  declared so the check required by C.3.1(11) can be implemented in
164    --  System.Tasking.Protected_Objects.Initialize_Protection.
165
166    procedure Initialize_Protection_Entries
167      (Object           : Protection_Entries_Access;
168       Ceiling_Priority : Integer;
169       Compiler_Info    : System.Address;
170       Entry_Bodies     : Protected_Entry_Body_Access;
171       Find_Body_Index  : Find_Body_Index_Access);
172    --  Initialize the Object parameter so that it can be used by the runtime
173    --  to keep track of the runtime state of a protected object.
174
175    procedure Lock_Entries (Object : Protection_Entries_Access);
176    --  Lock a protected object for write access. Upon return, the caller owns
177    --  the lock to this object, and no other call to Lock or Lock_Read_Only
178    --  with the same argument will return until the corresponding call to
179    --  Unlock has been made by the caller. Program_Error is raised in case of
180    --  ceiling violation.
181
182    procedure Lock_Entries
183      (Object            : Protection_Entries_Access;
184       Ceiling_Violation : out Boolean);
185    --  Same as above, but return the ceiling violation status instead of
186    --  raising Program_Error.
187
188    procedure Lock_Read_Only_Entries (Object : Protection_Entries_Access);
189    --  Lock a protected object for read access. Upon return, the caller owns
190    --  the lock for read access, and no other calls to Lock with the same
191    --  argument will return until the corresponding call to Unlock has been
192    --  made by the caller. Other calls to Lock_Read_Only may (but need not)
193    --  return before the call to Unlock, and the corresponding callers will
194    --  also own the lock for read access.
195    --
196    --  Note: we are not currently using this interface, it is provided for
197    --  possible future use. At the current time, everyone uses Lock for both
198    --  read and write locks.
199
200    procedure Set_Ceiling
201      (Object : Protection_Entries_Access;
202       Prio   : System.Any_Priority);
203    --  Sets the new ceiling priority of the protected object
204
205    procedure Unlock_Entries (Object : Protection_Entries_Access);
206    --  Relinquish ownership of the lock for the object represented by the
207    --  Object parameter. If this ownership was for write access, or if it was
208    --  for read access where there are no other read access locks outstanding,
209    --  one (or more, in the case of Lock_Read_Only) of the tasks waiting on
210    --  this lock (if any) will be given the lock and allowed to return from
211    --  the Lock or Lock_Read_Only call.
212
213 private
214
215    procedure Finalize (Object : in out Protection_Entries);
216    --  Clean up a Protection object; in particular, finalize the associated
217    --  Lock object.
218
219 end System.Tasking.Protected_Objects.Entries;