1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- A D A . T A S K _ A T T R I B U T E S --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2004, Ada Core Technologies --
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. --
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. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
33 ------------------------------------------------------------------------------
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".
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:
44 -- - It is vulnerable to bad Task_Id values, to the extent of
45 -- possibly trashing memory and crashing the runtime system.
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,
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.
55 -- The main implementation issue here is that the connection from
56 -- task to attribute is a potential source of dangling references.
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.
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.
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.
81 -- Lacking special compiler support, the RTS is limited to the
82 -- normal ways an application invokes finalization, i.e.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
205 -- type Node_Access is access all Node;
207 -- type Access_Wrapper is access all Wrapper;
208 -- type Node is record
209 -- Next : Node_Access;
211 -- Wrapper : Access_Wrapper;
213 -- type Wrapper is record
214 -- Noed : aliased Node;
215 -- Value : aliased Attribute; -- the generic formal type
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
229 with Ada
.Task_Identification
;
234 with System
.Error_Reporting
;
235 -- used for Shutdown;
237 with System
.Storage_Elements
;
238 -- used for Integer_Address
240 with System
.Task_Primitives
.Operations
;
241 -- used for Write_Lock
246 -- used for Access_Address
248 -- Direct_Index_Vector
251 with System
.Tasking
.Initialization
;
252 -- used for Defer_Abortion
254 -- Initialize_Attributes_Link
255 -- Finalize_Attributes_Link
257 with System
.Tasking
.Task_Attributes
;
258 -- used for Access_Node
259 -- Access_Dummy_Wrapper
266 -- used for Raise_Exception
268 with Unchecked_Conversion
;
269 with Unchecked_Deallocation
;
271 pragma Elaborate_All
(System
.Tasking
.Task_Attributes
);
272 -- to ensure the initialization of object Local (below) will work
274 package body Ada
.Task_Attributes
is
276 use System
.Error_Reporting
,
277 System
.Tasking
.Initialization
,
279 System
.Tasking
.Task_Attributes
,
282 use type System
.Tasking
.Access_Address
;
284 package POP
renames System
.Task_Primitives
.Operations
;
286 ---------------------------
287 -- Unchecked Conversions --
288 ---------------------------
290 -- The following type corresponds to Dummy_Wrapper,
291 -- declared in System.Tasking.Task_Attributes.
294 type Access_Wrapper
is access all Wrapper
;
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.
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
310 type Access_Integer_Address
is access all
311 System
.Storage_Elements
.Integer_Address
;
313 function To_Attribute_Handle
is new Unchecked_Conversion
314 (Access_Integer_Address
, Attribute_Handle
);
315 -- For reference to directly addressed task attributes
317 pragma Warnings
(On
);
318 -- End of warnings off region for directly addressed
319 -- attribute conversion functions.
321 function To_Access_Address
is new Unchecked_Conversion
322 (Access_Node
, Access_Address
);
323 -- To store pointer to list of indirect attributes
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).
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
339 function To_Task_Id
is new Unchecked_Conversion
340 (Task_Identification
.Task_Id
, Task_Id
);
341 -- To access TCB of identified task
343 type Local_Deallocator
is access procedure (P
: in out Access_Node
);
345 function To_Lib_Level_Deallocator
is new Unchecked_Conversion
346 (Local_Deallocator
, Deallocator
);
347 -- To defeat accessibility check
349 pragma Warnings
(On
);
351 ------------------------
352 -- Storage Management --
353 ------------------------
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
359 --------------------------
360 -- Instantiation Record --
361 --------------------------
363 Local
: aliased Instance
;
364 -- Initialized in package body
366 type Wrapper
is record
369 Value
: aliased Attribute
:= Initial_Value
;
370 -- The generic formal type, may be controlled
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.
377 for Wrapper
'Alignment use Standard
'Maximum_Alignment;
380 new Unchecked_Deallocation
(Wrapper
, Access_Wrapper
);
382 procedure Deallocate
(P
: in out Access_Node
) is
383 T
: Access_Wrapper
:= To_Access_Wrapper
(P
.Wrapper
);
394 (T
: Task_Identification
.Task_Id
:= Task_Identification
.Current_Task
)
395 return Attribute_Handle
397 TT
: constant Task_Id
:= To_Task_Id
(T
);
398 Error_Message
: constant String := "Trying to get the reference of a ";
402 Raise_Exception
(Program_Error
'Identity, Error_Message
& "null task");
405 if TT
.Common
.State
= Terminated
then
406 Raise_Exception
(Tasking_Error
'Identity,
407 Error_Message
& "terminated task");
410 -- Directly addressed case
412 if Local
.Index
/= 0 then
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).
418 pragma Warnings
(Off
);
420 To_Attribute_Handle
(TT
.Direct_Attributes
(Local
.Index
)'Address);
421 pragma Warnings
(On
);
423 -- Not directly addressed
427 P
: Access_Node
:= To_Access_Node
(TT
.Indirect_Attributes
);
435 if P
.Instance
= Access_Instance
'(Local'Unchecked_Access) then
438 return To_Access_Wrapper (P.Wrapper).Value'Access;
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.
450 ((null, Local
'Unchecked_Access, null), Initial_Value
);
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
);
459 return W
.Value
'Access;
469 pragma Assert
(Shutdown
("Should never get here in Reference"));
473 when Tasking_Error | Program_Error
=>
484 procedure Reinitialize
485 (T
: Task_Identification
.Task_Id
:= Task_Identification
.Current_Task
)
487 TT
: constant Task_Id
:= To_Task_Id
(T
);
488 Error_Message
: constant String := "Trying to Reinitialize a ";
492 Raise_Exception
(Program_Error
'Identity, Error_Message
& "null task");
495 if TT
.Common
.State
= Terminated
then
496 Raise_Exception
(Tasking_Error
'Identity,
497 Error_Message
& "terminated task");
500 if Local
.Index
/= 0 then
501 Set_Value
(Initial_Value
, T
);
509 Q
:= To_Access_Node
(TT
.Indirect_Attributes
);
512 if Q
.Instance
= Access_Instance
'(Local'Unchecked_Access) then
514 TT.Indirect_Attributes := To_Access_Address (Q.Next);
519 W := To_Access_Wrapper (Q.Wrapper);
542 when Tasking_Error | Program_Error =>
555 T : Task_Identification.Task_Id := Task_Identification.Current_Task)
557 TT : constant Task_Id := To_Task_Id (T);
558 Error_Message : constant String := "Trying to Set the Value of a ";
562 Raise_Exception (Program_Error'Identity, Error_Message & "null task");
565 if TT.Common.State = Terminated then
566 Raise_Exception (Tasking_Error'Identity,
567 Error_Message & "terminated task");
570 -- Directly addressed case
572 if Local.Index /= 0 then
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
579 pragma Warnings (Off);
581 (TT.Direct_Attributes (Local.Index)'Address).all := Val;
582 pragma Warnings (On);
586 -- Not directly addressed
589 P : Access_Node := To_Access_Node (TT.Indirect_Attributes);
598 if P.Instance = Access_Instance'(Local
'Unchecked_Access) then
599 To_Access_Wrapper
(P
.Wrapper
).Value
:= Val
;
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.
613 W
:= new Wrapper
'((null, Local'Unchecked_Access, null), Val);
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);
631 when Tasking_Error | Program_Error =>
643 (T : Task_Identification.Task_Id := Task_Identification.Current_Task)
646 TT : constant Task_Id := To_Task_Id (T);
647 Error_Message : constant String := "Trying to get the Value of a ";
651 Raise_Exception (Program_Error'Identity, Error_Message & "null task");
654 if TT.Common.State = Terminated then
656 (Program_Error'Identity, Error_Message & "terminated task");
659 -- Directly addressed case
661 if Local.Index /= 0 then
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.
668 pragma Warnings (Off);
669 return To_Attribute_Handle
670 (TT.Direct_Attributes (Local.Index)'Address).all;
671 pragma Warnings (On);
674 -- Not directly addressed
683 P := To_Access_Node (TT.Indirect_Attributes);
686 if P.Instance = Access_Instance'(Local
'Unchecked_Access) then
687 Result
:= To_Access_Wrapper
(P
.Wrapper
).Value
;
698 return Initial_Value
;
708 when Tasking_Error | Program_Error
=>
715 -- Start of elaboration code for package Ada.Task_Attributes
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.
722 pragma Warnings
(Off
);
723 Local
.Deallocate
:= To_Lib_Level_Deallocator
(Deallocate
'Access);
724 pragma Warnings
(On
);
727 Two_To_J
: Direct_Index_Vector
;
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.
737 -- Add this instantiation to the list of all instantiations.
739 Local
.Next
:= System
.Tasking
.Task_Attributes
.All_Attributes
;
740 System
.Tasking
.Task_Attributes
.All_Attributes
:=
741 Local
'Unchecked_Access;
743 -- Try to find space for the attribute in the TCB.
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
752 -- Reserve location J for this attribute
754 In_Use
:= In_Use
or Two_To_J
;
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.
761 pragma Warnings
(Off
);
762 To_Attribute_Handle
(Local
.Initial_Value
'Access).all :=
764 pragma Warnings
(On
);
769 Two_To_J
:= Two_To_J
* 2;
773 -- Attribute goes directly in the TCB
775 if Local
.Index
/= 0 then
776 -- Replace stub for initialization routine
777 -- that is called at task creation.
779 Initialization
.Initialize_Attributes_Link
:=
780 System
.Tasking
.Task_Attributes
.Initialize_Attributes
'Access;
782 -- Initialize the attribute, for all tasks.
785 C
: System
.Tasking
.Task_Id
:= System
.Tasking
.All_Tasks_List
;
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
;
795 -- Attribute goes into a node onto a linked list
798 -- Replace stub for finalization routine
799 -- that is called at task termination.
801 Initialization
.Finalize_Attributes_Link
:=
802 System
.Tasking
.Task_Attributes
.Finalize_Attributes
'Access;
808 end Ada
.Task_Attributes
;