OSDN Git Service

2007-08-14 Robert Dewar <dewar@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-2007, 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 Node_Access;
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 with System.Error_Reporting;
221 --  Used for Shutdown;
222
223 with System.Storage_Elements;
224 --  Used for Integer_Address
225
226 with System.Task_Primitives.Operations;
227 --  Used for Write_Lock
228 --           Unlock
229 --           Lock/Unlock_RTS
230
231 with System.Tasking;
232 --  Used for Access_Address
233 --           Task_Id
234 --           Direct_Index_Vector
235 --           Direct_Index
236
237 with System.Tasking.Initialization;
238 --  Used for Defer_Abort
239 --           Undefer_Abort
240 --           Initialize_Attributes_Link
241 --           Finalize_Attributes_Link
242
243 with System.Tasking.Task_Attributes;
244 --  Used for Access_Node
245 --           Access_Dummy_Wrapper
246 --           Deallocator
247 --           Instance
248 --           Node
249 --           Access_Instance
250
251 with Ada.Exceptions;
252 --  Used for Raise_Exception
253
254 with Ada.Unchecked_Conversion;
255 with Ada.Unchecked_Deallocation;
256
257 pragma Elaborate_All (System.Tasking.Task_Attributes);
258 --  To ensure the initialization of object Local (below) will work
259
260 package body Ada.Task_Attributes is
261
262    use System.Error_Reporting,
263        System.Tasking.Initialization,
264        System.Tasking,
265        System.Tasking.Task_Attributes,
266        Ada.Exceptions;
267
268    use type System.Tasking.Access_Address;
269
270    package POP renames System.Task_Primitives.Operations;
271
272    ---------------------------
273    -- Unchecked Conversions --
274    ---------------------------
275
276    --  The following type corresponds to Dummy_Wrapper,
277    --  declared in System.Tasking.Task_Attributes.
278
279    type Wrapper;
280    type Access_Wrapper is access all Wrapper;
281
282    pragma Warnings (Off);
283    --  We turn warnings off for the following To_Attribute_Handle conversions,
284    --  since these are used only for small attributes where we know that there
285    --  are no problems with alignment, but the compiler will generate warnings
286    --  for the occurrences in the large attribute case, even though they will
287    --  not actually be used.
288
289    function To_Attribute_Handle is new Ada.Unchecked_Conversion
290      (System.Address, Attribute_Handle);
291    function To_Direct_Attribute_Element is new Ada.Unchecked_Conversion
292      (System.Address, Direct_Attribute_Element);
293    --  For reference to directly addressed task attributes
294
295    type Access_Integer_Address is access all
296      System.Storage_Elements.Integer_Address;
297
298    function To_Attribute_Handle is new Ada.Unchecked_Conversion
299      (Access_Integer_Address, Attribute_Handle);
300    --  For reference to directly addressed task attributes
301
302    pragma Warnings (On);
303    --  End of warnings off region for directly addressed
304    --  attribute conversion functions.
305
306    function To_Access_Address is new Ada.Unchecked_Conversion
307      (Access_Node, Access_Address);
308    --  To store pointer to list of indirect attributes
309
310    pragma Warnings (Off);
311    function To_Access_Wrapper is new Ada.Unchecked_Conversion
312      (Access_Dummy_Wrapper, Access_Wrapper);
313    pragma Warnings (On);
314    --  To fetch pointer to actual wrapper of attribute node. We turn off
315    --  warnings since this may generate an alignment warning. The warning can
316    --  be ignored since Dummy_Wrapper is only a non-generic standin for the
317    --  real wrapper type (we never actually allocate objects of type
318    --  Dummy_Wrapper).
319
320    function To_Access_Dummy_Wrapper is new Ada.Unchecked_Conversion
321      (Access_Wrapper, Access_Dummy_Wrapper);
322    --  To store pointer to actual wrapper of attribute node
323
324    function To_Task_Id is new Ada.Unchecked_Conversion
325      (Task_Identification.Task_Id, Task_Id);
326    --  To access TCB of identified task
327
328    type Local_Deallocator is access procedure (P : in out Access_Node);
329
330    function To_Lib_Level_Deallocator is new Ada.Unchecked_Conversion
331      (Local_Deallocator, Deallocator);
332    --  To defeat accessibility check
333
334    pragma Warnings (On);
335
336    ------------------------
337    -- Storage Management --
338    ------------------------
339
340    procedure Deallocate (P : in out Access_Node);
341    --  Passed to the RTS via unchecked conversion of a pointer to permit
342    --  finalization and deallocation of attribute storage nodes.
343
344    --------------------------
345    -- Instantiation Record --
346    --------------------------
347
348    Local : aliased Instance;
349    --  Initialized in package body
350
351    type Wrapper is record
352       Dummy_Node : aliased Node;
353
354       Value : aliased Attribute := Initial_Value;
355       --  The generic formal type, may be controlled
356    end record;
357
358    --  A number of unchecked conversions involving Wrapper_Access sources are
359    --  performed in this unit. We have to ensure that the designated object is
360    --  always strictly enough aligned.
361
362    for Wrapper'Alignment use Standard'Maximum_Alignment;
363
364    procedure Free is
365       new Ada.Unchecked_Deallocation (Wrapper, Access_Wrapper);
366
367    procedure Deallocate (P : in out Access_Node) is
368       T : Access_Wrapper := To_Access_Wrapper (P.Wrapper);
369    begin
370       Free (T);
371    end Deallocate;
372
373    ---------------
374    -- Reference --
375    ---------------
376
377    function Reference
378      (T    : Task_Identification.Task_Id := Task_Identification.Current_Task)
379       return Attribute_Handle
380    is
381       TT            : constant Task_Id := To_Task_Id (T);
382       Error_Message : constant String  := "Trying to get the reference of a ";
383
384    begin
385       if TT = null then
386          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
387       end if;
388
389       if TT.Common.State = Terminated then
390          Raise_Exception (Tasking_Error'Identity,
391            Error_Message & "terminated task");
392       end if;
393
394       --  Directly addressed case
395
396       if Local.Index /= 0 then
397
398          --  Return the attribute handle. Warnings off because this return
399          --  statement generates alignment warnings for large attributes
400          --  (but will never be executed in this case anyway).
401
402          pragma Warnings (Off);
403          return
404            To_Attribute_Handle (TT.Direct_Attributes (Local.Index)'Address);
405          pragma Warnings (On);
406
407       --  Not directly addressed
408
409       else
410          declare
411             P       : Access_Node := To_Access_Node (TT.Indirect_Attributes);
412             W       : Access_Wrapper;
413             Self_Id : constant Task_Id := POP.Self;
414
415          begin
416             Defer_Abort (Self_Id);
417             POP.Lock_RTS;
418
419             while P /= null loop
420                if P.Instance = Access_Instance'(Local'Unchecked_Access) then
421                   POP.Unlock_RTS;
422                   Undefer_Abort (Self_Id);
423                   return To_Access_Wrapper (P.Wrapper).Value'Access;
424                end if;
425
426                P := P.Next;
427             end loop;
428
429             --  Unlock the RTS here to follow the lock ordering rule
430             --  that prevent us from using new (i.e the Global_Lock) while
431             --  holding any other lock.
432
433             POP.Unlock_RTS;
434             W := new Wrapper'
435                   ((null, Local'Unchecked_Access, null), Initial_Value);
436             POP.Lock_RTS;
437
438             P := W.Dummy_Node'Unchecked_Access;
439             P.Wrapper := To_Access_Dummy_Wrapper (W);
440             P.Next := To_Access_Node (TT.Indirect_Attributes);
441             TT.Indirect_Attributes := To_Access_Address (P);
442             POP.Unlock_RTS;
443             Undefer_Abort (Self_Id);
444             return W.Value'Access;
445
446          exception
447             when others =>
448                POP.Unlock_RTS;
449                Undefer_Abort (Self_Id);
450                raise;
451          end;
452       end if;
453
454       pragma Assert (Shutdown ("Should never get here in Reference"));
455       return null;
456
457    exception
458       when Tasking_Error | Program_Error =>
459          raise;
460
461       when others =>
462          raise Program_Error;
463    end Reference;
464
465    ------------------
466    -- Reinitialize --
467    ------------------
468
469    procedure Reinitialize
470      (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
471    is
472       TT            : constant Task_Id := To_Task_Id (T);
473       Error_Message : constant String  := "Trying to Reinitialize a ";
474
475    begin
476       if TT = null then
477          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
478       end if;
479
480       if TT.Common.State = Terminated then
481          Raise_Exception (Tasking_Error'Identity,
482            Error_Message & "terminated task");
483       end if;
484
485       if Local.Index /= 0 then
486          Set_Value (Initial_Value, T);
487       else
488          declare
489             P, Q    : Access_Node;
490             W       : Access_Wrapper;
491             Self_Id : constant Task_Id := POP.Self;
492
493          begin
494             Defer_Abort (Self_Id);
495             POP.Lock_RTS;
496             Q := To_Access_Node (TT.Indirect_Attributes);
497
498             while Q /= null loop
499                if Q.Instance = Access_Instance'(Local'Unchecked_Access) then
500                   if P = null then
501                      TT.Indirect_Attributes := To_Access_Address (Q.Next);
502                   else
503                      P.Next := Q.Next;
504                   end if;
505
506                   W := To_Access_Wrapper (Q.Wrapper);
507                   Free (W);
508                   POP.Unlock_RTS;
509                   Undefer_Abort (Self_Id);
510                   return;
511                end if;
512
513                P := Q;
514                Q := Q.Next;
515             end loop;
516
517             POP.Unlock_RTS;
518             Undefer_Abort (Self_Id);
519
520          exception
521             when others =>
522                POP.Unlock_RTS;
523                Undefer_Abort (Self_Id);
524                raise;
525          end;
526       end if;
527
528    exception
529       when Tasking_Error | Program_Error =>
530          raise;
531
532       when others =>
533          raise Program_Error;
534    end Reinitialize;
535
536    ---------------
537    -- Set_Value --
538    ---------------
539
540    procedure Set_Value
541      (Val : Attribute;
542       T   : Task_Identification.Task_Id := Task_Identification.Current_Task)
543    is
544       TT            : constant Task_Id := To_Task_Id (T);
545       Error_Message : constant String  := "Trying to Set the Value of a ";
546
547    begin
548       if TT = null then
549          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
550       end if;
551
552       if TT.Common.State = Terminated then
553          Raise_Exception (Tasking_Error'Identity,
554            Error_Message & "terminated task");
555       end if;
556
557       --  Directly addressed case
558
559       if Local.Index /= 0 then
560
561          --  Set attribute handle, warnings off, because this code can generate
562          --  alignment warnings with large attributes (but of course will not
563          --  be executed in this case, since we never have direct addressing in
564          --  such cases).
565
566          pragma Warnings (Off);
567          To_Attribute_Handle
568             (TT.Direct_Attributes (Local.Index)'Address).all := Val;
569          pragma Warnings (On);
570          return;
571       end if;
572
573       --  Not directly addressed
574
575       declare
576          P       : Access_Node := To_Access_Node (TT.Indirect_Attributes);
577          W       : Access_Wrapper;
578          Self_Id : constant Task_Id := POP.Self;
579
580       begin
581          Defer_Abort (Self_Id);
582          POP.Lock_RTS;
583
584          while P /= null loop
585
586             if P.Instance = Access_Instance'(Local'Unchecked_Access) then
587                To_Access_Wrapper (P.Wrapper).Value := Val;
588                POP.Unlock_RTS;
589                Undefer_Abort (Self_Id);
590                return;
591             end if;
592
593             P := P.Next;
594          end loop;
595
596          --  Unlock RTS here to follow the lock ordering rule that prevent us
597          --  from using new (i.e the Global_Lock) while holding any other lock.
598
599          POP.Unlock_RTS;
600          W := new Wrapper'((null, Local'Unchecked_Access, null), Val);
601          POP.Lock_RTS;
602          P := W.Dummy_Node'Unchecked_Access;
603          P.Wrapper := To_Access_Dummy_Wrapper (W);
604          P.Next := To_Access_Node (TT.Indirect_Attributes);
605          TT.Indirect_Attributes := To_Access_Address (P);
606
607          POP.Unlock_RTS;
608          Undefer_Abort (Self_Id);
609
610       exception
611          when others =>
612             POP.Unlock_RTS;
613             Undefer_Abort (Self_Id);
614             raise;
615       end;
616
617    exception
618       when Tasking_Error | Program_Error =>
619          raise;
620
621       when others =>
622          raise Program_Error;
623    end Set_Value;
624
625    -----------
626    -- Value --
627    -----------
628
629    function Value
630      (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
631       return Attribute
632    is
633       TT            : constant Task_Id := To_Task_Id (T);
634       Error_Message : constant String  := "Trying to get the Value of a ";
635
636    begin
637       if TT = null then
638          Raise_Exception (Program_Error'Identity, Error_Message & "null task");
639       end if;
640
641       if TT.Common.State = Terminated then
642          Raise_Exception
643            (Program_Error'Identity, Error_Message & "terminated task");
644       end if;
645
646       --  Directly addressed case
647
648       if Local.Index /= 0 then
649
650          --  Get value of attribute. We turn Warnings off, because for large
651          --  attributes, this code can generate alignment warnings. But of
652          --  course large attributes are never directly addressed so in fact
653          --  we will never execute the code in this case.
654
655          pragma Warnings (Off);
656          return To_Attribute_Handle
657            (TT.Direct_Attributes (Local.Index)'Address).all;
658          pragma Warnings (On);
659       end if;
660
661       --  Not directly addressed
662
663       declare
664          P       : Access_Node;
665          Result  : Attribute;
666          Self_Id : constant Task_Id := POP.Self;
667
668       begin
669          Defer_Abort (Self_Id);
670          POP.Lock_RTS;
671          P := To_Access_Node (TT.Indirect_Attributes);
672
673          while P /= null loop
674             if P.Instance = Access_Instance'(Local'Unchecked_Access) then
675                Result := To_Access_Wrapper (P.Wrapper).Value;
676                POP.Unlock_RTS;
677                Undefer_Abort (Self_Id);
678                return Result;
679             end if;
680
681             P := P.Next;
682          end loop;
683
684          POP.Unlock_RTS;
685          Undefer_Abort (Self_Id);
686          return Initial_Value;
687
688       exception
689          when others =>
690             POP.Unlock_RTS;
691             Undefer_Abort (Self_Id);
692             raise;
693       end;
694
695    exception
696       when Tasking_Error | Program_Error =>
697          raise;
698
699       when others =>
700          raise Program_Error;
701    end Value;
702
703 --  Start of elaboration code for package Ada.Task_Attributes
704
705 begin
706    --  This unchecked conversion can give warnings when alignments are
707    --  incorrect, but they will not be used in such cases anyway, so the
708    --  warnings can be safely ignored.
709
710    pragma Warnings (Off);
711    Local.Deallocate := To_Lib_Level_Deallocator (Deallocate'Access);
712    pragma Warnings (On);
713
714    declare
715       Two_To_J : Direct_Index_Vector;
716       Self_Id  : constant Task_Id := POP.Self;
717    begin
718       Defer_Abort (Self_Id);
719
720       --  Need protection for updating links to per-task initialization and
721       --  finalization routines, in case some task is being created or
722       --  terminated concurrently.
723
724       POP.Lock_RTS;
725
726       --  Add this instantiation to the list of all instantiations
727
728       Local.Next := System.Tasking.Task_Attributes.All_Attributes;
729       System.Tasking.Task_Attributes.All_Attributes :=
730         Local'Unchecked_Access;
731
732       --  Try to find space for the attribute in the TCB
733
734       Local.Index := 0;
735       Two_To_J := 1;
736
737       if Attribute'Size <= System.Address'Size then
738          for J in Direct_Index_Range loop
739             if (Two_To_J and In_Use) = 0 then
740
741                --  Reserve location J for this attribute
742
743                In_Use := In_Use or Two_To_J;
744                Local.Index := J;
745
746                --  This unchecked conversions can give a warning when the the
747                --  alignment is incorrect, but it will not be used in such a
748                --  case anyway, so the warning can be safely ignored.
749
750                pragma Warnings (Off);
751                To_Attribute_Handle (Local.Initial_Value'Access).all :=
752                  Initial_Value;
753                pragma Warnings (On);
754
755                exit;
756             end if;
757
758             Two_To_J := Two_To_J * 2;
759          end loop;
760       end if;
761
762       --  Attribute goes directly in the TCB
763
764       if Local.Index /= 0 then
765          --  Replace stub for initialization routine that is called at task
766          --  creation.
767
768          Initialization.Initialize_Attributes_Link :=
769            System.Tasking.Task_Attributes.Initialize_Attributes'Access;
770
771          --  Initialize the attribute, for all tasks
772
773          declare
774             C : System.Tasking.Task_Id := System.Tasking.All_Tasks_List;
775          begin
776             while C /= null loop
777                C.Direct_Attributes (Local.Index) :=
778                  To_Direct_Attribute_Element
779                    (System.Storage_Elements.To_Address (Local.Initial_Value));
780                C := C.Common.All_Tasks_Link;
781             end loop;
782          end;
783
784       --  Attribute goes into a node onto a linked list
785
786       else
787          --  Replace stub for finalization routine called at task termination
788
789          Initialization.Finalize_Attributes_Link :=
790            System.Tasking.Task_Attributes.Finalize_Attributes'Access;
791       end if;
792
793       POP.Unlock_RTS;
794       Undefer_Abort (Self_Id);
795    end;
796 end Ada.Task_Attributes;