| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . T A S K I N G . I N I T I A L I Z A T I O N -- |
| 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 Style_Checks (All_Checks); |
| 33 |
-- Turn off subprogram alpha ordering check, since we group soft link bodies |
| 34 |
-- and dummy soft link bodies together separately in this unit. |
| 35 |
|
| 36 |
with System.OS_Locks; |
| 37 |
with System.Soft_Links; |
| 38 |
with System.Soft_Links.Tasking; |
| 39 |
with System.Task_Primitives; |
| 40 |
with System.Task_Primitives.Operations; |
| 41 |
with System.Tasking.Debug; |
| 42 |
with System.Tasking.Task_Attributes; |
| 43 |
|
| 44 |
with System.Secondary_Stack; |
| 45 |
pragma Elaborate_All (System.Secondary_Stack); |
| 46 |
pragma Unreferenced (System.Secondary_Stack); |
| 47 |
-- Make sure the body of Secondary_Stack is elaborated before calling |
| 48 |
-- Init_Tasking_Soft_Links. See comments for this routine for explanation. |
| 49 |
|
| 50 |
package body System.Tasking.Initialization is |
| 51 |
|
| 52 |
package SOL renames System.OS_Locks; |
| 53 |
package SSL renames System.Soft_Links; |
| 54 |
package STPO renames System.Task_Primitives.Operations; |
| 55 |
|
| 56 |
use Parameters; |
| 57 |
use Task_Primitives.Operations; |
| 58 |
|
| 59 |
Global_Task_Lock : aliased SOL.RTS_Lock; |
| 60 |
-- This is a global lock; it is used to execute in mutual exclusion from |
| 61 |
-- all other tasks. It is only used by Task_Lock, Task_Unlock, and |
| 62 |
-- Final_Task_Unlock. |
| 63 |
|
| 64 |
---------------------------------------------------------------------- |
| 65 |
-- Tasking versions of some services needed by non-tasking programs -- |
| 66 |
---------------------------------------------------------------------- |
| 67 |
|
| 68 |
procedure Abort_Defer; |
| 69 |
-- NON-INLINE versions without Self_ID for soft links |
| 70 |
|
| 71 |
procedure Abort_Undefer; |
| 72 |
-- NON-INLINE versions without Self_ID for soft links |
| 73 |
|
| 74 |
procedure Task_Lock; |
| 75 |
-- Locks out other tasks. Preceding a section of code by Task_Lock and |
| 76 |
-- following it by Task_Unlock creates a critical region. This is used |
| 77 |
-- for ensuring that a region of non-tasking code (such as code used to |
| 78 |
-- allocate memory) is tasking safe. Note that it is valid for calls to |
| 79 |
-- Task_Lock/Task_Unlock to be nested, and this must work properly, i.e. |
| 80 |
-- only the corresponding outer level Task_Unlock will actually unlock. |
| 81 |
|
| 82 |
procedure Task_Unlock; |
| 83 |
-- Releases lock previously set by call to Task_Lock. In the nested case, |
| 84 |
-- all nested locks must be released before other tasks competing for the |
| 85 |
-- tasking lock are released. |
| 86 |
|
| 87 |
function Get_Current_Excep return SSL.EOA; |
| 88 |
-- Task-safe version of SSL.Get_Current_Excep |
| 89 |
|
| 90 |
function Task_Name return String; |
| 91 |
-- Returns current task's name |
| 92 |
|
| 93 |
procedure Initialize_RTS_Lock (Addr : Address); |
| 94 |
-- Initialize the RTS lock at Addr |
| 95 |
|
| 96 |
procedure Finalize_RTS_Lock (Addr : Address); |
| 97 |
-- Finalize the RTS lock at Addr |
| 98 |
|
| 99 |
procedure Acquire_RTS_Lock (Addr : Address); |
| 100 |
-- Acquire the RTS lock at Addr |
| 101 |
|
| 102 |
procedure Release_RTS_Lock (Addr : Address); |
| 103 |
-- Release the RTS lock at Addr |
| 104 |
|
| 105 |
---------------------------- |
| 106 |
-- Tasking Initialization -- |
| 107 |
---------------------------- |
| 108 |
|
| 109 |
procedure Init_RTS; |
| 110 |
-- This procedure completes the initialization of the GNARL. The first part |
| 111 |
-- of the initialization is done in the body of System.Tasking. It consists |
| 112 |
-- of initializing global locks, and installing tasking versions of certain |
| 113 |
-- operations used by the compiler. Init_RTS is called during elaboration. |
| 114 |
|
| 115 |
procedure Tasking_Runtime_Initialize; |
| 116 |
pragma Export (Ada, Tasking_Runtime_Initialize, |
| 117 |
"__gnat_tasking_runtime_initialize"); |
| 118 |
pragma Linker_Constructor (Tasking_Runtime_Initialize); |
| 119 |
-- This procedure starts the initialization of the GNARL. It installs the |
| 120 |
-- tasking version of the RTS_Lock manipulation routines. It is called |
| 121 |
-- very early before the elaboration of all the Ada units of the program, |
| 122 |
-- including those of the runtime, because this elaboration may require |
| 123 |
-- the initialization of RTS_Lock objects, which means that it must only |
| 124 |
-- contain code to which pragma Restrictions (No_Elaboration_Code) applies. |
| 125 |
|
| 126 |
-------------------------- |
| 127 |
-- Change_Base_Priority -- |
| 128 |
-------------------------- |
| 129 |
|
| 130 |
-- Call only with abort deferred and holding Self_ID locked |
| 131 |
|
| 132 |
procedure Change_Base_Priority (T : Task_Id) is |
| 133 |
begin |
| 134 |
if T.Common.Base_Priority /= T.New_Base_Priority then |
| 135 |
T.Common.Base_Priority := T.New_Base_Priority; |
| 136 |
Set_Priority (T, T.Common.Base_Priority); |
| 137 |
end if; |
| 138 |
end Change_Base_Priority; |
| 139 |
|
| 140 |
------------------------ |
| 141 |
-- Check_Abort_Status -- |
| 142 |
------------------------ |
| 143 |
|
| 144 |
function Check_Abort_Status return Integer is |
| 145 |
Self_ID : constant Task_Id := Self; |
| 146 |
begin |
| 147 |
if Self_ID /= null |
| 148 |
and then Self_ID.Deferral_Level = 0 |
| 149 |
and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level |
| 150 |
and then not Self_ID.Aborting |
| 151 |
then |
| 152 |
return 1; |
| 153 |
else |
| 154 |
return 0; |
| 155 |
end if; |
| 156 |
end Check_Abort_Status; |
| 157 |
|
| 158 |
----------------- |
| 159 |
-- Defer_Abort -- |
| 160 |
----------------- |
| 161 |
|
| 162 |
procedure Defer_Abort (Self_ID : Task_Id) is |
| 163 |
begin |
| 164 |
if No_Abort then |
| 165 |
return; |
| 166 |
end if; |
| 167 |
|
| 168 |
pragma Assert (Self_ID.Deferral_Level = 0); |
| 169 |
|
| 170 |
-- pragma Assert |
| 171 |
-- (Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level); |
| 172 |
|
| 173 |
-- The above check has been useful in detecting mismatched defer/undefer |
| 174 |
-- pairs. You may uncomment it when testing on systems that support |
| 175 |
-- preemptive abort. |
| 176 |
|
| 177 |
-- If the OS supports preemptive abort (e.g. pthread_kill), it should |
| 178 |
-- have happened already. A problem is with systems that do not support |
| 179 |
-- preemptive abort, and so rely on polling. On such systems we may get |
| 180 |
-- false failures of the assertion, since polling for pending abort does |
| 181 |
-- no occur until the abort undefer operation. |
| 182 |
|
| 183 |
-- Even on systems that only poll for abort, the assertion may be useful |
| 184 |
-- for catching missed abort completion polling points. The operations |
| 185 |
-- that undefer abort poll for pending aborts. This covers most of the |
| 186 |
-- places where the core Ada semantics require abort to be caught, |
| 187 |
-- without any special attention. However, this generally happens on |
| 188 |
-- exit from runtime system call, which means a pending abort will not |
| 189 |
-- be noticed on the way into the runtime system. We considered adding a |
| 190 |
-- check for pending aborts at this point, but chose not to, because of |
| 191 |
-- the overhead. Instead, we searched for RTS calls where abort |
| 192 |
-- completion is required and a task could go farther than Ada allows |
| 193 |
-- before undeferring abort; we then modified the code to ensure the |
| 194 |
-- abort would be detected. |
| 195 |
|
| 196 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1; |
| 197 |
end Defer_Abort; |
| 198 |
|
| 199 |
-------------------------- |
| 200 |
-- Defer_Abort_Nestable -- |
| 201 |
-------------------------- |
| 202 |
|
| 203 |
procedure Defer_Abort_Nestable (Self_ID : Task_Id) is |
| 204 |
begin |
| 205 |
if No_Abort then |
| 206 |
return; |
| 207 |
end if; |
| 208 |
|
| 209 |
-- The following assertion is by default disabled. See the comment in |
| 210 |
-- Defer_Abort on the situations in which it may be useful to uncomment |
| 211 |
-- this assertion and enable the test. |
| 212 |
|
| 213 |
-- pragma Assert |
| 214 |
-- (Self_ID.Pending_ATC_Level >= Self_ID.ATC_Nesting_Level or else |
| 215 |
-- Self_ID.Deferral_Level > 0); |
| 216 |
|
| 217 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1; |
| 218 |
end Defer_Abort_Nestable; |
| 219 |
|
| 220 |
----------------- |
| 221 |
-- Abort_Defer -- |
| 222 |
----------------- |
| 223 |
|
| 224 |
procedure Abort_Defer is |
| 225 |
Self_ID : Task_Id; |
| 226 |
begin |
| 227 |
if No_Abort then |
| 228 |
return; |
| 229 |
end if; |
| 230 |
|
| 231 |
Self_ID := STPO.Self; |
| 232 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1; |
| 233 |
end Abort_Defer; |
| 234 |
|
| 235 |
----------------------- |
| 236 |
-- Get_Current_Excep -- |
| 237 |
----------------------- |
| 238 |
|
| 239 |
function Get_Current_Excep return SSL.EOA is |
| 240 |
begin |
| 241 |
return STPO.Self.Common.Compiler_Data.Current_Excep'Access; |
| 242 |
end Get_Current_Excep; |
| 243 |
|
| 244 |
------------------------- |
| 245 |
-- Initialize_RTS_Lock -- |
| 246 |
------------------------- |
| 247 |
|
| 248 |
procedure Initialize_RTS_Lock (Addr : Address) is |
| 249 |
Lock : aliased SOL.RTS_Lock; |
| 250 |
for Lock'Address use Addr; |
| 251 |
pragma Import (Ada, Lock); |
| 252 |
|
| 253 |
begin |
| 254 |
Initialize_Lock (Lock'Unchecked_Access, PO_Level); |
| 255 |
end Initialize_RTS_Lock; |
| 256 |
|
| 257 |
----------------------- |
| 258 |
-- Finalize_RTS_Lock -- |
| 259 |
----------------------- |
| 260 |
|
| 261 |
procedure Finalize_RTS_Lock (Addr : Address) is |
| 262 |
Lock : aliased SOL.RTS_Lock; |
| 263 |
for Lock'Address use Addr; |
| 264 |
pragma Import (Ada, Lock); |
| 265 |
|
| 266 |
begin |
| 267 |
Finalize_Lock (Lock'Unchecked_Access); |
| 268 |
end Finalize_RTS_Lock; |
| 269 |
|
| 270 |
---------------------- |
| 271 |
-- Acquire_RTS_Lock -- |
| 272 |
---------------------- |
| 273 |
|
| 274 |
procedure Acquire_RTS_Lock (Addr : Address) is |
| 275 |
Lock : aliased SOL.RTS_Lock; |
| 276 |
for Lock'Address use Addr; |
| 277 |
pragma Import (Ada, Lock); |
| 278 |
|
| 279 |
begin |
| 280 |
Write_Lock (Lock'Unchecked_Access); |
| 281 |
end Acquire_RTS_Lock; |
| 282 |
|
| 283 |
---------------------- |
| 284 |
-- Release_RTS_Lock -- |
| 285 |
---------------------- |
| 286 |
|
| 287 |
procedure Release_RTS_Lock (Addr : Address) is |
| 288 |
Lock : aliased SOL.RTS_Lock; |
| 289 |
for Lock'Address use Addr; |
| 290 |
pragma Import (Ada, Lock); |
| 291 |
|
| 292 |
begin |
| 293 |
Unlock (Lock'Unchecked_Access); |
| 294 |
end Release_RTS_Lock; |
| 295 |
|
| 296 |
----------------------- |
| 297 |
-- Do_Pending_Action -- |
| 298 |
----------------------- |
| 299 |
|
| 300 |
-- Call only when holding no locks |
| 301 |
|
| 302 |
procedure Do_Pending_Action (Self_ID : Task_Id) is |
| 303 |
|
| 304 |
begin |
| 305 |
pragma Assert (Self_ID = Self and then Self_ID.Deferral_Level = 0); |
| 306 |
|
| 307 |
-- Needs loop to recheck for pending action in case a new one occurred |
| 308 |
-- while we had abort deferred below. |
| 309 |
|
| 310 |
loop |
| 311 |
-- Temporarily defer abort so that we can lock Self_ID |
| 312 |
|
| 313 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level + 1; |
| 314 |
|
| 315 |
Write_Lock (Self_ID); |
| 316 |
Self_ID.Pending_Action := False; |
| 317 |
Unlock (Self_ID); |
| 318 |
|
| 319 |
-- Restore the original Deferral value |
| 320 |
|
| 321 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1; |
| 322 |
|
| 323 |
if not Self_ID.Pending_Action then |
| 324 |
if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level then |
| 325 |
if not Self_ID.Aborting then |
| 326 |
Self_ID.Aborting := True; |
| 327 |
pragma Debug |
| 328 |
(Debug.Trace (Self_ID, "raise Abort_Signal", 'B')); |
| 329 |
raise Standard'Abort_Signal; |
| 330 |
|
| 331 |
pragma Assert (not Self_ID.ATC_Hack); |
| 332 |
|
| 333 |
elsif Self_ID.ATC_Hack then |
| 334 |
|
| 335 |
-- The solution really belongs in the Abort_Signal handler |
| 336 |
-- for async. entry calls. The present hack is very |
| 337 |
-- fragile. It relies that the very next point after |
| 338 |
-- Exit_One_ATC_Level at which the task becomes abortable |
| 339 |
-- will be the call to Undefer_Abort in the |
| 340 |
-- Abort_Signal handler. |
| 341 |
|
| 342 |
Self_ID.ATC_Hack := False; |
| 343 |
|
| 344 |
pragma Debug |
| 345 |
(Debug.Trace |
| 346 |
(Self_ID, "raise Abort_Signal (ATC hack)", 'B')); |
| 347 |
raise Standard'Abort_Signal; |
| 348 |
end if; |
| 349 |
end if; |
| 350 |
|
| 351 |
return; |
| 352 |
end if; |
| 353 |
end loop; |
| 354 |
end Do_Pending_Action; |
| 355 |
|
| 356 |
----------------------- |
| 357 |
-- Final_Task_Unlock -- |
| 358 |
----------------------- |
| 359 |
|
| 360 |
-- This version is only for use in Terminate_Task, when the task is |
| 361 |
-- relinquishing further rights to its own ATCB. |
| 362 |
|
| 363 |
-- There is a very interesting potential race condition there, where the |
| 364 |
-- old task may run concurrently with a new task that is allocated the old |
| 365 |
-- tasks (now reused) ATCB. The critical thing here is to not make any |
| 366 |
-- reference to the ATCB after the lock is released. See also comments on |
| 367 |
-- Terminate_Task and Unlock. |
| 368 |
|
| 369 |
procedure Final_Task_Unlock (Self_ID : Task_Id) is |
| 370 |
begin |
| 371 |
pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting = 1); |
| 372 |
Unlock (Global_Task_Lock'Access); |
| 373 |
end Final_Task_Unlock; |
| 374 |
|
| 375 |
-------------- |
| 376 |
-- Init_RTS -- |
| 377 |
-------------- |
| 378 |
|
| 379 |
procedure Init_RTS is |
| 380 |
Self_Id : Task_Id; |
| 381 |
begin |
| 382 |
Tasking.Initialize; |
| 383 |
|
| 384 |
-- Terminate run time (regular vs restricted) specific initialization |
| 385 |
-- of the environment task. |
| 386 |
|
| 387 |
Self_Id := Environment_Task; |
| 388 |
Self_Id.Master_Of_Task := Environment_Task_Level; |
| 389 |
Self_Id.Master_Within := Self_Id.Master_Of_Task + 1; |
| 390 |
|
| 391 |
for L in Self_Id.Entry_Calls'Range loop |
| 392 |
Self_Id.Entry_Calls (L).Self := Self_Id; |
| 393 |
Self_Id.Entry_Calls (L).Level := L; |
| 394 |
end loop; |
| 395 |
|
| 396 |
Self_Id.Awake_Count := 1; |
| 397 |
Self_Id.Alive_Count := 1; |
| 398 |
|
| 399 |
-- Normally, a task starts out with internal master nesting level one |
| 400 |
-- larger than external master nesting level. It is incremented to one |
| 401 |
-- by Enter_Master, which is called in the task body only if the |
| 402 |
-- compiler thinks the task may have dependent tasks. There is no |
| 403 |
-- corresponding call to Enter_Master for the environment task, so we |
| 404 |
-- would need to increment it to 2 here. Instead, we set it to 3. By |
| 405 |
-- doing this we reserve the level 2 for server tasks of the runtime |
| 406 |
-- system. The environment task does not need to wait for these server |
| 407 |
|
| 408 |
Self_Id.Master_Within := Library_Task_Level; |
| 409 |
|
| 410 |
-- Initialize lock used to implement mutual exclusion between all tasks |
| 411 |
|
| 412 |
Initialize_Lock (Global_Task_Lock'Access, STPO.Global_Task_Level); |
| 413 |
|
| 414 |
-- Notify that the tasking run time has been elaborated so that |
| 415 |
-- the tasking version of the soft links can be used. |
| 416 |
|
| 417 |
if not No_Abort then |
| 418 |
SSL.Abort_Defer := Abort_Defer'Access; |
| 419 |
SSL.Abort_Undefer := Abort_Undefer'Access; |
| 420 |
end if; |
| 421 |
|
| 422 |
SSL.Lock_Task := Task_Lock'Access; |
| 423 |
SSL.Unlock_Task := Task_Unlock'Access; |
| 424 |
SSL.Check_Abort_Status := Check_Abort_Status'Access; |
| 425 |
SSL.Task_Name := Task_Name'Access; |
| 426 |
SSL.Get_Current_Excep := Get_Current_Excep'Access; |
| 427 |
|
| 428 |
-- Initialize the tasking soft links (if not done yet) that are common |
| 429 |
-- to the full and the restricted run times. |
| 430 |
|
| 431 |
SSL.Tasking.Init_Tasking_Soft_Links; |
| 432 |
|
| 433 |
-- Abort is deferred in a new ATCB, so we need to undefer abort at this |
| 434 |
-- stage to make the environment task abortable. |
| 435 |
|
| 436 |
Undefer_Abort (Environment_Task); |
| 437 |
end Init_RTS; |
| 438 |
|
| 439 |
-------------------------------- |
| 440 |
-- Tasking_Runtime_Initialize -- |
| 441 |
-------------------------------- |
| 442 |
|
| 443 |
procedure Tasking_Runtime_Initialize is |
| 444 |
begin |
| 445 |
SSL.Initialize_RTS_Lock := Initialize_RTS_Lock'Access; |
| 446 |
SSL.Finalize_RTS_Lock := Finalize_RTS_Lock'Access; |
| 447 |
SSL.Acquire_RTS_Lock := Acquire_RTS_Lock'Access; |
| 448 |
SSL.Release_RTS_Lock := Release_RTS_Lock'Access; |
| 449 |
end Tasking_Runtime_Initialize; |
| 450 |
|
| 451 |
--------------------------- |
| 452 |
-- Locked_Abort_To_Level-- |
| 453 |
--------------------------- |
| 454 |
|
| 455 |
-- Abort a task to the specified ATC nesting level. |
| 456 |
-- Call this only with T locked. |
| 457 |
|
| 458 |
-- An earlier version of this code contained a call to Wakeup. That should |
| 459 |
-- not be necessary here, if Abort_Task is implemented correctly, since |
| 460 |
-- Abort_Task should include the effect of Wakeup. However, the above call |
| 461 |
-- was in earlier versions of this file, and at least for some targets |
| 462 |
-- Abort_Task has not been doing Wakeup. It should not hurt to uncomment |
| 463 |
-- the above call, until the error is corrected for all targets. |
| 464 |
|
| 465 |
-- See extended comments in package body System.Tasking.Abort for the |
| 466 |
-- overall design of the implementation of task abort. |
| 467 |
-- ??? there is no such package ??? |
| 468 |
|
| 469 |
-- If the task is sleeping it will be in an abort-deferred region, and will |
| 470 |
-- not have Abort_Signal raised by Abort_Task. Such an "abort deferral" is |
| 471 |
-- just to protect the RTS internals, and not necessarily required to |
| 472 |
-- enforce Ada semantics. Abort_Task should wake the task up and let it |
| 473 |
-- decide if it wants to complete the aborted construct immediately. |
| 474 |
|
| 475 |
-- Note that the effect of the low-level Abort_Task is not persistent. |
| 476 |
-- If the target task is not blocked, this wakeup will be missed. |
| 477 |
|
| 478 |
-- We don't bother calling Abort_Task if this task is aborting itself, |
| 479 |
-- since we are inside the RTS and have abort deferred. Similarly, We don't |
| 480 |
-- bother to call Abort_Task if T is terminated, since there is no need to |
| 481 |
-- abort a terminated task, and it could be dangerous to try if the task |
| 482 |
-- has stopped executing. |
| 483 |
|
| 484 |
-- Note that an earlier version of this code had some false reasoning about |
| 485 |
-- being able to reliably wake up a task that had suspended on a blocking |
| 486 |
-- system call that does not atomically release the task's lock (e.g., UNIX |
| 487 |
-- nanosleep, which we once thought could be used to implement delays). |
| 488 |
-- That still left the possibility of missed wakeups. |
| 489 |
|
| 490 |
-- We cannot safely call Vulnerable_Complete_Activation here, since that |
| 491 |
-- requires locking Self_ID.Parent. The anti-deadlock lock ordering rules |
| 492 |
-- would then require us to release the lock on Self_ID first, which would |
| 493 |
-- create a timing window for other tasks to lock Self_ID. This is |
| 494 |
-- significant for tasks that may be aborted before their execution can |
| 495 |
-- enter the task body, and so they do not get a chance to call |
| 496 |
-- Complete_Task. The actual work for this case is done in Terminate_Task. |
| 497 |
|
| 498 |
procedure Locked_Abort_To_Level |
| 499 |
(Self_ID : Task_Id; |
| 500 |
T : Task_Id; |
| 501 |
L : ATC_Level_Base) |
| 502 |
is |
| 503 |
begin |
| 504 |
if not T.Aborting and then T /= Self_ID then |
| 505 |
case T.Common.State is |
| 506 |
when Terminated |
| 507 |
| Unactivated |
| 508 |
=> |
| 509 |
pragma Assert (Standard.False); |
| 510 |
null; |
| 511 |
|
| 512 |
when Activating |
| 513 |
| Runnable |
| 514 |
=> |
| 515 |
if T.ATC_Nesting_Level > Level_No_ATC_Occurring then |
| 516 |
-- This scenario occurs when an asynchronous protected entry |
| 517 |
-- call is canceled during a requeue with abort. |
| 518 |
|
| 519 |
T.Entry_Calls |
| 520 |
(T.ATC_Nesting_Level).Cancellation_Attempted := True; |
| 521 |
end if; |
| 522 |
|
| 523 |
when Interrupt_Server_Blocked_On_Event_Flag => |
| 524 |
null; |
| 525 |
|
| 526 |
when AST_Server_Sleep |
| 527 |
| Async_Select_Sleep |
| 528 |
| Delay_Sleep |
| 529 |
| Interrupt_Server_Blocked_Interrupt_Sleep |
| 530 |
| Interrupt_Server_Idle_Sleep |
| 531 |
| Timer_Server_Sleep |
| 532 |
=> |
| 533 |
Wakeup (T, T.Common.State); |
| 534 |
|
| 535 |
when Acceptor_Delay_Sleep |
| 536 |
| Acceptor_Sleep |
| 537 |
=> |
| 538 |
T.Open_Accepts := null; |
| 539 |
Wakeup (T, T.Common.State); |
| 540 |
|
| 541 |
when Entry_Caller_Sleep => |
| 542 |
pragma Assert (T.ATC_Nesting_Level > Level_No_ATC_Occurring); |
| 543 |
|
| 544 |
T.Entry_Calls |
| 545 |
(T.ATC_Nesting_Level).Cancellation_Attempted := True; |
| 546 |
Wakeup (T, T.Common.State); |
| 547 |
|
| 548 |
when Activator_Sleep |
| 549 |
| Asynchronous_Hold |
| 550 |
| Master_Completion_Sleep |
| 551 |
| Master_Phase_2_Sleep |
| 552 |
=> |
| 553 |
null; |
| 554 |
end case; |
| 555 |
end if; |
| 556 |
|
| 557 |
if T.Pending_ATC_Level > L then |
| 558 |
T.Pending_ATC_Level := L; |
| 559 |
T.Pending_Action := True; |
| 560 |
|
| 561 |
if L = Level_Completed_Task then |
| 562 |
T.Callable := False; |
| 563 |
end if; |
| 564 |
|
| 565 |
-- This prevents aborted task from accepting calls |
| 566 |
|
| 567 |
if T.Aborting then |
| 568 |
|
| 569 |
-- The test above is just a heuristic, to reduce wasteful |
| 570 |
-- calls to Abort_Task. We are holding T locked, and this |
| 571 |
-- value will not be set to False except with T also locked, |
| 572 |
-- inside Exit_One_ATC_Level, so we should not miss wakeups. |
| 573 |
|
| 574 |
if T.Common.State = Acceptor_Sleep |
| 575 |
or else |
| 576 |
T.Common.State = Acceptor_Delay_Sleep |
| 577 |
then |
| 578 |
T.Open_Accepts := null; |
| 579 |
end if; |
| 580 |
|
| 581 |
elsif T /= Self_ID and then |
| 582 |
(T.Common.State = Runnable |
| 583 |
or else T.Common.State = Interrupt_Server_Blocked_On_Event_Flag) |
| 584 |
|
| 585 |
-- The task is blocked on a system call waiting for the |
| 586 |
-- completion event. In this case Abort_Task may need to take |
| 587 |
-- special action in order to succeed. |
| 588 |
|
| 589 |
then |
| 590 |
Abort_Task (T); |
| 591 |
end if; |
| 592 |
end if; |
| 593 |
end Locked_Abort_To_Level; |
| 594 |
|
| 595 |
-------------------------------- |
| 596 |
-- Remove_From_All_Tasks_List -- |
| 597 |
-------------------------------- |
| 598 |
|
| 599 |
procedure Remove_From_All_Tasks_List (T : Task_Id) is |
| 600 |
C : Task_Id; |
| 601 |
Previous : Task_Id; |
| 602 |
|
| 603 |
begin |
| 604 |
pragma Debug |
| 605 |
(Debug.Trace (Self, "Remove_From_All_Tasks_List", 'C')); |
| 606 |
|
| 607 |
Previous := Null_Task; |
| 608 |
C := All_Tasks_List; |
| 609 |
while C /= Null_Task loop |
| 610 |
if C = T then |
| 611 |
if Previous = Null_Task then |
| 612 |
All_Tasks_List := All_Tasks_List.Common.All_Tasks_Link; |
| 613 |
else |
| 614 |
Previous.Common.All_Tasks_Link := C.Common.All_Tasks_Link; |
| 615 |
end if; |
| 616 |
|
| 617 |
return; |
| 618 |
end if; |
| 619 |
|
| 620 |
Previous := C; |
| 621 |
C := C.Common.All_Tasks_Link; |
| 622 |
end loop; |
| 623 |
|
| 624 |
pragma Assert (Standard.False); |
| 625 |
end Remove_From_All_Tasks_List; |
| 626 |
|
| 627 |
--------------- |
| 628 |
-- Task_Lock -- |
| 629 |
--------------- |
| 630 |
|
| 631 |
procedure Task_Lock (Self_ID : Task_Id) is |
| 632 |
begin |
| 633 |
Self_ID.Common.Global_Task_Lock_Nesting := |
| 634 |
Self_ID.Common.Global_Task_Lock_Nesting + 1; |
| 635 |
|
| 636 |
if Self_ID.Common.Global_Task_Lock_Nesting = 1 then |
| 637 |
Defer_Abort_Nestable (Self_ID); |
| 638 |
Write_Lock (Global_Task_Lock'Access); |
| 639 |
end if; |
| 640 |
end Task_Lock; |
| 641 |
|
| 642 |
procedure Task_Lock is |
| 643 |
begin |
| 644 |
Task_Lock (STPO.Self); |
| 645 |
end Task_Lock; |
| 646 |
|
| 647 |
--------------- |
| 648 |
-- Task_Name -- |
| 649 |
--------------- |
| 650 |
|
| 651 |
function Task_Name return String is |
| 652 |
Self_Id : constant Task_Id := STPO.Self; |
| 653 |
begin |
| 654 |
return Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len); |
| 655 |
end Task_Name; |
| 656 |
|
| 657 |
----------------- |
| 658 |
-- Task_Unlock -- |
| 659 |
----------------- |
| 660 |
|
| 661 |
procedure Task_Unlock (Self_ID : Task_Id) is |
| 662 |
begin |
| 663 |
pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting > 0); |
| 664 |
Self_ID.Common.Global_Task_Lock_Nesting := |
| 665 |
Self_ID.Common.Global_Task_Lock_Nesting - 1; |
| 666 |
|
| 667 |
if Self_ID.Common.Global_Task_Lock_Nesting = 0 then |
| 668 |
Unlock (Global_Task_Lock'Access); |
| 669 |
Undefer_Abort_Nestable (Self_ID); |
| 670 |
end if; |
| 671 |
end Task_Unlock; |
| 672 |
|
| 673 |
procedure Task_Unlock is |
| 674 |
begin |
| 675 |
Task_Unlock (STPO.Self); |
| 676 |
end Task_Unlock; |
| 677 |
|
| 678 |
------------------- |
| 679 |
-- Undefer_Abort -- |
| 680 |
------------------- |
| 681 |
|
| 682 |
-- Precondition : Self does not hold any locks |
| 683 |
|
| 684 |
-- Undefer_Abort is called on any abort completion point (aka. |
| 685 |
-- synchronization point). It performs the following actions if they |
| 686 |
-- are pending: (1) change the base priority, (2) abort the task. |
| 687 |
|
| 688 |
-- The priority change has to occur before abort. Otherwise, it would |
| 689 |
-- take effect no earlier than the next abort completion point. |
| 690 |
|
| 691 |
procedure Undefer_Abort (Self_ID : Task_Id) is |
| 692 |
begin |
| 693 |
if No_Abort then |
| 694 |
return; |
| 695 |
end if; |
| 696 |
|
| 697 |
pragma Assert (Self_ID.Deferral_Level = 1); |
| 698 |
|
| 699 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1; |
| 700 |
|
| 701 |
if Self_ID.Deferral_Level = 0 then |
| 702 |
pragma Assert (Check_No_Locks (Self_ID)); |
| 703 |
|
| 704 |
if Self_ID.Pending_Action then |
| 705 |
Do_Pending_Action (Self_ID); |
| 706 |
end if; |
| 707 |
end if; |
| 708 |
end Undefer_Abort; |
| 709 |
|
| 710 |
---------------------------- |
| 711 |
-- Undefer_Abort_Nestable -- |
| 712 |
---------------------------- |
| 713 |
|
| 714 |
-- An earlier version would re-defer abort if an abort is in progress. |
| 715 |
-- Then, we modified the effect of the raise statement so that it defers |
| 716 |
-- abort until control reaches a handler. That was done to prevent |
| 717 |
-- "skipping over" a handler if another asynchronous abort occurs during |
| 718 |
-- the propagation of the abort to the handler. |
| 719 |
|
| 720 |
-- There has been talk of reversing that decision, based on a newer |
| 721 |
-- implementation of exception propagation. Care must be taken to evaluate |
| 722 |
-- how such a change would interact with the above code and all the places |
| 723 |
-- where abort-deferral is used to bridge over critical transitions, such |
| 724 |
-- as entry to the scope of a region with a finalizer and entry into the |
| 725 |
-- body of an accept-procedure. |
| 726 |
|
| 727 |
procedure Undefer_Abort_Nestable (Self_ID : Task_Id) is |
| 728 |
begin |
| 729 |
if No_Abort then |
| 730 |
return; |
| 731 |
end if; |
| 732 |
|
| 733 |
pragma Assert (Self_ID.Deferral_Level > 0); |
| 734 |
|
| 735 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1; |
| 736 |
|
| 737 |
if Self_ID.Deferral_Level = 0 then |
| 738 |
|
| 739 |
pragma Assert (Check_No_Locks (Self_ID)); |
| 740 |
|
| 741 |
if Self_ID.Pending_Action then |
| 742 |
Do_Pending_Action (Self_ID); |
| 743 |
end if; |
| 744 |
end if; |
| 745 |
end Undefer_Abort_Nestable; |
| 746 |
|
| 747 |
------------------- |
| 748 |
-- Abort_Undefer -- |
| 749 |
------------------- |
| 750 |
|
| 751 |
procedure Abort_Undefer is |
| 752 |
Self_ID : Task_Id; |
| 753 |
begin |
| 754 |
if No_Abort then |
| 755 |
return; |
| 756 |
end if; |
| 757 |
|
| 758 |
Self_ID := STPO.Self; |
| 759 |
|
| 760 |
if Self_ID.Deferral_Level = 0 then |
| 761 |
|
| 762 |
-- In case there are different views on whether Abort is supported |
| 763 |
-- between the expander and the run time, we may end up with |
| 764 |
-- Self_ID.Deferral_Level being equal to zero, when called from |
| 765 |
-- the procedure created by the expander that corresponds to a |
| 766 |
-- task body. In this case, there's nothing to be done. |
| 767 |
|
| 768 |
-- See related code in System.Tasking.Stages.Create_Task resetting |
| 769 |
-- Deferral_Level when System.Restrictions.Abort_Allowed is False. |
| 770 |
|
| 771 |
return; |
| 772 |
end if; |
| 773 |
|
| 774 |
pragma Assert (Self_ID.Deferral_Level > 0); |
| 775 |
Self_ID.Deferral_Level := Self_ID.Deferral_Level - 1; |
| 776 |
|
| 777 |
if Self_ID.Deferral_Level = 0 then |
| 778 |
pragma Assert (Check_No_Locks (Self_ID)); |
| 779 |
|
| 780 |
if Self_ID.Pending_Action then |
| 781 |
Do_Pending_Action (Self_ID); |
| 782 |
end if; |
| 783 |
end if; |
| 784 |
end Abort_Undefer; |
| 785 |
|
| 786 |
-------------------------- |
| 787 |
-- Wakeup_Entry_Caller -- |
| 788 |
-------------------------- |
| 789 |
|
| 790 |
-- This is called at the end of service of an entry call, to abort the |
| 791 |
-- caller if he is in an abortable part, and to wake up the caller if it |
| 792 |
-- is on Entry_Caller_Sleep. It assumes that the call is already off-queue. |
| 793 |
|
| 794 |
-- (This enforces the rule that a task must be off-queue if its state is |
| 795 |
-- Done or Cancelled.) Call it holding the lock of Entry_Call.Self. |
| 796 |
|
| 797 |
-- Timed_Call or Simple_Call: |
| 798 |
-- The caller is waiting on Entry_Caller_Sleep, in |
| 799 |
-- Wait_For_Completion, or Wait_For_Completion_With_Timeout. |
| 800 |
|
| 801 |
-- Conditional_Call: |
| 802 |
-- The caller might be in Wait_For_Completion, |
| 803 |
-- waiting for a rendezvous (possibly requeued without abort) |
| 804 |
-- to complete. |
| 805 |
|
| 806 |
-- Asynchronous_Call: |
| 807 |
-- The caller may be executing in the abortable part o |
| 808 |
-- an async. select, or on a time delay, |
| 809 |
-- if Entry_Call.State >= Was_Abortable. |
| 810 |
|
| 811 |
procedure Wakeup_Entry_Caller |
| 812 |
(Self_ID : Task_Id; |
| 813 |
Entry_Call : Entry_Call_Link; |
| 814 |
New_State : Entry_Call_State) |
| 815 |
is |
| 816 |
Caller : constant Task_Id := Entry_Call.Self; |
| 817 |
|
| 818 |
begin |
| 819 |
pragma Debug (Debug.Trace |
| 820 |
(Self_ID, "Wakeup_Entry_Caller", 'E', Caller)); |
| 821 |
pragma Assert (New_State = Done or else New_State = Cancelled); |
| 822 |
|
| 823 |
pragma Assert (Caller.Common.State /= Unactivated); |
| 824 |
|
| 825 |
Entry_Call.State := New_State; |
| 826 |
|
| 827 |
if Entry_Call.Mode = Asynchronous_Call then |
| 828 |
|
| 829 |
-- Abort the caller in his abortable part, but do so only if call has |
| 830 |
-- been queued abortably. |
| 831 |
|
| 832 |
if Entry_Call.State >= Was_Abortable or else New_State = Done then |
| 833 |
Locked_Abort_To_Level (Self_ID, Caller, Entry_Call.Level - 1); |
| 834 |
end if; |
| 835 |
|
| 836 |
elsif Caller.Common.State = Entry_Caller_Sleep then |
| 837 |
Wakeup (Caller, Entry_Caller_Sleep); |
| 838 |
end if; |
| 839 |
end Wakeup_Entry_Caller; |
| 840 |
|
| 841 |
------------------------- |
| 842 |
-- Finalize_Attributes -- |
| 843 |
------------------------- |
| 844 |
|
| 845 |
procedure Finalize_Attributes (T : Task_Id) is |
| 846 |
Attr : System.Address; |
| 847 |
|
| 848 |
begin |
| 849 |
for J in T.Attributes'Range loop |
| 850 |
Attr := T.Attributes (J); |
| 851 |
|
| 852 |
if Attr /= 0 and then Task_Attributes.Require_Finalization (J) then |
| 853 |
Task_Attributes.To_Attribute (Attr).Free (Attr); |
| 854 |
T.Attributes (J) := 0; |
| 855 |
end if; |
| 856 |
end loop; |
| 857 |
end Finalize_Attributes; |
| 858 |
|
| 859 |
begin |
| 860 |
Init_RTS; |
| 861 |
end System.Tasking.Initialization; |