2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / s-tataat.adb
blob528de085c9022532702b7b29688342cd7ce856b5
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . T A S K _ A T T R I B U T E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2005, 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 ------------------------------------------------------------------------------
35 with System.Storage_Elements;
36 -- used for To_Address
38 with System.Task_Primitives.Operations;
39 -- used for Write_Lock
40 -- Unlock
41 -- Lock/Unlock_RTS
43 with System.Tasking.Initialization;
44 -- used for Defer_Abort
45 -- Undefer_Abort
47 with Unchecked_Conversion;
49 package body System.Tasking.Task_Attributes is
51 use Task_Primitives.Operations;
52 use Tasking.Initialization;
54 function To_Access_Address is new Unchecked_Conversion
55 (Access_Node, Access_Address);
56 -- Store pointer to indirect attribute list
58 --------------
59 -- Finalize --
60 --------------
62 procedure Finalize (X : in out Instance) is
63 Q, To_Be_Freed : Access_Node;
64 Self_Id : constant Task_Id := Self;
66 begin
67 Defer_Abort (Self_Id);
68 Lock_RTS;
70 -- Remove this instantiation from the list of all instantiations.
72 declare
73 P : Access_Instance;
74 Q : Access_Instance := All_Attributes;
76 begin
77 while Q /= null and then Q /= X'Unchecked_Access loop
78 P := Q; Q := Q.Next;
79 end loop;
81 pragma Assert (Q /= null);
83 if P = null then
84 All_Attributes := Q.Next;
85 else
86 P.Next := Q.Next;
87 end if;
88 end;
90 if X.Index /= 0 then
91 -- Free location of this attribute, for reuse.
93 In_Use := In_Use and not (2**Natural (X.Index));
95 -- There is no need for finalization in this case, since controlled
96 -- types are too big to fit in the TCB.
98 else
99 -- Remove nodes for this attribute from the lists of all tasks,
100 -- and deallocate the nodes. Deallocation does finalization, if
101 -- necessary.
103 declare
104 C : System.Tasking.Task_Id := All_Tasks_List;
105 P : Access_Node;
107 begin
108 while C /= null loop
109 Write_Lock (C);
111 Q := To_Access_Node (C.Indirect_Attributes);
112 while Q /= null
113 and then Q.Instance /= X'Unchecked_Access
114 loop
115 P := Q;
116 Q := Q.Next;
117 end loop;
119 if Q /= null then
120 if P = null then
121 C.Indirect_Attributes := To_Access_Address (Q.Next);
122 else
123 P.Next := Q.Next;
124 end if;
126 -- Can't Deallocate now since we are holding RTS_Lock
128 Q.Next := To_Be_Freed;
129 To_Be_Freed := Q;
130 end if;
132 Unlock (C);
133 C := C.Common.All_Tasks_Link;
134 end loop;
135 end;
136 end if;
138 Unlock_RTS;
140 while To_Be_Freed /= null loop
141 Q := To_Be_Freed;
142 To_Be_Freed := To_Be_Freed.Next;
143 X.Deallocate.all (Q);
144 end loop;
146 Undefer_Abort (Self_Id);
148 exception
149 when others =>
150 null;
151 pragma Assert (False,
152 "Exception in task attribute instance finalization");
153 end Finalize;
155 -------------------------
156 -- Finalize Attributes --
157 -------------------------
159 -- This is to be called just before the ATCB is deallocated.
160 -- It relies on the caller holding T.L write-lock on entry.
162 procedure Finalize_Attributes (T : Task_Id) is
163 P : Access_Node;
164 Q : Access_Node := To_Access_Node (T.Indirect_Attributes);
166 begin
167 -- Deallocate all the indirect attributes of this task
169 while Q /= null loop
170 P := Q;
171 Q := Q.Next; P.Instance.Deallocate.all (P);
172 end loop;
174 T.Indirect_Attributes := null;
176 exception
177 when others =>
178 null;
179 pragma Assert (False,
180 "Exception in per-task attributes finalization");
181 end Finalize_Attributes;
183 ---------------------------
184 -- Initialize Attributes --
185 ---------------------------
187 -- This is to be called by System.Tasking.Stages.Create_Task
189 procedure Initialize_Attributes (T : Task_Id) is
190 P : Access_Instance;
191 Self_Id : constant Task_Id := Self;
193 begin
194 Defer_Abort (Self_Id);
195 Lock_RTS;
197 -- Initialize all the direct-access attributes of this task
199 P := All_Attributes;
201 while P /= null loop
202 if P.Index /= 0 then
203 T.Direct_Attributes (P.Index) :=
204 Direct_Attribute_Element
205 (System.Storage_Elements.To_Address (P.Initial_Value));
206 end if;
208 P := P.Next;
209 end loop;
211 Unlock_RTS;
212 Undefer_Abort (Self_Id);
214 exception
215 when others =>
216 null;
217 pragma Assert (False);
218 end Initialize_Attributes;
220 end System.Tasking.Task_Attributes;