OSDN Git Service

2003-01-29 Joel Sherrill <joel@OARcorp.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / 5rtpopsp.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
4 --                                                                          --
5 --    S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S .   --
6 --                              S P E C I F I C                             --
7 --                                                                          --
8 --                                  B o d y                                 --
9 --                                                                          --
10 --                             $Revision: 1.7 $
11 --                                                                          --
12 --            Copyright (C) 1991-2003, Florida State University             --
13 --                                                                          --
14 -- GNARL is free software; you can  redistribute it  and/or modify it under --
15 -- terms of the  GNU General Public License as published  by the Free Soft- --
16 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
17 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
20 -- for  more details.  You should have  received  a copy of the GNU General --
21 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
22 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
23 -- MA 02111-1307, USA.                                                      --
24 --                                                                          --
25 -- As a special exception,  if other files  instantiate  generics from this --
26 -- unit, or you link  this unit with other files  to produce an executable, --
27 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
28 -- covered  by the  GNU  General  Public  License.  This exception does not --
29 -- however invalidate  any other reasons why  the executable file  might be --
30 -- covered by the  GNU Public License.                                      --
31 --                                                                          --
32 -- GNARL was developed by the GNARL team at Florida State University. It is --
33 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
34 -- State University (http://www.gnat.com).                                  --
35 --                                                                          --
36 ------------------------------------------------------------------------------
37
38 --  This is a POSIX version of this package where foreign threads are
39 --  recognized.
40 --  Currently, DEC Unix, SCO UnixWare 7 and RTEMS use this version.
41
42 with System.Soft_Links;
43 --  used to initialize TSD for a C thread, in function Self
44
45 separate (System.Task_Primitives.Operations)
46 package body Specific is
47
48    ------------------
49    --  Local Data  --
50    ------------------
51
52    --  The followings are logically constants, but need to be initialized
53    --  at run time.
54
55    --  The following gives the Ada run-time direct access to a variable
56    --  context switched by RTEMS at the lowest level.
57
58    RTEMS_Ada_Self : System.Address;
59    pragma Import (C, RTEMS_Ada_Self, "rtems_ada_self");
60
61    --  The following are used to allow the Self function to
62    --  automatically generate ATCB's for C threads that happen to call
63    --  Ada procedure, which in turn happen to call the Ada runtime system.
64
65    type Fake_ATCB;
66    type Fake_ATCB_Ptr is access Fake_ATCB;
67    type Fake_ATCB is record
68       Stack_Base : Interfaces.C.unsigned := 0;
69       --  A value of zero indicates the node is not in use.
70       Next : Fake_ATCB_Ptr;
71       Real_ATCB : aliased Ada_Task_Control_Block (0);
72    end record;
73
74    Fake_ATCB_List : Fake_ATCB_Ptr;
75    --  A linear linked list.
76    --  The list is protected by All_Tasks_L;
77    --  Nodes are added to this list from the front.
78    --  Once a node is added to this list, it is never removed.
79
80    Fake_Task_Elaborated : aliased Boolean := True;
81    --  Used to identified fake tasks (i.e., non-Ada Threads).
82
83    Next_Fake_ATCB : Fake_ATCB_Ptr;
84    --  Used to allocate one Fake_ATCB in advance. See comment in New_Fake_ATCB
85
86    -----------------------
87    -- Local Subprograms --
88    -----------------------
89
90    ---------------------------------
91    --  Support for New_Fake_ATCB  --
92    ---------------------------------
93
94    function New_Fake_ATCB return Task_ID;
95    --  Allocate and Initialize a new ATCB. This code can safely be called from
96    --  a foreign thread, as it doesn't access implicitely or explicitely
97    --  "self" before having initialized the new ATCB.
98
99    -------------------
100    -- New_Fake_ATCB --
101    -------------------
102
103    function New_Fake_ATCB return Task_ID is
104       Self_ID   : Task_ID;
105       P, Q      : Fake_ATCB_Ptr;
106       Succeeded : Boolean;
107
108    begin
109       --  This section is ticklish.
110       --  We dare not call anything that might require an ATCB, until
111       --  we have the new ATCB in place.
112
113       Write_Lock (All_Tasks_L'Access);
114       Q := null;
115       P := Fake_ATCB_List;
116
117       while P /= null loop
118          if P.Stack_Base = 0 then
119             Q := P;
120          end if;
121
122          P := P.Next;
123       end loop;
124
125       if Q = null then
126
127          --  Create a new ATCB with zero entries.
128
129          Self_ID := Next_Fake_ATCB.Real_ATCB'Access;
130          Next_Fake_ATCB.Stack_Base := 1;
131          Next_Fake_ATCB.Next := Fake_ATCB_List;
132          Fake_ATCB_List := Next_Fake_ATCB;
133          Next_Fake_ATCB := null;
134
135       else
136          --  Reuse an existing fake ATCB.
137
138          Self_ID := Q.Real_ATCB'Access;
139          Q.Stack_Base := 1;
140       end if;
141
142       --  Record this as the Task_ID for the current thread.
143
144       Self_ID.Common.LL.Thread := pthread_self;
145
146       RTEMS_Ada_Self := To_Address (Self_ID);
147
148       --  Do the standard initializations
149
150       System.Tasking.Initialize_ATCB
151         (Self_ID, null, Null_Address, Null_Task, Fake_Task_Elaborated'Access,
152          System.Priority'First, Task_Info.Unspecified_Task_Info, 0, Self_ID,
153          Succeeded);
154       pragma Assert (Succeeded);
155
156       --  Finally, it is safe to use an allocator in this thread.
157
158       if Next_Fake_ATCB = null then
159          Next_Fake_ATCB := new Fake_ATCB;
160       end if;
161
162       Self_ID.Common.State := Runnable;
163       Self_ID.Awake_Count := 1;
164
165       --  Since this is not an ordinary Ada task, we will start out undeferred
166
167       Self_ID.Deferral_Level := 0;
168
169       System.Soft_Links.Create_TSD (Self_ID.Common.Compiler_Data);
170
171       --  ????
172       --  The following call is commented out to avoid dependence on
173       --  the System.Tasking.Initialization package.
174       --  It seems that if we want Ada.Task_Attributes to work correctly
175       --  for C threads we will need to raise the visibility of this soft
176       --  link to System.Soft_Links.
177       --  We are putting that off until this new functionality is otherwise
178       --  stable.
179       --  System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
180
181       for J in Known_Tasks'Range loop
182          if Known_Tasks (J) = null then
183             Known_Tasks (J) := Self_ID;
184             Self_ID.Known_Tasks_Index := J;
185             exit;
186          end if;
187       end loop;
188
189       --  Must not unlock until Next_ATCB is again allocated.
190
191       Unlock (All_Tasks_L'Access);
192       return Self_ID;
193    end New_Fake_ATCB;
194
195    ----------------
196    -- Initialize --
197    ----------------
198
199    procedure Initialize (Environment_Task : Task_ID) is
200
201    begin
202       RTEMS_Ada_Self := To_Address (Environment_Task);
203
204       --  Create a free ATCB for use on the Fake_ATCB_List.
205
206       Next_Fake_ATCB := new Fake_ATCB;
207    end Initialize;
208
209    ---------
210    -- Set --
211    ---------
212
213    procedure Set (Self_Id : Task_ID) is
214
215    begin
216       RTEMS_Ada_Self := To_Address (Self_Id);
217    end Set;
218
219    ----------
220    -- Self --
221    ----------
222
223    --  To make Ada tasks and C threads interoperate better, we have
224    --  added some functionality to Self.  Suppose a C main program
225    --  (with threads) calls an Ada procedure and the Ada procedure
226    --  calls the tasking runtime system.  Eventually, a call will be
227    --  made to self.  Since the call is not coming from an Ada task,
228    --  there will be no corresponding ATCB.
229
230    --  (The entire Ada run-time system may not have been elaborated,
231    --  either, but that is a different problem, that we will need to
232    --  solve another way.)
233
234    --  What we do in Self is to catch references that do not come
235    --  from recognized Ada tasks, and create an ATCB for the calling
236    --  thread.
237
238    --  The new ATCB will be "detached" from the normal Ada task
239    --  master hierarchy, much like the existing implicitly created
240    --  signal-server tasks.
241
242    --  We will also use such points to poll for disappearance of the
243    --  threads associated with any implicit ATCBs that we created
244    --  earlier, and take the opportunity to recover them.
245
246    --  A nasty problem here is the limitations of the compilation
247    --  order dependency, and in particular the GNARL/GNULLI layering.
248    --  To initialize an ATCB we need to assume System.Tasking has
249    --  been elaborated.
250
251    function Self return Task_ID is
252       Result : System.Address;
253
254    begin
255       Result := RTEMS_Ada_Self;
256
257       --  If the key value is Null, then it is a non-Ada task.
258
259       if Result = System.Null_Address then
260          return New_Fake_ATCB;
261       end if;
262
263       return To_Task_ID (Result);
264    end Self;
265
266 end Specific;