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-2008, Free Software Foundation, Inc. --
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 HPUX 11.0 (Native THREADS) version of this package
37 -- This package encapsulates all direct interfaces to OS services that are
38 -- needed by the tasking run-time (libgnarl).
40 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
41 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
43 with Ada.Unchecked_Conversion;
47 package System.OS_Interface is
50 pragma Linker_Options ("-lpthread");
52 subtype int is Interfaces.C.int;
53 subtype short is Interfaces.C.short;
54 subtype long is Interfaces.C.long;
55 subtype unsigned is Interfaces.C.unsigned;
56 subtype unsigned_short is Interfaces.C.unsigned_short;
57 subtype unsigned_long is Interfaces.C.unsigned_long;
58 subtype unsigned_char is Interfaces.C.unsigned_char;
59 subtype plain_char is Interfaces.C.plain_char;
60 subtype size_t is Interfaces.C.size_t;
66 function errno return int;
67 pragma Import (C, errno, "__get_errno");
69 EAGAIN : constant := 11;
70 EINTR : constant := 4;
71 EINVAL : constant := 22;
72 ENOMEM : constant := 12;
73 ETIMEDOUT : constant := 238;
79 Max_Interrupt : constant := 44;
80 type Signal is new int range 0 .. Max_Interrupt;
81 for Signal'Size use int'Size;
83 SIGHUP : constant := 1; -- hangup
84 SIGINT : constant := 2; -- interrupt (rubout)
85 SIGQUIT : constant := 3; -- quit (ASCD FS)
86 SIGILL : constant := 4; -- illegal instruction (not reset)
87 SIGTRAP : constant := 5; -- trace trap (not reset)
88 SIGIOT : constant := 6; -- IOT instruction
89 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
90 SIGEMT : constant := 7; -- EMT instruction
91 SIGFPE : constant := 8; -- floating point exception
92 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
93 SIGBUS : constant := 10; -- bus error
94 SIGSEGV : constant := 11; -- segmentation violation
95 SIGSYS : constant := 12; -- bad argument to system call
96 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
97 SIGALRM : constant := 14; -- alarm clock
98 SIGTERM : constant := 15; -- software termination signal from kill
99 SIGUSR1 : constant := 16; -- user defined signal 1
100 SIGUSR2 : constant := 17; -- user defined signal 2
101 SIGCLD : constant := 18; -- alias for SIGCHLD
102 SIGCHLD : constant := 18; -- child status change
103 SIGPWR : constant := 19; -- power-fail restart
104 SIGVTALRM : constant := 20; -- virtual timer alarm
105 SIGPROF : constant := 21; -- profiling timer alarm
106 SIGIO : constant := 22; -- asynchronous I/O
107 SIGPOLL : constant := 22; -- pollable event occurred
108 SIGWINCH : constant := 23; -- window size change
109 SIGSTOP : constant := 24; -- stop (cannot be caught or ignored)
110 SIGTSTP : constant := 25; -- user stop requested from tty
111 SIGCONT : constant := 26; -- stopped process has been continued
112 SIGTTIN : constant := 27; -- background tty read attempted
113 SIGTTOU : constant := 28; -- background tty write attempted
114 SIGURG : constant := 29; -- urgent condition on IO channel
115 SIGLOST : constant := 30; -- remote lock lost (NFS)
116 SIGDIL : constant := 32; -- DIL signal
117 SIGXCPU : constant := 33; -- CPU time limit exceeded (setrlimit)
118 SIGXFSZ : constant := 34; -- file size limit exceeded (setrlimit)
119 SIGCANCEL : constant := 35; -- used for pthread cancellation.
120 SIGGFAULT : constant := 36; -- Graphics framebuffer fault
122 SIGADAABORT : constant := SIGABRT;
123 -- Note: on other targets, we usually use SIGABRT, but on HPUX, it
124 -- appears that SIGABRT can't be used in sigwait(), so we use SIGTERM.
125 -- Do we use SIGTERM or SIGABRT???
127 type Signal_Set is array (Natural range <>) of Signal;
129 Unmasked : constant Signal_Set :=
130 (SIGABRT, SIGPIPE, SIGBUS, SIGTRAP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF,
131 SIGALRM, SIGVTALRM, SIGIO, SIGCHLD);
133 Reserved : constant Signal_Set := (SIGKILL, SIGSTOP);
135 type sigset_t is private;
137 function sigaddset (set : access sigset_t; sig : Signal) return int;
138 pragma Import (C, sigaddset, "sigaddset");
140 function sigdelset (set : access sigset_t; sig : Signal) return int;
141 pragma Import (C, sigdelset, "sigdelset");
143 function sigfillset (set : access sigset_t) return int;
144 pragma Import (C, sigfillset, "sigfillset");
146 function sigismember (set : access sigset_t; sig : Signal) return int;
147 pragma Import (C, sigismember, "sigismember");
149 function sigemptyset (set : access sigset_t) return int;
150 pragma Import (C, sigemptyset, "sigemptyset");
152 type struct_sigaction is record
153 sa_handler : System.Address;
157 pragma Convention (C, struct_sigaction);
158 type struct_sigaction_ptr is access all struct_sigaction;
160 SA_SIGINFO : constant := 16#10#;
161 SA_ONSTACK : constant := 16#01#;
163 SIG_BLOCK : constant := 0;
164 SIG_UNBLOCK : constant := 1;
165 SIG_SETMASK : constant := 2;
167 SIG_DFL : constant := 0;
168 SIG_IGN : constant := 1;
172 act : struct_sigaction_ptr;
173 oact : struct_sigaction_ptr) return int;
174 pragma Import (C, sigaction, "sigaction");
180 Time_Slice_Supported : constant Boolean := True;
181 -- Indicates whether time slicing is supported
183 type timespec is private;
185 type clockid_t is private;
187 CLOCK_REALTIME : constant clockid_t;
189 function clock_gettime
190 (clock_id : clockid_t;
191 tp : access timespec) return int;
192 pragma Import (C, clock_gettime, "clock_gettime");
194 function clock_getres
195 (clock_id : clockid_t;
196 res : access timespec) return int;
197 pragma Import (C, clock_getres, "clock_getres");
199 function To_Duration (TS : timespec) return Duration;
200 pragma Inline (To_Duration);
202 function To_Timespec (D : Duration) return timespec;
203 pragma Inline (To_Timespec);
205 type struct_timezone is record
206 tz_minuteswest : int;
209 pragma Convention (C, struct_timezone);
210 type struct_timezone_ptr is access all struct_timezone;
212 type struct_timeval is private;
213 -- This is needed on systems that do not have clock_gettime()
214 -- but do have gettimeofday().
216 function To_Duration (TV : struct_timeval) return Duration;
217 pragma Inline (To_Duration);
219 function To_Timeval (D : Duration) return struct_timeval;
220 pragma Inline (To_Timeval);
222 -------------------------
223 -- Priority Scheduling --
224 -------------------------
226 SCHED_FIFO : constant := 0;
227 SCHED_RR : constant := 1;
228 SCHED_OTHER : constant := 2;
230 function To_Target_Priority
231 (Prio : System.Any_Priority) return Interfaces.C.int;
232 -- Maps System.Any_Priority to a POSIX priority
238 type pid_t is private;
240 function kill (pid : pid_t; sig : Signal) return int;
241 pragma Import (C, kill, "kill");
243 function getpid return pid_t;
244 pragma Import (C, getpid, "getpid");
250 function lwp_self return System.Address;
251 pragma Import (C, lwp_self, "_lwp_self");
257 type Thread_Body is access
258 function (arg : System.Address) return System.Address;
259 pragma Convention (C, Thread_Body);
261 function Thread_Body_Access is new
262 Ada.Unchecked_Conversion (System.Address, Thread_Body);
264 type pthread_t is private;
265 subtype Thread_Id is pthread_t;
267 type pthread_mutex_t is limited private;
268 type pthread_cond_t is limited private;
269 type pthread_attr_t is limited private;
270 type pthread_mutexattr_t is limited private;
271 type pthread_condattr_t is limited private;
272 type pthread_key_t is private;
274 PTHREAD_CREATE_DETACHED : constant := 16#de#;
276 PTHREAD_SCOPE_PROCESS : constant := 2;
277 PTHREAD_SCOPE_SYSTEM : constant := 1;
283 type stack_t is record
284 ss_sp : System.Address;
288 pragma Convention (C, stack_t);
291 (ss : not null access stack_t;
292 oss : access stack_t) return int;
293 pragma Import (C, sigaltstack, "sigaltstack");
295 Alternate_Stack : aliased System.Address;
296 pragma Import (C, Alternate_Stack, "__gnat_alternate_stack");
297 -- The alternate signal stack for stack overflows
299 Alternate_Stack_Size : constant := 16 * 1024;
300 -- This must be in keeping with init.c:__gnat_alternate_stack
302 Stack_Base_Available : constant Boolean := False;
303 -- Indicates whether the stack base is available on this target
305 function Get_Stack_Base (thread : pthread_t) return Address;
306 pragma Inline (Get_Stack_Base);
307 -- Returns the stack base of the specified thread. Only call this function
308 -- when Stack_Base_Available is True.
310 function Get_Page_Size return size_t;
311 function Get_Page_Size return Address;
312 pragma Import (C, Get_Page_Size, "getpagesize");
313 -- Returns the size of a page, or 0 if this is not relevant on this target
315 PROT_NONE : constant := 0;
316 PROT_READ : constant := 1;
317 PROT_WRITE : constant := 2;
318 PROT_EXEC : constant := 4;
319 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
320 PROT_ON : constant := PROT_READ;
321 PROT_OFF : constant := PROT_ALL;
323 function mprotect (addr : Address; len : size_t; prot : int) return int;
324 pragma Import (C, mprotect);
326 ---------------------------------------
327 -- Nonstandard Thread Initialization --
328 ---------------------------------------
330 procedure pthread_init;
331 pragma Inline (pthread_init);
332 -- This is a dummy procedure to share some GNULLI files
334 -------------------------
335 -- POSIX.1c Section 3 --
336 -------------------------
339 (set : access sigset_t;
340 sig : access Signal) return int;
341 pragma Import (C, sigwait, "sigwait");
343 function pthread_kill
345 sig : Signal) return int;
346 pragma Import (C, pthread_kill, "pthread_kill");
348 function pthread_sigmask
350 set : access sigset_t;
351 oset : access sigset_t) return int;
352 pragma Import (C, pthread_sigmask, "pthread_sigmask");
354 --------------------------
355 -- POSIX.1c Section 11 --
356 --------------------------
358 function pthread_mutexattr_init
359 (attr : access pthread_mutexattr_t) return int;
360 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
362 function pthread_mutexattr_destroy
363 (attr : access pthread_mutexattr_t) return int;
364 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
366 function pthread_mutex_init
367 (mutex : access pthread_mutex_t;
368 attr : access pthread_mutexattr_t) return int;
369 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
371 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
372 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
374 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
375 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
377 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
378 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
380 function pthread_condattr_init
381 (attr : access pthread_condattr_t) return int;
382 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
384 function pthread_condattr_destroy
385 (attr : access pthread_condattr_t) return int;
386 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
388 function pthread_cond_init
389 (cond : access pthread_cond_t;
390 attr : access pthread_condattr_t) return int;
391 pragma Import (C, pthread_cond_init, "pthread_cond_init");
393 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
394 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
396 function pthread_cond_signal (cond : access pthread_cond_t) return int;
397 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
399 function pthread_cond_wait
400 (cond : access pthread_cond_t;
401 mutex : access pthread_mutex_t) return int;
402 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
404 function pthread_cond_timedwait
405 (cond : access pthread_cond_t;
406 mutex : access pthread_mutex_t;
407 abstime : access timespec) return int;
408 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
410 Relative_Timed_Wait : constant Boolean := False;
411 -- pthread_cond_timedwait requires an absolute delay time
413 --------------------------
414 -- POSIX.1c Section 13 --
415 --------------------------
417 PTHREAD_PRIO_NONE : constant := 16#100#;
418 PTHREAD_PRIO_PROTECT : constant := 16#200#;
419 PTHREAD_PRIO_INHERIT : constant := 16#400#;
421 function pthread_mutexattr_setprotocol
422 (attr : access pthread_mutexattr_t;
423 protocol : int) return int;
424 pragma Import (C, pthread_mutexattr_setprotocol);
426 function pthread_mutexattr_setprioceiling
427 (attr : access pthread_mutexattr_t;
428 prioceiling : int) return int;
429 pragma Import (C, pthread_mutexattr_setprioceiling);
431 type Array_7_Int is array (0 .. 6) of int;
432 type struct_sched_param is record
433 sched_priority : int;
434 sched_reserved : Array_7_Int;
437 function pthread_setschedparam
440 param : access struct_sched_param)
442 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
444 function pthread_attr_setscope
445 (attr : access pthread_attr_t;
446 contentionscope : int) return int;
447 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
449 function pthread_attr_setinheritsched
450 (attr : access pthread_attr_t;
451 inheritsched : int) return int;
452 pragma Import (C, pthread_attr_setinheritsched);
454 function pthread_attr_setschedpolicy
455 (attr : access pthread_attr_t;
456 policy : int) return int;
457 pragma Import (C, pthread_attr_setschedpolicy);
459 function sched_yield return int;
460 pragma Import (C, sched_yield, "sched_yield");
462 --------------------------
463 -- P1003.1c Section 16 --
464 --------------------------
466 function pthread_attr_init
467 (attributes : access pthread_attr_t) return int;
468 pragma Import (C, pthread_attr_init, "__pthread_attr_init_system");
470 function pthread_attr_destroy
471 (attributes : access pthread_attr_t) return int;
472 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
474 function pthread_attr_setdetachstate
475 (attr : access pthread_attr_t;
476 detachstate : int) return int;
477 pragma Import (C, pthread_attr_setdetachstate);
479 function pthread_attr_setstacksize
480 (attr : access pthread_attr_t;
481 stacksize : size_t) return int;
482 pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
484 function pthread_create
485 (thread : access pthread_t;
486 attributes : access pthread_attr_t;
487 start_routine : Thread_Body;
488 arg : System.Address) return int;
489 pragma Import (C, pthread_create, "__pthread_create_system");
491 procedure pthread_exit (status : System.Address);
492 pragma Import (C, pthread_exit, "pthread_exit");
494 function pthread_self return pthread_t;
495 pragma Import (C, pthread_self, "pthread_self");
497 --------------------------
498 -- POSIX.1c Section 17 --
499 --------------------------
501 function pthread_setspecific
502 (key : pthread_key_t;
503 value : System.Address) return int;
504 pragma Import (C, pthread_setspecific, "pthread_setspecific");
506 function pthread_getspecific (key : pthread_key_t) return System.Address;
507 pragma Import (C, pthread_getspecific, "pthread_getspecific");
509 type destructor_pointer is access procedure (arg : System.Address);
510 pragma Convention (C, destructor_pointer);
512 function pthread_key_create
513 (key : access pthread_key_t;
514 destructor : destructor_pointer) return int;
515 pragma Import (C, pthread_key_create, "pthread_key_create");
519 type unsigned_int_array_8 is array (0 .. 7) of unsigned;
520 type sigset_t is record
521 sigset : unsigned_int_array_8;
523 pragma Convention (C_Pass_By_Copy, sigset_t);
525 type pid_t is new int;
527 type time_t is new long;
529 type timespec is record
533 pragma Convention (C, timespec);
535 type clockid_t is new int;
536 CLOCK_REALTIME : constant clockid_t := 1;
538 type struct_timeval is record
542 pragma Convention (C, struct_timeval);
544 type pthread_attr_t is new int;
545 type pthread_condattr_t is new int;
546 type pthread_mutexattr_t is new int;
547 type pthread_t is new int;
549 type short_array is array (Natural range <>) of short;
550 type int_array is array (Natural range <>) of int;
552 type pthread_mutex_t is record
553 m_short : short_array (0 .. 1);
555 m_int1 : int_array (0 .. 3);
559 -- actually m_ptr is a void*, and on 32 bit ABI, m_pad is added so that
560 -- this field takes 64 bits. On 64 bit ABI, m_pad is gone, and m_ptr is
561 -- a 64 bit void*. Assume int'Size = 32.
563 m_int2 : int_array (0 .. 1);
564 m_int3 : int_array (0 .. 3);
565 m_short2 : short_array (0 .. 1);
566 m_int4 : int_array (0 .. 4);
567 m_int5 : int_array (0 .. 1);
569 for pthread_mutex_t'Alignment use System.Address'Alignment;
570 pragma Convention (C, pthread_mutex_t);
572 type pthread_cond_t is record
573 c_short : short_array (0 .. 1);
575 c_int1 : int_array (0 .. 3);
577 m_ptr : int; -- see comment in pthread_mutex_t
578 c_int2 : int_array (0 .. 1);
579 c_int3 : int_array (0 .. 1);
580 c_int4 : int_array (0 .. 1);
582 for pthread_cond_t'Alignment use System.Address'Alignment;
583 pragma Convention (C, pthread_cond_t);
585 type pthread_key_t is new int;
587 end System.OS_Interface;