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 --
9 -- Copyright (C) 1999-2003 Free Software Foundation, Inc. --
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. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a LynxOS (Native) version of this package
37 -- Turn off polling, we do not want ATC polling to take place during
38 -- tasking operations. It causes infinite loops and other problems.
42 package body System.OS_Interface is
50 function clock_gettime
51 (clock_id : clockid_t;
55 function clock_gettime_base
56 (clock_id : clockid_t;
59 pragma Import (C, clock_gettime_base, "clock_gettime");
62 if clock_gettime_base (clock_id, tp) /= 0 then
73 function To_Duration (TS : timespec) return Duration is
75 return Duration (TS.tv_sec) + Duration (TS.tv_nsec) / 10#1#E9;
78 function To_Duration (TV : struct_timeval) return Duration is
80 return Duration (TV.tv_sec) + Duration (TV.tv_usec) / 10#1#E6;
87 function To_Timespec (D : Duration) return timespec is
92 S := time_t (Long_Long_Integer (D));
93 F := D - Duration (S);
95 -- If F has negative value due to a round-up, adjust for positive F
103 return timespec'(tv_sec => S,
104 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
111 function To_Timeval (D : Duration) return struct_timeval is
116 S := time_t (Long_Long_Integer (D));
117 F := D - Duration (S);
119 -- If F has negative value due to a round-up, adjust for positive F
127 return struct_timeval'(tv_sec => S,
128 tv_usec => time_t (Long_Long_Integer (F * 10#1#E6)));
131 -------------------------
132 -- POSIX.1c Section 3 --
133 -------------------------
136 (set : access sigset_t;
140 function sigwait_base
141 (set : access sigset_t;
142 value : System.Address)
144 pragma Import (C, sigwait_base, "sigwait");
147 sig.all := sigwait_base (set, Null_Address);
156 --------------------------
157 -- POSIX.1c Section 11 --
158 --------------------------
160 -- For all the following functions, LynxOS threads has the POSIX Draft 4
161 -- begavior; it sets errno but the standard Posix requires it to be
164 function pthread_mutexattr_init
165 (attr : access pthread_mutexattr_t)
168 function pthread_mutexattr_create
169 (attr : access pthread_mutexattr_t)
171 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
174 if pthread_mutexattr_create (attr) /= 0 then
179 end pthread_mutexattr_init;
181 function pthread_mutexattr_destroy
182 (attr : access pthread_mutexattr_t)
185 function pthread_mutexattr_delete
186 (attr : access pthread_mutexattr_t)
188 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
191 if pthread_mutexattr_delete (attr) /= 0 then
196 end pthread_mutexattr_destroy;
198 function pthread_mutex_init
199 (mutex : access pthread_mutex_t;
200 attr : access pthread_mutexattr_t)
203 function pthread_mutex_init_base
204 (mutex : access pthread_mutex_t;
205 attr : pthread_mutexattr_t)
207 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
210 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
215 end pthread_mutex_init;
217 function pthread_mutex_destroy
218 (mutex : access pthread_mutex_t)
221 function pthread_mutex_destroy_base
222 (mutex : access pthread_mutex_t)
224 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
227 if pthread_mutex_destroy_base (mutex) /= 0 then
232 end pthread_mutex_destroy;
234 function pthread_mutex_lock
235 (mutex : access pthread_mutex_t)
238 function pthread_mutex_lock_base
239 (mutex : access pthread_mutex_t)
241 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
244 if pthread_mutex_lock_base (mutex) /= 0 then
249 end pthread_mutex_lock;
251 function pthread_mutex_unlock
252 (mutex : access pthread_mutex_t)
255 function pthread_mutex_unlock_base
256 (mutex : access pthread_mutex_t)
258 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
261 if pthread_mutex_unlock_base (mutex) /= 0 then
266 end pthread_mutex_unlock;
268 function pthread_condattr_init
269 (attr : access pthread_condattr_t)
272 function pthread_condattr_create
273 (attr : access pthread_condattr_t)
275 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
278 if pthread_condattr_create (attr) /= 0 then
283 end pthread_condattr_init;
285 function pthread_condattr_destroy
286 (attr : access pthread_condattr_t)
289 function pthread_condattr_delete
290 (attr : access pthread_condattr_t)
292 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
295 if pthread_condattr_delete (attr) /= 0 then
300 end pthread_condattr_destroy;
302 function pthread_cond_init
303 (cond : access pthread_cond_t;
304 attr : access pthread_condattr_t)
307 function pthread_cond_init_base
308 (cond : access pthread_cond_t;
309 attr : pthread_condattr_t)
311 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
314 if pthread_cond_init_base (cond, attr.all) /= 0 then
319 end pthread_cond_init;
321 function pthread_cond_destroy
322 (cond : access pthread_cond_t)
325 function pthread_cond_destroy_base
326 (cond : access pthread_cond_t)
328 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
331 if pthread_cond_destroy_base (cond) /= 0 then
336 end pthread_cond_destroy;
338 function pthread_cond_signal
339 (cond : access pthread_cond_t)
342 function pthread_cond_signal_base
343 (cond : access pthread_cond_t)
345 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
348 if pthread_cond_signal_base (cond) /= 0 then
353 end pthread_cond_signal;
355 function pthread_cond_wait
356 (cond : access pthread_cond_t;
357 mutex : access pthread_mutex_t)
360 function pthread_cond_wait_base
361 (cond : access pthread_cond_t;
362 mutex : access pthread_mutex_t)
364 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
367 if pthread_cond_wait_base (cond, mutex) /= 0 then
372 end pthread_cond_wait;
374 function pthread_cond_timedwait
375 (cond : access pthread_cond_t;
376 mutex : access pthread_mutex_t;
377 reltime : access timespec) return int
379 function pthread_cond_timedwait_base
380 (cond : access pthread_cond_t;
381 mutex : access pthread_mutex_t;
382 reltime : access timespec) return int;
383 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
386 if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
387 if errno = EAGAIN then
395 end pthread_cond_timedwait;
397 --------------------------
398 -- POSIX.1c Section 13 --
399 --------------------------
401 function pthread_setschedparam
404 param : access struct_sched_param)
407 function pthread_setscheduler
412 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
415 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
420 end pthread_setschedparam;
422 function pthread_mutexattr_setprotocol
423 (attr : access pthread_mutexattr_t;
427 pragma Unreferenced (attr, protocol);
430 end pthread_mutexattr_setprotocol;
432 function pthread_mutexattr_setprioceiling
433 (attr : access pthread_mutexattr_t;
437 pragma Unreferenced (attr, prioceiling);
440 end pthread_mutexattr_setprioceiling;
442 function pthread_attr_setscope
443 (attr : access pthread_attr_t;
444 contentionscope : int)
447 pragma Unreferenced (attr, contentionscope);
450 end pthread_attr_setscope;
452 function sched_yield return int is
453 procedure pthread_yield;
454 pragma Import (C, pthread_yield, "pthread_yield");
461 -----------------------------
462 -- P1003.1c - Section 16 --
463 -----------------------------
465 function pthread_attr_setdetachstate
466 (attr : access pthread_attr_t;
470 pragma Unreferenced (attr, detachstate);
473 end pthread_attr_setdetachstate;
475 function pthread_create
476 (thread : access pthread_t;
477 attributes : access pthread_attr_t;
478 start_routine : Thread_Body;
479 arg : System.Address)
482 -- The LynxOS pthread_create doesn't seems to work.
483 -- Workaround : We're using st_new instead.
485 -- function pthread_create_base
486 -- (thread : access pthread_t;
487 -- attributes : pthread_attr_t;
488 -- start_routine : Thread_Body;
489 -- arg : System.Address)
491 -- pragma Import (C, pthread_create_base, "pthread_create");
493 St : aliased st_t := attributes.st;
496 (start_routine : Thread_Body;
497 arg : System.Address;
498 attributes : access st_t;
499 thread : access pthread_t)
501 pragma Import (C, st_new, "st_new");
504 -- Following code would be used if above commented function worked
506 -- if pthread_create_base
507 -- (thread, attributes.all, start_routine, arg) /= 0 then
509 if st_new (start_routine, arg, St'Access, thread) /= 0 then
516 function pthread_detach (thread : pthread_t) return int is
517 aliased_thread : aliased pthread_t := thread;
519 function pthread_detach_base (thread : access pthread_t) return int;
520 pragma Import (C, pthread_detach_base, "pthread_detach");
523 if pthread_detach_base (aliased_thread'Access) /= 0 then
530 --------------------------
531 -- POSIX.1c Section 17 --
532 --------------------------
534 function pthread_setspecific
535 (key : pthread_key_t;
536 value : System.Address)
539 function pthread_setspecific_base
540 (key : pthread_key_t;
541 value : System.Address)
543 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
546 if pthread_setspecific_base (key, value) /= 0 then
551 end pthread_setspecific;
553 function pthread_getspecific (key : pthread_key_t) return System.Address is
554 procedure pthread_getspecific_base
555 (key : pthread_key_t;
556 value : access System.Address);
557 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
559 value : aliased System.Address := System.Null_Address;
562 pthread_getspecific_base (key, value'Unchecked_Access);
564 end pthread_getspecific;
566 function Get_Stack_Base (thread : pthread_t) return Address is
567 pragma Warnings (Off, thread);
573 function pthread_key_create
574 (key : access pthread_key_t;
575 destructor : destructor_pointer)
578 function pthread_keycreate
579 (key : access pthread_key_t;
580 destructor : destructor_pointer)
582 pragma Import (C, pthread_keycreate, "pthread_keycreate");
585 if pthread_keycreate (key, destructor) /= 0 then
590 end pthread_key_create;
592 procedure pthread_init is
597 end System.OS_Interface;