1------------------------------------------------------------------------------
2--                                                                          --
3--                        GNAT RUN-TIME COMPONENTS                          --
4--                                                                          --
5--              A D A . T A S K _ I D E N T I F I C A T I O N               --
6--                                                                          --
7--                                 B o d y                                  --
8--                                                                          --
9--          Copyright (C) 1992-2020, 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
32with System.Address_Image;
33with System.Soft_Links;
34with System.Task_Primitives;
35with System.Task_Primitives.Operations;
36with Ada.Unchecked_Conversion;
37
38pragma Warnings (Off);
39--  Allow withing of non-Preelaborated units in Ada 2005 mode where this
40--  package will be categorized as Preelaborate. See AI-362 for details.
41--  It is safe in the context of the run-time to violate the rules.
42
43with System.Tasking.Utilities;
44
45pragma Warnings (On);
46
47package body Ada.Task_Identification with
48  SPARK_Mode => Off
49is
50   package STPO renames System.Task_Primitives.Operations;
51
52   -----------------------
53   -- Local Subprograms --
54   -----------------------
55
56   function Convert_Ids (T : Task_Id) return System.Tasking.Task_Id;
57   function Convert_Ids (T : System.Tasking.Task_Id) return Task_Id;
58   pragma Inline (Convert_Ids);
59   --  Conversion functions between different forms of Task_Id
60
61   ---------
62   -- "=" --
63   ---------
64
65   function "=" (Left, Right : Task_Id) return Boolean is
66   begin
67      return System.Tasking."=" (Convert_Ids (Left), Convert_Ids (Right));
68   end "=";
69
70   -----------------
71   -- Abort_Task --
72   ----------------
73
74   procedure Abort_Task (T : Task_Id) is
75   begin
76      if T = Null_Task_Id then
77         raise Program_Error;
78      else
79         System.Tasking.Utilities.Abort_Tasks
80           (System.Tasking.Task_List'(1 => Convert_Ids (T)));
81      end if;
82   end Abort_Task;
83
84   ----------------------------
85   -- Activation_Is_Complete --
86   ----------------------------
87
88   function Activation_Is_Complete (T : Task_Id) return Boolean is
89      use type System.Tasking.Task_Id;
90   begin
91      if T = Null_Task_Id then
92         raise Program_Error;
93      else
94         return Convert_Ids (T).Common.Activator = null;
95      end if;
96   end Activation_Is_Complete;
97
98   -----------------
99   -- Convert_Ids --
100   -----------------
101
102   function Convert_Ids (T : Task_Id) return System.Tasking.Task_Id is
103   begin
104      return System.Tasking.Task_Id (T);
105   end Convert_Ids;
106
107   function Convert_Ids (T : System.Tasking.Task_Id) return Task_Id is
108   begin
109      return Task_Id (T);
110   end Convert_Ids;
111
112   ------------------
113   -- Current_Task --
114   ------------------
115
116   function Current_Task return Task_Id is
117   begin
118      return Convert_Ids (System.Task_Primitives.Operations.Self);
119   end Current_Task;
120
121   ----------------------
122   -- Environment_Task --
123   ----------------------
124
125   function Environment_Task return Task_Id is
126   begin
127      return Convert_Ids (System.Task_Primitives.Operations.Environment_Task);
128   end Environment_Task;
129
130   -----------
131   -- Image --
132   -----------
133
134   function Image (T : Task_Id) return String is
135      function To_Address is new
136        Ada.Unchecked_Conversion
137          (Task_Id, System.Task_Primitives.Task_Address);
138
139   begin
140      if T = Null_Task_Id then
141         return "";
142
143      elsif T.Common.Task_Image_Len = 0 then
144         return System.Address_Image (To_Address (T));
145
146      else
147         return T.Common.Task_Image (1 .. T.Common.Task_Image_Len)
148            & "_" &  System.Address_Image (To_Address (T));
149      end if;
150   end Image;
151
152   -----------------
153   -- Is_Callable --
154   -----------------
155
156   function Is_Callable (T : Task_Id) return Boolean is
157      Result : Boolean;
158      Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
159   begin
160      if T = Null_Task_Id then
161         raise Program_Error;
162      else
163         System.Soft_Links.Abort_Defer.all;
164         STPO.Write_Lock (Id);
165         Result := Id.Callable;
166         STPO.Unlock (Id);
167         System.Soft_Links.Abort_Undefer.all;
168
169         return Result;
170      end if;
171   end Is_Callable;
172
173   -------------------
174   -- Is_Terminated --
175   -------------------
176
177   function Is_Terminated (T : Task_Id) return Boolean is
178      Result : Boolean;
179      Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
180
181      use System.Tasking;
182
183   begin
184      if T = Null_Task_Id then
185         raise Program_Error;
186      else
187         System.Soft_Links.Abort_Defer.all;
188         STPO.Write_Lock (Id);
189         Result := Id.Common.State = Terminated;
190         STPO.Unlock (Id);
191         System.Soft_Links.Abort_Undefer.all;
192
193         return Result;
194      end if;
195   end Is_Terminated;
196
197end Ada.Task_Identification;
198