home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-taskin.ads < prev    next >
Text File  |  2000-07-19  |  39KB  |  947 lines

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