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) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2010, AdaCore --
12 -- GNAT 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 3, or (at your option) any later ver- --
15 -- sion. GNAT 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. --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 ------------------------------------------------------------------------------
33 -- This is a DCE version of this package.
34 -- Currently HP-UX and SNI use this file
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 -- This package encapsulates all direct interfaces to OS services
41 -- that are needed by children of System.
43 with Interfaces
.C
; use Interfaces
.C
;
45 package body System
.OS_Interface
is
51 function To_Duration
(TS
: timespec
) return Duration is
53 return Duration (TS
.tv_sec
) + Duration (TS
.tv_nsec
) / 10#
1#E9
;
60 function To_Timespec
(D
: Duration) return timespec
is
65 S
:= time_t
(Long_Long_Integer (D
));
66 F
:= D
- Duration (S
);
68 -- If F has negative value due to a round-up, adjust for positive F
75 return timespec
'(tv_sec => S,
76 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
79 -------------------------
80 -- POSIX.1c Section 3 --
81 -------------------------
84 (set : access sigset_t;
85 sig : access Signal) return int
90 Result := sigwait (set);
97 sig.all := Signal (Result);
101 -- DCE_THREADS does not have pthread_kill. Instead, we just ignore it
103 function pthread_kill (thread : pthread_t; sig : Signal) return int is
104 pragma Unreferenced (thread, sig);
109 --------------------------
110 -- POSIX.1c Section 11 --
111 --------------------------
113 -- For all following functions, DCE Threads has a non standard behavior.
114 -- It sets errno but the standard Posix requires it to be returned.
116 function pthread_mutexattr_init
117 (attr : access pthread_mutexattr_t) return int
119 function pthread_mutexattr_create
120 (attr : access pthread_mutexattr_t) return int;
121 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
124 if pthread_mutexattr_create (attr) /= 0 then
129 end pthread_mutexattr_init;
131 function pthread_mutexattr_destroy
132 (attr : access pthread_mutexattr_t) return int
134 function pthread_mutexattr_delete
135 (attr : access pthread_mutexattr_t) return int;
136 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
139 if pthread_mutexattr_delete (attr) /= 0 then
144 end pthread_mutexattr_destroy;
146 function pthread_mutex_init
147 (mutex : access pthread_mutex_t;
148 attr : access pthread_mutexattr_t) return int
150 function pthread_mutex_init_base
151 (mutex : access pthread_mutex_t;
152 attr : pthread_mutexattr_t) return int;
153 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
156 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
161 end pthread_mutex_init;
163 function pthread_mutex_destroy
164 (mutex : access pthread_mutex_t) return int
166 function pthread_mutex_destroy_base
167 (mutex : access pthread_mutex_t) return int;
168 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
171 if pthread_mutex_destroy_base (mutex) /= 0 then
176 end pthread_mutex_destroy;
178 function pthread_mutex_lock
179 (mutex : access pthread_mutex_t) return int
181 function pthread_mutex_lock_base
182 (mutex : access pthread_mutex_t) return int;
183 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
186 if pthread_mutex_lock_base (mutex) /= 0 then
191 end pthread_mutex_lock;
193 function pthread_mutex_unlock
194 (mutex : access pthread_mutex_t) return int
196 function pthread_mutex_unlock_base
197 (mutex : access pthread_mutex_t) return int;
198 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
201 if pthread_mutex_unlock_base (mutex) /= 0 then
206 end pthread_mutex_unlock;
208 function pthread_condattr_init
209 (attr : access pthread_condattr_t) return int
211 function pthread_condattr_create
212 (attr : access pthread_condattr_t) return int;
213 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
216 if pthread_condattr_create (attr) /= 0 then
221 end pthread_condattr_init;
223 function pthread_condattr_destroy
224 (attr : access pthread_condattr_t) return int
226 function pthread_condattr_delete
227 (attr : access pthread_condattr_t) return int;
228 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
231 if pthread_condattr_delete (attr) /= 0 then
236 end pthread_condattr_destroy;
238 function pthread_cond_init
239 (cond : access pthread_cond_t;
240 attr : access pthread_condattr_t) return int
242 function pthread_cond_init_base
243 (cond : access pthread_cond_t;
244 attr : pthread_condattr_t) return int;
245 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
248 if pthread_cond_init_base (cond, attr.all) /= 0 then
253 end pthread_cond_init;
255 function pthread_cond_destroy
256 (cond : access pthread_cond_t) return int
258 function pthread_cond_destroy_base
259 (cond : access pthread_cond_t) return int;
260 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
263 if pthread_cond_destroy_base (cond) /= 0 then
268 end pthread_cond_destroy;
270 function pthread_cond_signal
271 (cond : access pthread_cond_t) return int
273 function pthread_cond_signal_base
274 (cond : access pthread_cond_t) return int;
275 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
278 if pthread_cond_signal_base (cond) /= 0 then
283 end pthread_cond_signal;
285 function pthread_cond_wait
286 (cond : access pthread_cond_t;
287 mutex : access pthread_mutex_t) return int
289 function pthread_cond_wait_base
290 (cond : access pthread_cond_t;
291 mutex : access pthread_mutex_t) return int;
292 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
295 if pthread_cond_wait_base (cond, mutex) /= 0 then
300 end pthread_cond_wait;
302 function pthread_cond_timedwait
303 (cond : access pthread_cond_t;
304 mutex : access pthread_mutex_t;
305 abstime : access timespec) return int
307 function pthread_cond_timedwait_base
308 (cond : access pthread_cond_t;
309 mutex : access pthread_mutex_t;
310 abstime : access timespec) return int;
311 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
314 if pthread_cond_timedwait_base (cond, mutex, abstime) /= 0 then
315 return (if errno = EAGAIN then ETIMEDOUT else errno);
319 end pthread_cond_timedwait;
321 ----------------------------
322 -- POSIX.1c Section 13 --
323 ----------------------------
325 function pthread_setschedparam
328 param : access struct_sched_param) return int
330 function pthread_setscheduler
333 priority : int) return int;
334 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
337 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
342 end pthread_setschedparam;
344 function sched_yield return int is
345 procedure pthread_yield;
346 pragma Import (C, pthread_yield, "pthread_yield");
352 -----------------------------
353 -- P1003.1c - Section 16 --
354 -----------------------------
356 function pthread_attr_init
357 (attributes : access pthread_attr_t) return int
359 function pthread_attr_create
360 (attributes : access pthread_attr_t) return int;
361 pragma Import (C, pthread_attr_create, "pthread_attr_create");
364 if pthread_attr_create (attributes) /= 0 then
369 end pthread_attr_init;
371 function pthread_attr_destroy
372 (attributes : access pthread_attr_t) return int
374 function pthread_attr_delete
375 (attributes : access pthread_attr_t) return int;
376 pragma Import (C, pthread_attr_delete, "pthread_attr_delete");
379 if pthread_attr_delete (attributes) /= 0 then
384 end pthread_attr_destroy;
386 function pthread_attr_setstacksize
387 (attr : access pthread_attr_t;
388 stacksize : size_t) return int
390 function pthread_attr_setstacksize_base
391 (attr : access pthread_attr_t;
392 stacksize : size_t) return int;
393 pragma Import (C, pthread_attr_setstacksize_base,
394 "pthread_attr_setstacksize");
397 if pthread_attr_setstacksize_base (attr, stacksize) /= 0 then
402 end pthread_attr_setstacksize;
404 function pthread_create
405 (thread : access pthread_t;
406 attributes : access pthread_attr_t;
407 start_routine : Thread_Body;
408 arg : System.Address) return int
410 function pthread_create_base
411 (thread : access pthread_t;
412 attributes : pthread_attr_t;
413 start_routine : Thread_Body;
414 arg : System.Address) return int;
415 pragma Import (C, pthread_create_base, "pthread_create");
418 if pthread_create_base
419 (thread, attributes.all, start_routine, arg) /= 0
427 --------------------------
428 -- POSIX.1c Section 17 --
429 --------------------------
431 function pthread_setspecific
432 (key : pthread_key_t;
433 value : System.Address) return int
435 function pthread_setspecific_base
436 (key : pthread_key_t;
437 value : System.Address) return int;
438 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
441 if pthread_setspecific_base (key, value) /= 0 then
446 end pthread_setspecific;
448 function pthread_getspecific (key : pthread_key_t) return System.Address is
449 function pthread_getspecific_base
450 (key : pthread_key_t;
451 value : access System.Address) return int;
452 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
453 Addr : aliased System.Address;
456 if pthread_getspecific_base (key, Addr'Access) /= 0 then
457 return System.Null_Address;
461 end pthread_getspecific;
463 function pthread_key_create
464 (key : access pthread_key_t;
465 destructor : destructor_pointer) return int
467 function pthread_keycreate
468 (key : access pthread_key_t;
469 destructor : destructor_pointer) return int;
470 pragma Import (C, pthread_keycreate, "pthread_keycreate");
473 if pthread_keycreate (key, destructor) /= 0 then
478 end pthread_key_create;
480 function Get_Stack_Base (thread : pthread_t) return Address is
481 pragma Warnings (Off, thread);
486 procedure pthread_init is
491 function intr_attach (sig : int; handler : isr_address) return long is
492 function c_signal (sig : int; handler : isr_address) return long;
493 pragma Import (C, c_signal, "signal");
495 return c_signal (sig, handler);
498 end System.OS_Interface;