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 --
9 -- Copyright (C) 1997-2001 Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a OpenNT/Interix (FSU THREADS) version of this package.
36 -- This package encapsulates all direct interfaces to OS services
37 -- that are needed by children of System.
39 -- PLEASE DO NOT add any with-clauses to this package
40 -- or remove the pragma Elaborate_Body.
41 -- It is designed to be a bottom-level (leaf) package.
44 package System.OS_Interface is
47 pragma Linker_Options ("-lgthreads");
48 pragma Linker_Options ("-lmalloc");
50 subtype int is Interfaces.C.int;
51 subtype short is Interfaces.C.short;
52 subtype long is Interfaces.C.long;
53 subtype unsigned is Interfaces.C.unsigned;
54 subtype unsigned_short is Interfaces.C.unsigned_short;
55 subtype unsigned_long is Interfaces.C.unsigned_long;
56 subtype unsigned_char is Interfaces.C.unsigned_char;
57 subtype plain_char is Interfaces.C.plain_char;
58 subtype size_t is Interfaces.C.size_t;
64 function errno return int;
65 pragma Import (C, errno, "__get_errno");
67 EAGAIN : constant := 11;
68 EINTR : constant := 4;
69 EINVAL : constant := 22;
70 ENOMEM : constant := 12;
71 ETIMEDOUT : constant := 60;
77 Max_Interrupt : constant := 31;
78 type Signal is new int range 0 .. Max_Interrupt;
79 for Signal'Size use int'Size;
81 SIGHUP : constant := 1; -- hangup
82 SIGINT : constant := 2; -- interrupt (rubout)
83 SIGQUIT : constant := 3; -- quit (ASCD FS)
84 SIGILL : constant := 4; -- illegal instruction (not reset)
85 SIGTRAP : constant := 5; -- trace trap (not reset)
86 SIGIOT : constant := 6; -- IOT instruction
87 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
88 SIGEMT : constant := 0; -- EMT instruction
89 SIGFPE : constant := 8; -- floating point exception
90 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
91 SIGBUS : constant := 10; -- bus error
92 SIGSEGV : constant := 11; -- segmentation violation
93 SIGSYS : constant := 12; -- bad argument to system call
94 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
95 SIGALRM : constant := 14; -- alarm clock
96 SIGTERM : constant := 15; -- software termination signal from kill
97 SIGUSR1 : constant := 16; -- user defined signal 1
98 SIGUSR2 : constant := 17; -- user defined signal 2
99 SIGCLD : constant := 18; -- alias for SIGCHLD
100 SIGCHLD : constant := 18; -- child status change
101 SIGPWR : constant := 0; -- power-fail restart
102 SIGWINCH : constant := 20; -- window size change
103 SIGURG : constant := 21; -- urgent condition on IO channel
104 SIGPOLL : constant := 22; -- pollable event occurred
105 SIGIO : constant := 19; -- I/O possible (Solaris SIGPOLL alias)
106 SIGSTOP : constant := 23; -- stop (cannot be caught or ignored)
107 SIGTSTP : constant := 24; -- user stop requested from tty
108 SIGCONT : constant := 25; -- stopped process has been continued
109 SIGTTIN : constant := 26; -- background tty read attempted
110 SIGTTOU : constant := 27; -- background tty write attempted
111 SIGVTALRM : constant := 28; -- virtual timer expired
112 SIGPROF : constant := 29; -- profiling timer expired
113 SIGXCPU : constant := 30; -- CPU time limit exceeded
114 SIGXFSZ : constant := 31; -- filesize limit exceeded
116 SIGADAABORT : constant := SIGABRT;
118 type Signal_Set is array (Natural range <>) of Signal;
120 Unmasked : constant Signal_Set :=
121 (SIGTRAP, SIGALRM, SIGVTALRM, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF);
123 Reserved : constant Signal_Set := (SIGKILL, SIGSTOP);
125 type sigset_t is private;
127 function sigaddset (set : access sigset_t; sig : Signal) return int;
128 pragma Import (C, sigaddset, "sigaddset");
130 function sigdelset (set : access sigset_t; sig : Signal) return int;
131 pragma Import (C, sigdelset, "sigdelset");
133 function sigfillset (set : access sigset_t) return int;
134 pragma Import (C, sigfillset, "sigfillset");
136 function sigismember (set : access sigset_t; sig : Signal) return int;
137 pragma Import (C, sigismember, "sigismember");
139 function sigemptyset (set : access sigset_t) return int;
140 pragma Import (C, sigemptyset, "sigemptyset");
142 type struct_sigaction is record
143 sa_handler : System.Address;
146 sa_restorer : System.Address;
148 pragma Convention (C, struct_sigaction);
149 type struct_sigaction_ptr is access all struct_sigaction;
151 SIG_BLOCK : constant := 1;
152 SIG_UNBLOCK : constant := 2;
153 SIG_SETMASK : constant := 3;
155 SIG_DFL : constant := 0;
156 SIG_IGN : constant := 1;
160 act : struct_sigaction_ptr;
161 oact : struct_sigaction_ptr) return int;
162 -- FSU pthreads redefines sigaction and then uses a special syscall
163 -- API to call the system version. Doing syscalls on OpenNT is very
164 -- difficult, so we rename the pthread version instead.
165 pragma Import (C, sigaction, "pthread_wrapper_sigaction");
171 Time_Slice_Supported : constant Boolean := False;
172 -- Indicates wether time slicing is supported (i.e FSU threads have been
173 -- compiled with DEF_RR)
175 type timespec is private;
177 type clockid_t is private;
179 CLOCK_REALTIME : constant clockid_t;
181 function clock_gettime
182 (clock_id : clockid_t;
183 tp : access timespec) return int;
184 pragma Import (C, clock_gettime, "clock_gettime");
186 function To_Duration (TS : timespec) return Duration;
187 pragma Inline (To_Duration);
189 function To_Timespec (D : Duration) return timespec;
190 pragma Inline (To_Timespec);
192 type struct_timeval is private;
194 function To_Duration (TV : struct_timeval) return Duration;
195 pragma Inline (To_Duration);
197 function To_Timeval (D : Duration) return struct_timeval;
198 pragma Inline (To_Timeval);
200 -------------------------
201 -- Priority Scheduling --
202 -------------------------
204 SCHED_FIFO : constant := 0;
205 SCHED_RR : constant := 1;
206 SCHED_OTHER : constant := 2;
212 type pid_t is private;
214 function kill (pid : pid_t; sig : Signal) return int;
215 pragma Import (C, kill, "kill");
217 function getpid return pid_t;
218 pragma Import (C, getpid, "getpid");
224 function lwp_self return System.Address;
225 -- lwp_self does not exist on this thread library, revert to pthread_self
226 -- which is the closest approximation (with getpid). This function is
227 -- needed to share 7staprop.adb across POSIX-like targets.
228 pragma Import (C, lwp_self, "pthread_self");
234 type Thread_Body is access
235 function (arg : System.Address) return System.Address;
236 type pthread_t is private;
237 subtype Thread_Id is pthread_t;
239 type pthread_mutex_t is limited private;
240 type pthread_cond_t is limited private;
241 type pthread_attr_t is limited private;
242 type pthread_mutexattr_t is limited private;
243 type pthread_condattr_t is limited private;
244 type pthread_key_t is private;
246 PTHREAD_CREATE_DETACHED : constant := 1;
247 PTHREAD_CREATE_JOINABLE : constant := 0;
253 Stack_Base_Available : constant Boolean := False;
254 -- Indicates wether the stack base is available on this target.
255 -- This allows us to share s-osinte.adb between all the FSU run time.
256 -- Note that this value can only be true if pthread_t has a complete
257 -- definition that corresponds exactly to the C header files.
259 function Get_Stack_Base (thread : pthread_t) return Address;
260 pragma Inline (Get_Stack_Base);
261 -- returns the stack base of the specified thread.
262 -- Only call this function when Stack_Base_Available is True.
264 function Get_Page_Size return size_t;
265 function Get_Page_Size return Address;
266 pragma Import (C, Get_Page_Size, "getpagesize");
267 -- returns the size of a page, or 0 if this is not relevant on this
270 PROT_NONE : constant := 0;
271 PROT_READ : constant := 1;
272 PROT_WRITE : constant := 2;
273 PROT_EXEC : constant := 4;
274 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
276 PROT_ON : constant := PROT_NONE;
277 PROT_OFF : constant := PROT_ALL;
279 function mprotect (addr : Address; len : size_t; prot : int) return int;
280 pragma Import (C, mprotect);
282 ---------------------------------------
283 -- Nonstandard Thread Initialization --
284 ---------------------------------------
286 procedure pthread_init;
287 -- FSU_THREADS requires pthread_init, which is nonstandard
288 -- and this should be invoked during the elaboration of s-taprop.adb
289 pragma Import (C, pthread_init, "pthread_init");
291 -------------------------
292 -- POSIX.1c Section 3 --
293 -------------------------
296 (set : access sigset_t;
297 sig : access Signal) return int;
298 -- FSU_THREADS has a nonstandard sigwait
300 function pthread_kill
302 sig : Signal) return int;
303 pragma Import (C, pthread_kill, "pthread_kill");
305 type sigset_t_ptr is access all sigset_t;
307 function pthread_sigmask
310 oset : sigset_t_ptr) return int;
311 pragma Import (C, pthread_sigmask, "pthread_wrapper_sigprocmask");
313 --------------------------
314 -- POSIX.1c Section 11 --
315 --------------------------
317 function pthread_mutexattr_init
318 (attr : access pthread_mutexattr_t) return int;
319 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
321 function pthread_mutexattr_destroy
322 (attr : access pthread_mutexattr_t) return int;
323 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
325 function pthread_mutex_init
326 (mutex : access pthread_mutex_t;
327 attr : access pthread_mutexattr_t) return int;
328 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
330 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
331 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
333 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
334 -- FSU_THREADS has nonstandard pthread_mutex_lock
336 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
337 -- FSU_THREADS has nonstandard pthread_mutex_lock
339 function pthread_condattr_init
340 (attr : access pthread_condattr_t) return int;
341 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
343 function pthread_condattr_destroy
344 (attr : access pthread_condattr_t) return int;
345 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
347 function pthread_cond_init
348 (cond : access pthread_cond_t;
349 attr : access pthread_condattr_t) return int;
350 pragma Import (C, pthread_cond_init, "pthread_cond_init");
352 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
353 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
355 function pthread_cond_signal (cond : access pthread_cond_t) return int;
356 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
358 function pthread_cond_wait
359 (cond : access pthread_cond_t;
360 mutex : access pthread_mutex_t) return int;
361 -- FSU_THREADS has a nonstandard pthread_cond_wait
363 function pthread_cond_timedwait
364 (cond : access pthread_cond_t;
365 mutex : access pthread_mutex_t;
366 abstime : access timespec) return int;
367 -- FSU_THREADS has a nonstandard pthread_cond_timedwait
369 Relative_Timed_Wait : constant Boolean := False;
370 -- pthread_cond_timedwait requires an absolute delay time
372 --------------------------
373 -- POSIX.1c Section 13 --
374 --------------------------
376 PTHREAD_PRIO_NONE : constant := 0;
377 PTHREAD_PRIO_PROTECT : constant := 2;
378 PTHREAD_PRIO_INHERIT : constant := 1;
380 function pthread_mutexattr_setprotocol
381 (attr : access pthread_mutexattr_t;
382 protocol : int) return int;
383 pragma Import (C, pthread_mutexattr_setprotocol);
385 function pthread_mutexattr_setprioceiling
386 (attr : access pthread_mutexattr_t;
387 prioceiling : int) return int;
389 (C, pthread_mutexattr_setprioceiling,
390 "pthread_mutexattr_setprio_ceiling");
392 type struct_sched_param is record
393 sched_priority : int; -- scheduling priority
396 function pthread_setschedparam
399 param : access struct_sched_param) return int;
400 -- FSU_THREADS does not have pthread_setschedparam
402 function pthread_attr_setscope
403 (attr : access pthread_attr_t;
404 contentionscope : int) return int;
405 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
407 function pthread_attr_setinheritsched
408 (attr : access pthread_attr_t;
409 inheritsched : int) return int;
410 pragma Import (C, pthread_attr_setinheritsched);
412 function pthread_attr_setschedpolicy
413 (attr : access pthread_attr_t;
414 policy : int) return int;
416 (C, pthread_attr_setschedpolicy, "pthread_attr_setsched");
418 function sched_yield return int;
419 -- FSU_THREADS does not have sched_yield;
421 ---------------------------
422 -- P1003.1c - Section 16 --
423 ---------------------------
425 function pthread_attr_init (attributes : access pthread_attr_t) return int;
426 pragma Import (C, pthread_attr_init, "pthread_attr_init");
428 function pthread_attr_destroy
429 (attributes : access pthread_attr_t) return int;
430 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
432 function pthread_attr_setdetachstate
433 (attr : access pthread_attr_t;
434 detachstate : int) return int;
435 -- FSU_THREADS has a nonstandard pthread_attr_setdetachstate
437 function pthread_attr_setstacksize
438 (attr : access pthread_attr_t;
439 stacksize : size_t) return int;
440 pragma Import (C, pthread_attr_setstacksize);
442 function pthread_create
443 (thread : access pthread_t;
444 attributes : access pthread_attr_t;
445 start_routine : Thread_Body;
446 arg : System.Address) return int;
447 pragma Import (C, pthread_create, "pthread_create");
449 procedure pthread_exit (status : System.Address);
450 pragma Import (C, pthread_exit, "pthread_exit");
452 function pthread_self return pthread_t;
453 pragma Import (C, pthread_self, "pthread_self");
455 --------------------------
456 -- POSIX.1c Section 17 --
457 --------------------------
459 function pthread_setspecific
460 (key : pthread_key_t;
461 value : System.Address) return int;
462 pragma Import (C, pthread_setspecific, "pthread_setspecific");
464 function pthread_getspecific (key : pthread_key_t) return System.Address;
465 -- FSU_THREADS has a nonstandard pthread_getspecific
467 type destructor_pointer is access procedure (arg : System.Address);
469 function pthread_key_create
470 (key : access pthread_key_t;
471 destructor : destructor_pointer) return int;
472 pragma Import (C, pthread_key_create, "pthread_key_create");
476 type sigset_t is new unsigned_long;
477 pragma Convention (C, sigset_t);
479 type pid_t is new int;
481 subtype time_t is long;
483 type timespec is record
487 pragma Convention (C, timespec);
489 type clockid_t is new int;
490 CLOCK_REALTIME : constant clockid_t := 0;
492 type struct_timeval is record
496 pragma Convention (C, struct_timeval);
498 type pthread_attr_t is record
501 contentionscope : int;
506 starttime : timespec;
510 pragma Convention (C_Pass_By_Copy, pthread_attr_t);
512 type pthread_condattr_t is record
515 pragma Convention (C, pthread_condattr_t);
517 type pthread_mutexattr_t is record
522 pragma Convention (C, pthread_mutexattr_t);
524 type sigjmp_buf is array (Integer range 0 .. 17) of int;
526 type pthread_t_struct is record
527 context : sigjmp_buf;
531 stack_base : System.Address;
533 pragma Convention (C, pthread_t_struct);
535 type pthread_t is access all pthread_t_struct;
537 type queue_t is record
538 head : System.Address;
539 tail : System.Address;
541 pragma Convention (C, queue_t);
543 type pthread_mutex_t is record
546 owner : System.Address;
550 prev_max_ceiling_prio : int;
552 pragma Convention (C, pthread_mutex_t);
554 type pthread_cond_t is record
558 mutex : System.Address;
560 pragma Convention (C, pthread_cond_t);
562 type pthread_key_t is new int;
564 end System.OS_Interface;