OSDN Git Service

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