1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K I N G . U T I L I T I E S --
11 -- Copyright (C) 1991-2001, Florida State University --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This package provides RTS Internal Declarations.
38 -- These declarations are not part of the GNARLI
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 -- used for Known_Tasks
47 with System
.Task_Primitives
.Operations
;
48 -- used for Write_Lock
54 -- Lock/Unlock_All_Tasks_List
56 with System
.Tasking
.Initialization
;
57 -- Used for Defer_Abort
59 -- Locked_Abort_To_Level
61 with System
.Tasking
.Queuing
;
62 -- used for Dequeue_Call
65 with System
.Tasking
.Debug
;
68 with Unchecked_Conversion
;
70 package body System
.Tasking
.Utilities
is
72 package STPO
renames System
.Task_Primitives
.Operations
;
74 use System
.Tasking
.Debug
;
75 use System
.Task_Primitives
;
76 use System
.Task_Primitives
.Operations
;
78 procedure Locked_Abort_To_Level
83 Initialization
.Locked_Abort_To_Level
;
85 procedure Defer_Abort
(Self_Id
: Task_ID
) renames
86 System
.Tasking
.Initialization
.Defer_Abort
;
88 procedure Defer_Abort_Nestable
(Self_Id
: Task_ID
) renames
89 System
.Tasking
.Initialization
.Defer_Abort_Nestable
;
91 procedure Undefer_Abort
(Self_Id
: Task_ID
) renames
92 System
.Tasking
.Initialization
.Undefer_Abort
;
94 procedure Undefer_Abort_Nestable
(Self_Id
: Task_ID
) renames
95 System
.Tasking
.Initialization
.Undefer_Abort_Nestable
;
97 procedure Wakeup_Entry_Caller
99 Entry_Call
: Entry_Call_Link
;
100 New_State
: Entry_Call_State
)
102 Initialization
.Wakeup_Entry_Caller
;
108 -- Similar to Locked_Abort_To_Level (Self_ID, T, 0), but:
109 -- (1) caller should be holding no locks
110 -- (2) may be called for tasks that have not yet been activated
111 -- (3) always aborts whole task
113 procedure Abort_One_Task
120 if T
.Common
.State
= Unactivated
then
121 T
.Common
.Activator
:= null;
122 T
.Common
.State
:= Terminated
;
124 Cancel_Queued_Entry_Calls
(T
);
126 elsif T
.Common
.State
/= Terminated
then
127 Locked_Abort_To_Level
(Self_ID
, T
, 0);
137 -- Compiler interface only: Do not call from within the RTS,
139 -- except in the implementation of Ada.Task_Identification.
140 -- This must be called to implement the abort statement.
141 -- Much of the actual work of the abort is done by the abortee,
142 -- via the Abort_Handler signal handler, and propagation of the
143 -- Abort_Signal special exception.
145 procedure Abort_Tasks
(Tasks
: Task_List
) is
146 Self_Id
: constant Task_ID
:= STPO
.Self
;
152 -- Since this is a "potentially blocking operation", we should
153 -- add a separate check here that we are not inside a protected
156 Defer_Abort_Nestable
(Self_Id
);
159 -- Really should not be nested deferral here.
160 -- Patch for code generation error that defers abort before
161 -- evaluating parameters of an entry call (at least, timed entry
162 -- calls), and so may propagate an exception that causes abort
163 -- to remain undeferred indefinitely. See C97404B. When all
164 -- such bugs are fixed, this patch can be removed.
166 for J
in Tasks
'Range loop
168 Abort_One_Task
(Self_Id
, C
);
175 if C
.Pending_ATC_Level
> 0 then
176 P
:= C
.Common
.Parent
;
179 if P
.Pending_ATC_Level
= 0 then
180 Abort_One_Task
(Self_Id
, C
);
184 P
:= P
.Common
.Parent
;
188 C
:= C
.Common
.All_Tasks_Link
;
191 Unlock_All_Tasks_List
;
192 Undefer_Abort_Nestable
(Self_Id
);
195 -------------------------------
196 -- Cancel_Queued_Entry_Calls --
197 -------------------------------
199 -- Cancel any entry calls queued on target task. Call this only while
200 -- holding T locked, and nothing more. This should only be called by T,
201 -- unless T is a terminated previously unactivated task.
203 procedure Cancel_Queued_Entry_Calls
(T
: Task_ID
) is
204 Next_Entry_Call
: Entry_Call_Link
;
205 Entry_Call
: Entry_Call_Link
;
208 Self_Id
: constant Task_ID
:= STPO
.Self
;
211 pragma Assert
(T
= Self
or else T
.Common
.State
= Terminated
);
213 for J
in 1 .. T
.Entry_Num
loop
214 Queuing
.Dequeue_Head
(T
.Entry_Queues
(J
), Entry_Call
);
216 while Entry_Call
/= null loop
218 -- Leave Entry_Call.Done = False, since this is cancelled
220 Caller
:= Entry_Call
.Self
;
221 Entry_Call
.Exception_To_Raise
:= Tasking_Error
'Identity;
222 Queuing
.Dequeue_Head
(T
.Entry_Queues
(J
), Next_Entry_Call
);
223 Level
:= Entry_Call
.Level
- 1;
225 Write_Lock
(Entry_Call
.Self
);
226 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Cancelled
);
227 Unlock
(Entry_Call
.Self
);
229 Entry_Call
.State
:= Done
;
230 Entry_Call
:= Next_Entry_Call
;
233 end Cancel_Queued_Entry_Calls
;
235 ------------------------
236 -- Exit_One_ATC_Level --
237 ------------------------
239 -- Call only with abort deferred and holding lock of Self_Id.
240 -- This is a bit of common code for all entry calls.
241 -- The effect is to exit one level of ATC nesting.
243 -- If we have reached the desired ATC nesting level, reset the
244 -- requested level to effective infinity, to allow further calls.
245 -- In any case, reset Self_Id.Aborting, to allow re-raising of
248 procedure Exit_One_ATC_Level
(Self_ID
: Task_ID
) is
250 Self_ID
.ATC_Nesting_Level
:= Self_ID
.ATC_Nesting_Level
- 1;
253 (Debug
.Trace
(Self_ID
, "EOAL: exited to ATC level: " &
254 ATC_Level
'Image (Self_ID
.ATC_Nesting_Level
), 'A'));
256 pragma Assert
(Self_ID
.ATC_Nesting_Level
>= 1);
258 if Self_ID
.Pending_ATC_Level
< ATC_Level_Infinity
then
259 if Self_ID
.Pending_ATC_Level
= Self_ID
.ATC_Nesting_Level
then
260 Self_ID
.Pending_ATC_Level
:= ATC_Level_Infinity
;
261 Self_ID
.Aborting
:= False;
263 -- Force the next Undefer_Abort to re-raise Abort_Signal
266 (Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
);
268 if Self_ID
.Aborting
then
269 Self_ID
.ATC_Hack
:= True;
270 Self_ID
.Pending_Action
:= True;
274 end Exit_One_ATC_Level
;
276 ----------------------
277 -- Make_Independent --
278 ----------------------
280 -- Move the current task to the outermost level (level 2) of the master
281 -- hierarchy of the environment task. That is one level further out
282 -- than normal tasks defined in library-level packages (level 3). The
283 -- environment task will wait for level 3 tasks to terminate normally,
284 -- then it will abort all the level 2 tasks. See Finalize_Global_Tasks
285 -- procedure for more information.
287 -- This is a dangerous operation, and should only be used on nested tasks
288 -- or tasks that depend on any objects that might be finalized earlier than
289 -- the termination of the environment task. It is for internal use by the
290 -- GNARL, to prevent such internal server tasks from preventing a partition
293 -- Also note that the run time assumes that the parent of an independent
294 -- task is the environment task. If this is not the case, Make_Independent
295 -- will change the task's parent. This assumption is particularly
296 -- important for master level completion and for the computation of
297 -- Independent_Task_Count.
299 -- See procedures Init_RTS and Finalize_Global_Tasks for related code.
301 procedure Make_Independent
is
302 Self_Id
: constant Task_ID
:= STPO
.Self
;
303 Environment_Task
: constant Task_ID
:= STPO
.Environment_Task
;
304 Parent
: constant Task_ID
:= Self_Id
.Common
.Parent
;
305 Parent_Needs_Updating
: Boolean := False;
308 if Self_Id
.Known_Tasks_Index
/= -1 then
309 Known_Tasks
(Self_Id
.Known_Tasks_Index
) := null;
312 Defer_Abort
(Self_Id
);
313 Write_Lock
(Environment_Task
);
314 Write_Lock
(Self_Id
);
316 pragma Assert
(Parent
= Environment_Task
317 or else Self_Id
.Master_of_Task
= Library_Task_Level
);
319 Self_Id
.Master_of_Task
:= Independent_Task_Level
;
321 -- The run time assumes that the parent of an independent task is the
324 if Parent
/= Environment_Task
then
326 -- We can not lock three tasks at the same time, so defer the
327 -- operations on the parent.
329 Parent_Needs_Updating
:= True;
330 Self_Id
.Common
.Parent
:= Environment_Task
;
333 -- Update Independent_Task_Count that is needed for the GLADE
334 -- termination rule. See also pending update in
335 -- System.Tasking.Stages.Check_Independent
337 Independent_Task_Count
:= Independent_Task_Count
+ 1;
341 -- Changing the parent after creation is not trivial. Do not forget
342 -- to update the old parent counts, and the new parent (i.e. the
343 -- Environment_Task) counts.
345 if Parent_Needs_Updating
then
347 Parent
.Awake_Count
:= Parent
.Awake_Count
- 1;
348 Parent
.Alive_Count
:= Parent
.Alive_Count
- 1;
349 Environment_Task
.Awake_Count
:= Environment_Task
.Awake_Count
+ 1;
350 Environment_Task
.Alive_Count
:= Environment_Task
.Alive_Count
+ 1;
354 Unlock
(Environment_Task
);
355 Undefer_Abort
(Self_Id
);
356 end Make_Independent
;
362 -- Update counts to indicate current task is either terminated
363 -- or accepting on a terminate alternative. Call holding no locks.
365 procedure Make_Passive
367 Task_Completed
: Boolean)
369 C
: Task_ID
:= Self_ID
;
370 P
: Task_ID
:= C
.Common
.Parent
;
372 Master_Completion_Phase
: Integer;
381 if Task_Completed
then
382 Self_ID
.Common
.State
:= Terminated
;
384 if Self_ID
.Awake_Count
= 0 then
386 -- We are completing via a terminate alternative.
387 -- Our parent should wait in Phase 2 of Complete_Master.
389 Master_Completion_Phase
:= 2;
391 pragma Assert
(Task_Completed
);
392 pragma Assert
(Self_ID
.Terminate_Alternative
);
393 pragma Assert
(Self_ID
.Alive_Count
= 1);
396 -- We are NOT on a terminate alternative.
397 -- Our parent should wait in Phase 1 of Complete_Master.
399 Master_Completion_Phase
:= 1;
400 pragma Assert
(Self_ID
.Awake_Count
= 1);
403 -- We are accepting with a terminate alternative.
406 if Self_ID
.Open_Accepts
= null then
408 -- Somebody started a rendezvous while we had our lock open.
409 -- Skip the terminate alternative.
420 Self_ID
.Terminate_Alternative
:= True;
421 Master_Completion_Phase
:= 0;
423 pragma Assert
(Self_ID
.Terminate_Alternative
);
424 pragma Assert
(Self_ID
.Awake_Count
>= 1);
427 if Master_Completion_Phase
= 2 then
429 -- Since our Awake_Count is zero but our Alive_Count
430 -- is nonzero, we have been accepting with a terminate
431 -- alternative, and we now have been told to terminate
432 -- by a completed master (in some ancestor task) that
433 -- is waiting (with zero Awake_Count) in Phase 2 of
437 (Debug
.Trace
(Self_ID
, "Make_Passive: Phase 2", 'M'));
439 pragma Assert
(P
/= null);
441 C
.Alive_Count
:= C
.Alive_Count
- 1;
443 if C
.Alive_Count
> 0 then
449 -- C's count just went to zero, indicating that
450 -- all of C's dependents are terminated.
451 -- C has a parent, P.
454 -- C's count just went to zero, indicating that all of C's
455 -- dependents are terminated. C has a parent, P. Notify P that
456 -- C and its dependents have all terminated.
458 P
.Alive_Count
:= P
.Alive_Count
- 1;
459 exit when P
.Alive_Count
> 0;
463 P
:= C
.Common
.Parent
;
465 -- Environment task cannot have terminated yet
467 pragma Assert
(P
/= null);
473 pragma Assert
(P
.Awake_Count
/= 0);
475 if P
.Common
.State
= Master_Phase_2_Sleep
476 and then C
.Master_of_Task
= P
.Master_Within
479 pragma Assert
(P
.Common
.Wait_Count
> 0);
480 P
.Common
.Wait_Count
:= P
.Common
.Wait_Count
- 1;
482 if P
.Common
.Wait_Count
= 0 then
483 Wakeup
(P
, Master_Phase_2_Sleep
);
492 -- We are terminating in Phase 1 or Complete_Master,
493 -- or are accepting on a terminate alternative.
495 C
.Awake_Count
:= C
.Awake_Count
- 1;
497 if Task_Completed
then
498 pragma Assert
(Self_ID
.Awake_Count
= 0);
499 C
.Alive_Count
:= C
.Alive_Count
- 1;
502 if C
.Awake_Count
> 0 or else P
= null then
512 -- C's count just went to zero, indicating that all of C's
513 -- dependents are terminated or accepting with terminate alt.
514 -- C has a parent, P.
517 -- Notify P that C has gone passive.
519 P
.Awake_Count
:= P
.Awake_Count
- 1;
521 if Task_Completed
and then C
.Alive_Count
= 0 then
522 P
.Alive_Count
:= P
.Alive_Count
- 1;
525 exit when P
.Awake_Count
> 0;
529 P
:= C
.Common
.Parent
;
539 -- P has non-passive dependents.
541 if P
.Common
.State
= Master_Completion_Sleep
and then
542 C
.Master_of_Task
= P
.Master_Within
546 (Self_ID
, "Make_Passive: Phase 1, parent waiting", 'M'));
548 -- If parent is in Master_Completion_Sleep, it
549 -- cannot be on a terminate alternative, hence
550 -- it cannot have Awake_Count of zero.
552 pragma Assert
(P
.Common
.Wait_Count
> 0);
553 P
.Common
.Wait_Count
:= P
.Common
.Wait_Count
- 1;
555 if P
.Common
.Wait_Count
= 0 then
556 Wakeup
(P
, Master_Completion_Sleep
);
562 (Self_ID
, "Make_Passive: Phase 1, parent awake", 'M'));
570 end System
.Tasking
.Utilities
;