OSDN Git Service

2007-08-14 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-tataat.adb
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-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 with System.Task_Primitives.Operations;
36 --  used for Write_Lock
37 --           Unlock
38 --           Lock/Unlock_RTS
39
40 with System.Tasking.Initialization;
41 --  used for Defer_Abort
42 --           Undefer_Abort
43
44 with Ada.Unchecked_Conversion;
45
46 package body System.Tasking.Task_Attributes is
47
48    use Task_Primitives.Operations;
49    use Tasking.Initialization;
50
51    function To_Access_Address is new Ada.Unchecked_Conversion
52      (Access_Node, Access_Address);
53    --  Store pointer to indirect attribute list
54
55    --------------
56    -- Finalize --
57    --------------
58
59    procedure Finalize (X : in out Instance) is
60       Q, To_Be_Freed : Access_Node;
61       Self_Id        : constant Task_Id := Self;
62
63    begin
64       --  Defer abort. Note that we use the nestable versions of Defer_Abort
65       --  and Undefer_Abort, because abort can already deferred when this is
66       --  called during finalization, which would cause an assert failure
67       --  in Defer_Abort.
68
69       Defer_Abort_Nestable (Self_Id);
70       Lock_RTS;
71
72       --  Remove this instantiation from the list of all instantiations
73
74       declare
75          P : Access_Instance;
76          Q : Access_Instance := All_Attributes;
77
78       begin
79          while Q /= null and then Q /= X'Unchecked_Access loop
80             P := Q; Q := Q.Next;
81          end loop;
82
83          pragma Assert (Q /= null);
84
85          if P = null then
86             All_Attributes := Q.Next;
87          else
88             P.Next := Q.Next;
89          end if;
90       end;
91
92       if X.Index /= 0 then
93
94          --  Free location of this attribute, for reuse
95
96          In_Use := In_Use and not (2**Natural (X.Index));
97
98          --  There is no need for finalization in this case, since controlled
99          --  types are too big to fit in the TCB.
100
101       else
102          --  Remove nodes for this attribute from the lists of all tasks,
103          --  and deallocate the nodes. Deallocation does finalization, if
104          --  necessary.
105
106          declare
107             C : System.Tasking.Task_Id := All_Tasks_List;
108             P : Access_Node;
109
110          begin
111             while C /= null loop
112                Write_Lock (C);
113
114                Q := To_Access_Node (C.Indirect_Attributes);
115                while Q /= null
116                  and then Q.Instance /= X'Unchecked_Access
117                loop
118                   P := Q;
119                   Q := Q.Next;
120                end loop;
121
122                if Q /= null then
123                   if P = null then
124                      C.Indirect_Attributes := To_Access_Address (Q.Next);
125                   else
126                      P.Next := Q.Next;
127                   end if;
128
129                   --  Can't Deallocate now since we are holding RTS_Lock
130
131                   Q.Next := To_Be_Freed;
132                   To_Be_Freed := Q;
133                end if;
134
135                Unlock (C);
136                C := C.Common.All_Tasks_Link;
137             end loop;
138          end;
139       end if;
140
141       Unlock_RTS;
142
143       while To_Be_Freed /= null loop
144          Q := To_Be_Freed;
145          To_Be_Freed := To_Be_Freed.Next;
146          X.Deallocate.all (Q);
147       end loop;
148
149       Undefer_Abort_Nestable (Self_Id);
150
151    exception
152       when others =>
153          null;
154          pragma Assert (False,
155            "Exception in task attribute instance finalization");
156    end Finalize;
157
158    -------------------------
159    -- Finalize Attributes --
160    -------------------------
161
162    --  This is to be called just before the ATCB is deallocated.
163    --  It relies on the caller holding T.L write-lock on entry.
164
165    procedure Finalize_Attributes (T : Task_Id) is
166       P : Access_Node;
167       Q : Access_Node := To_Access_Node (T.Indirect_Attributes);
168
169    begin
170       --  Deallocate all the indirect attributes of this task
171
172       while Q /= null loop
173          P := Q;
174          Q := Q.Next; P.Instance.Deallocate.all (P);
175       end loop;
176
177       T.Indirect_Attributes := null;
178
179    exception
180       when others =>
181          null;
182          pragma Assert (False,
183            "Exception in per-task attributes finalization");
184    end Finalize_Attributes;
185
186    ---------------------------
187    -- Initialize Attributes --
188    ---------------------------
189
190    --  This is to be called by System.Tasking.Stages.Create_Task
191
192    procedure Initialize_Attributes (T : Task_Id) is
193       P       : Access_Instance;
194       Self_Id : constant Task_Id := Self;
195
196    begin
197       Defer_Abort (Self_Id);
198       Lock_RTS;
199
200       --  Initialize all the direct-access attributes of this task
201
202       P := All_Attributes;
203
204       while P /= null loop
205          if P.Index /= 0 then
206             T.Direct_Attributes (P.Index) :=
207               Direct_Attribute_Element
208                 (System.Storage_Elements.To_Address (P.Initial_Value));
209          end if;
210
211          P := P.Next;
212       end loop;
213
214       Unlock_RTS;
215       Undefer_Abort (Self_Id);
216
217    exception
218       when others =>
219          null;
220          pragma Assert (False);
221    end Initialize_Attributes;
222
223 end System.Tasking.Task_Attributes;