OSDN Git Service

2007-09-26 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-soflin.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                    S Y S T E M . S O F T _ L I N K S                     --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT 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.  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.  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 GNAT;  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 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This package contains a set of subprogram access variables that access
35 --  some low-level primitives that are different depending whether tasking is
36 --  involved or not (e.g. the Get/Set_Jmpbuf_Address that needs to provide a
37 --  different value for each task). To avoid dragging in the tasking runtimes
38 --  all the time, we use a system of soft links where the links are
39 --  initialized to non-tasking versions, and then if the tasking support is
40 --  initialized, they are set to the real tasking versions.
41
42 pragma Warnings (Off);
43 pragma Compiler_Unit;
44 pragma Warnings (On);
45
46 with Ada.Exceptions;
47 with System.Stack_Checking;
48
49 package System.Soft_Links is
50    pragma Warnings (Off);
51    pragma Preelaborate_05;
52    pragma Warnings (On);
53
54    subtype EOA is Ada.Exceptions.Exception_Occurrence_Access;
55    subtype EO is Ada.Exceptions.Exception_Occurrence;
56
57    function Current_Target_Exception return EO;
58    pragma Import
59      (Ada, Current_Target_Exception, "__gnat_current_target_exception");
60    --  Import this subprogram from the private part of Ada.Exceptions
61
62    --  First we have the access subprogram types used to establish the links.
63    --  The approach is to establish variables containing access subprogram
64    --  values, which by default point to dummy no tasking versions of routines.
65
66    type No_Param_Proc     is access procedure;
67    type Addr_Param_Proc   is access procedure (Addr : Address);
68    type EO_Param_Proc     is access procedure (Excep : EO);
69
70    type Get_Address_Call  is access function return Address;
71    type Set_Address_Call  is access procedure (Addr : Address);
72    type Set_Address_Call2 is access procedure
73      (Self_ID : Address; Addr : Address);
74
75    type Get_Integer_Call  is access function return Integer;
76    type Set_Integer_Call  is access procedure (Len : Integer);
77
78    type Get_EOA_Call      is access function return EOA;
79    type Set_EOA_Call      is access procedure (Excep : EOA);
80    type Set_EO_Call       is access procedure (Excep : EO);
81
82    type Special_EO_Call   is access
83      procedure (Excep : EO := Current_Target_Exception);
84
85    type Timed_Delay_Call  is access
86      procedure (Time : Duration; Mode : Integer);
87
88    type Get_Stack_Access_Call is access
89      function return Stack_Checking.Stack_Access;
90
91    type Task_Name_Call is access
92      function return String;
93
94    --  Suppress checks on all these types, since we know the corrresponding
95    --  values can never be null (the soft links are always initialized).
96
97    pragma Suppress (Access_Check, No_Param_Proc);
98    pragma Suppress (Access_Check, Addr_Param_Proc);
99    pragma Suppress (Access_Check, EO_Param_Proc);
100    pragma Suppress (Access_Check, Get_Address_Call);
101    pragma Suppress (Access_Check, Set_Address_Call);
102    pragma Suppress (Access_Check, Set_Address_Call2);
103    pragma Suppress (Access_Check, Get_Integer_Call);
104    pragma Suppress (Access_Check, Set_Integer_Call);
105    pragma Suppress (Access_Check, Get_EOA_Call);
106    pragma Suppress (Access_Check, Set_EOA_Call);
107    pragma Suppress (Access_Check, Timed_Delay_Call);
108    pragma Suppress (Access_Check, Get_Stack_Access_Call);
109    pragma Suppress (Access_Check, Task_Name_Call);
110
111    --  The following one is not related to tasking/no-tasking but to the
112    --  traceback decorators for exceptions.
113
114    type Traceback_Decorator_Wrapper_Call is access
115      function (Traceback : System.Address;
116                Len       : Natural)
117                return      String;
118
119    --  Declarations for the no tasking versions of the required routines
120
121    procedure Abort_Defer_NT;
122    --  Defer task abort (non-tasking case, does nothing)
123
124    procedure Abort_Undefer_NT;
125    --  Undefer task abort (non-tasking case, does nothing)
126
127    procedure Abort_Handler_NT;
128    --  Handle task abort (non-tasking case, does nothing). Currently, only VMS
129    --  uses this.
130
131    procedure Update_Exception_NT (X : EO := Current_Target_Exception);
132    --  Handle exception setting. This routine is provided for targets that
133    --  have built-in exception handling such as the Java Virtual Machine.
134    --  Currently, only JGNAT uses this. See 4jexcept.ads for an explanation on
135    --  how this routine is used.
136
137    function Check_Abort_Status_NT return Integer;
138    --  Returns Boolean'Pos (True) iff abort signal should raise
139    --  Standard.Abort_Signal.
140
141    procedure Task_Lock_NT;
142    --  Lock out other tasks (non-tasking case, does nothing)
143
144    procedure Task_Unlock_NT;
145    --  Release lock set by Task_Lock (non-tasking case, does nothing)
146
147    procedure Task_Termination_NT (Excep : EO);
148    --  Handle task termination routines for the environment task (non-tasking
149    --  case, does nothing).
150
151    procedure Null_Finalize_Global_List;
152    --  Finalize global list for controlled objects (does nothing)
153
154    procedure Adafinal_NT;
155    --  Shuts down the runtime system (non-tasking case)
156
157    Abort_Defer : No_Param_Proc := Abort_Defer_NT'Access;
158    pragma Suppress (Access_Check, Abort_Defer);
159    --  Defer task abort (task/non-task case as appropriate)
160
161    Abort_Undefer : No_Param_Proc := Abort_Undefer_NT'Access;
162    pragma Suppress (Access_Check, Abort_Undefer);
163    --  Undefer task abort (task/non-task case as appropriate)
164
165    Abort_Handler : No_Param_Proc := Abort_Handler_NT'Access;
166    --  Handle task abort (task/non-task case as appropriate)
167
168    Update_Exception : Special_EO_Call := Update_Exception_NT'Access;
169    --  Handle exception setting and tasking polling when appropriate
170
171    Check_Abort_Status : Get_Integer_Call := Check_Abort_Status_NT'Access;
172    --  Called when Abort_Signal is delivered to the process.  Checks to
173    --  see if signal should result in raising Standard.Abort_Signal.
174
175    Lock_Task : No_Param_Proc := Task_Lock_NT'Access;
176    --  Locks out other tasks. Preceding a section of code by Task_Lock and
177    --  following it by Task_Unlock creates a critical region. This is used
178    --  for ensuring that a region of non-tasking code (such as code used to
179    --  allocate memory) is tasking safe. Note that it is valid for calls to
180    --  Task_Lock/Task_Unlock to be nested, and this must work properly, i.e.
181    --  only the corresponding outer level Task_Unlock will actually unlock.
182    --  This routine also prevents against asynchronous aborts (abort is
183    --  deferred).
184
185    Unlock_Task : No_Param_Proc := Task_Unlock_NT'Access;
186    --  Releases lock previously set by call to Lock_Task. In the nested case,
187    --  all nested locks must be released before other tasks competing for the
188    --  tasking lock are released.
189    --
190    --  In the non nested case, this routine terminates the protection against
191    --  asynchronous aborts introduced by Lock_Task (unless abort was already
192    --  deferred before the call to Lock_Task (e.g in a protected procedures).
193    --
194    --  Note: the recommended protocol for using Lock_Task and Unlock_Task
195    --  is as follows:
196    --
197    --    Locked_Processing : begin
198    --       System.Soft_Links.Lock_Task.all;
199    --       ...
200    --       System.Soft_Links..Unlock_Task.all;
201    --
202    --    exception
203    --       when others =>
204    --          System.Soft_Links..Unlock_Task.all;
205    --          raise;
206    --    end Locked_Processing;
207    --
208    --  This ensures that the lock is not left set if an exception is raised
209    --  explicitly or implicitly during the critical locked region.
210
211    Task_Termination_Handler : EO_Param_Proc := Task_Termination_NT'Access;
212    --  Handle task termination routines (task/non-task case as appropriate)
213
214    Finalize_Global_List : No_Param_Proc := Null_Finalize_Global_List'Access;
215    --  Performs finalization of global list for controlled objects
216
217    Adafinal : No_Param_Proc := Adafinal_NT'Access;
218    --  Performs the finalization of the Ada Runtime
219
220    function  Get_Jmpbuf_Address_NT return  Address;
221    procedure Set_Jmpbuf_Address_NT (Addr : Address);
222
223    Get_Jmpbuf_Address : Get_Address_Call := Get_Jmpbuf_Address_NT'Access;
224    Set_Jmpbuf_Address : Set_Address_Call := Set_Jmpbuf_Address_NT'Access;
225
226    function  Get_Sec_Stack_Addr_NT return  Address;
227    procedure Set_Sec_Stack_Addr_NT (Addr : Address);
228
229    Get_Sec_Stack_Addr : Get_Address_Call := Get_Sec_Stack_Addr_NT'Access;
230    Set_Sec_Stack_Addr : Set_Address_Call := Set_Sec_Stack_Addr_NT'Access;
231
232    function Get_Exc_Stack_Addr_NT return Address;
233    Get_Exc_Stack_Addr : Get_Address_Call := Get_Exc_Stack_Addr_NT'Access;
234
235    function  Get_Current_Excep_NT return EOA;
236
237    Get_Current_Excep : Get_EOA_Call := Get_Current_Excep_NT'Access;
238
239    function Get_Stack_Info_NT return Stack_Checking.Stack_Access;
240
241    Get_Stack_Info : Get_Stack_Access_Call := Get_Stack_Info_NT'Access;
242
243    --------------------------
244    -- Master_Id Soft-Links --
245    --------------------------
246
247    --  Soft-Links are used for procedures that manipulate Master_Ids because
248    --  a Master_Id must be generated for access to limited class-wide types,
249    --  whose root may be extended with task components.
250
251    function Current_Master_NT return Integer;
252    procedure Enter_Master_NT;
253    procedure Complete_Master_NT;
254
255    Current_Master  : Get_Integer_Call :=  Current_Master_NT'Access;
256    Enter_Master    : No_Param_Proc    :=  Enter_Master_NT'Access;
257    Complete_Master : No_Param_Proc    :=  Complete_Master_NT'Access;
258
259    ----------------------
260    -- Delay Soft-Links --
261    ----------------------
262
263    --  Soft-Links are used for procedures that manipulate time to avoid
264    --  dragging the tasking run time when using delay statements.
265
266    Timed_Delay : Timed_Delay_Call;
267
268    --------------------------
269    -- Task Name Soft-Links --
270    --------------------------
271
272    function Task_Name_NT return String;
273
274    Task_Name : Task_Name_Call := Task_Name_NT'Access;
275
276    -------------------------------------
277    -- Exception Tracebacks Soft-Links --
278    -------------------------------------
279
280    Traceback_Decorator_Wrapper : Traceback_Decorator_Wrapper_Call;
281    --  Wrapper to the possible user specified traceback decorator to be
282    --  called during automatic output of exception data.
283
284    --  The nullity of this wrapper shall correspond to the nullity of the
285    --  current actual decorator. This is ensured first by the null initial
286    --  value of the corresponding variables, and then by Set_Trace_Decorator
287    --  in g-exctra.adb.
288
289    pragma Atomic (Traceback_Decorator_Wrapper);
290    --  Since concurrent read/write operations may occur on this variable.
291    --  See the body of Tailored_Exception_Traceback in Ada.Exceptions for
292    --  a more detailed description of the potential problems.
293
294    ------------------------
295    -- Task Specific Data --
296    ------------------------
297
298    --  Here we define a single type that encapsulates the various task
299    --  specific data. This type is used to store the necessary data into
300    --  the Task_Control_Block or into a global variable in the non tasking
301    --  case.
302
303    type TSD is record
304       Pri_Stack_Info : aliased Stack_Checking.Stack_Info;
305       --  Information on stack (Base/Limit/Size) that is used
306       --  by System.Stack_Checking. If this TSD does not belong to
307       --  the environment task, the Size field must be initialized
308       --  to the tasks requested stack size before the task can do
309       --  its first stack check.
310
311       pragma Warnings (Off);
312       Jmpbuf_Address : System.Address := System.Null_Address;
313       --  Address of jump buffer used to store the address of the
314       --  current longjmp/setjmp buffer for exception management.
315       --  These buffers are threaded into a stack, and the address
316       --  here is the top of the stack. A null address means that
317       --  no exception handler is currently active.
318
319       Sec_Stack_Addr : System.Address := System.Null_Address;
320       pragma Warnings (On);
321       --  Address of currently allocated secondary stack
322
323       Current_Excep : aliased EO;
324       --  Exception occurrence that contains the information for the
325       --  current exception. Note that any exception in the same task
326       --  destroys this information, so the data in this variable must
327       --  be copied out before another exception can occur.
328       --
329       --  Also act as a list of the active exceptions in the case of the GCC
330       --  exception mechanism, organized as a stack with the most recent first.
331    end record;
332
333    procedure Create_TSD (New_TSD : in out TSD);
334    pragma Inline (Create_TSD);
335    --  Called from s-tassta when a new thread is created to perform
336    --  any required initialization of the TSD.
337
338    procedure Destroy_TSD (Old_TSD : in out TSD);
339    pragma Inline (Destroy_TSD);
340    --  Called from s-tassta just before a thread is destroyed to perform
341    --  any required finalization.
342
343    function Get_GNAT_Exception return Ada.Exceptions.Exception_Id;
344    pragma Inline (Get_GNAT_Exception);
345    --  This function obtains the Exception_Id from the Exception_Occurrence
346    --  referenced by the Current_Excep field of the task specific data, i.e.
347    --  the call is equivalent to
348    --  Exception_Identity (Get_Current_Exception.all)
349
350    --  Export the Get/Set routines for the various Task Specific Data (TSD)
351    --  elements as callable subprograms instead of objects of access to
352    --  subprogram types.
353
354    function  Get_Jmpbuf_Address_Soft return  Address;
355    procedure Set_Jmpbuf_Address_Soft (Addr : Address);
356    pragma Inline (Get_Jmpbuf_Address_Soft);
357    pragma Inline (Set_Jmpbuf_Address_Soft);
358
359    function  Get_Sec_Stack_Addr_Soft return  Address;
360    procedure Set_Sec_Stack_Addr_Soft (Addr : Address);
361    pragma Inline (Get_Sec_Stack_Addr_Soft);
362    pragma Inline (Set_Sec_Stack_Addr_Soft);
363
364    function Get_Exc_Stack_Addr_Soft return Address;
365
366 end System.Soft_Links;