1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K I N G --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This package provides necessary type definitions for compiler interface
36 -- Note: the compiler generates direct calls to this interface, via Rtsfind.
37 -- Any changes to this interface may require corresponding compiler changes.
40 with Ada.Unchecked_Conversion;
42 with System.Parameters;
43 with System.Task_Info;
44 with System.Soft_Links;
45 with System.Task_Primitives;
46 with System.Stack_Usage;
48 package System.Tasking is
55 -- The following rules must be followed at all times, to prevent
56 -- deadlock and generally ensure correct operation of locking.
58 -- Never lock a lock unless abort is deferred
60 -- Never undefer abort while holding a lock
62 -- Overlapping critical sections must be properly nested, and locks must
63 -- be released in LIFO order. e.g., the following is not allowed:
73 -- Locks with lower (smaller) level number cannot be locked
74 -- while holding a lock with a higher level number. (The level
76 -- 1. System.Tasking.PO_Simple.Protection.L (any PO lock)
77 -- 2. System.Tasking.Initialization.Global_Task_Lock (in body)
78 -- 3. System.Task_Primitives.Operations.Single_RTS_Lock
79 -- 4. System.Tasking.Ada_Task_Control_Block.LL.L (any TCB lock)
81 -- Clearly, there can be no circular chain of hold-and-wait
82 -- relationships involving locks in different ordering levels.
84 -- We used to have Global_Task_Lock before Protection.L but this was
85 -- clearly wrong since there can be calls to "new" inside protected
86 -- operations. The new ordering prevents these failures.
88 -- Sometimes we need to hold two ATCB locks at the same time. To allow us
89 -- to order the locking, each ATCB is given a unique serial number. If one
90 -- needs to hold locks on several ATCBs at once, the locks with lower
91 -- serial numbers must be locked first.
93 -- We don't always need to check the serial numbers, since the serial
94 -- numbers are assigned sequentially, and so:
96 -- . The parent of a task always has a lower serial number.
97 -- . The activator of a task always has a lower serial number.
98 -- . The environment task has a lower serial number than any other task.
99 -- . If the activator of a task is different from the task's parent,
100 -- the parent always has a lower serial number than the activator.
102 ---------------------------------
103 -- Task_Id related definitions --
104 ---------------------------------
106 type Ada_Task_Control_Block;
108 type Task_Id is access all Ada_Task_Control_Block;
109 for Task_Id'Size use System.Task_Primitives.Task_Address_Size;
111 Null_Task : constant Task_Id;
113 type Task_List is array (Positive range <>) of Task_Id;
115 function Self return Task_Id;
116 pragma Inline (Self);
117 -- This is the compiler interface version of this function. Do not call
118 -- from the run-time system.
120 function To_Task_Id is
121 new Ada.Unchecked_Conversion
122 (System.Task_Primitives.Task_Address, Task_Id);
123 function To_Address is
124 new Ada.Unchecked_Conversion
125 (Task_Id, System.Task_Primitives.Task_Address);
127 -----------------------
128 -- Enumeration types --
129 -----------------------
133 -- Task has been created but has not been activated.
134 -- It cannot be executing.
137 -- For all states from here down, the task has been activated.
138 -- For all states from here down, except for Terminated, the task
140 -- Activator = null iff it has not yet completed activating.
142 -- For all states from here down,
143 -- the task has been activated, and may be executing.
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.
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.
156 -- Task is waiting for created tasks to complete activation
159 -- Task is waiting on an accept or selective wait statement
162 -- Task is waiting on an entry call
165 -- Task is waiting to start the abortable part of an
166 -- asynchronous select statement.
169 -- Task is waiting on a select statement with only a delay
172 Master_Completion_Sleep,
173 -- Master completion has two phases.
174 -- In Phase 1 the task is sleeping in Complete_Master
175 -- having completed a master within itself,
176 -- and is waiting for the tasks dependent on that master to become
177 -- terminated or waiting on a terminate Phase.
179 Master_Phase_2_Sleep,
180 -- In Phase 2 the task is sleeping in Complete_Master
181 -- waiting for tasks on terminate alternatives to finish
184 -- The following are special uses of sleep, for server tasks
185 -- within the run-time system.
187 Interrupt_Server_Idle_Sleep,
188 Interrupt_Server_Blocked_Interrupt_Sleep,
193 -- The task has been held by Asynchronous_Task_Control.Hold_Task
195 Interrupt_Server_Blocked_On_Event_Flag
196 -- The task has been blocked on a system call waiting for a
197 -- completion event/signal to occur.
201 (Simple_Call, Conditional_Call, Asynchronous_Call, Timed_Call);
203 type Select_Modes is (Simple_Mode, Else_Mode, Terminate_Mode, Delay_Mode);
205 subtype Delay_Modes is Integer;
207 -------------------------------
208 -- Entry related definitions --
209 -------------------------------
211 Null_Entry : constant := 0;
213 Max_Entry : constant := Integer'Last;
215 Interrupt_Entry : constant := -2;
217 Cancelled_Entry : constant := -1;
219 type Entry_Index is range Interrupt_Entry .. Max_Entry;
221 Null_Task_Entry : constant := Null_Entry;
223 Max_Task_Entry : constant := Max_Entry;
225 type Task_Entry_Index is new Entry_Index
226 range Null_Task_Entry .. Max_Task_Entry;
228 type Entry_Call_Record;
230 type Entry_Call_Link is access all Entry_Call_Record;
232 type Entry_Queue is record
233 Head : Entry_Call_Link;
234 Tail : Entry_Call_Link;
237 type Task_Entry_Queue_Array is
238 array (Task_Entry_Index range <>) of Entry_Queue;
240 ----------------------------------
241 -- Entry_Call_Record definition --
242 ----------------------------------
244 type Entry_Call_State is
246 -- the call is not abortable, and never can be
249 -- the call is not abortable, but may become so
252 -- the call is not abortable, but once was
255 -- the call is abortable
258 -- the call has been completed
261 -- the call was asynchronous, and was cancelled
264 -- Never_Abortable is used for calls that are made in a abort
265 -- deferred region (see ARM 9.8(5-11), 9.8 (20)).
266 -- Such a call is never abortable.
268 -- The Was_ vs. Not_Yet_ distinction is needed to decide whether it
269 -- is OK to advance into the abortable part of an async. select stmt.
270 -- That is allowed iff the mode is Now_ or Was_.
272 -- Done indicates the call has been completed, without cancellation,
273 -- or no call has been made yet at this ATC nesting level,
274 -- and so aborting the call is no longer an issue.
275 -- Completion of the call does not necessarily indicate "success";
276 -- the call may be returning an exception if Exception_To_Raise is
279 -- Cancelled indicates the call was cancelled,
280 -- and so aborting the call is no longer an issue.
282 -- The call is on an entry queue unless
283 -- State >= Done, in which case it may or may not be still Onqueue.
285 -- Please do not modify the order of the values, without checking
286 -- all uses of this type. We rely on partial "monotonicity" of
287 -- Entry_Call_Record.State to avoid locking when we access this
288 -- value for certain tests. In particular:
290 -- 1) Once State >= Done, we can rely that the call has been
291 -- completed. If State >= Done, it will not
292 -- change until the task does another entry call at this level.
294 -- 2) Once State >= Was_Abortable, we can rely that the call has
295 -- been queued abortably at least once, and so the check for
296 -- whether it is OK to advance to the abortable part of an
297 -- async. select statement does not need to lock anything.
299 type Restricted_Entry_Call_Record is record
305 State : Entry_Call_State;
306 pragma Atomic (State);
307 -- Indicates part of the state of the call.
309 -- Protection: If the call is not on a queue, it should only be
310 -- accessed by Self, and Self does not need any lock to modify this
313 -- Once the call is on a queue, the value should be something other
314 -- than Done unless it is cancelled, and access is controller by the
315 -- "server" of the queue -- i.e., the lock of Checked_To_Protection
316 -- (Call_Target) if the call record is on the queue of a PO, or the
317 -- lock of Called_Target if the call is on the queue of a task. See
318 -- comments on type declaration for more details.
320 Uninterpreted_Data : System.Address;
321 -- Data passed by the compiler
323 Exception_To_Raise : Ada.Exceptions.Exception_Id;
324 -- The exception to raise once this call has been completed without
327 pragma Suppress_Initialization (Restricted_Entry_Call_Record);
329 -------------------------------------------
330 -- Task termination procedure definition --
331 -------------------------------------------
333 -- We need to redefine here these types (already defined in
334 -- Ada.Task_Termination) for avoiding circular dependencies.
336 type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception);
337 -- Possible causes for task termination:
339 -- Normal means that the task terminates due to completing the
340 -- last sentence of its body, or as a result of waiting on a
341 -- terminate alternative.
343 -- Abnormal means that the task terminates because it is being aborted
345 -- handled_Exception means that the task terminates because of exception
346 -- raised by the execution of its task_body.
348 type Termination_Handler is access protected procedure
349 (Cause : Cause_Of_Termination;
351 X : Ada.Exceptions.Exception_Occurrence);
352 -- Used to represent protected procedures to be executed when task
355 ------------------------------------
356 -- Task related other definitions --
357 ------------------------------------
359 type Activation_Chain is limited private;
360 -- Linked list of to-be-activated tasks, linked through
361 -- Activation_Link. The order of tasks on the list is irrelevant, because
362 -- the priority rules will ensure that they actually start activating in
365 type Activation_Chain_Access is access all Activation_Chain;
367 type Task_Procedure_Access is access procedure (Arg : System.Address);
369 type Access_Boolean is access all Boolean;
371 function Detect_Blocking return Boolean;
372 pragma Inline (Detect_Blocking);
373 -- Return whether the Detect_Blocking pragma is enabled
375 function Storage_Size (T : Task_Id) return System.Parameters.Size_Type;
376 -- Retrieve from the TCB of the task the allocated size of its stack,
377 -- either the system default or the size specified by a pragma. This
378 -- is in general a non-static value that can depend on discriminants
381 ----------------------------------------------
382 -- Ada_Task_Control_Block (ATCB) definition --
383 ----------------------------------------------
385 -- Notes on protection (synchronization) of TRTS data structures
387 -- Any field of the TCB can be written by the activator of a task when the
388 -- task is created, since no other task can access the new task's
389 -- state until creation is complete.
391 -- The protection for each field is described in a comment starting with
394 -- When a lock is used to protect an ATCB field, this lock is simply named
396 -- Some protection is described in terms of tasks related to the
397 -- ATCB being protected. These are:
399 -- Self: The task which is controlled by this ATCB
400 -- Acceptor: A task accepting a call from Self
401 -- Caller: A task calling an entry of Self
402 -- Parent: The task executing the master on which Self depends
403 -- Dependent: A task dependent on Self
404 -- Activator: The task that created Self and initiated its activation
405 -- Created: A task created and activated by Self
407 -- Note: The order of the fields is important to implement efficiently
408 -- tasking support under gdb.
409 -- Currently gdb relies on the order of the State, Parent, Base_Priority,
410 -- Task_Image, Task_Image_Len, Call and LL fields.
412 -------------------------
413 -- Common ATCB section --
414 -------------------------
416 -- Section used by all GNARL implementations (regular and restricted)
418 type Common_ATCB is record
420 pragma Atomic (State);
421 -- Encodes some basic information about the state of a task,
422 -- including whether it has been activated, whether it is sleeping,
423 -- and whether it is terminated.
425 -- Protection: Self.L
428 -- The task on which this task depends.
429 -- See also Master_Level and Master_Within.
431 Base_Priority : System.Any_Priority;
432 -- Base priority, not changed during entry calls, only changed
433 -- via dynamic priorities package.
435 -- Protection: Only written by Self, accessed by anyone
437 Current_Priority : System.Any_Priority;
438 -- Active priority, except that the effects of protected object
439 -- priority ceilings are not reflected. This only reflects explicit
440 -- priority changes and priority inherited through task activation
443 -- Ada 95 notes: In Ada 95, this field will be transferred to the
444 -- Priority field of an Entry_Calls component when an entry call
445 -- is initiated. The Priority of the Entry_Calls component will not
446 -- change for the duration of the call. The accepting task can
447 -- use it to boost its own priority without fear of its changing in
450 -- This can safely be used in the priority ordering
451 -- of entry queues. Once a call is queued, its priority does not
454 -- Since an entry call cannot be made while executing
455 -- a protected action, the priority of a task will never reflect a
456 -- priority ceiling change at the point of an entry call.
458 -- Protection: Only written by Self, and only accessed when Acceptor
459 -- accepts an entry or when Created activates, at which points Self is
462 Protected_Action_Nesting : Natural;
463 pragma Atomic (Protected_Action_Nesting);
464 -- The dynamic level of protected action nesting for this task. This
465 -- field is needed for checking whether potentially blocking operations
466 -- are invoked from protected actions. pragma Atomic is used because it
467 -- can be read/written from protected interrupt handlers.
469 Task_Image : String (1 .. System.Parameters.Max_Task_Image_Length);
470 -- Hold a string that provides a readable id for task,
471 -- built from the variable of which it is a value or component.
473 Task_Image_Len : Natural;
474 -- Actual length of Task_Image
476 Call : Entry_Call_Link;
477 -- The entry call that has been accepted by this task.
479 -- Protection: Self.L. Self will modify this field when Self.Accepting
480 -- is False, and will not need the mutex to do so. Once a task sets
481 -- Pending_ATC_Level = 0, no other task can access this field.
483 LL : aliased Task_Primitives.Private_Data;
484 -- Control block used by the underlying low-level tasking service
487 -- Protection: This is used only by the GNULLI implementation, which
488 -- takes care of all of its synchronization.
490 Task_Arg : System.Address;
491 -- The argument to task procedure. Provide a handle for discriminant
494 -- Protection: Part of the synchronization between Self and Activator.
495 -- Activator writes it, once, before Self starts executing. Thereafter,
496 -- Self only reads it.
498 Task_Alternate_Stack : System.Address;
499 -- The address of the alternate signal stack for this task, if any
501 -- Protection: Only accessed by Self
503 Task_Entry_Point : Task_Procedure_Access;
504 -- Information needed to call the procedure containing the code for
505 -- the body of this task.
507 -- Protection: Part of the synchronization between Self and Activator.
508 -- Activator writes it, once, before Self starts executing. Self reads
509 -- it, once, as part of its execution.
511 Compiler_Data : System.Soft_Links.TSD;
512 -- Task-specific data needed by the compiler to store per-task
515 -- Protection: Only accessed by Self
517 All_Tasks_Link : Task_Id;
518 -- Used to link this task to the list of all tasks in the system
520 -- Protection: RTS_Lock
522 Activation_Link : Task_Id;
523 -- Used to link this task to a list of tasks to be activated
525 -- Protection: Only used by Activator
528 -- The task that created this task, either by declaring it as a task
529 -- object or by executing a task allocator. The value is null iff Self
530 -- has completed activation.
532 -- Protection: Set by Activator before Self is activated, and only read
533 -- and modified by Self after that.
535 Wait_Count : Integer;
536 -- This count is used by a task that is waiting for other tasks. At all
537 -- other times, the value should be zero. It is used differently in
538 -- several different states. Since a task cannot be in more than one of
539 -- these states at the same time, a single counter suffices.
541 -- Protection: Self.L
545 -- This is the number of tasks that this task is activating, i.e. the
546 -- children that have started activation but have not completed it.
548 -- Protection: Self.L and Created.L. Both mutexes must be locked, since
549 -- Self.Activation_Count and Created.State must be synchronized.
551 -- Master_Completion_Sleep (phase 1)
553 -- This is the number dependent tasks of a master being completed by
554 -- Self that are not activated, not terminated, and not waiting on a
555 -- terminate alternative.
557 -- Master_Completion_2_Sleep (phase 2)
559 -- This is the count of tasks dependent on a master being completed by
560 -- Self which are waiting on a terminate alternative.
562 Elaborated : Access_Boolean;
563 -- Pointer to a flag indicating that this task's body has been
564 -- elaborated. The flag is created and managed by the
565 -- compiler-generated code.
567 -- Protection: The field itself is only accessed by Activator. The flag
568 -- that it points to is updated by Master and read by Activator; access
569 -- is assumed to be atomic.
571 Activation_Failed : Boolean;
572 -- Set to True if activation of a chain of tasks fails,
573 -- so that the activator should raise Tasking_Error.
575 Task_Info : System.Task_Info.Task_Info_Type;
576 -- System-specific attributes of the task as specified by the
579 Analyzer : System.Stack_Usage.Stack_Analyzer;
580 -- For storing informations used to measure the stack usage
582 Global_Task_Lock_Nesting : Natural;
583 -- This is the current nesting level of calls to
584 -- System.Tasking.Initialization.Lock_Task. This allows a task to call
585 -- Lock_Task multiple times without deadlocking. A task only locks
586 -- Global_Task_Lock when its Global_Task_Lock_Nesting goes from 0 to 1,
587 -- and only unlocked when it goes from 1 to 0.
589 -- Protection: Only accessed by Self
591 Fall_Back_Handler : Termination_Handler;
592 -- This is the fall-back handler that applies to the dependent tasks of
595 -- Protection: Self.L
597 Specific_Handler : Termination_Handler;
598 -- This is the specific handler that applies only to this task, and not
599 -- any of its dependent tasks.
601 -- Protection: Self.L
604 ---------------------------------------
605 -- Restricted_Ada_Task_Control_Block --
606 ---------------------------------------
608 -- This type should only be used by the restricted GNARLI and by
609 -- restricted GNULL implementations to allocate an ATCB (see
610 -- System.Task_Primitives.Operations.New_ATCB) that will take
611 -- significantly less memory.
613 -- Note that the restricted GNARLI should only access fields that are
614 -- present in the Restricted_Ada_Task_Control_Block structure.
616 type Restricted_Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is
618 Common : Common_ATCB;
619 -- The common part between various tasking implementations
621 Entry_Call : aliased Restricted_Entry_Call_Record;
622 -- Protection: This field is used on entry call "queues" associated
623 -- with protected objects, and is protected by the protected object
626 pragma Suppress_Initialization (Restricted_Ada_Task_Control_Block);
628 Interrupt_Manager_ID : Task_Id;
629 -- This task ID is declared here to break circular dependencies.
630 -- Also declare Interrupt_Manager_ID after Task_Id is known, to avoid
631 -- generating unneeded finalization code.
633 -----------------------
634 -- List of all Tasks --
635 -----------------------
637 All_Tasks_List : Task_Id;
638 -- Global linked list of all tasks
640 ------------------------------------------
641 -- Regular (non restricted) definitions --
642 ------------------------------------------
644 --------------------------------
645 -- Master Related Definitions --
646 --------------------------------
648 subtype Master_Level is Integer;
649 subtype Master_ID is Master_Level;
651 -- Normally, a task starts out with internal master nesting level one
652 -- larger than external master nesting level. It is incremented to one by
653 -- Enter_Master, which is called in the task body only if the compiler
654 -- thinks the task may have dependent tasks. It is set to 1 for the
655 -- environment task, the level 2 is reserved for server tasks of the
656 -- run-time system (the so called "independent tasks"), and the level 3 is
657 -- for the library level tasks. Foreign threads which are detected by
658 -- the run-time have a level of 0, allowing these tasks to be easily
659 -- distinguished if needed.
661 Foreign_Task_Level : constant Master_Level := 0;
662 Environment_Task_Level : constant Master_Level := 1;
663 Independent_Task_Level : constant Master_Level := 2;
664 Library_Task_Level : constant Master_Level := 3;
666 ------------------------------
667 -- Task size, priority info --
668 ------------------------------
670 Unspecified_Priority : constant Integer := System.Priority'First - 1;
672 Priority_Not_Boosted : constant Integer := System.Priority'First - 1;
673 -- Definition of Priority actually has to come from the RTS configuration
675 subtype Rendezvous_Priority is Integer
676 range Priority_Not_Boosted .. System.Any_Priority'Last;
678 ------------------------------------
679 -- Rendezvous related definitions --
680 ------------------------------------
682 No_Rendezvous : constant := 0;
684 Max_Select : constant Integer := Integer'Last;
687 subtype Select_Index is Integer range No_Rendezvous .. Max_Select;
688 -- type Select_Index is range No_Rendezvous .. Max_Select;
690 subtype Positive_Select_Index is
691 Select_Index range 1 .. Select_Index'Last;
693 type Accept_Alternative is record
695 S : Task_Entry_Index;
699 array (Positive_Select_Index range <>) of Accept_Alternative;
701 type Accept_List_Access is access constant Accept_List;
703 -----------------------------------
704 -- ATC_Level related definitions --
705 -----------------------------------
707 Max_ATC_Nesting : constant Natural := 20;
709 subtype ATC_Level_Base is Integer range 0 .. Max_ATC_Nesting;
711 ATC_Level_Infinity : constant ATC_Level_Base := ATC_Level_Base'Last;
713 subtype ATC_Level is ATC_Level_Base range 0 .. ATC_Level_Base'Last - 1;
715 subtype ATC_Level_Index is ATC_Level range 1 .. ATC_Level'Last;
717 ----------------------------------
718 -- Entry_Call_Record definition --
719 ----------------------------------
721 type Entry_Call_Record is record
727 State : Entry_Call_State;
728 pragma Atomic (State);
729 -- Indicates part of the state of the call
731 -- Protection: If the call is not on a queue, it should only be
732 -- accessed by Self, and Self does not need any lock to modify this
733 -- field. Once the call is on a queue, the value should be something
734 -- other than Done unless it is cancelled, and access is controller by
735 -- the "server" of the queue -- i.e., the lock of Checked_To_Protection
736 -- (Call_Target) if the call record is on the queue of a PO, or the
737 -- lock of Called_Target if the call is on the queue of a task. See
738 -- comments on type declaration for more details.
740 Uninterpreted_Data : System.Address;
741 -- Data passed by the compiler
743 Exception_To_Raise : Ada.Exceptions.Exception_Id;
744 -- The exception to raise once this call has been completed without
747 Prev : Entry_Call_Link;
749 Next : Entry_Call_Link;
752 -- One of Self and Level are redundant in this implementation, since
753 -- each Entry_Call_Record is at Self.Entry_Calls (Level). Since we must
754 -- have access to the entry call record to be reading this, we could
755 -- get Self from Level, or Level from Self. However, this requires
756 -- non-portable address arithmetic.
760 Prio : System.Any_Priority;
762 -- The above fields are those that there may be some hope of packing.
763 -- They are gathered together to allow for compilers that lay records
764 -- out contiguously, to allow for such packing.
766 Called_Task : Task_Id;
767 pragma Atomic (Called_Task);
768 -- Use for task entry calls. The value is null if the call record is
769 -- not in use. Conversely, unless State is Done and Onqueue is false,
770 -- Called_Task points to an ATCB.
772 -- Protection: Called_Task.L
774 Called_PO : System.Address;
775 pragma Atomic (Called_PO);
776 -- Similar to Called_Task but for protected objects
778 -- Note that the previous implementation tried to merge both
779 -- Called_Task and Called_PO but this ended up in many unexpected
780 -- complications (e.g having to add a magic number in the ATCB, which
781 -- caused gdb lots of confusion) with no real gain since the
782 -- Lock_Server implementation still need to loop around chasing for
783 -- pointer changes even with a single pointer.
785 Acceptor_Prev_Call : Entry_Call_Link;
786 -- For task entry calls only
788 Acceptor_Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted;
789 -- For task entry calls only. The priority of the most recent prior
790 -- call being serviced. For protected entry calls, this function should
791 -- be performed by GNULLI ceiling locking.
793 Cancellation_Attempted : Boolean := False;
794 pragma Atomic (Cancellation_Attempted);
795 -- Cancellation of the call has been attempted.
796 -- Consider merging this into State???
798 With_Abort : Boolean := False;
799 -- Tell caller whether the call may be aborted
800 -- ??? consider merging this with Was_Abortable state
802 Needs_Requeue : Boolean := False;
803 -- Temporary to tell acceptor of task entry call that
804 -- Exceptional_Complete_Rendezvous needs to do requeue.
807 ------------------------------------
808 -- Task related other definitions --
809 ------------------------------------
811 type Access_Address is access all System.Address;
812 -- Anonymous pointer used to implement task attributes (see s-tataat.adb
815 pragma No_Strict_Aliasing (Access_Address);
816 -- This type is used in contexts where aliasing may be an issue (see
817 -- for example s-tataat.adb), so we avoid any incorrect aliasing
820 ----------------------------------------------
821 -- Ada_Task_Control_Block (ATCB) definition --
822 ----------------------------------------------
824 type Entry_Call_Array is array (ATC_Level_Index) of
825 aliased Entry_Call_Record;
827 type Direct_Index is range 0 .. Parameters.Default_Attribute_Count;
828 subtype Direct_Index_Range is Direct_Index range 1 .. Direct_Index'Last;
829 -- Attributes with indices in this range are stored directly in the task
830 -- control block. Such attributes must be Address-sized. Other attributes
831 -- will be held in dynamically allocated records chained off of the task
834 type Direct_Attribute_Element is mod Memory_Size;
835 pragma Atomic (Direct_Attribute_Element);
837 type Direct_Attribute_Array is
838 array (Direct_Index_Range) of aliased Direct_Attribute_Element;
840 type Direct_Index_Vector is mod 2 ** Parameters.Default_Attribute_Count;
841 -- This is a bit-vector type, used to store information about
842 -- the usage of the direct attribute fields.
844 type Task_Serial_Number is mod 2 ** 64;
845 -- Used to give each task a unique serial number
847 type Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is record
848 Common : Common_ATCB;
849 -- The common part between various tasking implementations
851 Entry_Calls : Entry_Call_Array;
852 -- An array of entry calls
854 -- Protection: The elements of this array are on entry call queues
855 -- associated with protected objects or task entries, and are protected
856 -- by the protected object lock or Acceptor.L, respectively.
858 New_Base_Priority : System.Any_Priority;
859 -- New value for Base_Priority (for dynamic priorities package)
861 -- Protection: Self.L
863 Open_Accepts : Accept_List_Access;
864 -- This points to the Open_Accepts array of accept alternatives passed
865 -- to the RTS by the compiler-generated code to Selective_Wait. It is
866 -- non-null iff this task is ready to accept an entry call.
868 -- Protection: Self.L
870 Chosen_Index : Select_Index;
871 -- The index in Open_Accepts of the entry call accepted by a selective
872 -- wait executed by this task.
874 -- Protection: Written by both Self and Caller. Usually protected by
875 -- Self.L. However, once the selection is known to have been written it
876 -- can be accessed without protection. This happens after Self has
877 -- updated it itself using information from a suspended Caller, or
878 -- after Caller has updated it and awakened Self.
880 Master_of_Task : Master_Level;
881 -- The task executing the master of this task, and the ID of this task's
882 -- master (unique only among masters currently active within Parent).
884 -- Protection: Set by Activator before Self is activated, and read
885 -- after Self is activated.
887 Master_Within : Master_Level;
888 -- The ID of the master currently executing within this task; that is,
889 -- the most deeply nested currently active master.
891 -- Protection: Only written by Self, and only read by Self or by
892 -- dependents when Self is attempting to exit a master. Since Self will
893 -- not write this field until the master is complete, the
894 -- synchronization should be adequate to prevent races.
896 Alive_Count : Integer := 0;
897 -- Number of tasks directly dependent on this task (including itself)
898 -- that are still "alive", i.e. not terminated.
900 -- Protection: Self.L
902 Awake_Count : Integer := 0;
903 -- Number of tasks directly dependent on this task (including itself)
904 -- still "awake", i.e., are not terminated and not waiting on a
905 -- terminate alternative.
907 -- Invariant: Awake_Count <= Alive_Count
909 -- Protection: Self.L
911 -- Beginning of flags
913 Aborting : Boolean := False;
914 pragma Atomic (Aborting);
915 -- Self is in the process of aborting. While set, prevents multiple
916 -- abort signals from being sent by different aborter while abort
917 -- is acted upon. This is essential since an aborter which calls
918 -- Abort_To_Level could set the Pending_ATC_Level to yet a lower level
919 -- (than the current level), may be preempted and would send the
920 -- abort signal when resuming execution. At this point, the abortee
921 -- may have completed abort to the proper level such that the
922 -- signal (and resulting abort exception) are not handled any more.
923 -- In other words, the flag prevents a race between multiple aborters
925 -- Protection: protected by atomic access.
927 ATC_Hack : Boolean := False;
928 pragma Atomic (ATC_Hack);
930 -- Temporary fix, to allow Undefer_Abort to reset Aborting in the
931 -- handler for Abort_Signal that encloses an async. entry call.
932 -- For the longer term, this should be done via code in the
935 Callable : Boolean := True;
936 -- It is OK to call entries of this task
938 Dependents_Aborted : Boolean := False;
939 -- This is set to True by whichever task takes responsibility for
940 -- aborting the dependents of this task.
942 -- Protection: Self.L
944 Interrupt_Entry : Boolean := False;
945 -- Indicates if one or more Interrupt Entries are attached to the task.
946 -- This flag is needed for cleaning up the Interrupt Entry bindings.
948 Pending_Action : Boolean := False;
949 -- Unified flag indicating some action needs to be take when abort
950 -- next becomes undeferred. Currently set if:
951 -- . Pending_Priority_Change is set
952 -- . Pending_ATC_Level is changed
953 -- . Requeue involving POs
954 -- (Abortable field may have changed and the Wait_Until_Abortable
955 -- has to recheck the abortable status of the call.)
956 -- . Exception_To_Raise is non-null
958 -- Protection: Self.L
960 -- This should never be reset back to False outside of the procedure
961 -- Do_Pending_Action, which is called by Undefer_Abort. It should only
962 -- be set to True by Set_Priority and Abort_To_Level.
964 Pending_Priority_Change : Boolean := False;
965 -- Flag to indicate pending priority change (for dynamic priorities
966 -- package). The base priority is updated on the next abort
967 -- completion point (aka. synchronization point).
969 -- Protection: Self.L
971 Terminate_Alternative : Boolean := False;
972 -- Task is accepting Select with Terminate Alternative
974 -- Protection: Self.L
978 -- Beginning of counts
980 ATC_Nesting_Level : ATC_Level := 1;
981 -- The dynamic level of ATC nesting (currently executing nested
982 -- asynchronous select statements) in this task.
984 -- Protection: Self_ID.L. Only Self reads or updates this field.
985 -- Decrementing it deallocates an Entry_Calls component, and care must
986 -- be taken that all references to that component are eliminated before
987 -- doing the decrement. This in turn will require locking a protected
988 -- object (for a protected entry call) or the Acceptor's lock (for a
989 -- task entry call). No other task should attempt to read or modify
992 Deferral_Level : Natural := 1;
993 -- This is the number of times that Defer_Abort has been called by
994 -- this task without a matching Undefer_Abort call. Abortion is only
995 -- allowed when this zero. It is initially 1, to protect the task at
998 -- Protection: Only updated by Self; access assumed to be atomic
1000 Pending_ATC_Level : ATC_Level_Base := ATC_Level_Infinity;
1001 -- The ATC level to which this task is currently being aborted. If the
1002 -- value is zero, the entire task has "completed". That may be via
1003 -- abort, exception propagation, or normal exit. If the value is
1004 -- ATC_Level_Infinity, the task is not being aborted to any level. If
1005 -- the value is positive, the task has not completed. This should ONLY
1006 -- be modified by Abort_To_Level and Exit_One_ATC_Level.
1008 -- Protection: Self.L
1010 Serial_Number : Task_Serial_Number;
1011 -- A growing number to provide some way to check locking rules/ordering
1013 Known_Tasks_Index : Integer := -1;
1014 -- Index in the System.Tasking.Debug.Known_Tasks array
1016 User_State : Long_Integer := 0;
1017 -- User-writeable location, for use in debugging tasks; also provides a
1018 -- simple task specific data.
1020 Direct_Attributes : Direct_Attribute_Array;
1021 -- For task attributes that have same size as Address
1023 Is_Defined : Direct_Index_Vector := 0;
1024 -- Bit I is 1 iff Direct_Attributes (I) is defined
1026 Indirect_Attributes : Access_Address;
1027 -- A pointer to chain of records for other attributes that are not
1028 -- address-sized, including all tagged types.
1030 Entry_Queues : Task_Entry_Queue_Array (1 .. Entry_Num);
1031 -- An array of task entry queues
1033 -- Protection: Self.L. Once a task has set Self.Stage to Completing, it
1034 -- has exclusive access to this field.
1037 --------------------
1038 -- Initialization --
1039 --------------------
1041 procedure Initialize;
1042 -- This procedure constitutes the first part of the initialization of the
1043 -- GNARL. This includes creating data structures to make the initial thread
1044 -- into the environment task. The last part of the initialization is done
1045 -- in System.Tasking.Initialization or System.Tasking.Restricted.Stages.
1046 -- All the initializations used to be in Tasking.Initialization, but this
1047 -- is no longer possible with the run time simplification (including
1048 -- optimized PO and the restricted run time) since one cannot rely on
1049 -- System.Tasking.Initialization being present, as was done before.
1051 procedure Initialize_ATCB
1053 Task_Entry_Point : Task_Procedure_Access;
1054 Task_Arg : System.Address;
1056 Elaborated : Access_Boolean;
1057 Base_Priority : System.Any_Priority;
1058 Task_Info : System.Task_Info.Task_Info_Type;
1059 Stack_Size : System.Parameters.Size_Type;
1061 Success : out Boolean);
1062 -- Initialize fields of a TCB and link into global TCB structures Call
1063 -- this only with abort deferred and holding RTS_Lock. Need more
1064 -- documentation, mention T, and describe Success ???
1068 Null_Task : constant Task_Id := null;
1070 type Activation_Chain is limited record
1074 -- Activation_Chain is an in-out parameter of initialization procedures and
1075 -- it must be passed by reference because the init proc may terminate
1076 -- abnormally after creating task components, and these must be properly
1077 -- registered for removal (Expunge_Unactivated_Tasks). The "limited" forces
1078 -- Activation_Chain to be a by-reference type; see RM-6.2(4).