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-2009, AdaCore --
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, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, 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. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
33 ------------------------------------------------------------------------------
35 -- This is a DCE version of this package.
36 -- Currently HP-UX and SNI use this file
39 -- Turn off polling, we do not want ATC polling to take place during
40 -- tasking operations. It causes infinite loops and other problems.
42 -- This package encapsulates all direct interfaces to OS services
43 -- that are needed by children of System.
45 with Interfaces
.C
; use Interfaces
.C
;
47 package body System
.OS_Interface
is
53 function To_Duration
(TS
: timespec
) return Duration is
55 return Duration (TS
.tv_sec
) + Duration (TS
.tv_nsec
) / 10#
1#E9
;
62 function To_Timespec
(D
: Duration) return timespec
is
67 S
:= time_t
(Long_Long_Integer (D
));
68 F
:= D
- Duration (S
);
70 -- If F has negative value due to a round-up, adjust for positive F
77 return timespec
'(tv_sec => S,
78 tv_nsec => long (Long_Long_Integer (F * 10#1#E9)));
81 -------------------------
82 -- POSIX.1c Section 3 --
83 -------------------------
86 (set : access sigset_t;
87 sig : access Signal) return int
92 Result := sigwait (set);
99 sig.all := Signal (Result);
103 -- DCE_THREADS does not have pthread_kill. Instead, we just ignore it
105 function pthread_kill (thread : pthread_t; sig : Signal) return int is
106 pragma Unreferenced (thread, sig);
111 --------------------------
112 -- POSIX.1c Section 11 --
113 --------------------------
115 -- For all following functions, DCE Threads has a non standard behavior.
116 -- It sets errno but the standard Posix requires it to be returned.
118 function pthread_mutexattr_init
119 (attr : access pthread_mutexattr_t) return int
121 function pthread_mutexattr_create
122 (attr : access pthread_mutexattr_t) return int;
123 pragma Import (C, pthread_mutexattr_create, "pthread_mutexattr_create");
126 if pthread_mutexattr_create (attr) /= 0 then
131 end pthread_mutexattr_init;
133 function pthread_mutexattr_destroy
134 (attr : access pthread_mutexattr_t) return int
136 function pthread_mutexattr_delete
137 (attr : access pthread_mutexattr_t) return int;
138 pragma Import (C, pthread_mutexattr_delete, "pthread_mutexattr_delete");
141 if pthread_mutexattr_delete (attr) /= 0 then
146 end pthread_mutexattr_destroy;
148 function pthread_mutex_init
149 (mutex : access pthread_mutex_t;
150 attr : access pthread_mutexattr_t) return int
152 function pthread_mutex_init_base
153 (mutex : access pthread_mutex_t;
154 attr : pthread_mutexattr_t) return int;
155 pragma Import (C, pthread_mutex_init_base, "pthread_mutex_init");
158 if pthread_mutex_init_base (mutex, attr.all) /= 0 then
163 end pthread_mutex_init;
165 function pthread_mutex_destroy
166 (mutex : access pthread_mutex_t) return int
168 function pthread_mutex_destroy_base
169 (mutex : access pthread_mutex_t) return int;
170 pragma Import (C, pthread_mutex_destroy_base, "pthread_mutex_destroy");
173 if pthread_mutex_destroy_base (mutex) /= 0 then
178 end pthread_mutex_destroy;
180 function pthread_mutex_lock
181 (mutex : access pthread_mutex_t) return int
183 function pthread_mutex_lock_base
184 (mutex : access pthread_mutex_t) return int;
185 pragma Import (C, pthread_mutex_lock_base, "pthread_mutex_lock");
188 if pthread_mutex_lock_base (mutex) /= 0 then
193 end pthread_mutex_lock;
195 function pthread_mutex_unlock
196 (mutex : access pthread_mutex_t) return int
198 function pthread_mutex_unlock_base
199 (mutex : access pthread_mutex_t) return int;
200 pragma Import (C, pthread_mutex_unlock_base, "pthread_mutex_unlock");
203 if pthread_mutex_unlock_base (mutex) /= 0 then
208 end pthread_mutex_unlock;
210 function pthread_condattr_init
211 (attr : access pthread_condattr_t) return int
213 function pthread_condattr_create
214 (attr : access pthread_condattr_t) return int;
215 pragma Import (C, pthread_condattr_create, "pthread_condattr_create");
218 if pthread_condattr_create (attr) /= 0 then
223 end pthread_condattr_init;
225 function pthread_condattr_destroy
226 (attr : access pthread_condattr_t) return int
228 function pthread_condattr_delete
229 (attr : access pthread_condattr_t) return int;
230 pragma Import (C, pthread_condattr_delete, "pthread_condattr_delete");
233 if pthread_condattr_delete (attr) /= 0 then
238 end pthread_condattr_destroy;
240 function pthread_cond_init
241 (cond : access pthread_cond_t;
242 attr : access pthread_condattr_t) return int
244 function pthread_cond_init_base
245 (cond : access pthread_cond_t;
246 attr : pthread_condattr_t) return int;
247 pragma Import (C, pthread_cond_init_base, "pthread_cond_init");
250 if pthread_cond_init_base (cond, attr.all) /= 0 then
255 end pthread_cond_init;
257 function pthread_cond_destroy
258 (cond : access pthread_cond_t) return int
260 function pthread_cond_destroy_base
261 (cond : access pthread_cond_t) return int;
262 pragma Import (C, pthread_cond_destroy_base, "pthread_cond_destroy");
265 if pthread_cond_destroy_base (cond) /= 0 then
270 end pthread_cond_destroy;
272 function pthread_cond_signal
273 (cond : access pthread_cond_t) return int
275 function pthread_cond_signal_base
276 (cond : access pthread_cond_t) return int;
277 pragma Import (C, pthread_cond_signal_base, "pthread_cond_signal");
280 if pthread_cond_signal_base (cond) /= 0 then
285 end pthread_cond_signal;
287 function pthread_cond_wait
288 (cond : access pthread_cond_t;
289 mutex : access pthread_mutex_t) return int
291 function pthread_cond_wait_base
292 (cond : access pthread_cond_t;
293 mutex : access pthread_mutex_t) return int;
294 pragma Import (C, pthread_cond_wait_base, "pthread_cond_wait");
297 if pthread_cond_wait_base (cond, mutex) /= 0 then
302 end pthread_cond_wait;
304 function pthread_cond_timedwait
305 (cond : access pthread_cond_t;
306 mutex : access pthread_mutex_t;
307 abstime : access timespec) return int
309 function pthread_cond_timedwait_base
310 (cond : access pthread_cond_t;
311 mutex : access pthread_mutex_t;
312 abstime : access timespec) return int;
313 pragma Import (C, pthread_cond_timedwait_base, "pthread_cond_timedwait");
316 if pthread_cond_timedwait_base (cond, mutex, abstime) /= 0 then
317 return (if errno = EAGAIN then ETIMEDOUT else errno);
321 end pthread_cond_timedwait;
323 ----------------------------
324 -- POSIX.1c Section 13 --
325 ----------------------------
327 function pthread_setschedparam
330 param : access struct_sched_param) return int
332 function pthread_setscheduler
335 priority : int) return int;
336 pragma Import (C, pthread_setscheduler, "pthread_setscheduler");
339 if pthread_setscheduler (thread, policy, param.sched_priority) = -1 then
344 end pthread_setschedparam;
346 function sched_yield return int is
347 procedure pthread_yield;
348 pragma Import (C, pthread_yield, "pthread_yield");
354 -----------------------------
355 -- P1003.1c - Section 16 --
356 -----------------------------
358 function pthread_attr_init
359 (attributes : access pthread_attr_t) return int
361 function pthread_attr_create
362 (attributes : access pthread_attr_t) return int;
363 pragma Import (C, pthread_attr_create, "pthread_attr_create");
366 if pthread_attr_create (attributes) /= 0 then
371 end pthread_attr_init;
373 function pthread_attr_destroy
374 (attributes : access pthread_attr_t) return int
376 function pthread_attr_delete
377 (attributes : access pthread_attr_t) return int;
378 pragma Import (C, pthread_attr_delete, "pthread_attr_delete");
381 if pthread_attr_delete (attributes) /= 0 then
386 end pthread_attr_destroy;
388 function pthread_attr_setstacksize
389 (attr : access pthread_attr_t;
390 stacksize : size_t) return int
392 function pthread_attr_setstacksize_base
393 (attr : access pthread_attr_t;
394 stacksize : size_t) return int;
395 pragma Import (C, pthread_attr_setstacksize_base,
396 "pthread_attr_setstacksize");
399 if pthread_attr_setstacksize_base (attr, stacksize) /= 0 then
404 end pthread_attr_setstacksize;
406 function pthread_create
407 (thread : access pthread_t;
408 attributes : access pthread_attr_t;
409 start_routine : Thread_Body;
410 arg : System.Address) return int
412 function pthread_create_base
413 (thread : access pthread_t;
414 attributes : pthread_attr_t;
415 start_routine : Thread_Body;
416 arg : System.Address) return int;
417 pragma Import (C, pthread_create_base, "pthread_create");
420 if pthread_create_base
421 (thread, attributes.all, start_routine, arg) /= 0
429 --------------------------
430 -- POSIX.1c Section 17 --
431 --------------------------
433 function pthread_setspecific
434 (key : pthread_key_t;
435 value : System.Address) return int
437 function pthread_setspecific_base
438 (key : pthread_key_t;
439 value : System.Address) return int;
440 pragma Import (C, pthread_setspecific_base, "pthread_setspecific");
443 if pthread_setspecific_base (key, value) /= 0 then
448 end pthread_setspecific;
450 function pthread_getspecific (key : pthread_key_t) return System.Address is
451 function pthread_getspecific_base
452 (key : pthread_key_t;
453 value : access System.Address) return int;
454 pragma Import (C, pthread_getspecific_base, "pthread_getspecific");
455 Addr : aliased System.Address;
458 if pthread_getspecific_base (key, Addr'Access) /= 0 then
459 return System.Null_Address;
463 end pthread_getspecific;
465 function pthread_key_create
466 (key : access pthread_key_t;
467 destructor : destructor_pointer) return int
469 function pthread_keycreate
470 (key : access pthread_key_t;
471 destructor : destructor_pointer) return int;
472 pragma Import (C, pthread_keycreate, "pthread_keycreate");
475 if pthread_keycreate (key, destructor) /= 0 then
480 end pthread_key_create;
482 function Get_Stack_Base (thread : pthread_t) return Address is
483 pragma Warnings (Off, thread);
488 procedure pthread_init is
493 function intr_attach (sig : int; handler : isr_address) return long is
494 function c_signal (sig : int; handler : isr_address) return long;
495 pragma Import (C, c_signal, "signal");
497 return c_signal (sig, handler);
500 end System.OS_Interface;