[multiple changes]
[gcc.git] / gcc / ada / libgnarl / s-tarest.adb
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-2017, 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 ------------------------------------------------------------------------------
31
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.
35
36 -- This is a simplified version of the System.Tasking.Stages package,
37 -- intended to be used in a restricted run time.
38
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.
41
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.
45
46 with Ada.Exceptions;
47
48 with System.Task_Primitives.Operations;
49 with System.Soft_Links.Tasking;
50 with System.Storage_Elements;
51
52 with System.Secondary_Stack;
53 pragma Elaborate_All (System.Secondary_Stack);
54 -- Make sure the body of Secondary_Stack is elaborated before calling
55 -- Init_Tasking_Soft_Links. See comments for this routine for explanation.
56
57 with System.Soft_Links;
58 -- Used for the non-tasking routines (*_NT) that refer to global data. They
59 -- are needed here before the tasking run time has been elaborated. used for
60 -- Create_TSD This package also provides initialization routines for task
61 -- specific data. The GNARL must call these to be sure that all non-tasking
62 -- Ada constructs will work.
63
64 package body System.Tasking.Restricted.Stages is
65
66 package STPO renames System.Task_Primitives.Operations;
67 package SSL renames System.Soft_Links;
68 package SSE renames System.Storage_Elements;
69 package SST renames System.Secondary_Stack;
70
71 use Ada.Exceptions;
72
73 use Parameters;
74 use Task_Primitives.Operations;
75
76 Tasks_Activation_Chain : Task_Id;
77 -- Chain of all the tasks to activate
78
79 Global_Task_Lock : aliased System.Task_Primitives.RTS_Lock;
80 -- This is a global lock; it is used to execute in mutual exclusion
81 -- from all other tasks. It is only used by Task_Lock and Task_Unlock.
82
83 -----------------------------------------------------------------
84 -- Tasking versions of services needed by non-tasking programs --
85 -----------------------------------------------------------------
86
87 function Get_Current_Excep return SSL.EOA;
88 -- Task-safe version of SSL.Get_Current_Excep
89
90 procedure Task_Lock;
91 -- Locks out other tasks. Preceding a section of code by Task_Lock and
92 -- following it by Task_Unlock creates a critical region. This is used
93 -- for ensuring that a region of non-tasking code (such as code used to
94 -- allocate memory) is tasking safe. Note that it is valid for calls to
95 -- Task_Lock/Task_Unlock to be nested, and this must work properly, i.e.
96 -- only the corresponding outer level Task_Unlock will actually unlock.
97
98 procedure Task_Unlock;
99 -- Releases lock previously set by call to Task_Lock. In the nested case,
100 -- all nested locks must be released before other tasks competing for the
101 -- tasking lock are released.
102
103 -----------------------
104 -- Local Subprograms --
105 -----------------------
106
107 procedure Task_Wrapper (Self_ID : Task_Id);
108 -- This is the procedure that is called by the GNULL from the
109 -- new context when a task is created. It waits for activation
110 -- and then calls the task body procedure. When the task body
111 -- procedure completes, it terminates the task.
112
113 procedure Terminate_Task (Self_ID : Task_Id);
114 -- Terminate the calling task.
115 -- This should only be called by the Task_Wrapper procedure.
116
117 procedure Create_Restricted_Task
118 (Priority : Integer;
119 Stack_Address : System.Address;
120 Size : System.Parameters.Size_Type;
121 Secondary_Stack_Size : System.Parameters.Size_Type;
122 Task_Info : System.Task_Info.Task_Info_Type;
123 CPU : Integer;
124 State : Task_Procedure_Access;
125 Discriminants : System.Address;
126 Elaborated : Access_Boolean;
127 Task_Image : String;
128 Created_Task : Task_Id);
129 -- Code shared between Create_Restricted_Task (the concurrent version) and
130 -- Create_Restricted_Task_Sequential. See comment of the former in the
131 -- specification of this package.
132
133 procedure Activate_Tasks (Chain : Task_Id);
134 -- Activate the list of tasks started by Chain
135
136 procedure Init_RTS;
137 -- This procedure performs the initialization of the GNARL.
138 -- It consists of initializing the environment task, global locks, and
139 -- installing tasking versions of certain operations used by the compiler.
140 -- Init_RTS is called during elaboration.
141
142 -----------------------
143 -- Get_Current_Excep --
144 -----------------------
145
146 function Get_Current_Excep return SSL.EOA is
147 begin
148 return STPO.Self.Common.Compiler_Data.Current_Excep'Access;
149 end Get_Current_Excep;
150
151 ---------------
152 -- Task_Lock --
153 ---------------
154
155 procedure Task_Lock is
156 Self_ID : constant Task_Id := STPO.Self;
157
158 begin
159 Self_ID.Common.Global_Task_Lock_Nesting :=
160 Self_ID.Common.Global_Task_Lock_Nesting + 1;
161
162 if Self_ID.Common.Global_Task_Lock_Nesting = 1 then
163 STPO.Write_Lock (Global_Task_Lock'Access, Global_Lock => True);
164 end if;
165 end Task_Lock;
166
167 -----------------
168 -- Task_Unlock --
169 -----------------
170
171 procedure Task_Unlock is
172 Self_ID : constant Task_Id := STPO.Self;
173
174 begin
175 pragma Assert (Self_ID.Common.Global_Task_Lock_Nesting > 0);
176 Self_ID.Common.Global_Task_Lock_Nesting :=
177 Self_ID.Common.Global_Task_Lock_Nesting - 1;
178
179 if Self_ID.Common.Global_Task_Lock_Nesting = 0 then
180 STPO.Unlock (Global_Task_Lock'Access, Global_Lock => True);
181 end if;
182 end Task_Unlock;
183
184 ------------------
185 -- Task_Wrapper --
186 ------------------
187
188 -- The task wrapper is a procedure that is called first for each task
189 -- task body, and which in turn calls the compiler-generated task body
190 -- procedure. The wrapper's main job is to do initialization for the task.
191
192 -- The variable ID in the task wrapper is used to implement the Self
193 -- function on targets where there is a fast way to find the stack base
194 -- of the current thread, since it should be at a fixed offset from the
195 -- stack base.
196
197 procedure Task_Wrapper (Self_ID : Task_Id) is
198 ID : Task_Id := Self_ID;
199 pragma Volatile (ID);
200 pragma Warnings (Off, ID);
201 -- Variable used on some targets to implement a fast self. We turn off
202 -- warnings because a stand alone volatile constant has to be imported,
203 -- so we don't want warnings about ID not being referenced, and volatile
204 -- having no effect.
205 --
206 -- DO NOT delete ID. As noted, it is needed on some targets.
207
208 function Secondary_Stack_Size return Storage_Elements.Storage_Offset;
209 -- Returns the size of the secondary stack for the task. For fixed
210 -- secondary stacks, the function will return the ATCB field
211 -- Secondary_Stack_Size if it is not set to Unspecified_Size,
212 -- otherwise a percentage of the stack is reserved using the
213 -- System.Parameters.Sec_Stack_Percentage property.
214
215 -- Dynamic secondary stacks are allocated in System.Soft_Links.
216 -- Create_TSD and thus the function returns 0 to suppress the
217 -- creation of the fixed secondary stack in the primary stack.
218
219 --------------------------
220 -- Secondary_Stack_Size --
221 --------------------------
222
223 function Secondary_Stack_Size return Storage_Elements.Storage_Offset is
224 use System.Storage_Elements;
225 use System.Secondary_Stack;
226
227 begin
228 if Parameters.Sec_Stack_Dynamic then
229 return 0;
230
231 elsif Self_ID.Common.Secondary_Stack_Size = Unspecified_Size then
232 return (Self_ID.Common.Compiler_Data.Pri_Stack_Info.Size
233 * SSE.Storage_Offset (Sec_Stack_Percentage) / 100);
234 else
235 -- Use the size specified by aspect Secondary_Stack_Size padded
236 -- by the amount of space used by the stack data structure.
237
238 return Storage_Offset (Self_ID.Common.Secondary_Stack_Size) +
239 Storage_Offset (Minimum_Secondary_Stack_Size);
240 end if;
241 end Secondary_Stack_Size;
242
243 Secondary_Stack : aliased Storage_Elements.Storage_Array
244 (1 .. Secondary_Stack_Size);
245 for Secondary_Stack'Alignment use Standard'Maximum_Alignment;
246 -- This is the secondary stack data. Note that it is critical that this
247 -- have maximum alignment, since any kind of data can be allocated here.
248
249 pragma Warnings (Off);
250 Secondary_Stack_Address : System.Address := Secondary_Stack'Address;
251 pragma Warnings (On);
252 -- Address of secondary stack. In the fixed secondary stack case, this
253 -- value is not modified, causing a warning, hence the bracketing with
254 -- Warnings (Off/On).
255
256 Cause : Cause_Of_Termination := Normal;
257 -- Indicates the reason why this task terminates. Normal corresponds to
258 -- a task terminating due to completing the last statement of its body.
259 -- If the task terminates because of an exception raised by the
260 -- execution of its task body, then Cause is set to Unhandled_Exception.
261 -- Aborts are not allowed in the restricted profile to which this file
262 -- belongs.
263
264 EO : Exception_Occurrence;
265 -- If the task terminates because of an exception raised by the
266 -- execution of its task body, then EO will contain the associated
267 -- exception occurrence. Otherwise, it will contain Null_Occurrence.
268
269 -- Start of processing for Task_Wrapper
270
271 begin
272 if not Parameters.Sec_Stack_Dynamic then
273 Self_ID.Common.Compiler_Data.Sec_Stack_Addr :=
274 Secondary_Stack'Address;
275 SST.SS_Init (Secondary_Stack_Address, Integer (Secondary_Stack'Last));
276 end if;
277
278 -- Initialize low-level TCB components, that cannot be initialized by
279 -- the creator.
280
281 Enter_Task (Self_ID);
282
283 -- Call the task body procedure
284
285 begin
286 -- We are separating the following portion of the code in order to
287 -- place the exception handlers in a different block. In this way we
288 -- do not call Set_Jmpbuf_Address (which needs Self) before we set
289 -- Self in Enter_Task.
290
291 -- Note that in the case of Ravenscar HI-E where there are no
292 -- exception handlers, the exception handler is suppressed.
293
294 -- Call the task body procedure
295
296 Self_ID.Common.Task_Entry_Point (Self_ID.Common.Task_Arg);
297
298 -- Normal task termination
299
300 Cause := Normal;
301 Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence);
302
303 exception
304 when E : others =>
305
306 -- Task terminating because of an unhandled exception
307
308 Cause := Unhandled_Exception;
309 Save_Occurrence (EO, E);
310 end;
311
312 -- Look for a fall-back handler
313
314 -- This package is part of the restricted run time which supports
315 -- neither task hierarchies (No_Task_Hierarchy) nor specific task
316 -- termination handlers (No_Specific_Termination_Handlers).
317
318 -- As specified in ARM C.7.3 par. 9/2, "the fall-back handler applies
319 -- only to the dependent tasks of the task". Hence, if the terminating
320 -- tasks (Self_ID) had a fall-back handler, it would not apply to
321 -- itself. This code is always executed by a task whose master is the
322 -- environment task (the task termination code for the environment task
323 -- is executed by SSL.Task_Termination_Handler), so the fall-back
324 -- handler to execute for this task can only be defined by its parent
325 -- (there is no grandparent).
326
327 declare
328 TH : Termination_Handler := null;
329
330 begin
331 if Single_Lock then
332 Lock_RTS;
333 end if;
334
335 Write_Lock (Self_ID.Common.Parent);
336
337 TH := Self_ID.Common.Parent.Common.Fall_Back_Handler;
338
339 Unlock (Self_ID.Common.Parent);
340
341 if Single_Lock then
342 Unlock_RTS;
343 end if;
344
345 -- Execute the task termination handler if we found it
346
347 if TH /= null then
348 TH.all (Cause, Self_ID, EO);
349 end if;
350 end;
351
352 Terminate_Task (Self_ID);
353 end Task_Wrapper;
354
355 -----------------------
356 -- Restricted GNARLI --
357 -----------------------
358
359 -----------------------------------
360 -- Activate_All_Tasks_Sequential --
361 -----------------------------------
362
363 procedure Activate_All_Tasks_Sequential is
364 begin
365 pragma Assert (Partition_Elaboration_Policy = 'S');
366
367 Activate_Tasks (Tasks_Activation_Chain);
368 Tasks_Activation_Chain := Null_Task;
369 end Activate_All_Tasks_Sequential;
370
371 -------------------------------
372 -- Activate_Restricted_Tasks --
373 -------------------------------
374
375 procedure Activate_Restricted_Tasks
376 (Chain_Access : Activation_Chain_Access) is
377 begin
378 if Partition_Elaboration_Policy = 'S' then
379
380 -- In sequential elaboration policy, the chain must be empty. This
381 -- procedure can be called if the unit has been compiled without
382 -- partition elaboration policy, but the partition has a sequential
383 -- elaboration policy.
384
385 pragma Assert (Chain_Access.T_ID = Null_Task);
386 null;
387 else
388 Activate_Tasks (Chain_Access.T_ID);
389 Chain_Access.T_ID := Null_Task;
390 end if;
391 end Activate_Restricted_Tasks;
392
393 --------------------
394 -- Activate_Tasks --
395 --------------------
396
397 -- Note that locks of activator and activated task are both locked here.
398 -- This is necessary because C.State and Self.Wait_Count have to be
399 -- synchronized. This is safe from deadlock because the activator is always
400 -- created before the activated task. That satisfies our
401 -- in-order-of-creation ATCB locking policy.
402
403 procedure Activate_Tasks (Chain : Task_Id) is
404 Self_ID : constant Task_Id := STPO.Self;
405 C : Task_Id;
406 Activate_Prio : System.Any_Priority;
407 Success : Boolean;
408
409 begin
410 pragma Assert (Self_ID = Environment_Task);
411 pragma Assert (Self_ID.Common.Wait_Count = 0);
412
413 if Single_Lock then
414 Lock_RTS;
415 end if;
416
417 -- Lock self, to prevent activated tasks from racing ahead before we
418 -- finish activating the chain.
419
420 Write_Lock (Self_ID);
421
422 -- Activate all the tasks in the chain. Creation of the thread of
423 -- control was deferred until activation. So create it now.
424
425 C := Chain;
426 while C /= null loop
427 if C.Common.State /= Terminated then
428 pragma Assert (C.Common.State = Unactivated);
429
430 Write_Lock (C);
431
432 Activate_Prio :=
433 (if C.Common.Base_Priority < Get_Priority (Self_ID)
434 then Get_Priority (Self_ID)
435 else C.Common.Base_Priority);
436
437 STPO.Create_Task
438 (C, Task_Wrapper'Address,
439 Parameters.Size_Type
440 (C.Common.Compiler_Data.Pri_Stack_Info.Size),
441 Activate_Prio, Success);
442
443 Self_ID.Common.Wait_Count := Self_ID.Common.Wait_Count + 1;
444
445 if Success then
446 C.Common.State := Runnable;
447 else
448 raise Program_Error;
449 end if;
450
451 Unlock (C);
452 end if;
453
454 C := C.Common.Activation_Link;
455 end loop;
456
457 Self_ID.Common.State := Activator_Sleep;
458
459 -- Wait for the activated tasks to complete activation. It is unsafe to
460 -- abort any of these tasks until the count goes to zero.
461
462 loop
463 exit when Self_ID.Common.Wait_Count = 0;
464 Sleep (Self_ID, Activator_Sleep);
465 end loop;
466
467 Self_ID.Common.State := Runnable;
468 Unlock (Self_ID);
469
470 if Single_Lock then
471 Unlock_RTS;
472 end if;
473 end Activate_Tasks;
474
475 ------------------------------------
476 -- Complete_Restricted_Activation --
477 ------------------------------------
478
479 -- As in several other places, the locks of the activator and activated
480 -- task are both locked here. This follows our deadlock prevention lock
481 -- ordering policy, since the activated task must be created after the
482 -- activator.
483
484 procedure Complete_Restricted_Activation is
485 Self_ID : constant Task_Id := STPO.Self;
486 Activator : constant Task_Id := Self_ID.Common.Activator;
487
488 begin
489 if Single_Lock then
490 Lock_RTS;
491 end if;
492
493 Write_Lock (Activator);
494 Write_Lock (Self_ID);
495
496 -- Remove dangling reference to Activator, since a task may outlive its
497 -- activator.
498
499 Self_ID.Common.Activator := null;
500
501 -- Wake up the activator, if it is waiting for a chain of tasks to
502 -- activate, and we are the last in the chain to complete activation
503
504 if Activator.Common.State = Activator_Sleep then
505 Activator.Common.Wait_Count := Activator.Common.Wait_Count - 1;
506
507 if Activator.Common.Wait_Count = 0 then
508 Wakeup (Activator, Activator_Sleep);
509 end if;
510 end if;
511
512 Unlock (Self_ID);
513 Unlock (Activator);
514
515 if Single_Lock then
516 Unlock_RTS;
517 end if;
518
519 -- After the activation, active priority should be the same as base
520 -- priority. We must unlock the Activator first, though, since it should
521 -- not wait if we have lower priority.
522
523 if Get_Priority (Self_ID) /= Self_ID.Common.Base_Priority then
524 Set_Priority (Self_ID, Self_ID.Common.Base_Priority);
525 end if;
526 end Complete_Restricted_Activation;
527
528 ------------------------------
529 -- Complete_Restricted_Task --
530 ------------------------------
531
532 procedure Complete_Restricted_Task is
533 begin
534 STPO.Self.Common.State := Terminated;
535 end Complete_Restricted_Task;
536
537 ----------------------------
538 -- Create_Restricted_Task --
539 ----------------------------
540
541 procedure Create_Restricted_Task
542 (Priority : Integer;
543 Stack_Address : System.Address;
544 Size : System.Parameters.Size_Type;
545 Secondary_Stack_Size : System.Parameters.Size_Type;
546 Task_Info : System.Task_Info.Task_Info_Type;
547 CPU : Integer;
548 State : Task_Procedure_Access;
549 Discriminants : System.Address;
550 Elaborated : Access_Boolean;
551 Task_Image : String;
552 Created_Task : Task_Id)
553 is
554 Self_ID : constant Task_Id := STPO.Self;
555 Base_Priority : System.Any_Priority;
556 Base_CPU : System.Multiprocessors.CPU_Range;
557 Success : Boolean;
558 Len : Integer;
559
560 begin
561 -- Stack is not preallocated on this target, so that Stack_Address must
562 -- be null.
563
564 pragma Assert (Stack_Address = Null_Address);
565
566 Base_Priority :=
567 (if Priority = Unspecified_Priority
568 then Self_ID.Common.Base_Priority
569 else System.Any_Priority (Priority));
570
571 -- Legal values of CPU are the special Unspecified_CPU value which is
572 -- inserted by the compiler for tasks without CPU aspect, and those in
573 -- the range of CPU_Range but no greater than Number_Of_CPUs. Otherwise
574 -- the task is defined to have failed, and it becomes a completed task
575 -- (RM D.16(14/3)).
576
577 if CPU /= Unspecified_CPU
578 and then (CPU < Integer (System.Multiprocessors.CPU_Range'First)
579 or else CPU > Integer (System.Multiprocessors.Number_Of_CPUs))
580 then
581 raise Tasking_Error with "CPU not in range";
582
583 -- Normal CPU affinity
584 else
585 -- When the application code says nothing about the task affinity
586 -- (task without CPU aspect) then the compiler inserts the
587 -- Unspecified_CPU value which indicates to the run-time library that
588 -- the task will activate and execute on the same processor as its
589 -- activating task if the activating task is assigned a processor
590 -- (RM D.16(14/3)).
591
592 Base_CPU :=
593 (if CPU = Unspecified_CPU
594 then Self_ID.Common.Base_CPU
595 else System.Multiprocessors.CPU_Range (CPU));
596 end if;
597
598 if Single_Lock then
599 Lock_RTS;
600 end if;
601
602 Write_Lock (Self_ID);
603
604 -- With no task hierarchy, the parent of all non-Environment tasks that
605 -- are created must be the Environment task. Dispatching domains are
606 -- not allowed in Ravenscar, so the dispatching domain parameter will
607 -- always be null.
608
609 Initialize_ATCB
610 (Self_ID, State, Discriminants, Self_ID, Elaborated, Base_Priority,
611 Base_CPU, null, Task_Info, Size, Secondary_Stack_Size,
612 Created_Task, Success);
613
614 -- If we do our job right then there should never be any failures, which
615 -- was probably said about the Titanic; so just to be safe, let's retain
616 -- this code for now
617
618 if not Success then
619 Unlock (Self_ID);
620
621 if Single_Lock then
622 Unlock_RTS;
623 end if;
624
625 raise Program_Error;
626 end if;
627
628 Created_Task.Entry_Calls (1).Self := Created_Task;
629
630 Len :=
631 Integer'Min (Created_Task.Common.Task_Image'Length, Task_Image'Length);
632 Created_Task.Common.Task_Image_Len := Len;
633 Created_Task.Common.Task_Image (1 .. Len) :=
634 Task_Image (Task_Image'First .. Task_Image'First + Len - 1);
635
636 Unlock (Self_ID);
637
638 if Single_Lock then
639 Unlock_RTS;
640 end if;
641
642 -- Create TSD as early as possible in the creation of a task, since it
643 -- may be used by the operation of Ada code within the task.
644
645 SSL.Create_TSD (Created_Task.Common.Compiler_Data);
646 end Create_Restricted_Task;
647
648 procedure Create_Restricted_Task
649 (Priority : Integer;
650 Stack_Address : System.Address;
651 Size : System.Parameters.Size_Type;
652 Secondary_Stack_Size : System.Parameters.Size_Type;
653 Task_Info : System.Task_Info.Task_Info_Type;
654 CPU : Integer;
655 State : Task_Procedure_Access;
656 Discriminants : System.Address;
657 Elaborated : Access_Boolean;
658 Chain : in out Activation_Chain;
659 Task_Image : String;
660 Created_Task : Task_Id)
661 is
662 begin
663 if Partition_Elaboration_Policy = 'S' then
664
665 -- A unit may have been compiled without partition elaboration
666 -- policy, and in this case the compiler will emit calls for the
667 -- default policy (concurrent). But if the partition policy is
668 -- sequential, activation must be deferred.
669
670 Create_Restricted_Task_Sequential
671 (Priority, Stack_Address, Size, Secondary_Stack_Size,
672 Task_Info, CPU, State, Discriminants, Elaborated,
673 Task_Image, Created_Task);
674
675 else
676 Create_Restricted_Task
677 (Priority, Stack_Address, Size, Secondary_Stack_Size,
678 Task_Info, CPU, State, Discriminants, Elaborated,
679 Task_Image, Created_Task);
680
681 -- Append this task to the activation chain
682
683 Created_Task.Common.Activation_Link := Chain.T_ID;
684 Chain.T_ID := Created_Task;
685 end if;
686 end Create_Restricted_Task;
687
688 ---------------------------------------
689 -- Create_Restricted_Task_Sequential --
690 ---------------------------------------
691
692 procedure Create_Restricted_Task_Sequential
693 (Priority : Integer;
694 Stack_Address : System.Address;
695 Size : System.Parameters.Size_Type;
696 Secondary_Stack_Size : System.Parameters.Size_Type;
697 Task_Info : System.Task_Info.Task_Info_Type;
698 CPU : Integer;
699 State : Task_Procedure_Access;
700 Discriminants : System.Address;
701 Elaborated : Access_Boolean;
702 Task_Image : String;
703 Created_Task : Task_Id) is
704 begin
705 Create_Restricted_Task (Priority, Stack_Address, Size,
706 Secondary_Stack_Size, Task_Info,
707 CPU, State, Discriminants, Elaborated,
708 Task_Image, Created_Task);
709
710 -- Append this task to the activation chain
711
712 Created_Task.Common.Activation_Link := Tasks_Activation_Chain;
713 Tasks_Activation_Chain := Created_Task;
714 end Create_Restricted_Task_Sequential;
715
716 ---------------------------
717 -- Finalize_Global_Tasks --
718 ---------------------------
719
720 -- This is needed to support the compiler interface; it will only be called
721 -- by the Environment task. Instead, it will cause the Environment to block
722 -- forever, since none of the dependent tasks are expected to terminate
723
724 procedure Finalize_Global_Tasks is
725 Self_ID : constant Task_Id := STPO.Self;
726
727 begin
728 pragma Assert (Self_ID = STPO.Environment_Task);
729
730 if Single_Lock then
731 Lock_RTS;
732 end if;
733
734 -- Handle normal task termination by the environment task, but only for
735 -- the normal task termination. In the case of Abnormal and
736 -- Unhandled_Exception they must have been handled before, and the task
737 -- termination soft link must have been changed so the task termination
738 -- routine is not executed twice.
739
740 -- Note that in the "normal" implementation in s-tassta.adb the task
741 -- termination procedure for the environment task should be executed
742 -- after termination of library-level tasks. However, this
743 -- implementation is to be used when the Ravenscar restrictions are in
744 -- effect, and AI-394 says that if there is a fall-back handler set for
745 -- the partition it should be called when the first task (including the
746 -- environment task) attempts to terminate.
747
748 SSL.Task_Termination_Handler.all (Ada.Exceptions.Null_Occurrence);
749
750 Write_Lock (Self_ID);
751 Sleep (Self_ID, Master_Completion_Sleep);
752 Unlock (Self_ID);
753
754 if Single_Lock then
755 Unlock_RTS;
756 end if;
757
758 -- Should never return from Master Completion Sleep
759
760 raise Program_Error;
761 end Finalize_Global_Tasks;
762
763 ---------------------------
764 -- Restricted_Terminated --
765 ---------------------------
766
767 function Restricted_Terminated (T : Task_Id) return Boolean is
768 begin
769 return T.Common.State = Terminated;
770 end Restricted_Terminated;
771
772 --------------------
773 -- Terminate_Task --
774 --------------------
775
776 procedure Terminate_Task (Self_ID : Task_Id) is
777 begin
778 Self_ID.Common.State := Terminated;
779 end Terminate_Task;
780
781 --------------
782 -- Init_RTS --
783 --------------
784
785 procedure Init_RTS is
786 begin
787 Tasking.Initialize;
788
789 -- Initialize lock used to implement mutual exclusion between all tasks
790
791 STPO.Initialize_Lock (Global_Task_Lock'Access, STPO.Global_Task_Level);
792
793 -- Notify that the tasking run time has been elaborated so that
794 -- the tasking version of the soft links can be used.
795
796 SSL.Lock_Task := Task_Lock'Access;
797 SSL.Unlock_Task := Task_Unlock'Access;
798 SSL.Adafinal := Finalize_Global_Tasks'Access;
799 SSL.Get_Current_Excep := Get_Current_Excep'Access;
800
801 -- Initialize the tasking soft links (if not done yet) that are common
802 -- to the full and the restricted run times.
803
804 SSL.Tasking.Init_Tasking_Soft_Links;
805 end Init_RTS;
806
807 begin
808 Init_RTS;
809 end System.Tasking.Restricted.Stages;