| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT 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 |
-- 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 |
pragma Partition_Elaboration_Policy (Concurrent); |
| 33 |
-- This package only implements the concurrent elaboration policy. This pragma |
| 34 |
-- will enforce it (and detect conflicts with user specified policy). |
| 35 |
|
| 36 |
with Ada.Exceptions; |
| 37 |
with Ada.Unchecked_Deallocation; |
| 38 |
with Ada.Task_Initialization; |
| 39 |
|
| 40 |
with System.Interrupt_Management; |
| 41 |
with System.Tasking.Debug; |
| 42 |
with System.Address_Image; |
| 43 |
with System.Task_Primitives; |
| 44 |
with System.Task_Primitives.Operations; |
| 45 |
with System.Tasking.Utilities; |
| 46 |
with System.Tasking.Queuing; |
| 47 |
with System.Tasking.Rendezvous; |
| 48 |
with System.OS_Primitives; |
| 49 |
with System.Secondary_Stack; |
| 50 |
with System.Restrictions; |
| 51 |
with System.Standard_Library; |
| 52 |
with System.Stack_Usage; |
| 53 |
with System.Storage_Elements; |
| 54 |
|
| 55 |
with System.Soft_Links; |
| 56 |
-- These are procedure pointers to non-tasking routines that use task |
| 57 |
-- specific data. In the absence of tasking, these routines refer to global |
| 58 |
-- data. In the presence of tasking, they must be replaced with pointers to |
| 59 |
-- task-specific versions. Also used for Create_TSD, Destroy_TSD, Get_Current |
| 60 |
-- _Excep, Finalize_Library_Objects, Task_Termination, Handler. |
| 61 |
|
| 62 |
with System.Tasking.Initialization; |
| 63 |
pragma Elaborate_All (System.Tasking.Initialization); |
| 64 |
-- This insures that tasking is initialized if any tasks are created |
| 65 |
|
| 66 |
package body System.Tasking.Stages is |
| 67 |
|
| 68 |
package STPO renames System.Task_Primitives.Operations; |
| 69 |
package SSL renames System.Soft_Links; |
| 70 |
package SSE renames System.Storage_Elements; |
| 71 |
|
| 72 |
use Ada.Exceptions; |
| 73 |
|
| 74 |
use Secondary_Stack; |
| 75 |
use Task_Primitives; |
| 76 |
use Task_Primitives.Operations; |
| 77 |
|
| 78 |
----------------------- |
| 79 |
-- Local Subprograms -- |
| 80 |
----------------------- |
| 81 |
|
| 82 |
procedure Free is new |
| 83 |
Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id); |
| 84 |
|
| 85 |
procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id); |
| 86 |
-- This procedure outputs the task specific message for exception |
| 87 |
-- tracing purposes. |
| 88 |
|
| 89 |
procedure Task_Wrapper (Self_ID : Task_Id); |
| 90 |
pragma Convention (C, Task_Wrapper); |
| 91 |
-- This is the procedure that is called by the GNULL from the new context |
| 92 |
-- when a task is created. It waits for activation and then calls the task |
| 93 |
-- body procedure. When the task body procedure completes, it terminates |
| 94 |
-- the task. |
| 95 |
-- |
| 96 |
-- The Task_Wrapper's address will be provided to the underlying threads |
| 97 |
-- library as the task entry point. Convention C is what makes most sense |
| 98 |
-- for that purpose (Export C would make the function globally visible, |
| 99 |
-- and affect the link name on which GDB depends). This will in addition |
| 100 |
-- trigger an automatic stack alignment suitable for GCC's assumptions if |
| 101 |
-- need be. |
| 102 |
|
| 103 |
-- "Vulnerable_..." in the procedure names below means they must be called |
| 104 |
-- with abort deferred. |
| 105 |
|
| 106 |
procedure Vulnerable_Complete_Task (Self_ID : Task_Id); |
| 107 |
-- Complete the calling task. This procedure must be called with |
| 108 |
-- abort deferred. It should only be called by Complete_Task and |
| 109 |
-- Finalize_Global_Tasks (for the environment task). |
| 110 |
|
| 111 |
procedure Vulnerable_Complete_Master (Self_ID : Task_Id); |
| 112 |
-- Complete the current master of the calling task. This procedure |
| 113 |
-- must be called with abort deferred. It should only be called by |
| 114 |
-- Vulnerable_Complete_Task and Complete_Master. |
| 115 |
|
| 116 |
procedure Vulnerable_Complete_Activation (Self_ID : Task_Id); |
| 117 |
-- Signal to Self_ID's activator that Self_ID has completed activation. |
| 118 |
-- This procedure must be called with abort deferred. |
| 119 |
|
| 120 |
procedure Abort_Dependents (Self_ID : Task_Id); |
| 121 |
-- Abort all the direct dependents of Self at its current master nesting |
| 122 |
-- level, plus all of their dependents, transitively. RTS_Lock should be |
| 123 |
-- locked by the caller. |
| 124 |
|
| 125 |
procedure Vulnerable_Free_Task (T : Task_Id); |
| 126 |
-- Recover all runtime system storage associated with the task T. This |
| 127 |
-- should only be called after T has terminated and will no longer be |
| 128 |
-- referenced. |
| 129 |
-- |
| 130 |
-- For tasks created by an allocator that fails, due to an exception, it is |
| 131 |
-- called from Expunge_Unactivated_Tasks. |
| 132 |
-- |
| 133 |
-- Different code is used at master completion, in Terminate_Dependents, |
| 134 |
-- due to a need for tighter synchronization with the master. |
| 135 |
|
| 136 |
---------------------- |
| 137 |
-- Abort_Dependents -- |
| 138 |
---------------------- |
| 139 |
|
| 140 |
procedure Abort_Dependents (Self_ID : Task_Id) is |
| 141 |
C : Task_Id; |
| 142 |
P : Task_Id; |
| 143 |
|
| 144 |
-- Each task C will take care of its own dependents, so there is no |
| 145 |
-- need to worry about them here. In fact, it would be wrong to abort |
| 146 |
-- indirect dependents here, because we can't distinguish between |
| 147 |
-- duplicate master ids. For example, suppose we have three nested |
| 148 |
-- task bodies T1,T2,T3. And suppose T1 also calls P which calls Q (and |
| 149 |
-- both P and Q are task masters). Q will have the same master id as |
| 150 |
-- Master_Of_Task of T3. Previous versions of this would abort T3 when |
| 151 |
-- Q calls Complete_Master, which was completely wrong. |
| 152 |
|
| 153 |
begin |
| 154 |
C := All_Tasks_List; |
| 155 |
while C /= null loop |
| 156 |
P := C.Common.Parent; |
| 157 |
|
| 158 |
if P = Self_ID then |
| 159 |
if C.Master_Of_Task = Self_ID.Master_Within then |
| 160 |
pragma Debug |
| 161 |
(Debug.Trace (Self_ID, "Aborting", 'X', C)); |
| 162 |
Utilities.Abort_One_Task (Self_ID, C); |
| 163 |
C.Dependents_Aborted := True; |
| 164 |
end if; |
| 165 |
end if; |
| 166 |
|
| 167 |
C := C.Common.All_Tasks_Link; |
| 168 |
end loop; |
| 169 |
|
| 170 |
Self_ID.Dependents_Aborted := True; |
| 171 |
end Abort_Dependents; |
| 172 |
|
| 173 |
----------------- |
| 174 |
-- Abort_Tasks -- |
| 175 |
----------------- |
| 176 |
|
| 177 |
procedure Abort_Tasks (Tasks : Task_List) is |
| 178 |
begin |
| 179 |
Utilities.Abort_Tasks (Tasks); |
| 180 |
end Abort_Tasks; |
| 181 |
|
| 182 |
-------------------- |
| 183 |
-- Activate_Tasks -- |
| 184 |
-------------------- |
| 185 |
|
| 186 |
-- Note that locks of activator and activated task are both locked here. |
| 187 |
-- This is necessary because C.Common.State and Self.Common.Wait_Count have |
| 188 |
-- to be synchronized. This is safe from deadlock because the activator is |
| 189 |
-- always created before the activated task. That satisfies our |
| 190 |
-- in-order-of-creation ATCB locking policy. |
| 191 |
|
| 192 |
-- At one point, we may also lock the parent, if the parent is different |
| 193 |
-- from the activator. That is also consistent with the lock ordering |
| 194 |
-- policy, since the activator cannot be created before the parent. |
| 195 |
|
| 196 |
-- Since we are holding both the activator's lock, and Task_Wrapper locks |
| 197 |
-- that before it does anything more than initialize the low-level ATCB |
| 198 |
-- components, it should be safe to wait to update the counts until we see |
| 199 |
-- that the thread creation is successful. |
| 200 |
|
| 201 |
-- If the thread creation fails, we do need to close the entries of the |
| 202 |
-- task. The first phase, of dequeuing calls, only requires locking the |
| 203 |
-- acceptor's ATCB, but the waking up of the callers requires locking the |
| 204 |
-- caller's ATCB. We cannot safely do this while we are holding other |
| 205 |
-- locks. Therefore, the queue-clearing operation is done in a separate |
| 206 |
-- pass over the activation chain. |
| 207 |
|
| 208 |
procedure Activate_Tasks (Chain_Access : Activation_Chain_Access) is |
| 209 |
Self_ID : constant Task_Id := STPO.Self; |
| 210 |
P : Task_Id; |
| 211 |
C : Task_Id; |
| 212 |
Next_C, Last_C : Task_Id; |
| 213 |
Activate_Prio : System.Any_Priority; |
| 214 |
Success : Boolean; |
| 215 |
All_Elaborated : Boolean := True; |
| 216 |
|
| 217 |
begin |
| 218 |
-- If pragma Detect_Blocking is active, then we must check whether this |
| 219 |
-- potentially blocking operation is called from a protected action. |
| 220 |
|
| 221 |
if System.Tasking.Detect_Blocking |
| 222 |
and then Self_ID.Common.Protected_Action_Nesting > 0 |
| 223 |
then |
| 224 |
raise Program_Error with "potentially blocking operation"; |
| 225 |
end if; |
| 226 |
|
| 227 |
pragma Debug |
| 228 |
(Debug.Trace (Self_ID, "Activate_Tasks", 'C')); |
| 229 |
|
| 230 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 231 |
|
| 232 |
pragma Assert (Self_ID.Common.Wait_Count = 0); |
| 233 |
|
| 234 |
-- Lock RTS_Lock, to prevent activated tasks from racing ahead before |
| 235 |
-- we finish activating the chain. |
| 236 |
|
| 237 |
Lock_RTS; |
| 238 |
|
| 239 |
-- Check that all task bodies have been elaborated |
| 240 |
|
| 241 |
C := Chain_Access.T_ID; |
| 242 |
Last_C := null; |
| 243 |
while C /= null loop |
| 244 |
if C.Common.Elaborated /= null |
| 245 |
and then not C.Common.Elaborated.all |
| 246 |
then |
| 247 |
All_Elaborated := False; |
| 248 |
end if; |
| 249 |
|
| 250 |
-- Reverse the activation chain so that tasks are activated in the |
| 251 |
-- same order they're declared. |
| 252 |
|
| 253 |
Next_C := C.Common.Activation_Link; |
| 254 |
C.Common.Activation_Link := Last_C; |
| 255 |
Last_C := C; |
| 256 |
C := Next_C; |
| 257 |
end loop; |
| 258 |
|
| 259 |
Chain_Access.T_ID := Last_C; |
| 260 |
|
| 261 |
if not All_Elaborated then |
| 262 |
Unlock_RTS; |
| 263 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 264 |
raise Program_Error with "Some tasks have not been elaborated"; |
| 265 |
end if; |
| 266 |
|
| 267 |
-- Activate all the tasks in the chain. Creation of the thread of |
| 268 |
-- control was deferred until activation. So create it now. |
| 269 |
|
| 270 |
C := Chain_Access.T_ID; |
| 271 |
while C /= null loop |
| 272 |
if C.Common.State /= Terminated then |
| 273 |
pragma Assert (C.Common.State = Unactivated); |
| 274 |
|
| 275 |
P := C.Common.Parent; |
| 276 |
Write_Lock (P); |
| 277 |
Write_Lock (C); |
| 278 |
|
| 279 |
Activate_Prio := |
| 280 |
(if C.Common.Base_Priority < Get_Priority (Self_ID) |
| 281 |
then Get_Priority (Self_ID) |
| 282 |
else C.Common.Base_Priority); |
| 283 |
|
| 284 |
System.Task_Primitives.Operations.Create_Task |
| 285 |
(C, Task_Wrapper'Address, |
| 286 |
Parameters.Size_Type |
| 287 |
(C.Common.Compiler_Data.Pri_Stack_Info.Size), |
| 288 |
Activate_Prio, Success); |
| 289 |
|
| 290 |
-- There would be a race between the created task and the creator |
| 291 |
-- to do the following initialization, if we did not have a |
| 292 |
-- Lock/Unlock_RTS pair in the task wrapper to prevent it from |
| 293 |
-- racing ahead. |
| 294 |
|
| 295 |
if Success then |
| 296 |
C.Common.State := Activating; |
| 297 |
C.Awake_Count := 1; |
| 298 |
C.Alive_Count := 1; |
| 299 |
P.Awake_Count := P.Awake_Count + 1; |
| 300 |
P.Alive_Count := P.Alive_Count + 1; |
| 301 |
|
| 302 |
if P.Common.State = Master_Completion_Sleep and then |
| 303 |
C.Master_Of_Task = P.Master_Within |
| 304 |
then |
| 305 |
pragma Assert (Self_ID /= P); |
| 306 |
P.Common.Wait_Count := P.Common.Wait_Count + 1; |
| 307 |
end if; |
| 308 |
|
| 309 |
for J in System.Tasking.Debug.Known_Tasks'Range loop |
| 310 |
if System.Tasking.Debug.Known_Tasks (J) = null then |
| 311 |
System.Tasking.Debug.Known_Tasks (J) := C; |
| 312 |
C.Known_Tasks_Index := J; |
| 313 |
exit; |
| 314 |
end if; |
| 315 |
end loop; |
| 316 |
|
| 317 |
if Global_Task_Debug_Event_Set then |
| 318 |
Debug.Signal_Debug_Event |
| 319 |
(Debug.Debug_Event_Activating, C); |
| 320 |
end if; |
| 321 |
|
| 322 |
C.Common.State := Runnable; |
| 323 |
|
| 324 |
Unlock (C); |
| 325 |
Unlock (P); |
| 326 |
|
| 327 |
else |
| 328 |
-- No need to set Awake_Count, State, etc. here since the loop |
| 329 |
-- below will do that for any Unactivated tasks. |
| 330 |
|
| 331 |
Unlock (C); |
| 332 |
Unlock (P); |
| 333 |
Self_ID.Common.Activation_Failed := True; |
| 334 |
end if; |
| 335 |
end if; |
| 336 |
|
| 337 |
C := C.Common.Activation_Link; |
| 338 |
end loop; |
| 339 |
|
| 340 |
Unlock_RTS; |
| 341 |
|
| 342 |
-- Close the entries of any tasks that failed thread creation, and count |
| 343 |
-- those that have not finished activation. |
| 344 |
|
| 345 |
Write_Lock (Self_ID); |
| 346 |
Self_ID.Common.State := Activator_Sleep; |
| 347 |
|
| 348 |
C := Chain_Access.T_ID; |
| 349 |
while C /= null loop |
| 350 |
Write_Lock (C); |
| 351 |
|
| 352 |
if C.Common.State = Unactivated then |
| 353 |
C.Common.Activator := null; |
| 354 |
C.Common.State := Terminated; |
| 355 |
C.Callable := False; |
| 356 |
Utilities.Cancel_Queued_Entry_Calls (C); |
| 357 |
|
| 358 |
elsif C.Common.Activator /= null then |
| 359 |
Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1; |
| 360 |
end if; |
| 361 |
|
| 362 |
Unlock (C); |
| 363 |
P := C.Common.Activation_Link; |
| 364 |
C.Common.Activation_Link := null; |
| 365 |
C := P; |
| 366 |
end loop; |
| 367 |
|
| 368 |
-- Wait for the activated tasks to complete activation. It is |
| 369 |
-- unsafe to abort any of these tasks until the count goes to zero. |
| 370 |
|
| 371 |
loop |
| 372 |
exit when Self_ID.Common.Wait_Count = 0; |
| 373 |
Sleep (Self_ID, Activator_Sleep); |
| 374 |
end loop; |
| 375 |
|
| 376 |
Self_ID.Common.State := Runnable; |
| 377 |
Unlock (Self_ID); |
| 378 |
|
| 379 |
-- Remove the tasks from the chain |
| 380 |
|
| 381 |
Chain_Access.T_ID := null; |
| 382 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 383 |
|
| 384 |
if Self_ID.Common.Activation_Failed then |
| 385 |
Self_ID.Common.Activation_Failed := False; |
| 386 |
raise Tasking_Error with "Failure during activation"; |
| 387 |
end if; |
| 388 |
end Activate_Tasks; |
| 389 |
|
| 390 |
------------------------- |
| 391 |
-- Complete_Activation -- |
| 392 |
------------------------- |
| 393 |
|
| 394 |
procedure Complete_Activation is |
| 395 |
Self_ID : constant Task_Id := STPO.Self; |
| 396 |
|
| 397 |
begin |
| 398 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 399 |
Vulnerable_Complete_Activation (Self_ID); |
| 400 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 401 |
|
| 402 |
-- ??? Why do we need to allow for nested deferral here? |
| 403 |
|
| 404 |
end Complete_Activation; |
| 405 |
|
| 406 |
--------------------- |
| 407 |
-- Complete_Master -- |
| 408 |
--------------------- |
| 409 |
|
| 410 |
procedure Complete_Master is |
| 411 |
Self_ID : constant Task_Id := STPO.Self; |
| 412 |
begin |
| 413 |
pragma Assert |
| 414 |
(Self_ID.Deferral_Level > 0 |
| 415 |
or else not System.Restrictions.Abort_Allowed); |
| 416 |
Vulnerable_Complete_Master (Self_ID); |
| 417 |
end Complete_Master; |
| 418 |
|
| 419 |
------------------- |
| 420 |
-- Complete_Task -- |
| 421 |
------------------- |
| 422 |
|
| 423 |
-- See comments on Vulnerable_Complete_Task for details |
| 424 |
|
| 425 |
procedure Complete_Task is |
| 426 |
Self_ID : constant Task_Id := STPO.Self; |
| 427 |
|
| 428 |
begin |
| 429 |
pragma Assert |
| 430 |
(Self_ID.Deferral_Level > 0 |
| 431 |
or else not System.Restrictions.Abort_Allowed); |
| 432 |
|
| 433 |
Vulnerable_Complete_Task (Self_ID); |
| 434 |
|
| 435 |
-- All of our dependents have terminated, never undefer abort again |
| 436 |
|
| 437 |
end Complete_Task; |
| 438 |
|
| 439 |
----------------- |
| 440 |
-- Create_Task -- |
| 441 |
----------------- |
| 442 |
|
| 443 |
-- Compiler interface only. Do not call from within the RTS. This must be |
| 444 |
-- called to create a new task. |
| 445 |
|
| 446 |
procedure Create_Task |
| 447 |
(Priority : Integer; |
| 448 |
Stack_Size : System.Parameters.Size_Type; |
| 449 |
Secondary_Stack_Size : System.Parameters.Size_Type; |
| 450 |
Task_Info : System.Task_Info.Task_Info_Type; |
| 451 |
CPU : Integer; |
| 452 |
Relative_Deadline : Ada.Real_Time.Time_Span; |
| 453 |
Domain : Dispatching_Domain_Access; |
| 454 |
Num_Entries : Task_Entry_Index; |
| 455 |
Master : Master_Level; |
| 456 |
State : Task_Procedure_Access; |
| 457 |
Discriminants : System.Address; |
| 458 |
Elaborated : Access_Boolean; |
| 459 |
Chain : in out Activation_Chain; |
| 460 |
Task_Image : String; |
| 461 |
Created_Task : out Task_Id) |
| 462 |
is |
| 463 |
T, P : Task_Id; |
| 464 |
Self_ID : constant Task_Id := STPO.Self; |
| 465 |
Success : Boolean; |
| 466 |
Base_Priority : System.Any_Priority; |
| 467 |
Len : Natural; |
| 468 |
Base_CPU : System.Multiprocessors.CPU_Range; |
| 469 |
|
| 470 |
use type System.Multiprocessors.CPU_Range; |
| 471 |
|
| 472 |
pragma Unreferenced (Relative_Deadline); |
| 473 |
-- EDF scheduling is not supported by any of the target platforms so |
| 474 |
-- this parameter is not passed any further. |
| 475 |
|
| 476 |
begin |
| 477 |
-- If Master is greater than the current master, it means that Master |
| 478 |
-- has already awaited its dependent tasks. This raises Program_Error, |
| 479 |
-- by 4.8(10.3/2). See AI-280. Ignore this check for foreign threads. |
| 480 |
|
| 481 |
if Self_ID.Master_Of_Task /= Foreign_Task_Level |
| 482 |
and then Master > Self_ID.Master_Within |
| 483 |
then |
| 484 |
raise Program_Error with |
| 485 |
"create task after awaiting termination"; |
| 486 |
end if; |
| 487 |
|
| 488 |
-- If pragma Detect_Blocking is active must be checked whether this |
| 489 |
-- potentially blocking operation is called from a protected action. |
| 490 |
|
| 491 |
if System.Tasking.Detect_Blocking |
| 492 |
and then Self_ID.Common.Protected_Action_Nesting > 0 |
| 493 |
then |
| 494 |
raise Program_Error with "potentially blocking operation"; |
| 495 |
end if; |
| 496 |
|
| 497 |
pragma Debug (Debug.Trace (Self_ID, "Create_Task", 'C')); |
| 498 |
|
| 499 |
Base_Priority := |
| 500 |
(if Priority = Unspecified_Priority |
| 501 |
then Self_ID.Common.Base_Priority |
| 502 |
else System.Any_Priority (Priority)); |
| 503 |
|
| 504 |
-- Legal values of CPU are the special Unspecified_CPU value which is |
| 505 |
-- inserted by the compiler for tasks without CPU aspect, and those in |
| 506 |
-- the range of CPU_Range but no greater than Number_Of_CPUs. Otherwise |
| 507 |
-- the task is defined to have failed, and it becomes a completed task |
| 508 |
-- (RM D.16(14/3)). |
| 509 |
|
| 510 |
if CPU /= Unspecified_CPU |
| 511 |
and then (CPU < Integer (System.Multiprocessors.CPU_Range'First) |
| 512 |
or else |
| 513 |
CPU > Integer (System.Multiprocessors.Number_Of_CPUs)) |
| 514 |
then |
| 515 |
raise Tasking_Error with "CPU not in range"; |
| 516 |
|
| 517 |
-- Normal CPU affinity |
| 518 |
|
| 519 |
else |
| 520 |
-- When the application code says nothing about the task affinity |
| 521 |
-- (task without CPU aspect) then the compiler inserts the value |
| 522 |
-- Unspecified_CPU which indicates to the run-time library that |
| 523 |
-- the task will activate and execute on the same processor as its |
| 524 |
-- activating task if the activating task is assigned a processor |
| 525 |
-- (RM D.16(14/3)). |
| 526 |
|
| 527 |
Base_CPU := |
| 528 |
(if CPU = Unspecified_CPU |
| 529 |
then Self_ID.Common.Base_CPU |
| 530 |
else System.Multiprocessors.CPU_Range (CPU)); |
| 531 |
end if; |
| 532 |
|
| 533 |
-- Find parent P of new Task, via master level number. Independent |
| 534 |
-- tasks should have Parent = Environment_Task, and all tasks created |
| 535 |
-- by independent tasks are also independent. See, for example, |
| 536 |
-- s-interr.adb, where Interrupt_Manager does "new Server_Task". The |
| 537 |
-- access type is at library level, so the parent of the Server_Task |
| 538 |
-- is Environment_Task. |
| 539 |
|
| 540 |
P := Self_ID; |
| 541 |
|
| 542 |
if P.Master_Of_Task <= Independent_Task_Level then |
| 543 |
P := Environment_Task; |
| 544 |
else |
| 545 |
while P /= null and then P.Master_Of_Task >= Master loop |
| 546 |
P := P.Common.Parent; |
| 547 |
end loop; |
| 548 |
end if; |
| 549 |
|
| 550 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 551 |
|
| 552 |
begin |
| 553 |
T := New_ATCB (Num_Entries); |
| 554 |
exception |
| 555 |
when others => |
| 556 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 557 |
raise Storage_Error with "Cannot allocate task"; |
| 558 |
end; |
| 559 |
|
| 560 |
-- RTS_Lock is used by Abort_Dependents and Abort_Tasks. Up to this |
| 561 |
-- point, it is possible that we may be part of a family of tasks that |
| 562 |
-- is being aborted. |
| 563 |
|
| 564 |
Lock_RTS; |
| 565 |
Write_Lock (Self_ID); |
| 566 |
|
| 567 |
-- Now, we must check that we have not been aborted. If so, we should |
| 568 |
-- give up on creating this task, and simply return. |
| 569 |
|
| 570 |
if not Self_ID.Callable then |
| 571 |
pragma Assert (Self_ID.Pending_ATC_Level = Level_Completed_Task); |
| 572 |
pragma Assert (Self_ID.Pending_Action); |
| 573 |
pragma Assert |
| 574 |
(Chain.T_ID = null or else Chain.T_ID.Common.State = Unactivated); |
| 575 |
|
| 576 |
Unlock (Self_ID); |
| 577 |
Unlock_RTS; |
| 578 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 579 |
|
| 580 |
-- ??? Should never get here |
| 581 |
|
| 582 |
pragma Assert (Standard.False); |
| 583 |
raise Standard'Abort_Signal; |
| 584 |
end if; |
| 585 |
|
| 586 |
Initialize_ATCB (Self_ID, State, Discriminants, P, Elaborated, |
| 587 |
Base_Priority, Base_CPU, CPU /= Unspecified_CPU, Domain, Task_Info, |
| 588 |
Stack_Size, T, Success); |
| 589 |
|
| 590 |
if not Success then |
| 591 |
Free (T); |
| 592 |
Unlock (Self_ID); |
| 593 |
Unlock_RTS; |
| 594 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 595 |
raise Storage_Error with "Failed to initialize task"; |
| 596 |
end if; |
| 597 |
|
| 598 |
if Master = Foreign_Task_Level + 2 then |
| 599 |
|
| 600 |
-- This should not happen, except when a foreign task creates non |
| 601 |
-- library-level Ada tasks. In this case, we pretend the master is |
| 602 |
-- a regular library level task, otherwise the run-time will get |
| 603 |
-- confused when waiting for these tasks to terminate. |
| 604 |
|
| 605 |
T.Master_Of_Task := Library_Task_Level; |
| 606 |
|
| 607 |
else |
| 608 |
T.Master_Of_Task := Master; |
| 609 |
end if; |
| 610 |
|
| 611 |
T.Master_Within := T.Master_Of_Task + 1; |
| 612 |
|
| 613 |
for L in T.Entry_Calls'Range loop |
| 614 |
T.Entry_Calls (L).Self := T; |
| 615 |
T.Entry_Calls (L).Level := L; |
| 616 |
end loop; |
| 617 |
|
| 618 |
if Task_Image'Length = 0 then |
| 619 |
T.Common.Task_Image_Len := 0; |
| 620 |
else |
| 621 |
Len := 1; |
| 622 |
T.Common.Task_Image (1) := Task_Image (Task_Image'First); |
| 623 |
|
| 624 |
-- Remove unwanted blank space generated by 'Image |
| 625 |
|
| 626 |
for J in Task_Image'First + 1 .. Task_Image'Last loop |
| 627 |
if Task_Image (J) /= ' ' |
| 628 |
or else Task_Image (J - 1) /= '(' |
| 629 |
then |
| 630 |
Len := Len + 1; |
| 631 |
T.Common.Task_Image (Len) := Task_Image (J); |
| 632 |
exit when Len = T.Common.Task_Image'Last; |
| 633 |
end if; |
| 634 |
end loop; |
| 635 |
|
| 636 |
T.Common.Task_Image_Len := Len; |
| 637 |
end if; |
| 638 |
|
| 639 |
-- Note: we used to have code here to initialize T.Common.Domain, but |
| 640 |
-- that is not needed, since this is initialized in System.Tasking. |
| 641 |
|
| 642 |
Unlock (Self_ID); |
| 643 |
Unlock_RTS; |
| 644 |
|
| 645 |
-- The CPU associated to the task (if any) must belong to the |
| 646 |
-- dispatching domain. |
| 647 |
|
| 648 |
if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU |
| 649 |
and then |
| 650 |
(Base_CPU not in T.Common.Domain'Range |
| 651 |
or else not T.Common.Domain (Base_CPU)) |
| 652 |
then |
| 653 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 654 |
raise Tasking_Error with "CPU not in dispatching domain"; |
| 655 |
end if; |
| 656 |
|
| 657 |
-- To handle the interaction between pragma CPU and dispatching domains |
| 658 |
-- we need to signal that this task is being allocated to a processor. |
| 659 |
-- This is needed only for tasks belonging to the system domain (the |
| 660 |
-- creation of new dispatching domains can only take processors from the |
| 661 |
-- system domain) and only before the environment task calls the main |
| 662 |
-- procedure (dispatching domains cannot be created after this). |
| 663 |
|
| 664 |
if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU |
| 665 |
and then T.Common.Domain = System.Tasking.System_Domain |
| 666 |
and then not System.Tasking.Dispatching_Domains_Frozen |
| 667 |
then |
| 668 |
-- Increase the number of tasks attached to the CPU to which this |
| 669 |
-- task is being moved. |
| 670 |
|
| 671 |
Dispatching_Domain_Tasks (Base_CPU) := |
| 672 |
Dispatching_Domain_Tasks (Base_CPU) + 1; |
| 673 |
end if; |
| 674 |
|
| 675 |
-- Create the secondary stack for the task as early as possible during |
| 676 |
-- in the creation of a task, since it may be used by the operation of |
| 677 |
-- Ada code within the task. |
| 678 |
|
| 679 |
begin |
| 680 |
SSL.Create_TSD (T.Common.Compiler_Data, null, Secondary_Stack_Size); |
| 681 |
exception |
| 682 |
when others => |
| 683 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 684 |
raise Storage_Error with "Secondary stack could not be allocated"; |
| 685 |
end; |
| 686 |
|
| 687 |
T.Common.Activation_Link := Chain.T_ID; |
| 688 |
Chain.T_ID := T; |
| 689 |
Created_Task := T; |
| 690 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 691 |
|
| 692 |
pragma Debug |
| 693 |
(Debug.Trace |
| 694 |
(Self_ID, "Created task in " & T.Master_Of_Task'Img, 'C', T)); |
| 695 |
end Create_Task; |
| 696 |
|
| 697 |
-------------------- |
| 698 |
-- Current_Master -- |
| 699 |
-------------------- |
| 700 |
|
| 701 |
function Current_Master return Master_Level is |
| 702 |
begin |
| 703 |
return STPO.Self.Master_Within; |
| 704 |
end Current_Master; |
| 705 |
|
| 706 |
------------------ |
| 707 |
-- Enter_Master -- |
| 708 |
------------------ |
| 709 |
|
| 710 |
procedure Enter_Master is |
| 711 |
Self_ID : constant Task_Id := STPO.Self; |
| 712 |
begin |
| 713 |
Self_ID.Master_Within := Self_ID.Master_Within + 1; |
| 714 |
pragma Debug |
| 715 |
(Debug.Trace |
| 716 |
(Self_ID, "Enter_Master ->" & Self_ID.Master_Within'Img, 'M')); |
| 717 |
end Enter_Master; |
| 718 |
|
| 719 |
------------------------------- |
| 720 |
-- Expunge_Unactivated_Tasks -- |
| 721 |
------------------------------- |
| 722 |
|
| 723 |
-- See procedure Close_Entries for the general case |
| 724 |
|
| 725 |
procedure Expunge_Unactivated_Tasks (Chain : in out Activation_Chain) is |
| 726 |
Self_ID : constant Task_Id := STPO.Self; |
| 727 |
C : Task_Id; |
| 728 |
Call : Entry_Call_Link; |
| 729 |
Temp : Task_Id; |
| 730 |
|
| 731 |
begin |
| 732 |
pragma Debug |
| 733 |
(Debug.Trace (Self_ID, "Expunge_Unactivated_Tasks", 'C')); |
| 734 |
|
| 735 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 736 |
|
| 737 |
-- ??? |
| 738 |
-- Experimentation has shown that abort is sometimes (but not always) |
| 739 |
-- already deferred when this is called. |
| 740 |
|
| 741 |
-- That may indicate an error. Find out what is going on |
| 742 |
|
| 743 |
C := Chain.T_ID; |
| 744 |
while C /= null loop |
| 745 |
pragma Assert (C.Common.State = Unactivated); |
| 746 |
|
| 747 |
Temp := C.Common.Activation_Link; |
| 748 |
|
| 749 |
if C.Common.State = Unactivated then |
| 750 |
Lock_RTS; |
| 751 |
Write_Lock (C); |
| 752 |
|
| 753 |
for J in 1 .. C.Entry_Num loop |
| 754 |
Queuing.Dequeue_Head (C.Entry_Queues (J), Call); |
| 755 |
pragma Assert (Call = null); |
| 756 |
end loop; |
| 757 |
|
| 758 |
Unlock (C); |
| 759 |
|
| 760 |
Initialization.Remove_From_All_Tasks_List (C); |
| 761 |
Unlock_RTS; |
| 762 |
|
| 763 |
Vulnerable_Free_Task (C); |
| 764 |
C := Temp; |
| 765 |
end if; |
| 766 |
end loop; |
| 767 |
|
| 768 |
Chain.T_ID := null; |
| 769 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 770 |
end Expunge_Unactivated_Tasks; |
| 771 |
|
| 772 |
--------------------------- |
| 773 |
-- Finalize_Global_Tasks -- |
| 774 |
--------------------------- |
| 775 |
|
| 776 |
-- ??? |
| 777 |
-- We have a potential problem here if finalization of global objects does |
| 778 |
-- anything with signals or the timer server, since by that time those |
| 779 |
-- servers have terminated. |
| 780 |
|
| 781 |
-- It is hard to see how that would occur |
| 782 |
|
| 783 |
-- However, a better solution might be to do all this finalization |
| 784 |
-- using the global finalization chain. |
| 785 |
|
| 786 |
procedure Finalize_Global_Tasks is |
| 787 |
Self_ID : constant Task_Id := STPO.Self; |
| 788 |
|
| 789 |
Ignore_1 : Boolean; |
| 790 |
Ignore_2 : Boolean; |
| 791 |
|
| 792 |
function State |
| 793 |
(Int : System.Interrupt_Management.Interrupt_ID) return Character; |
| 794 |
pragma Import (C, State, "__gnat_get_interrupt_state"); |
| 795 |
-- Get interrupt state for interrupt number Int. Defined in init.c |
| 796 |
|
| 797 |
Default : constant Character := 's'; |
| 798 |
-- 's' Interrupt_State pragma set state to System (use "default" |
| 799 |
-- system handler) |
| 800 |
|
| 801 |
begin |
| 802 |
if Self_ID.Deferral_Level = 0 then |
| 803 |
-- ??? |
| 804 |
-- In principle, we should be able to predict whether abort is |
| 805 |
-- already deferred here (and it should not be deferred yet but in |
| 806 |
-- practice it seems Finalize_Global_Tasks is being called sometimes, |
| 807 |
-- from RTS code for exceptions, with abort already deferred. |
| 808 |
|
| 809 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 810 |
|
| 811 |
-- Never undefer again |
| 812 |
end if; |
| 813 |
|
| 814 |
-- This code is only executed by the environment task |
| 815 |
|
| 816 |
pragma Assert (Self_ID = Environment_Task); |
| 817 |
|
| 818 |
-- Set Environment_Task'Callable to false to notify library-level tasks |
| 819 |
-- that it is waiting for them. |
| 820 |
|
| 821 |
Self_ID.Callable := False; |
| 822 |
|
| 823 |
-- Exit level 2 master, for normal tasks in library-level packages |
| 824 |
|
| 825 |
Complete_Master; |
| 826 |
|
| 827 |
-- Force termination of "independent" library-level server tasks |
| 828 |
|
| 829 |
Lock_RTS; |
| 830 |
Abort_Dependents (Self_ID); |
| 831 |
Unlock_RTS; |
| 832 |
|
| 833 |
-- We need to explicitly wait for the task to be terminated here |
| 834 |
-- because on true concurrent system, we may end this procedure before |
| 835 |
-- the tasks are really terminated. |
| 836 |
|
| 837 |
Write_Lock (Self_ID); |
| 838 |
|
| 839 |
-- If the Abort_Task signal is set to system, it means that we may |
| 840 |
-- not have been able to abort all independent tasks (in particular, |
| 841 |
-- Server_Task may be blocked, waiting for a signal), in which case, do |
| 842 |
-- not wait for Independent_Task_Count to go down to 0. We arbitrarily |
| 843 |
-- limit the number of loop iterations; if an independent task does not |
| 844 |
-- terminate, we do not want to hang here. In that case, the thread will |
| 845 |
-- be terminated when the process exits. |
| 846 |
|
| 847 |
if State (System.Interrupt_Management.Abort_Task_Interrupt) /= Default |
| 848 |
then |
| 849 |
for J in 1 .. 10 loop |
| 850 |
exit when Utilities.Independent_Task_Count = 0; |
| 851 |
|
| 852 |
-- We used to yield here, but this did not take into account low |
| 853 |
-- priority tasks that would cause dead lock in some cases (true |
| 854 |
-- FIFO scheduling). |
| 855 |
|
| 856 |
Timed_Sleep |
| 857 |
(Self_ID, 0.01, System.OS_Primitives.Relative, |
| 858 |
Self_ID.Common.State, Ignore_1, Ignore_2); |
| 859 |
end loop; |
| 860 |
end if; |
| 861 |
|
| 862 |
-- ??? On multi-processor environments, it seems that the above loop |
| 863 |
-- isn't sufficient, so we need to add an additional delay. |
| 864 |
|
| 865 |
Timed_Sleep |
| 866 |
(Self_ID, 0.01, System.OS_Primitives.Relative, |
| 867 |
Self_ID.Common.State, Ignore_1, Ignore_2); |
| 868 |
|
| 869 |
Unlock (Self_ID); |
| 870 |
|
| 871 |
-- Complete the environment task |
| 872 |
|
| 873 |
Vulnerable_Complete_Task (Self_ID); |
| 874 |
|
| 875 |
-- Handle normal task termination by the environment task, but only |
| 876 |
-- for the normal task termination. In the case of Abnormal and |
| 877 |
-- Unhandled_Exception they must have been handled before, and the |
| 878 |
-- task termination soft link must have been changed so the task |
| 879 |
-- termination routine is not executed twice. |
| 880 |
|
| 881 |
SSL.Task_Termination_Handler.all (Ada.Exceptions.Null_Occurrence); |
| 882 |
|
| 883 |
-- Finalize all library-level controlled objects |
| 884 |
|
| 885 |
if not SSL."=" (SSL.Finalize_Library_Objects, null) then |
| 886 |
SSL.Finalize_Library_Objects.all; |
| 887 |
end if; |
| 888 |
|
| 889 |
-- Reset the soft links to non-tasking |
| 890 |
|
| 891 |
SSL.Abort_Defer := SSL.Abort_Defer_NT'Access; |
| 892 |
SSL.Abort_Undefer := SSL.Abort_Undefer_NT'Access; |
| 893 |
SSL.Lock_Task := SSL.Task_Lock_NT'Access; |
| 894 |
SSL.Unlock_Task := SSL.Task_Unlock_NT'Access; |
| 895 |
SSL.Get_Jmpbuf_Address := SSL.Get_Jmpbuf_Address_NT'Access; |
| 896 |
SSL.Set_Jmpbuf_Address := SSL.Set_Jmpbuf_Address_NT'Access; |
| 897 |
SSL.Get_Sec_Stack := SSL.Get_Sec_Stack_NT'Access; |
| 898 |
SSL.Set_Sec_Stack := SSL.Set_Sec_Stack_NT'Access; |
| 899 |
SSL.Check_Abort_Status := SSL.Check_Abort_Status_NT'Access; |
| 900 |
SSL.Get_Stack_Info := SSL.Get_Stack_Info_NT'Access; |
| 901 |
|
| 902 |
-- Don't bother trying to finalize Initialization.Global_Task_Lock |
| 903 |
-- and System.Task_Primitives.RTS_Lock. |
| 904 |
|
| 905 |
end Finalize_Global_Tasks; |
| 906 |
|
| 907 |
--------------- |
| 908 |
-- Free_Task -- |
| 909 |
--------------- |
| 910 |
|
| 911 |
procedure Free_Task (T : Task_Id) is |
| 912 |
Self_Id : constant Task_Id := Self; |
| 913 |
|
| 914 |
begin |
| 915 |
Initialization.Task_Lock (Self_Id); |
| 916 |
|
| 917 |
if T.Common.State = Terminated then |
| 918 |
|
| 919 |
-- It is not safe to call Abort_Defer or Write_Lock at this stage |
| 920 |
|
| 921 |
Lock_RTS; |
| 922 |
Initialization.Finalize_Attributes (T); |
| 923 |
Initialization.Remove_From_All_Tasks_List (T); |
| 924 |
Unlock_RTS; |
| 925 |
|
| 926 |
Initialization.Task_Unlock (Self_Id); |
| 927 |
|
| 928 |
System.Task_Primitives.Operations.Finalize_TCB (T); |
| 929 |
|
| 930 |
else |
| 931 |
-- If the task is not terminated, then mark the task as to be freed |
| 932 |
-- upon termination. |
| 933 |
|
| 934 |
T.Free_On_Termination := True; |
| 935 |
Initialization.Task_Unlock (Self_Id); |
| 936 |
end if; |
| 937 |
end Free_Task; |
| 938 |
|
| 939 |
--------------------------- |
| 940 |
-- Move_Activation_Chain -- |
| 941 |
--------------------------- |
| 942 |
|
| 943 |
procedure Move_Activation_Chain |
| 944 |
(From, To : Activation_Chain_Access; |
| 945 |
New_Master : Master_ID) |
| 946 |
is |
| 947 |
Self_ID : constant Task_Id := STPO.Self; |
| 948 |
C : Task_Id; |
| 949 |
|
| 950 |
begin |
| 951 |
pragma Debug |
| 952 |
(Debug.Trace (Self_ID, "Move_Activation_Chain", 'C')); |
| 953 |
|
| 954 |
-- Nothing to do if From is empty, and we can check that without |
| 955 |
-- deferring aborts. |
| 956 |
|
| 957 |
C := From.all.T_ID; |
| 958 |
|
| 959 |
if C = null then |
| 960 |
return; |
| 961 |
end if; |
| 962 |
|
| 963 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 964 |
|
| 965 |
-- Loop through the From chain, changing their Master_Of_Task fields, |
| 966 |
-- and to find the end of the chain. |
| 967 |
|
| 968 |
loop |
| 969 |
C.Master_Of_Task := New_Master; |
| 970 |
exit when C.Common.Activation_Link = null; |
| 971 |
C := C.Common.Activation_Link; |
| 972 |
end loop; |
| 973 |
|
| 974 |
-- Hook From in at the start of To |
| 975 |
|
| 976 |
C.Common.Activation_Link := To.all.T_ID; |
| 977 |
To.all.T_ID := From.all.T_ID; |
| 978 |
|
| 979 |
-- Set From to empty |
| 980 |
|
| 981 |
From.all.T_ID := null; |
| 982 |
|
| 983 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 984 |
end Move_Activation_Chain; |
| 985 |
|
| 986 |
------------------ |
| 987 |
-- Task_Wrapper -- |
| 988 |
------------------ |
| 989 |
|
| 990 |
-- The task wrapper is a procedure that is called first for each task body |
| 991 |
-- and which in turn calls the compiler-generated task body procedure. |
| 992 |
-- The wrapper's main job is to do initialization for the task. It also |
| 993 |
-- has some locally declared objects that serve as per-task local data. |
| 994 |
-- Task finalization is done by Complete_Task, which is called from an |
| 995 |
-- at-end handler that the compiler generates. |
| 996 |
|
| 997 |
procedure Task_Wrapper (Self_ID : Task_Id) is |
| 998 |
use System.Standard_Library; |
| 999 |
use System.Stack_Usage; |
| 1000 |
|
| 1001 |
Bottom_Of_Stack : aliased Integer; |
| 1002 |
|
| 1003 |
Task_Alternate_Stack : |
| 1004 |
aliased SSE.Storage_Array (1 .. Alternate_Stack_Size); |
| 1005 |
-- The alternate signal stack for this task, if any |
| 1006 |
|
| 1007 |
Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0; |
| 1008 |
-- Whether to use above alternate signal stack for stack overflows |
| 1009 |
|
| 1010 |
SEH_Table : aliased SSE.Storage_Array (1 .. 8); |
| 1011 |
-- Structured Exception Registration table (2 words) |
| 1012 |
|
| 1013 |
procedure Install_SEH_Handler (Addr : System.Address); |
| 1014 |
pragma Import (C, Install_SEH_Handler, "__gnat_install_SEH_handler"); |
| 1015 |
-- Install the SEH (Structured Exception Handling) handler |
| 1016 |
|
| 1017 |
Cause : Cause_Of_Termination := Normal; |
| 1018 |
-- Indicates the reason why this task terminates. Normal corresponds to |
| 1019 |
-- a task terminating due to completing the last statement of its body, |
| 1020 |
-- or as a result of waiting on a terminate alternative. If the task |
| 1021 |
-- terminates because it is being aborted then Cause will be set |
| 1022 |
-- to Abnormal. If the task terminates because of an exception |
| 1023 |
-- raised by the execution of its task body, then Cause is set |
| 1024 |
-- to Unhandled_Exception. |
| 1025 |
|
| 1026 |
EO : Exception_Occurrence; |
| 1027 |
-- If the task terminates because of an exception raised by the |
| 1028 |
-- execution of its task body, then EO will contain the associated |
| 1029 |
-- exception occurrence. Otherwise, it will contain Null_Occurrence. |
| 1030 |
|
| 1031 |
TH : Termination_Handler := null; |
| 1032 |
-- Pointer to the protected procedure to be executed upon task |
| 1033 |
-- termination. |
| 1034 |
|
| 1035 |
procedure Search_Fall_Back_Handler (ID : Task_Id); |
| 1036 |
-- Procedure that searches recursively a fall-back handler through the |
| 1037 |
-- master relationship. If the handler is found, its pointer is stored |
| 1038 |
-- in TH. It stops when the handler is found or when the ID is null. |
| 1039 |
|
| 1040 |
------------------------------ |
| 1041 |
-- Search_Fall_Back_Handler -- |
| 1042 |
------------------------------ |
| 1043 |
|
| 1044 |
procedure Search_Fall_Back_Handler (ID : Task_Id) is |
| 1045 |
begin |
| 1046 |
-- A null Task_Id indicates that we have reached the root of the |
| 1047 |
-- task hierarchy and no handler has been found. |
| 1048 |
|
| 1049 |
if ID = null then |
| 1050 |
return; |
| 1051 |
|
| 1052 |
-- If there is a fall back handler, store its pointer for later |
| 1053 |
-- execution. |
| 1054 |
|
| 1055 |
elsif ID.Common.Fall_Back_Handler /= null then |
| 1056 |
TH := ID.Common.Fall_Back_Handler; |
| 1057 |
|
| 1058 |
-- Otherwise look for a fall back handler in the parent |
| 1059 |
|
| 1060 |
else |
| 1061 |
Search_Fall_Back_Handler (ID.Common.Parent); |
| 1062 |
end if; |
| 1063 |
end Search_Fall_Back_Handler; |
| 1064 |
|
| 1065 |
-- Start of processing for Task_Wrapper |
| 1066 |
|
| 1067 |
begin |
| 1068 |
pragma Assert (Self_ID.Deferral_Level = 1); |
| 1069 |
|
| 1070 |
Debug.Master_Hook |
| 1071 |
(Self_ID, Self_ID.Common.Parent, Self_ID.Master_Of_Task); |
| 1072 |
|
| 1073 |
if Use_Alternate_Stack then |
| 1074 |
Self_ID.Common.Task_Alternate_Stack := Task_Alternate_Stack'Address; |
| 1075 |
end if; |
| 1076 |
|
| 1077 |
-- Set the guard page at the bottom of the stack. The call to unprotect |
| 1078 |
-- the page is done in Terminate_Task |
| 1079 |
|
| 1080 |
Stack_Guard (Self_ID, True); |
| 1081 |
|
| 1082 |
-- Initialize low-level TCB components, that cannot be initialized by |
| 1083 |
-- the creator. |
| 1084 |
|
| 1085 |
Enter_Task (Self_ID); |
| 1086 |
|
| 1087 |
-- Initialize dynamic stack usage |
| 1088 |
|
| 1089 |
if System.Stack_Usage.Is_Enabled then |
| 1090 |
declare |
| 1091 |
Guard_Page_Size : constant := 16 * 1024; |
| 1092 |
-- Part of the stack used as a guard page. This is an OS dependent |
| 1093 |
-- value, so we need to use the maximum. This value is only used |
| 1094 |
-- when the stack address is known, that is currently Windows. |
| 1095 |
|
| 1096 |
Small_Overflow_Guard : constant := 12 * 1024; |
| 1097 |
-- Note: this used to be 4K, but was changed to 12K, since |
| 1098 |
-- smaller values resulted in segmentation faults from dynamic |
| 1099 |
-- stack analysis. |
| 1100 |
|
| 1101 |
Big_Overflow_Guard : constant := 64 * 1024 + 8 * 1024; |
| 1102 |
-- These two values are experimental, and seem to work on most |
| 1103 |
-- platforms. They still need to be analyzed further. They also |
| 1104 |
-- need documentation, what are they and why does the logic differ |
| 1105 |
-- depending on whether the stack is large or small??? |
| 1106 |
|
| 1107 |
Pattern_Size : Natural := |
| 1108 |
Natural (Self_ID.Common. |
| 1109 |
Compiler_Data.Pri_Stack_Info.Size); |
| 1110 |
-- Size of the pattern |
| 1111 |
|
| 1112 |
Stack_Base : Address; |
| 1113 |
-- Address of the base of the stack |
| 1114 |
|
| 1115 |
begin |
| 1116 |
Stack_Base := Self_ID.Common.Compiler_Data.Pri_Stack_Info.Base; |
| 1117 |
|
| 1118 |
if Stack_Base = Null_Address then |
| 1119 |
|
| 1120 |
-- On many platforms, we don't know the real stack base |
| 1121 |
-- address. Estimate it using an address in the frame. |
| 1122 |
|
| 1123 |
Stack_Base := Bottom_Of_Stack'Address; |
| 1124 |
|
| 1125 |
-- Adjustments for inner frames |
| 1126 |
|
| 1127 |
Pattern_Size := Pattern_Size - |
| 1128 |
(if Pattern_Size < Big_Overflow_Guard |
| 1129 |
then Small_Overflow_Guard |
| 1130 |
else Big_Overflow_Guard); |
| 1131 |
else |
| 1132 |
-- Reduce by the size of the final guard page |
| 1133 |
|
| 1134 |
Pattern_Size := Pattern_Size - Guard_Page_Size; |
| 1135 |
end if; |
| 1136 |
|
| 1137 |
STPO.Lock_RTS; |
| 1138 |
Initialize_Analyzer |
| 1139 |
(Self_ID.Common.Analyzer, |
| 1140 |
Self_ID.Common.Task_Image (1 .. Self_ID.Common.Task_Image_Len), |
| 1141 |
Natural (Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size), |
| 1142 |
SSE.To_Integer (Stack_Base), |
| 1143 |
Pattern_Size); |
| 1144 |
STPO.Unlock_RTS; |
| 1145 |
Fill_Stack (Self_ID.Common.Analyzer); |
| 1146 |
end; |
| 1147 |
end if; |
| 1148 |
|
| 1149 |
-- We setup the SEH (Structured Exception Handling) handler if supported |
| 1150 |
-- on the target. |
| 1151 |
|
| 1152 |
Install_SEH_Handler (SEH_Table'Address); |
| 1153 |
|
| 1154 |
-- Initialize exception occurrence |
| 1155 |
|
| 1156 |
Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence); |
| 1157 |
|
| 1158 |
-- We lock RTS_Lock to wait for activator to finish activating the rest |
| 1159 |
-- of the chain, so that everyone in the chain comes out in priority |
| 1160 |
-- order. |
| 1161 |
|
| 1162 |
-- This also protects the value of |
| 1163 |
-- Self_ID.Common.Activator.Common.Wait_Count. |
| 1164 |
|
| 1165 |
Lock_RTS; |
| 1166 |
Unlock_RTS; |
| 1167 |
|
| 1168 |
if not System.Restrictions.Abort_Allowed then |
| 1169 |
|
| 1170 |
-- If Abort is not allowed, reset the deferral level since it will |
| 1171 |
-- not get changed by the generated code. Keeping a default value |
| 1172 |
-- of one would prevent some operations (e.g. select or delay) to |
| 1173 |
-- proceed successfully. |
| 1174 |
|
| 1175 |
Self_ID.Deferral_Level := 0; |
| 1176 |
end if; |
| 1177 |
|
| 1178 |
if Global_Task_Debug_Event_Set then |
| 1179 |
Debug.Signal_Debug_Event (Debug.Debug_Event_Run, Self_ID); |
| 1180 |
end if; |
| 1181 |
|
| 1182 |
declare |
| 1183 |
use Ada.Task_Initialization; |
| 1184 |
|
| 1185 |
Global_Initialization_Handler : Initialization_Handler; |
| 1186 |
pragma Atomic (Global_Initialization_Handler); |
| 1187 |
pragma Import (Ada, Global_Initialization_Handler, |
| 1188 |
"__gnat_global_initialization_handler"); |
| 1189 |
|
| 1190 |
begin |
| 1191 |
-- We are separating the following portion of the code in order to |
| 1192 |
-- place the exception handlers in a different block. In this way, |
| 1193 |
-- we do not call Set_Jmpbuf_Address (which needs Self) before we |
| 1194 |
-- set Self in Enter_Task |
| 1195 |
|
| 1196 |
-- Call the initialization hook if any |
| 1197 |
|
| 1198 |
if Global_Initialization_Handler /= null then |
| 1199 |
Global_Initialization_Handler.all; |
| 1200 |
end if; |
| 1201 |
|
| 1202 |
-- Call the task body procedure |
| 1203 |
|
| 1204 |
-- The task body is called with abort still deferred. That |
| 1205 |
-- eliminates a dangerous window, for which we had to patch-up in |
| 1206 |
-- Terminate_Task. |
| 1207 |
|
| 1208 |
-- During the expansion of the task body, we insert an RTS-call |
| 1209 |
-- to Abort_Undefer, at the first point where abort should be |
| 1210 |
-- allowed. |
| 1211 |
|
| 1212 |
Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg); |
| 1213 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 1214 |
|
| 1215 |
exception |
| 1216 |
-- We can't call Terminate_Task in the exception handlers below, |
| 1217 |
-- since there may be (e.g. in the case of GCC exception handling) |
| 1218 |
-- clean ups associated with the exception handler that need to |
| 1219 |
-- access task specific data. |
| 1220 |
|
| 1221 |
-- Defer abort so that this task can't be aborted while exiting |
| 1222 |
|
| 1223 |
when Standard'Abort_Signal => |
| 1224 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 1225 |
|
| 1226 |
-- Update the cause that motivated the task termination so that |
| 1227 |
-- the appropriate information is passed to the task termination |
| 1228 |
-- procedure. Task termination as a result of waiting on a |
| 1229 |
-- terminate alternative is a normal termination, although it is |
| 1230 |
-- implemented using the abort mechanisms. |
| 1231 |
|
| 1232 |
if Self_ID.Terminate_Alternative then |
| 1233 |
Cause := Normal; |
| 1234 |
|
| 1235 |
if Global_Task_Debug_Event_Set then |
| 1236 |
Debug.Signal_Debug_Event |
| 1237 |
(Debug.Debug_Event_Terminated, Self_ID); |
| 1238 |
end if; |
| 1239 |
else |
| 1240 |
Cause := Abnormal; |
| 1241 |
|
| 1242 |
if Global_Task_Debug_Event_Set then |
| 1243 |
Debug.Signal_Debug_Event |
| 1244 |
(Debug.Debug_Event_Abort_Terminated, Self_ID); |
| 1245 |
end if; |
| 1246 |
end if; |
| 1247 |
|
| 1248 |
when others => |
| 1249 |
-- ??? Using an E : others here causes CD2C11A to fail on Tru64 |
| 1250 |
|
| 1251 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 1252 |
|
| 1253 |
-- Perform the task specific exception tracing duty. We handle |
| 1254 |
-- these outputs here and not in the common notification routine |
| 1255 |
-- because we need access to tasking related data and we don't |
| 1256 |
-- want to drag dependencies against tasking related units in the |
| 1257 |
-- the common notification units. Additionally, no trace is ever |
| 1258 |
-- triggered from the common routine for the Unhandled_Raise case |
| 1259 |
-- in tasks, since an exception never appears unhandled in this |
| 1260 |
-- context because of this handler. |
| 1261 |
|
| 1262 |
if Exception_Trace = Unhandled_Raise then |
| 1263 |
Trace_Unhandled_Exception_In_Task (Self_ID); |
| 1264 |
end if; |
| 1265 |
|
| 1266 |
-- Update the cause that motivated the task termination so that |
| 1267 |
-- the appropriate information is passed to the task termination |
| 1268 |
-- procedure, as well as the associated Exception_Occurrence. |
| 1269 |
|
| 1270 |
Cause := Unhandled_Exception; |
| 1271 |
|
| 1272 |
Save_Occurrence (EO, SSL.Get_Current_Excep.all.all); |
| 1273 |
|
| 1274 |
if Global_Task_Debug_Event_Set then |
| 1275 |
Debug.Signal_Debug_Event |
| 1276 |
(Debug.Debug_Event_Exception_Terminated, Self_ID); |
| 1277 |
end if; |
| 1278 |
end; |
| 1279 |
|
| 1280 |
-- Look for a task termination handler. This code is for all tasks but |
| 1281 |
-- the environment task. The task termination code for the environment |
| 1282 |
-- task is executed by SSL.Task_Termination_Handler. |
| 1283 |
|
| 1284 |
Write_Lock (Self_ID); |
| 1285 |
|
| 1286 |
if Self_ID.Common.Specific_Handler /= null then |
| 1287 |
TH := Self_ID.Common.Specific_Handler; |
| 1288 |
|
| 1289 |
-- Independent tasks should not call the Fall_Back_Handler (of the |
| 1290 |
-- environment task), because they are implementation artifacts that |
| 1291 |
-- should be invisible to Ada programs. |
| 1292 |
|
| 1293 |
elsif Self_ID.Master_Of_Task /= Independent_Task_Level then |
| 1294 |
|
| 1295 |
-- Look for a fall-back handler following the master relationship |
| 1296 |
-- for the task. As specified in ARM C.7.3 par. 9/2, "the fall-back |
| 1297 |
-- handler applies only to the dependent tasks of the task". Hence, |
| 1298 |
-- if the terminating tasks (Self_ID) had a fall-back handler, it |
| 1299 |
-- would not apply to itself, so we start the search with the parent. |
| 1300 |
|
| 1301 |
Search_Fall_Back_Handler (Self_ID.Common.Parent); |
| 1302 |
end if; |
| 1303 |
|
| 1304 |
Unlock (Self_ID); |
| 1305 |
|
| 1306 |
-- Execute the task termination handler if we found it |
| 1307 |
|
| 1308 |
if TH /= null then |
| 1309 |
begin |
| 1310 |
TH.all (Cause, Self_ID, EO); |
| 1311 |
exception |
| 1312 |
-- RM-C.7.3(16) requires all exceptions raised here to be ignored |
| 1313 |
|
| 1314 |
when others => |
| 1315 |
null; |
| 1316 |
end; |
| 1317 |
end if; |
| 1318 |
|
| 1319 |
if System.Stack_Usage.Is_Enabled then |
| 1320 |
Compute_Result (Self_ID.Common.Analyzer); |
| 1321 |
Report_Result (Self_ID.Common.Analyzer); |
| 1322 |
end if; |
| 1323 |
|
| 1324 |
Terminate_Task (Self_ID); |
| 1325 |
end Task_Wrapper; |
| 1326 |
|
| 1327 |
-------------------- |
| 1328 |
-- Terminate_Task -- |
| 1329 |
-------------------- |
| 1330 |
|
| 1331 |
-- Before we allow the thread to exit, we must clean up. This is a delicate |
| 1332 |
-- job. We must wake up the task's master, who may immediately try to |
| 1333 |
-- deallocate the ATCB from the current task WHILE IT IS STILL EXECUTING. |
| 1334 |
|
| 1335 |
-- To avoid this, the parent task must be blocked up to the latest |
| 1336 |
-- statement executed. The trouble is that we have another step that we |
| 1337 |
-- also want to postpone to the very end, i.e., calling SSL.Destroy_TSD. |
| 1338 |
-- We have to postpone that until the end because compiler-generated code |
| 1339 |
-- is likely to try to access that data at just about any point. |
| 1340 |
|
| 1341 |
-- We can't call Destroy_TSD while we are holding any other locks, because |
| 1342 |
-- it locks Global_Task_Lock, and our deadlock prevention rules require |
| 1343 |
-- that to be the outermost lock. Our first "solution" was to just lock |
| 1344 |
-- Global_Task_Lock in addition to the other locks, and force the parent to |
| 1345 |
-- also lock this lock between its wakeup and its freeing of the ATCB. See |
| 1346 |
-- Complete_Task for the parent-side of the code that has the matching |
| 1347 |
-- calls to Task_Lock and Task_Unlock. That was not really a solution, |
| 1348 |
-- since the operation Task_Unlock continued to access the ATCB after |
| 1349 |
-- unlocking, after which the parent was observed to race ahead, deallocate |
| 1350 |
-- the ATCB, and then reallocate it to another task. The call to |
| 1351 |
-- Undefer_Abort in Task_Unlock by the "terminated" task was overwriting |
| 1352 |
-- the data of the new task that reused the ATCB. To solve this problem, we |
| 1353 |
-- introduced the new operation Final_Task_Unlock. |
| 1354 |
|
| 1355 |
procedure Terminate_Task (Self_ID : Task_Id) is |
| 1356 |
Environment_Task : constant Task_Id := STPO.Environment_Task; |
| 1357 |
Master_Of_Task : Integer; |
| 1358 |
Deallocate : Boolean; |
| 1359 |
|
| 1360 |
begin |
| 1361 |
Debug.Task_Termination_Hook; |
| 1362 |
|
| 1363 |
-- Since GCC cannot allocate stack chunks efficiently without reordering |
| 1364 |
-- some of the allocations, we have to handle this unexpected situation |
| 1365 |
-- here. Normally we never have to call Vulnerable_Complete_Task here. |
| 1366 |
|
| 1367 |
if Self_ID.Common.Activator /= null then |
| 1368 |
Vulnerable_Complete_Task (Self_ID); |
| 1369 |
end if; |
| 1370 |
|
| 1371 |
Initialization.Task_Lock (Self_ID); |
| 1372 |
|
| 1373 |
Master_Of_Task := Self_ID.Master_Of_Task; |
| 1374 |
|
| 1375 |
-- Check if the current task is an independent task If so, decrement |
| 1376 |
-- the Independent_Task_Count value. |
| 1377 |
|
| 1378 |
if Master_Of_Task = Independent_Task_Level then |
| 1379 |
Write_Lock (Environment_Task); |
| 1380 |
Utilities.Independent_Task_Count := |
| 1381 |
Utilities.Independent_Task_Count - 1; |
| 1382 |
Unlock (Environment_Task); |
| 1383 |
end if; |
| 1384 |
|
| 1385 |
-- Unprotect the guard page if needed |
| 1386 |
|
| 1387 |
Stack_Guard (Self_ID, False); |
| 1388 |
|
| 1389 |
Utilities.Make_Passive (Self_ID, Task_Completed => True); |
| 1390 |
Deallocate := Self_ID.Free_On_Termination; |
| 1391 |
|
| 1392 |
pragma Assert (Check_Exit (Self_ID)); |
| 1393 |
|
| 1394 |
SSL.Destroy_TSD (Self_ID.Common.Compiler_Data); |
| 1395 |
Initialization.Final_Task_Unlock (Self_ID); |
| 1396 |
|
| 1397 |
-- WARNING: past this point, this thread must assume that the ATCB has |
| 1398 |
-- been deallocated, and can't access it anymore (which is why we have |
| 1399 |
-- saved the Free_On_Termination flag in a temporary variable). |
| 1400 |
|
| 1401 |
if Deallocate then |
| 1402 |
Free_Task (Self_ID); |
| 1403 |
end if; |
| 1404 |
|
| 1405 |
if Master_Of_Task > 0 then |
| 1406 |
STPO.Exit_Task; |
| 1407 |
end if; |
| 1408 |
end Terminate_Task; |
| 1409 |
|
| 1410 |
---------------- |
| 1411 |
-- Terminated -- |
| 1412 |
---------------- |
| 1413 |
|
| 1414 |
function Terminated (T : Task_Id) return Boolean is |
| 1415 |
Self_ID : constant Task_Id := STPO.Self; |
| 1416 |
Result : Boolean; |
| 1417 |
|
| 1418 |
begin |
| 1419 |
Initialization.Defer_Abort_Nestable (Self_ID); |
| 1420 |
Write_Lock (T); |
| 1421 |
Result := T.Common.State = Terminated; |
| 1422 |
Unlock (T); |
| 1423 |
Initialization.Undefer_Abort_Nestable (Self_ID); |
| 1424 |
|
| 1425 |
return Result; |
| 1426 |
end Terminated; |
| 1427 |
|
| 1428 |
---------------------------------------- |
| 1429 |
-- Trace_Unhandled_Exception_In_Task -- |
| 1430 |
---------------------------------------- |
| 1431 |
|
| 1432 |
procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id) is |
| 1433 |
procedure To_Stderr (S : String); |
| 1434 |
pragma Import (Ada, To_Stderr, "__gnat_to_stderr"); |
| 1435 |
|
| 1436 |
use System.Soft_Links; |
| 1437 |
|
| 1438 |
function To_Address is new |
| 1439 |
Ada.Unchecked_Conversion |
| 1440 |
(Task_Id, System.Task_Primitives.Task_Address); |
| 1441 |
|
| 1442 |
Excep : constant Exception_Occurrence_Access := |
| 1443 |
SSL.Get_Current_Excep.all; |
| 1444 |
|
| 1445 |
begin |
| 1446 |
-- This procedure is called by the task outermost handler in |
| 1447 |
-- Task_Wrapper below, so only once the task stack has been fully |
| 1448 |
-- unwound. The common notification routine has been called at the |
| 1449 |
-- raise point already. |
| 1450 |
|
| 1451 |
-- Lock to prevent unsynchronized output |
| 1452 |
|
| 1453 |
Initialization.Task_Lock (Self_Id); |
| 1454 |
To_Stderr ("task "); |
| 1455 |
|
| 1456 |
if Self_Id.Common.Task_Image_Len /= 0 then |
| 1457 |
To_Stderr |
| 1458 |
(Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len)); |
| 1459 |
To_Stderr ("_"); |
| 1460 |
end if; |
| 1461 |
|
| 1462 |
To_Stderr (System.Address_Image (To_Address (Self_Id))); |
| 1463 |
To_Stderr (" terminated by unhandled exception"); |
| 1464 |
To_Stderr ([ASCII.LF]); |
| 1465 |
To_Stderr (Exception_Information (Excep.all)); |
| 1466 |
Initialization.Task_Unlock (Self_Id); |
| 1467 |
end Trace_Unhandled_Exception_In_Task; |
| 1468 |
|
| 1469 |
------------------------------------ |
| 1470 |
-- Vulnerable_Complete_Activation -- |
| 1471 |
------------------------------------ |
| 1472 |
|
| 1473 |
-- As in several other places, the locks of the activator and activated |
| 1474 |
-- task are both locked here. This follows our deadlock prevention lock |
| 1475 |
-- ordering policy, since the activated task must be created after the |
| 1476 |
-- activator. |
| 1477 |
|
| 1478 |
procedure Vulnerable_Complete_Activation (Self_ID : Task_Id) is |
| 1479 |
Activator : constant Task_Id := Self_ID.Common.Activator; |
| 1480 |
|
| 1481 |
begin |
| 1482 |
pragma Debug (Debug.Trace (Self_ID, "V_Complete_Activation", 'C')); |
| 1483 |
|
| 1484 |
Write_Lock (Activator); |
| 1485 |
Write_Lock (Self_ID); |
| 1486 |
|
| 1487 |
pragma Assert (Self_ID.Common.Activator /= null); |
| 1488 |
|
| 1489 |
-- Remove dangling reference to Activator, since a task may outlive its |
| 1490 |
-- activator. |
| 1491 |
|
| 1492 |
Self_ID.Common.Activator := null; |
| 1493 |
|
| 1494 |
-- Wake up the activator, if it is waiting for a chain of tasks to |
| 1495 |
-- activate, and we are the last in the chain to complete activation. |
| 1496 |
|
| 1497 |
if Activator.Common.State = Activator_Sleep then |
| 1498 |
Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1; |
| 1499 |
|
| 1500 |
if Activator.Common.Wait_Count = 0 then |
| 1501 |
Wakeup (Activator, Activator_Sleep); |
| 1502 |
end if; |
| 1503 |
end if; |
| 1504 |
|
| 1505 |
-- The activator raises a Tasking_Error if any task it is activating |
| 1506 |
-- is completed before the activation is done. However, if the reason |
| 1507 |
-- for the task completion is an abort, we do not raise an exception. |
| 1508 |
-- See RM 9.2(5). |
| 1509 |
|
| 1510 |
if not Self_ID.Callable |
| 1511 |
and then Self_ID.Pending_ATC_Level /= Level_Completed_Task |
| 1512 |
then |
| 1513 |
Activator.Common.Activation_Failed := True; |
| 1514 |
end if; |
| 1515 |
|
| 1516 |
Unlock (Self_ID); |
| 1517 |
Unlock (Activator); |
| 1518 |
|
| 1519 |
-- After the activation, active priority should be the same as base |
| 1520 |
-- priority. We must unlock the Activator first, though, since it |
| 1521 |
-- should not wait if we have lower priority. |
| 1522 |
|
| 1523 |
if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then |
| 1524 |
Write_Lock (Self_ID); |
| 1525 |
Set_Priority (Self_ID, Self_ID.Common.Base_Priority); |
| 1526 |
Unlock (Self_ID); |
| 1527 |
end if; |
| 1528 |
end Vulnerable_Complete_Activation; |
| 1529 |
|
| 1530 |
-------------------------------- |
| 1531 |
-- Vulnerable_Complete_Master -- |
| 1532 |
-------------------------------- |
| 1533 |
|
| 1534 |
procedure Vulnerable_Complete_Master (Self_ID : Task_Id) is |
| 1535 |
C : Task_Id; |
| 1536 |
P : Task_Id; |
| 1537 |
CM : constant Master_Level := Self_ID.Master_Within; |
| 1538 |
T : aliased Task_Id; |
| 1539 |
|
| 1540 |
To_Be_Freed : Task_Id; |
| 1541 |
-- This is a list of ATCBs to be freed, after we have released all RTS |
| 1542 |
-- locks. This is necessary because of the locking order rules, since |
| 1543 |
-- the storage manager uses Global_Task_Lock. |
| 1544 |
|
| 1545 |
pragma Warnings (Off); |
| 1546 |
function Check_Unactivated_Tasks return Boolean; |
| 1547 |
pragma Warnings (On); |
| 1548 |
-- Temporary error-checking code below. This is part of the checks |
| 1549 |
-- added in the new run time. Call it only inside a pragma Assert. |
| 1550 |
|
| 1551 |
----------------------------- |
| 1552 |
-- Check_Unactivated_Tasks -- |
| 1553 |
----------------------------- |
| 1554 |
|
| 1555 |
function Check_Unactivated_Tasks return Boolean is |
| 1556 |
begin |
| 1557 |
Lock_RTS; |
| 1558 |
Write_Lock (Self_ID); |
| 1559 |
|
| 1560 |
C := All_Tasks_List; |
| 1561 |
while C /= null loop |
| 1562 |
if C.Common.Activator = Self_ID and then C.Master_Of_Task = CM then |
| 1563 |
return False; |
| 1564 |
end if; |
| 1565 |
|
| 1566 |
if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then |
| 1567 |
Write_Lock (C); |
| 1568 |
|
| 1569 |
if C.Common.State = Unactivated then |
| 1570 |
return False; |
| 1571 |
end if; |
| 1572 |
|
| 1573 |
Unlock (C); |
| 1574 |
end if; |
| 1575 |
|
| 1576 |
C := C.Common.All_Tasks_Link; |
| 1577 |
end loop; |
| 1578 |
|
| 1579 |
Unlock (Self_ID); |
| 1580 |
Unlock_RTS; |
| 1581 |
|
| 1582 |
return True; |
| 1583 |
end Check_Unactivated_Tasks; |
| 1584 |
|
| 1585 |
-- Start of processing for Vulnerable_Complete_Master |
| 1586 |
|
| 1587 |
begin |
| 1588 |
pragma Debug |
| 1589 |
(Debug.Trace (Self_ID, "V_Complete_Master(" & CM'Img & ")", 'C')); |
| 1590 |
|
| 1591 |
pragma Assert (Self_ID.Common.Wait_Count = 0); |
| 1592 |
pragma Assert |
| 1593 |
(Self_ID.Deferral_Level > 0 |
| 1594 |
or else not System.Restrictions.Abort_Allowed); |
| 1595 |
|
| 1596 |
-- Count how many active dependent tasks this master currently has, and |
| 1597 |
-- record this in Wait_Count. |
| 1598 |
|
| 1599 |
-- This count should start at zero, since it is initialized to zero for |
| 1600 |
-- new tasks, and the task should not exit the sleep-loops that use this |
| 1601 |
-- count until the count reaches zero. |
| 1602 |
|
| 1603 |
-- While we're counting, if we run across any unactivated tasks that |
| 1604 |
-- belong to this master, we summarily terminate them as required by |
| 1605 |
-- RM-9.2(6). |
| 1606 |
|
| 1607 |
Lock_RTS; |
| 1608 |
Write_Lock (Self_ID); |
| 1609 |
|
| 1610 |
C := All_Tasks_List; |
| 1611 |
while C /= null loop |
| 1612 |
|
| 1613 |
-- Terminate unactivated (never-to-be activated) tasks |
| 1614 |
|
| 1615 |
if C.Common.Activator = Self_ID and then C.Master_Of_Task = CM then |
| 1616 |
|
| 1617 |
-- Usually, C.Common.Activator = Self_ID implies C.Master_Of_Task |
| 1618 |
-- = CM. The only case where C is pending activation by this |
| 1619 |
-- task, but the master of C is not CM is when C is part of a |
| 1620 |
-- return object of a build-in-place function. |
| 1621 |
|
| 1622 |
pragma Assert (C.Common.State = Unactivated); |
| 1623 |
|
| 1624 |
Write_Lock (C); |
| 1625 |
C.Common.Activator := null; |
| 1626 |
C.Common.State := Terminated; |
| 1627 |
C.Callable := False; |
| 1628 |
Utilities.Cancel_Queued_Entry_Calls (C); |
| 1629 |
Unlock (C); |
| 1630 |
end if; |
| 1631 |
|
| 1632 |
-- Count it if directly dependent on this master |
| 1633 |
|
| 1634 |
if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then |
| 1635 |
Write_Lock (C); |
| 1636 |
|
| 1637 |
if C.Awake_Count /= 0 then |
| 1638 |
Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1; |
| 1639 |
end if; |
| 1640 |
|
| 1641 |
Unlock (C); |
| 1642 |
end if; |
| 1643 |
|
| 1644 |
C := C.Common.All_Tasks_Link; |
| 1645 |
end loop; |
| 1646 |
|
| 1647 |
Self_ID.Common.State := Master_Completion_Sleep; |
| 1648 |
Unlock (Self_ID); |
| 1649 |
Unlock_RTS; |
| 1650 |
|
| 1651 |
-- Wait until dependent tasks are all terminated or ready to terminate. |
| 1652 |
-- While waiting, the task may be awakened if the task's priority needs |
| 1653 |
-- changing, or this master is aborted. In the latter case, we abort the |
| 1654 |
-- dependents, and resume waiting until Wait_Count goes to zero. |
| 1655 |
|
| 1656 |
Write_Lock (Self_ID); |
| 1657 |
|
| 1658 |
loop |
| 1659 |
exit when Self_ID.Common.Wait_Count = 0; |
| 1660 |
|
| 1661 |
-- Here is a difference as compared to Complete_Master |
| 1662 |
|
| 1663 |
if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level |
| 1664 |
and then not Self_ID.Dependents_Aborted |
| 1665 |
then |
| 1666 |
Unlock (Self_ID); |
| 1667 |
Lock_RTS; |
| 1668 |
Abort_Dependents (Self_ID); |
| 1669 |
Unlock_RTS; |
| 1670 |
Write_Lock (Self_ID); |
| 1671 |
else |
| 1672 |
pragma Debug |
| 1673 |
(Debug.Trace (Self_ID, "master_completion_sleep", 'C')); |
| 1674 |
Sleep (Self_ID, Master_Completion_Sleep); |
| 1675 |
end if; |
| 1676 |
end loop; |
| 1677 |
|
| 1678 |
Self_ID.Common.State := Runnable; |
| 1679 |
Unlock (Self_ID); |
| 1680 |
|
| 1681 |
-- Dependents are all terminated or on terminate alternatives. Now, |
| 1682 |
-- force those on terminate alternatives to terminate, by aborting them. |
| 1683 |
|
| 1684 |
pragma Assert (Check_Unactivated_Tasks); |
| 1685 |
|
| 1686 |
if Self_ID.Alive_Count > 1 then |
| 1687 |
-- ??? |
| 1688 |
-- Consider finding a way to skip the following extra steps if there |
| 1689 |
-- are no dependents with terminate alternatives. This could be done |
| 1690 |
-- by adding another count to the ATCB, similar to Awake_Count, but |
| 1691 |
-- keeping track of tasks that are on terminate alternatives. |
| 1692 |
|
| 1693 |
pragma Assert (Self_ID.Common.Wait_Count = 0); |
| 1694 |
|
| 1695 |
-- Force any remaining dependents to terminate by aborting them |
| 1696 |
|
| 1697 |
Lock_RTS; |
| 1698 |
Abort_Dependents (Self_ID); |
| 1699 |
|
| 1700 |
-- Above, when we "abort" the dependents we are simply using this |
| 1701 |
-- operation for convenience. We are not required to support the full |
| 1702 |
-- abort-statement semantics; in particular, we are not required to |
| 1703 |
-- immediately cancel any queued or in-service entry calls. That is |
| 1704 |
-- good, because if we tried to cancel a call we would need to lock |
| 1705 |
-- the caller, in order to wake the caller up. Our anti-deadlock |
| 1706 |
-- rules prevent us from doing that without releasing the locks on C |
| 1707 |
-- and Self_ID. Releasing and retaking those locks would be wasteful |
| 1708 |
-- at best, and should not be considered further without more |
| 1709 |
-- detailed analysis of potential concurrent accesses to the ATCBs |
| 1710 |
-- of C and Self_ID. |
| 1711 |
|
| 1712 |
-- Count how many "alive" dependent tasks this master currently has, |
| 1713 |
-- and record this in Wait_Count. This count should start at zero, |
| 1714 |
-- since it is initialized to zero for new tasks, and the task should |
| 1715 |
-- not exit the sleep-loops that use this count until the count |
| 1716 |
-- reaches zero. |
| 1717 |
|
| 1718 |
pragma Assert (Self_ID.Common.Wait_Count = 0); |
| 1719 |
|
| 1720 |
Write_Lock (Self_ID); |
| 1721 |
|
| 1722 |
C := All_Tasks_List; |
| 1723 |
while C /= null loop |
| 1724 |
if C.Common.Parent = Self_ID and then C.Master_Of_Task = CM then |
| 1725 |
Write_Lock (C); |
| 1726 |
|
| 1727 |
pragma Assert (C.Awake_Count = 0); |
| 1728 |
|
| 1729 |
if C.Alive_Count > 0 then |
| 1730 |
pragma Assert (C.Terminate_Alternative); |
| 1731 |
Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1; |
| 1732 |
end if; |
| 1733 |
|
| 1734 |
Unlock (C); |
| 1735 |
end if; |
| 1736 |
|
| 1737 |
C := C.Common.All_Tasks_Link; |
| 1738 |
end loop; |
| 1739 |
|
| 1740 |
Self_ID.Common.State := Master_Phase_2_Sleep; |
| 1741 |
Unlock (Self_ID); |
| 1742 |
Unlock_RTS; |
| 1743 |
|
| 1744 |
-- Wait for all counted tasks to finish terminating themselves |
| 1745 |
|
| 1746 |
Write_Lock (Self_ID); |
| 1747 |
|
| 1748 |
loop |
| 1749 |
exit when Self_ID.Common.Wait_Count = 0; |
| 1750 |
Sleep (Self_ID, Master_Phase_2_Sleep); |
| 1751 |
end loop; |
| 1752 |
|
| 1753 |
Self_ID.Common.State := Runnable; |
| 1754 |
Unlock (Self_ID); |
| 1755 |
end if; |
| 1756 |
|
| 1757 |
-- We don't wake up for abort here. We are already terminating just as |
| 1758 |
-- fast as we can, so there is no point. |
| 1759 |
|
| 1760 |
-- Remove terminated tasks from the list of Self_ID's dependents, but |
| 1761 |
-- don't free their ATCBs yet, because of lock order restrictions, which |
| 1762 |
-- don't allow us to call "free" or "malloc" while holding any other |
| 1763 |
-- locks. Instead, we put those ATCBs to be freed onto a temporary list, |
| 1764 |
-- called To_Be_Freed. |
| 1765 |
|
| 1766 |
Lock_RTS; |
| 1767 |
C := All_Tasks_List; |
| 1768 |
P := null; |
| 1769 |
while C /= null loop |
| 1770 |
|
| 1771 |
-- If Free_On_Termination is set, do nothing here, and let the |
| 1772 |
-- task free itself if not already done, otherwise we risk a race |
| 1773 |
-- condition where Vulnerable_Free_Task is called in the loop below, |
| 1774 |
-- while the task calls Free_Task itself, in Terminate_Task. |
| 1775 |
|
| 1776 |
if C.Common.Parent = Self_ID |
| 1777 |
and then C.Master_Of_Task >= CM |
| 1778 |
and then not C.Free_On_Termination |
| 1779 |
then |
| 1780 |
if P /= null then |
| 1781 |
P.Common.All_Tasks_Link := C.Common.All_Tasks_Link; |
| 1782 |
else |
| 1783 |
All_Tasks_List := C.Common.All_Tasks_Link; |
| 1784 |
end if; |
| 1785 |
|
| 1786 |
T := C.Common.All_Tasks_Link; |
| 1787 |
C.Common.All_Tasks_Link := To_Be_Freed; |
| 1788 |
To_Be_Freed := C; |
| 1789 |
C := T; |
| 1790 |
|
| 1791 |
else |
| 1792 |
P := C; |
| 1793 |
C := C.Common.All_Tasks_Link; |
| 1794 |
end if; |
| 1795 |
end loop; |
| 1796 |
|
| 1797 |
Unlock_RTS; |
| 1798 |
|
| 1799 |
-- Free all the ATCBs on the list To_Be_Freed |
| 1800 |
|
| 1801 |
-- The ATCBs in the list are no longer in All_Tasks_List, and after |
| 1802 |
-- any interrupt entries are detached from them they should no longer |
| 1803 |
-- be referenced. |
| 1804 |
|
| 1805 |
-- Global_Task_Lock (Task_Lock/Unlock) is locked in the loop below to |
| 1806 |
-- avoid a race between a terminating task and its parent. The parent |
| 1807 |
-- might try to deallocate the ACTB out from underneath the exiting |
| 1808 |
-- task. Note that Free will also lock Global_Task_Lock, but that is |
| 1809 |
-- OK, since this is the *one* lock for which we have a mechanism to |
| 1810 |
-- support nested locking. See Task_Wrapper and its finalizer for more |
| 1811 |
-- explanation. |
| 1812 |
|
| 1813 |
-- ??? |
| 1814 |
-- The check "T.Common.Parent /= null ..." below is to prevent dangling |
| 1815 |
-- references to terminated library-level tasks, which could otherwise |
| 1816 |
-- occur during finalization of library-level objects. A better solution |
| 1817 |
-- might be to hook task objects into the finalization chain and |
| 1818 |
-- deallocate the ATCB when the task object is deallocated. However, |
| 1819 |
-- this change is not likely to gain anything significant, since all |
| 1820 |
-- this storage should be recovered en-masse when the process exits. |
| 1821 |
|
| 1822 |
while To_Be_Freed /= null loop |
| 1823 |
T := To_Be_Freed; |
| 1824 |
To_Be_Freed := T.Common.All_Tasks_Link; |
| 1825 |
|
| 1826 |
-- ??? On SGI there is currently no Interrupt_Manager, that's why we |
| 1827 |
-- need to check if the Interrupt_Manager_ID is null. |
| 1828 |
|
| 1829 |
if T.Interrupt_Entry and then Interrupt_Manager_ID /= null then |
| 1830 |
declare |
| 1831 |
Detach_Interrupt_Entries_Index : constant Task_Entry_Index := 1; |
| 1832 |
-- Corresponds to the entry index of System.Interrupts. |
| 1833 |
-- Interrupt_Manager.Detach_Interrupt_Entries. Be sure |
| 1834 |
-- to update this value when changing Interrupt_Manager specs. |
| 1835 |
|
| 1836 |
type Param_Type is access all Task_Id; |
| 1837 |
|
| 1838 |
Param : aliased Param_Type := T'Access; |
| 1839 |
|
| 1840 |
begin |
| 1841 |
System.Tasking.Rendezvous.Call_Simple |
| 1842 |
(Interrupt_Manager_ID, Detach_Interrupt_Entries_Index, |
| 1843 |
Param'Address); |
| 1844 |
end; |
| 1845 |
end if; |
| 1846 |
|
| 1847 |
if (T.Common.Parent /= null |
| 1848 |
and then T.Common.Parent.Common.Parent /= null) |
| 1849 |
or else T.Master_Of_Task > Library_Task_Level |
| 1850 |
then |
| 1851 |
Initialization.Task_Lock (Self_ID); |
| 1852 |
|
| 1853 |
-- If Sec_Stack_Ptr is not null, it means that Destroy_TSD |
| 1854 |
-- has not been called yet (case of an unactivated task). |
| 1855 |
|
| 1856 |
if T.Common.Compiler_Data.Sec_Stack_Ptr /= null then |
| 1857 |
SSL.Destroy_TSD (T.Common.Compiler_Data); |
| 1858 |
end if; |
| 1859 |
|
| 1860 |
Vulnerable_Free_Task (T); |
| 1861 |
Initialization.Task_Unlock (Self_ID); |
| 1862 |
end if; |
| 1863 |
end loop; |
| 1864 |
|
| 1865 |
-- It might seem nice to let the terminated task deallocate its own |
| 1866 |
-- ATCB. That would not cover the case of unactivated tasks. It also |
| 1867 |
-- would force us to keep the underlying thread around past termination, |
| 1868 |
-- since references to the ATCB are possible past termination. |
| 1869 |
|
| 1870 |
-- Currently, we get rid of the thread as soon as the task terminates, |
| 1871 |
-- and let the parent recover the ATCB later. |
| 1872 |
|
| 1873 |
-- Some day, if we want to recover the ATCB earlier, at task |
| 1874 |
-- termination, we could consider using "fat task IDs", that include the |
| 1875 |
-- serial number with the ATCB pointer, to catch references to tasks |
| 1876 |
-- that no longer have ATCBs. It is not clear how much this would gain, |
| 1877 |
-- since the user-level task object would still be occupying storage. |
| 1878 |
|
| 1879 |
-- Make next master level up active. We don't need to lock the ATCB, |
| 1880 |
-- since the value is only updated by each task for itself. |
| 1881 |
|
| 1882 |
Self_ID.Master_Within := CM - 1; |
| 1883 |
|
| 1884 |
Debug.Master_Completed_Hook (Self_ID, CM); |
| 1885 |
end Vulnerable_Complete_Master; |
| 1886 |
|
| 1887 |
------------------------------ |
| 1888 |
-- Vulnerable_Complete_Task -- |
| 1889 |
------------------------------ |
| 1890 |
|
| 1891 |
-- Complete the calling task |
| 1892 |
|
| 1893 |
-- This procedure must be called with abort deferred. It should only be |
| 1894 |
-- called by Complete_Task and Finalize_Global_Tasks (for the environment |
| 1895 |
-- task). |
| 1896 |
|
| 1897 |
-- The effect is similar to that of Complete_Master. Differences include |
| 1898 |
-- the closing of entries here, and computation of the number of active |
| 1899 |
-- dependent tasks in Complete_Master. |
| 1900 |
|
| 1901 |
-- We don't lock Self_ID before the call to Vulnerable_Complete_Activation, |
| 1902 |
-- because that does its own locking, and because we do not need the lock |
| 1903 |
-- to test Self_ID.Common.Activator. That value should only be read and |
| 1904 |
-- modified by Self. |
| 1905 |
|
| 1906 |
procedure Vulnerable_Complete_Task (Self_ID : Task_Id) is |
| 1907 |
begin |
| 1908 |
pragma Assert |
| 1909 |
(Self_ID.Deferral_Level > 0 |
| 1910 |
or else not System.Restrictions.Abort_Allowed); |
| 1911 |
pragma Assert (Self_ID = Self); |
| 1912 |
pragma Assert |
| 1913 |
(Self_ID.Master_Within in |
| 1914 |
Self_ID.Master_Of_Task .. Self_ID.Master_Of_Task + 3); |
| 1915 |
pragma Assert (Self_ID.Common.Wait_Count = 0); |
| 1916 |
pragma Assert (Self_ID.Open_Accepts = null); |
| 1917 |
pragma Assert (Self_ID.ATC_Nesting_Level = Level_No_ATC_Occurring); |
| 1918 |
|
| 1919 |
pragma Debug (Debug.Trace (Self_ID, "V_Complete_Task", 'C')); |
| 1920 |
|
| 1921 |
Write_Lock (Self_ID); |
| 1922 |
Self_ID.Callable := False; |
| 1923 |
|
| 1924 |
-- In theory, Self should have no pending entry calls left on its |
| 1925 |
-- call-stack. Each async. select statement should clean its own call, |
| 1926 |
-- and blocking entry calls should defer abort until the calls are |
| 1927 |
-- cancelled, then clean up. |
| 1928 |
|
| 1929 |
Utilities.Cancel_Queued_Entry_Calls (Self_ID); |
| 1930 |
Unlock (Self_ID); |
| 1931 |
|
| 1932 |
if Self_ID.Common.Activator /= null then |
| 1933 |
Vulnerable_Complete_Activation (Self_ID); |
| 1934 |
end if; |
| 1935 |
|
| 1936 |
-- If Self_ID.Master_Within = Self_ID.Master_Of_Task + 2 we may have |
| 1937 |
-- dependent tasks for which we need to wait. Otherwise we just exit. |
| 1938 |
|
| 1939 |
if Self_ID.Master_Within = Self_ID.Master_Of_Task + 2 then |
| 1940 |
Vulnerable_Complete_Master (Self_ID); |
| 1941 |
end if; |
| 1942 |
end Vulnerable_Complete_Task; |
| 1943 |
|
| 1944 |
-------------------------- |
| 1945 |
-- Vulnerable_Free_Task -- |
| 1946 |
-------------------------- |
| 1947 |
|
| 1948 |
-- Recover all runtime system storage associated with the task T. This |
| 1949 |
-- should only be called after T has terminated and will no longer be |
| 1950 |
-- referenced. |
| 1951 |
|
| 1952 |
-- For tasks created by an allocator that fails, due to an exception, it |
| 1953 |
-- is called from Expunge_Unactivated_Tasks. |
| 1954 |
|
| 1955 |
-- For tasks created by elaboration of task object declarations it is |
| 1956 |
-- called from the finalization code of the Task_Wrapper procedure. |
| 1957 |
|
| 1958 |
procedure Vulnerable_Free_Task (T : Task_Id) is |
| 1959 |
begin |
| 1960 |
pragma Debug (Debug.Trace (Self, "Vulnerable_Free_Task", 'C', T)); |
| 1961 |
|
| 1962 |
Write_Lock (T); |
| 1963 |
Initialization.Finalize_Attributes (T); |
| 1964 |
Unlock (T); |
| 1965 |
|
| 1966 |
System.Task_Primitives.Operations.Finalize_TCB (T); |
| 1967 |
end Vulnerable_Free_Task; |
| 1968 |
|
| 1969 |
-- Package elaboration code |
| 1970 |
|
| 1971 |
begin |
| 1972 |
-- Establish the Adafinal softlink |
| 1973 |
|
| 1974 |
-- This is not done inside the central RTS initialization routine |
| 1975 |
-- to avoid with'ing this package from System.Tasking.Initialization. |
| 1976 |
|
| 1977 |
SSL.Adafinal := Finalize_Global_Tasks'Access; |
| 1978 |
|
| 1979 |
-- Establish soft links for subprograms that manipulate master_id's. |
| 1980 |
-- This cannot be done when the RTS is initialized, because of various |
| 1981 |
-- elaboration constraints. |
| 1982 |
|
| 1983 |
SSL.Current_Master := Stages.Current_Master'Access; |
| 1984 |
SSL.Enter_Master := Stages.Enter_Master'Access; |
| 1985 |
SSL.Complete_Master := Stages.Complete_Master'Access; |
| 1986 |
end System.Tasking.Stages; |