OSDN Git Service

2004-05-17 Steve Kargl <kargls@comcast.net>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osinte-freebsd.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNU ADA RUNTIME 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-2004, 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,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, 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. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com).                                  --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 --  This is the FreeBSD PTHREADS version of this package
37
38 --  This package encapsulates all direct interfaces to OS services
39 --  that are needed by children of System.
40
41 --  PLEASE DO NOT add any with-clauses to this package or remove the pragma
42 --  Preelaborate. This package is designed to be a bottom-level (leaf) package.
43
44 with Interfaces.C;
45 with Unchecked_Conversion;
46
47 package System.OS_Interface is
48    pragma Preelaborate;
49
50    pragma Linker_Options ("-pthread");
51
52    subtype int            is Interfaces.C.int;
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 Inline (Errno);
68
69    EAGAIN   : constant := 35;
70    EINTR    : constant := 4;
71    EINVAL   : constant := 22;
72    ENOMEM   : constant := 12;
73    ETIMEDOUT    : constant := 60;
74
75    -------------
76    -- Signals --
77    -------------
78
79    Max_Interrupt : constant := 31;
80    type Signal is new int range 0 .. Max_Interrupt;
81    for Signal'Size use int'Size;
82
83    SIGHUP     : constant := 1; --  hangup
84    SIGINT     : constant := 2; --  interrupt (rubout)
85    SIGQUIT    : constant := 3; --  quit (ASCD FS)
86    SIGILL     : constant := 4; --  illegal instruction (not reset)
87    SIGTRAP    : constant := 5; --  trace trap (not reset)
88    SIGIOT     : constant := 6; --  IOT instruction
89    SIGABRT    : constant := 6; --  used by abort, replace SIGIOT in the  future
90    SIGEMT     : constant := 7; --  EMT instruction
91    SIGFPE     : constant := 8; --  floating point exception
92    SIGKILL    : constant := 9; --  kill (cannot be caught or ignored)
93    SIGBUS     : constant := 10; --  bus error
94    SIGSEGV    : constant := 11; --  segmentation violation
95    SIGSYS     : constant := 12; --  bad argument to system call
96    SIGPIPE    : constant := 13; --  write on a pipe with no one to read it
97    SIGALRM    : constant := 14; --  alarm clock
98    SIGTERM    : constant := 15; --  software termination signal from kill
99    SIGURG     : constant := 16; --  urgent condition on IO channel
100    SIGSTOP    : constant := 17; --  stop (cannot be caught or ignored)
101    SIGTSTP    : constant := 18; --  user stop requested from tty
102    SIGCONT    : constant := 19; --  stopped process has been continued
103    SIGCLD     : constant := 20; --  alias for SIGCHLD
104    SIGCHLD    : constant := 20; --  child status change
105    SIGTTIN    : constant := 21; --  background tty read attempted
106    SIGTTOU    : constant := 22; --  background tty write attempted
107    SIGIO      : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
108    SIGXCPU    : constant := 24; --  CPU time limit exceeded
109    SIGXFSZ    : constant := 25; --  filesize limit exceeded
110    SIGVTALRM  : constant := 26; --  virtual timer expired
111    SIGPROF    : constant := 27; --  profiling timer expired
112    SIGWINCH   : constant := 28; --  window size change
113    SIGINFO    : constant := 29; --  information request (NetBSD/FreeBSD)
114    SIGUSR1    : constant := 30; --  user defined signal 1
115    SIGUSR2    : constant := 31; --  user defined signal 2
116
117    SIGADAABORT : constant := SIGABRT;
118    --  Change this if you want to use another signal for task abort.
119    --  SIGTERM might be a good one.
120
121    type Signal_Set is array (Natural range <>) of Signal;
122
123    --  Interrupts that must be unmasked at all times.  FreeBSD
124    --  pthreads will not allow an application to mask out any
125    --  interrupt needed by the threads library.
126    Unmasked : constant Signal_Set :=
127      (SIGTRAP, SIGBUS, SIGTTIN, SIGTTOU, SIGTSTP);
128
129    --  FreeBSD will uses SIGPROF for timing.  Do not allow a
130    --  handler to attach to this signal.
131    Reserved : constant Signal_Set := (0 .. 0 => SIGPROF);
132
133    type sigset_t is private;
134
135    function sigaddset
136      (set : access sigset_t;
137       sig : Signal) return int;
138    pragma Import (C, sigaddset, "sigaddset");
139
140    function sigdelset
141      (set : access sigset_t;
142       sig : Signal) return int;
143    pragma Import (C, sigdelset, "sigdelset");
144
145    function sigfillset (set : access sigset_t) return int;
146    pragma Import (C, sigfillset, "sigfillset");
147
148    function sigismember
149      (set : access sigset_t;
150       sig : Signal) return int;
151    pragma Import (C, sigismember, "sigismember");
152
153    function sigemptyset (set : access sigset_t) return int;
154    pragma Import (C, sigemptyset, "sigemptyset");
155
156    --  sigcontext is architecture dependent, so define it private
157    type struct_sigcontext is private;
158
159    type old_struct_sigaction is record
160       sa_handler : System.Address;
161       sa_mask    : sigset_t;
162       sa_flags   : int;
163    end record;
164    pragma Convention (C, old_struct_sigaction);
165
166    type new_struct_sigaction is record
167       sa_handler : System.Address;
168       sa_flags   : int;
169       sa_mask    : sigset_t;
170    end record;
171    pragma Convention (C, new_struct_sigaction);
172
173    subtype struct_sigaction is new_struct_sigaction;
174    type struct_sigaction_ptr is access all struct_sigaction;
175
176    SIG_BLOCK   : constant := 1;
177    SIG_UNBLOCK : constant := 2;
178    SIG_SETMASK : constant := 3;
179
180    SIG_DFL : constant := 0;
181    SIG_IGN : constant := 1;
182
183    SA_SIGINFO : constant := 16#0040#;
184
185    function sigaction
186      (sig  : Signal;
187       act  : struct_sigaction_ptr;
188       oact : struct_sigaction_ptr) return int;
189    pragma Import (C, sigaction, "sigaction");
190
191    ----------
192    -- Time --
193    ----------
194
195    Time_Slice_Supported : constant Boolean := True;
196    --  Indicates wether time slicing is supported (i.e SCHED_RR is supported)
197
198    type timespec is private;
199
200    function nanosleep (rqtp, rmtp : access timespec)  return int;
201    pragma Import (C, nanosleep, "nanosleep");
202
203    type clockid_t is private;
204
205    CLOCK_REALTIME : constant clockid_t;
206
207    function clock_gettime
208      (clock_id : clockid_t;
209       tp       : access timespec)
210       return int;
211    pragma Import (C, clock_gettime, "clock_gettime");
212
213    function To_Duration (TS : timespec) return Duration;
214    pragma Inline (To_Duration);
215
216    function To_Timespec (D : Duration) return timespec;
217    pragma Inline (To_Timespec);
218
219    type struct_timezone is record
220       tz_minuteswest : int;
221       tz_dsttime     : int;
222    end record;
223    pragma Convention (C, struct_timezone);
224    type struct_timeval is private;
225    --  This is needed on systems that do not have clock_gettime()
226    --  but do have gettimeofday().
227
228    function To_Duration (TV : struct_timeval) return Duration;
229    pragma Inline (To_Duration);
230
231    function To_Timeval (D : Duration) return struct_timeval;
232    pragma Inline (To_Timeval);
233
234    function gettimeofday
235      (tv : access struct_timeval;
236       tz : System.Address) return int;
237    pragma Import (C, gettimeofday, "gettimeofday");
238
239    procedure usleep (useconds : unsigned_long);
240    pragma Import (C, usleep, "usleep");
241
242    -------------------------
243    -- Priority Scheduling --
244    -------------------------
245
246    SCHED_FIFO  : constant := 1;
247    SCHED_OTHER : constant := 2;
248    SCHED_RR    : constant := 3;
249
250    -------------
251    -- Process --
252    -------------
253
254    type pid_t is private;
255
256    Self_PID : constant pid_t;
257
258    function kill (pid : pid_t; sig : Signal) return int;
259    pragma Import (C, kill, "kill");
260
261    function getpid return pid_t;
262    pragma Import (C, getpid, "getpid");
263
264    ---------
265    -- LWP --
266    ---------
267
268    function lwp_self return System.Address;
269    --  lwp_self does not exist on this thread library, revert to pthread_self
270    --  which is the closest approximation (with getpid). This function is
271    --  needed to share 7staprop.adb across POSIX-like targets.
272    pragma Import (C, lwp_self, "pthread_self");
273
274    -------------
275    -- Threads --
276    -------------
277
278    type Thread_Body is access
279      function (arg : System.Address) return System.Address;
280
281    function Thread_Body_Access is new
282      Unchecked_Conversion (System.Address, Thread_Body);
283
284    type pthread_t           is private;
285    subtype Thread_Id        is pthread_t;
286
287    type pthread_mutex_t     is limited private;
288    type pthread_cond_t      is limited private;
289    type pthread_attr_t      is limited private;
290    type pthread_mutexattr_t is limited private;
291    type pthread_condattr_t  is limited private;
292    type pthread_key_t       is private;
293
294    PTHREAD_CREATE_DETACHED : constant := 1;
295    PTHREAD_CREATE_JOINABLE : constant := 0;
296
297    -----------
298    -- Stack --
299    -----------
300
301    Stack_Base_Available : constant Boolean := False;
302    --  Indicates wether the stack base is available on this target.
303    --  This allows us to share s-osinte.adb between all the FSU run time.
304    --  Note that this value can only be true if pthread_t has a complete
305    --  definition that corresponds exactly to the C header files.
306
307    function Get_Stack_Base (thread : pthread_t) return Address;
308    pragma Inline (Get_Stack_Base);
309    --  returns the stack base of the specified thread.
310    --  Only call this function when Stack_Base_Available is True.
311
312    function Get_Page_Size return size_t;
313    function Get_Page_Size return Address;
314    pragma Import (C, Get_Page_Size, "getpagesize");
315    --  returns the size of a page, or 0 if this is not relevant on this
316    --  target
317
318    PROT_NONE  : constant := 0;
319    PROT_READ  : constant := 1;
320    PROT_WRITE : constant := 2;
321    PROT_EXEC  : constant := 4;
322    PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
323
324    PROT_ON    : constant := PROT_NONE;
325    PROT_OFF   : constant := PROT_ALL;
326
327    function mprotect
328      (addr : Address; len : size_t; prot : int) return int;
329    pragma Import (C, mprotect);
330
331    -----------------------------------------
332    --  Nonstandard Thread Initialization  --
333    -----------------------------------------
334    --  FSU_THREADS requires pthread_init, which is nonstandard
335    --  and this should be invoked during the elaboration of s-taprop.adb
336    --
337    --  FreeBSD does not require this so we provide an empty Ada body.
338    procedure pthread_init;
339
340    ---------------------------
341    --  POSIX.1c  Section 3  --
342    ---------------------------
343
344    function sigwait
345      (set : access sigset_t;
346       sig : access Signal) return int;
347    pragma Import (C, sigwait, "sigwait");
348
349    function pthread_kill
350      (thread : pthread_t;
351       sig    : Signal) return   int;
352    pragma Import (C, pthread_kill, "pthread_kill");
353
354    type sigset_t_ptr is access all sigset_t;
355
356    function pthread_sigmask
357      (how  : int;
358       set  : sigset_t_ptr;
359       oset : sigset_t_ptr) return int;
360    pragma Import (C, pthread_sigmask, "pthread_sigmask");
361
362    ----------------------------
363    --  POSIX.1c  Section 11  --
364    ----------------------------
365
366    function pthread_mutexattr_init
367      (attr : access pthread_mutexattr_t) return int;
368    pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
369
370    function pthread_mutexattr_destroy
371      (attr : access pthread_mutexattr_t) return int;
372    pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
373
374    function pthread_mutex_init
375      (mutex : access pthread_mutex_t;
376       attr  : access pthread_mutexattr_t) return int;
377    pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
378
379    function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
380    pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
381
382    function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
383    pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
384
385    function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
386    pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
387
388    function pthread_condattr_init
389      (attr : access pthread_condattr_t) return int;
390    pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
391
392    function pthread_condattr_destroy
393      (attr : access pthread_condattr_t) return int;
394    pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
395
396    function pthread_cond_init
397      (cond : access pthread_cond_t;
398       attr : access pthread_condattr_t) return int;
399    pragma Import (C, pthread_cond_init, "pthread_cond_init");
400
401    function pthread_cond_destroy (cond : access pthread_cond_t) return int;
402    pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
403
404    function pthread_cond_signal (cond : access pthread_cond_t) return int;
405    pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
406
407    function pthread_cond_wait
408      (cond  : access pthread_cond_t;
409       mutex : access pthread_mutex_t) return int;
410    pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
411
412    function pthread_cond_timedwait
413      (cond    : access pthread_cond_t;
414       mutex   : access pthread_mutex_t;
415       abstime : access timespec) return int;
416    pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
417
418    Relative_Timed_Wait : constant Boolean := False;
419    --  pthread_cond_timedwait requires an absolute delay time
420
421    ----------------------------
422    --  POSIX.1c  Section 13  --
423    ----------------------------
424
425    PTHREAD_PRIO_NONE    : constant := 0;
426    PTHREAD_PRIO_PROTECT : constant := 2;
427    PTHREAD_PRIO_INHERIT : constant := 1;
428
429    function pthread_mutexattr_setprotocol
430      (attr     : access pthread_mutexattr_t;
431       protocol : int) return int;
432    pragma Import
433       (C, pthread_mutexattr_setprotocol, "pthread_mutexattr_setprotocol");
434
435    function pthread_mutexattr_getprotocol
436      (attr     : access pthread_mutexattr_t;
437       protocol : access int) return int;
438    pragma Import
439      (C, pthread_mutexattr_getprotocol, "pthread_mutexattr_getprotocol");
440
441    function pthread_mutexattr_setprioceiling
442      (attr     : access pthread_mutexattr_t;
443       prioceiling : int) return int;
444    pragma Import
445      (C, pthread_mutexattr_setprioceiling,
446       "pthread_mutexattr_setprioceiling");
447
448    function pthread_mutexattr_getprioceiling
449      (attr     : access pthread_mutexattr_t;
450       prioceiling : access int) return int;
451    pragma Import
452      (C, pthread_mutexattr_getprioceiling,
453       "pthread_mutexattr_getprioceiling");
454
455    type struct_sched_param is record
456       sched_priority : int;
457    end record;
458    pragma Convention (C, struct_sched_param);
459
460    function pthread_getschedparam
461      (thread : pthread_t;
462       policy : access int;
463       param  : access struct_sched_param) return int;
464    pragma Import (C, pthread_getschedparam, "pthread_getschedparam");
465
466    function pthread_setschedparam
467      (thread : pthread_t;
468       policy : int;
469       param  : access struct_sched_param) return int;
470    pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
471
472    function pthread_attr_setscope
473      (attr            : access pthread_attr_t;
474       contentionscope : int) return int;
475    pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
476
477    function pthread_attr_getscope
478      (attr            : access pthread_attr_t;
479       contentionscope : access int) return int;
480    pragma Import (C, pthread_attr_getscope, "pthread_attr_getscope");
481
482    function pthread_attr_setinheritsched
483      (attr            : access pthread_attr_t;
484       inheritsched : int) return int;
485    pragma Import
486      (C, pthread_attr_setinheritsched, "pthread_attr_setinheritsched");
487
488    function pthread_attr_getinheritsched
489      (attr         : access pthread_attr_t;
490       inheritsched : access int) return int;
491    pragma Import
492      (C, pthread_attr_getinheritsched, "pthread_attr_getinheritsched");
493
494    function pthread_attr_setschedpolicy
495      (attr   : access pthread_attr_t;
496       policy : int) return int;
497    pragma Import (C, pthread_attr_setschedpolicy,
498      "pthread_attr_setschedpolicy");
499
500    function pthread_attr_getschedpolicy
501      (attr   : access pthread_attr_t;
502       policy : access int) return int;
503    pragma Import (C, pthread_attr_getschedpolicy,
504      "pthread_attr_getschedpolicy");
505
506    function pthread_attr_setschedparam
507      (attr        : access pthread_attr_t;
508       sched_param : int) return int;
509    pragma Import (C, pthread_attr_setschedparam, "pthread_attr_setschedparam");
510
511    function pthread_attr_getschedparam
512      (attr        : access pthread_attr_t;
513       sched_param : access int) return int;
514    pragma Import (C, pthread_attr_getschedparam, "pthread_attr_getschedparam");
515
516    function sched_yield return int;
517    pragma Import (C, sched_yield, "pthread_yield");
518
519    -----------------------------
520    --  P1003.1c - Section 16  --
521    -----------------------------
522
523    function pthread_attr_init (attributes : access pthread_attr_t) return int;
524    pragma Import (C, pthread_attr_init, "pthread_attr_init");
525
526    function pthread_attr_destroy
527      (attributes : access pthread_attr_t) return int;
528    pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
529
530    function pthread_attr_setdetachstate
531      (attr        : access pthread_attr_t;
532       detachstate : int) return int;
533    pragma Import
534      (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate");
535
536    function pthread_attr_getdetachstate
537      (attr        : access pthread_attr_t;
538       detachstate : access int) return int;
539    pragma Import
540      (C, pthread_attr_getdetachstate, "pthread_attr_getdetachstate");
541
542    function pthread_attr_getstacksize
543      (attr      : access pthread_attr_t;
544       stacksize : access size_t) return int;
545    pragma Import
546      (C, pthread_attr_getstacksize, "pthread_attr_getstacksize");
547
548    function pthread_attr_setstacksize
549      (attr      : access pthread_attr_t;
550       stacksize : size_t) return int;
551    pragma Import
552      (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
553
554    function pthread_create
555      (thread        : access pthread_t;
556       attributes    : access pthread_attr_t;
557       start_routine : Thread_Body;
558       arg           : System.Address) return int;
559    pragma Import (C, pthread_create, "pthread_create");
560
561    function pthread_detach (thread : pthread_t) return int;
562    pragma Import (C, pthread_detach, "pthread_detach");
563
564    procedure pthread_exit (status : System.Address);
565    pragma Import (C, pthread_exit, "pthread_exit");
566
567    function pthread_self return pthread_t;
568    pragma Import (C, pthread_self, "pthread_self");
569
570    ----------------------------
571    --  POSIX.1c  Section 17  --
572    ----------------------------
573
574    function pthread_setspecific
575      (key   : pthread_key_t;
576       value : System.Address) return  int;
577    pragma Import (C, pthread_setspecific, "pthread_setspecific");
578
579    function pthread_getspecific (key : pthread_key_t) return System.Address;
580    pragma Import (C, pthread_getspecific, "pthread_getspecific");
581
582    type destructor_pointer is access
583       procedure (arg : System.Address);
584
585    function pthread_key_create
586      (key        : access pthread_key_t;
587       destructor : destructor_pointer) return int;
588    pragma Import (C, pthread_key_create, "pthread_key_create");
589
590    --------------------------------------
591    --  Non-portable pthread functions  --
592    --------------------------------------
593
594    function pthread_set_name_np
595      (thread : pthread_t;
596       name   : System.Address) return int;
597    pragma Import (C, pthread_set_name_np, "pthread_set_name_np");
598
599 private
600
601    type sigset_t is array (1 .. 4) of unsigned;
602
603    --  In FreeBSD the component sa_handler turns out to
604    --  be one a union type, and the selector is a macro:
605    --  #define sa_handler __sigaction_u._handler
606    --  #define sa_sigaction __sigaction_u._sigaction
607
608    --  Should we add a signal_context type here ?
609    --  How could it be done independent of the CPU architecture ?
610    --  sigcontext type is opaque, so it is architecturally neutral.
611    --  It is always passed as an access type, so define it as an empty record
612    --  since the contents are not used anywhere.
613    type struct_sigcontext is null record;
614    pragma Convention (C, struct_sigcontext);
615
616    type pid_t is new int;
617    Self_PID : constant pid_t := 0;
618
619    type time_t is new long;
620
621    type timespec is record
622       ts_sec  : time_t;
623       ts_nsec : long;
624    end record;
625    pragma Convention (C, timespec);
626
627    type clockid_t is new int;
628    CLOCK_REALTIME : constant clockid_t := 0;
629
630    type struct_timeval is record
631       tv_sec  : long;
632       tv_usec : long;
633    end record;
634    pragma Convention (C, struct_timeval);
635
636    type pthread_t           is new System.Address;
637    type pthread_attr_t      is new System.Address;
638    type pthread_mutex_t     is new System.Address;
639    type pthread_mutexattr_t is new System.Address;
640    type pthread_cond_t      is new System.Address;
641    type pthread_condattr_t  is new System.Address;
642    type pthread_key_t       is new int;
643
644 end System.OS_Interface;