2003-05-31 Bud Davis <bdavis9659@comcast.net>
[official-gcc.git] / gcc / ada / a-tasatt.adb
blob6d9d114d7f46c8dcff982e3d9a9edf185fee612a
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-2002 Florida State University --
10 -- --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNARL was developed by the GNARL team at Florida State University. It is --
30 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
31 -- --
32 ------------------------------------------------------------------------------
34 -- The following notes are provided in case someone decides the
35 -- implementation of this package is too complicated, or too slow.
36 -- Please read this before making any "simplifications".
38 -- Correct implementation of this package is more difficult than one
39 -- might expect. After considering (and coding) several alternatives,
40 -- we settled on the present compromise. Things we do not like about
41 -- this implementation include:
43 -- - It is vulnerable to bad Task_ID values, to the extent of
44 -- possibly trashing memory and crashing the runtime system.
46 -- - It requires dynamic storage allocation for each new attribute value,
47 -- except for types that happen to be the same size as System.Address,
48 -- or shorter.
50 -- - Instantiations at other than the library level rely on being able to
51 -- do down-level calls to a procedure declared in the generic package body.
52 -- This makes it potentially vulnerable to compiler changes.
54 -- The main implementation issue here is that the connection from
55 -- task to attribute is a potential source of dangling references.
57 -- When a task goes away, we want to be able to recover all the storage
58 -- associated with its attributes. The Ada mechanism for this is
59 -- finalization, via controlled attribute types. For this reason,
60 -- the ARM requires finalization of attribute values when the
61 -- associated task terminates.
63 -- This finalization must be triggered by the tasking runtime system,
64 -- during termination of the task. Given the active set of instantiations
65 -- of 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.
71 -- One way this might be done is to create a special finalization chain
72 -- for each task, similar to the finalization chain that is used for
73 -- controlled objects within the task. This would differ from the usual
74 -- finalization chain in that it would not have a LIFO structure, since
75 -- attributes may be added to a task at any time during its lifetime.
76 -- This might be the right way to go for the longer term, but at present
77 -- this approach is not open, since GNAT does not provide such special
78 -- finalization support.
80 -- Lacking special compiler support, the RTS is limited to the
81 -- normal ways an application invokes finalization, i.e.
83 -- a) Explicit call to the procedure Finalize, if we know the type
84 -- has this operation defined on it. This is not sufficient, since
85 -- we have no way of determining whether a given generic formal
86 -- Attribute type is controlled, and no visibility of the associated
87 -- Finalize procedure, in the generic body.
89 -- b) Leaving the scope of a local object of a controlled type.
90 -- This does not help, since the lifetime of an instantiation of
91 -- Ada.Task_Attributes does not correspond to the lifetimes of the
92 -- various tasks which may have that attribute.
94 -- c) Assignment of another value to the object. This would not help,
95 -- since we then have to finalize the new value of the object.
97 -- d) Unchecked deallocation of an object of a controlled type.
98 -- This seems to be the only mechanism available to the runtime
99 -- system for finalization of task attributes.
101 -- We considered two ways of using unchecked deallocation, both based
102 -- on a linked list of that would hang from the task control block.
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
107 -- with access type T'Class, and uses this to deallocate and finalize all
108 -- the 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
111 -- is only allowed at the library level.
113 -- In the second approach the objects on the attribute list are of
114 -- unrelated but structurally similar types. Unchecked conversion is
115 -- used to circument Ada type checking. Each attribute-storage node
116 -- contains not only the attribute value and a link for chaining, but
117 -- also a pointer to a descriptor for the corresponding instantiation
118 -- of Task_Attributes. The instantiation-descriptor contains a
119 -- pointer to a procedure that can do the correct deallocation and
120 -- finalization for that type of attribute. On task termination, the
121 -- runtime system uses the pointer to call the appropriate deallocator.
123 -- While this gets around the limitation that instantiations be at
124 -- the library level, it relies on an implementation feature that
125 -- may not always be safe, i.e. that it is safe to call the
126 -- Deallocate procedure for an instantiation of Ada.Task_Attributes
127 -- that no longer exists. In general, it seems this might result in
128 -- dangling references.
130 -- Another problem with instantiations deeper than the library level
131 -- is that there is risk of storage leakage, or dangling references
132 -- to reused storage. That is, if an instantiation of Ada.Task_Attributes
133 -- is made within a procedure, what happens to the storage allocated for
134 -- attributes, when the procedure call returns? Apparently (RM 7.6.1 (4))
135 -- any such objects must be finalized, since they will no longer be
136 -- accessible, and in general one would expect that the storage they occupy
137 -- would be recovered for later reuse. (If not, we would have a case of
138 -- storage leakage.) Assuming the storage is recovered and later reused,
139 -- we have potentially dangerous dangling references. When the procedure
140 -- containing the instantiation of Ada.Task_Attributes returns, there
141 -- may still be unterminated tasks with associated attribute values for
142 -- that instantiation. When such tasks eventually terminate, the RTS
143 -- will attempt to call the Deallocate procedure on them. If the
144 -- corresponding storage has already been deallocated, when the master
145 -- of the access type was left, we have a potential disaster. This
146 -- disaster is compounded since the pointer to Deallocate is probably
147 -- through a "trampoline" which will also have been destroyed.
149 -- For this reason, we arrange to remove all dangling references
150 -- before leaving the scope of an instantiation. This is ugly, since
151 -- it requires traversing the list of all tasks, but it is no more ugly
152 -- than a similar traversal that we must do at the point of instantiation
153 -- in order to initialize the attributes of all tasks. At least we only
154 -- need to do these traversals if the type is controlled.
156 -- We chose to defer allocation of storage for attributes until the
157 -- Reference function is called or the attribute is first set to a value
158 -- different from the default initial one. This allows a potential
159 -- savings in allocation, for attributes that are not used by all tasks.
161 -- For efficiency, we reserve space in the TCB for a fixed number of
162 -- direct-access attributes. These are required to be of a size that
163 -- fits in the space of an object of type System.Address. Because
164 -- we must use unchecked bitwise copy operations on these values, they
165 -- cannot be of a controlled type, but that is covered automatically
166 -- since controlled objects are too large to fit in the spaces.
168 -- We originally deferred the initialization of these direct-access
169 -- attributes, just as we do for the indirect-access attributes, and
170 -- used a per-task bit vector to keep track of which attributes were
171 -- currently defined for that task. We found that the overhead of
172 -- maintaining this bit-vector seriously slowed down access to the
173 -- attributes, and made the fetch operation non-atomic, so that even
174 -- to read an attribute value required locking the TCB. Therefore,
175 -- we now initialize such attributes for all existing tasks at the time
176 -- of the attribute instantiation, and initialize existing attributes
177 -- for each new task at the time it is created.
179 -- The latter initialization requires a list of all the instantiation
180 -- descriptors. Updates to this list, as well as the bit-vector that
181 -- is used to reserve slots for attributes in the TCB, require mutual
182 -- exclusion. That is provided by the Lock/Unlock_RTS.
184 -- One special problem that added complexity to the design is that
185 -- the per-task list of indirect attributes contains objects of
186 -- different types. We use unchecked pointer conversion to link
187 -- these nodes together and access them, but the records may not have
188 -- identical internal structure. Initially, we thought it would be
189 -- enough to allocate all the common components of the records at the
190 -- front of each record, so that their positions would correspond.
191 -- Unfortunately, GNAT adds "dope" information at the front of a record,
192 -- if the record contains any controlled-type components.
194 -- This means that the offset of the fields we use to link the nodes is
195 -- at different positions on nodes of different types. To get around this,
196 -- each attribute storage record consists of a core node and wrapper.
197 -- The core nodes are all of the same type, and it is these that are
198 -- linked together and generally "seen" by the RTS. Each core node
199 -- contains a pointer to its own wrapper, which is a record that contains
200 -- the core node along with an attribute value, approximately
201 -- as follows:
203 -- type Node;
204 -- type Node_Access is access all Node;
205 -- type Node_Access;
206 -- type Access_Wrapper is access all Wrapper;
207 -- type Node is record
208 -- Next : Node_Access;
209 -- ...
210 -- Wrapper : Access_Wrapper;
211 -- end record;
212 -- type Wrapper is record
213 -- Noed : aliased Node;
214 -- Value : aliased Attribute; -- the generic formal type
215 -- end record;
217 -- Another interesting problem is with the initialization of
218 -- the instantiation descriptors. Originally, we did this all via
219 -- the Initialize procedure of the descriptor type and code in the
220 -- package body. It turned out that the Initialize procedure needed
221 -- quite a bit of information, including the size of the attribute
222 -- type, the initial value of the attribute (if it fits in the TCB),
223 -- and a pointer to the deallocator procedure. These needed to be
224 -- "passed" in via access discriminants. GNAT was having trouble
225 -- with access discriminants, so all this work was moved to the
226 -- package body.
228 with Ada.Task_Identification;
229 -- used for Task_Id
230 -- Null_Task_ID
231 -- Current_Task
233 with System.Error_Reporting;
234 -- used for Shutdown;
236 with System.Storage_Elements;
237 -- used for Integer_Address
239 with System.Task_Primitives.Operations;
240 -- used for Write_Lock
241 -- Unlock
242 -- Lock/Unlock_RTS
244 with System.Tasking;
245 -- used for Access_Address
246 -- Task_ID
247 -- Direct_Index_Vector
248 -- Direct_Index
250 with System.Tasking.Initialization;
251 -- used for Defer_Abortion
252 -- Undefer_Abortion
253 -- Initialize_Attributes_Link
254 -- Finalize_Attributes_Link
256 with System.Tasking.Task_Attributes;
257 -- used for Access_Node
258 -- Access_Dummy_Wrapper
259 -- Deallocator
260 -- Instance
261 -- Node
262 -- Access_Instance
264 with Ada.Exceptions;
265 -- used for Raise_Exception
267 with Unchecked_Conversion;
268 with Unchecked_Deallocation;
270 pragma Elaborate_All (System.Tasking.Task_Attributes);
271 -- to ensure the initialization of object Local (below) will work
273 package body Ada.Task_Attributes is
275 use System.Error_Reporting,
276 System.Tasking.Initialization,
277 System.Tasking,
278 System.Tasking.Task_Attributes,
279 Ada.Exceptions;
281 use type System.Tasking.Access_Address;
283 package POP renames System.Task_Primitives.Operations;
285 ---------------------------
286 -- Unchecked Conversions --
287 ---------------------------
289 pragma Warnings (Off);
290 -- These unchecked conversions can give warnings when alignments
291 -- are incorrect, but they will not be used in such cases anyway,
292 -- so the warnings can be safely ignored.
294 -- The following type corresponds to Dummy_Wrapper,
295 -- declared in System.Tasking.Task_Attributes.
297 type Wrapper;
298 type Access_Wrapper is access all Wrapper;
300 pragma Warnings (Off);
301 -- We turn warnings off for the following declarations of the
302 -- To_Attribute_Handle conversions, since these are used only
303 -- for small attributes where we know that there are no problems
304 -- with alignment, but the compiler will generate warnings for
305 -- the occurrences in the large attribute case, even though
306 -- they will not actually be used.
308 function To_Attribute_Handle is new Unchecked_Conversion
309 (Access_Address, Attribute_Handle);
310 -- For reference to directly addressed task attributes
312 type Access_Integer_Address is access all
313 System.Storage_Elements.Integer_Address;
315 function To_Attribute_Handle is new Unchecked_Conversion
316 (Access_Integer_Address, Attribute_Handle);
317 -- For reference to directly addressed task attributes
319 pragma Warnings (On);
320 -- End of warnings off region for directly addressed
321 -- attribute conversion functions.
323 function To_Access_Address is new Unchecked_Conversion
324 (Access_Node, Access_Address);
325 -- To store pointer to list of indirect attributes
327 function To_Access_Node is new Unchecked_Conversion
328 (Access_Address, Access_Node);
329 -- To fetch pointer to list of indirect attributes
331 pragma Warnings (Off);
332 function To_Access_Wrapper is new Unchecked_Conversion
333 (Access_Dummy_Wrapper, Access_Wrapper);
334 pragma Warnings (On);
335 -- To fetch pointer to actual wrapper of attribute node. We turn off
336 -- warnings since this may generate an alignment warning. The warning
337 -- can be ignored since Dummy_Wrapper is only a non-generic standin
338 -- for the real wrapper type (we never actually allocate objects of
339 -- type Dummy_Wrapper).
341 function To_Access_Dummy_Wrapper is new Unchecked_Conversion
342 (Access_Wrapper, Access_Dummy_Wrapper);
343 -- To store pointer to actual wrapper of attribute node
345 function To_Task_ID is new Unchecked_Conversion
346 (Task_Identification.Task_Id, Task_ID);
347 -- To access TCB of identified task
349 Null_ID : constant Task_ID := To_Task_ID (Task_Identification.Null_Task_Id);
350 -- ??? need comments on use and purpose
352 type Local_Deallocator is
353 access procedure (P : in out Access_Node);
355 function To_Lib_Level_Deallocator is new Unchecked_Conversion
356 (Local_Deallocator, Deallocator);
357 -- To defeat accessibility check
359 pragma Warnings (On);
361 ------------------------
362 -- Storage Management --
363 ------------------------
365 procedure Deallocate (P : in out Access_Node);
366 -- Passed to the RTS via unchecked conversion of a pointer to
367 -- permit finalization and deallocation of attribute storage nodes
369 --------------------------
370 -- Instantiation Record --
371 --------------------------
373 Local : aliased Instance;
374 -- Initialized in package body
376 type Wrapper is record
377 Noed : aliased Node;
379 Value : aliased Attribute := Initial_Value;
380 -- The generic formal type, may be controlled
381 end record;
383 procedure Free is
384 new Unchecked_Deallocation (Wrapper, Access_Wrapper);
386 procedure Deallocate (P : in out Access_Node) is
387 T : Access_Wrapper := To_Access_Wrapper (P.Wrapper);
389 begin
390 Free (T);
392 exception
393 when others =>
394 pragma Assert (Shutdown ("Exception in Deallocate")); null;
395 end Deallocate;
397 ---------------
398 -- Reference --
399 ---------------
401 function Reference
402 (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
403 return Attribute_Handle
405 TT : Task_ID := To_Task_ID (T);
406 Error_Message : constant String := "Trying to get the reference of a";
408 begin
409 if TT = Null_ID then
410 Raise_Exception (Program_Error'Identity,
411 Error_Message & "null task");
412 end if;
414 if TT.Common.State = Terminated then
415 Raise_Exception (Tasking_Error'Identity,
416 Error_Message & "terminated task");
417 end if;
419 begin
420 Defer_Abortion;
421 POP.Lock_RTS;
423 -- Directly addressed case
425 if Local.Index /= 0 then
426 POP.Unlock_RTS;
427 Undefer_Abortion;
429 -- Return the attribute handle. Warnings off because this return
430 -- statement generates alignment warnings for large attributes
431 -- (but will never be executed in this case anyway).
433 pragma Warnings (Off);
434 return
435 To_Attribute_Handle (TT.Direct_Attributes (Local.Index)'Access);
436 pragma Warnings (On);
438 -- Not directly addressed
440 else
441 declare
442 P : Access_Node := To_Access_Node (TT.Indirect_Attributes);
443 W : Access_Wrapper;
445 begin
446 while P /= null loop
447 if P.Instance = Access_Instance'(Local'Unchecked_Access) then
448 POP.Unlock_RTS;
449 Undefer_Abortion;
450 return To_Access_Wrapper (P.Wrapper).Value'Access;
451 end if;
453 P := P.Next;
454 end loop;
456 -- Unlock the RTS here to follow the lock ordering rule
457 -- that prevent us from using new (i.e the Global_Lock) while
458 -- holding any other lock.
460 POP.Unlock_RTS;
461 W := new Wrapper'
462 ((null, Local'Unchecked_Access, null), Initial_Value);
463 POP.Lock_RTS;
465 P := W.Noed'Unchecked_Access;
466 P.Wrapper := To_Access_Dummy_Wrapper (W);
467 P.Next := To_Access_Node (TT.Indirect_Attributes);
468 TT.Indirect_Attributes := To_Access_Address (P);
469 POP.Unlock_RTS;
470 Undefer_Abortion;
471 return W.Value'Access;
472 end;
473 end if;
475 pragma Assert (Shutdown ("Should never get here in Reference"));
476 return null;
478 exception
479 when others =>
480 POP.Unlock_RTS;
481 Undefer_Abortion;
482 raise;
483 end;
485 exception
486 when Tasking_Error | Program_Error =>
487 raise;
489 when others =>
490 raise Program_Error;
491 end Reference;
493 ------------------
494 -- Reinitialize --
495 ------------------
497 procedure Reinitialize
498 (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
500 TT : Task_ID := To_Task_ID (T);
501 Error_Message : constant String := "Trying to Reinitialize a";
503 begin
504 if TT = Null_ID then
505 Raise_Exception (Program_Error'Identity,
506 Error_Message & "null task");
507 end if;
509 if TT.Common.State = Terminated then
510 Raise_Exception (Tasking_Error'Identity,
511 Error_Message & "terminated task");
512 end if;
514 if Local.Index = 0 then
515 declare
516 P, Q : Access_Node;
517 W : Access_Wrapper;
519 begin
520 Defer_Abortion;
521 POP.Lock_RTS;
522 Q := To_Access_Node (TT.Indirect_Attributes);
524 while Q /= null loop
525 if Q.Instance = Access_Instance'(Local'Unchecked_Access) then
526 if P = null then
527 TT.Indirect_Attributes := To_Access_Address (Q.Next);
528 else
529 P.Next := Q.Next;
530 end if;
532 W := To_Access_Wrapper (Q.Wrapper);
533 Free (W);
534 POP.Unlock_RTS;
535 Undefer_Abortion;
536 return;
537 end if;
539 P := Q;
540 Q := Q.Next;
541 end loop;
543 POP.Unlock_RTS;
544 Undefer_Abortion;
546 exception
547 when others =>
548 POP.Unlock_RTS;
549 Undefer_Abortion;
550 end;
552 else
553 Set_Value (Initial_Value, T);
554 end if;
556 exception
557 when Tasking_Error | Program_Error =>
558 raise;
560 when others =>
561 raise Program_Error;
562 end Reinitialize;
564 ---------------
565 -- Set_Value --
566 ---------------
568 procedure Set_Value
569 (Val : Attribute;
570 T : Task_Identification.Task_Id := Task_Identification.Current_Task)
572 TT : Task_ID := To_Task_ID (T);
573 Error_Message : constant String := "Trying to Set the Value of a";
575 begin
576 if TT = Null_ID then
577 Raise_Exception (Program_Error'Identity,
578 Error_Message & "null task");
579 end if;
581 if TT.Common.State = Terminated then
582 Raise_Exception (Tasking_Error'Identity,
583 Error_Message & "terminated task");
584 end if;
586 begin
587 Defer_Abortion;
588 POP.Lock_RTS;
590 -- Directly addressed case
592 if Local.Index /= 0 then
594 -- Set attribute handle, warnings off, because this code can
595 -- generate alignment warnings with large attributes (but of
596 -- course wil not be executed in this case, since we never
597 -- have direct addressing in such cases).
599 pragma Warnings (Off);
600 To_Attribute_Handle
601 (TT.Direct_Attributes (Local.Index)'Access).all := Val;
602 pragma Warnings (On);
603 POP.Unlock_RTS;
604 Undefer_Abortion;
605 return;
607 -- Not directly addressed
609 else
610 declare
611 P : Access_Node := To_Access_Node (TT.Indirect_Attributes);
612 W : Access_Wrapper;
614 begin
615 while P /= null loop
617 if P.Instance = Access_Instance'(Local'Unchecked_Access) then
618 To_Access_Wrapper (P.Wrapper).Value := Val;
619 POP.Unlock_RTS;
620 Undefer_Abortion;
621 return;
622 end if;
624 P := P.Next;
625 end loop;
627 -- Unlock RTS here to follow the lock ordering rule that
628 -- prevent us from using new (i.e the Global_Lock) while
629 -- holding any other lock.
631 POP.Unlock_RTS;
632 W := new Wrapper'
633 ((null, Local'Unchecked_Access, null), Val);
634 POP.Lock_RTS;
635 P := W.Noed'Unchecked_Access;
636 P.Wrapper := To_Access_Dummy_Wrapper (W);
637 P.Next := To_Access_Node (TT.Indirect_Attributes);
638 TT.Indirect_Attributes := To_Access_Address (P);
639 end;
640 end if;
642 POP.Unlock_RTS;
643 Undefer_Abortion;
645 exception
646 when others =>
647 POP.Unlock_RTS;
648 Undefer_Abortion;
649 raise;
650 end;
652 return;
654 exception
655 when Tasking_Error | Program_Error =>
656 raise;
658 when others =>
659 raise Program_Error;
661 end Set_Value;
663 -----------
664 -- Value --
665 -----------
667 function Value
668 (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
669 return Attribute
671 Result : Attribute;
672 TT : Task_ID := To_Task_ID (T);
673 Error_Message : constant String := "Trying to get the Value of a";
675 begin
676 if TT = Null_ID then
677 Raise_Exception
678 (Program_Error'Identity, Error_Message & "null task");
679 end if;
681 if TT.Common.State = Terminated then
682 Raise_Exception
683 (Program_Error'Identity, Error_Message & "terminated task");
684 end if;
686 begin
687 -- Directly addressed case
689 if Local.Index /= 0 then
691 -- Get value of attribute. Warnings off, because for large
692 -- attributes, this code can generate alignment warnings.
693 -- But of course large attributes are never directly addressed
694 -- so in fact we will never execute the code in this case.
696 pragma Warnings (Off);
697 Result :=
698 To_Attribute_Handle
699 (TT.Direct_Attributes (Local.Index)'Access).all;
700 pragma Warnings (On);
702 -- Not directly addressed
704 else
705 declare
706 P : Access_Node;
708 begin
709 Defer_Abortion;
710 POP.Lock_RTS;
711 P := To_Access_Node (TT.Indirect_Attributes);
713 while P /= null loop
714 if P.Instance = Access_Instance'(Local'Unchecked_Access) then
715 POP.Unlock_RTS;
716 Undefer_Abortion;
717 return To_Access_Wrapper (P.Wrapper).Value;
718 end if;
720 P := P.Next;
721 end loop;
723 Result := Initial_Value;
724 POP.Unlock_RTS;
725 Undefer_Abortion;
727 exception
728 when others =>
729 POP.Unlock_RTS;
730 Undefer_Abortion;
731 raise;
732 end;
733 end if;
735 return Result;
736 end;
738 exception
739 when Tasking_Error | Program_Error =>
740 raise;
742 when others =>
743 raise Program_Error;
744 end Value;
746 -- Start of elaboration code for package Ada.Task_Attributes
748 begin
749 -- This unchecked conversion can give warnings when alignments
750 -- are incorrect, but they will not be used in such cases anyway,
751 -- so the warnings can be safely ignored.
753 pragma Warnings (Off);
754 Local.Deallocate := To_Lib_Level_Deallocator (Deallocate'Access);
755 pragma Warnings (On);
757 declare
758 Two_To_J : Direct_Index_Vector;
759 begin
760 Defer_Abortion;
762 -- Need protection for updating links to per-task initialization and
763 -- finalization routines, in case some task is being created or
764 -- terminated concurrently.
766 POP.Lock_RTS;
768 -- Add this instantiation to the list of all instantiations.
770 Local.Next := System.Tasking.Task_Attributes.All_Attributes;
771 System.Tasking.Task_Attributes.All_Attributes :=
772 Local'Unchecked_Access;
774 -- Try to find space for the attribute in the TCB.
776 Local.Index := 0;
777 Two_To_J := 2 ** Direct_Index'First;
779 if Attribute'Size <= System.Address'Size then
780 for J in Direct_Index loop
781 if (Two_To_J and In_Use) /= 0 then
783 -- Reserve location J for this attribute
785 In_Use := In_Use or Two_To_J;
786 Local.Index := J;
788 -- This unchecked conversions can give a warning when the
789 -- the alignment is incorrect, but it will not be used in
790 -- such a case anyway, so the warning can be safely ignored.
792 pragma Warnings (Off);
793 To_Attribute_Handle (Local.Initial_Value'Access).all :=
794 Initial_Value;
795 pragma Warnings (On);
797 exit;
798 end if;
800 Two_To_J := Two_To_J * 2;
801 end loop;
802 end if;
804 -- Attribute goes directly in the TCB
806 if Local.Index /= 0 then
808 -- Replace stub for initialization routine
809 -- that is called at task creation.
811 Initialization.Initialize_Attributes_Link :=
812 System.Tasking.Task_Attributes.Initialize_Attributes'Access;
814 -- Initialize the attribute, for all tasks.
816 declare
817 C : System.Tasking.Task_ID := System.Tasking.All_Tasks_List;
819 begin
820 while C /= null loop
821 POP.Write_Lock (C);
822 C.Direct_Attributes (Local.Index) :=
823 System.Storage_Elements.To_Address (Local.Initial_Value);
824 POP.Unlock (C);
825 C := C.Common.All_Tasks_Link;
826 end loop;
827 end;
829 -- Attribute goes into a node onto a linked list
831 else
832 -- Replace stub for finalization routine
833 -- that is called at task termination.
835 Initialization.Finalize_Attributes_Link :=
836 System.Tasking.Task_Attributes.Finalize_Attributes'Access;
838 end if;
840 POP.Unlock_RTS;
841 Undefer_Abortion;
843 exception
844 when others => null;
845 pragma Assert (Shutdown ("Exception in task attribute initializer"));
847 -- If we later decide to allow exceptions to propagate, we need to
848 -- not only release locks and undefer abortion, we also need to undo
849 -- any initializations that succeeded up to this point, or we will
850 -- risk a dangling reference when the task terminates.
851 end;
852 end Ada.Task_Attributes;