02213086b128a3f076c452fd2af98805232e55a7
[gcc.git] / gcc / ada / s-osinte-linux.ads
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . O S _ I N T E R F A C E --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2010, Free Software Foundation, Inc. --
11 -- --
12 -- GNAT 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 3, or (at your option) any later ver- --
15 -- sion. GNAT 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. --
18 -- --
19 -- As a special exception under Section 7 of GPL version 3, you are granted --
20 -- additional permissions described in the GCC Runtime Library Exception, --
21 -- version 3.1, as published by the Free Software Foundation. --
22 -- --
23 -- You should have received a copy of the GNU General Public License and --
24 -- a copy of the GCC Runtime Library Exception along with this program; --
25 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
26 -- <http://www.gnu.org/licenses/>. --
27 -- --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 -- --
31 ------------------------------------------------------------------------------
32
33 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
34
35 -- This package encapsulates all direct interfaces to OS services
36 -- that are needed by the tasking run-time (libgnarl).
37
38 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
39 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
40
41 with Ada.Unchecked_Conversion;
42 with Interfaces.C;
43 with System.Linux;
44
45 package System.OS_Interface is
46 pragma Preelaborate;
47
48 pragma Linker_Options ("-lpthread");
49
50 subtype int is Interfaces.C.int;
51 subtype char is Interfaces.C.char;
52 subtype short is Interfaces.C.short;
53 subtype long is Interfaces.C.long;
54 subtype unsigned is Interfaces.C.unsigned;
55 subtype unsigned_short is Interfaces.C.unsigned_short;
56 subtype unsigned_long is Interfaces.C.unsigned_long;
57 subtype unsigned_char is Interfaces.C.unsigned_char;
58 subtype plain_char is Interfaces.C.plain_char;
59 subtype size_t is Interfaces.C.size_t;
60
61 -----------
62 -- Errno --
63 -----------
64
65 function errno return int;
66 pragma Import (C, errno, "__get_errno");
67
68 EAGAIN : constant := System.Linux.EAGAIN;
69 EINTR : constant := System.Linux.EINTR;
70 EINVAL : constant := System.Linux.EINVAL;
71 ENOMEM : constant := System.Linux.ENOMEM;
72 EPERM : constant := System.Linux.EPERM;
73 ETIMEDOUT : constant := System.Linux.ETIMEDOUT;
74
75 -------------
76 -- Signals --
77 -------------
78
79 Max_Interrupt : constant := 63;
80 type Signal is new int range 0 .. Max_Interrupt;
81 for Signal'Size use int'Size;
82
83 SIGHUP : constant := System.Linux.SIGHUP;
84 SIGINT : constant := System.Linux.SIGINT;
85 SIGQUIT : constant := System.Linux.SIGQUIT;
86 SIGILL : constant := System.Linux.SIGILL;
87 SIGTRAP : constant := System.Linux.SIGTRAP;
88 SIGIOT : constant := System.Linux.SIGIOT;
89 SIGABRT : constant := System.Linux.SIGABRT;
90 SIGFPE : constant := System.Linux.SIGFPE;
91 SIGKILL : constant := System.Linux.SIGKILL;
92 SIGBUS : constant := System.Linux.SIGBUS;
93 SIGSEGV : constant := System.Linux.SIGSEGV;
94 SIGPIPE : constant := System.Linux.SIGPIPE;
95 SIGALRM : constant := System.Linux.SIGALRM;
96 SIGTERM : constant := System.Linux.SIGTERM;
97 SIGUSR1 : constant := System.Linux.SIGUSR1;
98 SIGUSR2 : constant := System.Linux.SIGUSR2;
99 SIGCLD : constant := System.Linux.SIGCLD;
100 SIGCHLD : constant := System.Linux.SIGCHLD;
101 SIGPWR : constant := System.Linux.SIGPWR;
102 SIGWINCH : constant := System.Linux.SIGWINCH;
103 SIGURG : constant := System.Linux.SIGURG;
104 SIGPOLL : constant := System.Linux.SIGPOLL;
105 SIGIO : constant := System.Linux.SIGIO;
106 SIGLOST : constant := System.Linux.SIGLOST;
107 SIGSTOP : constant := System.Linux.SIGSTOP;
108 SIGTSTP : constant := System.Linux.SIGTSTP;
109 SIGCONT : constant := System.Linux.SIGCONT;
110 SIGTTIN : constant := System.Linux.SIGTTIN;
111 SIGTTOU : constant := System.Linux.SIGTTOU;
112 SIGVTALRM : constant := System.Linux.SIGVTALRM;
113 SIGPROF : constant := System.Linux.SIGPROF;
114 SIGXCPU : constant := System.Linux.SIGXCPU;
115 SIGXFSZ : constant := System.Linux.SIGXFSZ;
116 SIGUNUSED : constant := System.Linux.SIGUNUSED;
117 SIGSTKFLT : constant := System.Linux.SIGSTKFLT;
118 SIGLTHRRES : constant := System.Linux.SIGLTHRRES;
119 SIGLTHRCAN : constant := System.Linux.SIGLTHRCAN;
120 SIGLTHRDBG : constant := System.Linux.SIGLTHRDBG;
121
122 SIGADAABORT : constant := SIGABRT;
123 -- Change this to use another signal for task abort. SIGTERM might be a
124 -- good one.
125
126 type Signal_Set is array (Natural range <>) of Signal;
127
128 Unmasked : constant Signal_Set := (
129 SIGTRAP,
130 -- To enable debugging on multithreaded applications, mark SIGTRAP to
131 -- be kept unmasked.
132
133 SIGBUS,
134
135 SIGTTIN, SIGTTOU, SIGTSTP,
136 -- Keep these three signals unmasked so that background processes and IO
137 -- behaves as normal "C" applications
138
139 SIGPROF,
140 -- To avoid confusing the profiler
141
142 SIGKILL, SIGSTOP,
143 -- These two signals actually can't be masked (POSIX won't allow it)
144
145 SIGLTHRRES, SIGLTHRCAN, SIGLTHRDBG);
146 -- These three signals are used by GNU/LinuxThreads starting from glibc
147 -- 2.1 (future 2.2).
148
149 Reserved : constant Signal_Set := (SIGVTALRM, SIGUNUSED);
150 -- Not clear why these two signals are reserved. Perhaps they are not
151 -- supported by this version of GNU/Linux ???
152
153 type sigset_t is private;
154
155 function sigaddset (set : access sigset_t; sig : Signal) return int;
156 pragma Import (C, sigaddset, "sigaddset");
157
158 function sigdelset (set : access sigset_t; sig : Signal) return int;
159 pragma Import (C, sigdelset, "sigdelset");
160
161 function sigfillset (set : access sigset_t) return int;
162 pragma Import (C, sigfillset, "sigfillset");
163
164 function sigismember (set : access sigset_t; sig : Signal) return int;
165 pragma Import (C, sigismember, "sigismember");
166
167 function sigemptyset (set : access sigset_t) return int;
168 pragma Import (C, sigemptyset, "sigemptyset");
169
170 type union_type_3 is new String (1 .. 116);
171 type siginfo_t is record
172 si_signo : int;
173 si_code : int;
174 si_errno : int;
175 X_data : union_type_3;
176 end record;
177 pragma Convention (C, siginfo_t);
178
179 type struct_sigaction is record
180 sa_handler : System.Address;
181 sa_mask : sigset_t;
182 sa_flags : Interfaces.C.unsigned_long;
183 sa_restorer : System.Address;
184 end record;
185 pragma Convention (C, struct_sigaction);
186
187 type struct_sigaction_ptr is access all struct_sigaction;
188
189 type Machine_State is record
190 eip : unsigned_long;
191 ebx : unsigned_long;
192 esp : unsigned_long;
193 ebp : unsigned_long;
194 esi : unsigned_long;
195 edi : unsigned_long;
196 end record;
197 type Machine_State_Ptr is access all Machine_State;
198
199 SA_SIGINFO : constant := System.Linux.SA_SIGINFO;
200 SA_ONSTACK : constant := System.Linux.SA_ONSTACK;
201
202 SIG_BLOCK : constant := 0;
203 SIG_UNBLOCK : constant := 1;
204 SIG_SETMASK : constant := 2;
205
206 SIG_DFL : constant := 0;
207 SIG_IGN : constant := 1;
208
209 function sigaction
210 (sig : Signal;
211 act : struct_sigaction_ptr;
212 oact : struct_sigaction_ptr) return int;
213 pragma Import (C, sigaction, "sigaction");
214
215 ----------
216 -- Time --
217 ----------
218
219 type timespec is private;
220
221 function To_Duration (TS : timespec) return Duration;
222 pragma Inline (To_Duration);
223
224 function To_Timespec (D : Duration) return timespec;
225 pragma Inline (To_Timespec);
226
227 function sysconf (name : int) return long;
228 pragma Import (C, sysconf);
229
230 SC_CLK_TCK : constant := 2;
231 SC_NPROCESSORS_ONLN : constant := 84;
232
233 -------------------------
234 -- Priority Scheduling --
235 -------------------------
236
237 SCHED_OTHER : constant := 0;
238 SCHED_FIFO : constant := 1;
239 SCHED_RR : constant := 2;
240
241 function To_Target_Priority
242 (Prio : System.Any_Priority) return Interfaces.C.int;
243 -- Maps System.Any_Priority to a POSIX priority
244
245 -------------
246 -- Process --
247 -------------
248
249 type pid_t is private;
250
251 function kill (pid : pid_t; sig : Signal) return int;
252 pragma Import (C, kill, "kill");
253
254 function getpid return pid_t;
255 pragma Import (C, getpid, "getpid");
256
257 -------------
258 -- Threads --
259 -------------
260
261 type Thread_Body is access
262 function (arg : System.Address) return System.Address;
263 pragma Convention (C, Thread_Body);
264
265 function Thread_Body_Access is new
266 Ada.Unchecked_Conversion (System.Address, Thread_Body);
267
268 type pthread_t is new unsigned_long;
269 subtype Thread_Id is pthread_t;
270
271 function To_pthread_t is
272 new Ada.Unchecked_Conversion (unsigned_long, pthread_t);
273
274 type pthread_mutex_t is limited private;
275 type pthread_cond_t is limited private;
276 type pthread_attr_t is limited private;
277 type pthread_mutexattr_t is limited private;
278 type pthread_condattr_t is limited private;
279 type pthread_key_t is private;
280
281 PTHREAD_CREATE_DETACHED : constant := 1;
282
283 -----------
284 -- Stack --
285 -----------
286
287 type stack_t is record
288 ss_sp : System.Address;
289 ss_flags : int;
290 ss_size : size_t;
291 end record;
292 pragma Convention (C, stack_t);
293
294 function sigaltstack
295 (ss : not null access stack_t;
296 oss : access stack_t) return int;
297 pragma Import (C, sigaltstack, "sigaltstack");
298
299 Alternate_Stack : aliased System.Address;
300 pragma Import (C, Alternate_Stack, "__gnat_alternate_stack");
301 -- The alternate signal stack for stack overflows
302
303 Alternate_Stack_Size : constant := 16 * 1024;
304 -- This must be in keeping with init.c:__gnat_alternate_stack
305
306 function Get_Stack_Base (thread : pthread_t) return Address;
307 pragma Inline (Get_Stack_Base);
308 -- This is a dummy procedure to share some GNULLI files
309
310 ---------------------------------------
311 -- Nonstandard Thread Initialization --
312 ---------------------------------------
313
314 procedure pthread_init;
315 pragma Inline (pthread_init);
316 -- This is a dummy procedure to share some GNULLI files
317
318 -------------------------
319 -- POSIX.1c Section 3 --
320 -------------------------
321
322 function sigwait (set : access sigset_t; sig : access Signal) return int;
323 pragma Import (C, sigwait, "sigwait");
324
325 function pthread_kill (thread : pthread_t; sig : Signal) return int;
326 pragma Import (C, pthread_kill, "pthread_kill");
327
328 function pthread_sigmask
329 (how : int;
330 set : access sigset_t;
331 oset : access sigset_t) return int;
332 pragma Import (C, pthread_sigmask, "pthread_sigmask");
333
334 --------------------------
335 -- POSIX.1c Section 11 --
336 --------------------------
337
338 function pthread_mutexattr_init
339 (attr : access pthread_mutexattr_t) return int;
340 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
341
342 function pthread_mutexattr_destroy
343 (attr : access pthread_mutexattr_t) return int;
344 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
345
346 function pthread_mutex_init
347 (mutex : access pthread_mutex_t;
348 attr : access pthread_mutexattr_t) return int;
349 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
350
351 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
352 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
353
354 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
355 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
356
357 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
358 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
359
360 function pthread_condattr_init
361 (attr : access pthread_condattr_t) return int;
362 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
363
364 function pthread_condattr_destroy
365 (attr : access pthread_condattr_t) return int;
366 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
367
368 function pthread_cond_init
369 (cond : access pthread_cond_t;
370 attr : access pthread_condattr_t) return int;
371 pragma Import (C, pthread_cond_init, "pthread_cond_init");
372
373 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
374 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
375
376 function pthread_cond_signal (cond : access pthread_cond_t) return int;
377 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
378
379 function pthread_cond_wait
380 (cond : access pthread_cond_t;
381 mutex : access pthread_mutex_t) return int;
382 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
383
384 function pthread_cond_timedwait
385 (cond : access pthread_cond_t;
386 mutex : access pthread_mutex_t;
387 abstime : access timespec) return int;
388 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
389
390 --------------------------
391 -- POSIX.1c Section 13 --
392 --------------------------
393
394 type struct_sched_param is record
395 sched_priority : int; -- scheduling priority
396 end record;
397 pragma Convention (C, struct_sched_param);
398
399 function pthread_setschedparam
400 (thread : pthread_t;
401 policy : int;
402 param : access struct_sched_param) return int;
403 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
404
405 function pthread_attr_setschedpolicy
406 (attr : access pthread_attr_t;
407 policy : int) return int;
408 pragma Import
409 (C, pthread_attr_setschedpolicy, "pthread_attr_setschedpolicy");
410
411 function sched_yield return int;
412 pragma Import (C, sched_yield, "sched_yield");
413
414 ---------------------------
415 -- P1003.1c - Section 16 --
416 ---------------------------
417
418 function pthread_attr_init
419 (attributes : access pthread_attr_t) return int;
420 pragma Import (C, pthread_attr_init, "pthread_attr_init");
421
422 function pthread_attr_destroy
423 (attributes : access pthread_attr_t) return int;
424 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
425
426 function pthread_attr_setdetachstate
427 (attr : access pthread_attr_t;
428 detachstate : int) return int;
429 pragma Import
430 (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate");
431
432 function pthread_attr_setstacksize
433 (attr : access pthread_attr_t;
434 stacksize : size_t) return int;
435 pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
436
437 function pthread_create
438 (thread : access pthread_t;
439 attributes : access pthread_attr_t;
440 start_routine : Thread_Body;
441 arg : System.Address) return int;
442 pragma Import (C, pthread_create, "pthread_create");
443
444 procedure pthread_exit (status : System.Address);
445 pragma Import (C, pthread_exit, "pthread_exit");
446
447 function pthread_self return pthread_t;
448 pragma Import (C, pthread_self, "pthread_self");
449
450 function lwp_self return System.Address;
451 pragma Import (C, lwp_self, "__gnat_lwp_self");
452
453 --------------------------
454 -- POSIX.1c Section 17 --
455 --------------------------
456
457 function pthread_setspecific
458 (key : pthread_key_t;
459 value : System.Address) return int;
460 pragma Import (C, pthread_setspecific, "pthread_setspecific");
461
462 function pthread_getspecific (key : pthread_key_t) return System.Address;
463 pragma Import (C, pthread_getspecific, "pthread_getspecific");
464
465 type destructor_pointer is access procedure (arg : System.Address);
466 pragma Convention (C, destructor_pointer);
467
468 function pthread_key_create
469 (key : access pthread_key_t;
470 destructor : destructor_pointer) return int;
471 pragma Import (C, pthread_key_create, "pthread_key_create");
472
473 CPU_SETSIZE : constant := 1_024;
474
475 type bit_field is array (1 .. CPU_SETSIZE) of Boolean;
476 for bit_field'Size use CPU_SETSIZE;
477 pragma Pack (bit_field);
478 pragma Convention (C, bit_field);
479
480 type cpu_set_t is record
481 bits : bit_field;
482 end record;
483 pragma Convention (C, cpu_set_t);
484
485 function pthread_setaffinity_np
486 (thread : pthread_t;
487 cpusetsize : size_t;
488 cpuset : access cpu_set_t) return int;
489 pragma Import (C, pthread_setaffinity_np, "pthread_setaffinity_np");
490 pragma Weak_External (pthread_setaffinity_np);
491 -- Use a weak symbol because this function may be available or not,
492 -- depending on the version of the system.
493
494 function pthread_attr_setaffinity_np
495 (attr : access pthread_attr_t;
496 cpusetsize : size_t;
497 cpuset : access cpu_set_t) return int;
498 pragma Import (C, pthread_attr_setaffinity_np,
499 "pthread_attr_setaffinity_np");
500 pragma Weak_External (pthread_attr_setaffinity_np);
501 -- Use a weak symbol because this function may be available or not,
502 -- depending on the version of the system.
503
504 private
505
506 type sigset_t is array (0 .. 127) of unsigned_char;
507 pragma Convention (C, sigset_t);
508 for sigset_t'Alignment use Interfaces.C.unsigned_long'Alignment;
509
510 pragma Warnings (Off);
511 for struct_sigaction use record
512 sa_handler at Linux.sa_handler_pos range 0 .. Standard'Address_Size - 1;
513 sa_mask at Linux.sa_mask_pos range 0 .. 1023;
514 sa_flags at Linux.sa_flags_pos range 0 .. Standard'Address_Size - 1;
515 end record;
516 -- We intentionally leave sa_restorer unspecified and let the compiler
517 -- append it after the last field, so disable corresponding warning.
518 pragma Warnings (On);
519
520 type pid_t is new int;
521
522 type time_t is new long;
523
524 type timespec is record
525 tv_sec : time_t;
526 tv_nsec : long;
527 end record;
528 pragma Convention (C, timespec);
529
530 type pthread_attr_t is record
531 detachstate : int;
532 schedpolicy : int;
533 schedparam : struct_sched_param;
534 inheritsched : int;
535 scope : int;
536 guardsize : size_t;
537 stackaddr_set : int;
538 stackaddr : System.Address;
539 stacksize : size_t;
540 end record;
541 pragma Convention (C, pthread_attr_t);
542
543 type pthread_condattr_t is record
544 dummy : int;
545 end record;
546 pragma Convention (C, pthread_condattr_t);
547
548 type pthread_mutexattr_t is record
549 mutexkind : int;
550 end record;
551 pragma Convention (C, pthread_mutexattr_t);
552
553 type pthread_mutex_t is new System.Linux.pthread_mutex_t;
554
555 type unsigned_long_long_t is mod 2 ** 64;
556 -- Local type only used to get it's 'Alignment below
557
558 type pthread_cond_t is array (0 .. 47) of unsigned_char;
559 pragma Convention (C, pthread_cond_t);
560 for pthread_cond_t'Alignment use unsigned_long_long_t'Alignment;
561
562 type pthread_key_t is new unsigned;
563
564 end System.OS_Interface;