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-2009, 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 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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This package provides necessary type definitions for compiler interface
34 -- Note: the compiler generates direct calls to this interface, via Rtsfind.
35 -- Any changes to this interface may require corresponding compiler changes.
38 with Ada.Unchecked_Conversion;
40 with System.Parameters;
41 with System.Task_Info;
42 with System.Soft_Links;
43 with System.Task_Primitives;
44 with System.Stack_Usage;
46 package System.Tasking is
53 -- The following rules must be followed at all times, to prevent
54 -- deadlock and generally ensure correct operation of locking.
56 -- Never lock a lock unless abort is deferred
58 -- Never undefer abort while holding a lock
60 -- Overlapping critical sections must be properly nested, and locks must
61 -- be released in LIFO order. E.g., the following is not allowed:
71 -- Locks with lower (smaller) level number cannot be locked
72 -- while holding a lock with a higher level number. (The level
74 -- 1. System.Tasking.PO_Simple.Protection.L (any PO lock)
75 -- 2. System.Tasking.Initialization.Global_Task_Lock (in body)
76 -- 3. System.Task_Primitives.Operations.Single_RTS_Lock
77 -- 4. System.Tasking.Ada_Task_Control_Block.LL.L (any TCB lock)
79 -- Clearly, there can be no circular chain of hold-and-wait
80 -- relationships involving locks in different ordering levels.
82 -- We used to have Global_Task_Lock before Protection.L but this was
83 -- clearly wrong since there can be calls to "new" inside protected
84 -- operations. The new ordering prevents these failures.
86 -- Sometimes we need to hold two ATCB locks at the same time. To allow us
87 -- to order the locking, each ATCB is given a unique serial number. If one
88 -- needs to hold locks on several ATCBs at once, the locks with lower
89 -- serial numbers must be locked first.
91 -- We don't always need to check the serial numbers, since the serial
92 -- numbers are assigned sequentially, and so:
94 -- . The parent of a task always has a lower serial number.
95 -- . The activator of a task always has a lower serial number.
96 -- . The environment task has a lower serial number than any other task.
97 -- . If the activator of a task is different from the task's parent,
98 -- the parent always has a lower serial number than the activator.
100 ---------------------------------
101 -- Task_Id related definitions --
102 ---------------------------------
104 type Ada_Task_Control_Block;
106 type Task_Id is access all Ada_Task_Control_Block;
107 for Task_Id'Size use System.Task_Primitives.Task_Address_Size;
109 Null_Task : constant Task_Id;
111 type Task_List is array (Positive range <>) of Task_Id;
113 function Self return Task_Id;
114 pragma Inline (Self);
115 -- This is the compiler interface version of this function. Do not call
116 -- from the run-time system.
118 function To_Task_Id is
119 new Ada.Unchecked_Conversion
120 (System.Task_Primitives.Task_Address, Task_Id);
121 function To_Address is
122 new Ada.Unchecked_Conversion
123 (Task_Id, System.Task_Primitives.Task_Address);
125 -----------------------
126 -- Enumeration types --
127 -----------------------
131 -- Task has been created but has not been activated.
132 -- It cannot be executing.
135 -- For all states from here down, the task has been activated.
136 -- For all states from here down, except for Terminated, the task
138 -- Activator = null iff it has not yet completed activating.
140 -- For all states from here down,
141 -- the task has been activated, and may be executing.
144 -- Task is not blocked for any reason known to Ada.
145 -- (It may be waiting for a mutex, though.)
146 -- It is conceptually "executing" in normal mode.
149 -- The task is terminated, in the sense of ARM 9.3 (5).
150 -- Any dependents that were waiting on terminate
151 -- alternatives have been awakened and have terminated themselves.
154 -- Task is waiting for created tasks to complete activation
157 -- Task is waiting on an accept or selective wait statement
160 -- Task is waiting on an entry call
163 -- Task is waiting to start the abortable part of an
164 -- asynchronous select statement.
167 -- Task is waiting on a select statement with only a delay
170 Master_Completion_Sleep,
171 -- Master completion has two phases.
172 -- In Phase 1 the task is sleeping in Complete_Master
173 -- having completed a master within itself,
174 -- and is waiting for the tasks dependent on that master to become
175 -- terminated or waiting on a terminate Phase.
177 Master_Phase_2_Sleep,
178 -- In Phase 2 the task is sleeping in Complete_Master
179 -- waiting for tasks on terminate alternatives to finish
182 -- The following are special uses of sleep, for server tasks
183 -- within the run-time system.
185 Interrupt_Server_Idle_Sleep,
186 Interrupt_Server_Blocked_Interrupt_Sleep,
191 -- The task has been held by Asynchronous_Task_Control.Hold_Task
193 Interrupt_Server_Blocked_On_Event_Flag
194 -- The task has been blocked on a system call waiting for a
195 -- completion event/signal to occur.
199 (Simple_Call, Conditional_Call, Asynchronous_Call, Timed_Call);
201 type Select_Modes is (Simple_Mode, Else_Mode, Terminate_Mode, Delay_Mode);
203 subtype Delay_Modes is Integer;
205 -------------------------------
206 -- Entry related definitions --
207 -------------------------------
209 Null_Entry : constant := 0;
211 Max_Entry : constant := Integer'Last;
213 Interrupt_Entry : constant := -2;
215 Cancelled_Entry : constant := -1;
217 type Entry_Index is range Interrupt_Entry .. Max_Entry;
219 Null_Task_Entry : constant := Null_Entry;
221 Max_Task_Entry : constant := Max_Entry;
223 type Task_Entry_Index is new Entry_Index
224 range Null_Task_Entry .. Max_Task_Entry;
226 type Entry_Call_Record;
228 type Entry_Call_Link is access all Entry_Call_Record;
230 type Entry_Queue is record
231 Head : Entry_Call_Link;
232 Tail : Entry_Call_Link;
235 type Task_Entry_Queue_Array is
236 array (Task_Entry_Index range <>) of Entry_Queue;
238 -- A data structure which contains the string names of entries and entry
241 type String_Access is access all String;
243 type Entry_Names_Array is
244 array (Entry_Index range <>) of String_Access;
246 type Entry_Names_Array_Access is access all Entry_Names_Array;
248 procedure Free_Entry_Names_Array (Obj : in out Entry_Names_Array);
249 -- Deallocate all string names contained in an entry names array
251 ----------------------------------
252 -- Entry_Call_Record definition --
253 ----------------------------------
255 type Entry_Call_State is
257 -- the call is not abortable, and never can be
260 -- the call is not abortable, but may become so
263 -- the call is not abortable, but once was
266 -- the call is abortable
269 -- the call has been completed
272 -- the call was asynchronous, and was cancelled
275 -- Never_Abortable is used for calls that are made in a abort
276 -- deferred region (see ARM 9.8(5-11), 9.8 (20)).
277 -- Such a call is never abortable.
279 -- The Was_ vs. Not_Yet_ distinction is needed to decide whether it
280 -- is OK to advance into the abortable part of an async. select stmt.
281 -- That is allowed iff the mode is Now_ or Was_.
283 -- Done indicates the call has been completed, without cancellation,
284 -- or no call has been made yet at this ATC nesting level,
285 -- and so aborting the call is no longer an issue.
286 -- Completion of the call does not necessarily indicate "success";
287 -- the call may be returning an exception if Exception_To_Raise is
290 -- Cancelled indicates the call was cancelled,
291 -- and so aborting the call is no longer an issue.
293 -- The call is on an entry queue unless
294 -- State >= Done, in which case it may or may not be still Onqueue.
296 -- Please do not modify the order of the values, without checking
297 -- all uses of this type. We rely on partial "monotonicity" of
298 -- Entry_Call_Record.State to avoid locking when we access this
299 -- value for certain tests. In particular:
301 -- 1) Once State >= Done, we can rely that the call has been
302 -- completed. If State >= Done, it will not
303 -- change until the task does another entry call at this level.
305 -- 2) Once State >= Was_Abortable, we can rely that the call has
306 -- been queued abortably at least once, and so the check for
307 -- whether it is OK to advance to the abortable part of an
308 -- async. select statement does not need to lock anything.
310 type Restricted_Entry_Call_Record is record
316 State : Entry_Call_State;
317 pragma Atomic (State);
318 -- Indicates part of the state of the call.
320 -- Protection: If the call is not on a queue, it should only be
321 -- accessed by Self, and Self does not need any lock to modify this
324 -- Once the call is on a queue, the value should be something other
325 -- than Done unless it is cancelled, and access is controller by the
326 -- "server" of the queue -- i.e., the lock of Checked_To_Protection
327 -- (Call_Target) if the call record is on the queue of a PO, or the
328 -- lock of Called_Target if the call is on the queue of a task. See
329 -- comments on type declaration for more details.
331 Uninterpreted_Data : System.Address;
332 -- Data passed by the compiler
334 Exception_To_Raise : Ada.Exceptions.Exception_Id;
335 -- The exception to raise once this call has been completed without
338 pragma Suppress_Initialization (Restricted_Entry_Call_Record);
340 -------------------------------------------
341 -- Task termination procedure definition --
342 -------------------------------------------
344 -- We need to redefine here these types (already defined in
345 -- Ada.Task_Termination) for avoiding circular dependencies.
347 type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception);
348 -- Possible causes for task termination:
350 -- Normal means that the task terminates due to completing the
351 -- last sentence of its body, or as a result of waiting on a
352 -- terminate alternative.
354 -- Abnormal means that the task terminates because it is being aborted
356 -- handled_Exception means that the task terminates because of exception
357 -- raised by the execution of its task_body.
359 type Termination_Handler is access protected procedure
360 (Cause : Cause_Of_Termination;
362 X : Ada.Exceptions.Exception_Occurrence);
363 -- Used to represent protected procedures to be executed when task
366 ------------------------------------
367 -- Task related other definitions --
368 ------------------------------------
370 type Activation_Chain is limited private;
371 -- Linked list of to-be-activated tasks, linked through
372 -- Activation_Link. The order of tasks on the list is irrelevant, because
373 -- the priority rules will ensure that they actually start activating in
376 type Activation_Chain_Access is access all Activation_Chain;
378 type Task_Procedure_Access is access procedure (Arg : System.Address);
380 type Access_Boolean is access all Boolean;
382 function Detect_Blocking return Boolean;
383 pragma Inline (Detect_Blocking);
384 -- Return whether the Detect_Blocking pragma is enabled
386 function Storage_Size (T : Task_Id) return System.Parameters.Size_Type;
387 -- Retrieve from the TCB of the task the allocated size of its stack,
388 -- either the system default or the size specified by a pragma. This
389 -- is in general a non-static value that can depend on discriminants
392 ----------------------------------------------
393 -- Ada_Task_Control_Block (ATCB) definition --
394 ----------------------------------------------
396 -- Notes on protection (synchronization) of TRTS data structures
398 -- Any field of the TCB can be written by the activator of a task when the
399 -- task is created, since no other task can access the new task's
400 -- state until creation is complete.
402 -- The protection for each field is described in a comment starting with
405 -- When a lock is used to protect an ATCB field, this lock is simply named
407 -- Some protection is described in terms of tasks related to the
408 -- ATCB being protected. These are:
410 -- Self: The task which is controlled by this ATCB
411 -- Acceptor: A task accepting a call from Self
412 -- Caller: A task calling an entry of Self
413 -- Parent: The task executing the master on which Self depends
414 -- Dependent: A task dependent on Self
415 -- Activator: The task that created Self and initiated its activation
416 -- Created: A task created and activated by Self
418 -- Note: The order of the fields is important to implement efficiently
419 -- tasking support under gdb.
420 -- Currently gdb relies on the order of the State, Parent, Base_Priority,
421 -- Task_Image, Task_Image_Len, Call and LL fields.
423 -------------------------
424 -- Common ATCB section --
425 -------------------------
427 -- Section used by all GNARL implementations (regular and restricted)
429 type Common_ATCB is record
431 pragma Atomic (State);
432 -- Encodes some basic information about the state of a task,
433 -- including whether it has been activated, whether it is sleeping,
434 -- and whether it is terminated.
436 -- Protection: Self.L
439 -- The task on which this task depends.
440 -- See also Master_Level and Master_Within.
442 Base_Priority : System.Any_Priority;
443 -- Base priority, not changed during entry calls, only changed
444 -- via dynamic priorities package.
446 -- Protection: Only written by Self, accessed by anyone
448 Current_Priority : System.Any_Priority;
449 -- Active priority, except that the effects of protected object
450 -- priority ceilings are not reflected. This only reflects explicit
451 -- priority changes and priority inherited through task activation
454 -- Ada 95 notes: In Ada 95, this field will be transferred to the
455 -- Priority field of an Entry_Calls component when an entry call is
456 -- initiated. The Priority of the Entry_Calls component will not change
457 -- for the duration of the call. The accepting task can use it to boost
458 -- its own priority without fear of its changing in the meantime.
460 -- This can safely be used in the priority ordering of entry queues.
461 -- Once a call is queued, its priority does not change.
463 -- Since an entry call cannot be made while executing a protected
464 -- action, the priority of a task will never reflect a priority ceiling
465 -- change at the point of an entry call.
467 -- Protection: Only written by Self, and only accessed when Acceptor
468 -- accepts an entry or when Created activates, at which points Self is
471 Protected_Action_Nesting : Natural;
472 pragma Atomic (Protected_Action_Nesting);
473 -- The dynamic level of protected action nesting for this task. This
474 -- field is needed for checking whether potentially blocking operations
475 -- are invoked from protected actions. pragma Atomic is used because it
476 -- can be read/written from protected interrupt handlers.
478 Task_Image : String (1 .. System.Parameters.Max_Task_Image_Length);
479 -- Hold a string that provides a readable id for task, built from the
480 -- variable of which it is a value or component.
482 Task_Image_Len : Natural;
483 -- Actual length of Task_Image
485 Call : Entry_Call_Link;
486 -- The entry call that has been accepted by this task.
488 -- Protection: Self.L. Self will modify this field when Self.Accepting
489 -- is False, and will not need the mutex to do so. Once a task sets
490 -- Pending_ATC_Level = 0, no other task can access this field.
492 LL : aliased Task_Primitives.Private_Data;
493 -- Control block used by the underlying low-level tasking service
496 -- Protection: This is used only by the GNULLI implementation, which
497 -- takes care of all of its synchronization.
499 Task_Arg : System.Address;
500 -- The argument to task procedure. Provide a handle for discriminant
503 -- Protection: Part of the synchronization between Self and Activator.
504 -- Activator writes it, once, before Self starts executing. Thereafter,
505 -- Self only reads it.
507 Task_Alternate_Stack : System.Address;
508 -- The address of the alternate signal stack for this task, if any
510 -- Protection: Only accessed by Self
512 Task_Entry_Point : Task_Procedure_Access;
513 -- Information needed to call the procedure containing the code for
514 -- the body of this task.
516 -- Protection: Part of the synchronization between Self and Activator.
517 -- Activator writes it, once, before Self starts executing. Self reads
518 -- it, once, as part of its execution.
520 Compiler_Data : System.Soft_Links.TSD;
521 -- Task-specific data needed by the compiler to store per-task
524 -- Protection: Only accessed by Self
526 All_Tasks_Link : Task_Id;
527 -- Used to link this task to the list of all tasks in the system
529 -- Protection: RTS_Lock
531 Activation_Link : Task_Id;
532 -- Used to link this task to a list of tasks to be activated
534 -- Protection: Only used by Activator
537 -- The task that created this task, either by declaring it as a task
538 -- object or by executing a task allocator. The value is null iff Self
539 -- has completed activation.
541 -- Protection: Set by Activator before Self is activated, and only read
542 -- and modified by Self after that.
544 Wait_Count : Integer;
545 -- This count is used by a task that is waiting for other tasks. At all
546 -- other times, the value should be zero. It is used differently in
547 -- several different states. Since a task cannot be in more than one of
548 -- these states at the same time, a single counter suffices.
550 -- Protection: Self.L
554 -- This is the number of tasks that this task is activating, i.e. the
555 -- children that have started activation but have not completed it.
557 -- Protection: Self.L and Created.L. Both mutexes must be locked, since
558 -- Self.Activation_Count and Created.State must be synchronized.
560 -- Master_Completion_Sleep (phase 1)
562 -- This is the number dependent tasks of a master being completed by
563 -- Self that are not activated, not terminated, and not waiting on a
564 -- terminate alternative.
566 -- Master_Completion_2_Sleep (phase 2)
568 -- This is the count of tasks dependent on a master being completed by
569 -- Self which are waiting on a terminate alternative.
571 Elaborated : Access_Boolean;
572 -- Pointer to a flag indicating that this task's body has been
573 -- elaborated. The flag is created and managed by the
574 -- compiler-generated code.
576 -- Protection: The field itself is only accessed by Activator. The flag
577 -- that it points to is updated by Master and read by Activator; access
578 -- is assumed to be atomic.
580 Activation_Failed : Boolean;
581 -- Set to True if activation of a chain of tasks fails,
582 -- so that the activator should raise Tasking_Error.
584 Task_Info : System.Task_Info.Task_Info_Type;
585 -- System-specific attributes of the task as specified by the
588 Analyzer : System.Stack_Usage.Stack_Analyzer;
589 -- For storing informations used to measure the stack usage
591 Global_Task_Lock_Nesting : Natural;
592 -- This is the current nesting level of calls to
593 -- System.Tasking.Initialization.Lock_Task. This allows a task to call
594 -- Lock_Task multiple times without deadlocking. A task only locks
595 -- Global_Task_Lock when its Global_Task_Lock_Nesting goes from 0 to 1,
596 -- and only unlocked when it goes from 1 to 0.
598 -- Protection: Only accessed by Self
600 Fall_Back_Handler : Termination_Handler;
601 -- This is the fall-back handler that applies to the dependent tasks of
604 -- Protection: Self.L
606 Specific_Handler : Termination_Handler;
607 -- This is the specific handler that applies only to this task, and not
608 -- any of its dependent tasks.
610 -- Protection: Self.L
613 ---------------------------------------
614 -- Restricted_Ada_Task_Control_Block --
615 ---------------------------------------
617 -- This type should only be used by the restricted GNARLI and by restricted
618 -- GNULL implementations to allocate an ATCB (see System.Task_Primitives.
619 -- Operations.New_ATCB) that will take significantly less memory.
621 -- Note that the restricted GNARLI should only access fields that are
622 -- present in the Restricted_Ada_Task_Control_Block structure.
624 type Restricted_Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is
626 Common : Common_ATCB;
627 -- The common part between various tasking implementations
629 Entry_Call : aliased Restricted_Entry_Call_Record;
630 -- Protection: This field is used on entry call "queues" associated
631 -- with protected objects, and is protected by the protected object
634 pragma Suppress_Initialization (Restricted_Ada_Task_Control_Block);
636 Interrupt_Manager_ID : Task_Id;
637 -- This task ID is declared here to break circular dependencies.
638 -- Also declare Interrupt_Manager_ID after Task_Id is known, to avoid
639 -- generating unneeded finalization code.
641 -----------------------
642 -- List of all Tasks --
643 -----------------------
645 All_Tasks_List : Task_Id;
646 -- Global linked list of all tasks
648 ------------------------------------------
649 -- Regular (non restricted) definitions --
650 ------------------------------------------
652 --------------------------------
653 -- Master Related Definitions --
654 --------------------------------
656 subtype Master_Level is Integer;
657 subtype Master_ID is Master_Level;
659 -- Normally, a task starts out with internal master nesting level one
660 -- larger than external master nesting level. It is incremented to one by
661 -- Enter_Master, which is called in the task body only if the compiler
662 -- thinks the task may have dependent tasks. It is set to 1 for the
663 -- environment task, the level 2 is reserved for server tasks of the
664 -- run-time system (the so called "independent tasks"), and the level 3 is
665 -- for the library level tasks. Foreign threads which are detected by
666 -- the run-time have a level of 0, allowing these tasks to be easily
667 -- distinguished if needed.
669 Foreign_Task_Level : constant Master_Level := 0;
670 Environment_Task_Level : constant Master_Level := 1;
671 Independent_Task_Level : constant Master_Level := 2;
672 Library_Task_Level : constant Master_Level := 3;
674 ------------------------------
675 -- Task size, priority info --
676 ------------------------------
678 Unspecified_Priority : constant Integer := System.Priority'First - 1;
680 Priority_Not_Boosted : constant Integer := System.Priority'First - 1;
681 -- Definition of Priority actually has to come from the RTS configuration
683 subtype Rendezvous_Priority is Integer
684 range Priority_Not_Boosted .. System.Any_Priority'Last;
686 ------------------------------------
687 -- Rendezvous related definitions --
688 ------------------------------------
690 No_Rendezvous : constant := 0;
692 Max_Select : constant Integer := Integer'Last;
695 subtype Select_Index is Integer range No_Rendezvous .. Max_Select;
696 -- type Select_Index is range No_Rendezvous .. Max_Select;
698 subtype Positive_Select_Index is
699 Select_Index range 1 .. Select_Index'Last;
701 type Accept_Alternative is record
703 S : Task_Entry_Index;
707 array (Positive_Select_Index range <>) of Accept_Alternative;
709 type Accept_List_Access is access constant Accept_List;
711 -----------------------------------
712 -- ATC_Level related definitions --
713 -----------------------------------
715 Max_ATC_Nesting : constant Natural := 20;
717 subtype ATC_Level_Base is Integer range 0 .. Max_ATC_Nesting;
719 ATC_Level_Infinity : constant ATC_Level_Base := ATC_Level_Base'Last;
721 subtype ATC_Level is ATC_Level_Base range 0 .. ATC_Level_Base'Last - 1;
723 subtype ATC_Level_Index is ATC_Level range 1 .. ATC_Level'Last;
725 ----------------------------------
726 -- Entry_Call_Record definition --
727 ----------------------------------
729 type Entry_Call_Record is record
735 State : Entry_Call_State;
736 pragma Atomic (State);
737 -- Indicates part of the state of the call
739 -- Protection: If the call is not on a queue, it should only be
740 -- accessed by Self, and Self does not need any lock to modify this
741 -- field. Once the call is on a queue, the value should be something
742 -- other than Done unless it is cancelled, and access is controller by
743 -- the "server" of the queue -- i.e., the lock of Checked_To_Protection
744 -- (Call_Target) if the call record is on the queue of a PO, or the
745 -- lock of Called_Target if the call is on the queue of a task. See
746 -- comments on type declaration for more details.
748 Uninterpreted_Data : System.Address;
749 -- Data passed by the compiler
751 Exception_To_Raise : Ada.Exceptions.Exception_Id;
752 -- The exception to raise once this call has been completed without
755 Prev : Entry_Call_Link;
757 Next : Entry_Call_Link;
760 -- One of Self and Level are redundant in this implementation, since
761 -- each Entry_Call_Record is at Self.Entry_Calls (Level). Since we must
762 -- have access to the entry call record to be reading this, we could
763 -- get Self from Level, or Level from Self. However, this requires
764 -- non-portable address arithmetic.
768 Prio : System.Any_Priority;
770 -- The above fields are those that there may be some hope of packing.
771 -- They are gathered together to allow for compilers that lay records
772 -- out contiguously, to allow for such packing.
774 Called_Task : Task_Id;
775 pragma Atomic (Called_Task);
776 -- Use for task entry calls. The value is null if the call record is
777 -- not in use. Conversely, unless State is Done and Onqueue is false,
778 -- Called_Task points to an ATCB.
780 -- Protection: Called_Task.L
782 Called_PO : System.Address;
783 pragma Atomic (Called_PO);
784 -- Similar to Called_Task but for protected objects
786 -- Note that the previous implementation tried to merge both
787 -- Called_Task and Called_PO but this ended up in many unexpected
788 -- complications (e.g having to add a magic number in the ATCB, which
789 -- caused gdb lots of confusion) with no real gain since the
790 -- Lock_Server implementation still need to loop around chasing for
791 -- pointer changes even with a single pointer.
793 Acceptor_Prev_Call : Entry_Call_Link;
794 -- For task entry calls only
796 Acceptor_Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted;
797 -- For task entry calls only. The priority of the most recent prior
798 -- call being serviced. For protected entry calls, this function should
799 -- be performed by GNULLI ceiling locking.
801 Cancellation_Attempted : Boolean := False;
802 pragma Atomic (Cancellation_Attempted);
803 -- Cancellation of the call has been attempted.
804 -- Consider merging this into State???
806 With_Abort : Boolean := False;
807 -- Tell caller whether the call may be aborted
808 -- ??? consider merging this with Was_Abortable state
810 Needs_Requeue : Boolean := False;
811 -- Temporary to tell acceptor of task entry call that
812 -- Exceptional_Complete_Rendezvous needs to do requeue.
815 ------------------------------------
816 -- Task related other definitions --
817 ------------------------------------
819 type Access_Address is access all System.Address;
820 -- Anonymous pointer used to implement task attributes (see s-tataat.adb
823 pragma No_Strict_Aliasing (Access_Address);
824 -- This type is used in contexts where aliasing may be an issue (see
825 -- for example s-tataat.adb), so we avoid any incorrect aliasing
828 ----------------------------------------------
829 -- Ada_Task_Control_Block (ATCB) definition --
830 ----------------------------------------------
832 type Entry_Call_Array is array (ATC_Level_Index) of
833 aliased Entry_Call_Record;
835 type Direct_Index is range 0 .. Parameters.Default_Attribute_Count;
836 subtype Direct_Index_Range is Direct_Index range 1 .. Direct_Index'Last;
837 -- Attributes with indices in this range are stored directly in the task
838 -- control block. Such attributes must be Address-sized. Other attributes
839 -- will be held in dynamically allocated records chained off of the task
842 type Direct_Attribute_Element is mod Memory_Size;
843 pragma Atomic (Direct_Attribute_Element);
845 type Direct_Attribute_Array is
846 array (Direct_Index_Range) of aliased Direct_Attribute_Element;
848 type Direct_Index_Vector is mod 2 ** Parameters.Default_Attribute_Count;
849 -- This is a bit-vector type, used to store information about
850 -- the usage of the direct attribute fields.
852 type Task_Serial_Number is mod 2 ** 64;
853 -- Used to give each task a unique serial number
855 type Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is record
856 Common : Common_ATCB;
857 -- The common part between various tasking implementations
859 Entry_Calls : Entry_Call_Array;
860 -- An array of entry calls
862 -- Protection: The elements of this array are on entry call queues
863 -- associated with protected objects or task entries, and are protected
864 -- by the protected object lock or Acceptor.L, respectively.
866 Entry_Names : Entry_Names_Array_Access := null;
867 -- An array of string names which denotes entry [family member] names.
868 -- The structure is indexed by task entry index and contains Entry_Num
871 New_Base_Priority : System.Any_Priority;
872 -- New value for Base_Priority (for dynamic priorities package)
874 -- Protection: Self.L
876 Open_Accepts : Accept_List_Access;
877 -- This points to the Open_Accepts array of accept alternatives passed
878 -- to the RTS by the compiler-generated code to Selective_Wait. It is
879 -- non-null iff this task is ready to accept an entry call.
881 -- Protection: Self.L
883 Chosen_Index : Select_Index;
884 -- The index in Open_Accepts of the entry call accepted by a selective
885 -- wait executed by this task.
887 -- Protection: Written by both Self and Caller. Usually protected by
888 -- Self.L. However, once the selection is known to have been written it
889 -- can be accessed without protection. This happens after Self has
890 -- updated it itself using information from a suspended Caller, or
891 -- after Caller has updated it and awakened Self.
893 Master_of_Task : Master_Level;
894 -- The task executing the master of this task, and the ID of this task's
895 -- master (unique only among masters currently active within Parent).
897 -- Protection: Set by Activator before Self is activated, and read
898 -- after Self is activated.
900 Master_Within : Master_Level;
901 -- The ID of the master currently executing within this task; that is,
902 -- the most deeply nested currently active master.
904 -- Protection: Only written by Self, and only read by Self or by
905 -- dependents when Self is attempting to exit a master. Since Self will
906 -- not write this field until the master is complete, the
907 -- synchronization should be adequate to prevent races.
909 Alive_Count : Integer := 0;
910 -- Number of tasks directly dependent on this task (including itself)
911 -- that are still "alive", i.e. not terminated.
913 -- Protection: Self.L
915 Awake_Count : Integer := 0;
916 -- Number of tasks directly dependent on this task (including itself)
917 -- still "awake", i.e., are not terminated and not waiting on a
918 -- terminate alternative.
920 -- Invariant: Awake_Count <= Alive_Count
922 -- Protection: Self.L
924 -- Beginning of flags
926 Aborting : Boolean := False;
927 pragma Atomic (Aborting);
928 -- Self is in the process of aborting. While set, prevents multiple
929 -- abort signals from being sent by different aborter while abort
930 -- is acted upon. This is essential since an aborter which calls
931 -- Abort_To_Level could set the Pending_ATC_Level to yet a lower level
932 -- (than the current level), may be preempted and would send the
933 -- abort signal when resuming execution. At this point, the abortee
934 -- may have completed abort to the proper level such that the
935 -- signal (and resulting abort exception) are not handled any more.
936 -- In other words, the flag prevents a race between multiple aborters
938 -- Protection: protected by atomic access.
940 ATC_Hack : Boolean := False;
941 pragma Atomic (ATC_Hack);
943 -- Temporary fix, to allow Undefer_Abort to reset Aborting in the
944 -- handler for Abort_Signal that encloses an async. entry call.
945 -- For the longer term, this should be done via code in the
948 Callable : Boolean := True;
949 -- It is OK to call entries of this task
951 Dependents_Aborted : Boolean := False;
952 -- This is set to True by whichever task takes responsibility for
953 -- aborting the dependents of this task.
955 -- Protection: Self.L
957 Interrupt_Entry : Boolean := False;
958 -- Indicates if one or more Interrupt Entries are attached to the task.
959 -- This flag is needed for cleaning up the Interrupt Entry bindings.
961 Pending_Action : Boolean := False;
962 -- Unified flag indicating some action needs to be take when abort
963 -- next becomes undeferred. Currently set if:
964 -- . Pending_Priority_Change is set
965 -- . Pending_ATC_Level is changed
966 -- . Requeue involving POs
967 -- (Abortable field may have changed and the Wait_Until_Abortable
968 -- has to recheck the abortable status of the call.)
969 -- . Exception_To_Raise is non-null
971 -- Protection: Self.L
973 -- This should never be reset back to False outside of the procedure
974 -- Do_Pending_Action, which is called by Undefer_Abort. It should only
975 -- be set to True by Set_Priority and Abort_To_Level.
977 Pending_Priority_Change : Boolean := False;
978 -- Flag to indicate pending priority change (for dynamic priorities
979 -- package). The base priority is updated on the next abort
980 -- completion point (aka. synchronization point).
982 -- Protection: Self.L
984 Terminate_Alternative : Boolean := False;
985 -- Task is accepting Select with Terminate Alternative
987 -- Protection: Self.L
991 -- Beginning of counts
993 ATC_Nesting_Level : ATC_Level := 1;
994 -- The dynamic level of ATC nesting (currently executing nested
995 -- asynchronous select statements) in this task.
997 -- Protection: Self_ID.L. Only Self reads or updates this field.
998 -- Decrementing it deallocates an Entry_Calls component, and care must
999 -- be taken that all references to that component are eliminated before
1000 -- doing the decrement. This in turn will require locking a protected
1001 -- object (for a protected entry call) or the Acceptor's lock (for a
1002 -- task entry call). No other task should attempt to read or modify
1005 Deferral_Level : Natural := 1;
1006 -- This is the number of times that Defer_Abort has been called by
1007 -- this task without a matching Undefer_Abort call. Abortion is only
1008 -- allowed when this zero. It is initially 1, to protect the task at
1011 -- Protection: Only updated by Self; access assumed to be atomic
1013 Pending_ATC_Level : ATC_Level_Base := ATC_Level_Infinity;
1014 -- The ATC level to which this task is currently being aborted. If the
1015 -- value is zero, the entire task has "completed". That may be via
1016 -- abort, exception propagation, or normal exit. If the value is
1017 -- ATC_Level_Infinity, the task is not being aborted to any level. If
1018 -- the value is positive, the task has not completed. This should ONLY
1019 -- be modified by Abort_To_Level and Exit_One_ATC_Level.
1021 -- Protection: Self.L
1023 Serial_Number : Task_Serial_Number;
1024 -- A growing number to provide some way to check locking rules/ordering
1026 Known_Tasks_Index : Integer := -1;
1027 -- Index in the System.Tasking.Debug.Known_Tasks array
1029 User_State : Long_Integer := 0;
1030 -- User-writeable location, for use in debugging tasks; also provides a
1031 -- simple task specific data.
1033 Direct_Attributes : Direct_Attribute_Array;
1034 -- For task attributes that have same size as Address
1036 Is_Defined : Direct_Index_Vector := 0;
1037 -- Bit I is 1 iff Direct_Attributes (I) is defined
1039 Indirect_Attributes : Access_Address;
1040 -- A pointer to chain of records for other attributes that are not
1041 -- address-sized, including all tagged types.
1043 Entry_Queues : Task_Entry_Queue_Array (1 .. Entry_Num);
1044 -- An array of task entry queues
1046 -- Protection: Self.L. Once a task has set Self.Stage to Completing, it
1047 -- has exclusive access to this field.
1050 --------------------
1051 -- Initialization --
1052 --------------------
1054 procedure Initialize;
1055 -- This procedure constitutes the first part of the initialization of the
1056 -- GNARL. This includes creating data structures to make the initial thread
1057 -- into the environment task. The last part of the initialization is done
1058 -- in System.Tasking.Initialization or System.Tasking.Restricted.Stages.
1059 -- All the initializations used to be in Tasking.Initialization, but this
1060 -- is no longer possible with the run time simplification (including
1061 -- optimized PO and the restricted run time) since one cannot rely on
1062 -- System.Tasking.Initialization being present, as was done before.
1064 procedure Initialize_ATCB
1066 Task_Entry_Point : Task_Procedure_Access;
1067 Task_Arg : System.Address;
1069 Elaborated : Access_Boolean;
1070 Base_Priority : System.Any_Priority;
1071 Task_Info : System.Task_Info.Task_Info_Type;
1072 Stack_Size : System.Parameters.Size_Type;
1074 Success : out Boolean);
1075 -- Initialize fields of a TCB and link into global TCB structures Call
1076 -- this only with abort deferred and holding RTS_Lock. Need more
1077 -- documentation, mention T, and describe Success ???
1081 Null_Task : constant Task_Id := null;
1083 type Activation_Chain is limited record
1087 -- Activation_Chain is an in-out parameter of initialization procedures and
1088 -- it must be passed by reference because the init proc may terminate
1089 -- abnormally after creating task components, and these must be properly
1090 -- registered for removal (Expunge_Unactivated_Tasks). The "limited" forces
1091 -- Activation_Chain to be a by-reference type; see RM-6.2(4).