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