OSDN Git Service

Delete all lines containing "$Revision:".
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taskin.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA 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 --                                                                          --
10 --          Copyright (C) 1992-2002, Free Software Foundation, Inc.         --
11 --                                                                          --
12 -- GNARL is free software; you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com).     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This package provides necessary type definitions for compiler interface.
36
37 --  Note: the compiler generates direct calls to this interface, via Rtsfind.
38 --  Any changes to this interface may require corresponding compiler changes.
39
40 with Ada.Exceptions;
41 --  Used for:  Exception_Id
42
43 with System.Parameters;
44 --  used for Size_Type
45
46 with System.Task_Info;
47 --  used for Task_Info_Type, Task_Image_Type
48
49 with System.Soft_Links;
50 --  used for TSD
51
52 with System.Task_Primitives;
53 --  used for Private_Data
54
55 with Unchecked_Conversion;
56
57 package System.Tasking is
58
59    --  -------------------
60    --  -- Locking Rules --
61    --  -------------------
62    --
63    --  The following rules must be followed at all times, to prevent
64    --  deadlock and generally ensure correct operation of locking.
65    --
66    --  . Never lock a lock unless abort is deferred.
67    --
68    --  . Never undefer abort while holding a lock.
69    --
70    --  . Overlapping critical sections must be properly nested,
71    --    and locks must be released in LIFO order.
72    --    e.g., the following is not allowed:
73    --
74    --         Lock (X);
75    --         ...
76    --         Lock (Y);
77    --         ...
78    --         Unlock (X);
79    --         ...
80    --         Unlock (Y);
81    --
82    --  Locks with lower (smaller) level number cannot be locked
83    --  while holding a lock with a higher level number. (The level
84    --  number is the number at the left.)
85    --
86    --  1. System.Tasking.PO_Simple.Protection.L (any PO lock)
87    --  2. System.Tasking.Initialization.Global_Task_Lock (in body)
88    --  3. System.Task_Primitives.Operations.Single_RTS_Lock
89    --  4. System.Tasking.Ada_Task_Control_Block.LL.L (any TCB lock)
90    --
91    --  Clearly, there can be no circular chain of hold-and-wait
92    --  relationships involving locks in different ordering levels.
93    --
94    --  We used to have Global_Task_Lock before Protection.L but this was
95    --  clearly wrong since there can be calls to "new" inside protected
96    --  operations. The new ordering prevents these failures.
97    --
98    --  Sometimes we need to hold two ATCB locks at the same time. To allow
99    --  us to order the locking, each ATCB is given a unique serial
100    --  number. If one needs to hold locks on several ATCBs at once,
101    --  the locks with lower serial numbers must be locked first.
102    --
103    --  We don't always need to check the serial numbers, since
104    --  the serial numbers are assigned sequentially, and so:
105    --
106    --  . The parent of a task always has a lower serial number.
107    --  . The activator of a task always has a lower serial number.
108    --  . The environment task has a lower serial number than any other task.
109    --  . If the activator of a task is different from the task's parent,
110    --    the parent always has a lower serial number than the activator.
111
112    ---------------------------------
113    -- Task_ID related definitions --
114    ---------------------------------
115
116    type Ada_Task_Control_Block;
117
118    type Task_ID is access all Ada_Task_Control_Block;
119
120    Null_Task : constant Task_ID;
121
122    type Task_List is array (Positive range <>) of Task_ID;
123
124    function Self return Task_ID;
125    pragma Inline (Self);
126    --  This is the compiler interface version of this function. Do not call
127    --  from the run-time system.
128
129    function To_Task_Id is new Unchecked_Conversion (System.Address, Task_ID);
130    function To_Address is new Unchecked_Conversion (Task_ID, System.Address);
131
132    -----------------------
133    -- Enumeration types --
134    -----------------------
135
136    type Task_States is
137      (Unactivated,
138       --  Task has been created but has not been activated.
139       --  It cannot be executing.
140
141       --  Active states
142       --  For all states from here down, the task has been activated.
143       --  For all states from here down, except for Terminated, the task
144       --  may be executing.
145       --  Activator = null iff it has not yet completed activating.
146
147       --  For all states from here down,
148       --  the task has been activated, and may be executing.
149
150       Runnable,
151       --  Task is not blocked for any reason known to Ada.
152       --  (It may be waiting for a mutex, though.)
153       --  It is conceptually "executing" in normal mode.
154
155       Terminated,
156       --  The task is terminated, in the sense of ARM 9.3 (5).
157       --  Any dependents that were waiting on terminate
158       --  alternatives have been awakened and have terminated themselves.
159
160       Activator_Sleep,
161       --  Task is waiting for created tasks to complete activation.
162
163       Acceptor_Sleep,
164       --  Task is waiting on an accept or selective wait statement.
165
166       Entry_Caller_Sleep,
167       --  Task is waiting on an entry call.
168
169       Async_Select_Sleep,
170       --  Task is waiting to start the abortable part of an
171       --  asynchronous select statement.
172
173       Delay_Sleep,
174       --  Task is waiting on a select statement with only a delay
175       --  alternative open.
176
177       Master_Completion_Sleep,
178       --  Master completion has two phases.
179       --  In Phase 1 the task is sleeping in Complete_Master
180       --  having completed a master within itself,
181       --  and is waiting for the tasks dependent on that master to become
182       --  terminated or waiting on a terminate Phase.
183
184       Master_Phase_2_Sleep,
185       --  In Phase 2 the task is sleeping in Complete_Master
186       --  waiting for tasks on terminate alternatives to finish
187       --  terminating.
188
189       --  The following are special uses of sleep, for server tasks
190       --  within the run-time system.
191
192       Interrupt_Server_Idle_Sleep,
193       Interrupt_Server_Blocked_Interrupt_Sleep,
194       Timer_Server_Sleep,
195       AST_Server_Sleep,
196
197       Asynchronous_Hold,
198       --  The task has been held by Asynchronous_Task_Control.Hold_Task
199
200       Interrupt_Server_Blocked_On_Event_Flag
201       --  The task has been blocked on a system call waiting for the
202       --  completion event.
203      );
204
205    type Call_Modes is
206      (Simple_Call, Conditional_Call, Asynchronous_Call, Timed_Call);
207
208    type Select_Modes is (Simple_Mode, Else_Mode, Terminate_Mode, Delay_Mode);
209
210    subtype Delay_Modes is Integer;
211
212    -------------------------------
213    -- Entry related definitions --
214    -------------------------------
215
216    Null_Entry : constant := 0;
217
218    Max_Entry : constant := Integer'Last;
219
220    Interrupt_Entry : constant := -2;
221
222    Cancelled_Entry : constant := -1;
223
224    type Entry_Index is range Interrupt_Entry .. Max_Entry;
225
226    Null_Task_Entry : constant := Null_Entry;
227
228    Max_Task_Entry : constant := Max_Entry;
229
230    type Task_Entry_Index is new Entry_Index
231      range Null_Task_Entry .. Max_Task_Entry;
232
233    type Entry_Call_Record;
234
235    type Entry_Call_Link is access all Entry_Call_Record;
236
237    type Entry_Queue is record
238       Head : Entry_Call_Link;
239       Tail : Entry_Call_Link;
240    end record;
241
242    type Task_Entry_Queue_Array is
243      array (Task_Entry_Index range <>) of Entry_Queue;
244
245    ----------------------------------
246    -- Entry_Call_Record definition --
247    ----------------------------------
248
249    type Entry_Call_State is
250      (Never_Abortable,
251       --  the call is not abortable, and never can be
252
253       Not_Yet_Abortable,
254       --  the call is not abortable, but may become so
255
256       Was_Abortable,
257       --  the call is not abortable, but once was
258
259       Now_Abortable,
260       --  the call is abortable
261
262       Done,
263       --  the call has been completed
264
265       Cancelled
266       --  the call was asynchronous, and was cancelled
267      );
268
269    --  Never_Abortable is used for calls that are made in a abort
270    --  deferred region (see ARM 9.8(5-11), 9.8 (20)).
271    --  Such a call is never abortable.
272
273    --  The Was_ vs. Not_Yet_ distinction is needed to decide whether it
274    --  is OK to advance into the abortable part of an async. select stmt.
275    --  That is allowed iff the mode is Now_ or Was_.
276
277    --  Done indicates the call has been completed, without cancellation,
278    --  or no call has been made yet at this ATC nesting level,
279    --  and so aborting the call is no longer an issue.
280    --  Completion of the call does not necessarily indicate "success";
281    --  the call may be returning an exception if Exception_To_Raise is
282    --  non-null.
283
284    --  Cancelled indicates the call was cancelled,
285    --  and so aborting the call is no longer an issue.
286
287    --  The call is on an entry queue unless
288    --  State >= Done, in which case it may or may not be still Onqueue.
289
290    --  Please do not modify the order of the values, without checking
291    --  all uses of this type.  We rely on partial "monotonicity" of
292    --  Entry_Call_Record.State to avoid locking when we access this
293    --  value for certain tests.  In particular:
294
295    --  1)  Once State >= Done, we can rely that the call has been
296    --      completed.  If State >= Done, it will not
297    --      change until the task does another entry call at this level.
298
299    --  2)  Once State >= Was_Abortable, we can rely that the call has
300    --      been queued abortably at least once, and so the check for
301    --      whether it is OK to advance to the abortable part of an
302    --      async. select statement does not need to lock anything.
303
304    type Restricted_Entry_Call_Record is record
305       Self : Task_ID;
306       --  ID of the caller
307
308       Mode : Call_Modes;
309
310       State : Entry_Call_State;
311       pragma Atomic (State);
312       --  Indicates part of the state of the call.
313       --  Protection:
314       --  If the call is not on a queue, it should
315       --  only be accessed by Self, and Self does not need any
316       --  lock to modify this field.
317       --  Once the call is on a queue, the value should be
318       --  something other than Done unless it is cancelled, and access is
319       --  controller by the "server" of the queue -- i.e., the lock
320       --  of Checked_To_Protection (Call_Target)
321       --  if the call record is on the queue of a PO, or the lock
322       --  of Called_Target if the call is on the queue of a task.
323       --  See comments on type declaration for more details.
324
325       Uninterpreted_Data : System.Address;
326       --  Data passed by the compiler.
327
328       Exception_To_Raise : Ada.Exceptions.Exception_Id;
329       --  The exception to raise once this call has been completed without
330       --  being aborted.
331    end record;
332    pragma Suppress_Initialization (Restricted_Entry_Call_Record);
333
334    ------------------------------------
335    -- Task related other definitions --
336    ------------------------------------
337
338    type Activation_Chain is limited private;
339
340    type Activation_Chain_Access is access all Activation_Chain;
341
342    type Task_Procedure_Access is access procedure (Arg : System.Address);
343
344    type Access_Boolean is access all Boolean;
345
346    ----------------------------------------------
347    -- Ada_Task_Control_Block (ATCB) definition --
348    ----------------------------------------------
349
350    --  Notes on protection (synchronization) of TRTS data structures.
351
352    --  Any field of the TCB can be written by the activator of a task when the
353    --  task is created, since no other task can access the new task's
354    --  state until creation is complete.
355
356    --  The protection for each field is described in a comment starting with
357    --  "Protection:".
358
359    --  When a lock is used to protect an ATCB field, this lock is simply named.
360
361    --  Some protection is described in terms of tasks related to the
362    --  ATCB being protected. These are:
363
364    --    Self: The task which is controlled by this ATCB.
365    --    Acceptor: A task accepting a call from Self.
366    --    Caller: A task calling an entry of Self.
367    --    Parent: The task executing the master on which Self depends.
368    --    Dependent: A task dependent on Self.
369    --    Activator: The task that created Self and initiated its activation.
370    --    Created: A task created and activated by Self.
371
372    --  Note: The order of the fields is important to implement efficiently
373    --  tasking support under gdb.
374    --  Currently gdb relies on the order of the State, Parent, Base_Priority,
375    --  Task_Image, Call and LL fields.
376
377    ----------------------------------------------------------------------
378    --  Common ATCB section                                             --
379    --                                                                  --
380    --  This section is used by all GNARL implementations (regular and  --
381    --  restricted)                                                     --
382    ----------------------------------------------------------------------
383
384    type Common_ATCB is record
385       State : Task_States;
386       pragma Atomic (State);
387       --  Encodes some basic information about the state of a task,
388       --  including whether it has been activated, whether it is sleeping,
389       --  and whether it is terminated.
390       --  Protection: Self.L.
391
392       Parent : Task_ID;
393       --  The task on which this task depends.
394       --  See also Master_Level and Master_Within.
395
396       Base_Priority : System.Any_Priority;
397       --  Base priority, not changed during entry calls, only changed
398       --  via dynamic priorities package.
399       --  Protection: Only written by Self, accessed by anyone.
400
401       Current_Priority : System.Any_Priority;
402       --  Active priority, except that the effects of protected object
403       --  priority ceilings are not reflected. This only reflects explicit
404       --  priority changes and priority inherited through task activation
405       --  and rendezvous.
406       --
407       --  Ada 95 notes: In Ada 95, this field will be transferred to the
408       --  Priority field of an Entry_Calls component when an entry call
409       --  is initiated. The Priority of the Entry_Calls component will not
410       --  change for the duration of the call. The accepting task can
411       --  use it to boost its own priority without fear of its changing in
412       --  the meantime.
413       --
414       --  This can safely be used in the priority ordering
415       --  of entry queues. Once a call is queued, its priority does not
416       --  change.
417       --
418       --  Since an entry call cannot be made while executing
419       --  a protected action, the priority of a task will never reflect a
420       --  priority ceiling change at the point of an entry call.
421       --
422       --  Protection: Only written by Self, and only accessed when Acceptor
423       --  accepts an entry or when Created activates, at which points Self is
424       --  suspended.
425
426       Task_Image : System.Task_Info.Task_Image_Type;
427       --  holds an access to string that provides a readable id for task,
428       --  built from the variable of which it is a value or component.
429
430       Call : Entry_Call_Link;
431       --  The entry call that has been accepted by this task.
432       --  Protection: Self.L. Self will modify this field
433       --  when Self.Accepting is False, and will not need the mutex to do so.
434       --  Once a task sets Pending_ATC_Level = 0, no other task can access
435       --  this field.
436
437       LL : aliased Task_Primitives.Private_Data;
438       --  Control block used by the underlying low-level tasking
439       --  service (GNULLI).
440       --  Protection: This is used only by the GNULLI implementation, which
441       --  takes care of all of its synchronization.
442
443       Task_Arg : System.Address;
444       --  The argument to task procedure. Currently unused; this will
445       --  provide a handle for discriminant information.
446       --  Protection: Part of the synchronization between Self and
447       --  Activator. Activator writes it, once, before Self starts
448       --  executing. Thereafter, Self only reads it.
449
450       Task_Entry_Point : Task_Procedure_Access;
451       --  Information needed to call the procedure containing the code for
452       --  the body of this task.
453       --  Protection: Part of the synchronization between Self and
454       --  Activator. Activator writes it, once, before Self starts
455       --  executing. Self reads it, once, as part of its execution.
456
457       Compiler_Data : System.Soft_Links.TSD;
458       --  Task-specific data needed by the compiler to store
459       --  per-task structures.
460       --  Protection: Only accessed by Self.
461
462       All_Tasks_Link : Task_ID;
463       --  Used to link this task to the list of all tasks in the system.
464       --  Protection: RTS_Lock.
465
466       Activation_Link : Task_ID;
467       --  Used to link this task to a list of tasks to be activated.
468       --  Protection: Only used by Activator.
469
470       Activator : Task_ID;
471       --  The task that created this task, either by declaring it as a task
472       --  object or by executing a task allocator.
473       --  The value is null iff Self has completed activation.
474       --  Protection: Set by Activator before Self is activated, and
475       --  only read and modified by Self after that.
476
477       Wait_Count : Integer;
478       --  This count is used by a task that is waiting for other tasks.
479       --  At all other times, the value should be zero.
480       --  It is used differently in several different states.
481       --  Since a task cannot be in more than one of these states at the
482       --  same time, a single counter suffices.
483       --  Protection: Self.L.
484
485       --  Activator_Sleep
486
487       --  This is the number of tasks that this task is activating, i.e. the
488       --  children that have started activation but have not completed it.
489       --  Protection: Self.L and Created.L. Both mutexes must be locked,
490       --  since Self.Activation_Count and Created.State must be synchronized.
491
492       --  Master_Completion_Sleep (phase 1)
493
494       --  This is the number dependent tasks of a master being
495       --  completed by Self that are not activated, not terminated, and
496       --  not waiting on a terminate alternative.
497
498       --  Master_Completion_2_Sleep (phase 2)
499
500       --  This is the count of tasks dependent on a master being
501       --  completed by Self which are waiting on a terminate alternative.
502
503       Elaborated : Access_Boolean;
504       --  Pointer to a flag indicating that this task's body has been
505       --  elaborated. The flag is created and managed by the
506       --  compiler-generated code.
507       --  Protection: The field itself is only accessed by Activator. The flag
508       --  that it points to is updated by Master and read by Activator; access
509       --  is assumed to be atomic.
510
511       Activation_Failed : Boolean;
512       --  Set to True if activation of a chain of tasks fails,
513       --  so that the activator should raise Tasking_Error.
514
515       Task_Info : System.Task_Info.Task_Info_Type;
516       --  System-specific attributes of the task as specified by the
517       --  Task_Info pragma.
518    end record;
519
520    ---------------------------------------
521    -- Restricted_Ada_Task_Control_Block --
522    ---------------------------------------
523
524    --  This type should only be used by the restricted GNARLI and by
525    --  restricted GNULL implementations to allocate an ATCB (see
526    --  System.Task_Primitives.Operations.New_ATCB) that will take
527    --  significantly less memory.
528    --  Note that the restricted GNARLI should only access fields that are
529    --  present in the Restricted_Ada_Task_Control_Block structure.
530
531    type Restricted_Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is
532    record
533       Common : Common_ATCB;
534       --  The common part between various tasking implementations
535
536       Entry_Call : aliased Restricted_Entry_Call_Record;
537       --  Protection: This field is used on entry call "queues" associated
538       --  with protected objects, and is protected by the protected object
539       --  lock.
540    end record;
541    pragma Suppress_Initialization (Restricted_Ada_Task_Control_Block);
542
543    Interrupt_Manager_ID : Task_ID;
544    --  This task ID is declared here to break circular dependencies.
545    --  Also declare Interrupt_Manager_ID after Task_ID is known, to avoid
546    --  generating unneeded finalization code.
547
548    -----------------------
549    -- List of all Tasks --
550    -----------------------
551
552    All_Tasks_List : Task_ID;
553    --  Global linked list of all tasks.
554
555    ------------------------------------------
556    -- Regular (non restricted) definitions --
557    ------------------------------------------
558
559    --------------------------------
560    -- Master Related Definitions --
561    --------------------------------
562
563    subtype Master_Level is Integer;
564    subtype Master_ID is Master_Level;
565
566    --  Normally, a task starts out with internal master nesting level
567    --  one larger than external master nesting level. It is incremented
568    --  to one by Enter_Master, which is called in the task body only if
569    --  the compiler thinks the task may have dependent tasks. It is set to 1
570    --  for the environment task, the level 2 is reserved for server tasks of
571    --  the run-time system (the so called "independent tasks"), and the level
572    --  3 is for the library level tasks.
573
574    Environment_Task_Level : constant Master_Level := 1;
575    Independent_Task_Level : constant Master_Level := 2;
576    Library_Task_Level     : constant Master_Level := 3;
577
578    ------------------------------
579    -- Task size, priority info --
580    ------------------------------
581
582    Unspecified_Priority : constant Integer := System.Priority'First - 1;
583
584    Priority_Not_Boosted : constant Integer := System.Priority'First - 1;
585    --  Definition of Priority actually has to come from the RTS configuration.
586
587    subtype Rendezvous_Priority is Integer
588      range Priority_Not_Boosted .. System.Any_Priority'Last;
589
590    ------------------------------------
591    -- Rendezvous related definitions --
592    ------------------------------------
593
594    No_Rendezvous : constant := 0;
595
596    Max_Select : constant Integer := Integer'Last;
597    --  RTS-defined
598
599    subtype Select_Index is Integer range No_Rendezvous .. Max_Select;
600    --   type Select_Index is range No_Rendezvous .. Max_Select;
601
602    subtype Positive_Select_Index is
603      Select_Index range 1 .. Select_Index'Last;
604
605    type Accept_Alternative is record
606       Null_Body : Boolean;
607       S         : Task_Entry_Index;
608    end record;
609
610    type Accept_List is
611      array (Positive_Select_Index range <>) of Accept_Alternative;
612
613    type Accept_List_Access is access constant Accept_List;
614
615    -----------------------------------
616    -- ATC_Level related definitions --
617    -----------------------------------
618
619    Max_ATC_Nesting : constant Natural := 20;
620
621    subtype ATC_Level_Base is Integer range 0 .. Max_ATC_Nesting;
622
623    ATC_Level_Infinity : constant ATC_Level_Base := ATC_Level_Base'Last;
624
625    subtype ATC_Level is ATC_Level_Base range 0 .. ATC_Level_Base'Last - 1;
626
627    subtype ATC_Level_Index is ATC_Level range 1 .. ATC_Level'Last;
628
629    ----------------------------------
630    -- Entry_Call_Record definition --
631    ----------------------------------
632
633    type Entry_Call_Record is record
634       Self  : Task_ID;
635       --  ID of the caller
636
637       Mode : Call_Modes;
638
639       State : Entry_Call_State;
640       pragma Atomic (State);
641       --  Indicates part of the state of the call.
642       --  Protection:
643       --  If the call is not on a queue, it should
644       --  only be accessed by Self, and Self does not need any
645       --  lock to modify this field.
646       --  Once the call is on a queue, the value should be
647       --  something other than Done unless it is cancelled, and access is
648       --  controller by the "server" of the queue -- i.e., the lock
649       --  of Checked_To_Protection (Call_Target)
650       --  if the call record is on the queue of a PO, or the lock
651       --  of Called_Target if the call is on the queue of a task.
652       --  See comments on type declaration for more details.
653
654       Uninterpreted_Data : System.Address;
655       --  Data passed by the compiler.
656
657       Exception_To_Raise : Ada.Exceptions.Exception_Id;
658       --  The exception to raise once this call has been completed without
659       --  being aborted.
660
661       Prev : Entry_Call_Link;
662
663       Next : Entry_Call_Link;
664
665       Level : ATC_Level;
666       --  One of Self and Level are redundant in this implementation, since
667       --  each Entry_Call_Record is at Self.Entry_Calls (Level). Since we must
668       --  have access to the entry call record to be reading this, we could
669       --  get Self from Level, or Level from Self. However, this requires
670       --  non-portable address arithmetic.
671
672       E : Entry_Index;
673
674       Prio : System.Any_Priority;
675
676       --  The above fields are those that there may be some hope of packing.
677       --  They are gathered together to allow for compilers that lay records
678       --  out contiguously, to allow for such packing.
679
680       Called_Task : Task_ID;
681       pragma Atomic (Called_Task);
682       --  Use for task entry calls.
683       --  The value is null if the call record is not in use.
684       --  Conversely, unless State is Done and Onqueue is false,
685       --  Called_Task points to an ATCB.
686       --  Protection:  Called_Task.L.
687
688       Called_PO : System.Address;
689       pragma Atomic (Called_PO);
690       --  Similar to Called_Task but for protected objects.
691       --  Note that the previous implementation tried to merge both
692       --  Called_Task and Called_PO but this ended up in many unexpected
693       --  complications (e.g having to add a magic number in the ATCB, which
694       --  caused gdb lots of confusion) with no real gain since the Lock_Server
695       --  implementation still need to loop around chasing for pointer changes
696       --  even with a single pointer.
697
698       Acceptor_Prev_Call : Entry_Call_Link;
699       --  For task entry calls only.
700
701       Acceptor_Prev_Priority : Rendezvous_Priority := Priority_Not_Boosted;
702       --  For task entry calls only.
703       --  The priority of the most recent prior call being serviced.
704       --  For protected entry calls, this function should be performed by
705       --  GNULLI ceiling locking.
706
707       Cancellation_Attempted : Boolean := False;
708       pragma Atomic (Cancellation_Attempted);
709       --  Cancellation of the call has been attempted.
710       --  If it has succeeded, State = Cancelled.
711       --  ?????
712       --  Consider merging this into State?
713
714       Requeue_With_Abort : Boolean := False;
715       --  Temporary to tell caller whether requeue is with abort.
716       --  ?????
717       --  Find a better way of doing this.
718
719       Needs_Requeue : Boolean := False;
720       --  Temporary to tell acceptor of task entry call that
721       --  Exceptional_Complete_Rendezvous needs to do requeue.
722    end record;
723
724    ------------------------------------
725    -- Task related other definitions --
726    ------------------------------------
727
728    type Access_Address is access all System.Address;
729
730    ----------------------------------------------
731    -- Ada_Task_Control_Block (ATCB) definition --
732    ----------------------------------------------
733
734    type Entry_Call_Array is array (ATC_Level_Index) of
735      aliased Entry_Call_Record;
736
737    D_I_Count : constant := 2;
738    --  This constant may be adjusted, to allow more Address-sized
739    --  attributes to be stored directly in the task control block.
740
741    subtype Direct_Index is Integer range 0 .. D_I_Count - 1;
742    --  Attributes with indices in this range are stored directly in
743    --  the task control block.  Such attributes must be Address-sized.
744    --  Other attributes will be held in dynamically allocated records
745    --  chained off of the task control block.
746
747    type Direct_Attribute_Array is
748      array (Direct_Index) of aliased System.Address;
749
750    type Direct_Index_Vector is mod 2 ** D_I_Count;
751    --  This is a bit-vector type, used to store information about
752    --  the usage of the direct attribute fields.
753
754    type Task_Serial_Number is mod 2 ** 64;
755    --  Used to give each task a unique serial number.
756
757    type Ada_Task_Control_Block (Entry_Num : Task_Entry_Index) is record
758       Common : Common_ATCB;
759       --  The common part between various tasking implementations
760
761       Entry_Calls : Entry_Call_Array;
762       --  An array of entry calls.
763       --  Protection: The elements of this array are on entry call queues
764       --  associated with protected objects or task entries, and are protected
765       --  by the protected object lock or Acceptor.L, respectively.
766
767       New_Base_Priority : System.Any_Priority;
768       --  New value for Base_Priority (for dynamic priorities package).
769       --  Protection: Self.L.
770
771       Global_Task_Lock_Nesting : Natural := 0;
772       --  This is the current nesting level of calls to
773       --  System.Tasking.Stages.Lock_Task_T.
774       --  This allows a task to call Lock_Task_T multiple times without
775       --  deadlocking. A task only locks All_Task_Lock when its
776       --  All_Tasks_Nesting goes from 0 to 1, and only unlocked when it
777       --  goes from 1 to 0.
778       --  Protection: Only accessed by Self.
779
780       Open_Accepts : Accept_List_Access;
781       --  This points to the Open_Accepts array of accept alternatives passed
782       --  to the RTS by the compiler-generated code to Selective_Wait.
783       --  It is non-null iff this task is ready to accept an entry call.
784       --  Protection: Self.L.
785
786       Chosen_Index : Select_Index;
787       --  The index in Open_Accepts of the entry call accepted by a selective
788       --  wait executed by this task.
789       --  Protection: Written by both Self and Caller. Usually protected
790       --  by Self.L. However, once the selection is known to have been
791       --  written it can be accessed without protection. This happens
792       --  after Self has updated it itself using information from a suspended
793       --  Caller, or after Caller has updated it and awakened Self.
794
795       Master_of_Task : Master_Level;
796       --  The task executing the master of this task, and the ID of this task's
797       --  master (unique only among masters currently active within Parent).
798       --  Protection: Set by Activator before Self is activated, and
799       --  read after Self is activated.
800
801       Master_Within : Master_Level;
802       --  The ID of the master currently executing within this task; that is,
803       --  the most deeply nested currently active master.
804       --  Protection: Only written by Self, and only read by Self or by
805       --  dependents when Self is attempting to exit a master. Since Self
806       --  will not write this field until the master is complete, the
807       --  synchronization should be adequate to prevent races.
808
809       Alive_Count : Integer := 0;
810       --  Number of tasks directly dependent on this task (including itself)
811       --  that are still "alive", i.e. not terminated.
812       --  Protection: Self.L.
813
814       Awake_Count : Integer := 0;
815       --  Number of tasks directly dependent on this task (including itself)
816       --  still "awake", i.e., are not terminated and not waiting on a
817       --  terminate alternative.
818       --  Invariant: Awake_Count <= Alive_Count
819       --  Protection: Self.L.
820
821       --  beginning of flags
822
823       Aborting : Boolean := False;
824       pragma Atomic (Aborting);
825       --  Self is in the process of aborting. While set, prevents multiple
826       --  abortion signals from being sent by different aborter while abortion
827       --  is acted upon. This is essential since an aborter which calls
828       --  Abort_To_Level could set the Pending_ATC_Level to yet a lower level
829       --  (than the current level), may be preempted and would send the
830       --  abortion signal when resuming execution. At this point, the abortee
831       --  may have completed abortion to the proper level such that the
832       --  signal (and resulting abortion exception) are not handled any more.
833       --  In other words, the flag prevents a race between multiple aborters
834       --  and the abortee.
835       --  Protection: Self.L.
836
837       ATC_Hack : Boolean := False;
838       pragma Atomic (ATC_Hack);
839       --  ?????
840       --  Temporary fix, to allow Undefer_Abort to reset Aborting in the
841       --  handler for Abort_Signal that encloses an async. entry call.
842       --  For the longer term, this should be done via code in the
843       --  handler itself.
844
845       Callable : Boolean := True;
846       --  It is OK to call entries of this task.
847
848       Dependents_Aborted : Boolean := False;
849       --  This is set to True by whichever task takes responsibility
850       --  for aborting the dependents of this task.
851       --  Protection: Self.L.
852
853       Interrupt_Entry : Boolean := False;
854       --  Indicates if one or more Interrupt Entries are attached to
855       --  the task. This flag is needed for cleaning up the Interrupt
856       --  Entry bindings.
857
858       Pending_Action : Boolean := False;
859       --  Unified flag indicating some action needs to be take when abort
860       --  next becomes undeferred.  Currently set if:
861       --  . Pending_Priority_Change is set
862       --  . Pending_ATC_Level is changed
863       --  . Requeue involving POs
864       --    (Abortable field may have changed and the Wait_Until_Abortable
865       --     has to recheck the abortable status of the call.)
866       --  . Exception_To_Raise is non-null
867       --  Protection: Self.L.
868       --  This should never be reset back to False outside of the
869       --  procedure Do_Pending_Action, which is called by Undefer_Abort.
870       --  It should only be set to True by Set_Priority and Abort_To_Level.
871
872       Pending_Priority_Change : Boolean := False;
873       --  Flag to indicate pending priority change (for dynamic priorities
874       --  package). The base priority is updated on the next abortion
875       --  completion point (aka. synchronization point).
876       --  Protection: Self.L.
877
878       Terminate_Alternative : Boolean := False;
879       --  Task is accepting Select with Terminate Alternative.
880       --  Protection: Self.L.
881
882       --  end of flags
883
884       --  beginning of counts
885
886       ATC_Nesting_Level : ATC_Level := 1;
887       --  The dynamic level of ATC nesting (currently executing nested
888       --  asynchronous select statements) in this task.
889       --  Protection:  Self_ID.L.
890       --  Only Self reads or updates this field.
891       --  Decrementing it deallocates an Entry_Calls component, and care must
892       --  be taken that all references to that component are eliminated
893       --  before doing the decrement. This in turn will require locking
894       --  a protected object (for a protected entry call) or the Acceptor's
895       --  lock (for a task entry call).
896       --  No other task should attempt to read or modify this value.
897
898       Deferral_Level : Natural := 1;
899       --  This is the number of times that Defer_Abortion has been called by
900       --  this task without a matching Undefer_Abortion call. Abortion is
901       --  only allowed when this zero.
902       --  It is initially 1, to protect the task at startup.
903       --  Protection: Only updated by Self; access assumed to be atomic.
904
905       Pending_ATC_Level : ATC_Level_Base := ATC_Level_Infinity;
906       --  The ATC level to which this task is currently being aborted.
907       --  If the value is zero, the entire task has "completed".
908       --  That may be via abort, exception propagation, or normal exit.
909       --  If the value is ATC_Level_Infinity, the task is not being
910       --  aborted to any level.
911       --  If the value is positive, the task has not completed.
912       --  This should ONLY be modified by
913       --  Abort_To_Level and Exit_One_ATC_Level.
914       --  Protection: Self.L.
915
916       Serial_Number : Task_Serial_Number;
917       --  A growing number to provide some way to check locking
918       --  rules/ordering.
919
920       Known_Tasks_Index : Integer := -1;
921       --  Index in the System.Tasking.Debug.Known_Tasks array.
922
923       User_State : Integer := 0;
924       --  user-writeable location, for use in debugging tasks;
925       --  debugger can display this value to show where the task currently
926       --  is, in user terms
927
928       Direct_Attributes : Direct_Attribute_Array;
929       --  For task attributes that have same size as Address
930
931       Is_Defined : Direct_Index_Vector := 0;
932       --  Bit I is 1 iff Direct_Attributes (I) is defined
933
934       Indirect_Attributes : Access_Address;
935       --  A pointer to chain of records for other attributes that
936       --  are not address-sized, including all tagged types.
937
938       Entry_Queues : Task_Entry_Queue_Array (1 .. Entry_Num);
939       --  An array of task entry queues.
940       --  Protection: Self.L. Once a task has set Self.Stage to Completing, it
941       --  has exclusive access to this field.
942    end record;
943    pragma Volatile (Ada_Task_Control_Block);
944
945    ---------------------
946    -- Initialize_ATCB --
947    ---------------------
948
949    procedure Initialize_ATCB
950      (Self_ID          : Task_ID;
951       Task_Entry_Point : Task_Procedure_Access;
952       Task_Arg         : System.Address;
953       Parent           : Task_ID;
954       Elaborated       : Access_Boolean;
955       Base_Priority    : System.Any_Priority;
956       Task_Info        : System.Task_Info.Task_Info_Type;
957       Stack_Size       : System.Parameters.Size_Type;
958       T                : in out Task_ID;
959       Success          : out Boolean);
960    --  Initialize fields of a TCB and link into global TCB structures
961    --  Call this only with abort deferred and holding RTS_Lock.
962
963 private
964
965    Null_Task : constant Task_ID := null;
966
967    type Activation_Chain is record
968       T_ID : Task_ID;
969    end record;
970    pragma Volatile (Activation_Chain);
971
972    --  Activation_chain is an in-out parameter of initialization procedures
973    --  and it must be passed by reference because the init_proc may terminate
974    --  abnormally after creating task components, and these must be properly
975    --  registered for removal (Expunge_Unactivated_Tasks).
976
977 end System.Tasking;