1 ------------------------------------------------------------------------------
3 -- GNAT 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-2009, 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 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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a LynxOS (Native) version of this package
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
46 function clock_gettime
47 (clock_id
: clockid_t
;
51 function clock_gettime_base
52 (clock_id
: clockid_t
;
55 pragma Import
(C
, clock_gettime_base
, "clock_gettime");
58 if clock_gettime_base
(clock_id
, tp
) /= 0 then
69 function To_Duration
(TS
: timespec
) return Duration is
71 return Duration (TS
.tv_sec
) + Duration (TS
.tv_nsec
) / 10#
1#E9
;
74 ------------------------
75 -- To_Target_Priority --
76 ------------------------
78 function To_Target_Priority
79 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
82 return Interfaces
.C
.int
(Prio
);
83 end To_Target_Priority
;
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)));
109 -------------------------
110 -- POSIX.1c Section 3 --
111 -------------------------
114 (set : access sigset_t;
118 function sigwait_base
119 (set : access sigset_t;
120 value : System.Address)
122 pragma Import (C, sigwait_base, "sigwait");
125 sig.all := sigwait_base (set, Null_Address);
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
142 function pthread_mutexattr_init
143 (attr : access pthread_mutexattr_t)
146 function pthread_mutexattr_create
147 (attr : access pthread_mutexattr_t)
149 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
152 if pthread_mutexattr_create (attr) /= 0 then
157 end pthread_mutexattr_init;
159 function pthread_mutexattr_destroy
160 (attr : access pthread_mutexattr_t)
163 function pthread_mutexattr_delete
164 (attr : access pthread_mutexattr_t)
166 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
169 if pthread_mutexattr_delete (attr) /= 0 then
174 end pthread_mutexattr_destroy;
176 function pthread_mutex_init
177 (mutex : access pthread_mutex_t;
178 attr : access pthread_mutexattr_t)
181 function pthread_mutex_init_base
182 (mutex : access pthread_mutex_t;
183 attr : pthread_mutexattr_t)
185 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
188 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
193 end pthread_mutex_init;
195 function pthread_mutex_destroy
196 (mutex : access pthread_mutex_t)
199 function pthread_mutex_destroy_base
200 (mutex : access pthread_mutex_t)
202 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
205 if pthread_mutex_destroy_base (mutex) /= 0 then
210 end pthread_mutex_destroy;
212 function pthread_mutex_lock
213 (mutex : access pthread_mutex_t)
216 function pthread_mutex_lock_base
217 (mutex : access pthread_mutex_t)
219 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
222 if pthread_mutex_lock_base (mutex) /= 0 then
227 end pthread_mutex_lock;
229 function pthread_mutex_unlock
230 (mutex : access pthread_mutex_t)
233 function pthread_mutex_unlock_base
234 (mutex : access pthread_mutex_t)
236 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
239 if pthread_mutex_unlock_base (mutex) /= 0 then
244 end pthread_mutex_unlock;
246 function pthread_condattr_init
247 (attr : access pthread_condattr_t)
250 function pthread_condattr_create
251 (attr : access pthread_condattr_t)
253 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
256 if pthread_condattr_create (attr) /= 0 then
261 end pthread_condattr_init;
263 function pthread_condattr_destroy
264 (attr : access pthread_condattr_t)
267 function pthread_condattr_delete
268 (attr : access pthread_condattr_t)
270 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
273 if pthread_condattr_delete (attr) /= 0 then
278 end pthread_condattr_destroy;
280 function pthread_cond_init
281 (cond : access pthread_cond_t;
282 attr : access pthread_condattr_t)
285 function pthread_cond_init_base
286 (cond : access pthread_cond_t;
287 attr : pthread_condattr_t)
289 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
292 if pthread_cond_init_base (cond, attr.all) /= 0 then
297 end pthread_cond_init;
299 function pthread_cond_destroy
300 (cond : access pthread_cond_t)
303 function pthread_cond_destroy_base
304 (cond : access pthread_cond_t)
306 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
309 if pthread_cond_destroy_base (cond) /= 0 then
314 end pthread_cond_destroy;
316 function pthread_cond_signal
317 (cond : access pthread_cond_t)
320 function pthread_cond_signal_base
321 (cond : access pthread_cond_t)
323 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
326 if pthread_cond_signal_base (cond) /= 0 then
331 end pthread_cond_signal;
333 function pthread_cond_wait
334 (cond : access pthread_cond_t;
335 mutex : access pthread_mutex_t)
338 function pthread_cond_wait_base
339 (cond : access pthread_cond_t;
340 mutex : access pthread_mutex_t)
342 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
345 if pthread_cond_wait_base (cond, mutex) /= 0 then
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");
364 if pthread_cond_timedwait_base (cond, mutex, reltime) /= 0 then
365 if errno = EAGAIN then
373 end pthread_cond_timedwait;
375 --------------------------
376 -- POSIX.1c Section 13 --
377 --------------------------
379 function pthread_setschedparam
382 param : access struct_sched_param)
385 function pthread_setscheduler
390 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
393 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
398 end pthread_setschedparam;
400 function pthread_mutexattr_setprotocol
401 (attr : access pthread_mutexattr_t;
405 pragma Unreferenced (attr, protocol);
408 end pthread_mutexattr_setprotocol;
410 function pthread_mutexattr_setprioceiling
411 (attr : access pthread_mutexattr_t;
415 pragma Unreferenced (attr, prioceiling);
418 end pthread_mutexattr_setprioceiling;
420 function pthread_attr_setscope
421 (attr : access pthread_attr_t;
422 contentionscope : int)
425 pragma Unreferenced (attr, contentionscope);
428 end pthread_attr_setscope;
430 function sched_yield return int is
431 procedure pthread_yield;
432 pragma Import (C, pthread_yield, "pthread_yield");
439 -----------------------------
440 -- P1003.1c - Section 16 --
441 -----------------------------
443 function pthread_attr_setdetachstate
444 (attr : access pthread_attr_t;
448 pragma Unreferenced (attr, detachstate);
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)
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)
469 -- pragma Import (C, pthread_create_base, "pthread_create");
471 St : aliased st_t := attributes.st;
474 (start_routine : Thread_Body;
475 arg : System.Address;
476 attributes : access st_t;
477 thread : access pthread_t)
479 pragma Import (C, st_new, "st_new");
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
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");
501 if pthread_detach_base (aliased_thread'Access) /= 0 then
508 --------------------------
509 -- POSIX.1c Section 17 --
510 --------------------------
512 function pthread_setspecific
513 (key : pthread_key_t;
514 value : System.Address)
517 function pthread_setspecific_base
518 (key : pthread_key_t;
519 value : System.Address)
521 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
524 if pthread_setspecific_base (key, value) /= 0 then
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;
540 pthread_getspecific_base (key, value'Unchecked_Access);
542 end pthread_getspecific;
544 function Get_Stack_Base (thread : pthread_t) return Address is
545 pragma Warnings (Off, thread);
551 function pthread_key_create
552 (key : access pthread_key_t;
553 destructor : destructor_pointer)
556 function pthread_keycreate
557 (key : access pthread_key_t;
558 destructor : destructor_pointer)
560 pragma Import (C, pthread_keycreate, "pthread_keycreate");
563 if pthread_keycreate (key, destructor) /= 0 then
568 end pthread_key_create;
570 procedure pthread_init is
575 end System.OS_Interface;