OSDN Git Service

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