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) 1999-2001 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 UnixWare (Native THREADS) version of this package.
39 -- This package encapsulates 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 ("-lthread");
52 subtype int is Interfaces.C.int;
53 subtype char is Interfaces.C.char;
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 := 34;
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; -- all LWPs blocked interruptibly notific.
119 SIGLWP : constant := 33; -- signal reserved for thread lib impl.
120 SIGAIO : constant := 34; -- Asynchronous I/O signal
122 SIGADAABORT : constant := SIGABRT;
123 -- Change this if you want to use another signal for task abort.
124 -- SIGTERM might be a good one.
126 type Signal_Set is array (Natural range <>) of Signal;
128 Unmasked : constant Signal_Set :=
129 (SIGTRAP, SIGLWP, SIGWAITING, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF);
130 Reserved : constant Signal_Set := (SIGABRT, SIGKILL, SIGSTOP);
132 type sigset_t is private;
134 function sigaddset (set : access sigset_t; sig : Signal) return int;
135 pragma Import (C, sigaddset, "sigaddset");
137 function sigdelset (set : access sigset_t; sig : Signal) return int;
138 pragma Import (C, sigdelset, "sigdelset");
140 function sigfillset (set : access sigset_t) return int;
141 pragma Import (C, sigfillset, "sigfillset");
143 function sigismember (set : access sigset_t; sig : Signal) return int;
144 pragma Import (C, sigismember, "sigismember");
146 function sigemptyset (set : access sigset_t) return int;
147 pragma Import (C, sigemptyset, "sigemptyset");
149 type struct_sigaction is record
151 sa_handler : System.Address;
156 pragma Convention (C, struct_sigaction);
157 type struct_sigaction_ptr is access all struct_sigaction;
159 SIG_BLOCK : constant := 1;
160 SIG_UNBLOCK : constant := 2;
161 SIG_SETMASK : constant := 3;
163 SIG_DFL : constant := 0;
164 SIG_IGN : constant := 1;
165 -- SIG_ERR : constant := -1;
170 act : struct_sigaction_ptr;
171 oact : struct_sigaction_ptr) return int;
172 pragma Import (C, sigaction, "sigaction");
178 Time_Slice_Supported : constant Boolean := False;
179 -- Indicates wether time slicing is supported
181 type timespec is private;
183 type clockid_t is private;
185 CLOCK_REALTIME : constant clockid_t;
187 function clock_gettime
188 (clock_id : clockid_t;
189 tp : access timespec) return int;
190 -- UnixWare threads don't have clock_gettime
191 -- We instead use gettimeofday()
193 function To_Duration (TS : timespec) return Duration;
194 pragma Inline (To_Duration);
196 function To_Timespec (D : Duration) return timespec;
197 pragma Inline (To_Timespec);
199 type struct_timezone is record
200 tz_minuteswest : int;
203 pragma Convention (C, struct_timezone);
204 type struct_timezone_ptr is access all struct_timezone;
206 type struct_timeval is private;
207 -- This is needed on systems that do not have clock_gettime()
208 -- but do have gettimeofday().
210 function To_Duration (TV : struct_timeval) return Duration;
211 pragma Inline (To_Duration);
213 function To_Timeval (D : Duration) return struct_timeval;
214 pragma Inline (To_Timeval);
216 -------------------------
217 -- Priority Scheduling --
218 -------------------------
220 SCHED_FIFO : constant := 2;
221 SCHED_RR : constant := 3;
222 SCHED_OTHER : constant := 1;
228 type pid_t is private;
230 function kill (pid : pid_t; sig : Signal) return int;
231 pragma Import (C, kill, "kill");
233 function getpid return pid_t;
234 pragma Import (C, getpid, "getpid");
240 function lwp_self return System.Address;
241 pragma Import (C, lwp_self, "_lwp_self");
247 type Thread_Body is access
248 function (arg : System.Address) return System.Address;
249 type pthread_t is private;
250 subtype Thread_Id is pthread_t;
252 type pthread_mutex_t is limited private;
253 type pthread_cond_t is limited private;
254 type pthread_attr_t is limited private;
255 type pthread_mutexattr_t is limited private;
256 type pthread_condattr_t is limited private;
257 type pthread_key_t is private;
259 PTHREAD_CREATE_DETACHED : constant := 0;
265 Stack_Base_Available : constant Boolean := False;
266 -- Indicates wether the stack base is available on this target.
268 function Get_Stack_Base (thread : pthread_t) return Address;
269 pragma Inline (Get_Stack_Base);
270 -- returns the stack base of the specified thread.
271 -- Only call this function when Stack_Base_Available is True.
273 function Get_Page_Size return size_t;
274 function Get_Page_Size return Address;
275 pragma Import (C, Get_Page_Size, "getpagesize");
276 -- returns the size of a page, or 0 if this is not relevant on this
279 PROT_NONE : constant := 0;
280 PROT_READ : constant := 1;
281 PROT_WRITE : constant := 2;
282 PROT_EXEC : constant := 4;
283 PROT_USER : constant := 8;
284 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC + PROT_USER;
286 PROT_ON : constant := PROT_READ;
287 PROT_OFF : constant := PROT_ALL;
289 function mprotect (addr : Address; len : size_t; prot : int) return int;
290 pragma Import (C, mprotect);
292 -------------------------
293 -- POSIX.1c Section 3 --
294 -------------------------
296 function sigwait (set : access sigset_t; sig : access Signal) return int;
297 pragma Inline (sigwait);
298 -- UnixWare provides a non standard sigwait
300 function pthread_kill (thread : pthread_t; sig : Signal) return int;
301 pragma Inline (pthread_kill);
302 -- UnixWare provides a non standard pthread_kill
304 type sigset_t_ptr is access all sigset_t;
306 function pthread_sigmask
309 oset : sigset_t_ptr) return int;
310 pragma Import (C, pthread_sigmask, "pthread_sigmask");
312 --------------------------
313 -- POSIX.1c Section 11 --
314 --------------------------
316 function pthread_mutexattr_init
317 (attr : access pthread_mutexattr_t) return int;
318 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
320 function pthread_mutexattr_destroy
321 (attr : access pthread_mutexattr_t) return int;
322 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
324 function pthread_mutex_init
325 (mutex : access pthread_mutex_t;
326 attr : access pthread_mutexattr_t) return int;
327 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
329 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
330 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
332 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
333 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
335 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
336 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
338 function pthread_condattr_init
339 (attr : access pthread_condattr_t) return int;
340 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
342 function pthread_condattr_destroy
343 (attr : access pthread_condattr_t) return int;
344 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
346 function pthread_cond_init
347 (cond : access pthread_cond_t;
348 attr : access pthread_condattr_t) return int;
349 pragma Import (C, pthread_cond_init, "pthread_cond_init");
351 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
352 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
354 function pthread_cond_signal (cond : access pthread_cond_t) return int;
355 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
357 function pthread_cond_wait
358 (cond : access pthread_cond_t;
359 mutex : access pthread_mutex_t) return int;
360 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
362 function pthread_cond_timedwait
363 (cond : access pthread_cond_t;
364 mutex : access pthread_mutex_t;
365 abstime : access timespec) return int;
366 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
368 Relative_Timed_Wait : constant Boolean := False;
369 -- pthread_cond_timedwait requires an absolute delay time
371 --------------------------
372 -- POSIX.1c Section 13 --
373 --------------------------
375 PTHREAD_PRIO_NONE : constant := 1;
376 PTHREAD_PRIO_INHERIT : constant := 2;
377 PTHREAD_PRIO_PROTECT : constant := 3;
379 function pthread_mutexattr_setprotocol
380 (attr : access pthread_mutexattr_t;
381 protocol : int) return int;
382 pragma Import (C, pthread_mutexattr_setprotocol);
384 function pthread_mutexattr_setprioceiling
385 (attr : access pthread_mutexattr_t;
386 prioceiling : int) return int;
387 pragma Import (C, pthread_mutexattr_setprioceiling);
389 type sched_union is record
394 policy_params : long;
397 type struct_sched_param is record
398 sched_priority : int;
399 sched_other_stuff : sched_union;
402 function pthread_setschedparam
405 param : access struct_sched_param) return int;
406 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
408 function pthread_attr_setscope
409 (attr : access pthread_attr_t;
410 contentionscope : int) return int;
411 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
413 function pthread_attr_setinheritsched
414 (attr : access pthread_attr_t;
415 inheritsched : int) return int;
416 pragma Import (C, pthread_attr_setinheritsched);
418 function pthread_attr_setschedpolicy
419 (attr : access pthread_attr_t;
420 policy : int) return int;
421 pragma Import (C, pthread_attr_setschedpolicy);
423 function sched_yield return int;
424 pragma Import (C, sched_yield, "sched_yield");
426 ---------------------------
427 -- P1003.1c - Section 16 --
428 ---------------------------
430 function pthread_attr_init (attributes : access pthread_attr_t) return int;
431 pragma Import (C, pthread_attr_init, "pthread_attr_init");
433 function pthread_attr_destroy
434 (attributes : access pthread_attr_t) return int;
435 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
437 function pthread_attr_setdetachstate
438 (attr : access pthread_attr_t;
439 detachstate : int) return int;
440 pragma Import (C, pthread_attr_setdetachstate);
442 function pthread_attr_setstacksize
443 (attr : access pthread_attr_t;
444 stacksize : size_t) return int;
445 pragma Import (C, pthread_attr_setstacksize);
447 function pthread_create
448 (thread : access pthread_t;
449 attributes : access pthread_attr_t;
450 start_routine : Thread_Body;
451 arg : System.Address) return int;
452 pragma Import (C, pthread_create, "pthread_create");
454 procedure pthread_exit (status : System.Address);
455 pragma Import (C, pthread_exit, "pthread_exit");
457 function pthread_self return pthread_t;
458 pragma Import (C, pthread_self, "pthread_self");
460 --------------------------
461 -- POSIX.1c Section 17 --
462 --------------------------
464 function pthread_setspecific
465 (key : pthread_key_t;
466 value : System.Address) return int;
467 pragma Import (C, pthread_setspecific, "pthread_setspecific");
469 function pthread_getspecific (key : pthread_key_t) return System.Address;
470 pragma Import (C, pthread_getspecific, "pthread_getspecific");
472 type destructor_pointer is access procedure (arg : System.Address);
474 function pthread_key_create
475 (key : access pthread_key_t;
476 destructor : destructor_pointer) return int;
477 pragma Import (C, pthread_key_create, "pthread_key_create");
479 procedure pthread_init;
480 -- This is a dummy procedure to share some GNULLI files
484 type sigbit_array is array (1 .. 4) of unsigned;
485 type sigset_t is record
486 sa_sigbits : sigbit_array;
488 pragma Convention (C_Pass_By_Copy, sigset_t);
490 type pid_t is new unsigned;
492 type time_t is new long;
494 type timespec is record
498 pragma Convention (C, timespec);
500 type clockid_t is new int;
501 CLOCK_REALTIME : constant clockid_t := 0;
503 type struct_timeval is record
507 pragma Convention (C, struct_timeval);
509 type pthread_attr_t is record
510 pt_attr_status : int;
511 pt_attr_stacksize : size_t;
512 pt_attr_stackaddr : System.Address;
513 pt_attr_detachstate : int;
514 pt_attr_contentionscope : int;
515 pt_attr_inheritsched : int;
516 pt_attr_schedpolicy : int;
517 pt_attr_sched_param : struct_sched_param;
518 pt_attr_tlflags : int;
520 pragma Convention (C, pthread_attr_t);
522 type pthread_condattr_t is record
523 pt_condattr_status : int;
524 pt_condattr_pshared : int;
526 pragma Convention (C, pthread_condattr_t);
528 type pthread_mutexattr_t is record
529 pt_mutexattr_status : int;
530 pt_mutexattr_pshared : int;
531 pt_mutexattr_type : int;
533 pragma Convention (C, pthread_mutexattr_t);
535 type thread_t is new long;
536 type pthread_t is new thread_t;
539 type thrq_elt_t_ptr is access all thrq_elt_t;
541 type thrq_elt_t is record
542 thrq_next : thrq_elt_t_ptr;
543 thrq_prev : thrq_elt_t_ptr;
545 pragma Convention (C, thrq_elt_t);
547 type lwp_mutex_t is record
549 lock : unsigned_char;
551 pragma Convention (C, lwp_mutex_t);
552 pragma Volatile (lwp_mutex_t);
554 type mutex_t is record
555 m_lmutex : lwp_mutex_t;
556 m_sync_lock : lwp_mutex_t;
558 m_sleepq : thrq_elt_t;
562 pragma Convention (C, mutex_t);
563 pragma Volatile (mutex_t);
565 type pthread_mutex_t is record
566 pt_mutex_mutex : mutex_t;
567 pt_mutex_pid : pid_t;
568 pt_mutex_owner : thread_t;
569 pt_mutex_depth : int;
570 pt_mutex_attr : pthread_mutexattr_t;
572 pragma Convention (C, pthread_mutex_t);
574 type lwp_cond_t is record
577 pragma Convention (C, lwp_cond_t);
578 pragma Volatile (lwp_cond_t);
580 type cond_t is record
581 c_lcond : lwp_cond_t;
582 c_sync_lock : lwp_mutex_t;
584 c_syncq : thrq_elt_t;
586 pragma Convention (C, cond_t);
587 pragma Volatile (cond_t);
589 type pthread_cond_t is record
590 pt_cond_cond : cond_t;
591 pt_cond_attr : pthread_condattr_t;
593 pragma Convention (C, pthread_cond_t);
595 type pthread_key_t is new unsigned;
597 end System.OS_Interface;