OSDN Git Service

2005-03-29 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tassta.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
4 --                                                                          --
5 --                 S Y S T E M . T A S K I N G . S T A G E S                --
6 --                                                                          --
7 --                                  S p e c                                 --
8 --                                                                          --
9 --          Copyright (C) 1992-2005, 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 represents the high level tasking interface used by the
35 --  compiler to expand Ada 95 tasking constructs into simpler run time calls
36 --  (aka GNARLI, GNU Ada Run-time Library Interface)
37
38 --  Note: Only the compiler is allowed to use this interface, by generating
39 --  direct calls to it, via Rtsfind.
40 --  Any changes to this interface may require corresponding compiler changes
41 --  in exp_ch9.adb and possibly exp_ch7.adb
42
43 with System.Task_Info;
44 --  used for Task_Info_Type
45
46 with System.Parameters;
47 --  used for Size_Type
48
49 package System.Tasking.Stages is
50    pragma Elaborate_Body;
51
52    --   The compiler will expand in the GNAT tree the following construct:
53
54    --   task type T (Discr : Integer);
55
56    --   task body T is
57    --      ...declarations, possibly some controlled...
58    --   begin
59    --      ...B...;
60    --   end T;
61
62    --   T1 : T (1);
63
64    --  as follows:
65
66    --   enter_master.all;
67
68    --   _chain : aliased activation_chain;
69    --   activation_chainIP (_chain);
70
71    --   task type t (discr : integer);
72    --   tE : aliased boolean := false;
73    --   tZ : size_type := unspecified_size;
74    --   type tV (discr : integer) is limited record
75    --      _task_id : task_id;
76    --   end record;
77    --   procedure tB (_task : access tV);
78    --   freeze tV [
79    --      procedure tVIP (_init : in out tV; _master : master_id;
80    --        _chain : in out activation_chain; _task_id : in task_image_type;
81    --        discr : integer) is
82    --      begin
83    --         _init.discr := discr;
84    --         _init._task_id := null;
85    --         create_task (unspecified_priority, tZ,
86    --           unspecified_task_info, 0, _master,
87    --           task_procedure_access!(tB'address),
88    --           _init'address, tE'unchecked_access, _chain, _task_id, _init.
89    --           _task_id);
90    --         return;
91    --      end tVIP;
92    --   ]
93
94    --   procedure tB (_task : access tV) is
95    --      discr : integer renames _task.discr;
96
97    --      procedure _clean is
98    --      begin
99    --         abort_defer.all;
100    --         complete_task;
101    --         finalize_list (F14b);
102    --         abort_undefer.all;
103    --         return;
104    --      end _clean;
105    --   begin
106    --      abort_undefer.all;
107    --      ...declarations...
108    --      complete_activation;
109    --      ...B...;
110    --      return;
111    --   at end
112    --      _clean;
113    --   end tB;
114
115    --   tE := true;
116    --   t1 : t (1);
117    --   _master : constant master_id := current_master.all;
118    --   t1S : task_image_type := new string'"t1";
119    --   task_image_typeIP (t1, _master, _chain, t1S, 1);
120
121    --   activate_tasks (_chain'unchecked_access);
122
123    procedure Abort_Tasks (Tasks : Task_List);
124    --  Compiler interface only. Do not call from within the RTS. Initiate
125    --  abort, however, the actual abort is done by abortee by means of
126    --  Abort_Handler and Abort_Undefer
127    --
128    --  source code:
129    --     Abort T1, T2;
130    --  code expansion:
131    --     abort_tasks (task_list'(t1._task_id, t2._task_id));
132
133    procedure Activate_Tasks (Chain_Access : Activation_Chain_Access);
134    --  Compiler interface only. Do not call from within the RTS.
135    --  This must be called by the creator of a chain of one or more new tasks,
136    --  to activate them. The chain is a linked list that up to this point is
137    --  only known to the task that created them, though the individual tasks
138    --  are already in the All_Tasks_List.
139    --
140    --  The compiler builds the chain in LIFO order (as a stack). Another
141    --  version of this procedure had code to reverse the chain, so as to
142    --  activate the tasks in the order of declaration. This might be nice, but
143    --  it is not needed if priority-based scheduling is supported, since all
144    --  the activated tasks synchronize on the activators lock before they
145    --  start activating and so they should start activating in priority order.
146
147    procedure Complete_Activation;
148    --  Compiler interface only. Do not call from within the RTS.
149    --  This should be called from the task body at the end of
150    --  the elaboration code for its declarative part.
151    --  Decrement the count of tasks to be activated by the activator and
152    --  wake it up so it can check to see if all tasks have been activated.
153    --  Except for the environment task, which should never call this procedure,
154    --  T.Activator should only be null iff T has completed activation.
155
156    procedure Complete_Master;
157    --  Compiler interface only.  Do not call from within the RTS. This must
158    --  be called on exit from any master where Enter_Master was called.
159    --  Assume abort is deferred at this point.
160
161    procedure Complete_Task;
162    --  Compiler interface only. Do not call from within the RTS.
163    --  This should be called from an implicit at-end handler
164    --  associated with the task body, when it completes.
165    --  From this point, the current task will become not callable.
166    --  If the current task have not completed activation, this should be done
167    --  now in order to wake up the activator (the environment task).
168
169    procedure Create_Task
170      (Priority      : Integer;
171       Size          : System.Parameters.Size_Type;
172       Task_Info     : System.Task_Info.Task_Info_Type;
173       Num_Entries   : Task_Entry_Index;
174       Master        : Master_Level;
175       State         : Task_Procedure_Access;
176       Discriminants : System.Address;
177       Elaborated    : Access_Boolean;
178       Chain         : in out Activation_Chain;
179       Task_Image    : String;
180       Created_Task  : out Task_Id);
181    --  Compiler interface only. Do not call from within the RTS.
182    --  This must be called to create a new task.
183    --
184    --  Priority is the task's priority (assumed to be in the
185    --   System.Any_Priority'Range)
186    --  Size is the stack size of the task to create
187    --  Task_Info is the task info associated with the created task, or
188    --   Unspecified_Task_Info if none.
189    --  State is the compiler generated task's procedure body
190    --  Discriminants is a pointer to a limited record whose discriminants
191    --   are those of the task to create. This parameter should be passed as
192    --   the single argument to State.
193    --  Elaborated is a pointer to a Boolean that must be set to true on exit
194    --   if the task could be sucessfully elaborated.
195    --  Chain is a linked list of task that needs to be created. On exit,
196    --   Created_Task.Activation_Link will be Chain.T_ID, and Chain.T_ID
197    --   will be Created_Task (e.g the created task will be linked at the front
198    --   of Chain).
199    --  Task_Image is a string created by the compiler that the
200    --   run time can store to ease the debugging and the
201    --   Ada.Task_Identification facility.
202    --  Created_Task is the resulting task.
203    --
204    --  This procedure can raise Storage_Error if the task creation failed.
205
206    function Current_Master return Master_Level;
207    --  Compiler interface only.
208    --  This is called to obtain the current master nesting level.
209
210    procedure Enter_Master;
211    --  Compiler interface only.  Do not call from within the RTS.
212    --  This must be called on entry to any "master" where a task,
213    --  or access type designating objects containing tasks, may be
214    --  declared.
215
216    procedure Expunge_Unactivated_Tasks (Chain : in out Activation_Chain);
217    --  Compiler interface only. Do not call from within the RTS.
218    --  This must be called by the compiler-generated code for an allocator if
219    --  the allocated object contains tasks, if the allocator exits without
220    --  calling Activate_Tasks for a given activation chains, as can happen if
221    --  an exception occurs during initialization of the object.
222    --
223    --  This should be called ONLY for tasks created via an allocator. Recovery
224    --  of storage for unactivated local task declarations is done by
225    --  Complete_Master and Complete_Task.
226    --
227    --  We remove each task from Chain and All_Tasks_List before we free the
228    --  storage of its ATCB.
229    --
230    --  In other places where we recover the storage of unactivated tasks, we
231    --  need to clean out the entry queues, but here that should not be
232    --  necessary, since these tasks should not have been visible to any other
233    --  tasks, and so no task should be able to queue a call on their entries.
234    --
235    --  Just in case somebody misuses this subprogram, there is a check to
236    --  verify this condition.
237
238    procedure Finalize_Global_Tasks;
239    --  This should be called to complete the execution of the environment task
240    --  and shut down the tasking runtime system. It is the equivalent of
241    --  Complete_Task, but for the environment task.
242    --
243    --  The environment task must first call Complete_Master, to wait for user
244    --  tasks that depend on library-level packages to terminate. It then calls
245    --  Abort_Dependents to abort the "independent" library-level server tasks
246    --  that are created implicitly by the RTS packages (signal and timer server
247    --  tasks), and then waits for them to terminate. Then, it calls
248    --  Vulnerable_Complete_Task.
249    --
250    --  It currently also executes the global finalization list, and then resets
251    --  the "soft links".
252
253    procedure Free_Task (T : Task_Id);
254    --  Recover all runtime system storage associated with the task T, but only
255    --  if T has terminated. Do nothing in the other case. It is called from
256    --  Unchecked_Deallocation, for objects that are or contain tasks.
257
258    function Terminated (T : Task_Id) return Boolean;
259    --  This is called by the compiler to implement the 'Terminated attribute.
260    --  Though is not required to be so by the ARM, we choose to synchronize
261    --  with the task's ATCB, so that this is more useful for polling the state
262    --  of a task, and so that it becomes an abort completion point for the
263    --  calling task (via Undefer_Abort).
264    --
265    --  source code:
266    --     T1'Terminated
267    --
268    --  code expansion:
269    --     terminated (t1._task_id)
270
271    procedure Terminate_Task (Self_ID : Task_Id);
272    --  Terminate the calling task.
273    --  This should only be called by the Task_Wrapper procedure, and to
274    --  deallocate storage associate with foreign tasks.
275
276 end System.Tasking.Stages;