| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . S O F T _ L I N K S . T A S K I N G -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2004-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT 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 |
-- GNAT was originally developed by the GNAT team at New York 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 ordering check, since we group soft link bodies |
| 34 |
-- and dummy soft link bodies together separately in this unit. |
| 35 |
|
| 36 |
with Ada.Exceptions; |
| 37 |
with Ada.Exceptions.Is_Null_Occurrence; |
| 38 |
|
| 39 |
with System.Task_Primitives.Operations; |
| 40 |
with System.Tasking; |
| 41 |
with System.Stack_Checking; |
| 42 |
with System.Secondary_Stack; |
| 43 |
|
| 44 |
package body System.Soft_Links.Tasking is |
| 45 |
|
| 46 |
package STPO renames System.Task_Primitives.Operations; |
| 47 |
package SSL renames System.Soft_Links; |
| 48 |
|
| 49 |
use Ada.Exceptions; |
| 50 |
|
| 51 |
use type System.Secondary_Stack.SS_Stack_Ptr; |
| 52 |
|
| 53 |
use type System.Tasking.Task_Id; |
| 54 |
use type System.Tasking.Termination_Handler; |
| 55 |
|
| 56 |
---------------- |
| 57 |
-- Local Data -- |
| 58 |
---------------- |
| 59 |
|
| 60 |
Initialized : Boolean := False; |
| 61 |
-- Boolean flag that indicates whether the tasking soft links have |
| 62 |
-- already been set. |
| 63 |
|
| 64 |
----------------------------------------------------------------- |
| 65 |
-- Tasking Versions of Services Needed by Non-Tasking Programs -- |
| 66 |
----------------------------------------------------------------- |
| 67 |
|
| 68 |
function Get_Jmpbuf_Address return Address; |
| 69 |
procedure Set_Jmpbuf_Address (Addr : Address); |
| 70 |
-- Get/Set Jmpbuf_Address for current task |
| 71 |
|
| 72 |
function Get_Sec_Stack return SST.SS_Stack_Ptr; |
| 73 |
procedure Set_Sec_Stack (Stack : SST.SS_Stack_Ptr); |
| 74 |
-- Get/Set location of current task's secondary stack |
| 75 |
|
| 76 |
procedure Timed_Delay_T (Time : Duration; Mode : Integer); |
| 77 |
-- Task-safe version of SSL.Timed_Delay |
| 78 |
|
| 79 |
procedure Task_Termination_Handler_T (Excep : SSL.EO); |
| 80 |
-- Task-safe version of the task termination procedure |
| 81 |
|
| 82 |
function Get_Stack_Info return Stack_Checking.Stack_Access; |
| 83 |
-- Get access to the current task's Stack_Info |
| 84 |
|
| 85 |
-------------------------- |
| 86 |
-- Soft-Link Get Bodies -- |
| 87 |
-------------------------- |
| 88 |
|
| 89 |
function Get_Jmpbuf_Address return Address is |
| 90 |
begin |
| 91 |
return STPO.Self.Common.Compiler_Data.Jmpbuf_Address; |
| 92 |
end Get_Jmpbuf_Address; |
| 93 |
|
| 94 |
function Get_Sec_Stack return SST.SS_Stack_Ptr is |
| 95 |
begin |
| 96 |
return Result : constant SST.SS_Stack_Ptr := |
| 97 |
STPO.Self.Common.Compiler_Data.Sec_Stack_Ptr |
| 98 |
do |
| 99 |
pragma Assert (Result /= null); |
| 100 |
end return; |
| 101 |
end Get_Sec_Stack; |
| 102 |
|
| 103 |
function Get_Stack_Info return Stack_Checking.Stack_Access is |
| 104 |
begin |
| 105 |
return STPO.Self.Common.Compiler_Data.Pri_Stack_Info'Access; |
| 106 |
end Get_Stack_Info; |
| 107 |
|
| 108 |
-------------------------- |
| 109 |
-- Soft-Link Set Bodies -- |
| 110 |
-------------------------- |
| 111 |
|
| 112 |
procedure Set_Jmpbuf_Address (Addr : Address) is |
| 113 |
begin |
| 114 |
STPO.Self.Common.Compiler_Data.Jmpbuf_Address := Addr; |
| 115 |
end Set_Jmpbuf_Address; |
| 116 |
|
| 117 |
procedure Set_Sec_Stack (Stack : SST.SS_Stack_Ptr) is |
| 118 |
begin |
| 119 |
STPO.Self.Common.Compiler_Data.Sec_Stack_Ptr := Stack; |
| 120 |
end Set_Sec_Stack; |
| 121 |
|
| 122 |
------------------- |
| 123 |
-- Timed_Delay_T -- |
| 124 |
------------------- |
| 125 |
|
| 126 |
procedure Timed_Delay_T (Time : Duration; Mode : Integer) is |
| 127 |
Self_Id : constant System.Tasking.Task_Id := STPO.Self; |
| 128 |
|
| 129 |
begin |
| 130 |
-- In case pragma Detect_Blocking is active then Program_Error |
| 131 |
-- must be raised if this potentially blocking operation |
| 132 |
-- is called from a protected operation. |
| 133 |
|
| 134 |
if System.Tasking.Detect_Blocking |
| 135 |
and then Self_Id.Common.Protected_Action_Nesting > 0 |
| 136 |
then |
| 137 |
raise Program_Error with "potentially blocking operation"; |
| 138 |
else |
| 139 |
Abort_Defer.all; |
| 140 |
STPO.Timed_Delay (Self_Id, Time, Mode); |
| 141 |
Abort_Undefer.all; |
| 142 |
end if; |
| 143 |
end Timed_Delay_T; |
| 144 |
|
| 145 |
-------------------------------- |
| 146 |
-- Task_Termination_Handler_T -- |
| 147 |
-------------------------------- |
| 148 |
|
| 149 |
procedure Task_Termination_Handler_T (Excep : SSL.EO) is |
| 150 |
Self_Id : constant System.Tasking.Task_Id := STPO.Self; |
| 151 |
Cause : System.Tasking.Cause_Of_Termination; |
| 152 |
EO : Ada.Exceptions.Exception_Occurrence; |
| 153 |
|
| 154 |
begin |
| 155 |
-- We can only be here because we are terminating the environment task. |
| 156 |
-- Task termination for all other tasks is handled in the Task_Wrapper. |
| 157 |
|
| 158 |
-- We do not want to enable this check and e.g. call System.OS_Lib.Abort |
| 159 |
-- here because some restricted run-times may not have System.OS_Lib |
| 160 |
-- and calling abort may do more harm than good to the main application. |
| 161 |
|
| 162 |
pragma Assert (Self_Id = STPO.Environment_Task); |
| 163 |
|
| 164 |
-- Normal task termination |
| 165 |
|
| 166 |
if Is_Null_Occurrence (Excep) then |
| 167 |
Cause := System.Tasking.Normal; |
| 168 |
Ada.Exceptions.Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence); |
| 169 |
|
| 170 |
-- Abnormal task termination |
| 171 |
|
| 172 |
elsif Exception_Identity (Excep) = Standard'Abort_Signal'Identity then |
| 173 |
Cause := System.Tasking.Abnormal; |
| 174 |
Ada.Exceptions.Save_Occurrence (EO, Ada.Exceptions.Null_Occurrence); |
| 175 |
|
| 176 |
-- Termination because of an unhandled exception |
| 177 |
|
| 178 |
else |
| 179 |
Cause := System.Tasking.Unhandled_Exception; |
| 180 |
Ada.Exceptions.Save_Occurrence (EO, Excep); |
| 181 |
end if; |
| 182 |
|
| 183 |
-- There is no need for explicit protection against race conditions for |
| 184 |
-- this part because it can only be executed by the environment task |
| 185 |
-- after all the other tasks have been finalized. Note that there is no |
| 186 |
-- fall-back handler which could apply to this environment task because |
| 187 |
-- it has no parents, and, as specified in ARM C.7.3 par. 9/2, "the |
| 188 |
-- fall-back handler applies only to the dependent tasks of the task". |
| 189 |
|
| 190 |
if Self_Id.Common.Specific_Handler /= null then |
| 191 |
begin |
| 192 |
Self_Id.Common.Specific_Handler.all (Cause, Self_Id, EO); |
| 193 |
exception |
| 194 |
-- RM-C.7.3(16) requires all exceptions raised here to be ignored |
| 195 |
|
| 196 |
when others => |
| 197 |
null; |
| 198 |
end; |
| 199 |
end if; |
| 200 |
end Task_Termination_Handler_T; |
| 201 |
|
| 202 |
----------------------------- |
| 203 |
-- Init_Tasking_Soft_Links -- |
| 204 |
----------------------------- |
| 205 |
|
| 206 |
procedure Init_Tasking_Soft_Links is |
| 207 |
begin |
| 208 |
-- Set links only if not set already |
| 209 |
|
| 210 |
if not Initialized then |
| 211 |
|
| 212 |
-- Mark tasking soft links as initialized |
| 213 |
|
| 214 |
Initialized := True; |
| 215 |
|
| 216 |
-- The application being executed uses tasking so that the tasking |
| 217 |
-- version of the following soft links need to be used. |
| 218 |
|
| 219 |
SSL.Get_Jmpbuf_Address := Get_Jmpbuf_Address'Access; |
| 220 |
SSL.Set_Jmpbuf_Address := Set_Jmpbuf_Address'Access; |
| 221 |
SSL.Get_Sec_Stack := Get_Sec_Stack'Access; |
| 222 |
SSL.Get_Stack_Info := Get_Stack_Info'Access; |
| 223 |
SSL.Set_Sec_Stack := Set_Sec_Stack'Access; |
| 224 |
SSL.Timed_Delay := Timed_Delay_T'Access; |
| 225 |
SSL.Task_Termination_Handler := Task_Termination_Handler_T'Access; |
| 226 |
|
| 227 |
-- No need to create a new secondary stack, since we will use the |
| 228 |
-- default one created in s-secsta.adb. |
| 229 |
|
| 230 |
SSL.Set_Sec_Stack (SSL.Get_Sec_Stack_NT); |
| 231 |
SSL.Set_Jmpbuf_Address (SSL.Get_Jmpbuf_Address_NT); |
| 232 |
end if; |
| 233 |
|
| 234 |
pragma Assert (Get_Sec_Stack /= null); |
| 235 |
end Init_Tasking_Soft_Links; |
| 236 |
|
| 237 |
end System.Soft_Links.Tasking; |