| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . T A S K _ T E R M I N A T I O N -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2005-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 |
with System.Tasking; |
| 33 |
with System.Task_Primitives.Operations; |
| 34 |
with System.Soft_Links; |
| 35 |
|
| 36 |
with Ada.Unchecked_Conversion; |
| 37 |
|
| 38 |
package body Ada.Task_Termination is |
| 39 |
|
| 40 |
use type Ada.Task_Identification.Task_Id; |
| 41 |
|
| 42 |
package STPO renames System.Task_Primitives.Operations; |
| 43 |
package SSL renames System.Soft_Links; |
| 44 |
|
| 45 |
----------------------- |
| 46 |
-- Local subprograms -- |
| 47 |
----------------------- |
| 48 |
|
| 49 |
function To_TT is new Ada.Unchecked_Conversion |
| 50 |
(System.Tasking.Termination_Handler, Termination_Handler); |
| 51 |
|
| 52 |
function To_ST is new Ada.Unchecked_Conversion |
| 53 |
(Termination_Handler, System.Tasking.Termination_Handler); |
| 54 |
|
| 55 |
function To_Task_Id is new Ada.Unchecked_Conversion |
| 56 |
(Ada.Task_Identification.Task_Id, System.Tasking.Task_Id); |
| 57 |
|
| 58 |
----------------------------------- |
| 59 |
-- Current_Task_Fallback_Handler -- |
| 60 |
----------------------------------- |
| 61 |
|
| 62 |
function Current_Task_Fallback_Handler return Termination_Handler is |
| 63 |
begin |
| 64 |
-- There is no need for explicit protection against race conditions |
| 65 |
-- for this function because this function can only be executed by |
| 66 |
-- Self, and the Fall_Back_Handler can only be modified by Self. |
| 67 |
|
| 68 |
return To_TT (STPO.Self.Common.Fall_Back_Handler); |
| 69 |
end Current_Task_Fallback_Handler; |
| 70 |
|
| 71 |
------------------------------------- |
| 72 |
-- Set_Dependents_Fallback_Handler -- |
| 73 |
------------------------------------- |
| 74 |
|
| 75 |
procedure Set_Dependents_Fallback_Handler |
| 76 |
(Handler : Termination_Handler) |
| 77 |
is |
| 78 |
Self : constant System.Tasking.Task_Id := STPO.Self; |
| 79 |
|
| 80 |
begin |
| 81 |
SSL.Abort_Defer.all; |
| 82 |
STPO.Write_Lock (Self); |
| 83 |
|
| 84 |
Self.Common.Fall_Back_Handler := To_ST (Handler); |
| 85 |
|
| 86 |
STPO.Unlock (Self); |
| 87 |
SSL.Abort_Undefer.all; |
| 88 |
end Set_Dependents_Fallback_Handler; |
| 89 |
|
| 90 |
-------------------------- |
| 91 |
-- Set_Specific_Handler -- |
| 92 |
-------------------------- |
| 93 |
|
| 94 |
procedure Set_Specific_Handler |
| 95 |
(T : Ada.Task_Identification.Task_Id; |
| 96 |
Handler : Termination_Handler) |
| 97 |
is |
| 98 |
begin |
| 99 |
-- Tasking_Error is raised if the task identified by T has already |
| 100 |
-- terminated. Program_Error is raised if the value of T is |
| 101 |
-- Null_Task_Id. |
| 102 |
|
| 103 |
if T = Ada.Task_Identification.Null_Task_Id then |
| 104 |
raise Program_Error; |
| 105 |
elsif Ada.Task_Identification.Is_Terminated (T) then |
| 106 |
raise Tasking_Error; |
| 107 |
else |
| 108 |
declare |
| 109 |
Target : constant System.Tasking.Task_Id := To_Task_Id (T); |
| 110 |
|
| 111 |
begin |
| 112 |
SSL.Abort_Defer.all; |
| 113 |
STPO.Write_Lock (Target); |
| 114 |
|
| 115 |
Target.Common.Specific_Handler := To_ST (Handler); |
| 116 |
|
| 117 |
STPO.Unlock (Target); |
| 118 |
SSL.Abort_Undefer.all; |
| 119 |
end; |
| 120 |
end if; |
| 121 |
end Set_Specific_Handler; |
| 122 |
|
| 123 |
---------------------- |
| 124 |
-- Specific_Handler -- |
| 125 |
---------------------- |
| 126 |
|
| 127 |
function Specific_Handler |
| 128 |
(T : Ada.Task_Identification.Task_Id) return Termination_Handler |
| 129 |
is |
| 130 |
begin |
| 131 |
-- Tasking_Error is raised if the task identified by T has already |
| 132 |
-- terminated. Program_Error is raised if the value of T is |
| 133 |
-- Null_Task_Id. |
| 134 |
|
| 135 |
if T = Ada.Task_Identification.Null_Task_Id then |
| 136 |
raise Program_Error; |
| 137 |
elsif Ada.Task_Identification.Is_Terminated (T) then |
| 138 |
raise Tasking_Error; |
| 139 |
else |
| 140 |
declare |
| 141 |
Target : constant System.Tasking.Task_Id := To_Task_Id (T); |
| 142 |
TH : Termination_Handler; |
| 143 |
|
| 144 |
begin |
| 145 |
SSL.Abort_Defer.all; |
| 146 |
STPO.Write_Lock (Target); |
| 147 |
|
| 148 |
TH := To_TT (Target.Common.Specific_Handler); |
| 149 |
|
| 150 |
STPO.Unlock (Target); |
| 151 |
SSL.Abort_Undefer.all; |
| 152 |
|
| 153 |
return TH; |
| 154 |
end; |
| 155 |
end if; |
| 156 |
end Specific_Handler; |
| 157 |
|
| 158 |
end Ada.Task_Termination; |