2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / ada / s-tasuti.adb
blobee68ebcec7ba6c94fc4dd38f74a1165d0ac6c4a2
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . U T I L I T I E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This package provides RTS Internal Declarations.
35 -- These declarations are not part of the GNARLI
37 pragma Polling (Off);
38 -- Turn off polling, we do not want ATC polling to take place during
39 -- tasking operations. It causes infinite loops and other problems.
41 with System.Tasking.Debug;
42 -- used for Known_Tasks
44 with System.Task_Primitives.Operations;
45 -- used for Write_Lock
46 -- Set_Priority
47 -- Wakeup
48 -- Unlock
49 -- Sleep
50 -- Abort_Task
51 -- Lock/Unlock_RTS
53 with System.Tasking.Initialization;
54 -- Used for Defer_Abort
55 -- Undefer_Abort
56 -- Locked_Abort_To_Level
58 with System.Tasking.Queuing;
59 -- used for Dequeue_Call
60 -- Dequeue_Head
62 with System.Tasking.Debug;
63 -- used for Trace
65 with System.Parameters;
66 -- used for Single_Lock
67 -- Runtime_Traces
69 with System.Traces.Tasking;
70 -- used for Send_Trace_Info
72 with Unchecked_Conversion;
74 package body System.Tasking.Utilities is
76 package STPO renames System.Task_Primitives.Operations;
78 use Parameters;
79 use Tasking.Debug;
80 use Task_Primitives;
81 use Task_Primitives.Operations;
83 use System.Traces;
84 use System.Traces.Tasking;
86 --------------------
87 -- Abort_One_Task --
88 --------------------
90 -- Similar to Locked_Abort_To_Level (Self_ID, T, 0), but:
91 -- (1) caller should be holding no locks except RTS_Lock when Single_Lock
92 -- (2) may be called for tasks that have not yet been activated
93 -- (3) always aborts whole task
95 procedure Abort_One_Task (Self_ID : Task_Id; T : Task_Id) is
96 begin
97 if Parameters.Runtime_Traces then
98 Send_Trace_Info (T_Abort, Self_ID, T);
99 end if;
101 Write_Lock (T);
103 if T.Common.State = Unactivated then
104 T.Common.Activator := null;
105 T.Common.State := Terminated;
106 T.Callable := False;
107 Cancel_Queued_Entry_Calls (T);
109 elsif T.Common.State /= Terminated then
110 Initialization.Locked_Abort_To_Level (Self_ID, T, 0);
111 end if;
113 Unlock (T);
114 end Abort_One_Task;
116 -----------------
117 -- Abort_Tasks --
118 -----------------
120 -- This must be called to implement the abort statement.
121 -- Much of the actual work of the abort is done by the abortee,
122 -- via the Abort_Handler signal handler, and propagation of the
123 -- Abort_Signal special exception.
125 procedure Abort_Tasks (Tasks : Task_List) is
126 Self_Id : constant Task_Id := STPO.Self;
127 C : Task_Id;
128 P : Task_Id;
130 begin
131 -- If pragma Detect_Blocking is active then Program_Error must be
132 -- raised if this potentially blocking operation is called from a
133 -- protected action.
135 if System.Tasking.Detect_Blocking
136 and then Self_Id.Common.Protected_Action_Nesting > 0
137 then
138 Ada.Exceptions.Raise_Exception
139 (Program_Error'Identity, "potentially blocking operation");
140 end if;
142 Initialization.Defer_Abort_Nestable (Self_Id);
144 -- ?????
145 -- Really should not be nested deferral here.
146 -- Patch for code generation error that defers abort before
147 -- evaluating parameters of an entry call (at least, timed entry
148 -- calls), and so may propagate an exception that causes abort
149 -- to remain undeferred indefinitely. See C97404B. When all
150 -- such bugs are fixed, this patch can be removed.
152 Lock_RTS;
154 for J in Tasks'Range loop
155 C := Tasks (J);
156 Abort_One_Task (Self_Id, C);
157 end loop;
159 C := All_Tasks_List;
161 while C /= null loop
162 if C.Pending_ATC_Level > 0 then
163 P := C.Common.Parent;
165 while P /= null loop
166 if P.Pending_ATC_Level = 0 then
167 Abort_One_Task (Self_Id, C);
168 exit;
169 end if;
171 P := P.Common.Parent;
172 end loop;
173 end if;
175 C := C.Common.All_Tasks_Link;
176 end loop;
178 Unlock_RTS;
179 Initialization.Undefer_Abort_Nestable (Self_Id);
180 end Abort_Tasks;
182 -------------------------------
183 -- Cancel_Queued_Entry_Calls --
184 -------------------------------
186 -- This should only be called by T, unless T is a terminated previously
187 -- unactivated task.
189 procedure Cancel_Queued_Entry_Calls (T : Task_Id) is
190 Next_Entry_Call : Entry_Call_Link;
191 Entry_Call : Entry_Call_Link;
192 Self_Id : constant Task_Id := STPO.Self;
194 Caller : Task_Id;
195 pragma Unreferenced (Caller);
196 -- Should this be removed ???
198 Level : Integer;
199 pragma Unreferenced (Level);
200 -- Should this be removed ???
202 begin
203 pragma Assert (T = Self or else T.Common.State = Terminated);
205 for J in 1 .. T.Entry_Num loop
206 Queuing.Dequeue_Head (T.Entry_Queues (J), Entry_Call);
208 while Entry_Call /= null loop
210 -- Leave Entry_Call.Done = False, since this is cancelled
212 Caller := Entry_Call.Self;
213 Entry_Call.Exception_To_Raise := Tasking_Error'Identity;
214 Queuing.Dequeue_Head (T.Entry_Queues (J), Next_Entry_Call);
215 Level := Entry_Call.Level - 1;
216 Unlock (T);
217 Write_Lock (Entry_Call.Self);
218 Initialization.Wakeup_Entry_Caller
219 (Self_Id, Entry_Call, Cancelled);
220 Unlock (Entry_Call.Self);
221 Write_Lock (T);
222 Entry_Call.State := Done;
223 Entry_Call := Next_Entry_Call;
224 end loop;
225 end loop;
226 end Cancel_Queued_Entry_Calls;
228 ------------------------
229 -- Exit_One_ATC_Level --
230 ------------------------
232 -- Call only with abort deferred and holding lock of Self_Id.
233 -- This is a bit of common code for all entry calls.
234 -- The effect is to exit one level of ATC nesting.
236 -- If we have reached the desired ATC nesting level, reset the
237 -- requested level to effective infinity, to allow further calls.
238 -- In any case, reset Self_Id.Aborting, to allow re-raising of
239 -- Abort_Signal.
241 procedure Exit_One_ATC_Level (Self_ID : Task_Id) is
242 begin
243 Self_ID.ATC_Nesting_Level := Self_ID.ATC_Nesting_Level - 1;
245 pragma Debug
246 (Debug.Trace (Self_ID, "EOAL: exited to ATC level: " &
247 ATC_Level'Image (Self_ID.ATC_Nesting_Level), 'A'));
249 pragma Assert (Self_ID.ATC_Nesting_Level >= 1);
251 if Self_ID.Pending_ATC_Level < ATC_Level_Infinity then
252 if Self_ID.Pending_ATC_Level = Self_ID.ATC_Nesting_Level then
253 Self_ID.Pending_ATC_Level := ATC_Level_Infinity;
254 Self_ID.Aborting := False;
255 else
256 -- Force the next Undefer_Abort to re-raise Abort_Signal
258 pragma Assert
259 (Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level);
261 if Self_ID.Aborting then
262 Self_ID.ATC_Hack := True;
263 Self_ID.Pending_Action := True;
264 end if;
265 end if;
266 end if;
267 end Exit_One_ATC_Level;
269 ----------------------
270 -- Make_Independent --
271 ----------------------
273 procedure Make_Independent is
274 Self_Id : constant Task_Id := STPO.Self;
275 Environment_Task : constant Task_Id := STPO.Environment_Task;
276 Parent : constant Task_Id := Self_Id.Common.Parent;
277 Parent_Needs_Updating : Boolean := False;
278 Master_of_Task : Integer;
280 begin
281 if Self_Id.Known_Tasks_Index /= -1 then
282 Known_Tasks (Self_Id.Known_Tasks_Index) := null;
283 end if;
285 Initialization.Defer_Abort (Self_Id);
287 if Single_Lock then
288 Lock_RTS;
289 end if;
291 Write_Lock (Environment_Task);
292 Write_Lock (Self_Id);
294 pragma Assert (Parent = Environment_Task
295 or else Self_Id.Master_of_Task = Library_Task_Level);
297 Master_of_Task := Self_Id.Master_of_Task;
298 Self_Id.Master_of_Task := Independent_Task_Level;
300 -- The run time assumes that the parent of an independent task is the
301 -- environment task.
303 if Parent /= Environment_Task then
305 -- We cannot lock three tasks at the same time, so defer the
306 -- operations on the parent.
308 Parent_Needs_Updating := True;
309 Self_Id.Common.Parent := Environment_Task;
310 end if;
312 -- Update Independent_Task_Count that is needed for the GLADE
313 -- termination rule. See also pending update in
314 -- System.Tasking.Stages.Check_Independent
316 Independent_Task_Count := Independent_Task_Count + 1;
318 Unlock (Self_Id);
320 -- Changing the parent after creation is not trivial. Do not forget
321 -- to update the old parent counts, and the new parent (i.e. the
322 -- Environment_Task) counts.
324 if Parent_Needs_Updating then
325 Write_Lock (Parent);
326 Parent.Awake_Count := Parent.Awake_Count - 1;
327 Parent.Alive_Count := Parent.Alive_Count - 1;
328 Environment_Task.Awake_Count := Environment_Task.Awake_Count + 1;
329 Environment_Task.Alive_Count := Environment_Task.Alive_Count + 1;
330 Unlock (Parent);
331 end if;
333 -- In case the environment task is already waiting for children to
334 -- complete.
335 -- ??? There may be a race condition if the environment task was not in
336 -- master completion sleep when this task was created, but now is
338 if Environment_Task.Common.State = Master_Completion_Sleep and then
339 Master_of_Task = Environment_Task.Master_Within
340 then
341 Environment_Task.Common.Wait_Count :=
342 Environment_Task.Common.Wait_Count - 1;
343 end if;
345 Unlock (Environment_Task);
347 if Single_Lock then
348 Unlock_RTS;
349 end if;
351 Initialization.Undefer_Abort (Self_Id);
352 end Make_Independent;
354 ------------------
355 -- Make_Passive --
356 ------------------
358 procedure Make_Passive (Self_ID : Task_Id; Task_Completed : Boolean) is
359 C : Task_Id := Self_ID;
360 P : Task_Id := C.Common.Parent;
362 Master_Completion_Phase : Integer;
364 begin
365 if P /= null then
366 Write_Lock (P);
367 end if;
369 Write_Lock (C);
371 if Task_Completed then
372 Self_ID.Common.State := Terminated;
374 if Self_ID.Awake_Count = 0 then
376 -- We are completing via a terminate alternative.
377 -- Our parent should wait in Phase 2 of Complete_Master.
379 Master_Completion_Phase := 2;
381 pragma Assert (Task_Completed);
382 pragma Assert (Self_ID.Terminate_Alternative);
383 pragma Assert (Self_ID.Alive_Count = 1);
385 else
386 -- We are NOT on a terminate alternative.
387 -- Our parent should wait in Phase 1 of Complete_Master.
389 Master_Completion_Phase := 1;
390 pragma Assert (Self_ID.Awake_Count = 1);
391 end if;
393 -- We are accepting with a terminate alternative
395 else
396 if Self_ID.Open_Accepts = null then
398 -- Somebody started a rendezvous while we had our lock open.
399 -- Skip the terminate alternative.
401 Unlock (C);
403 if P /= null then
404 Unlock (P);
405 end if;
407 return;
408 end if;
410 Self_ID.Terminate_Alternative := True;
411 Master_Completion_Phase := 0;
413 pragma Assert (Self_ID.Terminate_Alternative);
414 pragma Assert (Self_ID.Awake_Count >= 1);
415 end if;
417 if Master_Completion_Phase = 2 then
419 -- Since our Awake_Count is zero but our Alive_Count
420 -- is nonzero, we have been accepting with a terminate
421 -- alternative, and we now have been told to terminate
422 -- by a completed master (in some ancestor task) that
423 -- is waiting (with zero Awake_Count) in Phase 2 of
424 -- Complete_Master.
426 pragma Debug (Debug.Trace (Self_ID, "Make_Passive: Phase 2", 'M'));
428 pragma Assert (P /= null);
430 C.Alive_Count := C.Alive_Count - 1;
432 if C.Alive_Count > 0 then
433 Unlock (C);
434 Unlock (P);
435 return;
436 end if;
438 -- C's count just went to zero, indicating that
439 -- all of C's dependents are terminated.
440 -- C has a parent, P.
442 loop
443 -- C's count just went to zero, indicating that all of C's
444 -- dependents are terminated. C has a parent, P. Notify P that
445 -- C and its dependents have all terminated.
447 P.Alive_Count := P.Alive_Count - 1;
448 exit when P.Alive_Count > 0;
449 Unlock (C);
450 Unlock (P);
451 C := P;
452 P := C.Common.Parent;
454 -- Environment task cannot have terminated yet
456 pragma Assert (P /= null);
458 Write_Lock (P);
459 Write_Lock (C);
460 end loop;
462 pragma Assert (P.Awake_Count /= 0);
464 if P.Common.State = Master_Phase_2_Sleep
465 and then C.Master_of_Task = P.Master_Within
466 then
467 pragma Assert (P.Common.Wait_Count > 0);
468 P.Common.Wait_Count := P.Common.Wait_Count - 1;
470 if P.Common.Wait_Count = 0 then
471 Wakeup (P, Master_Phase_2_Sleep);
472 end if;
473 end if;
475 Unlock (C);
476 Unlock (P);
477 return;
478 end if;
480 -- We are terminating in Phase 1 or Complete_Master,
481 -- or are accepting on a terminate alternative.
483 C.Awake_Count := C.Awake_Count - 1;
485 if Task_Completed then
486 pragma Assert (Self_ID.Awake_Count = 0);
487 C.Alive_Count := C.Alive_Count - 1;
488 end if;
490 if C.Awake_Count > 0 or else P = null then
491 Unlock (C);
493 if P /= null then
494 Unlock (P);
495 end if;
497 return;
498 end if;
500 -- C's count just went to zero, indicating that all of C's
501 -- dependents are terminated or accepting with terminate alt.
502 -- C has a parent, P.
504 loop
505 -- Notify P that C has gone passive
507 P.Awake_Count := P.Awake_Count - 1;
509 if Task_Completed and then C.Alive_Count = 0 then
510 P.Alive_Count := P.Alive_Count - 1;
511 end if;
513 exit when P.Awake_Count > 0;
514 Unlock (C);
515 Unlock (P);
516 C := P;
517 P := C.Common.Parent;
519 if P = null then
520 return;
521 end if;
523 Write_Lock (P);
524 Write_Lock (C);
525 end loop;
527 -- P has non-passive dependents
529 if P.Common.State = Master_Completion_Sleep
530 and then C.Master_of_Task = P.Master_Within
531 then
532 pragma Debug
533 (Debug.Trace
534 (Self_ID, "Make_Passive: Phase 1, parent waiting", 'M'));
536 -- If parent is in Master_Completion_Sleep, it
537 -- cannot be on a terminate alternative, hence
538 -- it cannot have Awake_Count of zero.
540 pragma Assert (P.Common.Wait_Count > 0);
541 P.Common.Wait_Count := P.Common.Wait_Count - 1;
543 if P.Common.Wait_Count = 0 then
544 Wakeup (P, Master_Completion_Sleep);
545 end if;
547 else
548 pragma Debug
549 (Debug.Trace
550 (Self_ID, "Make_Passive: Phase 1, parent awake", 'M'));
551 null;
552 end if;
554 Unlock (C);
555 Unlock (P);
556 end Make_Passive;
558 end System.Tasking.Utilities;