Update concepts branch to revision 131834
[official-gcc.git] / gcc / ada / s-osinte-lynxos-3.adb
blob09cbfca99b7a57120c970c7321225e0fc939d5b9
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-2007, 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 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, 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 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is a LynxOS (Native) version of this package
36 pragma Polling (Off);
37 -- Turn off polling, we do not want ATC polling to take place during
38 -- tasking operations. It causes infinite loops and other problems.
40 package body System.OS_Interface is
42 use Interfaces.C;
44 -------------------
45 -- clock_gettime --
46 -------------------
48 function clock_gettime
49 (clock_id : clockid_t;
50 tp : access timespec)
51 return int
53 function clock_gettime_base
54 (clock_id : clockid_t;
55 tp : access timespec)
56 return int;
57 pragma Import (C, clock_gettime_base, "clock_gettime");
59 begin
60 if clock_gettime_base (clock_id, tp) /= 0 then
61 return errno;
62 end if;
64 return 0;
65 end clock_gettime;
67 -----------------
68 -- To_Duration --
69 -----------------
71 function To_Duration (TS : timespec) return Duration is
72 begin
73 return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9;
74 end To_Duration;
76 ------------------------
77 -- To_Target_Priority --
78 ------------------------
80 function To_Target_Priority
81 (Prio : System.Any_Priority) return Interfaces.C.int
83 begin
84 return Interfaces.C.int (Prio);
85 end To_Target_Priority;
87 -----------------
88 -- To_Timespec --
89 -----------------
91 function To_Timespec (D : Duration) return timespec is
92 S : time_t;
93 F : Duration;
95 begin
96 S := time_t (Long_Long_Integer (D));
97 F := D - Duration (S);
99 -- If F has negative value due to a round-up, adjust for positive F
100 -- value.
102 if F < 0.0 then
103 S := S - 1;
104 F := F + 1.0;
105 end if;
107 return timespec'(tv_sec => S,
108 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
109 end To_Timespec;
111 -------------------------
112 -- POSIX.1c Section 3 --
113 -------------------------
115 function sigwait
116 (set : access sigset_t;
117 sig : access Signal)
118 return int
120 function sigwait_base
121 (set : access sigset_t;
122 value : System.Address)
123 return Signal;
124 pragma Import (C, sigwait_base, "sigwait");
126 begin
127 sig.all := sigwait_base (set, Null_Address);
129 if sig.all = -1 then
130 return errno;
131 end if;
133 return 0;
134 end sigwait;
136 --------------------------
137 -- POSIX.1c Section 11 --
138 --------------------------
140 -- For all the following functions, LynxOS threads has the POSIX Draft 4
141 -- behavior; it sets errno but the standard Posix requires it to be
142 -- returned.
144 function pthread_mutexattr_init
145 (attr : access pthread_mutexattr_t)
146 return int
148 function pthread_mutexattr_create
149 (attr : access pthread_mutexattr_t)
150 return int;
151 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
153 begin
154 if pthread_mutexattr_create (attr) /= 0 then
155 return errno;
156 end if;
158 return 0;
159 end pthread_mutexattr_init;
161 function pthread_mutexattr_destroy
162 (attr : access pthread_mutexattr_t)
163 return int
165 function pthread_mutexattr_delete
166 (attr : access pthread_mutexattr_t)
167 return int;
168 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
170 begin
171 if pthread_mutexattr_delete (attr) /= 0 then
172 return errno;
173 end if;
175 return 0;
176 end pthread_mutexattr_destroy;
178 function pthread_mutex_init
179 (mutex : access pthread_mutex_t;
180 attr : access pthread_mutexattr_t)
181 return int
183 function pthread_mutex_init_base
184 (mutex : access pthread_mutex_t;
185 attr : pthread_mutexattr_t)
186 return int;
187 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
189 begin
190 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
191 return errno;
192 end if;
194 return 0;
195 end pthread_mutex_init;
197 function pthread_mutex_destroy
198 (mutex : access pthread_mutex_t)
199 return int
201 function pthread_mutex_destroy_base
202 (mutex : access pthread_mutex_t)
203 return int;
204 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
206 begin
207 if pthread_mutex_destroy_base (mutex) /= 0 then
208 return errno;
209 end if;
211 return 0;
212 end pthread_mutex_destroy;
214 function pthread_mutex_lock
215 (mutex : access pthread_mutex_t)
216 return int
218 function pthread_mutex_lock_base
219 (mutex : access pthread_mutex_t)
220 return int;
221 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
223 begin
224 if pthread_mutex_lock_base (mutex) /= 0 then
225 return errno;
226 end if;
228 return 0;
229 end pthread_mutex_lock;
231 function pthread_mutex_unlock
232 (mutex : access pthread_mutex_t)
233 return int
235 function pthread_mutex_unlock_base
236 (mutex : access pthread_mutex_t)
237 return int;
238 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
240 begin
241 if pthread_mutex_unlock_base (mutex) /= 0 then
242 return errno;
243 end if;
245 return 0;
246 end pthread_mutex_unlock;
248 function pthread_condattr_init
249 (attr : access pthread_condattr_t)
250 return int
252 function pthread_condattr_create
253 (attr : access pthread_condattr_t)
254 return int;
255 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
257 begin
258 if pthread_condattr_create (attr) /= 0 then
259 return errno;
260 end if;
262 return 0;
263 end pthread_condattr_init;
265 function pthread_condattr_destroy
266 (attr : access pthread_condattr_t)
267 return int
269 function pthread_condattr_delete
270 (attr : access pthread_condattr_t)
271 return int;
272 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
274 begin
275 if pthread_condattr_delete (attr) /= 0 then
276 return errno;
277 end if;
279 return 0;
280 end pthread_condattr_destroy;
282 function pthread_cond_init
283 (cond : access pthread_cond_t;
284 attr : access pthread_condattr_t)
285 return int
287 function pthread_cond_init_base
288 (cond : access pthread_cond_t;
289 attr : pthread_condattr_t)
290 return int;
291 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
293 begin
294 if pthread_cond_init_base (cond, attr.all) /= 0 then
295 return errno;
296 end if;
298 return 0;
299 end pthread_cond_init;
301 function pthread_cond_destroy
302 (cond : access pthread_cond_t)
303 return int
305 function pthread_cond_destroy_base
306 (cond : access pthread_cond_t)
307 return int;
308 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
310 begin
311 if pthread_cond_destroy_base (cond) /= 0 then
312 return errno;
313 end if;
315 return 0;
316 end pthread_cond_destroy;
318 function pthread_cond_signal
319 (cond : access pthread_cond_t)
320 return int
322 function pthread_cond_signal_base
323 (cond : access pthread_cond_t)
324 return int;
325 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
327 begin
328 if pthread_cond_signal_base (cond) /= 0 then
329 return errno;
330 end if;
332 return 0;
333 end pthread_cond_signal;
335 function pthread_cond_wait
336 (cond : access pthread_cond_t;
337 mutex : access pthread_mutex_t)
338 return int
340 function pthread_cond_wait_base
341 (cond : access pthread_cond_t;
342 mutex : access pthread_mutex_t)
343 return int;
344 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
346 begin
347 if pthread_cond_wait_base (cond, mutex) /= 0 then
348 return errno;
349 end if;
351 return 0;
352 end pthread_cond_wait;
354 function pthread_cond_timedwait
355 (cond : access pthread_cond_t;
356 mutex : access pthread_mutex_t;
357 reltime : access timespec) return int
359 function pthread_cond_timedwait_base
360 (cond : access pthread_cond_t;
361 mutex : access pthread_mutex_t;
362 reltime : access timespec) return int;
363 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
365 begin
366 if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
367 if errno = EAGAIN then
368 return ETIMEDOUT;
369 end if;
371 return errno;
372 end if;
374 return 0;
375 end pthread_cond_timedwait;
377 --------------------------
378 -- POSIX.1c Section 13 --
379 --------------------------
381 function pthread_setschedparam
382 (thread : pthread_t;
383 policy : int;
384 param : access struct_sched_param)
385 return int
387 function pthread_setscheduler
388 (thread : pthread_t;
389 policy : int;
390 prio : int)
391 return int;
392 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
394 begin
395 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
396 return errno;
397 end if;
399 return 0;
400 end pthread_setschedparam;
402 function pthread_mutexattr_setprotocol
403 (attr : access pthread_mutexattr_t;
404 protocol : int)
405 return int
407 pragma Unreferenced (attr, protocol);
408 begin
409 return 0;
410 end pthread_mutexattr_setprotocol;
412 function pthread_mutexattr_setprioceiling
413 (attr : access pthread_mutexattr_t;
414 prioceiling : int)
415 return int
417 pragma Unreferenced (attr, prioceiling);
418 begin
419 return 0;
420 end pthread_mutexattr_setprioceiling;
422 function pthread_attr_setscope
423 (attr : access pthread_attr_t;
424 contentionscope : int)
425 return int
427 pragma Unreferenced (attr, contentionscope);
428 begin
429 return 0;
430 end pthread_attr_setscope;
432 function sched_yield return int is
433 procedure pthread_yield;
434 pragma Import (C, pthread_yield, "pthread_yield");
436 begin
437 pthread_yield;
438 return 0;
439 end sched_yield;
441 -----------------------------
442 -- P1003.1c - Section 16 --
443 -----------------------------
445 function pthread_attr_setdetachstate
446 (attr : access pthread_attr_t;
447 detachstate : int)
448 return int
450 pragma Unreferenced (attr, detachstate);
451 begin
452 return 0;
453 end pthread_attr_setdetachstate;
455 function pthread_create
456 (thread : access pthread_t;
457 attributes : access pthread_attr_t;
458 start_routine : Thread_Body;
459 arg : System.Address)
460 return int
462 -- The LynxOS pthread_create doesn't seems to work.
463 -- Workaround : We're using st_new instead.
465 -- function pthread_create_base
466 -- (thread : access pthread_t;
467 -- attributes : pthread_attr_t;
468 -- start_routine : Thread_Body;
469 -- arg : System.Address)
470 -- return int;
471 -- pragma Import (C, pthread_create_base, "pthread_create");
473 St : aliased st_t := attributes.st;
475 function st_new
476 (start_routine : Thread_Body;
477 arg : System.Address;
478 attributes : access st_t;
479 thread : access pthread_t)
480 return int;
481 pragma Import (C, st_new, "st_new");
483 begin
484 -- Following code would be used if above commented function worked
486 -- if pthread_create_base
487 -- (thread, attributes.all, start_routine, arg) /= 0 then
489 if st_new (start_routine, arg, St'Access, thread) /= 0 then
490 return errno;
491 end if;
493 return 0;
494 end pthread_create;
496 function pthread_detach (thread : pthread_t) return int is
497 aliased_thread : aliased pthread_t := thread;
499 function pthread_detach_base (thread : access pthread_t) return int;
500 pragma Import (C, pthread_detach_base, "pthread_detach");
502 begin
503 if pthread_detach_base (aliased_thread'Access) /= 0 then
504 return errno;
505 end if;
507 return 0;
508 end pthread_detach;
510 --------------------------
511 -- POSIX.1c Section 17 --
512 --------------------------
514 function pthread_setspecific
515 (key : pthread_key_t;
516 value : System.Address)
517 return int
519 function pthread_setspecific_base
520 (key : pthread_key_t;
521 value : System.Address)
522 return int;
523 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
525 begin
526 if pthread_setspecific_base (key, value) /= 0 then
527 return errno;
528 end if;
530 return 0;
531 end pthread_setspecific;
533 function pthread_getspecific (key : pthread_key_t) return System.Address is
534 procedure pthread_getspecific_base
535 (key : pthread_key_t;
536 value : access System.Address);
537 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
539 value : aliased System.Address := System.Null_Address;
541 begin
542 pthread_getspecific_base (key, value'Unchecked_Access);
543 return value;
544 end pthread_getspecific;
546 function Get_Stack_Base (thread : pthread_t) return Address is
547 pragma Warnings (Off, thread);
549 begin
550 return Null_Address;
551 end Get_Stack_Base;
553 function pthread_key_create
554 (key : access pthread_key_t;
555 destructor : destructor_pointer)
556 return int
558 function pthread_keycreate
559 (key : access pthread_key_t;
560 destructor : destructor_pointer)
561 return int;
562 pragma Import (C, pthread_keycreate, "pthread_keycreate");
564 begin
565 if pthread_keycreate (key, destructor) /= 0 then
566 return errno;
567 end if;
569 return 0;
570 end pthread_key_create;
572 procedure pthread_init is
573 begin
574 null;
575 end pthread_init;
577 end System.OS_Interface;