1------------------------------------------------------------------------------ 2-- -- 3-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- 4-- -- 5-- S Y S T E M . T A S K I N G -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 1992-2012, Free Software Foundation, Inc. -- 10-- -- 11-- GNARL is free software; you can redistribute it and/or modify it under -- 12-- terms of the GNU General Public License as published by the Free Soft- -- 13-- ware Foundation; either version 3, or (at your option) any later ver- -- 14-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- 15-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- 16-- or FITNESS FOR A PARTICULAR PURPOSE. -- 17-- -- 18-- As a special exception under Section 7 of GPL version 3, you are granted -- 19-- additional permissions described in the GCC Runtime Library Exception, -- 20-- version 3.1, as published by the Free Software Foundation. -- 21-- -- 22-- You should have received a copy of the GNU General Public License and -- 23-- a copy of the GCC Runtime Library Exception along with this program; -- 24-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- 25-- <http://www.gnu.org/licenses/>. -- 26-- -- 27-- GNARL was developed by the GNARL team at Florida State University. -- 28-- Extensive contributions were provided by Ada Core Technologies, Inc. -- 29-- -- 30------------------------------------------------------------------------------ 31 32-- This package provides necessary type definitions for compiler interface 33 34-- Note: the compiler generates direct calls to this interface, via Rtsfind. 35-- Any changes to this interface may require corresponding compiler changes. 36 37with Ada.Exceptions; 38with Ada.Unchecked_Conversion; 39 40with System.Parameters; 41with System.Task_Info; 42with System.Soft_Links; 43with System.Task_Primitives; 44with System.Stack_Usage; 45with System.Multiprocessors; 46 47package System.Tasking is 48 pragma Preelaborate; 49 50 ------------------- 51 -- Locking Rules -- 52 ------------------- 53 54 -- The following rules must be followed at all times, to prevent 55 -- deadlock and generally ensure correct operation of locking. 56 57 -- Never lock a lock unless abort is deferred 58 59 -- Never undefer abort while holding a lock 60 61 -- Overlapping critical sections must be properly nested, and locks must 62 -- be released in LIFO order. E.g., the following is not allowed: 63 64 -- Lock (X); 65 -- ... 66 -- Lock (Y); 67 -- ... 68 -- Unlock (X); 69 -- ... 70 -- Unlock (Y); 71 72 -- Locks with lower (smaller) level number cannot be locked 73 -- while holding a lock with a higher level number. (The level 74 75 -- 1. System.Tasking.PO_Simple.Protection.L (any PO lock) 76 -- 2. System.Tasking.Initialization.Global_Task_Lock (in body) 77 -- 3. System.Task_Primitives.Operations.Single_RTS_Lock 78 -- 4. System.Tasking.Ada_Task_Control_Block.LL.L (any TCB lock) 79 80 -- Clearly, there can be no circular chain of hold-and-wait 81 -- relationships involving locks in different ordering levels. 82 83 -- We used to have Global_Task_Lock before Protection.L but this was 84 -- clearly wrong since there can be calls to "new" inside protected 85 -- operations. The new ordering prevents these failures. 86 87 -- Sometimes we need to hold two ATCB locks at the same time. To allow us 88 -- to order the locking, each ATCB is given a unique serial number. If one 89 -- needs to hold locks on several ATCBs at once, the locks with lower 90 -- serial numbers must be locked first. 91 92 -- We don't always need to check the serial numbers, since the serial 93 -- numbers are assigned sequentially, and so: 94 95 -- . The parent of a task always has a lower serial number. 96 -- . The activator of a task always has a lower serial number. 97 -- . The environment task has a lower serial number than any other task. 98 -- . If the activator of a task is different from the task's parent, 99 -- the parent always has a lower serial number than the activator. 100 101 --------------------------------- 102 -- Task_Id related definitions -- 103 --------------------------------- 104 105 type Ada_Task_Control_Block; 106 107 type Task_Id is access all Ada_Task_Control_Block; 108 for Task_Id'Size use System.Task_Primitives.Task_Address_Size; 109 110 Null_Task : constant Task_Id; 111 112 type Task_List is array (Positive range <>) of Task_Id; 113 114 function Self return Task_Id; 115 pragma Inline (Self); 116 -- This is the compiler interface version of this function. Do not call 117 -- from the run-time system. 118 119 function To_Task_Id is 120 new Ada.Unchecked_Conversion 121 (System.Task_Primitives.Task_Address, Task_Id); 122 function To_Address is 123 new Ada.Unchecked_Conversion 124 (Task_Id, System.Task_Primitives.Task_Address); 125 126 ----------------------- 127 -- Enumeration types -- 128 ----------------------- 129 130 type Task_States is 131 (Unactivated, 132 -- TCB initialized but not task has not been created. 133 -- It cannot be executing. 134 135-- Activating, 136-- -- ??? Temporarily at end of list for GDB compatibility 137-- -- Task has been created and is being made Runnable. 138 139 -- Active states 140 -- For all states from here down, the task has been activated. 141 -- For all states from here down, except for Terminated, the task 142 -- may be executing. 143 -- Activator = null iff it has not yet completed activating. 144 145 Runnable, 146 -- Task is not blocked for any reason known to Ada. 147 -- (It may be waiting for a mutex, though.) 148 -- It is conceptually "executing" in normal mode. 149 150 Terminated, 151 -- The task is terminated, in the sense of ARM 9.3 (5). 152 -- Any dependents that were waiting on terminate 153 -- alternatives have been awakened and have terminated themselves. 154 155 Activator_Sleep, 156 -- Task is waiting for created tasks to complete activation 157 158 Acceptor_Sleep, 159 -- Task is waiting on an accept or select with terminate 160 161-- Acceptor_Delay_Sleep, 162-- -- ??? Temporarily at end of list for GDB compatibility 163-- -- Task is waiting on an selective wait statement 164 165 Entry_Caller_Sleep, 166 -- Task is waiting on an entry call 167 168 Async_Select_Sleep, 169 -- Task is waiting to start the abortable part of an 170 -- asynchronous select statement. 171 172 Delay_Sleep, 173 -- Task is waiting on a select statement with only a delay 174 -- alternative open. 175 176 Master_Completion_Sleep, 177 -- Master completion has two phases. 178 -- In Phase 1 the task is sleeping in Complete_Master 179 -- having completed a master within itself, 180 -- and is waiting for the tasks dependent on that master to become 181 -- terminated or waiting on a terminate Phase. 182 183 Master_Phase_2_Sleep, 184 -- In Phase 2 the task is sleeping in Complete_Master 185 -- waiting for tasks on terminate alternatives to finish 186 -- terminating. 187 188 -- The following are special uses of sleep, for server tasks 189 -- within the run-time system. 190 191 Interrupt_Server_Idle_Sleep, 192 Interrupt_Server_Blocked_Interrupt_Sleep, 193 Timer_Server_Sleep, 194 AST_Server_Sleep, 195 196 Asynchronous_Hold, 197 -- The task has been held by Asynchronous_Task_Control.Hold_Task 198 199 Interrupt_Server_Blocked_On_Event_Flag, 200 -- The task has been blocked on a system call waiting for a 201 -- completion event/signal to occur. 202 203 Activating, 204 -- Task has been created and is being made Runnable 205 206 Acceptor_Delay_Sleep 207 -- Task is waiting on an selective wait statement 208 ); 209 210 type Call_Modes is 211 (Simple_Call, Conditional_Call, Asynchronous_Call, Timed_Call); 212 213 type Select_Modes is (Simple_Mode, Else_Mode, Terminate_Mode, Delay_Mode); 214 215 subtype Delay_Modes is Integer; 216 217 ------------------------------- 218 -- Entry related definitions -- 219 ------------------------------- 220 221 Null_Entry : constant := 0; 222 223 Max_Entry : constant := Integer'Last; 224 225 Interrupt_Entry : constant := -2; 226 227 Cancelled_Entry : constant := -1; 228 229 type Entry_Index is range Interrupt_Entry .. Max_Entry; 230 231 Null_Task_Entry : constant := Null_Entry; 232 233 Max_Task_Entry : constant := Max_Entry; 234 235 type Task_Entry_Index is new Entry_Index 236 range Null_Task_Entry .. Max_Task_Entry; 237 238 type Entry_Call_Record; 239 240 type Entry_Call_Link is access all Entry_Call_Record; 241 242 type Entry_Queue is record 243 Head : Entry_Call_Link; 244 Tail : Entry_Call_Link; 245 end record; 246 247 type Task_Entry_Queue_Array is 248 array (Task_Entry_Index range <>) of Entry_Queue; 249 250 -- A data structure which contains the string names of entries and entry 251 -- family members. 252 253 type String_Access is access all String; 254 255 type Task_Entry_Names_Array is 256 array (Entry_Index range <>) of String_Access; 257 258 type Task_Entry_Names_Access is access all Task_Entry_Names_Array; 259 260 ---------------------------------- 261 -- Entry_Call_Record definition -- 262 ---------------------------------- 263 264 type Entry_Call_State is 265 (Never_Abortable, 266 -- the call is not abortable, and never can be 267 268 Not_Yet_Abortable, 269 -- the call is not abortable, but may become so 270 271 Was_Abortable, 272 -- the call is not abortable, but once was 273 274 Now_Abortable, 275 -- the call is abortable 276 277 Done, 278 -- the call has been completed 279 280 Cancelled 281 -- the call was asynchronous, and was cancelled 282 ); 283 pragma Ordered (Entry_Call_State); 284 285 -- Never_Abortable is used for calls that are made in a abort deferred 286 -- region (see ARM 9.8(5-11), 9.8 (20)). Such a call is never abortable. 287 288 -- The Was_ vs. Not_Yet_ distinction is needed to decide whether it is OK 289 -- to advance into the abortable part of an async. select stmt. That is 290 -- allowed iff the mode is Now_ or Was_. 291 292 -- Done indicates the call has been completed, without cancellation, or no 293 -- call has been made yet at this ATC nesting level, and so aborting the 294 -- call is no longer an issue. Completion of the call does not necessarily 295 -- indicate "success"; the call may be returning an exception if 296 -- Exception_To_Raise is non-null. 297 298 -- Cancelled indicates the call was cancelled, and so aborting the call is 299 -- no longer an issue. 300 301 -- The call is on an entry queue unless State >= Done, in which case it may 302 -- or may not be still Onqueue. 303 304 -- Please do not modify the order of the values, without checking all uses 305 -- of this type. We rely on partial "monotonicity" of 306 -- Entry_Call_Record.State to avoid locking when we access this value for 307 -- certain tests. In particular: 308 309 -- 1) Once State >= Done, we can rely that the call has been 310 -- completed. If State >= Done, it will not 311 -- change until the task does another entry call at this level. 312 313 -- 2) Once State >= Was_Abortable, we can rely that the call has 314 -- been queued abortably at least once, and so the check for 315 -- whether it is OK to advance to the abortable part of an 316 -- async. select statement does not need to lock anything. 317 318 type Restricted_Entry_Call_Record is record 319 Self : Task_Id; 320 -- ID of the caller 321 322 Mode : Call_Modes; 323 324 State : Entry_Call_State; 325 pragma Atomic (State); 326 -- Indicates part of the state of the call. 327 -- 328 -- Protection: If the call is not on a queue, it should only be 329 -- accessed by Self, and Self does not need any lock to modify this 330 -- field. 331 -- 332 -- Once the call is on a queue, the value should be something other 333 -- than Done unless it is cancelled, and access is controller by the 334 -- "server" of the queue -- i.e., the lock of Checked_To_Protection 335 -- (Call_Target) if the call record is on the queue of a PO, or the 336 -- lock of Called_Target if the call is on the queue of a task. See 337 -- comments on type declaration for more details. 338 339 Uninterpreted_Data : System.Address; 340 -- Data passed by the compiler 341 342 Exception_To_Raise : Ada.Exceptions.Exception_Id; 343 -- The exception to raise once this call has been completed without 344 -- being aborted. 345 end record; 346 pragma Suppress_Initialization (Restricted_Entry_Call_Record); 347 348 ------------------------------------------- 349 -- Task termination procedure definition -- 350 ------------------------------------------- 351 352 -- We need to redefine here these types (already defined in 353 -- Ada.Task_Termination) for avoiding circular dependencies. 354 355 type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception); 356 -- Possible causes for task termination: 357 -- 358 -- Normal means that the task terminates due to completing the 359 -- last sentence of its body, or as a result of waiting on a 360 -- terminate alternative. 361 362 -- Abnormal means that the task terminates because it is being aborted 363 364 -- handled_Exception means that the task terminates because of exception 365 -- raised by the execution of its task_body. 366 367 type Termination_Handler is access protected procedure 368 (Cause : Cause_Of_Termination; 369 T : Task_Id; 370 X : Ada.Exceptions.Exception_Occurrence); 371 -- Used to represent protected procedures to be executed when task 372 -- terminates. 373 374 ------------------------------------ 375 -- Dispatching domain definitions -- 376 ------------------------------------ 377 378 -- We need to redefine here these types (already defined in 379 -- System.Multiprocessor.Dispatching_Domains) for avoiding circular 380 -- dependencies. 381 382 type Dispatching_Domain is 383 array (System.Multiprocessors.CPU range <>) of Boolean; 384 -- A dispatching domain needs to contain the set of processors belonging 385 -- to it. This is a processor mask where a True indicates that the 386 -- processor belongs to the dispatching domain. 387 -- Do not use the full range of CPU_Range because it would create a very 388 -- long array. This way we can use the exact range of processors available 389 -- in the system. 390 391 type Dispatching_Domain_Access is access Dispatching_Domain; 392 393 System_Domain : Dispatching_Domain_Access; 394 -- All processors belong to default system dispatching domain at start up. 395 -- We use a pointer which creates the actual variable for the reasons 396 -- explained bellow in Dispatching_Domain_Tasks. 397 398 Dispatching_Domains_Frozen : Boolean := False; 399 -- True when the main procedure has been called. Hence, no new dispatching 400 -- domains can be created when this flag is True. 401 402 type Array_Allocated_Tasks is 403 array (System.Multiprocessors.CPU range <>) of Natural; 404 -- At start-up time, we need to store the number of tasks attached to 405 -- concrete processors within the system domain (we can only create 406 -- dispatching domains with processors belonging to the system domain and 407 -- without tasks allocated). 408 409 type Array_Allocated_Tasks_Access is access Array_Allocated_Tasks; 410 411 Dispatching_Domain_Tasks : Array_Allocated_Tasks_Access; 412 -- We need to store whether there are tasks allocated to concrete 413 -- processors in the default system dispatching domain because we need to 414 -- check it before creating a new dispatching domain. Two comments about 415 -- why we use a pointer here and not in package Dispatching_Domains: 416 -- 417 -- 1) We use an array created dynamically in procedure Initialize which 418 -- is called at the beginning of the initialization of the run-time 419 -- library. Declaring a static array here in the spec would not work 420 -- across different installations because it would get the value of 421 -- Number_Of_CPUs from the machine where the run-time library is built, 422 -- and not from the machine where the application is executed. That is 423 -- the reason why we create the array (CPU'First .. Number_Of_CPUs) at 424 -- execution time in the procedure body, ensuring that the function 425 -- Number_Of_CPUs is executed at execution time (the same trick as we 426 -- use for System_Domain). 427 -- 428 -- 2) We have moved this declaration from package Dispatching_Domains 429 -- because when we use a pragma CPU, the affinity is passed through the 430 -- call to Create_Task. Hence, at this point, we may need to update the 431 -- number of tasks associated to the processor, but we do not want to 432 -- force a dependency from this package on Dispatching_Domains. 433 434 ------------------------------------ 435 -- Task related other definitions -- 436 ------------------------------------ 437 438 type Activation_Chain is limited private; 439 -- Linked list of to-be-activated tasks, linked through 440 -- Activation_Link. The order of tasks on the list is irrelevant, because 441 -- the priority rules will ensure that they actually start activating in 442 -- priority order. 443 444 type Activation_Chain_Access is access all Activation_Chain; 445 446 type Task_Procedure_Access is access procedure (Arg : System.Address); 447 448 type Access_Boolean is access all Boolean; 449 450 function Detect_Blocking return Boolean; 451 pragma Inline (Detect_Blocking); 452 -- Return whether the Detect_Blocking pragma is enabled 453 454 function Storage_Size (T : Task_Id) return System.Parameters.Size_Type; 455 -- Retrieve from the TCB of the task the allocated size of its stack, 456 -- either the system default or the size specified by a pragma. This is in 457 -- general a non-static value that can depend on discriminants of the task. 458 459 type Bit_Array is array (Integer range <>) of Boolean; 460 pragma Pack (Bit_Array); 461 462 subtype Debug_Event_Array is Bit_Array (1 .. 16); 463 464 Global_Task_Debug_Event_Set : Boolean := False; 465 -- Set True when running under debugger control and a task debug event 466 -- signal has been requested. 467 468 ---------------------------------------------- 469 -- Ada_Task_Control_Block (ATCB) definition -- 470 ---------------------------------------------- 471 472 -- Notes on protection (synchronization) of TRTS data structures 473 474 -- Any field of the TCB can be written by the activator of a task when the 475 -- task is created, since no other task can access the new task's 476 -- state until creation is complete. 477 478 -- The protection for each field is described in a comment starting with 479 -- "Protection:". 480 481 -- When a lock is used to protect an ATCB field, this lock is simply named 482 483 -- Some protection is described in terms of tasks related to the 484 -- ATCB being protected. These are: 485 486 -- Self: The task which is controlled by this ATCB 487 -- Acceptor: A task accepting a call from Self 488 -- Caller: A task calling an entry of Self 489 -- Parent: The task executing the master on which Self depends 490 -- Dependent: A task dependent on Self 491 -- Activator: The task that created Self and initiated its activation 492 -- Created: A task created and activated by Self 493 494 -- Note: The order of the fields is important to implement efficiently 495 -- tasking support under gdb. 496 -- Currently gdb relies on the order of the State, Parent, Base_Priority, 497 -- Task_Image, Task_Image_Len, Call and LL fields. 498 499 ------------------------- 500 -- Common ATCB section -- 501 ------------------------- 502 503 -- Section used by all GNARL implementations (regular and restricted) 504 505 type Common_ATCB is record 506 State : Task_States; 507 pragma Atomic (State); 508 -- Encodes some basic information about the state of a task, 509 -- including whether it has been activated, whether it is sleeping, 510 -- and whether it is terminated. 511 -- 512 -- Protection: Self.L 513 514 Parent : Task_Id; 515 -- The task on which this task depends. 516 -- See also Master_Level and Master_Within. 517 518 Base_Priority : System.Any_Priority; 519 -- Base priority, not changed during entry calls, only changed 520 -- via dynamic priorities package. 521 -- 522 -- Protection: Only written by Self, accessed by anyone 523 524 Base_CPU : System.Multiprocessors.CPU_Range; 525 -- Base CPU, only changed via dispatching domains package. 526 -- 527 -- Protection: Self.L 528 529 Current_Priority : System.Any_Priority; 530 -- Active priority, except that the effects of protected object 531 -- priority ceilings are not reflected. This only reflects explicit 532 -- priority changes and priority inherited through task activation 533 -- and rendezvous. 534 -- 535 -- Ada 95 notes: In Ada 95, this field will be transferred to the 536 -- Priority field of an Entry_Calls component when an entry call is 537 -- initiated. The Priority of the Entry_Calls component will not change 538 -- for the duration of the call. The accepting task can use it to boost 539 -- its own priority without fear of its changing in the meantime. 540 -- 541 -- This can safely be used in the priority ordering of entry queues. 542 -- Once a call is queued, its priority does not change. 543 -- 544 -- Since an entry call cannot be made while executing a protected 545 -- action, the priority of a task will never reflect a priority ceiling 546 -- change at the point of an entry call. 547 -- 548 -- Protection: Only written by Self, and only accessed when Acceptor 549 -- accepts an entry or when Created activates, at which points Self is 550 -- suspended. 551 552 Protected_Action_Nesting : Natural; 553 pragma Atomic (Protected_Action_Nesting); 554 -- The dynamic level of protected action nesting for this task. This 555 -- field is needed for checking whether potentially blocking operations 556 -- are invoked from protected actions. pragma Atomic is used because it 557 -- can be read/written from protected interrupt handlers. 558 559 Task_Image : String (1 .. System.Parameters.Max_Task_Image_Length); 560 -- Hold a string that provides a readable id for task, built from the 561 -- variable of which it is a value or component. 562 563 Task_Image_Len : Natural; 564 -- Actual length of Task_Image 565 566 Call : Entry_Call_Link; 567 -- The entry call that has been accepted by this task. 568 -- 569 -- Protection: Self.L. Self will modify this field when Self.Accepting 570 -- is False, and will not need the mutex to do so. Once a task sets 571 -- Pending_ATC_Level = 0, no other task can access this field. 572 573 LL : aliased Task_Primitives.Private_Data; 574 -- Control block used by the underlying low-level tasking service 575 -- (GNULLI). 576 -- 577 -- Protection: This is used only by the GNULLI implementation, which 578 -- takes care of all of its synchronization. 579 580 Task_Arg : System.Address; 581 -- The argument to task procedure. Provide a handle for discriminant 582 -- information. 583 -- 584 -- Protection: Part of the synchronization between Self and Activator. 585 -- Activator writes it, once, before Self starts executing. Thereafter, 586 -- Self only reads it. 587 588 Task_Alternate_Stack : System.Address; 589 -- The address of the alternate signal stack for this task, if any 590 -- 591 -- Protection: Only accessed by Self 592 593 Task_Entry_Point : Task_Procedure_Access; 594 -- Information needed to call the procedure containing the code for 595 -- the body of this task. 596 -- 597 -- Protection: Part of the synchronization between Self and Activator. 598 -- Activator writes it, once, before Self starts executing. Self reads 599 -- it, once, as part of its execution. 600 601 Compiler_Data : System.Soft_Links.TSD; 602 -- Task-specific data needed by the compiler to store per-task 603 -- structures. 604 -- 605 -- Protection: Only accessed by Self 606 607 All_Tasks_Link : Task_Id; 608 -- Used to link this task to the list of all tasks in the system 609 -- 610 -- Protection: RTS_Lock 611 612 Activation_Link : Task_Id; 613 -- Used to link this task to a list of tasks to be activated 614 -- 615 -- Protection: Only used by Activator 616 617 Activator : Task_Id; 618 -- The task that created this task, either by declaring it as a task 619 -- object or by executing a task allocator. The value is null iff Self 620 -- has completed activation. 621 -- 622 -- Protection: Set by Activator before Self is activated, and only read 623 -- and modified by Self after that. 624 625 Wait_Count : Natural; 626 -- This count is used by a task that is waiting for other tasks. At all 627 -- other times, the value should be zero. It is used differently in 628 -- several different states. Since a task cannot be in more than one of 629 -- these states at the same time, a single counter suffices. 630 -- 631 -- Protection: Self.L 632 633 -- Activator_Sleep 634 635 -- This is the number of tasks that this task is activating, i.e. the 636 -- children that have started activation but have not completed it. 637 -- 638 -- Protection: Self.L and Created.L. Both mutexes must be locked, since 639 -- Self.Activation_Count and Created.State must be synchronized. 640 641 -- Master_Completion_Sleep (phase 1) 642 643 -- This is the number dependent tasks of a master being completed by 644 -- Self that are activated, but have not yet terminated, and are not 645 -- waiting on a terminate alternative. 646 647 -- Master_Completion_2_Sleep (phase 2) 648 649 -- This is the count of tasks dependent on a master being completed by 650 -- Self which are waiting on a terminate alternative. 651 652 Elaborated : Access_Boolean; 653 -- Pointer to a flag indicating that this task's body has been 654 -- elaborated. The flag is created and managed by the 655 -- compiler-generated code. 656 -- 657 -- Protection: The field itself is only accessed by Activator. The flag 658 -- that it points to is updated by Master and read by Activator; access 659 -- is assumed to be atomic. 660 661 Activation_Failed : Boolean; 662 -- Set to True if activation of a chain of tasks fails, 663 -- so that the activator should raise Tasking_Error. 664 665 Task_Info : System.Task_Info.Task_Info_Type; 666 -- System-specific attributes of the task as specified by the 667 -- Task_Info pragma. 668 669 Analyzer : System.Stack_Usage.Stack_Analyzer; 670 -- For storing informations used to measure the stack usage 671 672 Global_Task_Lock_Nesting : Natural; 673 -- This is the current nesting level of calls to 674 -- System.Tasking.Initialization.Lock_Task. This allows a task to call 675 -- Lock_Task multiple times without deadlocking. A task only locks 676 -- Global_Task_Lock when its Global_Task_Lock_Nesting goes from 0 to 1, 677 -- and only unlocked when it goes from 1 to 0. 678 -- 679 -- Protection: Only accessed by Self 680 681 Fall_Back_Handler : Termination_Handler; 682 -- This is the fall-back handler that applies to the dependent tasks of 683 -- the task. 684 -- 685 -- Protection: Self.L 686 687 Specific_Handler : Termination_Handler; 688 -- This is the specific handler that applies only to this task, and not 689 -- any of its dependent tasks. 690 -- 691 -- Protection: Self.L 692 693 Debug_Events : Debug_Event_Array; 694 -- Word length array of per task debug events, of which 11 kinds are 695 -- currently defined in System.Tasking.Debugging package. 696 697 Domain : Dispatching_Domain_Access; 698 -- Domain is the dispatching domain to which the task belongs. It is 699 -- only changed via dispatching domains package. This field is made 700 -- part of the Common_ATCB, even when restricted run-times (namely 701 -- Ravenscar) do not use it, because this way the field is always 702 -- available to the underlying layers to set the affinity and we do not 703 -- need to do different things depending on the situation. 704 -- 705 -- Protection: Self.L 706 end record; 707 708 --------------------------------------- 709 -- Restricted_Ada_Task_Control_Block -- 710 --------------------------------------- 711 712 -- This type should only be used by the restricted GNARLI and by restricted 713 -- GNULL implementations to allocate an ATCB (see System.Task_Primitives. 714 -- Operations.New_ATCB) that will take significantly less memory. 715 716 -- Note that the restricted GNARLI should only access fields that are 717 -- present in the Restricted_Ada_Task_Control_Block structure. 718 719 type Restricted_Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is 720 record 721 Common : Common_ATCB; 722 -- The common part between various tasking implementations 723 724 Entry_Call : aliased Restricted_Entry_Call_Record; 725 -- Protection: This field is used on entry call "queues" associated 726 -- with protected objects, and is protected by the protected object 727 -- lock. 728 end record; 729 pragma Suppress_Initialization (Restricted_Ada_Task_Control_Block); 730 731 Interrupt_Manager_ID : Task_Id; 732 -- This task ID is declared here to break circular dependencies. 733 -- Also declare Interrupt_Manager_ID after Task_Id is known, to avoid 734 -- generating unneeded finalization code. 735 736 ----------------------- 737 -- List of all Tasks -- 738 ----------------------- 739 740 All_Tasks_List : Task_Id; 741 -- Global linked list of all tasks 742 743 ------------------------------------------ 744 -- Regular (non restricted) definitions -- 745 ------------------------------------------ 746 747 -------------------------------- 748 -- Master Related Definitions -- 749 -------------------------------- 750 751 subtype Master_Level is Integer; 752 subtype Master_ID is Master_Level; 753 754 -- Normally, a task starts out with internal master nesting level one 755 -- larger than external master nesting level. It is incremented to one by 756 -- Enter_Master, which is called in the task body only if the compiler 757 -- thinks the task may have dependent tasks. It is set to 1 for the 758 -- environment task, the level 2 is reserved for server tasks of the 759 -- run-time system (the so called "independent tasks"), and the level 3 is 760 -- for the library level tasks. Foreign threads which are detected by 761 -- the run-time have a level of 0, allowing these tasks to be easily 762 -- distinguished if needed. 763 764 Foreign_Task_Level : constant Master_Level := 0; 765 Environment_Task_Level : constant Master_Level := 1; 766 Independent_Task_Level : constant Master_Level := 2; 767 Library_Task_Level : constant Master_Level := 3; 768 769 ------------------- 770 -- Priority info -- 771 ------------------- 772 773 Unspecified_Priority : constant Integer := System.Priority'First - 1; 774 775 Priority_Not_Boosted : constant Integer := System.Priority'First - 1; 776 -- Definition of Priority actually has to come from the RTS configuration 777 778 subtype Rendezvous_Priority is Integer 779 range Priority_Not_Boosted .. System.Any_Priority'Last; 780 781 ------------------- 782 -- Affinity info -- 783 ------------------- 784 785 Unspecified_CPU : constant := -1; 786 -- No affinity specified 787 788 ------------------------------------ 789 -- Rendezvous related definitions -- 790 ------------------------------------ 791 792 No_Rendezvous : constant := 0; 793 794 Max_Select : constant Integer := Integer'Last; 795 -- RTS-defined 796 797 subtype Select_Index is Integer range No_Rendezvous .. Max_Select; 798 -- type Select_Index is range No_Rendezvous .. Max_Select; 799 800 subtype Positive_Select_Index is 801 Select_Index range 1 .. Select_Index'Last; 802 803 type Accept_Alternative is record 804 Null_Body : Boolean; 805 S : Task_Entry_Index; 806 end record; 807 808 type Accept_List is 809 array (Positive_Select_Index range <>) of Accept_Alternative; 810 811 type Accept_List_Access is access constant Accept_List; 812 813 ----------------------------------- 814 -- ATC_Level related definitions -- 815 ----------------------------------- 816 817 Max_ATC_Nesting : constant Natural := 20; 818 819 subtype ATC_Level_Base is Integer range 0 .. Max_ATC_Nesting; 820 821 ATC_Level_Infinity : constant ATC_Level_Base := ATC_Level_Base'Last; 822 823 subtype ATC_Level is ATC_Level_Base range 0 .. ATC_Level_Base'Last - 1; 824 825 subtype ATC_Level_Index is ATC_Level range 1 .. ATC_Level'Last; 826 827 ---------------------------------- 828 -- Entry_Call_Record definition -- 829 ---------------------------------- 830 831 type Entry_Call_Record is record 832 Self : Task_Id; 833 -- ID of the caller 834 835 Mode : Call_Modes; 836 837 State : Entry_Call_State; 838 pragma Atomic (State); 839 -- Indicates part of the state of the call 840 -- 841 -- Protection: If the call is not on a queue, it should only be 842 -- accessed by Self, and Self does not need any lock to modify this 843 -- field. Once the call is on a queue, the value should be something 844 -- other than Done unless it is cancelled, and access is controller by 845 -- the "server" of the queue -- i.e., the lock of Checked_To_Protection 846 -- (Call_Target) if the call record is on the queue of a PO, or the 847 -- lock of Called_Target if the call is on the queue of a task. See 848 -- comments on type declaration for more details. 849 850 Uninterpreted_Data : System.Address; 851 -- Data passed by the compiler 852 853 Exception_To_Raise : Ada.Exceptions.Exception_Id; 854 -- The exception to raise once this call has been completed without 855 -- being aborted. 856 857 Prev : Entry_Call_Link; 858 859 Next : Entry_Call_Link; 860 861 Level : ATC_Level; 862 -- One of Self and Level are redundant in this implementation, since 863 -- each Entry_Call_Record is at Self.Entry_Calls (Level). Since we must 864 -- have access to the entry call record to be reading this, we could 865 -- get Self from Level, or Level from Self. However, this requires 866 -- non-portable address arithmetic. 867 868 E : Entry_Index; 869 870 Prio : System.Any_Priority; 871 872 -- The above fields are those that there may be some hope of packing. 873 -- They are gathered together to allow for compilers that lay records 874 -- out contiguously, to allow for such packing. 875 876 Called_Task : Task_Id; 877 pragma Atomic (Called_Task); 878 -- Use for task entry calls. The value is null if the call record is 879 -- not in use. Conversely, unless State is Done and Onqueue is false, 880 -- Called_Task points to an ATCB. 881 -- 882 -- Protection: Called_Task.L 883 884 Called_PO : System.Address; 885 pragma Atomic (Called_PO); 886 -- Similar to Called_Task but for protected objects 887 -- 888 -- Note that the previous implementation tried to merge both 889 -- Called_Task and Called_PO but this ended up in many unexpected 890 -- complications (e.g having to add a magic number in the ATCB, which 891 -- caused gdb lots of confusion) with no real gain since the 892 -- Lock_Server implementation still need to loop around chasing for 893 -- pointer changes even with a single pointer. 894 895 Acceptor_Prev_Call : Entry_Call_Link; 896 -- For task entry calls only 897 898 Acceptor_Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted; 899 -- For task entry calls only. The priority of the most recent prior 900 -- call being serviced. For protected entry calls, this function should 901 -- be performed by GNULLI ceiling locking. 902 903 Cancellation_Attempted : Boolean := False; 904 pragma Atomic (Cancellation_Attempted); 905 -- Cancellation of the call has been attempted. 906 -- Consider merging this into State??? 907 908 With_Abort : Boolean := False; 909 -- Tell caller whether the call may be aborted 910 -- ??? consider merging this with Was_Abortable state 911 912 Needs_Requeue : Boolean := False; 913 -- Temporary to tell acceptor of task entry call that 914 -- Exceptional_Complete_Rendezvous needs to do requeue. 915 end record; 916 917 ------------------------------------ 918 -- Task related other definitions -- 919 ------------------------------------ 920 921 type Access_Address is access all System.Address; 922 -- Anonymous pointer used to implement task attributes (see s-tataat.adb 923 -- and a-tasatt.adb) 924 925 pragma No_Strict_Aliasing (Access_Address); 926 -- This type is used in contexts where aliasing may be an issue (see 927 -- for example s-tataat.adb), so we avoid any incorrect aliasing 928 -- assumptions. 929 930 ---------------------------------------------- 931 -- Ada_Task_Control_Block (ATCB) definition -- 932 ---------------------------------------------- 933 934 type Entry_Call_Array is array (ATC_Level_Index) of 935 aliased Entry_Call_Record; 936 937 type Direct_Index is range 0 .. Parameters.Default_Attribute_Count; 938 subtype Direct_Index_Range is Direct_Index range 1 .. Direct_Index'Last; 939 -- Attributes with indexes in this range are stored directly in the task 940 -- control block. Such attributes must be Address-sized. Other attributes 941 -- will be held in dynamically allocated records chained off of the task 942 -- control block. 943 944 type Direct_Attribute_Element is mod Memory_Size; 945 pragma Atomic (Direct_Attribute_Element); 946 947 type Direct_Attribute_Array is 948 array (Direct_Index_Range) of aliased Direct_Attribute_Element; 949 950 type Direct_Index_Vector is mod 2 ** Parameters.Default_Attribute_Count; 951 -- This is a bit-vector type, used to store information about 952 -- the usage of the direct attribute fields. 953 954 type Task_Serial_Number is mod 2 ** 64; 955 -- Used to give each task a unique serial number 956 957 type Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is record 958 Common : Common_ATCB; 959 -- The common part between various tasking implementations 960 961 Entry_Calls : Entry_Call_Array; 962 -- An array of entry calls 963 -- 964 -- Protection: The elements of this array are on entry call queues 965 -- associated with protected objects or task entries, and are protected 966 -- by the protected object lock or Acceptor.L, respectively. 967 968 Entry_Names : Task_Entry_Names_Access := null; 969 -- An array of string names which denotes entry [family member] names. 970 -- The structure is indexed by task entry index and contains Entry_Num 971 -- components. 972 -- 973 -- Protection: The array is populated during task initialization, before 974 -- the task has been activated. No protection is required in this case. 975 976 New_Base_Priority : System.Any_Priority; 977 -- New value for Base_Priority (for dynamic priorities package) 978 -- 979 -- Protection: Self.L 980 981 Open_Accepts : Accept_List_Access; 982 -- This points to the Open_Accepts array of accept alternatives passed 983 -- to the RTS by the compiler-generated code to Selective_Wait. It is 984 -- non-null iff this task is ready to accept an entry call. 985 -- 986 -- Protection: Self.L 987 988 Chosen_Index : Select_Index; 989 -- The index in Open_Accepts of the entry call accepted by a selective 990 -- wait executed by this task. 991 -- 992 -- Protection: Written by both Self and Caller. Usually protected by 993 -- Self.L. However, once the selection is known to have been written it 994 -- can be accessed without protection. This happens after Self has 995 -- updated it itself using information from a suspended Caller, or 996 -- after Caller has updated it and awakened Self. 997 998 Master_of_Task : Master_Level; 999 -- The task executing the master of this task, and the ID of this task's 1000 -- master (unique only among masters currently active within Parent). 1001 -- 1002 -- Protection: Set by Activator before Self is activated, and read 1003 -- after Self is activated. 1004 1005 Master_Within : Master_Level; 1006 -- The ID of the master currently executing within this task; that is, 1007 -- the most deeply nested currently active master. 1008 -- 1009 -- Protection: Only written by Self, and only read by Self or by 1010 -- dependents when Self is attempting to exit a master. Since Self will 1011 -- not write this field until the master is complete, the 1012 -- synchronization should be adequate to prevent races. 1013 1014 Alive_Count : Natural := 0; 1015 -- Number of tasks directly dependent on this task (including itself) 1016 -- that are still "alive", i.e. not terminated. 1017 -- 1018 -- Protection: Self.L 1019 1020 Awake_Count : Natural := 0; 1021 -- Number of tasks directly dependent on this task (including itself) 1022 -- still "awake", i.e., are not terminated and not waiting on a 1023 -- terminate alternative. 1024 -- 1025 -- Invariant: Awake_Count <= Alive_Count 1026 1027 -- Protection: Self.L 1028 1029 -- Beginning of flags 1030 1031 Aborting : Boolean := False; 1032 pragma Atomic (Aborting); 1033 -- Self is in the process of aborting. While set, prevents multiple 1034 -- abort signals from being sent by different aborter while abort 1035 -- is acted upon. This is essential since an aborter which calls 1036 -- Abort_To_Level could set the Pending_ATC_Level to yet a lower level 1037 -- (than the current level), may be preempted and would send the 1038 -- abort signal when resuming execution. At this point, the abortee 1039 -- may have completed abort to the proper level such that the 1040 -- signal (and resulting abort exception) are not handled any more. 1041 -- In other words, the flag prevents a race between multiple aborters 1042 -- 1043 -- Protection: protected by atomic access. 1044 1045 ATC_Hack : Boolean := False; 1046 pragma Atomic (ATC_Hack); 1047 -- ????? 1048 -- Temporary fix, to allow Undefer_Abort to reset Aborting in the 1049 -- handler for Abort_Signal that encloses an async. entry call. 1050 -- For the longer term, this should be done via code in the 1051 -- handler itself. 1052 1053 Callable : Boolean := True; 1054 -- It is OK to call entries of this task 1055 1056 Dependents_Aborted : Boolean := False; 1057 -- This is set to True by whichever task takes responsibility for 1058 -- aborting the dependents of this task. 1059 -- 1060 -- Protection: Self.L 1061 1062 Interrupt_Entry : Boolean := False; 1063 -- Indicates if one or more Interrupt Entries are attached to the task. 1064 -- This flag is needed for cleaning up the Interrupt Entry bindings. 1065 1066 Pending_Action : Boolean := False; 1067 -- Unified flag indicating some action needs to be take when abort 1068 -- next becomes undeferred. Currently set if: 1069 -- . Pending_Priority_Change is set 1070 -- . Pending_ATC_Level is changed 1071 -- . Requeue involving POs 1072 -- (Abortable field may have changed and the Wait_Until_Abortable 1073 -- has to recheck the abortable status of the call.) 1074 -- . Exception_To_Raise is non-null 1075 -- 1076 -- Protection: Self.L 1077 -- 1078 -- This should never be reset back to False outside of the procedure 1079 -- Do_Pending_Action, which is called by Undefer_Abort. It should only 1080 -- be set to True by Set_Priority and Abort_To_Level. 1081 1082 Pending_Priority_Change : Boolean := False; 1083 -- Flag to indicate pending priority change (for dynamic priorities 1084 -- package). The base priority is updated on the next abort 1085 -- completion point (aka. synchronization point). 1086 -- 1087 -- Protection: Self.L 1088 1089 Terminate_Alternative : Boolean := False; 1090 -- Task is accepting Select with Terminate Alternative 1091 -- 1092 -- Protection: Self.L 1093 1094 -- End of flags 1095 1096 -- Beginning of counts 1097 1098 ATC_Nesting_Level : ATC_Level := 1; 1099 -- The dynamic level of ATC nesting (currently executing nested 1100 -- asynchronous select statements) in this task. 1101 1102 -- Protection: Self_ID.L. Only Self reads or updates this field. 1103 -- Decrementing it deallocates an Entry_Calls component, and care must 1104 -- be taken that all references to that component are eliminated before 1105 -- doing the decrement. This in turn will require locking a protected 1106 -- object (for a protected entry call) or the Acceptor's lock (for a 1107 -- task entry call). No other task should attempt to read or modify 1108 -- this value. 1109 1110 Deferral_Level : Natural := 1; 1111 -- This is the number of times that Defer_Abort has been called by 1112 -- this task without a matching Undefer_Abort call. Abortion is only 1113 -- allowed when this zero. It is initially 1, to protect the task at 1114 -- startup. 1115 1116 -- Protection: Only updated by Self; access assumed to be atomic 1117 1118 Pending_ATC_Level : ATC_Level_Base := ATC_Level_Infinity; 1119 -- The ATC level to which this task is currently being aborted. If the 1120 -- value is zero, the entire task has "completed". That may be via 1121 -- abort, exception propagation, or normal exit. If the value is 1122 -- ATC_Level_Infinity, the task is not being aborted to any level. If 1123 -- the value is positive, the task has not completed. This should ONLY 1124 -- be modified by Abort_To_Level and Exit_One_ATC_Level. 1125 -- 1126 -- Protection: Self.L 1127 1128 Serial_Number : Task_Serial_Number; 1129 -- Monotonic counter to provide some way to check locking rules/ordering 1130 1131 Known_Tasks_Index : Integer := -1; 1132 -- Index in the System.Tasking.Debug.Known_Tasks array 1133 1134 User_State : Long_Integer := 0; 1135 -- User-writeable location, for use in debugging tasks; also provides a 1136 -- simple task specific data. 1137 1138 Direct_Attributes : Direct_Attribute_Array; 1139 -- For task attributes that have same size as Address 1140 1141 Is_Defined : Direct_Index_Vector := 0; 1142 -- Bit I is 1 iff Direct_Attributes (I) is defined 1143 1144 Indirect_Attributes : Access_Address; 1145 -- A pointer to chain of records for other attributes that are not 1146 -- address-sized, including all tagged types. 1147 1148 Entry_Queues : Task_Entry_Queue_Array (1 .. Entry_Num); 1149 -- An array of task entry queues 1150 -- 1151 -- Protection: Self.L. Once a task has set Self.Stage to Completing, it 1152 -- has exclusive access to this field. 1153 1154 Free_On_Termination : Boolean := False; 1155 -- Deallocate the ATCB when the task terminates. This flag is normally 1156 -- False, and is set True when Unchecked_Deallocation is called on a 1157 -- non-terminated task so that the associated storage is automatically 1158 -- reclaimed when the task terminates. 1159 end record; 1160 1161 -------------------- 1162 -- Initialization -- 1163 -------------------- 1164 1165 procedure Initialize; 1166 -- This procedure constitutes the first part of the initialization of the 1167 -- GNARL. This includes creating data structures to make the initial thread 1168 -- into the environment task. The last part of the initialization is done 1169 -- in System.Tasking.Initialization or System.Tasking.Restricted.Stages. 1170 -- All the initializations used to be in Tasking.Initialization, but this 1171 -- is no longer possible with the run time simplification (including 1172 -- optimized PO and the restricted run time) since one cannot rely on 1173 -- System.Tasking.Initialization being present, as was done before. 1174 1175 procedure Initialize_ATCB 1176 (Self_ID : Task_Id; 1177 Task_Entry_Point : Task_Procedure_Access; 1178 Task_Arg : System.Address; 1179 Parent : Task_Id; 1180 Elaborated : Access_Boolean; 1181 Base_Priority : System.Any_Priority; 1182 Base_CPU : System.Multiprocessors.CPU_Range; 1183 Domain : Dispatching_Domain_Access; 1184 Task_Info : System.Task_Info.Task_Info_Type; 1185 Stack_Size : System.Parameters.Size_Type; 1186 T : Task_Id; 1187 Success : out Boolean); 1188 -- Initialize fields of a TCB and link into global TCB structures Call 1189 -- this only with abort deferred and holding RTS_Lock. Need more 1190 -- documentation, mention T, and describe Success ??? 1191 1192private 1193 1194 Null_Task : constant Task_Id := null; 1195 1196 type Activation_Chain is limited record 1197 T_ID : Task_Id; 1198 end record; 1199 1200 -- Activation_Chain is an in-out parameter of initialization procedures and 1201 -- it must be passed by reference because the init proc may terminate 1202 -- abnormally after creating task components, and these must be properly 1203 -- registered for removal (Expunge_Unactivated_Tasks). The "limited" forces 1204 -- Activation_Chain to be a by-reference type; see RM-6.2(4). 1205 1206 function Number_Of_Entries (Self_Id : Task_Id) return Entry_Index; 1207 -- Given a task, return the number of entries it contains 1208 1209 procedure Set_Entry_Names 1210 (Self_Id : Task_Id; 1211 Names : Task_Entry_Names_Access); 1212 -- Associate an array of strings denotinge entry [family] names with a task 1213 1214end System.Tasking; 1215