OSDN Git Service

2007-04-20 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-taside.adb
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-2007, 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 2,  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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 with System.Address_Image;
35 with System.Parameters;
36 with System.Soft_Links;
37 with System.Task_Primitives.Operations;
38 with Ada.Unchecked_Conversion;
39
40 pragma Warnings (Off);
41 --  Allow withing of non-Preelaborated units in Ada 2005 mode where this
42 --  package will be categorized as Preelaborate. See AI-362 for details.
43 --  It is safe in the context of the run-time to violate the rules!
44
45 with System.Tasking.Utilities;
46 --  Used for Abort_Tasks
47
48 pragma Warnings (On);
49
50 package body Ada.Task_Identification is
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    -- Convert_Ids --
90    -----------------
91
92    function Convert_Ids (T : Task_Id) return System.Tasking.Task_Id is
93    begin
94       return System.Tasking.Task_Id (T);
95    end Convert_Ids;
96
97    function Convert_Ids (T : System.Tasking.Task_Id) return Task_Id is
98    begin
99       return Task_Id (T);
100    end Convert_Ids;
101
102    ------------------
103    -- Current_Task --
104    ------------------
105
106    function Current_Task return Task_Id is
107    begin
108       return Convert_Ids (System.Task_Primitives.Operations.Self);
109    end Current_Task;
110
111    -----------
112    -- Image --
113    -----------
114
115    function Image (T : Task_Id) return String is
116       function To_Address is new
117         Ada.Unchecked_Conversion (Task_Id, System.Address);
118
119    begin
120       if T = Null_Task_Id then
121          return "";
122
123       elsif T.Common.Task_Image_Len = 0 then
124          return System.Address_Image (To_Address (T));
125
126       else
127          return T.Common.Task_Image (1 .. T.Common.Task_Image_Len)
128             & "_" &  System.Address_Image (To_Address (T));
129       end if;
130    end Image;
131
132    -----------------
133    -- Is_Callable --
134    -----------------
135
136    function Is_Callable (T : Task_Id) return Boolean is
137       Result : Boolean;
138       Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
139    begin
140       if T = Null_Task_Id then
141          raise Program_Error;
142       else
143          System.Soft_Links.Abort_Defer.all;
144
145          if Single_Lock then
146             STPO.Lock_RTS;
147          end if;
148
149          STPO.Write_Lock (Id);
150          Result := Id.Callable;
151          STPO.Unlock (Id);
152
153          if Single_Lock then
154             STPO.Unlock_RTS;
155          end if;
156
157          System.Soft_Links.Abort_Undefer.all;
158          return Result;
159       end if;
160    end Is_Callable;
161
162    -------------------
163    -- Is_Terminated --
164    -------------------
165
166    function Is_Terminated (T : Task_Id) return Boolean is
167       Result : Boolean;
168       Id     : constant System.Tasking.Task_Id := Convert_Ids (T);
169
170       use System.Tasking;
171
172    begin
173       if T = Null_Task_Id then
174          raise Program_Error;
175       else
176          System.Soft_Links.Abort_Defer.all;
177
178          if Single_Lock then
179             STPO.Lock_RTS;
180          end if;
181
182          STPO.Write_Lock (Id);
183          Result := Id.Common.State = Terminated;
184          STPO.Unlock (Id);
185
186          if Single_Lock then
187             STPO.Unlock_RTS;
188          end if;
189
190          System.Soft_Links.Abort_Undefer.all;
191          return Result;
192       end if;
193    end Is_Terminated;
194
195 end Ada.Task_Identification;