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-2009, 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 is
49
50   use System.Parameters;
51
52   package STPO renames System.Task_Primitives.Operations;
53
54   -----------------------
55   -- Local Subprograms --
56   -----------------------
57
58   function Convert_Ids (T : Task_Id) return System.Tasking.Task_Id;
59   function Convert_Ids (T : System.Tasking.Task_Id) return Task_Id;
60   pragma Inline (Convert_Ids);
61   --  Conversion functions between different forms of Task_Id
62
63   ---------
64   -- "=" --
65   ---------
66
67   function "=" (Left, Right : Task_Id) return Boolean is
68   begin
69      return System.Tasking."=" (Convert_Ids (Left), Convert_Ids (Right));
70   end "=";
71
72   -----------------
73   -- Abort_Task --
74   ----------------
75
76   procedure Abort_Task (T : Task_Id) is
77   begin
78      if T = Null_Task_Id then
79         raise Program_Error;
80      else
81         System.Tasking.Utilities.Abort_Tasks
82           (System.Tasking.Task_List'(1 => Convert_Ids (T)));
83      end if;
84   end Abort_Task;
85
86   -----------------
87   -- Convert_Ids --
88   -----------------
89
90   function Convert_Ids (T : Task_Id) return System.Tasking.Task_Id is
91   begin
92      return System.Tasking.Task_Id (T);
93   end Convert_Ids;
94
95   function Convert_Ids (T : System.Tasking.Task_Id) return Task_Id is
96   begin
97      return Task_Id (T);
98   end Convert_Ids;
99
100   ------------------
101   -- Current_Task --
102   ------------------
103
104   function Current_Task return Task_Id is
105   begin
106      return Convert_Ids (System.Task_Primitives.Operations.Self);
107   end Current_Task;
108
109   -----------
110   -- Image --
111   -----------
112
113   function Image (T : Task_Id) return String is
114      function To_Address is new
115        Ada.Unchecked_Conversion
116          (Task_Id, System.Task_Primitives.Task_Address);
117
118   begin
119      if T = Null_Task_Id then
120         return "";
121
122      elsif T.Common.Task_Image_Len = 0 then
123         return System.Address_Image (To_Address (T));
124
125      else
126         return T.Common.Task_Image (1 .. T.Common.Task_Image_Len)
127            & "_" &  System.Address_Image (To_Address (T));
128      end if;
129   end Image;
130
131   -----------------
132   -- Is_Callable --
133   -----------------
134
135   function Is_Callable (T : Task_Id) return Boolean is
136      Result : Boolean;
137      Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
138   begin
139      if T = Null_Task_Id then
140         raise Program_Error;
141      else
142         System.Soft_Links.Abort_Defer.all;
143
144         if Single_Lock then
145            STPO.Lock_RTS;
146         end if;
147
148         STPO.Write_Lock (Id);
149         Result := Id.Callable;
150         STPO.Unlock (Id);
151
152         if Single_Lock then
153            STPO.Unlock_RTS;
154         end if;
155
156         System.Soft_Links.Abort_Undefer.all;
157         return Result;
158      end if;
159   end Is_Callable;
160
161   -------------------
162   -- Is_Terminated --
163   -------------------
164
165   function Is_Terminated (T : Task_Id) return Boolean is
166      Result : Boolean;
167      Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
168
169      use System.Tasking;
170
171   begin
172      if T = Null_Task_Id then
173         raise Program_Error;
174      else
175         System.Soft_Links.Abort_Defer.all;
176
177         if Single_Lock then
178            STPO.Lock_RTS;
179         end if;
180
181         STPO.Write_Lock (Id);
182         Result := Id.Common.State = Terminated;
183         STPO.Unlock (Id);
184
185         if Single_Lock then
186            STPO.Unlock_RTS;
187         end if;
188
189         System.Soft_Links.Abort_Undefer.all;
190         return Result;
191      end if;
192   end Is_Terminated;
193
194end Ada.Task_Identification;
195