fixing pr42337
[official-gcc.git] / gcc / ada / s-osinte-lynxos-3.adb
blob0a4a3deb46398f07d2e50ee9ce7b5b95cc67a696
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT 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 -- --
9 -- Copyright (C) 1999-2009, Free Software Foundation, Inc. --
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 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 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is a LynxOS (Native) version of this package
34 pragma Polling (Off);
35 -- Turn off polling, we do not want ATC polling to take place during
36 -- tasking operations. It causes infinite loops and other problems.
38 package body System.OS_Interface is
40 use Interfaces.C;
42 -------------------
43 -- clock_gettime --
44 -------------------
46 function clock_gettime
47 (clock_id : clockid_t;
48 tp : access timespec)
49 return int
51 function clock_gettime_base
52 (clock_id : clockid_t;
53 tp : access timespec)
54 return int;
55 pragma Import (C, clock_gettime_base, "clock_gettime");
57 begin
58 if clock_gettime_base (clock_id, tp) /= 0 then
59 return errno;
60 end if;
62 return 0;
63 end clock_gettime;
65 -----------------
66 -- To_Duration --
67 -----------------
69 function To_Duration (TS : timespec) return Duration is
70 begin
71 return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9;
72 end To_Duration;
74 ------------------------
75 -- To_Target_Priority --
76 ------------------------
78 function To_Target_Priority
79 (Prio : System.Any_Priority) return Interfaces.C.int
81 begin
82 return Interfaces.C.int (Prio);
83 end To_Target_Priority;
85 -----------------
86 -- To_Timespec --
87 -----------------
89 function To_Timespec (D : Duration) return timespec is
90 S : time_t;
91 F : Duration;
93 begin
94 S := time_t (Long_Long_Integer (D));
95 F := D - Duration (S);
97 -- If F has negative value due to a round-up, adjust for positive F
98 -- value.
100 if F < 0.0 then
101 S := S - 1;
102 F := F + 1.0;
103 end if;
105 return timespec'(tv_sec => S,
106 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
107 end To_Timespec;
109 -------------------------
110 -- POSIX.1c Section 3 --
111 -------------------------
113 function sigwait
114 (set : access sigset_t;
115 sig : access Signal)
116 return int
118 function sigwait_base
119 (set : access sigset_t;
120 value : System.Address)
121 return Signal;
122 pragma Import (C, sigwait_base, "sigwait");
124 begin
125 sig.all := sigwait_base (set, Null_Address);
127 if sig.all = -1 then
128 return errno;
129 end if;
131 return 0;
132 end sigwait;
134 --------------------------
135 -- POSIX.1c Section 11 --
136 --------------------------
138 -- For all the following functions, LynxOS threads has the POSIX Draft 4
139 -- behavior; it sets errno but the standard Posix requires it to be
140 -- returned.
142 function pthread_mutexattr_init
143 (attr : access pthread_mutexattr_t)
144 return int
146 function pthread_mutexattr_create
147 (attr : access pthread_mutexattr_t)
148 return int;
149 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
151 begin
152 if pthread_mutexattr_create (attr) /= 0 then
153 return errno;
154 end if;
156 return 0;
157 end pthread_mutexattr_init;
159 function pthread_mutexattr_destroy
160 (attr : access pthread_mutexattr_t)
161 return int
163 function pthread_mutexattr_delete
164 (attr : access pthread_mutexattr_t)
165 return int;
166 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
168 begin
169 if pthread_mutexattr_delete (attr) /= 0 then
170 return errno;
171 end if;
173 return 0;
174 end pthread_mutexattr_destroy;
176 function pthread_mutex_init
177 (mutex : access pthread_mutex_t;
178 attr : access pthread_mutexattr_t)
179 return int
181 function pthread_mutex_init_base
182 (mutex : access pthread_mutex_t;
183 attr : pthread_mutexattr_t)
184 return int;
185 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
187 begin
188 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
189 return errno;
190 end if;
192 return 0;
193 end pthread_mutex_init;
195 function pthread_mutex_destroy
196 (mutex : access pthread_mutex_t)
197 return int
199 function pthread_mutex_destroy_base
200 (mutex : access pthread_mutex_t)
201 return int;
202 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
204 begin
205 if pthread_mutex_destroy_base (mutex) /= 0 then
206 return errno;
207 end if;
209 return 0;
210 end pthread_mutex_destroy;
212 function pthread_mutex_lock
213 (mutex : access pthread_mutex_t)
214 return int
216 function pthread_mutex_lock_base
217 (mutex : access pthread_mutex_t)
218 return int;
219 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
221 begin
222 if pthread_mutex_lock_base (mutex) /= 0 then
223 return errno;
224 end if;
226 return 0;
227 end pthread_mutex_lock;
229 function pthread_mutex_unlock
230 (mutex : access pthread_mutex_t)
231 return int
233 function pthread_mutex_unlock_base
234 (mutex : access pthread_mutex_t)
235 return int;
236 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
238 begin
239 if pthread_mutex_unlock_base (mutex) /= 0 then
240 return errno;
241 end if;
243 return 0;
244 end pthread_mutex_unlock;
246 function pthread_condattr_init
247 (attr : access pthread_condattr_t)
248 return int
250 function pthread_condattr_create
251 (attr : access pthread_condattr_t)
252 return int;
253 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
255 begin
256 if pthread_condattr_create (attr) /= 0 then
257 return errno;
258 end if;
260 return 0;
261 end pthread_condattr_init;
263 function pthread_condattr_destroy
264 (attr : access pthread_condattr_t)
265 return int
267 function pthread_condattr_delete
268 (attr : access pthread_condattr_t)
269 return int;
270 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
272 begin
273 if pthread_condattr_delete (attr) /= 0 then
274 return errno;
275 end if;
277 return 0;
278 end pthread_condattr_destroy;
280 function pthread_cond_init
281 (cond : access pthread_cond_t;
282 attr : access pthread_condattr_t)
283 return int
285 function pthread_cond_init_base
286 (cond : access pthread_cond_t;
287 attr : pthread_condattr_t)
288 return int;
289 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
291 begin
292 if pthread_cond_init_base (cond, attr.all) /= 0 then
293 return errno;
294 end if;
296 return 0;
297 end pthread_cond_init;
299 function pthread_cond_destroy
300 (cond : access pthread_cond_t)
301 return int
303 function pthread_cond_destroy_base
304 (cond : access pthread_cond_t)
305 return int;
306 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
308 begin
309 if pthread_cond_destroy_base (cond) /= 0 then
310 return errno;
311 end if;
313 return 0;
314 end pthread_cond_destroy;
316 function pthread_cond_signal
317 (cond : access pthread_cond_t)
318 return int
320 function pthread_cond_signal_base
321 (cond : access pthread_cond_t)
322 return int;
323 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
325 begin
326 if pthread_cond_signal_base (cond) /= 0 then
327 return errno;
328 end if;
330 return 0;
331 end pthread_cond_signal;
333 function pthread_cond_wait
334 (cond : access pthread_cond_t;
335 mutex : access pthread_mutex_t)
336 return int
338 function pthread_cond_wait_base
339 (cond : access pthread_cond_t;
340 mutex : access pthread_mutex_t)
341 return int;
342 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
344 begin
345 if pthread_cond_wait_base (cond, mutex) /= 0 then
346 return errno;
347 end if;
349 return 0;
350 end pthread_cond_wait;
352 function pthread_cond_timedwait
353 (cond : access pthread_cond_t;
354 mutex : access pthread_mutex_t;
355 reltime : access timespec) return int
357 function pthread_cond_timedwait_base
358 (cond : access pthread_cond_t;
359 mutex : access pthread_mutex_t;
360 reltime : access timespec) return int;
361 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
363 begin
364 if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
365 if errno = EAGAIN then
366 return ETIMEDOUT;
367 end if;
369 return errno;
370 end if;
372 return 0;
373 end pthread_cond_timedwait;
375 --------------------------
376 -- POSIX.1c Section 13 --
377 --------------------------
379 function pthread_setschedparam
380 (thread : pthread_t;
381 policy : int;
382 param : access struct_sched_param)
383 return int
385 function pthread_setscheduler
386 (thread : pthread_t;
387 policy : int;
388 prio : int)
389 return int;
390 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
392 begin
393 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
394 return errno;
395 end if;
397 return 0;
398 end pthread_setschedparam;
400 function pthread_mutexattr_setprotocol
401 (attr : access pthread_mutexattr_t;
402 protocol : int)
403 return int
405 pragma Unreferenced (attr, protocol);
406 begin
407 return 0;
408 end pthread_mutexattr_setprotocol;
410 function pthread_mutexattr_setprioceiling
411 (attr : access pthread_mutexattr_t;
412 prioceiling : int)
413 return int
415 pragma Unreferenced (attr, prioceiling);
416 begin
417 return 0;
418 end pthread_mutexattr_setprioceiling;
420 function pthread_attr_setscope
421 (attr : access pthread_attr_t;
422 contentionscope : int)
423 return int
425 pragma Unreferenced (attr, contentionscope);
426 begin
427 return 0;
428 end pthread_attr_setscope;
430 function sched_yield return int is
431 procedure pthread_yield;
432 pragma Import (C, pthread_yield, "pthread_yield");
434 begin
435 pthread_yield;
436 return 0;
437 end sched_yield;
439 -----------------------------
440 -- P1003.1c - Section 16 --
441 -----------------------------
443 function pthread_attr_setdetachstate
444 (attr : access pthread_attr_t;
445 detachstate : int)
446 return int
448 pragma Unreferenced (attr, detachstate);
449 begin
450 return 0;
451 end pthread_attr_setdetachstate;
453 function pthread_create
454 (thread : access pthread_t;
455 attributes : access pthread_attr_t;
456 start_routine : Thread_Body;
457 arg : System.Address)
458 return int
460 -- The LynxOS pthread_create doesn't seems to work.
461 -- Workaround : We're using st_new instead.
463 -- function pthread_create_base
464 -- (thread : access pthread_t;
465 -- attributes : pthread_attr_t;
466 -- start_routine : Thread_Body;
467 -- arg : System.Address)
468 -- return int;
469 -- pragma Import (C, pthread_create_base, "pthread_create");
471 St : aliased st_t := attributes.st;
473 function st_new
474 (start_routine : Thread_Body;
475 arg : System.Address;
476 attributes : access st_t;
477 thread : access pthread_t)
478 return int;
479 pragma Import (C, st_new, "st_new");
481 begin
482 -- Following code would be used if above commented function worked
484 -- if pthread_create_base
485 -- (thread, attributes.all, start_routine, arg) /= 0 then
487 if st_new (start_routine, arg, St'Access, thread) /= 0 then
488 return errno;
489 end if;
491 return 0;
492 end pthread_create;
494 function pthread_detach (thread : pthread_t) return int is
495 aliased_thread : aliased pthread_t := thread;
497 function pthread_detach_base (thread : access pthread_t) return int;
498 pragma Import (C, pthread_detach_base, "pthread_detach");
500 begin
501 if pthread_detach_base (aliased_thread'Access) /= 0 then
502 return errno;
503 end if;
505 return 0;
506 end pthread_detach;
508 --------------------------
509 -- POSIX.1c Section 17 --
510 --------------------------
512 function pthread_setspecific
513 (key : pthread_key_t;
514 value : System.Address)
515 return int
517 function pthread_setspecific_base
518 (key : pthread_key_t;
519 value : System.Address)
520 return int;
521 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
523 begin
524 if pthread_setspecific_base (key, value) /= 0 then
525 return errno;
526 end if;
528 return 0;
529 end pthread_setspecific;
531 function pthread_getspecific (key : pthread_key_t) return System.Address is
532 procedure pthread_getspecific_base
533 (key : pthread_key_t;
534 value : access System.Address);
535 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
537 value : aliased System.Address := System.Null_Address;
539 begin
540 pthread_getspecific_base (key, value'Unchecked_Access);
541 return value;
542 end pthread_getspecific;
544 function Get_Stack_Base (thread : pthread_t) return Address is
545 pragma Warnings (Off, thread);
547 begin
548 return Null_Address;
549 end Get_Stack_Base;
551 function pthread_key_create
552 (key : access pthread_key_t;
553 destructor : destructor_pointer)
554 return int
556 function pthread_keycreate
557 (key : access pthread_key_t;
558 destructor : destructor_pointer)
559 return int;
560 pragma Import (C, pthread_keycreate, "pthread_keycreate");
562 begin
563 if pthread_keycreate (key, destructor) /= 0 then
564 return errno;
565 end if;
567 return 0;
568 end pthread_key_create;
570 procedure pthread_init is
571 begin
572 null;
573 end pthread_init;
575 end System.OS_Interface;