OSDN Git Service

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