Daily bump.
[official-gcc.git] / gcc / ada / s-tarest.adb
bloba29aed78a41c27db5e53dcdc35ef0467ac04ff55
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . R E S T R I C T E D . S T A G E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2009, 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 Style_Checks (All_Checks);
33 -- Turn off subprogram alpha order check, since we group soft link
34 -- bodies and also separate off subprograms for restricted GNARLI.
36 -- This is a simplified version of the System.Tasking.Stages package,
37 -- intended to be used in a restricted run time.
39 -- This package represents the high level tasking interface used by the
40 -- compiler to expand Ada 95 tasking constructs into simpler run time calls.
42 pragma Polling (Off);
43 -- Turn off polling, we do not want ATC polling to take place during
44 -- tasking operations. It causes infinite loops and other problems.
46 with Ada.Exceptions;
48 with System.Task_Primitives.Operations;
49 with System.Soft_Links.Tasking;
50 with System.Secondary_Stack;
51 with System.Storage_Elements;
53 with System.Soft_Links;
54 -- Used for the non-tasking routines (*_NT) that refer to global data. They
55 -- are needed here before the tasking run time has been elaborated. used for
56 -- Create_TSD This package also provides initialization routines for task
57 -- specific data. The GNARL must call these to be sure that all non-tasking
58 -- Ada constructs will work.
60 package body System.Tasking.Restricted.Stages is
62 package STPO renames System.Task_Primitives.Operations;
63 package SSL renames System.Soft_Links;
64 package SSE renames System.Storage_Elements;
65 package SST renames System.Secondary_Stack;
67 use Ada.Exceptions;
69 use Parameters;
70 use Task_Primitives.Operations;
71 use Task_Info;
73 Global_Task_Lock : aliased System.Task_Primitives.RTS_Lock;
74 -- This is a global lock; it is used to execute in mutual exclusion
75 -- from all other tasks. It is only used by Task_Lock and Task_Unlock.
77 -----------------------------------------------------------------
78 -- Tasking versions of services needed by non-tasking programs --
79 -----------------------------------------------------------------
81 function Get_Current_Excep return SSL.EOA;
82 -- Task-safe version of SSL.Get_Current_Excep
84 procedure Task_Lock;
85 -- Locks out other tasks. Preceding a section of code by Task_Lock and
86 -- following it by Task_Unlock creates a critical region. This is used
87 -- for ensuring that a region of non-tasking code (such as code used to
88 -- allocate memory) is tasking safe. Note that it is valid for calls to
89 -- Task_Lock/Task_Unlock to be nested, and this must work properly, i.e.
90 -- only the corresponding outer level Task_Unlock will actually unlock.
92 procedure Task_Unlock;
93 -- Releases lock previously set by call to Task_Lock. In the nested case,
94 -- all nested locks must be released before other tasks competing for the
95 -- tasking lock are released.
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Task_Wrapper (Self_ID : Task_Id);
102 -- This is the procedure that is called by the GNULL from the
103 -- new context when a task is created. It waits for activation
104 -- and then calls the task body procedure. When the task body
105 -- procedure completes, it terminates the task.
107 procedure Terminate_Task (Self_ID : Task_Id);
108 -- Terminate the calling task.
109 -- This should only be called by the Task_Wrapper procedure.
111 procedure Init_RTS;
112 -- This procedure performs the initialization of the GNARL.
113 -- It consists of initializing the environment task, global locks, and
114 -- installing tasking versions of certain operations used by the compiler.
115 -- Init_RTS is called during elaboration.
117 -----------------------
118 -- Get_Current_Excep --
119 -----------------------
121 function Get_Current_Excep return SSL.EOA is
122 begin
123 return STPO.Self.Common.Compiler_Data.Current_Excep'Access;
124 end Get_Current_Excep;
126 ---------------
127 -- Task_Lock --
128 ---------------
130 procedure Task_Lock is
131 Self_ID : constant Task_Id := STPO.Self;
133 begin
134 Self_ID.Common.Global_Task_Lock_Nesting :=
135 Self_ID.Common.Global_Task_Lock_Nesting + 1;
137 if Self_ID.Common.Global_Task_Lock_Nesting = 1 then
138 STPO.Write_Lock (Global_Task_Lock'Access, Global_Lock => True);
139 end if;
140 end Task_Lock;
142 -----------------
143 -- Task_Unlock --
144 -----------------
146 procedure Task_Unlock is
147 Self_ID : constant Task_Id := STPO.Self;
149 begin
150 pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting > 0);
151 Self_ID.Common.Global_Task_Lock_Nesting :=
152 Self_ID.Common.Global_Task_Lock_Nesting - 1;
154 if Self_ID.Common.Global_Task_Lock_Nesting = 0 then
155 STPO.Unlock (Global_Task_Lock'Access, Global_Lock => True);
156 end if;
157 end Task_Unlock;
159 ------------------
160 -- Task_Wrapper --
161 ------------------
163 -- The task wrapper is a procedure that is called first for each task
164 -- task body, and which in turn calls the compiler-generated task body
165 -- procedure. The wrapper's main job is to do initialization for the task.
167 -- The variable ID in the task wrapper is used to implement the Self
168 -- function on targets where there is a fast way to find the stack base
169 -- of the current thread, since it should be at a fixed offset from the
170 -- stack base.
172 procedure Task_Wrapper (Self_ID : Task_Id) is
173 ID : Task_Id := Self_ID;
174 pragma Volatile (ID);
175 pragma Warnings (Off, ID);
176 -- Variable used on some targets to implement a fast self. We turn off
177 -- warnings because a stand alone volatile constant has to be imported,
178 -- so we don't want warnings about ID not being referenced, and volatile
179 -- having no effect.
181 -- DO NOT delete ID. As noted, it is needed on some targets.
183 use type SSE.Storage_Offset;
185 Secondary_Stack : aliased SSE.Storage_Array
186 (1 .. Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size *
187 SSE.Storage_Offset (Parameters.Sec_Stack_Ratio) / 100);
189 pragma Warnings (Off);
190 Secondary_Stack_Address : System.Address := Secondary_Stack'Address;
191 pragma Warnings (On);
192 -- Address of secondary stack. In the fixed secondary stack case, this
193 -- value is not modified, causing a warning, hence the bracketing with
194 -- Warnings (Off/On).
196 Cause : Cause_Of_Termination := Normal;
197 -- Indicates the reason why this task terminates. Normal corresponds to
198 -- a task terminating due to completing the last statement of its body.
199 -- If the task terminates because of an exception raised by the
200 -- execution of its task body, then Cause is set to Unhandled_Exception.
201 -- Aborts are not allowed in the restricted profile to which this file
202 -- belongs.
204 EO : Exception_Occurrence;
205 -- If the task terminates because of an exception raised by the
206 -- execution of its task body, then EO will contain the associated
207 -- exception occurrence. Otherwise, it will contain Null_Occurrence.
209 begin
210 if not Parameters.Sec_Stack_Dynamic then
211 Self_ID.Common.Compiler_Data.Sec_Stack_Addr :=
212 Secondary_Stack'Address;
213 SST.SS_Init (Secondary_Stack_Address, Integer (Secondary_Stack'Last));
214 end if;
216 -- Initialize low-level TCB components, that
217 -- cannot be initialized by the creator.
219 Enter_Task (Self_ID);
221 -- Call the task body procedure
223 begin
224 -- We are separating the following portion of the code in order to
225 -- place the exception handlers in a different block. In this way we
226 -- do not call Set_Jmpbuf_Address (which needs Self) before we set
227 -- Self in Enter_Task.
229 -- Note that in the case of Ravenscar HI-E where there are no
230 -- exception handlers, the exception handler is suppressed.
232 -- Call the task body procedure
234 Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg);
236 -- Normal task termination
238 Cause := Normal;
239 Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence);
241 exception
242 when E : others =>
244 -- Task terminating because of an unhandled exception
246 Cause := Unhandled_Exception;
247 Save_Occurrence (EO, E);
248 end;
250 -- Look for a fall-back handler. It can be either in the task itself
251 -- or in the environment task. Note that this code is always executed
252 -- by a task whose master is the environment task. The task termination
253 -- code for the environment task is executed by
254 -- SSL.Task_Termination_Handler.
256 -- This package is part of the restricted run time which supports
257 -- neither task hierarchies (No_Task_Hierarchy) nor specific task
258 -- termination handlers (No_Specific_Termination_Handlers).
260 -- There is no need for explicit protection against race conditions
261 -- for Self_ID.Common.Fall_Back_Handler because this procedure can
262 -- only be executed by Self, and the Fall_Back_Handler can only be
263 -- modified by Self.
265 if Self_ID.Common.Fall_Back_Handler /= null then
266 Self_ID.Common.Fall_Back_Handler (Cause, Self_ID, EO);
267 else
268 declare
269 TH : Termination_Handler := null;
271 begin
272 if Single_Lock then
273 Lock_RTS;
274 end if;
276 Write_Lock (Self_ID.Common.Parent);
278 TH := Self_ID.Common.Parent.Common.Fall_Back_Handler;
280 Unlock (Self_ID.Common.Parent);
282 if Single_Lock then
283 Unlock_RTS;
284 end if;
286 -- Execute the task termination handler if we found it
288 if TH /= null then
289 TH.all (Cause, Self_ID, EO);
290 end if;
291 end;
292 end if;
294 Terminate_Task (Self_ID);
295 end Task_Wrapper;
297 -----------------------
298 -- Restricted GNARLI --
299 -----------------------
301 -------------------------------
302 -- Activate_Restricted_Tasks --
303 -------------------------------
305 -- Note that locks of activator and activated task are both locked here.
306 -- This is necessary because C.State and Self.Wait_Count have to be
307 -- synchronized. This is safe from deadlock because the activator is always
308 -- created before the activated task. That satisfies our
309 -- in-order-of-creation ATCB locking policy.
311 procedure Activate_Restricted_Tasks
312 (Chain_Access : Activation_Chain_Access)
314 Self_ID : constant Task_Id := STPO.Self;
315 C : Task_Id;
316 Activate_Prio : System.Any_Priority;
317 Success : Boolean;
319 begin
320 pragma Assert (Self_ID = Environment_Task);
321 pragma Assert (Self_ID.Common.Wait_Count = 0);
323 if Single_Lock then
324 Lock_RTS;
325 end if;
327 -- Lock self, to prevent activated tasks from racing ahead before we
328 -- finish activating the chain.
330 Write_Lock (Self_ID);
332 -- Activate all the tasks in the chain. Creation of the thread of
333 -- control was deferred until activation. So create it now.
335 C := Chain_Access.T_ID;
337 while C /= null loop
338 if C.Common.State /= Terminated then
339 pragma Assert (C.Common.State = Unactivated);
341 Write_Lock (C);
343 if C.Common.Base_Priority < Get_Priority (Self_ID) then
344 Activate_Prio := Get_Priority (Self_ID);
345 else
346 Activate_Prio := C.Common.Base_Priority;
347 end if;
349 STPO.Create_Task
350 (C, Task_Wrapper'Address,
351 Parameters.Size_Type
352 (C.Common.Compiler_Data.Pri_Stack_Info.Size),
353 Activate_Prio, Success);
355 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
357 if Success then
358 C.Common.State := Runnable;
359 else
360 raise Program_Error;
361 end if;
363 Unlock (C);
364 end if;
366 C := C.Common.Activation_Link;
367 end loop;
369 Self_ID.Common.State := Activator_Sleep;
371 -- Wait for the activated tasks to complete activation. It is unsafe to
372 -- abort any of these tasks until the count goes to zero.
374 loop
375 exit when Self_ID.Common.Wait_Count = 0;
376 Sleep (Self_ID, Activator_Sleep);
377 end loop;
379 Self_ID.Common.State := Runnable;
380 Unlock (Self_ID);
382 if Single_Lock then
383 Unlock_RTS;
384 end if;
386 -- Remove the tasks from the chain
388 Chain_Access.T_ID := null;
389 end Activate_Restricted_Tasks;
391 ------------------------------------
392 -- Complete_Restricted_Activation --
393 ------------------------------------
395 -- As in several other places, the locks of the activator and activated
396 -- task are both locked here. This follows our deadlock prevention lock
397 -- ordering policy, since the activated task must be created after the
398 -- activator.
400 procedure Complete_Restricted_Activation is
401 Self_ID : constant Task_Id := STPO.Self;
402 Activator : constant Task_Id := Self_ID.Common.Activator;
404 begin
405 if Single_Lock then
406 Lock_RTS;
407 end if;
409 Write_Lock (Activator);
410 Write_Lock (Self_ID);
412 -- Remove dangling reference to Activator, since a task may outlive its
413 -- activator.
415 Self_ID.Common.Activator := null;
417 -- Wake up the activator, if it is waiting for a chain of tasks to
418 -- activate, and we are the last in the chain to complete activation
420 if Activator.Common.State = Activator_Sleep then
421 Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1;
423 if Activator.Common.Wait_Count = 0 then
424 Wakeup (Activator, Activator_Sleep);
425 end if;
426 end if;
428 Unlock (Self_ID);
429 Unlock (Activator);
431 if Single_Lock then
432 Unlock_RTS;
433 end if;
435 -- After the activation, active priority should be the same as base
436 -- priority. We must unlock the Activator first, though, since it should
437 -- not wait if we have lower priority.
439 if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then
440 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
441 end if;
442 end Complete_Restricted_Activation;
444 ------------------------------
445 -- Complete_Restricted_Task --
446 ------------------------------
448 procedure Complete_Restricted_Task is
449 begin
450 STPO.Self.Common.State := Terminated;
451 end Complete_Restricted_Task;
453 ----------------------------
454 -- Create_Restricted_Task --
455 ----------------------------
457 procedure Create_Restricted_Task
458 (Priority : Integer;
459 Stack_Address : System.Address;
460 Size : System.Parameters.Size_Type;
461 Task_Info : System.Task_Info.Task_Info_Type;
462 State : Task_Procedure_Access;
463 Discriminants : System.Address;
464 Elaborated : Access_Boolean;
465 Chain : in out Activation_Chain;
466 Task_Image : String;
467 Created_Task : Task_Id)
469 Self_ID : constant Task_Id := STPO.Self;
470 Base_Priority : System.Any_Priority;
471 Success : Boolean;
472 Len : Integer;
474 begin
475 -- Stack is not preallocated on this target, so that Stack_Address must
476 -- be null.
478 pragma Assert (Stack_Address = Null_Address);
480 if Priority = Unspecified_Priority then
481 Base_Priority := Self_ID.Common.Base_Priority;
482 else
483 Base_Priority := System.Any_Priority (Priority);
484 end if;
486 if Single_Lock then
487 Lock_RTS;
488 end if;
490 Write_Lock (Self_ID);
492 -- With no task hierarchy, the parent of all non-Environment tasks that
493 -- are created must be the Environment task
495 Initialize_ATCB
496 (Self_ID, State, Discriminants, Self_ID, Elaborated, Base_Priority,
497 Task_Info, Size, Created_Task, Success);
499 -- If we do our job right then there should never be any failures, which
500 -- was probably said about the Titanic; so just to be safe, let's retain
501 -- this code for now
503 if not Success then
504 Unlock (Self_ID);
506 if Single_Lock then
507 Unlock_RTS;
508 end if;
510 raise Program_Error;
511 end if;
513 Created_Task.Entry_Calls (1).Self := Created_Task;
515 Len :=
516 Integer'Min (Created_Task.Common.Task_Image'Length, Task_Image'Length);
517 Created_Task.Common.Task_Image_Len := Len;
518 Created_Task.Common.Task_Image (1 .. Len) :=
519 Task_Image (Task_Image'First .. Task_Image'First + Len - 1);
521 Unlock (Self_ID);
523 if Single_Lock then
524 Unlock_RTS;
525 end if;
527 -- Create TSD as early as possible in the creation of a task, since it
528 -- may be used by the operation of Ada code within the task.
530 SSL.Create_TSD (Created_Task.Common.Compiler_Data);
531 Created_Task.Common.Activation_Link := Chain.T_ID;
532 Chain.T_ID := Created_Task;
533 end Create_Restricted_Task;
535 ---------------------------
536 -- Finalize_Global_Tasks --
537 ---------------------------
539 -- This is needed to support the compiler interface; it will only be called
540 -- by the Environment task. Instead, it will cause the Environment to block
541 -- forever, since none of the dependent tasks are expected to terminate
543 procedure Finalize_Global_Tasks is
544 Self_ID : constant Task_Id := STPO.Self;
546 begin
547 pragma Assert (Self_ID = STPO.Environment_Task);
549 if Single_Lock then
550 Lock_RTS;
551 end if;
553 -- Handle normal task termination by the environment task, but only for
554 -- the normal task termination. In the case of Abnormal and
555 -- Unhandled_Exception they must have been handled before, and the task
556 -- termination soft link must have been changed so the task termination
557 -- routine is not executed twice.
559 -- Note that in the "normal" implementation in s-tassta.adb the task
560 -- termination procedure for the environment task should be executed
561 -- after termination of library-level tasks. However, this
562 -- implementation is to be used when the Ravenscar restrictions are in
563 -- effect, and AI-394 says that if there is a fall-back handler set for
564 -- the partition it should be called when the first task (including the
565 -- environment task) attempts to terminate.
567 SSL.Task_Termination_Handler.all (Ada.Exceptions.Null_Occurrence);
569 Write_Lock (Self_ID);
570 Sleep (Self_ID, Master_Completion_Sleep);
571 Unlock (Self_ID);
573 if Single_Lock then
574 Unlock_RTS;
575 end if;
577 -- Should never return from Master Completion Sleep
579 raise Program_Error;
580 end Finalize_Global_Tasks;
582 ---------------------------
583 -- Restricted_Terminated --
584 ---------------------------
586 function Restricted_Terminated (T : Task_Id) return Boolean is
587 begin
588 return T.Common.State = Terminated;
589 end Restricted_Terminated;
591 --------------------
592 -- Terminate_Task --
593 --------------------
595 procedure Terminate_Task (Self_ID : Task_Id) is
596 begin
597 Self_ID.Common.State := Terminated;
598 end Terminate_Task;
600 --------------
601 -- Init_RTS --
602 --------------
604 procedure Init_RTS is
605 begin
606 Tasking.Initialize;
608 -- Initialize lock used to implement mutual exclusion between all tasks
610 STPO.Initialize_Lock (Global_Task_Lock'Access, STPO.Global_Task_Level);
612 -- Notify that the tasking run time has been elaborated so that
613 -- the tasking version of the soft links can be used.
615 SSL.Lock_Task := Task_Lock'Access;
616 SSL.Unlock_Task := Task_Unlock'Access;
617 SSL.Adafinal := Finalize_Global_Tasks'Access;
618 SSL.Get_Current_Excep := Get_Current_Excep'Access;
620 -- Initialize the tasking soft links (if not done yet) that are common
621 -- to the full and the restricted run times.
623 SSL.Tasking.Init_Tasking_Soft_Links;
624 end Init_RTS;
626 begin
627 Init_RTS;
628 end System.Tasking.Restricted.Stages;