]>
Commit | Line | Data |
---|---|---|
84481f76 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS -- | |
4 | -- -- | |
5 | -- S Y S T E M . O S _ I N T E R F A C E -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
84481f76 RK |
9 | -- Copyright (C) 1997-2001 Florida State University -- |
10 | -- -- | |
11 | -- GNARL 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. GNARL 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 GNARL; see file COPYING. If not, write -- | |
19 | -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, -- | |
20 | -- MA 02111-1307, 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 | -- GNARL was developed by the GNARL team at Florida State University. It is -- | |
30 | -- now maintained by Ada Core Technologies Inc. in cooperation with Florida -- | |
31 | -- State University (http://www.gnat.com). -- | |
32 | -- -- | |
33 | ------------------------------------------------------------------------------ | |
34 | ||
35 | -- This is a FSU Threads version of this package | |
36 | ||
37 | pragma Polling (Off); | |
38 | -- Turn off polling, we do not want ATC polling to take place during | |
39 | -- tasking operations. It causes infinite loops and other problems. | |
40 | ||
41 | with Interfaces.C; | |
42 | ||
43 | package body System.OS_Interface is | |
44 | ||
45 | use Interfaces.C; | |
46 | ||
47 | ----------------- | |
48 | -- To_Duration -- | |
49 | ----------------- | |
50 | ||
51 | function To_Duration (TS : timespec) return Duration is | |
52 | begin | |
53 | return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9; | |
54 | end To_Duration; | |
55 | ||
56 | function To_Duration (TV : struct_timeval) return Duration is | |
57 | begin | |
58 | return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6; | |
59 | end To_Duration; | |
60 | ||
61 | ----------------- | |
62 | -- To_Timespec -- | |
63 | ----------------- | |
64 | ||
65 | function To_Timespec (D : Duration) return timespec is | |
66 | S : time_t; | |
67 | F : Duration; | |
68 | ||
69 | begin | |
70 | S := time_t (Long_Long_Integer (D)); | |
71 | F := D - Duration (S); | |
72 | ||
73 | -- If F has negative value due to a round-up, adjust for positive F | |
74 | -- value. | |
75 | ||
76 | if F < 0.0 then | |
77 | S := S - 1; | |
78 | F := F + 1.0; | |
79 | end if; | |
80 | ||
81 | return timespec' (tv_sec => S, | |
82 | tv_nsec => long (Long_Long_Integer (F * 10#1#E9))); | |
83 | end To_Timespec; | |
84 | ||
85 | ---------------- | |
86 | -- To_Timeval -- | |
87 | ---------------- | |
88 | ||
89 | function To_Timeval (D : Duration) return struct_timeval is | |
90 | S : long; | |
91 | F : Duration; | |
92 | ||
93 | begin | |
94 | S := long (Long_Long_Integer (D)); | |
95 | F := D - Duration (S); | |
96 | ||
97 | -- If F has negative value due to a round-up, adjust for positive F | |
98 | -- value. | |
99 | ||
100 | if F < 0.0 then | |
101 | S := S - 1; | |
102 | F := F + 1.0; | |
103 | end if; | |
104 | ||
105 | return struct_timeval' (tv_sec => S, | |
106 | tv_usec => long (Long_Long_Integer (F * 10#1#E6))); | |
107 | end To_Timeval; | |
108 | ||
109 | ------------- | |
110 | -- sigwait -- | |
111 | ------------- | |
112 | ||
113 | -- FSU_THREADS has a nonstandard sigwait | |
114 | ||
115 | function sigwait | |
116 | (set : access sigset_t; | |
117 | sig : access Signal) return int | |
118 | is | |
119 | Result : int; | |
120 | ||
121 | function sigwait_base (set : access sigset_t) return int; | |
122 | pragma Import (C, sigwait_base, "sigwait"); | |
123 | ||
124 | begin | |
125 | Result := sigwait_base (set); | |
126 | ||
127 | if Result = -1 then | |
128 | sig.all := 0; | |
129 | return errno; | |
130 | end if; | |
131 | ||
132 | sig.all := Signal (Result); | |
133 | return 0; | |
134 | end sigwait; | |
135 | ||
136 | ------------------------ | |
137 | -- pthread_mutex_lock -- | |
138 | ------------------------ | |
139 | ||
140 | -- FSU_THREADS has nonstandard pthread_mutex_lock and unlock. | |
141 | -- It sets errno but the standard Posix requires it to be returned. | |
142 | ||
143 | function pthread_mutex_lock (mutex : access pthread_mutex_t) return int is | |
144 | function pthread_mutex_lock_base | |
145 | (mutex : access pthread_mutex_t) return int; | |
146 | pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock"); | |
147 | ||
148 | Result : int; | |
149 | ||
150 | begin | |
151 | Result := pthread_mutex_lock_base (mutex); | |
152 | ||
153 | if Result /= 0 then | |
154 | return errno; | |
155 | end if; | |
156 | ||
157 | return 0; | |
158 | end pthread_mutex_lock; | |
159 | ||
160 | -------------------------- | |
161 | -- pthread_mutex_unlock -- | |
162 | -------------------------- | |
163 | ||
164 | function pthread_mutex_unlock | |
165 | (mutex : access pthread_mutex_t) return int | |
166 | is | |
167 | function pthread_mutex_unlock_base | |
168 | (mutex : access pthread_mutex_t) return int; | |
169 | pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock"); | |
170 | ||
171 | Result : int; | |
172 | ||
173 | begin | |
174 | Result := pthread_mutex_unlock_base (mutex); | |
175 | ||
176 | if Result /= 0 then | |
177 | return errno; | |
178 | end if; | |
179 | ||
180 | return 0; | |
181 | end pthread_mutex_unlock; | |
182 | ||
183 | ----------------------- | |
184 | -- pthread_cond_wait -- | |
185 | ----------------------- | |
186 | ||
187 | -- FSU_THREADS has a nonstandard pthread_cond_wait. | |
188 | -- The FSU_THREADS version returns EINTR when interrupted. | |
189 | ||
190 | function pthread_cond_wait | |
191 | (cond : access pthread_cond_t; | |
192 | mutex : access pthread_mutex_t) return int | |
193 | is | |
194 | function pthread_cond_wait_base | |
195 | (cond : access pthread_cond_t; | |
196 | mutex : access pthread_mutex_t) return int; | |
197 | pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait"); | |
198 | ||
199 | Result : int; | |
200 | ||
201 | begin | |
202 | Result := pthread_cond_wait_base (cond, mutex); | |
203 | ||
204 | if Result = EINTR then | |
205 | return 0; | |
206 | else | |
207 | return Result; | |
208 | end if; | |
209 | end pthread_cond_wait; | |
210 | ||
211 | ---------------------------- | |
212 | -- pthread_cond_timedwait -- | |
213 | ---------------------------- | |
214 | ||
215 | -- FSU_THREADS has a nonstandard pthread_cond_timedwait. The | |
216 | -- FSU_THREADS version returns -1 and set errno to EAGAIN for timeout. | |
217 | ||
218 | function pthread_cond_timedwait | |
219 | (cond : access pthread_cond_t; | |
220 | mutex : access pthread_mutex_t; | |
221 | abstime : access timespec) return int | |
222 | is | |
223 | function pthread_cond_timedwait_base | |
224 | (cond : access pthread_cond_t; | |
225 | mutex : access pthread_mutex_t; | |
226 | abstime : access timespec) return int; | |
227 | pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait"); | |
228 | ||
229 | Result : int; | |
230 | ||
231 | begin | |
232 | Result := pthread_cond_timedwait_base (cond, mutex, abstime); | |
233 | ||
234 | if Result = -1 then | |
235 | if errno = EAGAIN then | |
236 | return ETIMEDOUT; | |
237 | else | |
238 | return EINVAL; | |
239 | end if; | |
240 | end if; | |
241 | ||
242 | return 0; | |
243 | end pthread_cond_timedwait; | |
244 | ||
245 | --------------------------- | |
246 | -- pthread_setschedparam -- | |
247 | --------------------------- | |
248 | ||
249 | -- FSU_THREADS does not have pthread_setschedparam | |
250 | ||
251 | -- This routine returns a non-negative value upon failure | |
252 | -- but the error code can not be set conforming the POSIX standard. | |
253 | ||
254 | function pthread_setschedparam | |
255 | (thread : pthread_t; | |
256 | policy : int; | |
257 | param : access struct_sched_param) return int | |
258 | is | |
259 | function pthread_setschedattr | |
260 | (thread : pthread_t; | |
261 | attr : pthread_attr_t) return int; | |
262 | pragma Import (C, pthread_setschedattr, "pthread_setschedattr"); | |
263 | ||
264 | attr : aliased pthread_attr_t; | |
265 | Result : int; | |
266 | ||
267 | begin | |
268 | Result := pthread_attr_init (attr'Access); | |
269 | ||
270 | if Result /= 0 then | |
271 | return Result; | |
272 | end if; | |
273 | ||
274 | attr.sched := policy; | |
275 | ||
276 | -- Short-cut around pthread_attr_setprio | |
277 | ||
278 | attr.prio := param.sched_priority; | |
279 | ||
280 | Result := pthread_setschedattr (thread, attr); | |
281 | ||
282 | if Result /= 0 then | |
283 | return Result; | |
284 | end if; | |
285 | ||
286 | Result := pthread_attr_destroy (attr'Access); | |
287 | ||
288 | if Result /= 0 then | |
289 | return Result; | |
290 | else | |
291 | return 0; | |
292 | end if; | |
293 | end pthread_setschedparam; | |
294 | ||
295 | ------------------------- | |
296 | -- pthread_getspecific -- | |
297 | ------------------------- | |
298 | ||
299 | -- FSU_THREADS has a nonstandard pthread_getspecific | |
300 | ||
301 | function pthread_getspecific (key : pthread_key_t) return System.Address is | |
302 | function pthread_getspecific_base | |
303 | (key : pthread_key_t; | |
304 | value : access System.Address) return int; | |
305 | pragma Import (C, pthread_getspecific_base, "pthread_getspecific"); | |
306 | ||
307 | Tmp : aliased System.Address; | |
308 | Result : int; | |
309 | ||
310 | begin | |
311 | Result := pthread_getspecific_base (key, Tmp'Access); | |
312 | ||
313 | if Result /= 0 then | |
314 | return System.Null_Address; | |
315 | end if; | |
316 | ||
317 | return Tmp; | |
318 | end pthread_getspecific; | |
319 | ||
320 | --------------------------------- | |
321 | -- pthread_attr_setdetachstate -- | |
322 | --------------------------------- | |
323 | ||
324 | function pthread_attr_setdetachstate | |
325 | (attr : access pthread_attr_t; | |
326 | detachstate : int) return int | |
327 | is | |
328 | function pthread_attr_setdetachstate_base | |
329 | (attr : access pthread_attr_t; | |
330 | detachstate : access int) return int; | |
331 | pragma Import | |
332 | (C, pthread_attr_setdetachstate_base, "pthread_attr_setdetachstate"); | |
333 | ||
334 | Tmp : aliased int := detachstate; | |
335 | ||
336 | begin | |
337 | return pthread_attr_setdetachstate_base (attr, Tmp'Access); | |
338 | end pthread_attr_setdetachstate; | |
339 | ||
340 | ----------------- | |
341 | -- sched_yield -- | |
342 | ----------------- | |
343 | ||
344 | -- FSU_THREADS does not have sched_yield; | |
345 | ||
346 | function sched_yield return int is | |
347 | procedure sched_yield_base (arg : System.Address); | |
348 | pragma Import (C, sched_yield_base, "pthread_yield"); | |
349 | ||
350 | begin | |
351 | sched_yield_base (System.Null_Address); | |
352 | return 0; | |
353 | end sched_yield; | |
354 | ||
355 | ---------------- | |
356 | -- Stack_Base -- | |
357 | ---------------- | |
358 | ||
359 | function Get_Stack_Base (thread : pthread_t) return Address is | |
360 | begin | |
361 | return thread.stack_base; | |
362 | end Get_Stack_Base; | |
363 | ||
364 | end System.OS_Interface; |