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