gcc/
[official-gcc.git] / gcc / ada / s-tassta.adb
blobdecfcab0f83db4a05f909eebfd52192bed450d27
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . S T A G E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 pragma Polling (Off);
33 -- Turn off polling, we do not want ATC polling to take place during tasking
34 -- operations. It causes infinite loops and other problems.
36 pragma Partition_Elaboration_Policy (Concurrent);
37 -- This package only implements the concurrent elaboration policy. This pragma
38 -- will enforce it (and detect conflicts with user specified policy).
40 with Ada.Exceptions;
41 with Ada.Unchecked_Deallocation;
43 with System.Interrupt_Management;
44 with System.Tasking.Debug;
45 with System.Address_Image;
46 with System.Task_Primitives;
47 with System.Task_Primitives.Operations;
48 with System.Tasking.Utilities;
49 with System.Tasking.Queuing;
50 with System.Tasking.Rendezvous;
51 with System.OS_Primitives;
52 with System.Secondary_Stack;
53 with System.Storage_Elements;
54 with System.Restrictions;
55 with System.Standard_Library;
56 with System.Traces.Tasking;
57 with System.Stack_Usage;
59 with System.Soft_Links;
60 -- These are procedure pointers to non-tasking routines that use task
61 -- specific data. In the absence of tasking, these routines refer to global
62 -- data. In the presence of tasking, they must be replaced with pointers to
63 -- task-specific versions. Also used for Create_TSD, Destroy_TSD, Get_Current
64 -- _Excep, Finalize_Library_Objects, Task_Termination, Handler.
66 with System.Tasking.Initialization;
67 pragma Elaborate_All (System.Tasking.Initialization);
68 -- This insures that tasking is initialized if any tasks are created
70 package body System.Tasking.Stages is
72 package STPO renames System.Task_Primitives.Operations;
73 package SSL renames System.Soft_Links;
74 package SSE renames System.Storage_Elements;
75 package SST renames System.Secondary_Stack;
77 use Ada.Exceptions;
79 use Parameters;
80 use Task_Primitives;
81 use Task_Primitives.Operations;
82 use Task_Info;
84 use System.Traces;
85 use System.Traces.Tasking;
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 procedure Free is new
92 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
94 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id);
95 -- This procedure outputs the task specific message for exception
96 -- tracing purposes.
98 procedure Task_Wrapper (Self_ID : Task_Id);
99 pragma Convention (C, Task_Wrapper);
100 -- This is the procedure that is called by the GNULL from the new context
101 -- when a task is created. It waits for activation and then calls the task
102 -- body procedure. When the task body procedure completes, it terminates
103 -- the task.
105 -- The Task_Wrapper's address will be provided to the underlying threads
106 -- library as the task entry point. Convention C is what makes most sense
107 -- for that purpose (Export C would make the function globally visible,
108 -- and affect the link name on which GDB depends). This will in addition
109 -- trigger an automatic stack alignment suitable for GCC's assumptions if
110 -- need be.
112 -- "Vulnerable_..." in the procedure names below means they must be called
113 -- with abort deferred.
115 procedure Vulnerable_Complete_Task (Self_ID : Task_Id);
116 -- Complete the calling task. This procedure must be called with
117 -- abort deferred. It should only be called by Complete_Task and
118 -- Finalize_Global_Tasks (for the environment task).
120 procedure Vulnerable_Complete_Master (Self_ID : Task_Id);
121 -- Complete the current master of the calling task. This procedure
122 -- must be called with abort deferred. It should only be called by
123 -- Vulnerable_Complete_Task and Complete_Master.
125 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id);
126 -- Signal to Self_ID's activator that Self_ID has completed activation.
127 -- This procedure must be called with abort deferred.
129 procedure Abort_Dependents (Self_ID : Task_Id);
130 -- Abort all the direct dependents of Self at its current master nesting
131 -- level, plus all of their dependents, transitively. RTS_Lock should be
132 -- locked by the caller.
134 procedure Vulnerable_Free_Task (T : Task_Id);
135 -- Recover all runtime system storage associated with the task T. This
136 -- should only be called after T has terminated and will no longer be
137 -- referenced.
139 -- For tasks created by an allocator that fails, due to an exception, it is
140 -- called from Expunge_Unactivated_Tasks.
142 -- Different code is used at master completion, in Terminate_Dependents,
143 -- due to a need for tighter synchronization with the master.
145 ----------------------
146 -- Abort_Dependents --
147 ----------------------
149 procedure Abort_Dependents (Self_ID : Task_Id) is
150 C : Task_Id;
151 P : Task_Id;
153 -- Each task C will take care of its own dependents, so there is no
154 -- need to worry about them here. In fact, it would be wrong to abort
155 -- indirect dependents here, because we can't distinguish between
156 -- duplicate master ids. For example, suppose we have three nested
157 -- task bodies T1,T2,T3. And suppose T1 also calls P which calls Q (and
158 -- both P and Q are task masters). Q will have the same master id as
159 -- Master_of_Task of T3. Previous versions of this would abort T3 when
160 -- Q calls Complete_Master, which was completely wrong.
162 begin
163 C := All_Tasks_List;
164 while C /= null loop
165 P := C.Common.Parent;
167 if P = Self_ID then
168 if C.Master_of_Task = Self_ID.Master_Within then
169 pragma Debug
170 (Debug.Trace (Self_ID, "Aborting", 'X', C));
171 Utilities.Abort_One_Task (Self_ID, C);
172 C.Dependents_Aborted := True;
173 end if;
174 end if;
176 C := C.Common.All_Tasks_Link;
177 end loop;
179 Self_ID.Dependents_Aborted := True;
180 end Abort_Dependents;
182 -----------------
183 -- Abort_Tasks --
184 -----------------
186 procedure Abort_Tasks (Tasks : Task_List) is
187 begin
188 Utilities.Abort_Tasks (Tasks);
189 end Abort_Tasks;
191 --------------------
192 -- Activate_Tasks --
193 --------------------
195 -- Note that locks of activator and activated task are both locked here.
196 -- This is necessary because C.Common.State and Self.Common.Wait_Count have
197 -- to be synchronized. This is safe from deadlock because the activator is
198 -- always created before the activated task. That satisfies our
199 -- in-order-of-creation ATCB locking policy.
201 -- At one point, we may also lock the parent, if the parent is different
202 -- from the activator. That is also consistent with the lock ordering
203 -- policy, since the activator cannot be created before the parent.
205 -- Since we are holding both the activator's lock, and Task_Wrapper locks
206 -- that before it does anything more than initialize the low-level ATCB
207 -- components, it should be safe to wait to update the counts until we see
208 -- that the thread creation is successful.
210 -- If the thread creation fails, we do need to close the entries of the
211 -- task. The first phase, of dequeuing calls, only requires locking the
212 -- acceptor's ATCB, but the waking up of the callers requires locking the
213 -- caller's ATCB. We cannot safely do this while we are holding other
214 -- locks. Therefore, the queue-clearing operation is done in a separate
215 -- pass over the activation chain.
217 procedure Activate_Tasks (Chain_Access : Activation_Chain_Access) is
218 Self_ID : constant Task_Id := STPO.Self;
219 P : Task_Id;
220 C : Task_Id;
221 Next_C, Last_C : Task_Id;
222 Activate_Prio : System.Any_Priority;
223 Success : Boolean;
224 All_Elaborated : Boolean := True;
226 begin
227 -- If pragma Detect_Blocking is active, then we must check whether this
228 -- potentially blocking operation is called from a protected action.
230 if System.Tasking.Detect_Blocking
231 and then Self_ID.Common.Protected_Action_Nesting > 0
232 then
233 raise Program_Error with "potentially blocking operation";
234 end if;
236 pragma Debug
237 (Debug.Trace (Self_ID, "Activate_Tasks", 'C'));
239 Initialization.Defer_Abort_Nestable (Self_ID);
241 pragma Assert (Self_ID.Common.Wait_Count = 0);
243 -- Lock RTS_Lock, to prevent activated tasks from racing ahead before
244 -- we finish activating the chain.
246 Lock_RTS;
248 -- Check that all task bodies have been elaborated
250 C := Chain_Access.T_ID;
251 Last_C := null;
252 while C /= null loop
253 if C.Common.Elaborated /= null
254 and then not C.Common.Elaborated.all
255 then
256 All_Elaborated := False;
257 end if;
259 -- Reverse the activation chain so that tasks are activated in the
260 -- same order they're declared.
262 Next_C := C.Common.Activation_Link;
263 C.Common.Activation_Link := Last_C;
264 Last_C := C;
265 C := Next_C;
266 end loop;
268 Chain_Access.T_ID := Last_C;
270 if not All_Elaborated then
271 Unlock_RTS;
272 Initialization.Undefer_Abort_Nestable (Self_ID);
273 raise Program_Error with "Some tasks have not been elaborated";
274 end if;
276 -- Activate all the tasks in the chain. Creation of the thread of
277 -- control was deferred until activation. So create it now.
279 C := Chain_Access.T_ID;
280 while C /= null loop
281 if C.Common.State /= Terminated then
282 pragma Assert (C.Common.State = Unactivated);
284 P := C.Common.Parent;
285 Write_Lock (P);
286 Write_Lock (C);
288 Activate_Prio :=
289 (if C.Common.Base_Priority < Get_Priority (Self_ID)
290 then Get_Priority (Self_ID)
291 else C.Common.Base_Priority);
293 System.Task_Primitives.Operations.Create_Task
294 (C, Task_Wrapper'Address,
295 Parameters.Size_Type
296 (C.Common.Compiler_Data.Pri_Stack_Info.Size),
297 Activate_Prio, Success);
299 -- There would be a race between the created task and the creator
300 -- to do the following initialization, if we did not have a
301 -- Lock/Unlock_RTS pair in the task wrapper to prevent it from
302 -- racing ahead.
304 if Success then
305 C.Common.State := Activating;
306 C.Awake_Count := 1;
307 C.Alive_Count := 1;
308 P.Awake_Count := P.Awake_Count + 1;
309 P.Alive_Count := P.Alive_Count + 1;
311 if P.Common.State = Master_Completion_Sleep and then
312 C.Master_of_Task = P.Master_Within
313 then
314 pragma Assert (Self_ID /= P);
315 P.Common.Wait_Count := P.Common.Wait_Count + 1;
316 end if;
318 for J in System.Tasking.Debug.Known_Tasks'Range loop
319 if System.Tasking.Debug.Known_Tasks (J) = null then
320 System.Tasking.Debug.Known_Tasks (J) := C;
321 C.Known_Tasks_Index := J;
322 exit;
323 end if;
324 end loop;
326 if Global_Task_Debug_Event_Set then
327 Debug.Signal_Debug_Event
328 (Debug.Debug_Event_Activating, C);
329 end if;
331 C.Common.State := Runnable;
333 Unlock (C);
334 Unlock (P);
336 else
337 -- No need to set Awake_Count, State, etc. here since the loop
338 -- below will do that for any Unactivated tasks.
340 Unlock (C);
341 Unlock (P);
342 Self_ID.Common.Activation_Failed := True;
343 end if;
344 end if;
346 C := C.Common.Activation_Link;
347 end loop;
349 if not Single_Lock then
350 Unlock_RTS;
351 end if;
353 -- Close the entries of any tasks that failed thread creation, and count
354 -- those that have not finished activation.
356 Write_Lock (Self_ID);
357 Self_ID.Common.State := Activator_Sleep;
359 C := Chain_Access.T_ID;
360 while C /= null loop
361 Write_Lock (C);
363 if C.Common.State = Unactivated then
364 C.Common.Activator := null;
365 C.Common.State := Terminated;
366 C.Callable := False;
367 Utilities.Cancel_Queued_Entry_Calls (C);
369 elsif C.Common.Activator /= null then
370 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
371 end if;
373 Unlock (C);
374 P := C.Common.Activation_Link;
375 C.Common.Activation_Link := null;
376 C := P;
377 end loop;
379 -- Wait for the activated tasks to complete activation. It is
380 -- unsafe to abort any of these tasks until the count goes to zero.
382 loop
383 exit when Self_ID.Common.Wait_Count = 0;
384 Sleep (Self_ID, Activator_Sleep);
385 end loop;
387 Self_ID.Common.State := Runnable;
388 Unlock (Self_ID);
390 if Single_Lock then
391 Unlock_RTS;
392 end if;
394 -- Remove the tasks from the chain
396 Chain_Access.T_ID := null;
397 Initialization.Undefer_Abort_Nestable (Self_ID);
399 if Self_ID.Common.Activation_Failed then
400 Self_ID.Common.Activation_Failed := False;
401 raise Tasking_Error with "Failure during activation";
402 end if;
403 end Activate_Tasks;
405 -------------------------
406 -- Complete_Activation --
407 -------------------------
409 procedure Complete_Activation is
410 Self_ID : constant Task_Id := STPO.Self;
412 begin
413 Initialization.Defer_Abort_Nestable (Self_ID);
415 if Single_Lock then
416 Lock_RTS;
417 end if;
419 Vulnerable_Complete_Activation (Self_ID);
421 if Single_Lock then
422 Unlock_RTS;
423 end if;
425 Initialization.Undefer_Abort_Nestable (Self_ID);
427 -- ??? Why do we need to allow for nested deferral here?
429 if Runtime_Traces then
430 Send_Trace_Info (T_Activate);
431 end if;
432 end Complete_Activation;
434 ---------------------
435 -- Complete_Master --
436 ---------------------
438 procedure Complete_Master is
439 Self_ID : constant Task_Id := STPO.Self;
440 begin
441 pragma Assert
442 (Self_ID.Deferral_Level > 0
443 or else not System.Restrictions.Abort_Allowed);
444 Vulnerable_Complete_Master (Self_ID);
445 end Complete_Master;
447 -------------------
448 -- Complete_Task --
449 -------------------
451 -- See comments on Vulnerable_Complete_Task for details
453 procedure Complete_Task is
454 Self_ID : constant Task_Id := STPO.Self;
456 begin
457 pragma Assert
458 (Self_ID.Deferral_Level > 0
459 or else not System.Restrictions.Abort_Allowed);
461 Vulnerable_Complete_Task (Self_ID);
463 -- All of our dependents have terminated, never undefer abort again
465 end Complete_Task;
467 -----------------
468 -- Create_Task --
469 -----------------
471 -- Compiler interface only. Do not call from within the RTS. This must be
472 -- called to create a new task.
474 procedure Create_Task
475 (Priority : Integer;
476 Size : System.Parameters.Size_Type;
477 Task_Info : System.Task_Info.Task_Info_Type;
478 CPU : Integer;
479 Relative_Deadline : Ada.Real_Time.Time_Span;
480 Domain : Dispatching_Domain_Access;
481 Num_Entries : Task_Entry_Index;
482 Master : Master_Level;
483 State : Task_Procedure_Access;
484 Discriminants : System.Address;
485 Elaborated : Access_Boolean;
486 Chain : in out Activation_Chain;
487 Task_Image : String;
488 Created_Task : out Task_Id)
490 T, P : Task_Id;
491 Self_ID : constant Task_Id := STPO.Self;
492 Success : Boolean;
493 Base_Priority : System.Any_Priority;
494 Len : Natural;
495 Base_CPU : System.Multiprocessors.CPU_Range;
497 use type System.Multiprocessors.CPU_Range;
499 pragma Unreferenced (Relative_Deadline);
500 -- EDF scheduling is not supported by any of the target platforms so
501 -- this parameter is not passed any further.
503 begin
504 -- If Master is greater than the current master, it means that Master
505 -- has already awaited its dependent tasks. This raises Program_Error,
506 -- by 4.8(10.3/2). See AI-280. Ignore this check for foreign threads.
508 if Self_ID.Master_of_Task /= Foreign_Task_Level
509 and then Master > Self_ID.Master_Within
510 then
511 raise Program_Error with
512 "create task after awaiting termination";
513 end if;
515 -- If pragma Detect_Blocking is active must be checked whether this
516 -- potentially blocking operation is called from a protected action.
518 if System.Tasking.Detect_Blocking
519 and then Self_ID.Common.Protected_Action_Nesting > 0
520 then
521 raise Program_Error with "potentially blocking operation";
522 end if;
524 pragma Debug (Debug.Trace (Self_ID, "Create_Task", 'C'));
526 Base_Priority :=
527 (if Priority = Unspecified_Priority
528 then Self_ID.Common.Base_Priority
529 else System.Any_Priority (Priority));
531 -- Legal values of CPU are the special Unspecified_CPU value which is
532 -- inserted by the compiler for tasks without CPU aspect, and those in
533 -- the range of CPU_Range but no greater than Number_Of_CPUs. Otherwise
534 -- the task is defined to have failed, and it becomes a completed task
535 -- (RM D.16(14/3)).
537 if CPU /= Unspecified_CPU
538 and then (CPU < Integer (System.Multiprocessors.CPU_Range'First)
539 or else
540 CPU > Integer (System.Multiprocessors.CPU_Range'Last)
541 or else
542 CPU > Integer (System.Multiprocessors.Number_Of_CPUs))
543 then
544 raise Tasking_Error with "CPU not in range";
546 -- Normal CPU affinity
548 else
549 -- When the application code says nothing about the task affinity
550 -- (task without CPU aspect) then the compiler inserts the
551 -- Unspecified_CPU value which indicates to the run-time library that
552 -- the task will activate and execute on the same processor as its
553 -- activating task if the activating task is assigned a processor
554 -- (RM D.16(14/3)).
556 Base_CPU :=
557 (if CPU = Unspecified_CPU
558 then Self_ID.Common.Base_CPU
559 else System.Multiprocessors.CPU_Range (CPU));
560 end if;
562 -- Find parent P of new Task, via master level number
564 P := Self_ID;
566 if P /= null then
567 while P.Master_of_Task >= Master loop
568 P := P.Common.Parent;
569 exit when P = null;
570 end loop;
571 end if;
573 Initialization.Defer_Abort_Nestable (Self_ID);
575 begin
576 T := New_ATCB (Num_Entries);
577 exception
578 when others =>
579 Initialization.Undefer_Abort_Nestable (Self_ID);
580 raise Storage_Error with "Cannot allocate task";
581 end;
583 -- RTS_Lock is used by Abort_Dependents and Abort_Tasks. Up to this
584 -- point, it is possible that we may be part of a family of tasks that
585 -- is being aborted.
587 Lock_RTS;
588 Write_Lock (Self_ID);
590 -- Now, we must check that we have not been aborted. If so, we should
591 -- give up on creating this task, and simply return.
593 if not Self_ID.Callable then
594 pragma Assert (Self_ID.Pending_ATC_Level = 0);
595 pragma Assert (Self_ID.Pending_Action);
596 pragma Assert
597 (Chain.T_ID = null or else Chain.T_ID.Common.State = Unactivated);
599 Unlock (Self_ID);
600 Unlock_RTS;
601 Initialization.Undefer_Abort_Nestable (Self_ID);
603 -- ??? Should never get here
605 pragma Assert (False);
606 raise Standard'Abort_Signal;
607 end if;
609 Initialize_ATCB (Self_ID, State, Discriminants, P, Elaborated,
610 Base_Priority, Base_CPU, Domain, Task_Info, Size, T, Success);
612 if not Success then
613 Free (T);
614 Unlock (Self_ID);
615 Unlock_RTS;
616 Initialization.Undefer_Abort_Nestable (Self_ID);
617 raise Storage_Error with "Failed to initialize task";
618 end if;
620 if Master = Foreign_Task_Level + 2 then
622 -- This should not happen, except when a foreign task creates non
623 -- library-level Ada tasks. In this case, we pretend the master is
624 -- a regular library level task, otherwise the run-time will get
625 -- confused when waiting for these tasks to terminate.
627 T.Master_of_Task := Library_Task_Level;
629 else
630 T.Master_of_Task := Master;
631 end if;
633 T.Master_Within := T.Master_of_Task + 1;
635 for L in T.Entry_Calls'Range loop
636 T.Entry_Calls (L).Self := T;
637 T.Entry_Calls (L).Level := L;
638 end loop;
640 if Task_Image'Length = 0 then
641 T.Common.Task_Image_Len := 0;
642 else
643 Len := 1;
644 T.Common.Task_Image (1) := Task_Image (Task_Image'First);
646 -- Remove unwanted blank space generated by 'Image
648 for J in Task_Image'First + 1 .. Task_Image'Last loop
649 if Task_Image (J) /= ' '
650 or else Task_Image (J - 1) /= '('
651 then
652 Len := Len + 1;
653 T.Common.Task_Image (Len) := Task_Image (J);
654 exit when Len = T.Common.Task_Image'Last;
655 end if;
656 end loop;
658 T.Common.Task_Image_Len := Len;
659 end if;
661 -- The task inherits the dispatching domain of the parent only if no
662 -- specific domain has been defined in the spec of the task (using the
663 -- dispatching domain pragma or aspect).
665 if T.Common.Domain /= null then
666 null;
667 elsif T.Common.Activator /= null then
668 T.Common.Domain := T.Common.Activator.Common.Domain;
669 else
670 T.Common.Domain := System.Tasking.System_Domain;
671 end if;
673 Unlock (Self_ID);
674 Unlock_RTS;
676 -- The CPU associated to the task (if any) must belong to the
677 -- dispatching domain.
679 if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
680 and then
681 (Base_CPU not in T.Common.Domain'Range
682 or else not T.Common.Domain (Base_CPU))
683 then
684 Initialization.Undefer_Abort_Nestable (Self_ID);
685 raise Tasking_Error with "CPU not in dispatching domain";
686 end if;
688 -- To handle the interaction between pragma CPU and dispatching domains
689 -- we need to signal that this task is being allocated to a processor.
690 -- This is needed only for tasks belonging to the system domain (the
691 -- creation of new dispatching domains can only take processors from the
692 -- system domain) and only before the environment task calls the main
693 -- procedure (dispatching domains cannot be created after this).
695 if Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
696 and then T.Common.Domain = System.Tasking.System_Domain
697 and then not System.Tasking.Dispatching_Domains_Frozen
698 then
699 -- Increase the number of tasks attached to the CPU to which this
700 -- task is being moved.
702 Dispatching_Domain_Tasks (Base_CPU) :=
703 Dispatching_Domain_Tasks (Base_CPU) + 1;
704 end if;
706 -- Create TSD as early as possible in the creation of a task, since it
707 -- may be used by the operation of Ada code within the task.
709 SSL.Create_TSD (T.Common.Compiler_Data);
710 T.Common.Activation_Link := Chain.T_ID;
711 Chain.T_ID := T;
712 Initialization.Initialize_Attributes_Link.all (T);
713 Created_Task := T;
714 Initialization.Undefer_Abort_Nestable (Self_ID);
716 if Runtime_Traces then
717 Send_Trace_Info (T_Create, T);
718 end if;
720 pragma Debug
721 (Debug.Trace
722 (Self_ID, "Created task in " & T.Master_of_Task'Img, 'C', T));
723 end Create_Task;
725 --------------------
726 -- Current_Master --
727 --------------------
729 function Current_Master return Master_Level is
730 begin
731 return STPO.Self.Master_Within;
732 end Current_Master;
734 ------------------
735 -- Enter_Master --
736 ------------------
738 procedure Enter_Master is
739 Self_ID : constant Task_Id := STPO.Self;
740 begin
741 Self_ID.Master_Within := Self_ID.Master_Within + 1;
742 pragma Debug
743 (Debug.Trace
744 (Self_ID, "Enter_Master ->" & Self_ID.Master_Within'Img, 'M'));
745 end Enter_Master;
747 -------------------------------
748 -- Expunge_Unactivated_Tasks --
749 -------------------------------
751 -- See procedure Close_Entries for the general case
753 procedure Expunge_Unactivated_Tasks (Chain : in out Activation_Chain) is
754 Self_ID : constant Task_Id := STPO.Self;
755 C : Task_Id;
756 Call : Entry_Call_Link;
757 Temp : Task_Id;
759 begin
760 pragma Debug
761 (Debug.Trace (Self_ID, "Expunge_Unactivated_Tasks", 'C'));
763 Initialization.Defer_Abort_Nestable (Self_ID);
765 -- ???
766 -- Experimentation has shown that abort is sometimes (but not always)
767 -- already deferred when this is called.
769 -- That may indicate an error. Find out what is going on
771 C := Chain.T_ID;
772 while C /= null loop
773 pragma Assert (C.Common.State = Unactivated);
775 Temp := C.Common.Activation_Link;
777 if C.Common.State = Unactivated then
778 Lock_RTS;
779 Write_Lock (C);
781 for J in 1 .. C.Entry_Num loop
782 Queuing.Dequeue_Head (C.Entry_Queues (J), Call);
783 pragma Assert (Call = null);
784 end loop;
786 Unlock (C);
788 Initialization.Remove_From_All_Tasks_List (C);
789 Unlock_RTS;
791 Vulnerable_Free_Task (C);
792 C := Temp;
793 end if;
794 end loop;
796 Chain.T_ID := null;
797 Initialization.Undefer_Abort_Nestable (Self_ID);
798 end Expunge_Unactivated_Tasks;
800 ---------------------------
801 -- Finalize_Global_Tasks --
802 ---------------------------
804 -- ???
805 -- We have a potential problem here if finalization of global objects does
806 -- anything with signals or the timer server, since by that time those
807 -- servers have terminated.
809 -- It is hard to see how that would occur
811 -- However, a better solution might be to do all this finalization
812 -- using the global finalization chain.
814 procedure Finalize_Global_Tasks is
815 Self_ID : constant Task_Id := STPO.Self;
817 Ignore_1 : Boolean;
818 Ignore_2 : Boolean;
819 pragma Unreferenced (Ignore_1, Ignore_2);
821 function State
822 (Int : System.Interrupt_Management.Interrupt_ID) return Character;
823 pragma Import (C, State, "__gnat_get_interrupt_state");
824 -- Get interrupt state for interrupt number Int. Defined in init.c
826 Default : constant Character := 's';
827 -- 's' Interrupt_State pragma set state to System (use "default"
828 -- system handler)
830 begin
831 if Self_ID.Deferral_Level = 0 then
832 -- ???
833 -- In principle, we should be able to predict whether abort is
834 -- already deferred here (and it should not be deferred yet but in
835 -- practice it seems Finalize_Global_Tasks is being called sometimes,
836 -- from RTS code for exceptions, with abort already deferred.
838 Initialization.Defer_Abort_Nestable (Self_ID);
840 -- Never undefer again
841 end if;
843 -- This code is only executed by the environment task
845 pragma Assert (Self_ID = Environment_Task);
847 -- Set Environment_Task'Callable to false to notify library-level tasks
848 -- that it is waiting for them.
850 Self_ID.Callable := False;
852 -- Exit level 2 master, for normal tasks in library-level packages
854 Complete_Master;
856 -- Force termination of "independent" library-level server tasks
858 Lock_RTS;
860 Abort_Dependents (Self_ID);
862 if not Single_Lock then
863 Unlock_RTS;
864 end if;
866 -- We need to explicitly wait for the task to be terminated here
867 -- because on true concurrent system, we may end this procedure before
868 -- the tasks are really terminated.
870 Write_Lock (Self_ID);
872 -- If the Abort_Task signal is set to system, it means that we may
873 -- not have been able to abort all independent tasks (in particular,
874 -- Server_Task may be blocked, waiting for a signal), in which case, do
875 -- not wait for Independent_Task_Count to go down to 0. We arbitrarily
876 -- limit the number of loop iterations; if an independent task does not
877 -- terminate, we do not want to hang here. In that case, the thread will
878 -- be terminated when the process exits.
880 if State (System.Interrupt_Management.Abort_Task_Interrupt) /= Default
881 then
882 for J in 1 .. 10 loop
883 exit when Utilities.Independent_Task_Count = 0;
885 -- We used to yield here, but this did not take into account low
886 -- priority tasks that would cause dead lock in some cases (true
887 -- FIFO scheduling).
889 Timed_Sleep
890 (Self_ID, 0.01, System.OS_Primitives.Relative,
891 Self_ID.Common.State, Ignore_1, Ignore_2);
892 end loop;
893 end if;
895 -- ??? On multi-processor environments, it seems that the above loop
896 -- isn't sufficient, so we need to add an additional delay.
898 Timed_Sleep
899 (Self_ID, 0.01, System.OS_Primitives.Relative,
900 Self_ID.Common.State, Ignore_1, Ignore_2);
902 Unlock (Self_ID);
904 if Single_Lock then
905 Unlock_RTS;
906 end if;
908 -- Complete the environment task
910 Vulnerable_Complete_Task (Self_ID);
912 -- Handle normal task termination by the environment task, but only
913 -- for the normal task termination. In the case of Abnormal and
914 -- Unhandled_Exception they must have been handled before, and the
915 -- task termination soft link must have been changed so the task
916 -- termination routine is not executed twice.
918 SSL.Task_Termination_Handler.all (Ada.Exceptions.Null_Occurrence);
920 -- Finalize all library-level controlled objects
922 if not SSL."=" (SSL.Finalize_Library_Objects, null) then
923 SSL.Finalize_Library_Objects.all;
924 end if;
926 -- Reset the soft links to non-tasking
928 SSL.Abort_Defer := SSL.Abort_Defer_NT'Access;
929 SSL.Abort_Undefer := SSL.Abort_Undefer_NT'Access;
930 SSL.Lock_Task := SSL.Task_Lock_NT'Access;
931 SSL.Unlock_Task := SSL.Task_Unlock_NT'Access;
932 SSL.Get_Jmpbuf_Address := SSL.Get_Jmpbuf_Address_NT'Access;
933 SSL.Set_Jmpbuf_Address := SSL.Set_Jmpbuf_Address_NT'Access;
934 SSL.Get_Sec_Stack_Addr := SSL.Get_Sec_Stack_Addr_NT'Access;
935 SSL.Set_Sec_Stack_Addr := SSL.Set_Sec_Stack_Addr_NT'Access;
936 SSL.Check_Abort_Status := SSL.Check_Abort_Status_NT'Access;
937 SSL.Get_Stack_Info := SSL.Get_Stack_Info_NT'Access;
939 -- Don't bother trying to finalize Initialization.Global_Task_Lock
940 -- and System.Task_Primitives.RTS_Lock.
942 end Finalize_Global_Tasks;
944 ---------------
945 -- Free_Task --
946 ---------------
948 procedure Free_Task (T : Task_Id) is
949 Self_Id : constant Task_Id := Self;
951 begin
952 if T.Common.State = Terminated then
954 -- It is not safe to call Abort_Defer or Write_Lock at this stage
956 Initialization.Task_Lock (Self_Id);
958 Lock_RTS;
959 Initialization.Finalize_Attributes_Link.all (T);
960 Initialization.Remove_From_All_Tasks_List (T);
961 Unlock_RTS;
963 Initialization.Task_Unlock (Self_Id);
965 System.Task_Primitives.Operations.Finalize_TCB (T);
967 else
968 -- If the task is not terminated, then mark the task as to be freed
969 -- upon termination.
971 T.Free_On_Termination := True;
972 end if;
973 end Free_Task;
975 ---------------------------
976 -- Move_Activation_Chain --
977 ---------------------------
979 procedure Move_Activation_Chain
980 (From, To : Activation_Chain_Access;
981 New_Master : Master_ID)
983 Self_ID : constant Task_Id := STPO.Self;
984 C : Task_Id;
986 begin
987 pragma Debug
988 (Debug.Trace (Self_ID, "Move_Activation_Chain", 'C'));
990 -- Nothing to do if From is empty, and we can check that without
991 -- deferring aborts.
993 C := From.all.T_ID;
995 if C = null then
996 return;
997 end if;
999 Initialization.Defer_Abort (Self_ID);
1001 -- Loop through the From chain, changing their Master_of_Task fields,
1002 -- and to find the end of the chain.
1004 loop
1005 C.Master_of_Task := New_Master;
1006 exit when C.Common.Activation_Link = null;
1007 C := C.Common.Activation_Link;
1008 end loop;
1010 -- Hook From in at the start of To
1012 C.Common.Activation_Link := To.all.T_ID;
1013 To.all.T_ID := From.all.T_ID;
1015 -- Set From to empty
1017 From.all.T_ID := null;
1019 Initialization.Undefer_Abort (Self_ID);
1020 end Move_Activation_Chain;
1022 ------------------
1023 -- Task_Wrapper --
1024 ------------------
1026 -- The task wrapper is a procedure that is called first for each task body
1027 -- and which in turn calls the compiler-generated task body procedure.
1028 -- The wrapper's main job is to do initialization for the task. It also
1029 -- has some locally declared objects that serve as per-task local data.
1030 -- Task finalization is done by Complete_Task, which is called from an
1031 -- at-end handler that the compiler generates.
1033 procedure Task_Wrapper (Self_ID : Task_Id) is
1034 use type SSE.Storage_Offset;
1035 use System.Standard_Library;
1036 use System.Stack_Usage;
1038 Bottom_Of_Stack : aliased Integer;
1040 Task_Alternate_Stack :
1041 aliased SSE.Storage_Array (1 .. Alternate_Stack_Size);
1042 -- The alternate signal stack for this task, if any
1044 Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0;
1045 -- Whether to use above alternate signal stack for stack overflows
1047 Secondary_Stack_Size :
1048 constant SSE.Storage_Offset :=
1049 Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size *
1050 SSE.Storage_Offset (Parameters.Sec_Stack_Percentage) / 100;
1052 Secondary_Stack : aliased SSE.Storage_Array (1 .. Secondary_Stack_Size);
1053 -- Actual area allocated for secondary stack
1055 Secondary_Stack_Address : System.Address := Secondary_Stack'Address;
1056 -- Address of secondary stack. In the fixed secondary stack case, this
1057 -- value is not modified, causing a warning, hence the bracketing with
1058 -- Warnings (Off/On). But why is so much *more* bracketed???
1060 SEH_Table : aliased SSE.Storage_Array (1 .. 8);
1061 -- Structured Exception Registration table (2 words)
1063 procedure Install_SEH_Handler (Addr : System.Address);
1064 pragma Import (C, Install_SEH_Handler, "__gnat_install_SEH_handler");
1065 -- Install the SEH (Structured Exception Handling) handler
1067 Cause : Cause_Of_Termination := Normal;
1068 -- Indicates the reason why this task terminates. Normal corresponds to
1069 -- a task terminating due to completing the last statement of its body,
1070 -- or as a result of waiting on a terminate alternative. If the task
1071 -- terminates because it is being aborted then Cause will be set
1072 -- to Abnormal. If the task terminates because of an exception
1073 -- raised by the execution of its task body, then Cause is set
1074 -- to Unhandled_Exception.
1076 EO : Exception_Occurrence;
1077 -- If the task terminates because of an exception raised by the
1078 -- execution of its task body, then EO will contain the associated
1079 -- exception occurrence. Otherwise, it will contain Null_Occurrence.
1081 TH : Termination_Handler := null;
1082 -- Pointer to the protected procedure to be executed upon task
1083 -- termination.
1085 procedure Search_Fall_Back_Handler (ID : Task_Id);
1086 -- Procedure that searches recursively a fall-back handler through the
1087 -- master relationship. If the handler is found, its pointer is stored
1088 -- in TH. It stops when the handler is found or when the ID is null.
1090 ------------------------------
1091 -- Search_Fall_Back_Handler --
1092 ------------------------------
1094 procedure Search_Fall_Back_Handler (ID : Task_Id) is
1095 begin
1096 -- A null Task_Id indicates that we have reached the root of the
1097 -- task hierarchy and no handler has been found.
1099 if ID = null then
1100 return;
1102 -- If there is a fall back handler, store its pointer for later
1103 -- execution.
1105 elsif ID.Common.Fall_Back_Handler /= null then
1106 TH := ID.Common.Fall_Back_Handler;
1108 -- Otherwise look for a fall back handler in the parent
1110 else
1111 Search_Fall_Back_Handler (ID.Common.Parent);
1112 end if;
1113 end Search_Fall_Back_Handler;
1115 -- Start of processing for Task_Wrapper
1117 begin
1118 pragma Assert (Self_ID.Deferral_Level = 1);
1120 -- Assume a size of the stack taken at this stage
1122 if not Parameters.Sec_Stack_Dynamic then
1123 Self_ID.Common.Compiler_Data.Sec_Stack_Addr :=
1124 Secondary_Stack'Address;
1125 SST.SS_Init (Secondary_Stack_Address, Integer (Secondary_Stack'Last));
1126 end if;
1128 if Use_Alternate_Stack then
1129 Self_ID.Common.Task_Alternate_Stack := Task_Alternate_Stack'Address;
1130 end if;
1132 -- Set the guard page at the bottom of the stack. The call to unprotect
1133 -- the page is done in Terminate_Task
1135 Stack_Guard (Self_ID, True);
1137 -- Initialize low-level TCB components, that cannot be initialized by
1138 -- the creator. Enter_Task sets Self_ID.LL.Thread.
1140 Enter_Task (Self_ID);
1142 -- Initialize dynamic stack usage
1144 if System.Stack_Usage.Is_Enabled then
1145 declare
1146 Guard_Page_Size : constant := 16 * 1024;
1147 -- Part of the stack used as a guard page. This is an OS dependent
1148 -- value, so we need to use the maximum. This value is only used
1149 -- when the stack address is known, that is currently Windows.
1151 Small_Overflow_Guard : constant := 12 * 1024;
1152 -- Note: this used to be 4K, but was changed to 12K, since
1153 -- smaller values resulted in segmentation faults from dynamic
1154 -- stack analysis.
1156 Big_Overflow_Guard : constant := 64 * 1024 + 8 * 1024;
1157 Small_Stack_Limit : constant := 64 * 1024;
1158 -- ??? These three values are experimental, and seem to work on
1159 -- most platforms. They still need to be analyzed further. They
1160 -- also need documentation, what are they and why does the logic
1161 -- differ depending on whether the stack is large or small???
1163 Pattern_Size : Natural :=
1164 Natural (Self_ID.Common.
1165 Compiler_Data.Pri_Stack_Info.Size);
1166 -- Size of the pattern
1168 Stack_Base : Address;
1169 -- Address of the base of the stack
1171 begin
1172 Stack_Base := Self_ID.Common.Compiler_Data.Pri_Stack_Info.Base;
1174 if Stack_Base = Null_Address then
1176 -- On many platforms, we don't know the real stack base
1177 -- address. Estimate it using an address in the frame.
1179 Stack_Base := Bottom_Of_Stack'Address;
1181 -- Also reduce the size of the stack to take into account the
1182 -- secondary stack array declared in this frame. This is for
1183 -- sure very conservative.
1185 if not Parameters.Sec_Stack_Dynamic then
1186 Pattern_Size :=
1187 Pattern_Size - Natural (Secondary_Stack_Size);
1188 end if;
1190 -- Adjustments for inner frames
1192 Pattern_Size := Pattern_Size -
1193 (if Pattern_Size < Small_Stack_Limit
1194 then Small_Overflow_Guard
1195 else Big_Overflow_Guard);
1196 else
1197 -- Reduce by the size of the final guard page
1199 Pattern_Size := Pattern_Size - Guard_Page_Size;
1200 end if;
1202 STPO.Lock_RTS;
1203 Initialize_Analyzer
1204 (Self_ID.Common.Analyzer,
1205 Self_ID.Common.Task_Image (1 .. Self_ID.Common.Task_Image_Len),
1206 Natural (Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size),
1207 SSE.To_Integer (Stack_Base),
1208 Pattern_Size);
1209 STPO.Unlock_RTS;
1210 Fill_Stack (Self_ID.Common.Analyzer);
1211 end;
1212 end if;
1214 -- We setup the SEH (Structured Exception Handling) handler if supported
1215 -- on the target.
1217 Install_SEH_Handler (SEH_Table'Address);
1219 -- Initialize exception occurrence
1221 Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence);
1223 -- We lock RTS_Lock to wait for activator to finish activating the rest
1224 -- of the chain, so that everyone in the chain comes out in priority
1225 -- order.
1227 -- This also protects the value of
1228 -- Self_ID.Common.Activator.Common.Wait_Count.
1230 Lock_RTS;
1231 Unlock_RTS;
1233 if not System.Restrictions.Abort_Allowed then
1235 -- If Abort is not allowed, reset the deferral level since it will
1236 -- not get changed by the generated code. Keeping a default value
1237 -- of one would prevent some operations (e.g. select or delay) to
1238 -- proceed successfully.
1240 Self_ID.Deferral_Level := 0;
1241 end if;
1243 if Global_Task_Debug_Event_Set then
1244 Debug.Signal_Debug_Event (Debug.Debug_Event_Run, Self_ID);
1245 end if;
1247 begin
1248 -- We are separating the following portion of the code in order to
1249 -- place the exception handlers in a different block. In this way,
1250 -- we do not call Set_Jmpbuf_Address (which needs Self) before we
1251 -- set Self in Enter_Task
1253 -- Call the task body procedure
1255 -- The task body is called with abort still deferred. That
1256 -- eliminates a dangerous window, for which we had to patch-up in
1257 -- Terminate_Task.
1259 -- During the expansion of the task body, we insert an RTS-call
1260 -- to Abort_Undefer, at the first point where abort should be
1261 -- allowed.
1263 Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg);
1264 Initialization.Defer_Abort_Nestable (Self_ID);
1266 exception
1267 -- We can't call Terminate_Task in the exception handlers below,
1268 -- since there may be (e.g. in the case of GCC exception handling)
1269 -- clean ups associated with the exception handler that need to
1270 -- access task specific data.
1272 -- Defer abort so that this task can't be aborted while exiting
1274 when Standard'Abort_Signal =>
1275 Initialization.Defer_Abort_Nestable (Self_ID);
1277 -- Update the cause that motivated the task termination so that
1278 -- the appropriate information is passed to the task termination
1279 -- procedure. Task termination as a result of waiting on a
1280 -- terminate alternative is a normal termination, although it is
1281 -- implemented using the abort mechanisms.
1283 if Self_ID.Terminate_Alternative then
1284 Cause := Normal;
1286 if Global_Task_Debug_Event_Set then
1287 Debug.Signal_Debug_Event
1288 (Debug.Debug_Event_Terminated, Self_ID);
1289 end if;
1290 else
1291 Cause := Abnormal;
1293 if Global_Task_Debug_Event_Set then
1294 Debug.Signal_Debug_Event
1295 (Debug.Debug_Event_Abort_Terminated, Self_ID);
1296 end if;
1297 end if;
1299 when others =>
1300 -- ??? Using an E : others here causes CD2C11A to fail on Tru64
1302 Initialization.Defer_Abort_Nestable (Self_ID);
1304 -- Perform the task specific exception tracing duty. We handle
1305 -- these outputs here and not in the common notification routine
1306 -- because we need access to tasking related data and we don't
1307 -- want to drag dependencies against tasking related units in the
1308 -- the common notification units. Additionally, no trace is ever
1309 -- triggered from the common routine for the Unhandled_Raise case
1310 -- in tasks, since an exception never appears unhandled in this
1311 -- context because of this handler.
1313 if Exception_Trace = Unhandled_Raise then
1314 Trace_Unhandled_Exception_In_Task (Self_ID);
1315 end if;
1317 -- Update the cause that motivated the task termination so that
1318 -- the appropriate information is passed to the task termination
1319 -- procedure, as well as the associated Exception_Occurrence.
1321 Cause := Unhandled_Exception;
1323 Save_Occurrence (EO, SSL.Get_Current_Excep.all.all);
1325 if Global_Task_Debug_Event_Set then
1326 Debug.Signal_Debug_Event
1327 (Debug.Debug_Event_Exception_Terminated, Self_ID);
1328 end if;
1329 end;
1331 -- Look for a task termination handler. This code is for all tasks but
1332 -- the environment task. The task termination code for the environment
1333 -- task is executed by SSL.Task_Termination_Handler.
1335 if Single_Lock then
1336 Lock_RTS;
1337 end if;
1339 Write_Lock (Self_ID);
1341 if Self_ID.Common.Specific_Handler /= null then
1342 TH := Self_ID.Common.Specific_Handler;
1343 else
1344 -- Look for a fall-back handler following the master relationship
1345 -- for the task. As specified in ARM C.7.3 par. 9/2, "the fall-back
1346 -- handler applies only to the dependent tasks of the task". Hence,
1347 -- if the terminating tasks (Self_ID) had a fall-back handler, it
1348 -- would not apply to itself, so we start the search with the parent.
1350 Search_Fall_Back_Handler (Self_ID.Common.Parent);
1351 end if;
1353 Unlock (Self_ID);
1355 if Single_Lock then
1356 Unlock_RTS;
1357 end if;
1359 -- Execute the task termination handler if we found it
1361 if TH /= null then
1362 begin
1363 TH.all (Cause, Self_ID, EO);
1365 exception
1367 -- RM-C.7.3 requires all exceptions raised here to be ignored
1369 when others =>
1370 null;
1371 end;
1372 end if;
1374 if System.Stack_Usage.Is_Enabled then
1375 Compute_Result (Self_ID.Common.Analyzer);
1376 Report_Result (Self_ID.Common.Analyzer);
1377 end if;
1379 Terminate_Task (Self_ID);
1380 end Task_Wrapper;
1382 --------------------
1383 -- Terminate_Task --
1384 --------------------
1386 -- Before we allow the thread to exit, we must clean up. This is a delicate
1387 -- job. We must wake up the task's master, who may immediately try to
1388 -- deallocate the ATCB from the current task WHILE IT IS STILL EXECUTING.
1390 -- To avoid this, the parent task must be blocked up to the latest
1391 -- statement executed. The trouble is that we have another step that we
1392 -- also want to postpone to the very end, i.e., calling SSL.Destroy_TSD.
1393 -- We have to postpone that until the end because compiler-generated code
1394 -- is likely to try to access that data at just about any point.
1396 -- We can't call Destroy_TSD while we are holding any other locks, because
1397 -- it locks Global_Task_Lock, and our deadlock prevention rules require
1398 -- that to be the outermost lock. Our first "solution" was to just lock
1399 -- Global_Task_Lock in addition to the other locks, and force the parent to
1400 -- also lock this lock between its wakeup and its freeing of the ATCB. See
1401 -- Complete_Task for the parent-side of the code that has the matching
1402 -- calls to Task_Lock and Task_Unlock. That was not really a solution,
1403 -- since the operation Task_Unlock continued to access the ATCB after
1404 -- unlocking, after which the parent was observed to race ahead, deallocate
1405 -- the ATCB, and then reallocate it to another task. The call to
1406 -- Undefer_Abort in Task_Unlock by the "terminated" task was overwriting
1407 -- the data of the new task that reused the ATCB. To solve this problem, we
1408 -- introduced the new operation Final_Task_Unlock.
1410 procedure Terminate_Task (Self_ID : Task_Id) is
1411 Environment_Task : constant Task_Id := STPO.Environment_Task;
1412 Master_of_Task : Integer;
1413 Deallocate : Boolean;
1415 begin
1416 Debug.Task_Termination_Hook;
1418 if Runtime_Traces then
1419 Send_Trace_Info (T_Terminate);
1420 end if;
1422 -- Since GCC cannot allocate stack chunks efficiently without reordering
1423 -- some of the allocations, we have to handle this unexpected situation
1424 -- here. Normally we never have to call Vulnerable_Complete_Task here.
1426 if Self_ID.Common.Activator /= null then
1427 Vulnerable_Complete_Task (Self_ID);
1428 end if;
1430 Initialization.Task_Lock (Self_ID);
1432 if Single_Lock then
1433 Lock_RTS;
1434 end if;
1436 Master_of_Task := Self_ID.Master_of_Task;
1438 -- Check if the current task is an independent task If so, decrement
1439 -- the Independent_Task_Count value.
1441 if Master_of_Task = Independent_Task_Level then
1442 if Single_Lock then
1443 Utilities.Independent_Task_Count :=
1444 Utilities.Independent_Task_Count - 1;
1446 else
1447 Write_Lock (Environment_Task);
1448 Utilities.Independent_Task_Count :=
1449 Utilities.Independent_Task_Count - 1;
1450 Unlock (Environment_Task);
1451 end if;
1452 end if;
1454 -- Unprotect the guard page if needed
1456 Stack_Guard (Self_ID, False);
1458 Utilities.Make_Passive (Self_ID, Task_Completed => True);
1459 Deallocate := Self_ID.Free_On_Termination;
1461 if Single_Lock then
1462 Unlock_RTS;
1463 end if;
1465 pragma Assert (Check_Exit (Self_ID));
1467 SSL.Destroy_TSD (Self_ID.Common.Compiler_Data);
1468 Initialization.Final_Task_Unlock (Self_ID);
1470 -- WARNING: past this point, this thread must assume that the ATCB has
1471 -- been deallocated, and can't access it anymore (which is why we have
1472 -- saved the Free_On_Termination flag in a temporary variable).
1474 if Deallocate then
1475 Free_Task (Self_ID);
1476 end if;
1478 if Master_of_Task > 0 then
1479 STPO.Exit_Task;
1480 end if;
1481 end Terminate_Task;
1483 ----------------
1484 -- Terminated --
1485 ----------------
1487 function Terminated (T : Task_Id) return Boolean is
1488 Self_ID : constant Task_Id := STPO.Self;
1489 Result : Boolean;
1491 begin
1492 Initialization.Defer_Abort_Nestable (Self_ID);
1494 if Single_Lock then
1495 Lock_RTS;
1496 end if;
1498 Write_Lock (T);
1499 Result := T.Common.State = Terminated;
1500 Unlock (T);
1502 if Single_Lock then
1503 Unlock_RTS;
1504 end if;
1506 Initialization.Undefer_Abort_Nestable (Self_ID);
1507 return Result;
1508 end Terminated;
1510 ----------------------------------------
1511 -- Trace_Unhandled_Exception_In_Task --
1512 ----------------------------------------
1514 procedure Trace_Unhandled_Exception_In_Task (Self_Id : Task_Id) is
1515 procedure To_Stderr (S : String);
1516 pragma Import (Ada, To_Stderr, "__gnat_to_stderr");
1518 use System.Soft_Links;
1519 use System.Standard_Library;
1521 function To_Address is new
1522 Ada.Unchecked_Conversion
1523 (Task_Id, System.Task_Primitives.Task_Address);
1525 function Tailored_Exception_Information
1526 (E : Exception_Occurrence) return String;
1527 pragma Import
1528 (Ada, Tailored_Exception_Information,
1529 "__gnat_tailored_exception_information");
1531 Excep : constant Exception_Occurrence_Access :=
1532 SSL.Get_Current_Excep.all;
1534 begin
1535 -- This procedure is called by the task outermost handler in
1536 -- Task_Wrapper below, so only once the task stack has been fully
1537 -- unwound. The common notification routine has been called at the
1538 -- raise point already.
1540 -- Lock to prevent unsynchronized output
1542 Initialization.Task_Lock (Self_Id);
1543 To_Stderr ("task ");
1545 if Self_Id.Common.Task_Image_Len /= 0 then
1546 To_Stderr
1547 (Self_Id.Common.Task_Image (1 .. Self_Id.Common.Task_Image_Len));
1548 To_Stderr ("_");
1549 end if;
1551 To_Stderr (System.Address_Image (To_Address (Self_Id)));
1552 To_Stderr (" terminated by unhandled exception");
1553 To_Stderr ((1 => ASCII.LF));
1554 To_Stderr (Tailored_Exception_Information (Excep.all));
1555 Initialization.Task_Unlock (Self_Id);
1556 end Trace_Unhandled_Exception_In_Task;
1558 ------------------------------------
1559 -- Vulnerable_Complete_Activation --
1560 ------------------------------------
1562 -- As in several other places, the locks of the activator and activated
1563 -- task are both locked here. This follows our deadlock prevention lock
1564 -- ordering policy, since the activated task must be created after the
1565 -- activator.
1567 procedure Vulnerable_Complete_Activation (Self_ID : Task_Id) is
1568 Activator : constant Task_Id := Self_ID.Common.Activator;
1570 begin
1571 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Activation", 'C'));
1573 Write_Lock (Activator);
1574 Write_Lock (Self_ID);
1576 pragma Assert (Self_ID.Common.Activator /= null);
1578 -- Remove dangling reference to Activator, since a task may outlive its
1579 -- activator.
1581 Self_ID.Common.Activator := null;
1583 -- Wake up the activator, if it is waiting for a chain of tasks to
1584 -- activate, and we are the last in the chain to complete activation.
1586 if Activator.Common.State = Activator_Sleep then
1587 Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1;
1589 if Activator.Common.Wait_Count = 0 then
1590 Wakeup (Activator, Activator_Sleep);
1591 end if;
1592 end if;
1594 -- The activator raises a Tasking_Error if any task it is activating
1595 -- is completed before the activation is done. However, if the reason
1596 -- for the task completion is an abort, we do not raise an exception.
1597 -- See RM 9.2(5).
1599 if not Self_ID.Callable and then Self_ID.Pending_ATC_Level /= 0 then
1600 Activator.Common.Activation_Failed := True;
1601 end if;
1603 Unlock (Self_ID);
1604 Unlock (Activator);
1606 -- After the activation, active priority should be the same as base
1607 -- priority. We must unlock the Activator first, though, since it
1608 -- should not wait if we have lower priority.
1610 if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then
1611 Write_Lock (Self_ID);
1612 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
1613 Unlock (Self_ID);
1614 end if;
1615 end Vulnerable_Complete_Activation;
1617 --------------------------------
1618 -- Vulnerable_Complete_Master --
1619 --------------------------------
1621 procedure Vulnerable_Complete_Master (Self_ID : Task_Id) is
1622 C : Task_Id;
1623 P : Task_Id;
1624 CM : constant Master_Level := Self_ID.Master_Within;
1625 T : aliased Task_Id;
1627 To_Be_Freed : Task_Id;
1628 -- This is a list of ATCBs to be freed, after we have released all RTS
1629 -- locks. This is necessary because of the locking order rules, since
1630 -- the storage manager uses Global_Task_Lock.
1632 pragma Warnings (Off);
1633 function Check_Unactivated_Tasks return Boolean;
1634 pragma Warnings (On);
1635 -- Temporary error-checking code below. This is part of the checks
1636 -- added in the new run time. Call it only inside a pragma Assert.
1638 -----------------------------
1639 -- Check_Unactivated_Tasks --
1640 -----------------------------
1642 function Check_Unactivated_Tasks return Boolean is
1643 begin
1644 if not Single_Lock then
1645 Lock_RTS;
1646 end if;
1648 Write_Lock (Self_ID);
1650 C := All_Tasks_List;
1651 while C /= null loop
1652 if C.Common.Activator = Self_ID and then C.Master_of_Task = CM then
1653 return False;
1654 end if;
1656 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1657 Write_Lock (C);
1659 if C.Common.State = Unactivated then
1660 return False;
1661 end if;
1663 Unlock (C);
1664 end if;
1666 C := C.Common.All_Tasks_Link;
1667 end loop;
1669 Unlock (Self_ID);
1671 if not Single_Lock then
1672 Unlock_RTS;
1673 end if;
1675 return True;
1676 end Check_Unactivated_Tasks;
1678 -- Start of processing for Vulnerable_Complete_Master
1680 begin
1681 pragma Debug
1682 (Debug.Trace (Self_ID, "V_Complete_Master(" & CM'Img & ")", 'C'));
1684 pragma Assert (Self_ID.Common.Wait_Count = 0);
1685 pragma Assert
1686 (Self_ID.Deferral_Level > 0
1687 or else not System.Restrictions.Abort_Allowed);
1689 -- Count how many active dependent tasks this master currently has, and
1690 -- record this in Wait_Count.
1692 -- This count should start at zero, since it is initialized to zero for
1693 -- new tasks, and the task should not exit the sleep-loops that use this
1694 -- count until the count reaches zero.
1696 -- While we're counting, if we run across any unactivated tasks that
1697 -- belong to this master, we summarily terminate them as required by
1698 -- RM-9.2(6).
1700 Lock_RTS;
1701 Write_Lock (Self_ID);
1703 C := All_Tasks_List;
1704 while C /= null loop
1706 -- Terminate unactivated (never-to-be activated) tasks
1708 if C.Common.Activator = Self_ID and then C.Master_of_Task = CM then
1710 -- Usually, C.Common.Activator = Self_ID implies C.Master_of_Task
1711 -- = CM. The only case where C is pending activation by this
1712 -- task, but the master of C is not CM is in Ada 2005, when C is
1713 -- part of a return object of a build-in-place function.
1715 pragma Assert (C.Common.State = Unactivated);
1717 Write_Lock (C);
1718 C.Common.Activator := null;
1719 C.Common.State := Terminated;
1720 C.Callable := False;
1721 Utilities.Cancel_Queued_Entry_Calls (C);
1722 Unlock (C);
1723 end if;
1725 -- Count it if directly dependent on this master
1727 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1728 Write_Lock (C);
1730 if C.Awake_Count /= 0 then
1731 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
1732 end if;
1734 Unlock (C);
1735 end if;
1737 C := C.Common.All_Tasks_Link;
1738 end loop;
1740 Self_ID.Common.State := Master_Completion_Sleep;
1741 Unlock (Self_ID);
1743 if not Single_Lock then
1744 Unlock_RTS;
1745 end if;
1747 -- Wait until dependent tasks are all terminated or ready to terminate.
1748 -- While waiting, the task may be awakened if the task's priority needs
1749 -- changing, or this master is aborted. In the latter case, we abort the
1750 -- dependents, and resume waiting until Wait_Count goes to zero.
1752 Write_Lock (Self_ID);
1754 loop
1755 exit when Self_ID.Common.Wait_Count = 0;
1757 -- Here is a difference as compared to Complete_Master
1759 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
1760 and then not Self_ID.Dependents_Aborted
1761 then
1762 if Single_Lock then
1763 Abort_Dependents (Self_ID);
1764 else
1765 Unlock (Self_ID);
1766 Lock_RTS;
1767 Abort_Dependents (Self_ID);
1768 Unlock_RTS;
1769 Write_Lock (Self_ID);
1770 end if;
1771 else
1772 pragma Debug
1773 (Debug.Trace (Self_ID, "master_completion_sleep", 'C'));
1774 Sleep (Self_ID, Master_Completion_Sleep);
1775 end if;
1776 end loop;
1778 Self_ID.Common.State := Runnable;
1779 Unlock (Self_ID);
1781 -- Dependents are all terminated or on terminate alternatives. Now,
1782 -- force those on terminate alternatives to terminate, by aborting them.
1784 pragma Assert (Check_Unactivated_Tasks);
1786 if Self_ID.Alive_Count > 1 then
1787 -- ???
1788 -- Consider finding a way to skip the following extra steps if there
1789 -- are no dependents with terminate alternatives. This could be done
1790 -- by adding another count to the ATCB, similar to Awake_Count, but
1791 -- keeping track of tasks that are on terminate alternatives.
1793 pragma Assert (Self_ID.Common.Wait_Count = 0);
1795 -- Force any remaining dependents to terminate by aborting them
1797 if not Single_Lock then
1798 Lock_RTS;
1799 end if;
1801 Abort_Dependents (Self_ID);
1803 -- Above, when we "abort" the dependents we are simply using this
1804 -- operation for convenience. We are not required to support the full
1805 -- abort-statement semantics; in particular, we are not required to
1806 -- immediately cancel any queued or in-service entry calls. That is
1807 -- good, because if we tried to cancel a call we would need to lock
1808 -- the caller, in order to wake the caller up. Our anti-deadlock
1809 -- rules prevent us from doing that without releasing the locks on C
1810 -- and Self_ID. Releasing and retaking those locks would be wasteful
1811 -- at best, and should not be considered further without more
1812 -- detailed analysis of potential concurrent accesses to the ATCBs
1813 -- of C and Self_ID.
1815 -- Count how many "alive" dependent tasks this master currently has,
1816 -- and record this in Wait_Count. This count should start at zero,
1817 -- since it is initialized to zero for new tasks, and the task should
1818 -- not exit the sleep-loops that use this count until the count
1819 -- reaches zero.
1821 pragma Assert (Self_ID.Common.Wait_Count = 0);
1823 Write_Lock (Self_ID);
1825 C := All_Tasks_List;
1826 while C /= null loop
1827 if C.Common.Parent = Self_ID and then C.Master_of_Task = CM then
1828 Write_Lock (C);
1830 pragma Assert (C.Awake_Count = 0);
1832 if C.Alive_Count > 0 then
1833 pragma Assert (C.Terminate_Alternative);
1834 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
1835 end if;
1837 Unlock (C);
1838 end if;
1840 C := C.Common.All_Tasks_Link;
1841 end loop;
1843 Self_ID.Common.State := Master_Phase_2_Sleep;
1844 Unlock (Self_ID);
1846 if not Single_Lock then
1847 Unlock_RTS;
1848 end if;
1850 -- Wait for all counted tasks to finish terminating themselves
1852 Write_Lock (Self_ID);
1854 loop
1855 exit when Self_ID.Common.Wait_Count = 0;
1856 Sleep (Self_ID, Master_Phase_2_Sleep);
1857 end loop;
1859 Self_ID.Common.State := Runnable;
1860 Unlock (Self_ID);
1861 end if;
1863 -- We don't wake up for abort here. We are already terminating just as
1864 -- fast as we can, so there is no point.
1866 -- Remove terminated tasks from the list of Self_ID's dependents, but
1867 -- don't free their ATCBs yet, because of lock order restrictions, which
1868 -- don't allow us to call "free" or "malloc" while holding any other
1869 -- locks. Instead, we put those ATCBs to be freed onto a temporary list,
1870 -- called To_Be_Freed.
1872 if not Single_Lock then
1873 Lock_RTS;
1874 end if;
1876 C := All_Tasks_List;
1877 P := null;
1878 while C /= null loop
1880 -- If Free_On_Termination is set, do nothing here, and let the
1881 -- task free itself if not already done, otherwise we risk a race
1882 -- condition where Vulnerable_Free_Task is called in the loop below,
1883 -- while the task calls Free_Task itself, in Terminate_Task.
1885 if C.Common.Parent = Self_ID
1886 and then C.Master_of_Task >= CM
1887 and then not C.Free_On_Termination
1888 then
1889 if P /= null then
1890 P.Common.All_Tasks_Link := C.Common.All_Tasks_Link;
1891 else
1892 All_Tasks_List := C.Common.All_Tasks_Link;
1893 end if;
1895 T := C.Common.All_Tasks_Link;
1896 C.Common.All_Tasks_Link := To_Be_Freed;
1897 To_Be_Freed := C;
1898 C := T;
1900 else
1901 P := C;
1902 C := C.Common.All_Tasks_Link;
1903 end if;
1904 end loop;
1906 Unlock_RTS;
1908 -- Free all the ATCBs on the list To_Be_Freed
1910 -- The ATCBs in the list are no longer in All_Tasks_List, and after
1911 -- any interrupt entries are detached from them they should no longer
1912 -- be referenced.
1914 -- Global_Task_Lock (Task_Lock/Unlock) is locked in the loop below to
1915 -- avoid a race between a terminating task and its parent. The parent
1916 -- might try to deallocate the ACTB out from underneath the exiting
1917 -- task. Note that Free will also lock Global_Task_Lock, but that is
1918 -- OK, since this is the *one* lock for which we have a mechanism to
1919 -- support nested locking. See Task_Wrapper and its finalizer for more
1920 -- explanation.
1922 -- ???
1923 -- The check "T.Common.Parent /= null ..." below is to prevent dangling
1924 -- references to terminated library-level tasks, which could otherwise
1925 -- occur during finalization of library-level objects. A better solution
1926 -- might be to hook task objects into the finalization chain and
1927 -- deallocate the ATCB when the task object is deallocated. However,
1928 -- this change is not likely to gain anything significant, since all
1929 -- this storage should be recovered en-masse when the process exits.
1931 while To_Be_Freed /= null loop
1932 T := To_Be_Freed;
1933 To_Be_Freed := T.Common.All_Tasks_Link;
1935 -- ??? On SGI there is currently no Interrupt_Manager, that's why we
1936 -- need to check if the Interrupt_Manager_ID is null.
1938 if T.Interrupt_Entry and then Interrupt_Manager_ID /= null then
1939 declare
1940 Detach_Interrupt_Entries_Index : constant Task_Entry_Index := 1;
1941 -- Corresponds to the entry index of System.Interrupts.
1942 -- Interrupt_Manager.Detach_Interrupt_Entries. Be sure
1943 -- to update this value when changing Interrupt_Manager specs.
1945 type Param_Type is access all Task_Id;
1947 Param : aliased Param_Type := T'Access;
1949 begin
1950 System.Tasking.Rendezvous.Call_Simple
1951 (Interrupt_Manager_ID, Detach_Interrupt_Entries_Index,
1952 Param'Address);
1953 end;
1954 end if;
1956 if (T.Common.Parent /= null
1957 and then T.Common.Parent.Common.Parent /= null)
1958 or else T.Master_of_Task > Library_Task_Level
1959 then
1960 Initialization.Task_Lock (Self_ID);
1962 -- If Sec_Stack_Addr is not null, it means that Destroy_TSD
1963 -- has not been called yet (case of an unactivated task).
1965 if T.Common.Compiler_Data.Sec_Stack_Addr /= Null_Address then
1966 SSL.Destroy_TSD (T.Common.Compiler_Data);
1967 end if;
1969 Vulnerable_Free_Task (T);
1970 Initialization.Task_Unlock (Self_ID);
1971 end if;
1972 end loop;
1974 -- It might seem nice to let the terminated task deallocate its own
1975 -- ATCB. That would not cover the case of unactivated tasks. It also
1976 -- would force us to keep the underlying thread around past termination,
1977 -- since references to the ATCB are possible past termination.
1979 -- Currently, we get rid of the thread as soon as the task terminates,
1980 -- and let the parent recover the ATCB later.
1982 -- Some day, if we want to recover the ATCB earlier, at task
1983 -- termination, we could consider using "fat task IDs", that include the
1984 -- serial number with the ATCB pointer, to catch references to tasks
1985 -- that no longer have ATCBs. It is not clear how much this would gain,
1986 -- since the user-level task object would still be occupying storage.
1988 -- Make next master level up active. We don't need to lock the ATCB,
1989 -- since the value is only updated by each task for itself.
1991 Self_ID.Master_Within := CM - 1;
1992 end Vulnerable_Complete_Master;
1994 ------------------------------
1995 -- Vulnerable_Complete_Task --
1996 ------------------------------
1998 -- Complete the calling task
2000 -- This procedure must be called with abort deferred. It should only be
2001 -- called by Complete_Task and Finalize_Global_Tasks (for the environment
2002 -- task).
2004 -- The effect is similar to that of Complete_Master. Differences include
2005 -- the closing of entries here, and computation of the number of active
2006 -- dependent tasks in Complete_Master.
2008 -- We don't lock Self_ID before the call to Vulnerable_Complete_Activation,
2009 -- because that does its own locking, and because we do not need the lock
2010 -- to test Self_ID.Common.Activator. That value should only be read and
2011 -- modified by Self.
2013 procedure Vulnerable_Complete_Task (Self_ID : Task_Id) is
2014 begin
2015 pragma Assert
2016 (Self_ID.Deferral_Level > 0
2017 or else not System.Restrictions.Abort_Allowed);
2018 pragma Assert (Self_ID = Self);
2019 pragma Assert (Self_ID.Master_Within = Self_ID.Master_of_Task + 1
2020 or else
2021 Self_ID.Master_Within = Self_ID.Master_of_Task + 2);
2022 pragma Assert (Self_ID.Common.Wait_Count = 0);
2023 pragma Assert (Self_ID.Open_Accepts = null);
2024 pragma Assert (Self_ID.ATC_Nesting_Level = 1);
2026 pragma Debug (Debug.Trace (Self_ID, "V_Complete_Task", 'C'));
2028 if Single_Lock then
2029 Lock_RTS;
2030 end if;
2032 Write_Lock (Self_ID);
2033 Self_ID.Callable := False;
2035 -- In theory, Self should have no pending entry calls left on its
2036 -- call-stack. Each async. select statement should clean its own call,
2037 -- and blocking entry calls should defer abort until the calls are
2038 -- cancelled, then clean up.
2040 Utilities.Cancel_Queued_Entry_Calls (Self_ID);
2041 Unlock (Self_ID);
2043 if Self_ID.Common.Activator /= null then
2044 Vulnerable_Complete_Activation (Self_ID);
2045 end if;
2047 if Single_Lock then
2048 Unlock_RTS;
2049 end if;
2051 -- If Self_ID.Master_Within = Self_ID.Master_of_Task + 2 we may have
2052 -- dependent tasks for which we need to wait. Otherwise we just exit.
2054 if Self_ID.Master_Within = Self_ID.Master_of_Task + 2 then
2055 Vulnerable_Complete_Master (Self_ID);
2056 end if;
2057 end Vulnerable_Complete_Task;
2059 --------------------------
2060 -- Vulnerable_Free_Task --
2061 --------------------------
2063 -- Recover all runtime system storage associated with the task T. This
2064 -- should only be called after T has terminated and will no longer be
2065 -- referenced.
2067 -- For tasks created by an allocator that fails, due to an exception, it
2068 -- is called from Expunge_Unactivated_Tasks.
2070 -- For tasks created by elaboration of task object declarations it is
2071 -- called from the finalization code of the Task_Wrapper procedure.
2073 procedure Vulnerable_Free_Task (T : Task_Id) is
2074 begin
2075 pragma Debug (Debug.Trace (Self, "Vulnerable_Free_Task", 'C', T));
2077 if Single_Lock then
2078 Lock_RTS;
2079 end if;
2081 Write_Lock (T);
2082 Initialization.Finalize_Attributes_Link.all (T);
2083 Unlock (T);
2085 if Single_Lock then
2086 Unlock_RTS;
2087 end if;
2089 System.Task_Primitives.Operations.Finalize_TCB (T);
2090 end Vulnerable_Free_Task;
2092 -- Package elaboration code
2094 begin
2095 -- Establish the Adafinal softlink
2097 -- This is not done inside the central RTS initialization routine
2098 -- to avoid with'ing this package from System.Tasking.Initialization.
2100 SSL.Adafinal := Finalize_Global_Tasks'Access;
2102 -- Establish soft links for subprograms that manipulate master_id's.
2103 -- This cannot be done when the RTS is initialized, because of various
2104 -- elaboration constraints.
2106 SSL.Current_Master := Stages.Current_Master'Access;
2107 SSL.Enter_Master := Stages.Enter_Master'Access;
2108 SSL.Complete_Master := Stages.Complete_Master'Access;
2109 end System.Tasking.Stages;