OSDN Git Service

2008-04-08 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-tasatt.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                        GNAT RUN-TIME COMPONENTS                          --
4 --                                                                          --
5 --                  A D A . T A S K _ A T T R I B U T E S                   --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --             Copyright (C) 1991-1994, Florida State University            --
10 --                     Copyright (C) 1995-2008, AdaCore                     --
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,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, 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.       --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  The following notes are provided in case someone decides the implementation
36 --  of this package is too complicated, or too slow. Please read this before
37 --  making any "simplifications".
38
39 --  Correct implementation of this package is more difficult than one might
40 --  expect. After considering (and coding) several alternatives, we settled on
41 --  the present compromise. Things we do not like about this implementation
42 --  include:
43
44 --  - It is vulnerable to bad Task_Id values, to the extent of possibly
45 --     trashing memory and crashing the runtime system.
46
47 --  - It requires dynamic storage allocation for each new attribute value,
48 --     except for types that happen to be the same size as System.Address, or
49 --     shorter.
50
51 --  -  Instantiations at other than the library level rely on being able to
52 --     do down-level calls to a procedure declared in the generic package body.
53 --     This makes it potentially vulnerable to compiler changes.
54
55 --  The main implementation issue here is that the connection from task to
56 --  attribute is a potential source of dangling references.
57
58 --  When a task goes away, we want to be able to recover all the storage
59 --  associated with its attributes. The Ada mechanism for this is finalization,
60 --  via controlled attribute types. For this reason, the ARM requires
61 --  finalization of attribute values when the associated task terminates.
62
63 --  This finalization must be triggered by the tasking runtime system, during
64 --  termination of the task. Given the active set of instantiations of
65 --  Ada.Task_Attributes is dynamic, the number and types of attributes
66 --  belonging to a task will not be known until the task actually terminates.
67 --  Some of these types may be controlled and some may not. The RTS must find
68 --  some way to determine which of these attributes need finalization, and
69 --  invoke the appropriate finalization on them.
70
71 --  One way this might be done is to create a special finalization chain for
72 --  each task, similar to the finalization chain that is used for controlled
73 --  objects within the task. This would differ from the usual finalization
74 --  chain in that it would not have a LIFO structure, since attributes may be
75 --  added to a task at any time during its lifetime. This might be the right
76 --  way to go for the longer term, but at present this approach is not open,
77 --  since GNAT does not provide such special finalization support.
78
79 --  Lacking special compiler support, the RTS is limited to the normal ways an
80 --  application invokes finalization, i.e.
81
82 --  a) Explicit call to the procedure Finalize, if we know the type has this
83 --     operation defined on it. This is not sufficient, since we have no way
84 --     of determining whether a given generic formal Attribute type is
85 --     controlled, and no visibility of the associated Finalize procedure, in
86 --     the generic body.
87
88 --  b) Leaving the scope of a local object of a controlled type. This does not
89 --     help, since the lifetime of an instantiation of Ada.Task_Attributes
90 --     does not correspond to the lifetimes of the various tasks which may
91 --     have that attribute.
92
93 --  c) Assignment of another value to the object. This would not help, since
94 --     we then have to finalize the new value of the object.
95
96 --  d) Unchecked deallocation of an object of a controlled type. This seems to
97 --     be the only mechanism available to the runtime system for finalization
98 --     of task attributes.
99
100 --  We considered two ways of using unchecked deallocation, both based on a
101 --  linked list of that would hang from the task control block.
102
103 --  In the first approach the objects on the attribute list are all derived
104 --  from one controlled type, say T, and are linked using an access type to
105 --  T'Class. The runtime system has an Ada.Unchecked_Deallocation for T'Class
106 --  with access type T'Class, and uses this to deallocate and finalize all the
107 --  items in the list. The limitation of this approach is that each
108 --  instantiation of the package Ada.Task_Attributes derives a new record
109 --  extension of T, and since T is controlled (RM 3.9.1 (3)), instantiation is
110 --  only allowed at the library level.
111
112 --  In the second approach the objects on the attribute list are of unrelated
113 --  but structurally similar types. Unchecked conversion is used to circument
114 --  Ada type checking. Each attribute-storage node contains not only the
115 --  attribute value and a link for chaining, but also a pointer to descriptor
116 --  for the corresponding instantiation of Task_Attributes. The instantiation
117 --  descriptor contains pointer to a procedure that can do the correct
118 --  deallocation and finalization for that type of attribute. On task
119 --  termination, the runtime system uses the pointer to call the appropriate
120 --  deallocator.
121
122 --  While this gets around the limitation that instantations be at the library
123 --  level, it relies on an implementation feature that may not always be safe,
124 --  i.e. that it is safe to call the Deallocate procedure for an instantiation
125 --  of Ada.Task_Attributes that no longer exists. In general, it seems this
126 --  might result in dangling references.
127
128 --  Another problem with instantiations deeper than the library level is that
129 --  there is risk of storage leakage, or dangling references to reused storage.
130 --  That is, if an instantiation of Ada.Task_Attributes is made within a
131 --  procedure, what happens to the storage allocated for attributes, when the
132 --  procedure call returns? Apparently (RM 7.6.1 (4)) any such objects must be
133 --  finalized, since they will no longer be accessible, and in general one
134 --  would expect that the storage they occupy would be recovered for later
135 --  reuse. (If not, we would have a case of storage leakage.) Assuming the
136 --  storage is recovered and later reused, we have potentially dangerous
137 --  dangling references. When the procedure containing the instantiation of
138 --  Ada.Task_Attributes returns, there may still be unterminated tasks with
139 --  associated attribute values for that instantiation. When such tasks
140 --  eventually terminate, the RTS will attempt to call the Deallocate procedure
141 --  on them. If the corresponding storage has already been deallocated, when
142 --  the master of the access type was left, we have a potential disaster. This
143 --  disaster is compounded since the pointer to Deallocate is probably through
144 --  a "trampoline" which will also have been destroyed.
145
146 --  For this reason, we arrange to remove all dangling references before
147 --  leaving the scope of an instantiation. This is ugly, since it requires
148 --  traversing the list of all tasks, but it is no more ugly than a similar
149 --  traversal that we must do at the point of instantiation in order to
150 --  initialize the attributes of all tasks. At least we only need to do these
151 --  traversals if the type is controlled.
152
153 --  We chose to defer allocation of storage for attributes until the Reference
154 --  function is called or the attribute is first set to a value different from
155 --  the default initial one. This allows a potential savings in allocation,
156 --  for attributes that are not used by all tasks.
157
158 --  For efficiency, we reserve space in the TCB for a fixed number of direct-
159 --  access attributes. These are required to be of a size that fits in the
160 --  space of an object of type System.Address. Because we must use unchecked
161 --  bitwise copy operations on these values, they cannot be of a controlled
162 --  type, but that is covered automatically since controlled objects are too
163 --  large to fit in the spaces.
164
165 --  We originally deferred initialization of these direct-access attributes,
166 --  just as we do for the indirect-access attributes, and used a per-task bit
167 --  vector to keep track of which attributes were currently defined for that
168 --  task. We found that the overhead of maintaining this bit-vector seriously
169 --  slowed down access to the attributes, and made the fetch operation non-
170 --  atomic, so that even to read an attribute value required locking the TCB.
171 --  Therefore, we now initialize such attributes for all existing tasks at the
172 --  time of the attribute instantiation, and initialize existing attributes for
173 --  each new task at the time it is created.
174
175 --  The latter initialization requires a list of all the instantiation
176 --  descriptors. Updates to this list, as well as the bit-vector that is used
177 --  to reserve slots for attributes in the TCB, require mutual exclusion. That
178 --  is provided by the Lock/Unlock_RTS.
179
180 --  One special problem that added complexity to the design is that the per-
181 --  task list of indirect attributes contains objects of different types. We
182 --  use unchecked pointer conversion to link these nodes together and access
183 --  them, but the records may not have identical internal structure. Initially,
184 --  we thought it would be enough to allocate all the common components of
185 --  the records at the front of each record, so that their positions would
186 --  correspond. Unfortunately, GNAT adds "dope" information at the front
187 --  of a record, if the record contains any controlled-type components.
188 --
189 --  This means that the offset of the fields we use to link the nodes is at
190 --  different positions on nodes of different types. To get around this, each
191 --  attribute storage record consists of a core node and wrapper. The core
192 --  nodes are all of the same type, and it is these that are linked together
193 --  and generally "seen" by the RTS. Each core node contains a pointer to its
194 --  own wrapper, which is a record that contains the core node along with an
195 --  attribute value, approximately as follows:
196
197 --    type Node;
198 --    type Node_Access is access all Node;
199 --    type Wrapper;
200 --    type Access_Wrapper is access all Wrapper;
201 --    type Node is record
202 --       Next    : Node_Access;
203 --       ...
204 --       Wrapper : Access_Wrapper;
205 --    end record;
206 --    type Wrapper is record
207 --       Dummy_Node : aliased Node;
208 --       Value      : aliased Attribute;  --  the generic formal type
209 --    end record;
210
211 --  Another interesting problem is with the initialization of the instantiation
212 --  descriptors. Originally, we did this all via the Initialize procedure of
213 --  the descriptor type and code in the package body. It turned out that the
214 --  Initialize procedure needed quite a bit of information, including the size
215 --  of the attribute type, the initial value of the attribute (if it fits in
216 --  the TCB), and a pointer to the deallocator procedure. These needed to be
217 --  "passed" in via access discriminants. GNAT was having trouble with access
218 --  discriminants, so all this work was moved to the package body.
219
220 --  Note that references to objects declared in this package body must in
221 --  general use 'Unchecked_Access instead of 'Access as the package can be
222 --  instantiated from within a local context.
223
224 with System.Error_Reporting;
225 with System.Storage_Elements;
226 with System.Task_Primitives.Operations;
227 with System.Tasking;
228 with System.Tasking.Initialization;
229 with System.Tasking.Task_Attributes;
230
231 with Ada.Exceptions;
232 with Ada.Unchecked_Conversion;
233 with Ada.Unchecked_Deallocation;
234
235 pragma Elaborate_All (System.Tasking.Task_Attributes);
236 --  To ensure the initialization of object Local (below) will work
237
238 package body Ada.Task_Attributes is
239
240    use System.Error_Reporting,
241        System.Tasking.Initialization,
242        System.Tasking,
243        System.Tasking.Task_Attributes,
244        Ada.Exceptions;
245
246    package POP renames System.Task_Primitives.Operations;
247
248    ---------------------------
249    -- Unchecked Conversions --
250    ---------------------------
251
252    --  The following type corresponds to Dummy_Wrapper,
253    --  declared in System.Tasking.Task_Attributes.
254
255    type Wrapper;
256    type Access_Wrapper is access all Wrapper;
257
258    pragma Warnings (Off);
259    --  We turn warnings off for the following To_Attribute_Handle conversions,
260    --  since these are used only for small attributes where we know that there
261    --  are no problems with alignment, but the compiler will generate warnings
262    --  for the occurrences in the large attribute case, even though they will
263    --  not actually be used.
264
265    function To_Attribute_Handle is new Ada.Unchecked_Conversion
266      (System.Address, Attribute_Handle);
267    function To_Direct_Attribute_Element is new Ada.Unchecked_Conversion
268      (System.Address, Direct_Attribute_Element);
269    --  For reference to directly addressed task attributes
270
271    type Access_Integer_Address is access all
272      System.Storage_Elements.Integer_Address;
273
274    function To_Attribute_Handle is new Ada.Unchecked_Conversion
275      (Access_Integer_Address, Attribute_Handle);
276    --  For reference to directly addressed task attributes
277
278    pragma Warnings (On);
279    --  End of warnings off region for directly addressed
280    --  attribute conversion functions.
281
282    function To_Access_Address is new Ada.Unchecked_Conversion
283      (Access_Node, Access_Address);
284    --  To store pointer to list of indirect attributes
285
286    pragma Warnings (Off);
287    function To_Access_Wrapper is new Ada.Unchecked_Conversion
288      (Access_Dummy_Wrapper, Access_Wrapper);
289    pragma Warnings (On);
290    --  To fetch pointer to actual wrapper of attribute node. We turn off
291    --  warnings since this may generate an alignment warning. The warning can
292    --  be ignored since Dummy_Wrapper is only a non-generic standin for the
293    --  real wrapper type (we never actually allocate objects of type
294    --  Dummy_Wrapper).
295
296    function To_Access_Dummy_Wrapper is new Ada.Unchecked_Conversion
297      (Access_Wrapper, Access_Dummy_Wrapper);
298    --  To store pointer to actual wrapper of attribute node
299
300    function To_Task_Id is new Ada.Unchecked_Conversion
301      (Task_Identification.Task_Id, Task_Id);
302    --  To access TCB of identified task
303
304    type Local_Deallocator is access procedure (P : in out Access_Node);
305
306    function To_Lib_Level_Deallocator is new Ada.Unchecked_Conversion
307      (Local_Deallocator, Deallocator);
308    --  To defeat accessibility check
309
310    pragma Warnings (On);
311
312    ------------------------
313    -- Storage Management --
314    ------------------------
315
316    procedure Deallocate (P : in out Access_Node);
317    --  Passed to the RTS via unchecked conversion of a pointer to permit
318    --  finalization and deallocation of attribute storage nodes.
319
320    --------------------------
321    -- Instantiation Record --
322    --------------------------
323
324    Local : aliased Instance;
325    --  Initialized in package body
326
327    type Wrapper is record
328       Dummy_Node : aliased Node;
329
330       Value : aliased Attribute := Initial_Value;
331       --  The generic formal type, may be controlled
332    end record;
333
334    --  A number of unchecked conversions involving Wrapper_Access sources are
335    --  performed in this unit. We have to ensure that the designated object is
336    --  always strictly enough aligned.
337
338    for Wrapper'Alignment use Standard'Maximum_Alignment;
339
340    procedure Free is
341       new Ada.Unchecked_Deallocation (Wrapper, Access_Wrapper);
342
343    procedure Deallocate (P : in out Access_Node) is
344       T : Access_Wrapper := To_Access_Wrapper (P.Wrapper);
345    begin
346       Free (T);
347    end Deallocate;
348
349    ---------------
350    -- Reference --
351    ---------------
352
353    function Reference
354      (T    : Task_Identification.Task_Id := Task_Identification.Current_Task)
355       return Attribute_Handle
356    is
357       TT            : constant Task_Id := To_Task_Id (T);
358       Error_Message : constant String  := "Trying to get the reference of a ";
359
360    begin
361       if TT = null then
362          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
363       end if;
364
365       if TT.Common.State = Terminated then
366          Raise_Exception (Tasking_Error'Identity,
367            Error_Message & "terminated task");
368       end if;
369
370       --  Directly addressed case
371
372       if Local.Index /= 0 then
373
374          --  Return the attribute handle. Warnings off because this return
375          --  statement generates alignment warnings for large attributes
376          --  (but will never be executed in this case anyway).
377
378          pragma Warnings (Off);
379          return
380            To_Attribute_Handle (TT.Direct_Attributes (Local.Index)'Address);
381          pragma Warnings (On);
382
383       --  Not directly addressed
384
385       else
386          declare
387             P       : Access_Node := To_Access_Node (TT.Indirect_Attributes);
388             W       : Access_Wrapper;
389             Self_Id : constant Task_Id := POP.Self;
390
391          begin
392             Defer_Abort (Self_Id);
393             POP.Lock_RTS;
394
395             while P /= null loop
396                if P.Instance = Access_Instance'(Local'Unchecked_Access) then
397                   POP.Unlock_RTS;
398                   Undefer_Abort (Self_Id);
399                   return To_Access_Wrapper (P.Wrapper).Value'Access;
400                end if;
401
402                P := P.Next;
403             end loop;
404
405             --  Unlock the RTS here to follow the lock ordering rule
406             --  that prevent us from using new (i.e the Global_Lock) while
407             --  holding any other lock.
408
409             POP.Unlock_RTS;
410             W := new Wrapper'
411                   ((null, Local'Unchecked_Access, null), Initial_Value);
412             POP.Lock_RTS;
413
414             P := W.Dummy_Node'Unchecked_Access;
415             P.Wrapper := To_Access_Dummy_Wrapper (W);
416             P.Next := To_Access_Node (TT.Indirect_Attributes);
417             TT.Indirect_Attributes := To_Access_Address (P);
418             POP.Unlock_RTS;
419             Undefer_Abort (Self_Id);
420             return W.Value'Access;
421
422          exception
423             when others =>
424                POP.Unlock_RTS;
425                Undefer_Abort (Self_Id);
426                raise;
427          end;
428       end if;
429
430       pragma Assert (Shutdown ("Should never get here in Reference"));
431       return null;
432
433    exception
434       when Tasking_Error | Program_Error =>
435          raise;
436
437       when others =>
438          raise Program_Error;
439    end Reference;
440
441    ------------------
442    -- Reinitialize --
443    ------------------
444
445    procedure Reinitialize
446      (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
447    is
448       TT            : constant Task_Id := To_Task_Id (T);
449       Error_Message : constant String  := "Trying to Reinitialize a ";
450
451    begin
452       if TT = null then
453          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
454       end if;
455
456       if TT.Common.State = Terminated then
457          Raise_Exception (Tasking_Error'Identity,
458            Error_Message & "terminated task");
459       end if;
460
461       if Local.Index /= 0 then
462          Set_Value (Initial_Value, T);
463       else
464          declare
465             P, Q    : Access_Node;
466             W       : Access_Wrapper;
467             Self_Id : constant Task_Id := POP.Self;
468
469          begin
470             Defer_Abort (Self_Id);
471             POP.Lock_RTS;
472             Q := To_Access_Node (TT.Indirect_Attributes);
473
474             while Q /= null loop
475                if Q.Instance = Access_Instance'(Local'Unchecked_Access) then
476                   if P = null then
477                      TT.Indirect_Attributes := To_Access_Address (Q.Next);
478                   else
479                      P.Next := Q.Next;
480                   end if;
481
482                   W := To_Access_Wrapper (Q.Wrapper);
483                   Free (W);
484                   POP.Unlock_RTS;
485                   Undefer_Abort (Self_Id);
486                   return;
487                end if;
488
489                P := Q;
490                Q := Q.Next;
491             end loop;
492
493             POP.Unlock_RTS;
494             Undefer_Abort (Self_Id);
495
496          exception
497             when others =>
498                POP.Unlock_RTS;
499                Undefer_Abort (Self_Id);
500                raise;
501          end;
502       end if;
503
504    exception
505       when Tasking_Error | Program_Error =>
506          raise;
507
508       when others =>
509          raise Program_Error;
510    end Reinitialize;
511
512    ---------------
513    -- Set_Value --
514    ---------------
515
516    procedure Set_Value
517      (Val : Attribute;
518       T   : Task_Identification.Task_Id := Task_Identification.Current_Task)
519    is
520       TT            : constant Task_Id := To_Task_Id (T);
521       Error_Message : constant String  := "Trying to Set the Value of a ";
522
523    begin
524       if TT = null then
525          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
526       end if;
527
528       if TT.Common.State = Terminated then
529          Raise_Exception (Tasking_Error'Identity,
530            Error_Message & "terminated task");
531       end if;
532
533       --  Directly addressed case
534
535       if Local.Index /= 0 then
536
537          --  Set attribute handle, warnings off, because this code can generate
538          --  alignment warnings with large attributes (but of course will not
539          --  be executed in this case, since we never have direct addressing in
540          --  such cases).
541
542          pragma Warnings (Off);
543          To_Attribute_Handle
544             (TT.Direct_Attributes (Local.Index)'Address).all := Val;
545          pragma Warnings (On);
546          return;
547       end if;
548
549       --  Not directly addressed
550
551       declare
552          P       : Access_Node := To_Access_Node (TT.Indirect_Attributes);
553          W       : Access_Wrapper;
554          Self_Id : constant Task_Id := POP.Self;
555
556       begin
557          Defer_Abort (Self_Id);
558          POP.Lock_RTS;
559
560          while P /= null loop
561
562             if P.Instance = Access_Instance'(Local'Unchecked_Access) then
563                To_Access_Wrapper (P.Wrapper).Value := Val;
564                POP.Unlock_RTS;
565                Undefer_Abort (Self_Id);
566                return;
567             end if;
568
569             P := P.Next;
570          end loop;
571
572          --  Unlock RTS here to follow the lock ordering rule that prevent us
573          --  from using new (i.e the Global_Lock) while holding any other lock.
574
575          POP.Unlock_RTS;
576          W := new Wrapper'((null, Local'Unchecked_Access, null), Val);
577          POP.Lock_RTS;
578          P := W.Dummy_Node'Unchecked_Access;
579          P.Wrapper := To_Access_Dummy_Wrapper (W);
580          P.Next := To_Access_Node (TT.Indirect_Attributes);
581          TT.Indirect_Attributes := To_Access_Address (P);
582
583          POP.Unlock_RTS;
584          Undefer_Abort (Self_Id);
585
586       exception
587          when others =>
588             POP.Unlock_RTS;
589             Undefer_Abort (Self_Id);
590             raise;
591       end;
592
593    exception
594       when Tasking_Error | Program_Error =>
595          raise;
596
597       when others =>
598          raise Program_Error;
599    end Set_Value;
600
601    -----------
602    -- Value --
603    -----------
604
605    function Value
606      (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
607       return Attribute
608    is
609       TT            : constant Task_Id := To_Task_Id (T);
610       Error_Message : constant String  := "Trying to get the Value of a ";
611
612    begin
613       if TT = null then
614          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
615       end if;
616
617       if TT.Common.State = Terminated then
618          Raise_Exception
619            (Program_Error'Identity, Error_Message & "terminated task");
620       end if;
621
622       --  Directly addressed case
623
624       if Local.Index /= 0 then
625
626          --  Get value of attribute. We turn Warnings off, because for large
627          --  attributes, this code can generate alignment warnings. But of
628          --  course large attributes are never directly addressed so in fact
629          --  we will never execute the code in this case.
630
631          pragma Warnings (Off);
632          return To_Attribute_Handle
633            (TT.Direct_Attributes (Local.Index)'Address).all;
634          pragma Warnings (On);
635       end if;
636
637       --  Not directly addressed
638
639       declare
640          P       : Access_Node;
641          Result  : Attribute;
642          Self_Id : constant Task_Id := POP.Self;
643
644       begin
645          Defer_Abort (Self_Id);
646          POP.Lock_RTS;
647          P := To_Access_Node (TT.Indirect_Attributes);
648
649          while P /= null loop
650             if P.Instance = Access_Instance'(Local'Unchecked_Access) then
651                Result := To_Access_Wrapper (P.Wrapper).Value;
652                POP.Unlock_RTS;
653                Undefer_Abort (Self_Id);
654                return Result;
655             end if;
656
657             P := P.Next;
658          end loop;
659
660          POP.Unlock_RTS;
661          Undefer_Abort (Self_Id);
662          return Initial_Value;
663
664       exception
665          when others =>
666             POP.Unlock_RTS;
667             Undefer_Abort (Self_Id);
668             raise;
669       end;
670
671    exception
672       when Tasking_Error | Program_Error =>
673          raise;
674
675       when others =>
676          raise Program_Error;
677    end Value;
678
679 --  Start of elaboration code for package Ada.Task_Attributes
680
681 begin
682    --  This unchecked conversion can give warnings when alignments are
683    --  incorrect, but they will not be used in such cases anyway, so the
684    --  warnings can be safely ignored.
685
686    pragma Warnings (Off);
687    Local.Deallocate := To_Lib_Level_Deallocator (Deallocate'Access);
688    pragma Warnings (On);
689
690    declare
691       Two_To_J : Direct_Index_Vector;
692       Self_Id  : constant Task_Id := POP.Self;
693    begin
694       Defer_Abort (Self_Id);
695
696       --  Need protection for updating links to per-task initialization and
697       --  finalization routines, in case some task is being created or
698       --  terminated concurrently.
699
700       POP.Lock_RTS;
701
702       --  Add this instantiation to the list of all instantiations
703
704       Local.Next := System.Tasking.Task_Attributes.All_Attributes;
705       System.Tasking.Task_Attributes.All_Attributes :=
706         Local'Unchecked_Access;
707
708       --  Try to find space for the attribute in the TCB
709
710       Local.Index := 0;
711       Two_To_J := 1;
712
713       if Attribute'Size <= System.Address'Size then
714          for J in Direct_Index_Range loop
715             if (Two_To_J and In_Use) = 0 then
716
717                --  Reserve location J for this attribute
718
719                In_Use := In_Use or Two_To_J;
720                Local.Index := J;
721
722                --  This unchecked conversions can give a warning when the the
723                --  alignment is incorrect, but it will not be used in such a
724                --  case anyway, so the warning can be safely ignored.
725
726                pragma Warnings (Off);
727                To_Attribute_Handle (Local.Initial_Value'Access).all :=
728                  Initial_Value;
729                pragma Warnings (On);
730
731                exit;
732             end if;
733
734             Two_To_J := Two_To_J * 2;
735          end loop;
736       end if;
737
738       --  Attribute goes directly in the TCB
739
740       if Local.Index /= 0 then
741          --  Replace stub for initialization routine that is called at task
742          --  creation.
743
744          Initialization.Initialize_Attributes_Link :=
745            System.Tasking.Task_Attributes.Initialize_Attributes'Access;
746
747          --  Initialize the attribute, for all tasks
748
749          declare
750             C : System.Tasking.Task_Id := System.Tasking.All_Tasks_List;
751          begin
752             while C /= null loop
753                C.Direct_Attributes (Local.Index) :=
754                  To_Direct_Attribute_Element
755                    (System.Storage_Elements.To_Address (Local.Initial_Value));
756                C := C.Common.All_Tasks_Link;
757             end loop;
758          end;
759
760       --  Attribute goes into a node onto a linked list
761
762       else
763          --  Replace stub for finalization routine called at task termination
764
765          Initialization.Finalize_Attributes_Link :=
766            System.Tasking.Task_Attributes.Finalize_Attributes'Access;
767       end if;
768
769       POP.Unlock_RTS;
770       Undefer_Abort (Self_Id);
771    end;
772 end Ada.Task_Attributes;