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-2007, 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 Solaris (POSIX Threads) version of this package
37 -- This package encapsulates all direct interfaces to OS services
38 -- that are needed by children of System.
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.
44 with Ada.Unchecked_Conversion;
46 package System.OS_Interface is
49 pragma Linker_Options ("-lposix4");
50 pragma Linker_Options ("-lpthread");
52 -- The following is needed to allow --enable-threads=solaris
54 pragma Linker_Options ("-lthread");
56 subtype int is Interfaces.C.int;
57 subtype short is Interfaces.C.short;
58 subtype long is Interfaces.C.long;
59 subtype unsigned is Interfaces.C.unsigned;
60 subtype unsigned_short is Interfaces.C.unsigned_short;
61 subtype unsigned_long is Interfaces.C.unsigned_long;
62 subtype unsigned_char is Interfaces.C.unsigned_char;
63 subtype plain_char is Interfaces.C.plain_char;
64 subtype size_t is Interfaces.C.size_t;
70 function errno return int;
71 pragma Import (C, errno, "__get_errno");
73 EAGAIN : constant := 11;
74 EINTR : constant := 4;
75 EINVAL : constant := 22;
76 ENOMEM : constant := 12;
77 ETIMEDOUT : constant := 145;
83 Max_Interrupt : constant := 45;
84 type Signal is new int range 0 .. Max_Interrupt;
85 for Signal'Size use int'Size;
87 SIGHUP : constant := 1; -- hangup
88 SIGINT : constant := 2; -- interrupt (rubout)
89 SIGQUIT : constant := 3; -- quit (ASCD FS)
90 SIGILL : constant := 4; -- illegal instruction (not reset)
91 SIGTRAP : constant := 5; -- trace trap (not reset)
92 SIGIOT : constant := 6; -- IOT instruction
93 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
94 SIGEMT : constant := 7; -- EMT instruction
95 SIGFPE : constant := 8; -- floating point exception
96 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
97 SIGBUS : constant := 10; -- bus error
98 SIGSEGV : constant := 11; -- segmentation violation
99 SIGSYS : constant := 12; -- bad argument to system call
100 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
101 SIGALRM : constant := 14; -- alarm clock
102 SIGTERM : constant := 15; -- software termination signal from kill
103 SIGUSR1 : constant := 16; -- user defined signal 1
104 SIGUSR2 : constant := 17; -- user defined signal 2
105 SIGCLD : constant := 18; -- alias for SIGCHLD
106 SIGCHLD : constant := 18; -- child status change
107 SIGPWR : constant := 19; -- power-fail restart
108 SIGWINCH : constant := 20; -- window size change
109 SIGURG : constant := 21; -- urgent condition on IO channel
110 SIGPOLL : constant := 22; -- pollable event occurred
111 SIGIO : constant := 22; -- I/O possible (Solaris SIGPOLL alias)
112 SIGSTOP : constant := 23; -- stop (cannot be caught or ignored)
113 SIGTSTP : constant := 24; -- user stop requested from tty
114 SIGCONT : constant := 25; -- stopped process has been continued
115 SIGTTIN : constant := 26; -- background tty read attempted
116 SIGTTOU : constant := 27; -- background tty write attempted
117 SIGVTALRM : constant := 28; -- virtual timer expired
118 SIGPROF : constant := 29; -- profiling timer expired
119 SIGXCPU : constant := 30; -- CPU time limit exceeded
120 SIGXFSZ : constant := 31; -- filesize limit exceeded
121 SIGWAITING : constant := 32; -- process's lwps blocked (Solaris)
122 SIGLWP : constant := 33; -- used by thread library (Solaris)
123 SIGFREEZE : constant := 34; -- used by CPR (Solaris)
124 SIGTHAW : constant := 35; -- used by CPR (Solaris)
125 SIGCANCEL : constant := 36; -- thread cancellation signal (libthread)
127 SIGADAABORT : constant := SIGABRT;
129 type Signal_Set is array (Natural range <>) of Signal;
131 Unmasked : constant Signal_Set := (SIGTRAP, SIGLWP, SIGPROF);
133 -- Following signals should not be disturbed.
134 -- See c-posix-signals.c in FLORIST
136 Reserved : constant Signal_Set :=
137 (SIGKILL, SIGSTOP, SIGWAITING, SIGCANCEL);
139 type sigset_t is private;
141 function sigaddset (set : access sigset_t; sig : Signal) return int;
142 pragma Import (C, sigaddset, "sigaddset");
144 function sigdelset (set : access sigset_t; sig : Signal) return int;
145 pragma Import (C, sigdelset, "sigdelset");
147 function sigfillset (set : access sigset_t) return int;
148 pragma Import (C, sigfillset, "sigfillset");
150 function sigismember (set : access sigset_t; sig : Signal) return int;
151 pragma Import (C, sigismember, "sigismember");
153 function sigemptyset (set : access sigset_t) return int;
154 pragma Import (C, sigemptyset, "sigemptyset");
156 type struct_sigaction is record
158 sa_handler : System.Address;
163 pragma Convention (C, struct_sigaction);
164 type struct_sigaction_ptr is access all struct_sigaction;
166 SA_SIGINFO : constant := 16#0008#;
168 SIG_BLOCK : constant := 1;
169 SIG_UNBLOCK : constant := 2;
170 SIG_SETMASK : constant := 3;
172 SIG_DFL : constant := 0;
173 SIG_IGN : constant := 1;
177 act : struct_sigaction_ptr;
178 oact : struct_sigaction_ptr) return int;
179 pragma Import (C, sigaction, "sigaction");
185 Time_Slice_Supported : constant Boolean := True;
186 -- Indicates wether time slicing is supported
188 type timespec is private;
190 type clockid_t is private;
192 CLOCK_REALTIME : constant clockid_t;
194 function clock_gettime
195 (clock_id : clockid_t;
196 tp : access timespec) return int;
197 pragma Import (C, clock_gettime, "clock_gettime");
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_timeval is private;
207 function To_Duration (TV : struct_timeval) return Duration;
208 pragma Inline (To_Duration);
210 function To_Timeval (D : Duration) return struct_timeval;
211 pragma Inline (To_Timeval);
213 -------------------------
214 -- Priority Scheduling --
215 -------------------------
217 SCHED_FIFO : constant := 1;
218 SCHED_RR : constant := 2;
219 SCHED_OTHER : constant := 0;
221 function To_Target_Priority
222 (Prio : System.Any_Priority) return Interfaces.C.int;
223 -- Maps System.Any_Priority to a POSIX priority.
229 type pid_t is private;
231 function kill (pid : pid_t; sig : Signal) return int;
232 pragma Import (C, kill, "kill");
234 function getpid return pid_t;
235 pragma Import (C, getpid, "getpid");
241 function lwp_self return System.Address;
242 pragma Import (C, lwp_self, "_lwp_self");
248 type Thread_Body is access
249 function (arg : System.Address) return System.Address;
251 function Thread_Body_Access is new
252 Ada.Unchecked_Conversion (System.Address, Thread_Body);
254 type pthread_t is private;
255 subtype Thread_Id is pthread_t;
257 type pthread_mutex_t is limited private;
258 type pthread_cond_t is limited private;
259 type pthread_attr_t is limited private;
260 type pthread_mutexattr_t is limited private;
261 type pthread_condattr_t is limited private;
262 type pthread_key_t is private;
264 PTHREAD_CREATE_DETACHED : constant := 16#40#;
266 PTHREAD_SCOPE_PROCESS : constant := 0;
267 PTHREAD_SCOPE_SYSTEM : constant := 1;
273 Stack_Base_Available : constant Boolean := False;
274 -- Indicates whether the stack base is available on this target.
276 function Get_Stack_Base (thread : pthread_t) return Address;
277 pragma Inline (Get_Stack_Base);
278 -- returns the stack base of the specified thread.
279 -- Only call this function when Stack_Base_Available is True.
281 function Get_Page_Size return size_t;
282 function Get_Page_Size return Address;
283 pragma Import (C, Get_Page_Size, "getpagesize");
284 -- returns the size of a page, or 0 if this is not relevant on this
287 PROT_NONE : constant := 0;
288 PROT_READ : constant := 1;
289 PROT_WRITE : constant := 2;
290 PROT_EXEC : constant := 4;
291 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
293 PROT_ON : constant := PROT_READ;
294 PROT_OFF : constant := PROT_ALL;
296 function mprotect (addr : Address; len : size_t; prot : int) return int;
297 pragma Import (C, mprotect);
299 ---------------------------------------
300 -- Nonstandard Thread Initialization --
301 ---------------------------------------
303 procedure pthread_init;
304 -- This is a dummy procedure to share some GNULLI files
306 -------------------------
307 -- POSIX.1c Section 3 --
308 -------------------------
311 (set : access sigset_t;
312 sig : access Signal) return int;
313 pragma Import (C, sigwait, "__posix_sigwait");
315 function pthread_kill
317 sig : Signal) return int;
318 pragma Import (C, pthread_kill, "pthread_kill");
320 function pthread_sigmask
322 set : access sigset_t;
323 oset : access sigset_t) return int;
324 pragma Import (C, pthread_sigmask, "pthread_sigmask");
326 --------------------------
327 -- POSIX.1c Section 11 --
328 --------------------------
330 function pthread_mutexattr_init
331 (attr : access pthread_mutexattr_t) return int;
332 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
334 function pthread_mutexattr_destroy
335 (attr : access pthread_mutexattr_t) return int;
336 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
338 function pthread_mutex_init
339 (mutex : access pthread_mutex_t;
340 attr : access pthread_mutexattr_t) return int;
341 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
343 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
344 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
346 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
347 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
349 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
350 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
352 function pthread_condattr_init
353 (attr : access pthread_condattr_t) return int;
354 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
356 function pthread_condattr_destroy
357 (attr : access pthread_condattr_t) return int;
358 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
360 function pthread_cond_init
361 (cond : access pthread_cond_t;
362 attr : access pthread_condattr_t) return int;
363 pragma Import (C, pthread_cond_init, "pthread_cond_init");
365 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
366 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
368 function pthread_cond_signal (cond : access pthread_cond_t) return int;
369 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
371 function pthread_cond_wait
372 (cond : access pthread_cond_t;
373 mutex : access pthread_mutex_t) return int;
374 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
376 function pthread_cond_timedwait
377 (cond : access pthread_cond_t;
378 mutex : access pthread_mutex_t;
379 abstime : access timespec) return int;
380 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
382 Relative_Timed_Wait : constant Boolean := False;
383 -- pthread_cond_timedwait requires an absolute delay time
385 --------------------------
386 -- POSIX.1c Section 13 --
387 --------------------------
389 PTHREAD_PRIO_NONE : constant := 0;
390 PTHREAD_PRIO_INHERIT : constant := 16#10#;
391 PTHREAD_PRIO_PROTECT : constant := 16#20#;
393 function pthread_mutexattr_setprotocol
394 (attr : access pthread_mutexattr_t;
395 protocol : int) return int;
396 pragma Import (C, pthread_mutexattr_setprotocol);
398 function pthread_mutexattr_setprioceiling
399 (attr : access pthread_mutexattr_t;
400 prioceiling : int) return int;
401 pragma Import (C, pthread_mutexattr_setprioceiling);
403 type Array_8_Int is array (0 .. 7) of int;
404 type struct_sched_param is record
405 sched_priority : int;
406 sched_pad : Array_8_Int;
409 function pthread_setschedparam
412 param : access struct_sched_param) return int;
413 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
415 function pthread_attr_setscope
416 (attr : access pthread_attr_t;
417 contentionscope : int) return int;
418 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
420 function pthread_attr_setinheritsched
421 (attr : access pthread_attr_t;
422 inheritsched : int) return int;
423 pragma Import (C, pthread_attr_setinheritsched);
425 function pthread_attr_setschedpolicy
426 (attr : access pthread_attr_t;
427 policy : int) return int;
428 pragma Import (C, pthread_attr_setschedpolicy);
430 function sched_yield return int;
431 pragma Import (C, sched_yield, "sched_yield");
433 ---------------------------
434 -- P1003.1c - Section 16 --
435 ---------------------------
437 function pthread_attr_init (attributes : access pthread_attr_t) return int;
438 pragma Import (C, pthread_attr_init, "pthread_attr_init");
440 function pthread_attr_destroy
441 (attributes : access pthread_attr_t) return int;
442 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
444 function pthread_attr_setdetachstate
445 (attr : access pthread_attr_t;
446 detachstate : int) return int;
447 pragma Import (C, pthread_attr_setdetachstate);
449 function pthread_attr_setstacksize
450 (attr : access pthread_attr_t;
451 stacksize : size_t) return int;
452 pragma Import (C, pthread_attr_setstacksize);
454 function pthread_create
455 (thread : access pthread_t;
456 attributes : access pthread_attr_t;
457 start_routine : Thread_Body;
458 arg : System.Address) return int;
459 pragma Import (C, pthread_create, "pthread_create");
461 procedure pthread_exit (status : System.Address);
462 pragma Import (C, pthread_exit, "pthread_exit");
464 function pthread_self return pthread_t;
465 pragma Import (C, pthread_self, "pthread_self");
467 --------------------------
468 -- POSIX.1c Section 17 --
469 --------------------------
471 function pthread_setspecific
472 (key : pthread_key_t;
473 value : System.Address) return int;
474 pragma Import (C, pthread_setspecific, "pthread_setspecific");
476 function pthread_getspecific (key : pthread_key_t) return System.Address;
477 pragma Import (C, pthread_getspecific, "pthread_getspecific");
479 type destructor_pointer is access procedure (arg : System.Address);
481 function pthread_key_create
482 (key : access pthread_key_t;
483 destructor : destructor_pointer) return int;
484 pragma Import (C, pthread_key_create, "pthread_key_create");
488 type array_type_1 is array (Integer range 0 .. 3) of unsigned_long;
489 type sigset_t is record
490 X_X_sigbits : array_type_1;
492 pragma Convention (C, sigset_t);
494 type pid_t is new long;
496 type time_t is new long;
498 type timespec is record
502 pragma Convention (C, timespec);
504 type clockid_t is new int;
505 CLOCK_REALTIME : constant clockid_t := 0;
507 type struct_timeval is record
511 pragma Convention (C, struct_timeval);
513 type pthread_attr_t is record
514 pthread_attrp : System.Address;
516 pragma Convention (C, pthread_attr_t);
518 type pthread_condattr_t is record
519 pthread_condattrp : System.Address;
521 pragma Convention (C, pthread_condattr_t);
523 type pthread_mutexattr_t is record
524 pthread_mutexattrp : System.Address;
526 pragma Convention (C, pthread_mutexattr_t);
528 type pthread_t is new unsigned;
530 type uint64_t is mod 2 ** 64;
532 type pthread_mutex_t is record
533 pthread_mutex_flags : uint64_t;
534 pthread_mutex_owner64 : uint64_t;
535 pthread_mutex_data : uint64_t;
537 pragma Convention (C, pthread_mutex_t);
538 type pthread_mutex_t_ptr is access pthread_mutex_t;
540 type pthread_cond_t is record
541 pthread_cond_flags : uint64_t;
542 pthread_cond_data : uint64_t;
544 pragma Convention (C, pthread_cond_t);
546 type pthread_key_t is new unsigned;
548 end System.OS_Interface;