1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1999-2009, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
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.
43 -- Turn off polling, we do not want ATC polling to take place during
44 -- tasking operations. It causes infinite loops and other problems.
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
;
70 use Task_Primitives
.Operations
;
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
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.
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
123 return STPO
.Self
.Common
.Compiler_Data
.Current_Excep
'Access;
124 end Get_Current_Excep
;
130 procedure Task_Lock
is
131 Self_ID
: constant Task_Id
:= STPO
.Self
;
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);
146 procedure Task_Unlock
is
147 Self_ID
: constant Task_Id
:= STPO
.Self
;
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);
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
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
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
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.
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));
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
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
239 Save_Occurrence
(EO
, Ada
.Exceptions
.Null_Occurrence
);
244 -- Task terminating because of an unhandled exception
246 Cause
:= Unhandled_Exception
;
247 Save_Occurrence
(EO
, E
);
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
265 if Self_ID
.Common
.Fall_Back_Handler
/= null then
266 Self_ID
.Common
.Fall_Back_Handler
(Cause
, Self_ID
, EO
);
269 TH
: Termination_Handler
:= null;
276 Write_Lock
(Self_ID
.Common
.Parent
);
278 TH
:= Self_ID
.Common
.Parent
.Common
.Fall_Back_Handler
;
280 Unlock
(Self_ID
.Common
.Parent
);
286 -- Execute the task termination handler if we found it
289 TH
.all (Cause
, Self_ID
, EO
);
294 Terminate_Task
(Self_ID
);
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
;
316 Activate_Prio
: System
.Any_Priority
;
320 pragma Assert
(Self_ID
= Environment_Task
);
321 pragma Assert
(Self_ID
.Common
.Wait_Count
= 0);
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
;
338 if C
.Common
.State
/= Terminated
then
339 pragma Assert
(C
.Common
.State
= Unactivated
);
343 if C
.Common
.Base_Priority
< Get_Priority
(Self_ID
) then
344 Activate_Prio
:= Get_Priority
(Self_ID
);
346 Activate_Prio
:= C
.Common
.Base_Priority
;
350 (C
, Task_Wrapper
'Address,
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;
358 C
.Common
.State
:= Runnable
;
366 C
:= C
.Common
.Activation_Link
;
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.
375 exit when Self_ID
.Common
.Wait_Count
= 0;
376 Sleep
(Self_ID
, Activator_Sleep
);
379 Self_ID
.Common
.State
:= Runnable
;
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
400 procedure Complete_Restricted_Activation
is
401 Self_ID
: constant Task_Id
:= STPO
.Self
;
402 Activator
: constant Task_Id
:= Self_ID
.Common
.Activator
;
409 Write_Lock
(Activator
);
410 Write_Lock
(Self_ID
);
412 -- Remove dangling reference to Activator, since a task may outlive its
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
);
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
);
442 end Complete_Restricted_Activation
;
444 ------------------------------
445 -- Complete_Restricted_Task --
446 ------------------------------
448 procedure Complete_Restricted_Task
is
450 STPO
.Self
.Common
.State
:= Terminated
;
451 end Complete_Restricted_Task
;
453 ----------------------------
454 -- Create_Restricted_Task --
455 ----------------------------
457 procedure Create_Restricted_Task
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
;
467 Created_Task
: Task_Id
)
469 Self_ID
: constant Task_Id
:= STPO
.Self
;
470 Base_Priority
: System
.Any_Priority
;
475 -- Stack is not preallocated on this target, so that Stack_Address must
478 pragma Assert
(Stack_Address
= Null_Address
);
480 if Priority
= Unspecified_Priority
then
481 Base_Priority
:= Self_ID
.Common
.Base_Priority
;
483 Base_Priority
:= System
.Any_Priority
(Priority
);
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
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
513 Created_Task
.Entry_Calls
(1).Self
:= Created_Task
;
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);
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
;
547 pragma Assert
(Self_ID
= STPO
.Environment_Task
);
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
);
577 -- Should never return from Master Completion Sleep
580 end Finalize_Global_Tasks
;
582 ---------------------------
583 -- Restricted_Terminated --
584 ---------------------------
586 function Restricted_Terminated
(T
: Task_Id
) return Boolean is
588 return T
.Common
.State
= Terminated
;
589 end Restricted_Terminated
;
595 procedure Terminate_Task
(Self_ID
: Task_Id
) is
597 Self_ID
.Common
.State
:= Terminated
;
604 procedure Init_RTS
is
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
;
628 end System
.Tasking
.Restricted
.Stages
;