Add hppa-openbsd target
[official-gcc.git] / gcc / ada / 5atpopsp.adb
blob40e60a8f7fc1f5b2517d05d74c6b7b47cc23827e
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 -- --
11 -- Copyright (C) 1992-2001, Free Software Foundation, Inc. --
12 -- --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
23 -- --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
30 -- --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
33 -- --
34 ------------------------------------------------------------------------------
36 -- This is a POSIX version of this package where foreign threads are
37 -- recognized.
38 -- Currently, DEC Unix, SCO UnixWare, Solaris pthread, HPUX pthread,
39 -- GNU/Linux threads and RTEMS use this version.
41 with System.Task_Info;
42 -- Use for Unspecified_Task_Info
44 with System.Soft_Links;
45 -- used to initialize TSD for a C thread, in function Self
47 separate (System.Task_Primitives.Operations)
48 package body Specific is
50 ------------------
51 -- Local Data --
52 ------------------
54 -- The followings are logically constants, but need to be initialized
55 -- at run time.
57 ATCB_Key : aliased pthread_key_t;
58 -- Key used to find the Ada Task_ID associated with a thread
60 -- The following are used to allow the Self function to
61 -- automatically generate ATCB's for C threads that happen to call
62 -- Ada procedure, which in turn happen to call the Ada runtime system.
64 type Fake_ATCB;
65 type Fake_ATCB_Ptr is access Fake_ATCB;
66 type Fake_ATCB is record
67 Stack_Base : Interfaces.C.unsigned := 0;
68 -- A value of zero indicates the node is not in use.
69 Next : Fake_ATCB_Ptr;
70 Real_ATCB : aliased Ada_Task_Control_Block (0);
71 end record;
73 Fake_ATCB_List : Fake_ATCB_Ptr;
74 -- A linear linked list.
75 -- The list is protected by Single_RTS_Lock;
76 -- Nodes are added to this list from the front.
77 -- Once a node is added to this list, it is never removed.
79 Fake_Task_Elaborated : aliased Boolean := True;
80 -- Used to identified fake tasks (i.e., non-Ada Threads).
82 Next_Fake_ATCB : Fake_ATCB_Ptr;
83 -- Used to allocate one Fake_ATCB in advance. See comment in New_Fake_ATCB
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 ---------------------------------
90 -- Support for New_Fake_ATCB --
91 ---------------------------------
93 function New_Fake_ATCB return Task_ID;
94 -- Allocate and Initialize a new ATCB. This code can safely be called from
95 -- a foreign thread, as it doesn't access implicitly or explicitly
96 -- "self" before having initialized the new ATCB.
98 -------------------
99 -- New_Fake_ATCB --
100 -------------------
102 function New_Fake_ATCB return Task_ID is
103 Self_ID : Task_ID;
104 P, Q : Fake_ATCB_Ptr;
105 Succeeded : Boolean;
106 Result : Interfaces.C.int;
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.
113 Lock_RTS;
114 Q := null;
115 P := Fake_ATCB_List;
117 while P /= null loop
118 if P.Stack_Base = 0 then
119 Q := P;
120 end if;
122 P := P.Next;
123 end loop;
125 if Q = null then
127 -- Create a new ATCB with zero entries.
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;
135 else
136 -- Reuse an existing fake ATCB.
138 Self_ID := Q.Real_ATCB'Access;
139 Q.Stack_Base := 1;
140 end if;
142 -- Record this as the Task_ID for the current thread.
144 Self_ID.Common.LL.Thread := pthread_self;
145 Result := pthread_setspecific (ATCB_Key, To_Address (Self_ID));
146 pragma Assert (Result = 0);
148 -- Do the standard initializations
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);
156 -- Finally, it is safe to use an allocator in this thread.
158 if Next_Fake_ATCB = null then
159 Next_Fake_ATCB := new Fake_ATCB;
160 end if;
162 Self_ID.Master_of_Task := 0;
163 Self_ID.Master_Within := Self_ID.Master_of_Task + 1;
165 for L in Self_ID.Entry_Calls'Range loop
166 Self_ID.Entry_Calls (L).Self := Self_ID;
167 Self_ID.Entry_Calls (L).Level := L;
168 end loop;
170 Self_ID.Common.State := Runnable;
171 Self_ID.Awake_Count := 1;
173 -- Since this is not an ordinary Ada task, we will start out undeferred
175 Self_ID.Deferral_Level := 0;
177 System.Soft_Links.Create_TSD (Self_ID.Common.Compiler_Data);
179 -- ????
180 -- The following call is commented out to avoid dependence on
181 -- the System.Tasking.Initialization package.
182 -- It seems that if we want Ada.Task_Attributes to work correctly
183 -- for C threads we will need to raise the visibility of this soft
184 -- link to System.Soft_Links.
185 -- We are putting that off until this new functionality is otherwise
186 -- stable.
187 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
189 for J in Known_Tasks'Range loop
190 if Known_Tasks (J) = null then
191 Known_Tasks (J) := Self_ID;
192 Self_ID.Known_Tasks_Index := J;
193 exit;
194 end if;
195 end loop;
197 -- Must not unlock until Next_ATCB is again allocated.
199 Unlock_RTS;
200 return Self_ID;
201 end New_Fake_ATCB;
203 ----------------
204 -- Initialize --
205 ----------------
207 procedure Initialize (Environment_Task : Task_ID) is
208 Result : Interfaces.C.int;
209 begin
210 Result := pthread_key_create (ATCB_Key'Access, null);
211 pragma Assert (Result = 0);
212 Result := pthread_setspecific (ATCB_Key, To_Address (Environment_Task));
213 pragma Assert (Result = 0);
215 -- Create a free ATCB for use on the Fake_ATCB_List.
217 Next_Fake_ATCB := new Fake_ATCB;
218 end Initialize;
220 ---------
221 -- Set --
222 ---------
224 procedure Set (Self_Id : Task_ID) is
225 Result : Interfaces.C.int;
226 begin
227 Result := pthread_setspecific (ATCB_Key, To_Address (Self_Id));
228 pragma Assert (Result = 0);
229 end Set;
231 ----------
232 -- Self --
233 ----------
235 -- To make Ada tasks and C threads interoperate better, we have added some
236 -- functionality to Self. Suppose a C main program (with threads) calls an
237 -- Ada procedure and the Ada procedure calls the tasking runtime system.
238 -- Eventually, a call will be made to self. Since the call is not coming
239 -- from an Ada task, there will be no corresponding ATCB.
241 -- What we do in Self is to catch references that do not come from
242 -- recognized Ada tasks, and create an ATCB for the calling thread.
244 -- The new ATCB will be "detached" from the normal Ada task master
245 -- hierarchy, much like the existing implicitly created signal-server
246 -- tasks.
248 function Self return Task_ID is
249 Result : System.Address;
250 begin
251 Result := pthread_getspecific (ATCB_Key);
253 -- If the key value is Null, then it is a non-Ada task.
255 if Result = System.Null_Address then
256 return New_Fake_ATCB;
257 end if;
259 return To_Task_ID (Result);
260 end Self;
262 end Specific;