fixing pr42337
[official-gcc.git] / gcc / ada / s-taskin.ads
blob0cc43f386803eea7aab5341389e5cc052f8a09bd
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-2009, 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 ------------------------------------------------------------------------------
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.
37 with Ada.Exceptions;
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
47 pragma Preelaborate;
49 -------------------
50 -- Locking Rules --
51 -------------------
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:
63 -- Lock (X);
64 -- ...
65 -- Lock (Y);
66 -- ...
67 -- Unlock (X);
68 -- ...
69 -- Unlock (Y);
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 -----------------------
129 type Task_States is
130 (Unactivated,
131 -- TCB initialized but not task has not been created.
132 -- It cannot be executing.
134 -- Activating,
135 -- -- ??? Temporarily at end of list for GDB compatibility
136 -- -- Task has been created and is being made Runnable.
138 -- Active states
139 -- For all states from here down, the task has been activated.
140 -- For all states from here down, except for Terminated, the task
141 -- may be executing.
142 -- Activator = null iff it has not yet completed activating.
144 Runnable,
145 -- Task is not blocked for any reason known to Ada.
146 -- (It may be waiting for a mutex, though.)
147 -- It is conceptually "executing" in normal mode.
149 Terminated,
150 -- The task is terminated, in the sense of ARM 9.3 (5).
151 -- Any dependents that were waiting on terminate
152 -- alternatives have been awakened and have terminated themselves.
154 Activator_Sleep,
155 -- Task is waiting for created tasks to complete activation
157 Acceptor_Sleep,
158 -- Task is waiting on an accept or select with terminate
160 -- Acceptor_Delay_Sleep,
161 -- -- ??? Temporarily at end of list for GDB compatibility
162 -- -- Task is waiting on an selective wait statement
164 Entry_Caller_Sleep,
165 -- Task is waiting on an entry call
167 Async_Select_Sleep,
168 -- Task is waiting to start the abortable part of an
169 -- asynchronous select statement.
171 Delay_Sleep,
172 -- Task is waiting on a select statement with only a delay
173 -- alternative open.
175 Master_Completion_Sleep,
176 -- Master completion has two phases.
177 -- In Phase 1 the task is sleeping in Complete_Master
178 -- having completed a master within itself,
179 -- and is waiting for the tasks dependent on that master to become
180 -- terminated or waiting on a terminate Phase.
182 Master_Phase_2_Sleep,
183 -- In Phase 2 the task is sleeping in Complete_Master
184 -- waiting for tasks on terminate alternatives to finish
185 -- terminating.
187 -- The following are special uses of sleep, for server tasks
188 -- within the run-time system.
190 Interrupt_Server_Idle_Sleep,
191 Interrupt_Server_Blocked_Interrupt_Sleep,
192 Timer_Server_Sleep,
193 AST_Server_Sleep,
195 Asynchronous_Hold,
196 -- The task has been held by Asynchronous_Task_Control.Hold_Task
198 Interrupt_Server_Blocked_On_Event_Flag,
199 -- The task has been blocked on a system call waiting for a
200 -- completion event/signal to occur.
202 Activating,
203 -- Task has been created and is being made Runnable
205 Acceptor_Delay_Sleep
206 -- Task is waiting on an selective wait statement
209 type Call_Modes is
210 (Simple_Call, Conditional_Call, Asynchronous_Call, Timed_Call);
212 type Select_Modes is (Simple_Mode, Else_Mode, Terminate_Mode, Delay_Mode);
214 subtype Delay_Modes is Integer;
216 -------------------------------
217 -- Entry related definitions --
218 -------------------------------
220 Null_Entry : constant := 0;
222 Max_Entry : constant := Integer'Last;
224 Interrupt_Entry : constant := -2;
226 Cancelled_Entry : constant := -1;
228 type Entry_Index is range Interrupt_Entry .. Max_Entry;
230 Null_Task_Entry : constant := Null_Entry;
232 Max_Task_Entry : constant := Max_Entry;
234 type Task_Entry_Index is new Entry_Index
235 range Null_Task_Entry .. Max_Task_Entry;
237 type Entry_Call_Record;
239 type Entry_Call_Link is access all Entry_Call_Record;
241 type Entry_Queue is record
242 Head : Entry_Call_Link;
243 Tail : Entry_Call_Link;
244 end record;
246 type Task_Entry_Queue_Array is
247 array (Task_Entry_Index range <>) of Entry_Queue;
249 -- A data structure which contains the string names of entries and entry
250 -- family members.
252 type String_Access is access all String;
254 type Entry_Names_Array is
255 array (Entry_Index range <>) of String_Access;
257 type Entry_Names_Array_Access is access all Entry_Names_Array;
259 procedure Free_Entry_Names_Array (Obj : in out Entry_Names_Array);
260 -- Deallocate all string names contained in an entry names array
262 ----------------------------------
263 -- Entry_Call_Record definition --
264 ----------------------------------
266 type Entry_Call_State is
267 (Never_Abortable,
268 -- the call is not abortable, and never can be
270 Not_Yet_Abortable,
271 -- the call is not abortable, but may become so
273 Was_Abortable,
274 -- the call is not abortable, but once was
276 Now_Abortable,
277 -- the call is abortable
279 Done,
280 -- the call has been completed
282 Cancelled
283 -- the call was asynchronous, and was cancelled
286 -- Never_Abortable is used for calls that are made in a abort
287 -- deferred region (see ARM 9.8(5-11), 9.8 (20)).
288 -- Such a call is never abortable.
290 -- The Was_ vs. Not_Yet_ distinction is needed to decide whether it
291 -- is OK to advance into the abortable part of an async. select stmt.
292 -- That is allowed iff the mode is Now_ or Was_.
294 -- Done indicates the call has been completed, without cancellation,
295 -- or no call has been made yet at this ATC nesting level,
296 -- and so aborting the call is no longer an issue.
297 -- Completion of the call does not necessarily indicate "success";
298 -- the call may be returning an exception if Exception_To_Raise is
299 -- non-null.
301 -- Cancelled indicates the call was cancelled,
302 -- and so aborting the call is no longer an issue.
304 -- The call is on an entry queue unless
305 -- State >= Done, in which case it may or may not be still Onqueue.
307 -- Please do not modify the order of the values, without checking
308 -- all uses of this type. We rely on partial "monotonicity" of
309 -- Entry_Call_Record.State to avoid locking when we access this
310 -- value for certain tests. In particular:
312 -- 1) Once State >= Done, we can rely that the call has been
313 -- completed. If State >= Done, it will not
314 -- change until the task does another entry call at this level.
316 -- 2) Once State >= Was_Abortable, we can rely that the call has
317 -- been queued abortably at least once, and so the check for
318 -- whether it is OK to advance to the abortable part of an
319 -- async. select statement does not need to lock anything.
321 type Restricted_Entry_Call_Record is record
322 Self : Task_Id;
323 -- ID of the caller
325 Mode : Call_Modes;
327 State : Entry_Call_State;
328 pragma Atomic (State);
329 -- Indicates part of the state of the call.
331 -- Protection: If the call is not on a queue, it should only be
332 -- accessed by Self, and Self does not need any lock to modify this
333 -- field.
335 -- Once the call is on a queue, the value should be something other
336 -- than Done unless it is cancelled, and access is controller by the
337 -- "server" of the queue -- i.e., the lock of Checked_To_Protection
338 -- (Call_Target) if the call record is on the queue of a PO, or the
339 -- lock of Called_Target if the call is on the queue of a task. See
340 -- comments on type declaration for more details.
342 Uninterpreted_Data : System.Address;
343 -- Data passed by the compiler
345 Exception_To_Raise : Ada.Exceptions.Exception_Id;
346 -- The exception to raise once this call has been completed without
347 -- being aborted.
348 end record;
349 pragma Suppress_Initialization (Restricted_Entry_Call_Record);
351 -------------------------------------------
352 -- Task termination procedure definition --
353 -------------------------------------------
355 -- We need to redefine here these types (already defined in
356 -- Ada.Task_Termination) for avoiding circular dependencies.
358 type Cause_Of_Termination is (Normal, Abnormal, Unhandled_Exception);
359 -- Possible causes for task termination:
361 -- Normal means that the task terminates due to completing the
362 -- last sentence of its body, or as a result of waiting on a
363 -- terminate alternative.
365 -- Abnormal means that the task terminates because it is being aborted
367 -- handled_Exception means that the task terminates because of exception
368 -- raised by the execution of its task_body.
370 type Termination_Handler is access protected procedure
371 (Cause : Cause_Of_Termination;
372 T : Task_Id;
373 X : Ada.Exceptions.Exception_Occurrence);
374 -- Used to represent protected procedures to be executed when task
375 -- terminates.
377 ------------------------------------
378 -- Task related other definitions --
379 ------------------------------------
381 type Activation_Chain is limited private;
382 -- Linked list of to-be-activated tasks, linked through
383 -- Activation_Link. The order of tasks on the list is irrelevant, because
384 -- the priority rules will ensure that they actually start activating in
385 -- priority order.
387 type Activation_Chain_Access is access all Activation_Chain;
389 type Task_Procedure_Access is access procedure (Arg : System.Address);
391 type Access_Boolean is access all Boolean;
393 function Detect_Blocking return Boolean;
394 pragma Inline (Detect_Blocking);
395 -- Return whether the Detect_Blocking pragma is enabled
397 function Storage_Size (T : Task_Id) return System.Parameters.Size_Type;
398 -- Retrieve from the TCB of the task the allocated size of its stack,
399 -- either the system default or the size specified by a pragma. This
400 -- is in general a non-static value that can depend on discriminants
401 -- of the task.
403 type Bit_Array is array (Integer range <>) of Boolean;
404 pragma Pack (Bit_Array);
406 subtype Debug_Event_Array is Bit_Array (1 .. 16);
408 Global_Task_Debug_Event_Set : Boolean := False;
409 -- Set True when running under debugger control and a task debug
410 -- event signal has been requested.
412 ----------------------------------------------
413 -- Ada_Task_Control_Block (ATCB) definition --
414 ----------------------------------------------
416 -- Notes on protection (synchronization) of TRTS data structures
418 -- Any field of the TCB can be written by the activator of a task when the
419 -- task is created, since no other task can access the new task's
420 -- state until creation is complete.
422 -- The protection for each field is described in a comment starting with
423 -- "Protection:".
425 -- When a lock is used to protect an ATCB field, this lock is simply named
427 -- Some protection is described in terms of tasks related to the
428 -- ATCB being protected. These are:
430 -- Self: The task which is controlled by this ATCB
431 -- Acceptor: A task accepting a call from Self
432 -- Caller: A task calling an entry of Self
433 -- Parent: The task executing the master on which Self depends
434 -- Dependent: A task dependent on Self
435 -- Activator: The task that created Self and initiated its activation
436 -- Created: A task created and activated by Self
438 -- Note: The order of the fields is important to implement efficiently
439 -- tasking support under gdb.
440 -- Currently gdb relies on the order of the State, Parent, Base_Priority,
441 -- Task_Image, Task_Image_Len, Call and LL fields.
443 -------------------------
444 -- Common ATCB section --
445 -------------------------
447 -- Section used by all GNARL implementations (regular and restricted)
449 type Common_ATCB is record
450 State : Task_States;
451 pragma Atomic (State);
452 -- Encodes some basic information about the state of a task,
453 -- including whether it has been activated, whether it is sleeping,
454 -- and whether it is terminated.
456 -- Protection: Self.L
458 Parent : Task_Id;
459 -- The task on which this task depends.
460 -- See also Master_Level and Master_Within.
462 Base_Priority : System.Any_Priority;
463 -- Base priority, not changed during entry calls, only changed
464 -- via dynamic priorities package.
466 -- Protection: Only written by Self, accessed by anyone
468 Current_Priority : System.Any_Priority;
469 -- Active priority, except that the effects of protected object
470 -- priority ceilings are not reflected. This only reflects explicit
471 -- priority changes and priority inherited through task activation
472 -- and rendezvous.
474 -- Ada 95 notes: In Ada 95, this field will be transferred to the
475 -- Priority field of an Entry_Calls component when an entry call is
476 -- initiated. The Priority of the Entry_Calls component will not change
477 -- for the duration of the call. The accepting task can use it to boost
478 -- its own priority without fear of its changing in the meantime.
480 -- This can safely be used in the priority ordering of entry queues.
481 -- Once a call is queued, its priority does not change.
483 -- Since an entry call cannot be made while executing a protected
484 -- action, the priority of a task will never reflect a priority ceiling
485 -- change at the point of an entry call.
487 -- Protection: Only written by Self, and only accessed when Acceptor
488 -- accepts an entry or when Created activates, at which points Self is
489 -- suspended.
491 Protected_Action_Nesting : Natural;
492 pragma Atomic (Protected_Action_Nesting);
493 -- The dynamic level of protected action nesting for this task. This
494 -- field is needed for checking whether potentially blocking operations
495 -- are invoked from protected actions. pragma Atomic is used because it
496 -- can be read/written from protected interrupt handlers.
498 Task_Image : String (1 .. System.Parameters.Max_Task_Image_Length);
499 -- Hold a string that provides a readable id for task, built from the
500 -- variable of which it is a value or component.
502 Task_Image_Len : Natural;
503 -- Actual length of Task_Image
505 Call : Entry_Call_Link;
506 -- The entry call that has been accepted by this task.
508 -- Protection: Self.L. Self will modify this field when Self.Accepting
509 -- is False, and will not need the mutex to do so. Once a task sets
510 -- Pending_ATC_Level = 0, no other task can access this field.
512 LL : aliased Task_Primitives.Private_Data;
513 -- Control block used by the underlying low-level tasking service
514 -- (GNULLI).
516 -- Protection: This is used only by the GNULLI implementation, which
517 -- takes care of all of its synchronization.
519 Task_Arg : System.Address;
520 -- The argument to task procedure. Provide a handle for discriminant
521 -- information.
523 -- Protection: Part of the synchronization between Self and Activator.
524 -- Activator writes it, once, before Self starts executing. Thereafter,
525 -- Self only reads it.
527 Task_Alternate_Stack : System.Address;
528 -- The address of the alternate signal stack for this task, if any
530 -- Protection: Only accessed by Self
532 Task_Entry_Point : Task_Procedure_Access;
533 -- Information needed to call the procedure containing the code for
534 -- the body of this task.
536 -- Protection: Part of the synchronization between Self and Activator.
537 -- Activator writes it, once, before Self starts executing. Self reads
538 -- it, once, as part of its execution.
540 Compiler_Data : System.Soft_Links.TSD;
541 -- Task-specific data needed by the compiler to store per-task
542 -- structures.
544 -- Protection: Only accessed by Self
546 All_Tasks_Link : Task_Id;
547 -- Used to link this task to the list of all tasks in the system
549 -- Protection: RTS_Lock
551 Activation_Link : Task_Id;
552 -- Used to link this task to a list of tasks to be activated
554 -- Protection: Only used by Activator
556 Activator : Task_Id;
557 -- The task that created this task, either by declaring it as a task
558 -- object or by executing a task allocator. The value is null iff Self
559 -- has completed activation.
561 -- Protection: Set by Activator before Self is activated, and only read
562 -- and modified by Self after that.
564 Wait_Count : Integer;
565 -- This count is used by a task that is waiting for other tasks. At all
566 -- other times, the value should be zero. It is used differently in
567 -- several different states. Since a task cannot be in more than one of
568 -- these states at the same time, a single counter suffices.
570 -- Protection: Self.L
572 -- Activator_Sleep
574 -- This is the number of tasks that this task is activating, i.e. the
575 -- children that have started activation but have not completed it.
577 -- Protection: Self.L and Created.L. Both mutexes must be locked, since
578 -- Self.Activation_Count and Created.State must be synchronized.
580 -- Master_Completion_Sleep (phase 1)
582 -- This is the number dependent tasks of a master being completed by
583 -- Self that are not activated, not terminated, and not waiting on a
584 -- terminate alternative.
586 -- Master_Completion_2_Sleep (phase 2)
588 -- This is the count of tasks dependent on a master being completed by
589 -- Self which are waiting on a terminate alternative.
591 Elaborated : Access_Boolean;
592 -- Pointer to a flag indicating that this task's body has been
593 -- elaborated. The flag is created and managed by the
594 -- compiler-generated code.
596 -- Protection: The field itself is only accessed by Activator. The flag
597 -- that it points to is updated by Master and read by Activator; access
598 -- is assumed to be atomic.
600 Activation_Failed : Boolean;
601 -- Set to True if activation of a chain of tasks fails,
602 -- so that the activator should raise Tasking_Error.
604 Task_Info : System.Task_Info.Task_Info_Type;
605 -- System-specific attributes of the task as specified by the
606 -- Task_Info pragma.
608 Analyzer : System.Stack_Usage.Stack_Analyzer;
609 -- For storing informations used to measure the stack usage
611 Global_Task_Lock_Nesting : Natural;
612 -- This is the current nesting level of calls to
613 -- System.Tasking.Initialization.Lock_Task. This allows a task to call
614 -- Lock_Task multiple times without deadlocking. A task only locks
615 -- Global_Task_Lock when its Global_Task_Lock_Nesting goes from 0 to 1,
616 -- and only unlocked when it goes from 1 to 0.
618 -- Protection: Only accessed by Self
620 Fall_Back_Handler : Termination_Handler;
621 -- This is the fall-back handler that applies to the dependent tasks of
622 -- the task.
624 -- Protection: Self.L
626 Specific_Handler : Termination_Handler;
627 -- This is the specific handler that applies only to this task, and not
628 -- any of its dependent tasks.
630 -- Protection: Self.L
632 Debug_Events : Debug_Event_Array;
633 -- Word length array of per task debug events, of which 11 kinds are
634 -- currently defined in System.Tasking.Debugging package.
635 end record;
637 ---------------------------------------
638 -- Restricted_Ada_Task_Control_Block --
639 ---------------------------------------
641 -- This type should only be used by the restricted GNARLI and by restricted
642 -- GNULL implementations to allocate an ATCB (see System.Task_Primitives.
643 -- Operations.New_ATCB) that will take significantly less memory.
645 -- Note that the restricted GNARLI should only access fields that are
646 -- present in the Restricted_Ada_Task_Control_Block structure.
648 type Restricted_Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is
649 record
650 Common : Common_ATCB;
651 -- The common part between various tasking implementations
653 Entry_Call : aliased Restricted_Entry_Call_Record;
654 -- Protection: This field is used on entry call "queues" associated
655 -- with protected objects, and is protected by the protected object
656 -- lock.
657 end record;
658 pragma Suppress_Initialization (Restricted_Ada_Task_Control_Block);
660 Interrupt_Manager_ID : Task_Id;
661 -- This task ID is declared here to break circular dependencies.
662 -- Also declare Interrupt_Manager_ID after Task_Id is known, to avoid
663 -- generating unneeded finalization code.
665 -----------------------
666 -- List of all Tasks --
667 -----------------------
669 All_Tasks_List : Task_Id;
670 -- Global linked list of all tasks
672 ------------------------------------------
673 -- Regular (non restricted) definitions --
674 ------------------------------------------
676 --------------------------------
677 -- Master Related Definitions --
678 --------------------------------
680 subtype Master_Level is Integer;
681 subtype Master_ID is Master_Level;
683 -- Normally, a task starts out with internal master nesting level one
684 -- larger than external master nesting level. It is incremented to one by
685 -- Enter_Master, which is called in the task body only if the compiler
686 -- thinks the task may have dependent tasks. It is set to 1 for the
687 -- environment task, the level 2 is reserved for server tasks of the
688 -- run-time system (the so called "independent tasks"), and the level 3 is
689 -- for the library level tasks. Foreign threads which are detected by
690 -- the run-time have a level of 0, allowing these tasks to be easily
691 -- distinguished if needed.
693 Foreign_Task_Level : constant Master_Level := 0;
694 Environment_Task_Level : constant Master_Level := 1;
695 Independent_Task_Level : constant Master_Level := 2;
696 Library_Task_Level : constant Master_Level := 3;
698 ------------------------------
699 -- Task size, priority info --
700 ------------------------------
702 Unspecified_Priority : constant Integer := System.Priority'First - 1;
704 Priority_Not_Boosted : constant Integer := System.Priority'First - 1;
705 -- Definition of Priority actually has to come from the RTS configuration
707 subtype Rendezvous_Priority is Integer
708 range Priority_Not_Boosted .. System.Any_Priority'Last;
710 ------------------------------------
711 -- Rendezvous related definitions --
712 ------------------------------------
714 No_Rendezvous : constant := 0;
716 Max_Select : constant Integer := Integer'Last;
717 -- RTS-defined
719 subtype Select_Index is Integer range No_Rendezvous .. Max_Select;
720 -- type Select_Index is range No_Rendezvous .. Max_Select;
722 subtype Positive_Select_Index is
723 Select_Index range 1 .. Select_Index'Last;
725 type Accept_Alternative is record
726 Null_Body : Boolean;
727 S : Task_Entry_Index;
728 end record;
730 type Accept_List is
731 array (Positive_Select_Index range <>) of Accept_Alternative;
733 type Accept_List_Access is access constant Accept_List;
735 -----------------------------------
736 -- ATC_Level related definitions --
737 -----------------------------------
739 Max_ATC_Nesting : constant Natural := 20;
741 subtype ATC_Level_Base is Integer range 0 .. Max_ATC_Nesting;
743 ATC_Level_Infinity : constant ATC_Level_Base := ATC_Level_Base'Last;
745 subtype ATC_Level is ATC_Level_Base range 0 .. ATC_Level_Base'Last - 1;
747 subtype ATC_Level_Index is ATC_Level range 1 .. ATC_Level'Last;
749 ----------------------------------
750 -- Entry_Call_Record definition --
751 ----------------------------------
753 type Entry_Call_Record is record
754 Self : Task_Id;
755 -- ID of the caller
757 Mode : Call_Modes;
759 State : Entry_Call_State;
760 pragma Atomic (State);
761 -- Indicates part of the state of the call
763 -- Protection: If the call is not on a queue, it should only be
764 -- accessed by Self, and Self does not need any lock to modify this
765 -- field. Once the call is on a queue, the value should be something
766 -- other than Done unless it is cancelled, and access is controller by
767 -- the "server" of the queue -- i.e., the lock of Checked_To_Protection
768 -- (Call_Target) if the call record is on the queue of a PO, or the
769 -- lock of Called_Target if the call is on the queue of a task. See
770 -- comments on type declaration for more details.
772 Uninterpreted_Data : System.Address;
773 -- Data passed by the compiler
775 Exception_To_Raise : Ada.Exceptions.Exception_Id;
776 -- The exception to raise once this call has been completed without
777 -- being aborted.
779 Prev : Entry_Call_Link;
781 Next : Entry_Call_Link;
783 Level : ATC_Level;
784 -- One of Self and Level are redundant in this implementation, since
785 -- each Entry_Call_Record is at Self.Entry_Calls (Level). Since we must
786 -- have access to the entry call record to be reading this, we could
787 -- get Self from Level, or Level from Self. However, this requires
788 -- non-portable address arithmetic.
790 E : Entry_Index;
792 Prio : System.Any_Priority;
794 -- The above fields are those that there may be some hope of packing.
795 -- They are gathered together to allow for compilers that lay records
796 -- out contiguously, to allow for such packing.
798 Called_Task : Task_Id;
799 pragma Atomic (Called_Task);
800 -- Use for task entry calls. The value is null if the call record is
801 -- not in use. Conversely, unless State is Done and Onqueue is false,
802 -- Called_Task points to an ATCB.
804 -- Protection: Called_Task.L
806 Called_PO : System.Address;
807 pragma Atomic (Called_PO);
808 -- Similar to Called_Task but for protected objects
810 -- Note that the previous implementation tried to merge both
811 -- Called_Task and Called_PO but this ended up in many unexpected
812 -- complications (e.g having to add a magic number in the ATCB, which
813 -- caused gdb lots of confusion) with no real gain since the
814 -- Lock_Server implementation still need to loop around chasing for
815 -- pointer changes even with a single pointer.
817 Acceptor_Prev_Call : Entry_Call_Link;
818 -- For task entry calls only
820 Acceptor_Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted;
821 -- For task entry calls only. The priority of the most recent prior
822 -- call being serviced. For protected entry calls, this function should
823 -- be performed by GNULLI ceiling locking.
825 Cancellation_Attempted : Boolean := False;
826 pragma Atomic (Cancellation_Attempted);
827 -- Cancellation of the call has been attempted.
828 -- Consider merging this into State???
830 With_Abort : Boolean := False;
831 -- Tell caller whether the call may be aborted
832 -- ??? consider merging this with Was_Abortable state
834 Needs_Requeue : Boolean := False;
835 -- Temporary to tell acceptor of task entry call that
836 -- Exceptional_Complete_Rendezvous needs to do requeue.
837 end record;
839 ------------------------------------
840 -- Task related other definitions --
841 ------------------------------------
843 type Access_Address is access all System.Address;
844 -- Anonymous pointer used to implement task attributes (see s-tataat.adb
845 -- and a-tasatt.adb)
847 pragma No_Strict_Aliasing (Access_Address);
848 -- This type is used in contexts where aliasing may be an issue (see
849 -- for example s-tataat.adb), so we avoid any incorrect aliasing
850 -- assumptions.
852 ----------------------------------------------
853 -- Ada_Task_Control_Block (ATCB) definition --
854 ----------------------------------------------
856 type Entry_Call_Array is array (ATC_Level_Index) of
857 aliased Entry_Call_Record;
859 type Direct_Index is range 0 .. Parameters.Default_Attribute_Count;
860 subtype Direct_Index_Range is Direct_Index range 1 .. Direct_Index'Last;
861 -- Attributes with indices in this range are stored directly in the task
862 -- control block. Such attributes must be Address-sized. Other attributes
863 -- will be held in dynamically allocated records chained off of the task
864 -- control block.
866 type Direct_Attribute_Element is mod Memory_Size;
867 pragma Atomic (Direct_Attribute_Element);
869 type Direct_Attribute_Array is
870 array (Direct_Index_Range) of aliased Direct_Attribute_Element;
872 type Direct_Index_Vector is mod 2 ** Parameters.Default_Attribute_Count;
873 -- This is a bit-vector type, used to store information about
874 -- the usage of the direct attribute fields.
876 type Task_Serial_Number is mod 2 ** 64;
877 -- Used to give each task a unique serial number
879 type Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is record
880 Common : Common_ATCB;
881 -- The common part between various tasking implementations
883 Entry_Calls : Entry_Call_Array;
884 -- An array of entry calls
886 -- Protection: The elements of this array are on entry call queues
887 -- associated with protected objects or task entries, and are protected
888 -- by the protected object lock or Acceptor.L, respectively.
890 Entry_Names : Entry_Names_Array_Access := null;
891 -- An array of string names which denotes entry [family member] names.
892 -- The structure is indexed by task entry index and contains Entry_Num
893 -- components.
895 New_Base_Priority : System.Any_Priority;
896 -- New value for Base_Priority (for dynamic priorities package)
898 -- Protection: Self.L
900 Open_Accepts : Accept_List_Access;
901 -- This points to the Open_Accepts array of accept alternatives passed
902 -- to the RTS by the compiler-generated code to Selective_Wait. It is
903 -- non-null iff this task is ready to accept an entry call.
905 -- Protection: Self.L
907 Chosen_Index : Select_Index;
908 -- The index in Open_Accepts of the entry call accepted by a selective
909 -- wait executed by this task.
911 -- Protection: Written by both Self and Caller. Usually protected by
912 -- Self.L. However, once the selection is known to have been written it
913 -- can be accessed without protection. This happens after Self has
914 -- updated it itself using information from a suspended Caller, or
915 -- after Caller has updated it and awakened Self.
917 Master_of_Task : Master_Level;
918 -- The task executing the master of this task, and the ID of this task's
919 -- master (unique only among masters currently active within Parent).
921 -- Protection: Set by Activator before Self is activated, and read
922 -- after Self is activated.
924 Master_Within : Master_Level;
925 -- The ID of the master currently executing within this task; that is,
926 -- the most deeply nested currently active master.
928 -- Protection: Only written by Self, and only read by Self or by
929 -- dependents when Self is attempting to exit a master. Since Self will
930 -- not write this field until the master is complete, the
931 -- synchronization should be adequate to prevent races.
933 Alive_Count : Integer := 0;
934 -- Number of tasks directly dependent on this task (including itself)
935 -- that are still "alive", i.e. not terminated.
937 -- Protection: Self.L
939 Awake_Count : Integer := 0;
940 -- Number of tasks directly dependent on this task (including itself)
941 -- still "awake", i.e., are not terminated and not waiting on a
942 -- terminate alternative.
944 -- Invariant: Awake_Count <= Alive_Count
946 -- Protection: Self.L
948 -- Beginning of flags
950 Aborting : Boolean := False;
951 pragma Atomic (Aborting);
952 -- Self is in the process of aborting. While set, prevents multiple
953 -- abort signals from being sent by different aborter while abort
954 -- is acted upon. This is essential since an aborter which calls
955 -- Abort_To_Level could set the Pending_ATC_Level to yet a lower level
956 -- (than the current level), may be preempted and would send the
957 -- abort signal when resuming execution. At this point, the abortee
958 -- may have completed abort to the proper level such that the
959 -- signal (and resulting abort exception) are not handled any more.
960 -- In other words, the flag prevents a race between multiple aborters
962 -- Protection: protected by atomic access.
964 ATC_Hack : Boolean := False;
965 pragma Atomic (ATC_Hack);
966 -- ?????
967 -- Temporary fix, to allow Undefer_Abort to reset Aborting in the
968 -- handler for Abort_Signal that encloses an async. entry call.
969 -- For the longer term, this should be done via code in the
970 -- handler itself.
972 Callable : Boolean := True;
973 -- It is OK to call entries of this task
975 Dependents_Aborted : Boolean := False;
976 -- This is set to True by whichever task takes responsibility for
977 -- aborting the dependents of this task.
979 -- Protection: Self.L
981 Interrupt_Entry : Boolean := False;
982 -- Indicates if one or more Interrupt Entries are attached to the task.
983 -- This flag is needed for cleaning up the Interrupt Entry bindings.
985 Pending_Action : Boolean := False;
986 -- Unified flag indicating some action needs to be take when abort
987 -- next becomes undeferred. Currently set if:
988 -- . Pending_Priority_Change is set
989 -- . Pending_ATC_Level is changed
990 -- . Requeue involving POs
991 -- (Abortable field may have changed and the Wait_Until_Abortable
992 -- has to recheck the abortable status of the call.)
993 -- . Exception_To_Raise is non-null
995 -- Protection: Self.L
997 -- This should never be reset back to False outside of the procedure
998 -- Do_Pending_Action, which is called by Undefer_Abort. It should only
999 -- be set to True by Set_Priority and Abort_To_Level.
1001 Pending_Priority_Change : Boolean := False;
1002 -- Flag to indicate pending priority change (for dynamic priorities
1003 -- package). The base priority is updated on the next abort
1004 -- completion point (aka. synchronization point).
1006 -- Protection: Self.L
1008 Terminate_Alternative : Boolean := False;
1009 -- Task is accepting Select with Terminate Alternative
1011 -- Protection: Self.L
1013 -- End of flags
1015 -- Beginning of counts
1017 ATC_Nesting_Level : ATC_Level := 1;
1018 -- The dynamic level of ATC nesting (currently executing nested
1019 -- asynchronous select statements) in this task.
1021 -- Protection: Self_ID.L. Only Self reads or updates this field.
1022 -- Decrementing it deallocates an Entry_Calls component, and care must
1023 -- be taken that all references to that component are eliminated before
1024 -- doing the decrement. This in turn will require locking a protected
1025 -- object (for a protected entry call) or the Acceptor's lock (for a
1026 -- task entry call). No other task should attempt to read or modify
1027 -- this value.
1029 Deferral_Level : Natural := 1;
1030 -- This is the number of times that Defer_Abort has been called by
1031 -- this task without a matching Undefer_Abort call. Abortion is only
1032 -- allowed when this zero. It is initially 1, to protect the task at
1033 -- startup.
1035 -- Protection: Only updated by Self; access assumed to be atomic
1037 Pending_ATC_Level : ATC_Level_Base := ATC_Level_Infinity;
1038 -- The ATC level to which this task is currently being aborted. If the
1039 -- value is zero, the entire task has "completed". That may be via
1040 -- abort, exception propagation, or normal exit. If the value is
1041 -- ATC_Level_Infinity, the task is not being aborted to any level. If
1042 -- the value is positive, the task has not completed. This should ONLY
1043 -- be modified by Abort_To_Level and Exit_One_ATC_Level.
1045 -- Protection: Self.L
1047 Serial_Number : Task_Serial_Number;
1048 -- A growing number to provide some way to check locking rules/ordering
1050 Known_Tasks_Index : Integer := -1;
1051 -- Index in the System.Tasking.Debug.Known_Tasks array
1053 User_State : Long_Integer := 0;
1054 -- User-writeable location, for use in debugging tasks; also provides a
1055 -- simple task specific data.
1057 Direct_Attributes : Direct_Attribute_Array;
1058 -- For task attributes that have same size as Address
1060 Is_Defined : Direct_Index_Vector := 0;
1061 -- Bit I is 1 iff Direct_Attributes (I) is defined
1063 Indirect_Attributes : Access_Address;
1064 -- A pointer to chain of records for other attributes that are not
1065 -- address-sized, including all tagged types.
1067 Entry_Queues : Task_Entry_Queue_Array (1 .. Entry_Num);
1068 -- An array of task entry queues
1070 -- Protection: Self.L. Once a task has set Self.Stage to Completing, it
1071 -- has exclusive access to this field.
1072 end record;
1074 --------------------
1075 -- Initialization --
1076 --------------------
1078 procedure Initialize;
1079 -- This procedure constitutes the first part of the initialization of the
1080 -- GNARL. This includes creating data structures to make the initial thread
1081 -- into the environment task. The last part of the initialization is done
1082 -- in System.Tasking.Initialization or System.Tasking.Restricted.Stages.
1083 -- All the initializations used to be in Tasking.Initialization, but this
1084 -- is no longer possible with the run time simplification (including
1085 -- optimized PO and the restricted run time) since one cannot rely on
1086 -- System.Tasking.Initialization being present, as was done before.
1088 procedure Initialize_ATCB
1089 (Self_ID : Task_Id;
1090 Task_Entry_Point : Task_Procedure_Access;
1091 Task_Arg : System.Address;
1092 Parent : Task_Id;
1093 Elaborated : Access_Boolean;
1094 Base_Priority : System.Any_Priority;
1095 Task_Info : System.Task_Info.Task_Info_Type;
1096 Stack_Size : System.Parameters.Size_Type;
1097 T : Task_Id;
1098 Success : out Boolean);
1099 -- Initialize fields of a TCB and link into global TCB structures Call
1100 -- this only with abort deferred and holding RTS_Lock. Need more
1101 -- documentation, mention T, and describe Success ???
1103 private
1105 Null_Task : constant Task_Id := null;
1107 type Activation_Chain is limited record
1108 T_ID : Task_Id;
1109 end record;
1111 -- Activation_Chain is an in-out parameter of initialization procedures and
1112 -- it must be passed by reference because the init proc may terminate
1113 -- abnormally after creating task components, and these must be properly
1114 -- registered for removal (Expunge_Unactivated_Tasks). The "limited" forces
1115 -- Activation_Chain to be a by-reference type; see RM-6.2(4).
1117 end System.Tasking;