1------------------------------------------------------------------------------
2--                                                                          --
3--                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4--                                                                          --
5--     S Y S T E M . T A S K I N G . R E S T R I C T E D . S T A G E S      --
6--                                                                          --
7--                                  S p e c                                 --
8--                                                                          --
9--          Copyright (C) 1992-2019, 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 is a simplified version of the System.Tasking.Stages package,
33--  intended to be used in a restricted run time.
34
35--  This package represents the high level tasking interface used by the
36--  compiler to expand Ada 95 tasking constructs into simpler run time calls
37--  (aka GNARLI, GNU Ada Run-time Library Interface)
38
39--  Note: the compiler generates direct calls to this interface, 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--  The restricted GNARLI is also composed of System.Protected_Objects and
44--  System.Protected_Objects.Single_Entry
45
46with System.Parameters;
47with System.Secondary_Stack;
48with System.Task_Info;
49
50package System.Tasking.Restricted.Stages is
51   pragma Elaborate_Body;
52
53   ---------------------------------
54   -- Compiler Interface (GNARLI) --
55   ---------------------------------
56
57   --  The compiler will expand in the GNAT tree the following construct:
58
59   --   task type T (Discr : Integer);
60
61   --   task body T is
62   --      ...declarations, possibly some controlled...
63   --   begin
64   --      ...B...;
65   --   end T;
66
67   --   T1 : T (1);
68
69   --  as follows:
70
71   --   task type t (discr : integer);
72   --   tE : aliased boolean := false;
73   --   tZ : size_type := unspecified_size;
74
75   --   type tV (discr : integer) is limited record
76   --      _task_id : task_id;
77   --      _atcb : aliased system__tasking__ada_task_control_block (0);
78   --   end record;
79
80   --   procedure tB (_task : access tV);
81   --   freeze tV [
82   --      procedure tVIP (_init : in out tV; _master : master_id;
83   --        _chain : in out activation_chain; _task_name : in string;
84   --        discr : integer) is
85   --      begin
86   --         _init.discr := discr;
87   --         _init._task_id := null;
88   --         system__tasking__ada_task_control_blockIP (_init._atcb, 0);
89   --         _init._task_id := _init._atcb'unchecked_access;
90   --         create_restricted_task (unspecified_priority, tZ,
91   --           unspecified_task_info, unspecified_cpu,
92   --           task_procedure_access!(tB'address), _init'address,
93   --           tE'unchecked_access, _task_name, _init._task_id);
94   --         return;
95   --      end tVIP;
96
97   --   _chain : aliased activation_chain;
98   --   activation_chainIP (_chain);
99
100   --   procedure tB (_task : access tV) is
101   --      discr : integer renames _task.discr;
102
103   --      procedure _clean is
104   --      begin
105   --         complete_restricted_task;
106   --         finalize_list (F14b);
107   --         return;
108   --      end _clean;
109
110   --   begin
111   --      ...declarations...
112   --      complete_restricted_activation;
113   --      ...B...;
114   --      return;
115   --   at end
116   --      _clean;
117   --   end tB;
118
119   --   tE := true;
120   --   t1 : t (1);
121   --   t1S : constant String := "t1";
122   --   tIP (t1, 3, _chain, t1S, 1);
123
124   Partition_Elaboration_Policy : Character := 'C';
125   pragma Export (C, Partition_Elaboration_Policy,
126                  "__gnat_partition_elaboration_policy");
127   --  Partition elaboration policy. Value can be either 'C' for concurrent,
128   --  which is the default or 'S' for sequential. This value can be modified
129   --  by the binder generated code, before calling elaboration code.
130
131   procedure Create_Restricted_Task
132     (Priority          : Integer;
133      Stack_Address     : System.Address;
134      Stack_Size        : System.Parameters.Size_Type;
135      Sec_Stack_Address : System.Secondary_Stack.SS_Stack_Ptr;
136      Sec_Stack_Size    : System.Parameters.Size_Type;
137      Task_Info         : System.Task_Info.Task_Info_Type;
138      CPU               : Integer;
139      State             : Task_Procedure_Access;
140      Discriminants     : System.Address;
141      Elaborated        : Access_Boolean;
142      Chain             : in out Activation_Chain;
143      Task_Image        : String;
144      Created_Task      : Task_Id);
145   --  Compiler interface only. Do not call from within the RTS.
146   --  This must be called to create a new task, when the partition
147   --  elaboration policy is not specified (or is concurrent).
148   --
149   --  Priority is the task's priority (assumed to be in the
150   --  System.Any_Priority'Range).
151   --
152   --  Stack_Address is the start address of the stack associated to the task,
153   --  in case it has been preallocated by the compiler; it is equal to
154   --  Null_Address when the stack needs to be allocated by the underlying
155   --  operating system.
156   --
157   --  Stack_Size is the stack size of the task to create.
158   --
159   --  Sec_Stack_Address is the pointer to the secondary stack created by the
160   --  compiler. If null, the secondary stack is either allocated by the binder
161   --  or the run-time.
162   --
163   --  Secondary_Stack_Size is the secondary stack size of the task to create.
164   --
165   --  Task_Info is the task info associated with the created task, or
166   --  Unspecified_Task_Info if none.
167   --
168   --  CPU is the task affinity. We pass it as an Integer to avoid an explicit
169   --   dependency from System.Multiprocessors when not needed. Static range
170   --   checks are performed when analyzing the pragma, and dynamic ones are
171   --   performed before setting the affinity at run time.
172   --
173   --  State is the compiler generated task's procedure body.
174   --
175   --  Discriminants is a pointer to a limited record whose discriminants are
176   --  those of the task to create. This parameter should be passed as the
177   --  single argument to State.
178   --
179   --  Elaborated is a pointer to a Boolean that must be set to true on exit
180   --  if the task could be successfully elaborated.
181   --
182   --  Chain is a linked list of task that needs to be created. On exit,
183   --  Created_Task.Activation_Link will be Chain.T_ID, and Chain.T_ID will be
184   --  Created_Task (the created task will be linked at the front of Chain).
185   --
186   --  Task_Image is a string created by the compiler that the run time can
187   --  store to ease the debugging and the Ada.Task_Identification facility.
188   --
189   --  Created_Task is the resulting task.
190   --
191   --  This procedure can raise Storage_Error if the task creation fails.
192
193   procedure Create_Restricted_Task_Sequential
194     (Priority          : Integer;
195      Stack_Address     : System.Address;
196      Stack_Size        : System.Parameters.Size_Type;
197      Sec_Stack_Address : System.Secondary_Stack.SS_Stack_Ptr;
198      Sec_Stack_Size    : System.Parameters.Size_Type;
199      Task_Info         : System.Task_Info.Task_Info_Type;
200      CPU               : Integer;
201      State             : Task_Procedure_Access;
202      Discriminants     : System.Address;
203      Elaborated        : Access_Boolean;
204      Task_Image        : String;
205      Created_Task      : Task_Id);
206   --  Compiler interface only. Do not call from within the RTS.
207   --  This must be called to create a new task, when the sequential partition
208   --  elaboration policy is used.
209   --
210   --  The parameters are the same as Create_Restricted_Task except there is
211   --  no Chain parameter (for the activation chain), as there is only one
212   --  global activation chain, which is declared in the body of this package.
213
214   procedure Activate_Restricted_Tasks
215     (Chain_Access : Activation_Chain_Access);
216   --  Compiler interface only. Do not call from within the RTS.
217   --  This must be called by the creator of a chain of one or more new tasks,
218   --  to activate them. The chain is a linked list that up to this point is
219   --  only known to the task that created them, though the individual tasks
220   --  are already in the All_Tasks_List.
221   --
222   --  The compiler builds the chain in LIFO order (as a stack). Another
223   --  version of this procedure had code to reverse the chain, so as to
224   --  activate the tasks in the order of declaration. This might be nice, but
225   --  it is not needed if priority-based scheduling is supported, since all
226   --  the activated tasks synchronize on the activators lock before they start
227   --  activating and so they should start activating in priority order.
228   --
229   --  When the partition elaboration policy is sequential, this procedure
230   --  does nothing, tasks will be activated at end of elaboration.
231
232   procedure Activate_All_Tasks_Sequential;
233   pragma Export (C, Activate_All_Tasks_Sequential,
234                  "__gnat_activate_all_tasks");
235   --  Binder interface only. Do not call from within the RTS. This must be
236   --  called an the end of the elaboration to activate all tasks, in order
237   --  to implement the sequential elaboration policy.
238
239   procedure Complete_Restricted_Activation;
240   --  Compiler interface only. Do not call from within the RTS. This should be
241   --  called from the task body at the end of the elaboration code for its
242   --  declarative part. Decrement the count of tasks to be activated by the
243   --  activator and wake it up so it can check to see if all tasks have been
244   --  activated. Except for the environment task, which should never call this
245   --  procedure, T.Activator should only be null iff T has completed
246   --  activation.
247
248   procedure Complete_Restricted_Task;
249   --  Compiler interface only. Do not call from within the RTS. This should be
250   --  called from an implicit at-end handler associated with the task body,
251   --  when it completes. From this point, the current task will become not
252   --  callable. If the current task have not completed activation, this should
253   --  be done now in order to wake up the activator (the environment task).
254
255   function Restricted_Terminated (T : Task_Id) return Boolean;
256   --  Compiler interface only. Do not call from within the RTS. This is called
257   --  by the compiler to implement the 'Terminated attribute.
258   --
259   --  source code:
260   --     T1'Terminated
261   --
262   --  code expansion:
263   --     restricted_terminated (t1._task_id)
264
265   procedure Finalize_Global_Tasks;
266   --  This is needed to support the compiler interface. It will only be called
267   --  by the Environment task in the binder generated file (by adafinal).
268   --  Instead, it will cause the Environment to block forever, since none of
269   --  the dependent tasks are expected to terminate
270
271end System.Tasking.Restricted.Stages;
272