OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.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-2007, Free Software Foundation, Inc.         --
11 --                                                                          --
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.                                              --
22 --                                                                          --
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.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University.       --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 --  This is a GNU/Linux (GNU/LinuxThreads) version of this package
36
37 --  This package encapsulates all direct interfaces to OS services
38 --  that are needed by children of System.
39
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.
42
43 with Interfaces.C;
44 with Ada.Unchecked_Conversion;
45
46 package System.OS_Interface is
47    pragma Preelaborate;
48
49    pragma Linker_Options ("-lpthread");
50
51    subtype int            is Interfaces.C.int;
52    subtype char           is Interfaces.C.char;
53    subtype short          is Interfaces.C.short;
54    subtype long           is Interfaces.C.long;
55    subtype unsigned       is Interfaces.C.unsigned;
56    subtype unsigned_short is Interfaces.C.unsigned_short;
57    subtype unsigned_long  is Interfaces.C.unsigned_long;
58    subtype unsigned_char  is Interfaces.C.unsigned_char;
59    subtype plain_char     is Interfaces.C.plain_char;
60    subtype size_t         is Interfaces.C.size_t;
61
62    -----------
63    -- Errno --
64    -----------
65
66    function errno return int;
67    pragma Import (C, errno, "__get_errno");
68
69    EAGAIN    : constant := 11;
70    EINTR     : constant := 4;
71    EINVAL    : constant := 22;
72    ENOMEM    : constant := 12;
73    EPERM     : constant := 1;
74    ETIMEDOUT : constant := 110;
75
76    -------------
77    -- Signals --
78    -------------
79
80    Max_Interrupt : constant := 63;
81    type Signal is new int range 0 .. Max_Interrupt;
82    for Signal'Size use int'Size;
83
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    SIGFPE     : constant := 8; --  floating point exception
92    SIGKILL    : constant := 9; --  kill (cannot be caught or ignored)
93    SIGBUS     : constant := 7; --  bus error
94    SIGSEGV    : constant := 11; --  segmentation violation
95    SIGPIPE    : constant := 13; --  write on a pipe with no one to read it
96    SIGALRM    : constant := 14; --  alarm clock
97    SIGTERM    : constant := 15; --  software termination signal from kill
98    SIGUSR1    : constant := 10; --  user defined signal 1
99    SIGUSR2    : constant := 12; --  user defined signal 2
100    SIGCLD     : constant := 17; --  alias for SIGCHLD
101    SIGCHLD    : constant := 17; --  child status change
102    SIGPWR     : constant := 30; --  power-fail restart
103    SIGWINCH   : constant := 28; --  window size change
104    SIGURG     : constant := 23; --  urgent condition on IO channel
105    SIGPOLL    : constant := 29; --  pollable event occurred
106    SIGIO      : constant := 29; --  I/O now possible (4.2 BSD)
107    SIGLOST    : constant := 29; --  File lock lost
108    SIGSTOP    : constant := 19; --  stop (cannot be caught or ignored)
109    SIGTSTP    : constant := 20; --  user stop requested from tty
110    SIGCONT    : constant := 18; --  stopped process has been continued
111    SIGTTIN    : constant := 21; --  background tty read attempted
112    SIGTTOU    : constant := 22; --  background tty write attempted
113    SIGVTALRM  : constant := 26; --  virtual timer expired
114    SIGPROF    : constant := 27; --  profiling timer expired
115    SIGXCPU    : constant := 24; --  CPU time limit exceeded
116    SIGXFSZ    : constant := 25; --  filesize limit exceeded
117    SIGUNUSED  : constant := 31; --  unused signal (GNU/Linux)
118    SIGSTKFLT  : constant := 16; --  coprocessor stack fault (Linux)
119    SIGLTHRRES : constant := 32; --  GNU/LinuxThreads restart signal
120    SIGLTHRCAN : constant := 33; --  GNU/LinuxThreads cancel signal
121    SIGLTHRDBG : constant := 34; --  GNU/LinuxThreads debugger signal
122
123    SIGADAABORT : constant := SIGABRT;
124    --  Change this if you want to use another signal for task abort.
125    --  SIGTERM might be a good one.
126
127    type Signal_Set is array (Natural range <>) of Signal;
128
129    Unmasked    : constant Signal_Set := (
130       SIGTRAP,
131       --  To enable debugging on multithreaded applications, mark SIGTRAP to
132       --  be kept unmasked.
133
134       SIGBUS,
135
136       SIGTTIN, SIGTTOU, SIGTSTP,
137       --  Keep these three signals unmasked so that background processes
138       --  and IO behaves as normal "C" applications
139
140       SIGPROF,
141       --  To avoid confusing the profiler
142
143       SIGKILL, SIGSTOP,
144       --  These two signals actually cannot be masked;
145       --  POSIX simply won't allow it.
146
147       SIGLTHRRES, SIGLTHRCAN, SIGLTHRDBG);
148       --  These three signals are used by GNU/LinuxThreads starting from
149       --  glibc 2.1 (future 2.2).
150
151    Reserved    : constant Signal_Set :=
152    --  I am not sure why the following two signals are reserved.
153    --  I guess they are not supported by this version of GNU/Linux.
154      (SIGVTALRM, SIGUNUSED);
155
156    type sigset_t is private;
157
158    function sigaddset (set : access sigset_t; sig : Signal) return int;
159    pragma Import (C, sigaddset, "sigaddset");
160
161    function sigdelset (set : access sigset_t; sig : Signal) return int;
162    pragma Import (C, sigdelset, "sigdelset");
163
164    function sigfillset (set : access sigset_t) return int;
165    pragma Import (C, sigfillset, "sigfillset");
166
167    function sigismember (set : access sigset_t; sig : Signal) return int;
168    pragma Import (C, sigismember, "sigismember");
169
170    function sigemptyset (set : access sigset_t) return int;
171    pragma Import (C, sigemptyset, "sigemptyset");
172
173    type union_type_3 is new String (1 .. 116);
174    type siginfo_t is record
175       si_signo : int;
176       si_code  : int;
177       si_errno : int;
178       X_data   : union_type_3;
179    end record;
180    pragma Convention (C, siginfo_t);
181
182    type struct_sigaction is record
183       sa_handler   : System.Address;
184       sa_mask      : sigset_t;
185       sa_flags     : unsigned_long;
186       sa_restorer  : System.Address;
187    end record;
188    pragma Convention (C, struct_sigaction);
189    type struct_sigaction_ptr is access all struct_sigaction;
190
191    type Machine_State is record
192       eip : unsigned_long;
193       ebx : unsigned_long;
194       esp : unsigned_long;
195       ebp : unsigned_long;
196       esi : unsigned_long;
197       edi : unsigned_long;
198    end record;
199    type Machine_State_Ptr is access all Machine_State;
200
201    SA_SIGINFO  : constant := 16#04#;
202
203    SIG_BLOCK   : constant := 0;
204    SIG_UNBLOCK : constant := 1;
205    SIG_SETMASK : constant := 2;
206
207    SIG_DFL : constant := 0;
208    SIG_IGN : constant := 1;
209
210    function sigaction
211      (sig  : Signal;
212       act  : struct_sigaction_ptr;
213       oact : struct_sigaction_ptr) return int;
214    pragma Import (C, sigaction, "sigaction");
215
216    ----------
217    -- Time --
218    ----------
219
220    type timespec is private;
221
222    function To_Duration (TS : timespec) return Duration;
223    pragma Inline (To_Duration);
224
225    function To_Timespec (D : Duration) return timespec;
226    pragma Inline (To_Timespec);
227
228    type struct_timeval is private;
229
230    function To_Duration (TV : struct_timeval) return Duration;
231    pragma Inline (To_Duration);
232
233    function To_Timeval (D : Duration) return struct_timeval;
234    pragma Inline (To_Timeval);
235
236    function gettimeofday
237      (tv : access struct_timeval;
238       tz : System.Address := System.Null_Address) return int;
239    pragma Import (C, gettimeofday, "gettimeofday");
240
241    function sysconf (name : int) return long;
242    pragma Import (C, sysconf);
243
244    SC_CLK_TCK          : constant := 2;
245    SC_NPROCESSORS_ONLN : constant := 84;
246
247    -------------------------
248    -- Priority Scheduling --
249    -------------------------
250
251    SCHED_OTHER : constant := 0;
252    SCHED_FIFO  : constant := 1;
253    SCHED_RR    : constant := 2;
254
255    function To_Target_Priority
256      (Prio : System.Any_Priority) return Interfaces.C.int;
257    --  Maps System.Any_Priority to a POSIX priority
258
259    -------------
260    -- Process --
261    -------------
262
263    type pid_t is private;
264
265    function kill (pid : pid_t; sig : Signal) return int;
266    pragma Import (C, kill, "kill");
267
268    function getpid return pid_t;
269    pragma Import (C, getpid, "getpid");
270
271    -------------
272    -- Threads --
273    -------------
274
275    type Thread_Body is access
276      function (arg : System.Address) return System.Address;
277    pragma Convention (C, Thread_Body);
278
279    function Thread_Body_Access is new
280      Ada.Unchecked_Conversion (System.Address, Thread_Body);
281
282    type pthread_t is new unsigned_long;
283    subtype Thread_Id        is pthread_t;
284
285    function To_pthread_t is new Ada.Unchecked_Conversion
286      (unsigned_long, pthread_t);
287
288    type pthread_mutex_t     is limited private;
289    type pthread_cond_t      is limited private;
290    type pthread_attr_t      is limited private;
291    type pthread_mutexattr_t is limited private;
292    type pthread_condattr_t  is limited private;
293    type pthread_key_t       is private;
294
295    PTHREAD_CREATE_DETACHED : constant := 1;
296
297    -----------
298    -- Stack --
299    -----------
300
301    function Get_Stack_Base (thread : pthread_t) return Address;
302    pragma Inline (Get_Stack_Base);
303    --  This is a dummy procedure to share some GNULLI files
304
305    ---------------------------------------
306    -- Nonstandard Thread Initialization --
307    ---------------------------------------
308
309    procedure pthread_init;
310    pragma Inline (pthread_init);
311    --  This is a dummy procedure to share some GNULLI files
312
313    -------------------------
314    -- POSIX.1c  Section 3 --
315    -------------------------
316
317    function sigwait (set : access sigset_t; sig : access Signal) return int;
318    pragma Import (C, sigwait, "sigwait");
319
320    function pthread_kill (thread : pthread_t; sig : Signal) return int;
321    pragma Import (C, pthread_kill, "pthread_kill");
322
323    function pthread_sigmask
324      (how  : int;
325       set  : access sigset_t;
326       oset : access sigset_t) return int;
327    pragma Import (C, pthread_sigmask, "pthread_sigmask");
328
329    --------------------------
330    -- POSIX.1c  Section 11 --
331    --------------------------
332
333    function pthread_mutexattr_init
334      (attr : access pthread_mutexattr_t) return int;
335    pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
336
337    function pthread_mutexattr_destroy
338      (attr : access pthread_mutexattr_t) return int;
339    pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
340
341    function pthread_mutex_init
342      (mutex : access pthread_mutex_t;
343       attr  : access pthread_mutexattr_t) return int;
344    pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
345
346    function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
347    pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
348
349    function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
350    pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
351
352    function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
353    pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
354
355    function pthread_condattr_init
356      (attr : access pthread_condattr_t) return int;
357    pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
358
359    function pthread_condattr_destroy
360      (attr : access pthread_condattr_t) return int;
361    pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
362
363    function pthread_cond_init
364      (cond : access pthread_cond_t;
365       attr : access pthread_condattr_t) return int;
366    pragma Import (C, pthread_cond_init, "pthread_cond_init");
367
368    function pthread_cond_destroy (cond : access pthread_cond_t) return int;
369    pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
370
371    function pthread_cond_signal (cond : access pthread_cond_t) return int;
372    pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
373
374    function pthread_cond_wait
375      (cond  : access pthread_cond_t;
376       mutex : access pthread_mutex_t) return int;
377    pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
378
379    function pthread_cond_timedwait
380      (cond    : access pthread_cond_t;
381       mutex   : access pthread_mutex_t;
382       abstime : access timespec) return int;
383    pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
384
385    --------------------------
386    -- POSIX.1c  Section 13 --
387    --------------------------
388
389    type struct_sched_param is record
390       sched_priority : int;  --  scheduling priority
391    end record;
392    pragma Convention (C, struct_sched_param);
393
394    function pthread_setschedparam
395      (thread : pthread_t;
396       policy : int;
397       param  : access struct_sched_param) return int;
398    pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
399
400    function pthread_attr_setschedpolicy
401      (attr   : access pthread_attr_t;
402       policy : int) return int;
403    pragma Import
404      (C, pthread_attr_setschedpolicy, "pthread_attr_setschedpolicy");
405
406    function sched_yield return int;
407    pragma Import (C, sched_yield, "sched_yield");
408
409    ---------------------------
410    -- P1003.1c - Section 16 --
411    ---------------------------
412
413    function pthread_attr_init
414      (attributes : access pthread_attr_t) return int;
415    pragma Import (C, pthread_attr_init, "pthread_attr_init");
416
417    function pthread_attr_destroy
418      (attributes : access pthread_attr_t) return int;
419    pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
420
421    function pthread_attr_setdetachstate
422      (attr        : access pthread_attr_t;
423       detachstate : int) return int;
424    pragma Import
425      (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate");
426
427    function pthread_attr_setstacksize
428      (attr      : access pthread_attr_t;
429       stacksize : size_t) return int;
430    pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
431
432    function pthread_create
433      (thread        : access pthread_t;
434       attributes    : access pthread_attr_t;
435       start_routine : Thread_Body;
436       arg           : System.Address) return int;
437    pragma Import (C, pthread_create, "pthread_create");
438
439    procedure pthread_exit (status : System.Address);
440    pragma Import (C, pthread_exit, "pthread_exit");
441
442    function pthread_self return pthread_t;
443    pragma Import (C, pthread_self, "pthread_self");
444
445    --------------------------
446    -- POSIX.1c  Section 17 --
447    --------------------------
448
449    function pthread_setspecific
450      (key   : pthread_key_t;
451       value : System.Address) return int;
452    pragma Import (C, pthread_setspecific, "pthread_setspecific");
453
454    function pthread_getspecific (key : pthread_key_t) return System.Address;
455    pragma Import (C, pthread_getspecific, "pthread_getspecific");
456
457    type destructor_pointer is access procedure (arg : System.Address);
458    pragma Convention (C, destructor_pointer);
459
460    function pthread_key_create
461      (key        : access pthread_key_t;
462       destructor : destructor_pointer) return int;
463    pragma Import (C, pthread_key_create, "pthread_key_create");
464
465    CPU_SETSIZE : constant := 1_024;
466
467    type bit_field is array (1 .. CPU_SETSIZE) of Boolean;
468    for bit_field'Size use CPU_SETSIZE;
469    pragma Pack (bit_field);
470    pragma Convention (C, bit_field);
471
472    type cpu_set_t is record
473       bits : bit_field;
474    end record;
475    pragma Convention (C, cpu_set_t);
476
477    function pthread_setaffinity_np
478      (thread     : pthread_t;
479       cpusetsize : size_t;
480       cpuset     : access cpu_set_t) return int;
481    pragma Import (C, pthread_setaffinity_np, "__gnat_pthread_setaffinity_np");
482
483 private
484
485    type sigset_t is array (0 .. 127) of unsigned_char;
486    pragma Convention (C, sigset_t);
487    for sigset_t'Alignment use unsigned_long'Alignment;
488
489    type pid_t is new int;
490
491    type time_t is new long;
492
493    type timespec is record
494       tv_sec  : time_t;
495       tv_nsec : long;
496    end record;
497    pragma Convention (C, timespec);
498
499    type struct_timeval is record
500       tv_sec  : time_t;
501       tv_usec : time_t;
502    end record;
503    pragma Convention (C, struct_timeval);
504
505    type pthread_attr_t is record
506       detachstate   : int;
507       schedpolicy   : int;
508       schedparam    : struct_sched_param;
509       inheritsched  : int;
510       scope         : int;
511       guardsize     : size_t;
512       stackaddr_set : int;
513       stackaddr     : System.Address;
514       stacksize     : size_t;
515    end record;
516    pragma Convention (C, pthread_attr_t);
517
518    type pthread_condattr_t is record
519       dummy : int;
520    end record;
521    pragma Convention (C, pthread_condattr_t);
522
523    type pthread_mutexattr_t is record
524       mutexkind : int;
525    end record;
526    pragma Convention (C, pthread_mutexattr_t);
527
528    type struct_pthread_fast_lock is record
529       status   : long;
530       spinlock : int;
531    end record;
532    pragma Convention (C, struct_pthread_fast_lock);
533
534    type pthread_mutex_t is record
535       m_reserved : int;
536       m_count    : int;
537       m_owner    : System.Address;
538       m_kind     : int;
539       m_lock     : struct_pthread_fast_lock;
540    end record;
541    pragma Convention (C, pthread_mutex_t);
542
543    type pthread_cond_t is array (0 .. 47) of unsigned_char;
544    pragma Convention (C, pthread_cond_t);
545
546    type pthread_key_t is new unsigned;
547
548 end System.OS_Interface;