| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . T A S K I N G -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 |
with System.Task_Primitives.Operations; |
| 33 |
with System.Storage_Elements; |
| 34 |
|
| 35 |
package body System.Tasking is |
| 36 |
|
| 37 |
package STPO renames System.Task_Primitives.Operations; |
| 38 |
|
| 39 |
--------------------- |
| 40 |
-- Detect_Blocking -- |
| 41 |
--------------------- |
| 42 |
|
| 43 |
function Detect_Blocking return Boolean is |
| 44 |
GL_Detect_Blocking : Integer; |
| 45 |
pragma Import (C, GL_Detect_Blocking, "__gl_detect_blocking"); |
| 46 |
-- Global variable exported by the binder generated file. A value equal |
| 47 |
-- to 1 indicates that pragma Detect_Blocking is active, while 0 is used |
| 48 |
-- for the pragma not being present. |
| 49 |
|
| 50 |
begin |
| 51 |
return GL_Detect_Blocking = 1; |
| 52 |
end Detect_Blocking; |
| 53 |
|
| 54 |
----------------------- |
| 55 |
-- Number_Of_Entries -- |
| 56 |
----------------------- |
| 57 |
|
| 58 |
function Number_Of_Entries (Self_Id : Task_Id) return Entry_Index is |
| 59 |
begin |
| 60 |
return Entry_Index (Self_Id.Entry_Num); |
| 61 |
end Number_Of_Entries; |
| 62 |
|
| 63 |
---------- |
| 64 |
-- Self -- |
| 65 |
---------- |
| 66 |
|
| 67 |
function Self return Task_Id renames STPO.Self; |
| 68 |
|
| 69 |
------------------ |
| 70 |
-- Storage_Size -- |
| 71 |
------------------ |
| 72 |
|
| 73 |
function Storage_Size (T : Task_Id) return System.Parameters.Size_Type is |
| 74 |
begin |
| 75 |
return |
| 76 |
System.Parameters.Size_Type |
| 77 |
(T.Common.Compiler_Data.Pri_Stack_Info.Size); |
| 78 |
end Storage_Size; |
| 79 |
|
| 80 |
--------------------- |
| 81 |
-- Initialize_ATCB -- |
| 82 |
--------------------- |
| 83 |
|
| 84 |
procedure Initialize_ATCB |
| 85 |
(Self_ID : Task_Id; |
| 86 |
Task_Entry_Point : Task_Procedure_Access; |
| 87 |
Task_Arg : System.Address; |
| 88 |
Parent : Task_Id; |
| 89 |
Elaborated : Access_Boolean; |
| 90 |
Base_Priority : System.Any_Priority; |
| 91 |
Base_CPU : System.Multiprocessors.CPU_Range; |
| 92 |
CPU_Is_Explicit : Boolean; |
| 93 |
Domain : Dispatching_Domain_Access; |
| 94 |
Task_Info : System.Task_Info.Task_Info_Type; |
| 95 |
Stack_Size : System.Parameters.Size_Type; |
| 96 |
T : Task_Id; |
| 97 |
Success : out Boolean) is |
| 98 |
begin |
| 99 |
T.Common.State := Unactivated; |
| 100 |
|
| 101 |
-- Initialize T.Common.LL |
| 102 |
|
| 103 |
STPO.Initialize_TCB (T, Success); |
| 104 |
|
| 105 |
if not Success then |
| 106 |
return; |
| 107 |
end if; |
| 108 |
|
| 109 |
-- Note that use of an aggregate here for this assignment |
| 110 |
-- would be illegal, because Common_ATCB is limited because |
| 111 |
-- Task_Primitives.Private_Data is limited. |
| 112 |
|
| 113 |
T.Common.Parent := Parent; |
| 114 |
T.Common.Base_Priority := Base_Priority; |
| 115 |
T.Common.CPU_Is_Explicit := CPU_Is_Explicit; |
| 116 |
T.Common.Base_CPU := Base_CPU; |
| 117 |
|
| 118 |
-- The Domain defaults to that of the activator. But that can be null in |
| 119 |
-- the case of foreign threads (see Register_Foreign_Thread), in which |
| 120 |
-- case we default to the System_Domain. |
| 121 |
|
| 122 |
if Domain /= null then |
| 123 |
T.Common.Domain := Domain; |
| 124 |
elsif Self_ID.Common.Domain /= null then |
| 125 |
T.Common.Domain := Self_ID.Common.Domain; |
| 126 |
else |
| 127 |
T.Common.Domain := System_Domain; |
| 128 |
end if; |
| 129 |
pragma Assert (T.Common.Domain /= null); |
| 130 |
|
| 131 |
T.Common.Current_Priority := Priority'First; |
| 132 |
T.Common.Protected_Action_Nesting := 0; |
| 133 |
T.Common.Call := null; |
| 134 |
T.Common.Task_Arg := Task_Arg; |
| 135 |
T.Common.Task_Entry_Point := Task_Entry_Point; |
| 136 |
T.Common.Activator := Self_ID; |
| 137 |
T.Common.Wait_Count := 0; |
| 138 |
T.Common.Elaborated := Elaborated; |
| 139 |
T.Common.Activation_Failed := False; |
| 140 |
T.Common.Task_Info := Task_Info; |
| 141 |
T.Common.Global_Task_Lock_Nesting := 0; |
| 142 |
T.Common.Fall_Back_Handler := null; |
| 143 |
T.Common.Specific_Handler := null; |
| 144 |
T.Common.Debug_Events := [others => False]; |
| 145 |
T.Common.Task_Image_Len := 0; |
| 146 |
|
| 147 |
if T.Common.Parent = null then |
| 148 |
|
| 149 |
-- For the environment task, the adjusted stack size is meaningless. |
| 150 |
-- For example, an unspecified Stack_Size means that the stack size |
| 151 |
-- is determined by the environment, or can grow dynamically. The |
| 152 |
-- Stack_Checking algorithm therefore needs to use the requested |
| 153 |
-- size, or 0 in case of an unknown size. |
| 154 |
|
| 155 |
T.Common.Compiler_Data.Pri_Stack_Info.Size := |
| 156 |
Storage_Elements.Storage_Offset (Stack_Size); |
| 157 |
|
| 158 |
else |
| 159 |
T.Common.Compiler_Data.Pri_Stack_Info.Size := |
| 160 |
Storage_Elements.Storage_Offset |
| 161 |
(Parameters.Adjust_Storage_Size (Stack_Size)); |
| 162 |
end if; |
| 163 |
|
| 164 |
-- Link the task into the list of all tasks |
| 165 |
|
| 166 |
T.Common.All_Tasks_Link := All_Tasks_List; |
| 167 |
All_Tasks_List := T; |
| 168 |
end Initialize_ATCB; |
| 169 |
|
| 170 |
---------------- |
| 171 |
-- Initialize -- |
| 172 |
---------------- |
| 173 |
|
| 174 |
Main_Task_Image : constant String := "main_task"; |
| 175 |
-- Image of environment task |
| 176 |
|
| 177 |
Main_Priority : constant Integer; |
| 178 |
pragma Import (C, Main_Priority, "__gl_main_priority"); |
| 179 |
-- Priority for main task. Note that this is of type Integer, not Priority, |
| 180 |
-- because we use the value -1 to indicate the default main priority, and |
| 181 |
-- that is of course not in Priority'range. |
| 182 |
|
| 183 |
Main_CPU : constant Integer; |
| 184 |
pragma Import (C, Main_CPU, "__gl_main_cpu"); |
| 185 |
-- Affinity for main task. Note that this is of type Integer, not |
| 186 |
-- CPU_Range, because we use the value -1 to indicate the unassigned |
| 187 |
-- affinity, and that is of course not in CPU_Range'Range. |
| 188 |
|
| 189 |
Initialized : Boolean := False; |
| 190 |
-- Used to prevent multiple calls to Initialize |
| 191 |
|
| 192 |
procedure Initialize is |
| 193 |
T : Task_Id; |
| 194 |
Base_Priority : Any_Priority; |
| 195 |
Base_CPU : System.Multiprocessors.CPU_Range; |
| 196 |
Success : Boolean; |
| 197 |
|
| 198 |
use type System.Multiprocessors.CPU_Range; |
| 199 |
|
| 200 |
begin |
| 201 |
if Initialized then |
| 202 |
return; |
| 203 |
end if; |
| 204 |
|
| 205 |
Initialized := True; |
| 206 |
|
| 207 |
-- Initialize Environment Task |
| 208 |
|
| 209 |
Base_Priority := |
| 210 |
(if Main_Priority = Unspecified_Priority |
| 211 |
then Default_Priority |
| 212 |
else Priority (Main_Priority)); |
| 213 |
|
| 214 |
Base_CPU := |
| 215 |
(if Main_CPU = Unspecified_CPU |
| 216 |
then System.Multiprocessors.Not_A_Specific_CPU |
| 217 |
else System.Multiprocessors.CPU_Range (Main_CPU)); |
| 218 |
|
| 219 |
-- At program start-up the environment task is allocated to the default |
| 220 |
-- system dispatching domain. |
| 221 |
-- Make sure that the processors which are not available are not taken |
| 222 |
-- into account. Use Number_Of_CPUs to know the exact number of |
| 223 |
-- processors in the system at execution time. |
| 224 |
|
| 225 |
System_Domain := |
| 226 |
new Dispatching_Domain' |
| 227 |
(Multiprocessors.CPU'First .. Multiprocessors.Number_Of_CPUs => |
| 228 |
True); |
| 229 |
|
| 230 |
T := STPO.New_ATCB (0); |
| 231 |
Initialize_ATCB |
| 232 |
(Self_ID => null, |
| 233 |
Task_Entry_Point => null, |
| 234 |
Task_Arg => Null_Address, |
| 235 |
Parent => Null_Task, |
| 236 |
Elaborated => null, |
| 237 |
Base_Priority => Base_Priority, |
| 238 |
Base_CPU => Base_CPU, |
| 239 |
CPU_Is_Explicit => Main_CPU /= Unspecified_CPU, |
| 240 |
Domain => System_Domain, |
| 241 |
Task_Info => Task_Info.Unspecified_Task_Info, |
| 242 |
Stack_Size => 0, |
| 243 |
T => T, |
| 244 |
Success => Success); |
| 245 |
pragma Assert (Success); |
| 246 |
|
| 247 |
STPO.Initialize (T); |
| 248 |
STPO.Set_Priority (T, T.Common.Base_Priority); |
| 249 |
T.Common.State := Runnable; |
| 250 |
T.Common.Task_Image_Len := Main_Task_Image'Length; |
| 251 |
T.Common.Task_Image (Main_Task_Image'Range) := Main_Task_Image; |
| 252 |
|
| 253 |
Dispatching_Domain_Tasks := |
| 254 |
new Array_Allocated_Tasks' |
| 255 |
(Multiprocessors.CPU'First .. Multiprocessors.Number_Of_CPUs => 0); |
| 256 |
|
| 257 |
-- Signal that this task is being allocated to a processor |
| 258 |
|
| 259 |
if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then |
| 260 |
|
| 261 |
-- Increase the number of tasks attached to the CPU to which this |
| 262 |
-- task is allocated. |
| 263 |
|
| 264 |
Dispatching_Domain_Tasks (Base_CPU) := |
| 265 |
Dispatching_Domain_Tasks (Base_CPU) + 1; |
| 266 |
end if; |
| 267 |
|
| 268 |
-- The full initialization of the environment task's Entry_Calls array |
| 269 |
-- is deferred to Init_RTS because only the first element of the array |
| 270 |
-- is used by the restricted Ravenscar runtime. |
| 271 |
|
| 272 |
T.Entry_Calls (T.Entry_Calls'First).Self := T; |
| 273 |
T.Entry_Calls (T.Entry_Calls'First).Level := T.Entry_Calls'First; |
| 274 |
|
| 275 |
end Initialize; |
| 276 |
end System.Tasking; |