1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ I N T E R F A C E --
11 -- Copyright (C) 1997-2002, Free Software Foundation, Inc. --
13 -- GNARL is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNARL; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNARL was developed by the GNARL team at Florida State University. It is --
32 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
33 -- State University (http://www.gnat.com). --
35 ------------------------------------------------------------------------------
37 -- This is a Solaris (FSU THREADS) version of this package.
39 -- This package includes all direct interfaces to OS services
40 -- that are needed by children of System.
42 -- PLEASE DO NOT add any with-clauses to this package
43 -- or remove the pragma Elaborate_Body.
44 -- It is designed to be a bottom-level (leaf) package.
47 package System.OS_Interface is
50 pragma Linker_Options ("-lgthreads");
51 pragma Linker_Options ("-lmalloc");
53 subtype int is Interfaces.C.int;
54 subtype short is Interfaces.C.short;
55 subtype long is Interfaces.C.long;
56 subtype unsigned is Interfaces.C.unsigned;
57 subtype unsigned_short is Interfaces.C.unsigned_short;
58 subtype unsigned_long is Interfaces.C.unsigned_long;
59 subtype unsigned_char is Interfaces.C.unsigned_char;
60 subtype plain_char is Interfaces.C.plain_char;
61 subtype size_t is Interfaces.C.size_t;
67 function errno return int;
68 pragma Import (C, errno, "__get_errno");
70 EAGAIN : constant := 11;
71 EINTR : constant := 4;
72 EINVAL : constant := 22;
73 ENOMEM : constant := 12;
74 ETIMEDOUT : constant := 145;
80 Max_Interrupt : constant := 45;
81 type Signal is new int range 0 .. Max_Interrupt;
82 for Signal'Size use int'Size;
84 SIGHUP : constant := 1; -- hangup
85 SIGINT : constant := 2; -- interrupt (rubout)
86 SIGQUIT : constant := 3; -- quit (ASCD FS)
87 SIGILL : constant := 4; -- illegal instruction (not reset)
88 SIGTRAP : constant := 5; -- trace trap (not reset)
89 SIGIOT : constant := 6; -- IOT instruction
90 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
91 SIGEMT : constant := 7; -- EMT instruction
92 SIGFPE : constant := 8; -- floating point exception
93 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
94 SIGBUS : constant := 10; -- bus error
95 SIGSEGV : constant := 11; -- segmentation violation
96 SIGSYS : constant := 12; -- bad argument to system call
97 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
98 SIGALRM : constant := 14; -- alarm clock
99 SIGTERM : constant := 15; -- software termination signal from kill
100 SIGUSR1 : constant := 16; -- user defined signal 1
101 SIGUSR2 : constant := 17; -- user defined signal 2
102 SIGCLD : constant := 18; -- alias for SIGCHLD
103 SIGCHLD : constant := 18; -- child status change
104 SIGPWR : constant := 19; -- power-fail restart
105 SIGWINCH : constant := 20; -- window size change
106 SIGURG : constant := 21; -- urgent condition on IO channel
107 SIGPOLL : constant := 22; -- pollable event occurred
108 SIGIO : constant := 22; -- I/O possible (Solaris SIGPOLL alias)
109 SIGSTOP : constant := 23; -- stop (cannot be caught or ignored)
110 SIGTSTP : constant := 24; -- user stop requested from tty
111 SIGCONT : constant := 25; -- stopped process has been continued
112 SIGTTIN : constant := 26; -- background tty read attempted
113 SIGTTOU : constant := 27; -- background tty write attempted
114 SIGVTALRM : constant := 28; -- virtual timer expired
115 SIGPROF : constant := 29; -- profiling timer expired
116 SIGXCPU : constant := 30; -- CPU time limit exceeded
117 SIGXFSZ : constant := 31; -- filesize limit exceeded
118 SIGWAITING : constant := 32; -- process's lwps blocked (Solaris)
119 SIGLWP : constant := 33; -- used by thread library (Solaris)
120 SIGFREEZE : constant := 34; -- used by CPR (Solaris)
121 SIGTHAW : constant := 35; -- used by CPR (Solaris)
122 SIGCANCEL : constant := 36; -- used for thread cancel (Solaris)
123 SIGRTMIN : constant := 38; -- first (highest-priority) realtime signal
124 SIGRTMAX : constant := 45; -- last (lowest-priority) realtime signal
126 type Signal_Set is array (Natural range <>) of Signal;
128 Unmasked : constant Signal_Set :=
129 (SIGTRAP, SIGLWP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF);
131 Reserved : constant Signal_Set :=
132 (SIGKILL, SIGSTOP, SIGALRM, SIGVTALRM, SIGWAITING, SIGRTMAX);
134 type sigset_t is private;
136 function sigaddset (set : access sigset_t; sig : Signal) return int;
137 pragma Import (C, sigaddset, "sigaddset");
139 function sigdelset (set : access sigset_t; sig : Signal) return int;
140 pragma Import (C, sigdelset, "sigdelset");
142 function sigfillset (set : access sigset_t) return int;
143 pragma Import (C, sigfillset, "sigfillset");
145 function sigismember (set : access sigset_t; sig : Signal) return int;
146 pragma Import (C, sigismember, "sigismember");
148 function sigemptyset (set : access sigset_t) return int;
149 pragma Import (C, sigemptyset, "sigemptyset");
151 type union_type_3 is new String (1 .. 116);
152 type siginfo_t is record
156 X_data : union_type_3;
158 pragma Convention (C, siginfo_t);
160 -- The types mcontext_t and gregset_t are part of the ucontext_t
161 -- information, which is specific to Solaris2.4 for SPARC
162 -- The ucontext_t info seems to be used by the handler
163 -- for SIGSEGV to decide whether it is a Storage_Error (stack overflow) or
164 -- a Constraint_Error (bad pointer). The original code that did this
165 -- is suspect, so it is not clear whether we really need this part of
166 -- the signal context information, or perhaps something else.
167 -- More analysis is needed, after which these declarations may need to
170 EMT_TAGOVF : constant := 1; -- tag overflow
171 FPE_INTDIV : constant := 1; -- integer divide by zero
172 FPE_INTOVF : constant := 2; -- integer overflow
173 FPE_FLTDIV : constant := 3; -- floating point divide by zero
174 FPE_FLTOVF : constant := 4; -- floating point overflow
175 FPE_FLTUND : constant := 5; -- floating point underflow
176 FPE_FLTRES : constant := 6; -- floating point inexact result
177 FPE_FLTINV : constant := 7; -- invalid floating point operation
178 FPE_FLTSUB : constant := 8; -- subscript out of range
180 SEGV_MAPERR : constant := 1; -- address not mapped to object
181 SEGV_ACCERR : constant := 2; -- invalid permissions
183 BUS_ADRALN : constant := 1; -- invalid address alignment
184 BUS_ADRERR : constant := 2; -- non-existent physical address
185 BUS_OBJERR : constant := 3; -- object specific hardware error
187 ILL_ILLOPC : constant := 1; -- illegal opcode
188 ILL_ILLOPN : constant := 2; -- illegal operand
189 ILL_ILLADR : constant := 3; -- illegal addressing mode
190 ILL_ILLTRP : constant := 4; -- illegal trap
191 ILL_PRVOPC : constant := 5; -- privileged opcode
192 ILL_PRVREG : constant := 6; -- privileged register
193 ILL_COPROC : constant := 7; -- co-processor
194 ILL_BADSTK : constant := 8; -- bad stack
196 type greg_t is new int;
198 type gregset_t is array (Integer range 0 .. 18) of greg_t;
200 REG_O0 : constant := 11;
201 -- index of saved register O0 in ucontext.uc_mcontext.gregs array
203 type union_type_2 is new String (1 .. 128);
204 type record_type_1 is record
205 fpu_fr : union_type_2;
206 fpu_q : System.Address;
208 fpu_qcnt : unsigned_char;
209 fpu_q_entrysize : unsigned_char;
210 fpu_en : unsigned_char;
212 pragma Convention (C, record_type_1);
213 type array_type_7 is array (Integer range 0 .. 20) of long;
214 type mcontext_t is record
216 gwins : System.Address;
217 fpregs : record_type_1;
218 filler : array_type_7;
220 pragma Convention (C, mcontext_t);
222 type record_type_2 is record
223 ss_sp : System.Address;
227 pragma Convention (C, record_type_2);
228 type array_type_8 is array (Integer range 0 .. 22) of long;
229 type ucontext_t is record
230 uc_flags : unsigned_long;
231 uc_link : System.Address;
232 uc_sigmask : sigset_t;
233 uc_stack : record_type_2;
234 uc_mcontext : mcontext_t;
235 uc_filler : array_type_8;
237 pragma Convention (C, ucontext_t);
239 type Signal_Handler is access procedure
241 info : access siginfo_t;
242 context : access ucontext_t);
244 type union_type_1 is new plain_char;
245 type array_type_2 is array (Integer range 0 .. 1) of int;
246 type struct_sigaction is record
248 sa_handler : System.Address;
250 sa_resv : array_type_2;
252 pragma Convention (C, struct_sigaction);
253 type struct_sigaction_ptr is access all struct_sigaction;
255 SIG_BLOCK : constant := 1;
256 SIG_UNBLOCK : constant := 2;
257 SIG_SETMASK : constant := 3;
259 SIG_DFL : constant := 0;
260 SIG_IGN : constant := 1;
264 act : struct_sigaction_ptr;
265 oact : struct_sigaction_ptr) return int;
266 pragma Import (C, sigaction, "sigaction");
272 Time_Slice_Supported : constant Boolean := False;
273 -- Indicates wether time slicing is supported (i.e FSU threads have been
274 -- compiled with DEF_RR)
276 type timespec is private;
278 type clockid_t is private;
280 CLOCK_REALTIME : constant clockid_t;
282 function clock_gettime
283 (clock_id : clockid_t;
284 tp : access timespec) return int;
285 pragma Import (C, clock_gettime, "clock_gettime");
287 function To_Duration (TS : timespec) return Duration;
288 pragma Inline (To_Duration);
290 function To_Timespec (D : Duration) return timespec;
291 pragma Inline (To_Timespec);
293 type struct_timeval is private;
295 function To_Duration (TV : struct_timeval) return Duration;
296 pragma Inline (To_Duration);
298 function To_Timeval (D : Duration) return struct_timeval;
299 pragma Inline (To_Timeval);
301 -------------------------
302 -- Priority Scheduling --
303 -------------------------
305 SCHED_FIFO : constant := 0;
306 SCHED_RR : constant := 1;
307 SCHED_OTHER : constant := 2;
313 type pid_t is private;
315 function kill (pid : pid_t; sig : Signal) return int;
316 pragma Import (C, kill, "kill");
318 function getpid return pid_t;
319 pragma Import (C, getpid, "getpid");
325 function lwp_self return System.Address;
326 -- lwp_self does not exist on this thread library, revert to pthread_self
327 -- which is the closest approximation (with getpid). This function is
328 -- needed to share 7staprop.adb across POSIX-like targets.
329 pragma Import (C, lwp_self, "pthread_self");
335 type Thread_Body is access
336 function (arg : System.Address) return System.Address;
337 type pthread_t is private;
338 subtype Thread_Id is pthread_t;
340 type pthread_mutex_t is limited private;
341 type pthread_cond_t is limited private;
342 type pthread_attr_t is limited private;
343 type pthread_mutexattr_t is limited private;
344 type pthread_condattr_t is limited private;
345 type pthread_key_t is private;
347 PTHREAD_CREATE_DETACHED : constant := 1;
353 Stack_Base_Available : constant Boolean := False;
354 -- Indicates wether the stack base is available on this target.
355 -- This allows us to share s-osinte.adb between all the FSU run time.
356 -- Note that this value can only be true if pthread_t has a complete
357 -- definition that corresponds exactly to the C header files.
359 function Get_Stack_Base (thread : pthread_t) return Address;
360 pragma Inline (Get_Stack_Base);
361 -- returns the stack base of the specified thread.
362 -- Only call this function when Stack_Base_Available is True.
364 function Get_Page_Size return size_t;
365 function Get_Page_Size return Address;
366 pragma Import (C, Get_Page_Size, "getpagesize");
367 -- returns the size of a page, or 0 if this is not relevant on this
370 PROT_NONE : constant := 0;
371 PROT_READ : constant := 1;
372 PROT_WRITE : constant := 2;
373 PROT_EXEC : constant := 4;
374 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
376 PROT_ON : constant := PROT_NONE;
377 PROT_OFF : constant := PROT_ALL;
379 function mprotect (addr : Address; len : size_t; prot : int) return int;
380 pragma Import (C, mprotect);
382 ---------------------------------------
383 -- Nonstandard Thread Initialization --
384 ---------------------------------------
386 procedure pthread_init;
387 -- FSU_THREADS requires pthread_init, which is nonstandard
388 -- and this should be invoked during the elaboration of s-taprop.adb
389 pragma Import (C, pthread_init, "pthread_init");
391 -------------------------
392 -- POSIX.1c Section 3 --
393 -------------------------
395 function sigwait (set : access sigset_t; sig : access Signal) return int;
396 -- FSU_THREADS has a nonstandard sigwait
398 function pthread_kill (thread : pthread_t; sig : Signal) return int;
399 pragma Import (C, pthread_kill, "pthread_kill");
401 type sigset_t_ptr is access all sigset_t;
403 function pthread_sigmask
406 oset : sigset_t_ptr) return int;
407 pragma Import (C, pthread_sigmask, "sigprocmask");
409 --------------------------
410 -- POSIX.1c Section 11 --
411 --------------------------
413 function pthread_mutexattr_init
414 (attr : access pthread_mutexattr_t) return int;
415 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
417 function pthread_mutexattr_destroy
418 (attr : access pthread_mutexattr_t) return int;
419 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
421 function pthread_mutex_init
422 (mutex : access pthread_mutex_t;
423 attr : access pthread_mutexattr_t) return int;
424 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
426 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
427 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
429 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
430 -- FSU_THREADS has nonstandard pthread_mutex_lock
432 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
433 -- FSU_THREADS has nonstandard pthread_mutex_lock
435 function pthread_condattr_init
436 (attr : access pthread_condattr_t) return int;
437 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
439 function pthread_condattr_destroy
440 (attr : access pthread_condattr_t) return int;
441 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
443 function pthread_cond_init
444 (cond : access pthread_cond_t;
445 attr : access pthread_condattr_t) return int;
446 pragma Import (C, pthread_cond_init, "pthread_cond_init");
448 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
449 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
451 function pthread_cond_signal (cond : access pthread_cond_t) return int;
452 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
454 function pthread_cond_wait
455 (cond : access pthread_cond_t;
456 mutex : access pthread_mutex_t) return int;
457 -- FSU_THREADS has a nonstandard pthread_cond_wait
459 function pthread_cond_timedwait
460 (cond : access pthread_cond_t;
461 mutex : access pthread_mutex_t;
462 abstime : access timespec) return int;
463 -- FSU_THREADS has a nonstandard pthread_cond_timedwait
465 Relative_Timed_Wait : constant Boolean := False;
466 -- pthread_cond_timedwait requires an absolute delay time
468 --------------------------
469 -- POSIX.1c Section 13 --
470 --------------------------
472 PTHREAD_PRIO_NONE : constant := 0;
473 PTHREAD_PRIO_PROTECT : constant := 2;
474 PTHREAD_PRIO_INHERIT : constant := 1;
476 function pthread_mutexattr_setprotocol
477 (attr : access pthread_mutexattr_t;
478 protocol : int) return int;
479 pragma Import (C, pthread_mutexattr_setprotocol);
481 function pthread_mutexattr_setprioceiling
482 (attr : access pthread_mutexattr_t;
483 prioceiling : int) return int;
485 (C, pthread_mutexattr_setprioceiling,
486 "pthread_mutexattr_setprio_ceiling");
488 type struct_sched_param is record
489 sched_priority : int; -- scheduling priority
492 function pthread_setschedparam
495 param : access struct_sched_param) return int;
496 -- FSU_THREADS does not have pthread_setschedparam
498 function pthread_attr_setscope
499 (attr : access pthread_attr_t;
500 contentionscope : int) return int;
501 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
503 function pthread_attr_setinheritsched
504 (attr : access pthread_attr_t;
505 inheritsched : int) return int;
506 pragma Import (C, pthread_attr_setinheritsched);
508 function pthread_attr_setschedpolicy
509 (attr : access pthread_attr_t;
510 policy : int) return int;
511 pragma Import (C, pthread_attr_setschedpolicy, "pthread_attr_setsched");
513 function sched_yield return int;
514 -- FSU_THREADS does not have sched_yield;
516 ---------------------------
517 -- P1003.1c - Section 16 --
518 ---------------------------
520 function pthread_attr_init (attributes : access pthread_attr_t) return int;
521 pragma Import (C, pthread_attr_init, "pthread_attr_init");
523 function pthread_attr_destroy
524 (attributes : access pthread_attr_t) return int;
525 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
527 function pthread_attr_setdetachstate
528 (attr : access pthread_attr_t;
529 detachstate : int) return int;
530 -- FSU_THREADS has a nonstandard pthread_attr_setdetachstate
532 function pthread_attr_setstacksize
533 (attr : access pthread_attr_t;
534 stacksize : size_t) return int;
535 pragma Import (C, pthread_attr_setstacksize);
537 function pthread_create
538 (thread : access pthread_t;
539 attributes : access pthread_attr_t;
540 start_routine : Thread_Body;
541 arg : System.Address) return int;
542 pragma Import (C, pthread_create, "pthread_create");
544 procedure pthread_exit (status : System.Address);
545 pragma Import (C, pthread_exit, "pthread_exit");
547 function pthread_self return pthread_t;
548 pragma Import (C, pthread_self, "pthread_self");
550 --------------------------
551 -- POSIX.1c Section 17 --
552 --------------------------
554 function pthread_setspecific
555 (key : pthread_key_t;
556 value : System.Address) return int;
557 pragma Import (C, pthread_setspecific, "pthread_setspecific");
559 function pthread_getspecific (key : pthread_key_t) return System.Address;
560 -- FSU_THREADS has a nonstandard pthread_getspecific
562 type destructor_pointer is access procedure (arg : System.Address);
564 function pthread_key_create
565 (key : access pthread_key_t;
566 destructor : destructor_pointer) return int;
567 pragma Import (C, pthread_key_create, "pthread_key_create");
571 type array_type_1 is array (Integer range 0 .. 3) of unsigned_long;
572 type sigset_t is record
573 X_X_sigbits : array_type_1;
575 pragma Convention (C, sigset_t);
577 type pid_t is new long;
579 type time_t is new long;
581 type timespec is record
585 pragma Convention (C, timespec);
587 type clockid_t is new int;
588 CLOCK_REALTIME : constant clockid_t := 0;
590 type struct_timeval is record
594 pragma Convention (C, struct_timeval);
596 type pthread_attr_t is record
599 contentionscope : int;
604 starttime : timespec;
608 pragma Convention (C, pthread_attr_t);
610 type pthread_condattr_t is record
613 pragma Convention (C, pthread_condattr_t);
615 type pthread_mutexattr_t is record
620 pragma Convention (C, pthread_mutexattr_t);
622 type sigjmp_buf is array (Integer range 0 .. 18) of int;
624 type pthread_t_struct is record
625 context : sigjmp_buf;
629 stack_base : System.Address;
631 pragma Convention (C, pthread_t_struct);
633 type pthread_t is access all pthread_t_struct;
635 type queue_t is record
636 head : System.Address;
637 tail : System.Address;
639 pragma Convention (C, queue_t);
641 type pthread_mutex_t is record
644 owner : System.Address;
648 prev_max_ceiling_prio : int;
650 pragma Convention (C, pthread_mutex_t);
652 type pthread_cond_t is record
656 mutex : System.Address;
658 pragma Convention (C, pthread_cond_t);
660 type pthread_key_t is new int;
662 end System.OS_Interface;