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-2006 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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.
40 package body System
.OS_Interface
is
48 function clock_gettime
49 (clock_id
: clockid_t
;
53 function clock_gettime_base
54 (clock_id
: clockid_t
;
57 pragma Import
(C
, clock_gettime_base
, "clock_gettime");
60 if clock_gettime_base
(clock_id
, tp
) /= 0 then
71 function To_Duration
(TS
: timespec
) return Duration is
73 return Duration (TS
.tv_sec
) + Duration (TS
.tv_nsec
) / 10#
1#E9
;
76 function To_Duration
(TV
: struct_timeval
) return Duration is
78 return Duration (TV
.tv_sec
) + Duration (TV
.tv_usec
) / 10#
1#E6
;
85 function To_Timespec
(D
: Duration) return timespec
is
90 S
:= time_t
(Long_Long_Integer (D
));
91 F
:= D
- Duration (S
);
93 -- If F has negative value due to a round-up, adjust for positive F
101 return timespec
'(tv_sec => S,
102 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
109 function To_Timeval (D : Duration) return struct_timeval is
114 S := time_t (Long_Long_Integer (D));
115 F := D - Duration (S);
117 -- If F has negative value due to a round-up, adjust for positive F
125 return struct_timeval'(tv_sec
=> S
,
126 tv_usec
=> time_t
(Long_Long_Integer (F
* 10#
1#E6
)));
129 -------------------------
130 -- POSIX.1c Section 3 --
131 -------------------------
134 (set
: access sigset_t
;
138 function sigwait_base
139 (set
: access sigset_t
;
140 value
: System
.Address
)
142 pragma Import
(C
, sigwait_base
, "sigwait");
145 sig
.all := sigwait_base
(set
, Null_Address
);
154 --------------------------
155 -- POSIX.1c Section 11 --
156 --------------------------
158 -- For all the following functions, LynxOS threads has the POSIX Draft 4
159 -- begavior; it sets errno but the standard Posix requires it to be
162 function pthread_mutexattr_init
163 (attr
: access pthread_mutexattr_t
)
166 function pthread_mutexattr_create
167 (attr
: access pthread_mutexattr_t
)
169 pragma Import
(C
, pthread_mutexattr_create
, "pthread_mutexattr_create");
172 if pthread_mutexattr_create
(attr
) /= 0 then
177 end pthread_mutexattr_init
;
179 function pthread_mutexattr_destroy
180 (attr
: access pthread_mutexattr_t
)
183 function pthread_mutexattr_delete
184 (attr
: access pthread_mutexattr_t
)
186 pragma Import
(C
, pthread_mutexattr_delete
, "pthread_mutexattr_delete");
189 if pthread_mutexattr_delete
(attr
) /= 0 then
194 end pthread_mutexattr_destroy
;
196 function pthread_mutex_init
197 (mutex
: access pthread_mutex_t
;
198 attr
: access pthread_mutexattr_t
)
201 function pthread_mutex_init_base
202 (mutex
: access pthread_mutex_t
;
203 attr
: pthread_mutexattr_t
)
205 pragma Import
(C
, pthread_mutex_init_base
, "pthread_mutex_init");
208 if pthread_mutex_init_base
(mutex
, attr
.all) /= 0 then
213 end pthread_mutex_init
;
215 function pthread_mutex_destroy
216 (mutex
: access pthread_mutex_t
)
219 function pthread_mutex_destroy_base
220 (mutex
: access pthread_mutex_t
)
222 pragma Import
(C
, pthread_mutex_destroy_base
, "pthread_mutex_destroy");
225 if pthread_mutex_destroy_base
(mutex
) /= 0 then
230 end pthread_mutex_destroy
;
232 function pthread_mutex_lock
233 (mutex
: access pthread_mutex_t
)
236 function pthread_mutex_lock_base
237 (mutex
: access pthread_mutex_t
)
239 pragma Import
(C
, pthread_mutex_lock_base
, "pthread_mutex_lock");
242 if pthread_mutex_lock_base
(mutex
) /= 0 then
247 end pthread_mutex_lock
;
249 function pthread_mutex_unlock
250 (mutex
: access pthread_mutex_t
)
253 function pthread_mutex_unlock_base
254 (mutex
: access pthread_mutex_t
)
256 pragma Import
(C
, pthread_mutex_unlock_base
, "pthread_mutex_unlock");
259 if pthread_mutex_unlock_base
(mutex
) /= 0 then
264 end pthread_mutex_unlock
;
266 function pthread_condattr_init
267 (attr
: access pthread_condattr_t
)
270 function pthread_condattr_create
271 (attr
: access pthread_condattr_t
)
273 pragma Import
(C
, pthread_condattr_create
, "pthread_condattr_create");
276 if pthread_condattr_create
(attr
) /= 0 then
281 end pthread_condattr_init
;
283 function pthread_condattr_destroy
284 (attr
: access pthread_condattr_t
)
287 function pthread_condattr_delete
288 (attr
: access pthread_condattr_t
)
290 pragma Import
(C
, pthread_condattr_delete
, "pthread_condattr_delete");
293 if pthread_condattr_delete
(attr
) /= 0 then
298 end pthread_condattr_destroy
;
300 function pthread_cond_init
301 (cond
: access pthread_cond_t
;
302 attr
: access pthread_condattr_t
)
305 function pthread_cond_init_base
306 (cond
: access pthread_cond_t
;
307 attr
: pthread_condattr_t
)
309 pragma Import
(C
, pthread_cond_init_base
, "pthread_cond_init");
312 if pthread_cond_init_base
(cond
, attr
.all) /= 0 then
317 end pthread_cond_init
;
319 function pthread_cond_destroy
320 (cond
: access pthread_cond_t
)
323 function pthread_cond_destroy_base
324 (cond
: access pthread_cond_t
)
326 pragma Import
(C
, pthread_cond_destroy_base
, "pthread_cond_destroy");
329 if pthread_cond_destroy_base
(cond
) /= 0 then
334 end pthread_cond_destroy
;
336 function pthread_cond_signal
337 (cond
: access pthread_cond_t
)
340 function pthread_cond_signal_base
341 (cond
: access pthread_cond_t
)
343 pragma Import
(C
, pthread_cond_signal_base
, "pthread_cond_signal");
346 if pthread_cond_signal_base
(cond
) /= 0 then
351 end pthread_cond_signal
;
353 function pthread_cond_wait
354 (cond
: access pthread_cond_t
;
355 mutex
: access pthread_mutex_t
)
358 function pthread_cond_wait_base
359 (cond
: access pthread_cond_t
;
360 mutex
: access pthread_mutex_t
)
362 pragma Import
(C
, pthread_cond_wait_base
, "pthread_cond_wait");
365 if pthread_cond_wait_base
(cond
, mutex
) /= 0 then
370 end pthread_cond_wait
;
372 function pthread_cond_timedwait
373 (cond
: access pthread_cond_t
;
374 mutex
: access pthread_mutex_t
;
375 reltime
: access timespec
) return int
377 function pthread_cond_timedwait_base
378 (cond
: access pthread_cond_t
;
379 mutex
: access pthread_mutex_t
;
380 reltime
: access timespec
) return int
;
381 pragma Import
(C
, pthread_cond_timedwait_base
, "pthread_cond_timedwait");
384 if pthread_cond_timedwait_base
(cond
, mutex
, reltime
) /= 0 then
385 if errno
= EAGAIN
then
393 end pthread_cond_timedwait
;
395 --------------------------
396 -- POSIX.1c Section 13 --
397 --------------------------
399 function pthread_setschedparam
402 param
: access struct_sched_param
)
405 function pthread_setscheduler
410 pragma Import
(C
, pthread_setscheduler
, "pthread_setscheduler");
413 if pthread_setscheduler
(thread
, policy
, param
.sched_priority
) = -1 then
418 end pthread_setschedparam
;
420 function pthread_mutexattr_setprotocol
421 (attr
: access pthread_mutexattr_t
;
425 pragma Unreferenced
(attr
, protocol
);
428 end pthread_mutexattr_setprotocol
;
430 function pthread_mutexattr_setprioceiling
431 (attr
: access pthread_mutexattr_t
;
435 pragma Unreferenced
(attr
, prioceiling
);
438 end pthread_mutexattr_setprioceiling
;
440 function pthread_attr_setscope
441 (attr
: access pthread_attr_t
;
442 contentionscope
: int
)
445 pragma Unreferenced
(attr
, contentionscope
);
448 end pthread_attr_setscope
;
450 function sched_yield
return int
is
451 procedure pthread_yield
;
452 pragma Import
(C
, pthread_yield
, "pthread_yield");
459 -----------------------------
460 -- P1003.1c - Section 16 --
461 -----------------------------
463 function pthread_attr_setdetachstate
464 (attr
: access pthread_attr_t
;
468 pragma Unreferenced
(attr
, detachstate
);
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
565 pragma Warnings
(Off
, thread
);
571 function pthread_key_create
572 (key
: access pthread_key_t
;
573 destructor
: destructor_pointer
)
576 function pthread_keycreate
577 (key
: access pthread_key_t
;
578 destructor
: destructor_pointer
)
580 pragma Import
(C
, pthread_keycreate
, "pthread_keycreate");
583 if pthread_keycreate
(key
, destructor
) /= 0 then
588 end pthread_key_create
;
590 procedure pthread_init
is
595 end System
.OS_Interface
;