1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ I N T E R F A C E --
10 -- Copyright (C) 1999-2000 Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- This is a LynxOS (Native) version of this package
39 -- Turn off polling, we do not want ATC polling to take place during
40 -- tasking operations. It causes infinite loops and other problems.
44 package body System.OS_Interface is
52 function clock_gettime
53 (clock_id : clockid_t;
57 function clock_gettime_base
58 (clock_id : clockid_t;
61 pragma Import (C, clock_gettime_base, "clock_gettime");
64 if clock_gettime_base (clock_id, tp) /= 0 then
75 function To_Duration (TS : timespec) return Duration is
77 return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9;
80 function To_Duration (TV : struct_timeval) return Duration is
82 return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
89 function To_Timespec (D : Duration) return timespec is
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
105 return timespec' (tv_sec => S,
106 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
113 function To_Timeval (D : Duration) return struct_timeval is
118 S := time_t (Long_Long_Integer (D));
119 F := D - Duration (S);
121 -- If F has negative value due to a round-up, adjust for positive F
129 return struct_timeval' (tv_sec => S,
130 tv_usec => time_t (Long_Long_Integer (F * 10#1#E6)));
133 -------------------------
134 -- POSIX.1c Section 3 --
135 -------------------------
138 (set : access sigset_t;
142 function sigwait_base
143 (set : access sigset_t;
144 value : System.Address)
146 pragma Import (C, sigwait_base, "sigwait");
149 sig.all := sigwait_base (set, Null_Address);
158 --------------------------
159 -- POSIX.1c Section 11 --
160 --------------------------
162 -- For all the following functions, LynxOS threads has the POSIX Draft 4
163 -- begavior; it sets errno but the standard Posix requires it to be
166 function pthread_mutexattr_init
167 (attr : access pthread_mutexattr_t)
170 function pthread_mutexattr_create
171 (attr : access pthread_mutexattr_t)
173 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
176 if pthread_mutexattr_create (attr) /= 0 then
181 end pthread_mutexattr_init;
183 function pthread_mutexattr_destroy
184 (attr : access pthread_mutexattr_t)
187 function pthread_mutexattr_delete
188 (attr : access pthread_mutexattr_t)
190 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
193 if pthread_mutexattr_delete (attr) /= 0 then
198 end pthread_mutexattr_destroy;
200 function pthread_mutex_init
201 (mutex : access pthread_mutex_t;
202 attr : access pthread_mutexattr_t)
205 function pthread_mutex_init_base
206 (mutex : access pthread_mutex_t;
207 attr : pthread_mutexattr_t)
209 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
212 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
217 end pthread_mutex_init;
219 function pthread_mutex_destroy
220 (mutex : access pthread_mutex_t)
223 function pthread_mutex_destroy_base
224 (mutex : access pthread_mutex_t)
226 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
229 if pthread_mutex_destroy_base (mutex) /= 0 then
234 end pthread_mutex_destroy;
236 function pthread_mutex_lock
237 (mutex : access pthread_mutex_t)
240 function pthread_mutex_lock_base
241 (mutex : access pthread_mutex_t)
243 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
246 if pthread_mutex_lock_base (mutex) /= 0 then
251 end pthread_mutex_lock;
253 function pthread_mutex_unlock
254 (mutex : access pthread_mutex_t)
257 function pthread_mutex_unlock_base
258 (mutex : access pthread_mutex_t)
260 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
263 if pthread_mutex_unlock_base (mutex) /= 0 then
268 end pthread_mutex_unlock;
270 function pthread_condattr_init
271 (attr : access pthread_condattr_t)
274 function pthread_condattr_create
275 (attr : access pthread_condattr_t)
277 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
280 if pthread_condattr_create (attr) /= 0 then
285 end pthread_condattr_init;
287 function pthread_condattr_destroy
288 (attr : access pthread_condattr_t)
291 function pthread_condattr_delete
292 (attr : access pthread_condattr_t)
294 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
297 if pthread_condattr_delete (attr) /= 0 then
302 end pthread_condattr_destroy;
304 function pthread_cond_init
305 (cond : access pthread_cond_t;
306 attr : access pthread_condattr_t)
309 function pthread_cond_init_base
310 (cond : access pthread_cond_t;
311 attr : pthread_condattr_t)
313 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
316 if pthread_cond_init_base (cond, attr.all) /= 0 then
321 end pthread_cond_init;
323 function pthread_cond_destroy
324 (cond : access pthread_cond_t)
327 function pthread_cond_destroy_base
328 (cond : access pthread_cond_t)
330 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
333 if pthread_cond_destroy_base (cond) /= 0 then
338 end pthread_cond_destroy;
340 function pthread_cond_signal
341 (cond : access pthread_cond_t)
344 function pthread_cond_signal_base
345 (cond : access pthread_cond_t)
347 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
350 if pthread_cond_signal_base (cond) /= 0 then
355 end pthread_cond_signal;
357 function pthread_cond_wait
358 (cond : access pthread_cond_t;
359 mutex : access pthread_mutex_t)
362 function pthread_cond_wait_base
363 (cond : access pthread_cond_t;
364 mutex : access pthread_mutex_t)
366 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
369 if pthread_cond_wait_base (cond, mutex) /= 0 then
374 end pthread_cond_wait;
376 function pthread_cond_timedwait
377 (cond : access pthread_cond_t;
378 mutex : access pthread_mutex_t;
379 reltime : access timespec) return int
381 function pthread_cond_timedwait_base
382 (cond : access pthread_cond_t;
383 mutex : access pthread_mutex_t;
384 reltime : access timespec) return int;
385 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
388 if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
389 if errno = EAGAIN then
397 end pthread_cond_timedwait;
399 --------------------------
400 -- POSIX.1c Section 13 --
401 --------------------------
403 function pthread_setschedparam
406 param : access struct_sched_param)
409 function pthread_setscheduler
414 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
417 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
422 end pthread_setschedparam;
424 function pthread_mutexattr_setprotocol
425 (attr : access pthread_mutexattr_t;
431 end pthread_mutexattr_setprotocol;
433 function pthread_mutexattr_setprioceiling
434 (attr : access pthread_mutexattr_t;
440 end pthread_mutexattr_setprioceiling;
442 function pthread_attr_setscope
443 (attr : access pthread_attr_t;
444 contentionscope : int)
449 end pthread_attr_setscope;
451 function sched_yield return int is
452 procedure pthread_yield;
453 pragma Import (C, pthread_yield, "pthread_yield");
460 -----------------------------
461 -- P1003.1c - Section 16 --
462 -----------------------------
464 function pthread_attr_setdetachstate
465 (attr : access pthread_attr_t;
471 end pthread_attr_setdetachstate;
473 function pthread_create
474 (thread : access pthread_t;
475 attributes : access pthread_attr_t;
476 start_routine : Thread_Body;
477 arg : System.Address)
480 -- The LynxOS pthread_create doesn't seems to work.
481 -- Workaround : We're using st_new instead.
483 -- function pthread_create_base
484 -- (thread : access pthread_t;
485 -- attributes : pthread_attr_t;
486 -- start_routine : Thread_Body;
487 -- arg : System.Address)
489 -- pragma Import (C, pthread_create_base, "pthread_create");
491 St : aliased st_t := attributes.st;
494 (start_routine : Thread_Body;
495 arg : System.Address;
496 attributes : access st_t;
497 thread : access pthread_t)
499 pragma Import (C, st_new, "st_new");
502 -- Following code would be used if above commented function worked
504 -- if pthread_create_base
505 -- (thread, attributes.all, start_routine, arg) /= 0 then
507 if st_new (start_routine, arg, St'Access, thread) /= 0 then
514 function pthread_detach (thread : pthread_t) return int is
515 aliased_thread : aliased pthread_t := thread;
517 function pthread_detach_base (thread : access pthread_t) return int;
518 pragma Import (C, pthread_detach_base, "pthread_detach");
521 if pthread_detach_base (aliased_thread'Access) /= 0 then
528 --------------------------
529 -- POSIX.1c Section 17 --
530 --------------------------
532 function pthread_setspecific
533 (key : pthread_key_t;
534 value : System.Address)
537 function pthread_setspecific_base
538 (key : pthread_key_t;
539 value : System.Address)
541 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
544 if pthread_setspecific_base (key, value) /= 0 then
549 end pthread_setspecific;
551 function pthread_getspecific (key : pthread_key_t) return System.Address is
552 procedure pthread_getspecific_base
553 (key : pthread_key_t;
554 value : access System.Address);
555 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
557 value : aliased System.Address := System.Null_Address;
560 pthread_getspecific_base (key, value'Unchecked_Access);
562 end pthread_getspecific;
564 function Get_Stack_Base (thread : pthread_t) return Address is
569 function pthread_key_create
570 (key : access pthread_key_t;
571 destructor : destructor_pointer)
574 function pthread_keycreate
575 (key : access pthread_key_t;
576 destructor : destructor_pointer)
578 pragma Import (C, pthread_keycreate, "pthread_keycreate");
581 if pthread_keycreate (key, destructor) /= 0 then
586 end pthread_key_create;
588 procedure pthread_init is
593 end System.OS_Interface;