OSDN Git Service

2009-04-16 Tristan Gingold <gingold@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-osinte-darwin.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-2008, 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 Darwin pthreads version of this package
36
37 --  This package includes all direct interfaces to OS services that are needed
38 --  by the tasking run-time (libgnarl).
39
40 --  PLEASE DO NOT add any with-clauses to this package or remove the pragma
41 --  Elaborate_Body. It is designed to be a bottom-level (leaf) package.
42
43 with Interfaces.C;
44 with System.OS_Constants;
45
46 package System.OS_Interface is
47    pragma Preelaborate;
48
49    subtype int            is Interfaces.C.int;
50    subtype short          is Interfaces.C.short;
51    subtype long           is Interfaces.C.long;
52    subtype unsigned       is Interfaces.C.unsigned;
53    subtype unsigned_short is Interfaces.C.unsigned_short;
54    subtype unsigned_long  is Interfaces.C.unsigned_long;
55    subtype unsigned_char  is Interfaces.C.unsigned_char;
56    subtype plain_char     is Interfaces.C.plain_char;
57    subtype size_t         is Interfaces.C.size_t;
58
59    -----------
60    -- Errno --
61    -----------
62
63    function errno return int;
64    pragma Import (C, errno, "__get_errno");
65
66    EINTR     : constant := 4;
67    ENOMEM    : constant := 12;
68    EINVAL    : constant := 22;
69    EAGAIN    : constant := 35;
70    ETIMEDOUT : constant := 60;
71
72    -------------
73    -- Signals --
74    -------------
75
76    Max_Interrupt : constant := 31;
77    type Signal is new int range 0 .. Max_Interrupt;
78    for Signal'Size use int'Size;
79
80    SIGHUP     : constant := 1; --  hangup
81    SIGINT     : constant := 2; --  interrupt (rubout)
82    SIGQUIT    : constant := 3; --  quit (ASCD FS)
83    SIGILL     : constant := 4; --  illegal instruction (not reset)
84    SIGTRAP    : constant := 5; --  trace trap (not reset)
85    SIGIOT     : constant := 6; --  IOT instruction
86    SIGABRT    : constant := 6; --  used by abort, replace SIGIOT in the  future
87    SIGEMT     : constant := 7; --  EMT instruction
88    SIGFPE     : constant := 8; --  floating point exception
89    SIGKILL    : constant := 9; --  kill (cannot be caught or ignored)
90    SIGBUS     : constant := 10; --  bus error
91    SIGSEGV    : constant := 11; --  segmentation violation
92    SIGSYS     : constant := 12; --  bad argument to system call
93    SIGPIPE    : constant := 13; --  write on a pipe with no one to read it
94    SIGALRM    : constant := 14; --  alarm clock
95    SIGTERM    : constant := 15; --  software termination signal from kill
96    SIGURG     : constant := 16; --  urgent condition on IO channel
97    SIGSTOP    : constant := 17; --  stop (cannot be caught or ignored)
98    SIGTSTP    : constant := 18; --  user stop requested from tty
99    SIGCONT    : constant := 19; --  stopped process has been continued
100    SIGCHLD    : constant := 20; --  child status change
101    SIGTTIN    : constant := 21; --  background tty read attempted
102    SIGTTOU    : constant := 22; --  background tty write attempted
103    SIGIO      : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
104    SIGXCPU    : constant := 24; --  CPU time limit exceeded
105    SIGXFSZ    : constant := 25; --  filesize limit exceeded
106    SIGVTALRM  : constant := 26; --  virtual timer expired
107    SIGPROF    : constant := 27; --  profiling timer expired
108    SIGWINCH   : constant := 28; --  window size change
109    SIGINFO    : constant := 29; --  information request
110    SIGUSR1    : constant := 30; --  user defined signal 1
111    SIGUSR2    : constant := 31; --  user defined signal 2
112
113    SIGADAABORT : constant := SIGTERM;
114    --  Change this if you want to use another signal for task abort.
115    --  SIGTERM might be a good one.
116
117    type Signal_Set is array (Natural range <>) of Signal;
118
119    Unmasked : constant Signal_Set :=
120                 (SIGTTIN, SIGTTOU, SIGSTOP, SIGTSTP);
121
122    Reserved : constant Signal_Set :=
123                 (SIGKILL, SIGSTOP);
124
125    Exception_Signals : constant Signal_Set :=
126                          (SIGFPE, SIGILL, SIGSEGV, SIGBUS);
127    --  These signals (when runtime or system) will be caught and converted
128    --  into an Ada exception.
129
130    type sigset_t is private;
131
132    function sigaddset (set : access sigset_t; sig : Signal) return int;
133    pragma Import (C, sigaddset, "sigaddset");
134
135    function sigdelset (set : access sigset_t; sig : Signal) return int;
136    pragma Import (C, sigdelset, "sigdelset");
137
138    function sigfillset (set : access sigset_t) return int;
139    pragma Import (C, sigfillset, "sigfillset");
140
141    function sigismember (set : access sigset_t; sig : Signal) return int;
142    pragma Import (C, sigismember, "sigismember");
143
144    function sigemptyset (set : access sigset_t) return int;
145    pragma Import (C, sigemptyset, "sigemptyset");
146
147    type siginfo_t is private;
148    type ucontext_t is private;
149
150    type Signal_Handler is access procedure
151      (signo   : Signal;
152       info    : access siginfo_t;
153       context : access ucontext_t);
154
155    type struct_sigaction is record
156       sa_handler : System.Address;
157       sa_mask    : sigset_t;
158       sa_flags   : int;
159    end record;
160    pragma Convention (C, struct_sigaction);
161    type struct_sigaction_ptr is access all struct_sigaction;
162
163    SIG_BLOCK   : constant := 1;
164    SIG_UNBLOCK : constant := 2;
165    SIG_SETMASK : constant := 3;
166
167    SIG_DFL : constant := 0;
168    SIG_IGN : constant := 1;
169
170    SA_SIGINFO : constant := 16#0040#;
171    SA_ONSTACK : constant := 16#0001#;
172
173    function sigaction
174      (sig  : Signal;
175       act  : struct_sigaction_ptr;
176       oact : struct_sigaction_ptr) return int;
177    pragma Import (C, sigaction, "sigaction");
178
179    ----------
180    -- Time --
181    ----------
182
183    Time_Slice_Supported : constant Boolean := True;
184    --  Indicates whether time slicing is supported
185
186    type timespec is private;
187
188    type clockid_t is private;
189
190    CLOCK_REALTIME : constant clockid_t;
191
192    function clock_gettime
193      (clock_id : clockid_t;
194       tp       : access timespec) return int;
195
196    function To_Duration (TS : timespec) return Duration;
197    pragma Inline (To_Duration);
198
199    function To_Timespec (D : Duration) return timespec;
200    pragma Inline (To_Timespec);
201
202    type struct_timeval is private;
203
204    function To_Duration (TV : struct_timeval) return Duration;
205    pragma Inline (To_Duration);
206
207    function To_Timeval (D : Duration) return struct_timeval;
208    pragma Inline (To_Timeval);
209
210    -------------------------
211    -- Priority Scheduling --
212    -------------------------
213
214    SCHED_OTHER : constant := 1;
215    SCHED_RR    : constant := 2;
216    SCHED_FIFO  : constant := 4;
217
218    function To_Target_Priority
219      (Prio : System.Any_Priority) return Interfaces.C.int;
220    --  Maps System.Any_Priority to a POSIX priority
221
222    -------------
223    -- Process --
224    -------------
225
226    type pid_t is private;
227
228    function kill (pid : pid_t; sig : Signal) return int;
229    pragma Import (C, kill, "kill");
230
231    function getpid return pid_t;
232    pragma Import (C, getpid, "getpid");
233
234    ---------
235    -- LWP --
236    ---------
237
238    function lwp_self return System.Address;
239    pragma Import (C, lwp_self, "pthread_self");
240    --  lwp_self does not exist on this thread library, revert to pthread_self
241    --  which is the closest approximation (with getpid). This function is
242    --  needed to share 7staprop.adb across POSIX-like targets.
243
244    -------------
245    -- Threads --
246    -------------
247
248    type Thread_Body is access
249      function (arg : System.Address) return System.Address;
250    pragma Convention (C, Thread_Body);
251
252    type pthread_t           is private;
253    subtype Thread_Id        is pthread_t;
254
255    type pthread_mutex_t     is limited private;
256    type pthread_cond_t      is limited private;
257    type pthread_attr_t      is limited private;
258    type pthread_mutexattr_t is limited private;
259    type pthread_condattr_t  is limited private;
260    type pthread_key_t       is private;
261
262    type pthread_mutex_ptr is access all pthread_mutex_t;
263    type pthread_cond_ptr is access all pthread_cond_t;
264
265    PTHREAD_CREATE_DETACHED : constant := 2;
266
267    PTHREAD_SCOPE_PROCESS : constant := 2;
268    PTHREAD_SCOPE_SYSTEM  : constant := 1;
269
270    -----------
271    -- Stack --
272    -----------
273
274    type stack_t is record
275       ss_sp    : System.Address;
276       ss_size  : size_t;
277       ss_flags : int;
278    end record;
279    pragma Convention (C, stack_t);
280
281    function sigaltstack
282      (ss  : not null access stack_t;
283       oss : access stack_t) return int;
284    pragma Import (C, sigaltstack, "sigaltstack");
285
286    Alternate_Stack : aliased System.Address;
287    pragma Import (C, Alternate_Stack, "__gnat_alternate_stack");
288    --  The alternate signal stack for stack overflows
289
290    Alternate_Stack_Size : constant := 64 * 1024;
291    --  This must be in keeping with init.c:__gnat_alternate_stack
292
293    Stack_Base_Available : constant Boolean := False;
294    --  Indicates whether the stack base is available on this target. This
295    --  allows us to share s-osinte.adb between all the FSU run time. Note that
296    --  this value can only be true if pthread_t has a complete definition that
297    --  corresponds exactly to the C header files.
298
299    function Get_Stack_Base (thread : pthread_t) return System.Address;
300    pragma Inline (Get_Stack_Base);
301    --  returns the stack base of the specified thread. Only call this function
302    --  when Stack_Base_Available is True.
303
304    function Get_Page_Size return size_t;
305    function Get_Page_Size return System.Address;
306    pragma Import (C, Get_Page_Size, "getpagesize");
307    --  Returns the size of a page, or 0 if this is not relevant on this target
308
309    PROT_NONE  : constant := 0;
310    PROT_READ  : constant := 1;
311    PROT_WRITE : constant := 2;
312    PROT_EXEC  : constant := 4;
313    PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
314
315    PROT_ON    : constant := PROT_NONE;
316    PROT_OFF   : constant := PROT_ALL;
317
318    function mprotect
319      (addr : System.Address;
320       len  : size_t;
321       prot : int) return int;
322    pragma Import (C, mprotect);
323
324    ---------------------------------------
325    -- Nonstandard Thread Initialization --
326    ---------------------------------------
327
328    procedure pthread_init;
329
330    -------------------------
331    -- POSIX.1c  Section 3 --
332    -------------------------
333
334    function sigwait (set : access sigset_t; sig : access Signal) return int;
335    pragma Import (C, sigwait, "sigwait");
336
337    function pthread_kill (thread : pthread_t; sig : Signal) return int;
338    pragma Import (C, pthread_kill, "pthread_kill");
339
340    function pthread_sigmask
341      (how  : int;
342       set  : access sigset_t;
343       oset : access sigset_t) return int;
344    pragma Import (C, pthread_sigmask, "pthread_sigmask");
345
346    --------------------------
347    -- POSIX.1c  Section 11 --
348    --------------------------
349
350    function pthread_mutexattr_init
351      (attr : access pthread_mutexattr_t) return int;
352    pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
353
354    function pthread_mutexattr_destroy
355      (attr : access pthread_mutexattr_t) return int;
356    pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
357
358    function pthread_mutex_init
359      (mutex : access pthread_mutex_t;
360       attr  : access pthread_mutexattr_t) return int;
361    pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
362
363    function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
364    pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
365
366    function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
367    pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
368
369    function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
370    pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
371
372    function pthread_condattr_init
373      (attr : access pthread_condattr_t) return int;
374    pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
375
376    function pthread_condattr_destroy
377      (attr : access pthread_condattr_t) return int;
378    pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
379
380    function pthread_cond_init
381      (cond : access pthread_cond_t;
382       attr : access pthread_condattr_t) return int;
383    pragma Import (C, pthread_cond_init, "pthread_cond_init");
384
385    function pthread_cond_destroy (cond : access pthread_cond_t) return int;
386    pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
387
388    function pthread_cond_signal (cond : access pthread_cond_t) return int;
389    pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
390
391    function pthread_cond_wait
392      (cond  : access pthread_cond_t;
393       mutex : access pthread_mutex_t) return int;
394    pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
395
396    function pthread_cond_timedwait
397      (cond    : access pthread_cond_t;
398       mutex   : access pthread_mutex_t;
399       abstime : access timespec) return int;
400    pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
401
402    Relative_Timed_Wait : constant Boolean := False;
403    --  pthread_cond_timedwait requires an absolute delay time
404
405    --------------------------
406    -- POSIX.1c  Section 13 --
407    --------------------------
408
409    PTHREAD_PRIO_NONE    : constant := 0;
410    PTHREAD_PRIO_INHERIT : constant := 1;
411    PTHREAD_PRIO_PROTECT : constant := 2;
412
413    function pthread_mutexattr_setprotocol
414      (attr     : access pthread_mutexattr_t;
415       protocol : int) return int;
416    pragma Import
417      (C, pthread_mutexattr_setprotocol, "pthread_mutexattr_setprotocol");
418
419    function pthread_mutexattr_setprioceiling
420      (attr     : access pthread_mutexattr_t;
421       prioceiling : int) return int;
422    pragma Import
423      (C, pthread_mutexattr_setprioceiling,
424       "pthread_mutexattr_setprioceiling");
425
426    type padding is array (int range <>) of Interfaces.C.char;
427
428    type struct_sched_param is record
429       sched_priority : int;  --  scheduling priority
430       opaque         : padding (1 .. 4);
431    end record;
432    pragma Convention (C, struct_sched_param);
433
434    function pthread_setschedparam
435      (thread : pthread_t;
436       policy : int;
437       param  : access struct_sched_param) return int;
438    pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
439
440    function pthread_attr_setscope
441      (attr            : access pthread_attr_t;
442       contentionscope : int) return int;
443    pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
444
445    function pthread_attr_setinheritsched
446      (attr            : access pthread_attr_t;
447       inheritsched : int) return int;
448    pragma Import
449      (C, pthread_attr_setinheritsched, "pthread_attr_setinheritsched");
450
451    function pthread_attr_setschedpolicy
452      (attr   : access pthread_attr_t;
453       policy : int) return int;
454    pragma Import (C, pthread_attr_setschedpolicy, "pthread_attr_setsched");
455
456    function sched_yield return int;
457
458    ---------------------------
459    -- P1003.1c - Section 16 --
460    ---------------------------
461
462    function pthread_attr_init (attributes : access pthread_attr_t) return int;
463    pragma Import (C, pthread_attr_init, "pthread_attr_init");
464
465    function pthread_attr_destroy
466      (attributes : access pthread_attr_t) return int;
467    pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
468
469    function pthread_attr_setdetachstate
470      (attr        : access pthread_attr_t;
471       detachstate : int) return int;
472    pragma Import
473      (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate");
474
475    function pthread_attr_setstacksize
476      (attr      : access pthread_attr_t;
477       stacksize : size_t) return int;
478    pragma Import
479      (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
480
481    function pthread_create
482      (thread        : access pthread_t;
483       attributes    : access pthread_attr_t;
484       start_routine : Thread_Body;
485       arg           : System.Address) return int;
486    pragma Import (C, pthread_create, "pthread_create");
487
488    procedure pthread_exit (status : System.Address);
489    pragma Import (C, pthread_exit, "pthread_exit");
490
491    function pthread_self return pthread_t;
492    pragma Import (C, pthread_self, "pthread_self");
493
494    --------------------------
495    -- POSIX.1c  Section 17 --
496    --------------------------
497
498    function pthread_setspecific
499      (key   : pthread_key_t;
500       value : System.Address) return int;
501    pragma Import (C, pthread_setspecific, "pthread_setspecific");
502
503    function pthread_getspecific (key : pthread_key_t) return System.Address;
504    pragma Import (C, pthread_getspecific, "pthread_getspecific");
505
506    type destructor_pointer is access procedure (arg : System.Address);
507    pragma Convention (C, destructor_pointer);
508
509    function pthread_key_create
510      (key        : access pthread_key_t;
511       destructor : destructor_pointer) return int;
512    pragma Import (C, pthread_key_create, "pthread_key_create");
513
514 private
515
516    type sigset_t is new unsigned;
517
518    type int32_t is new int;
519
520    type pid_t is new int32_t;
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 clockid_t is new int;
531    CLOCK_REALTIME : constant clockid_t := 0;
532
533    type struct_timeval is record
534       tv_sec  : time_t;
535       tv_usec : int32_t;
536    end record;
537    pragma Convention (C, struct_timeval);
538
539    --
540    --  Darwin specific signal implementation
541    --
542    type Pad_Type is array (1 .. 7) of unsigned_long;
543    type siginfo_t is record
544       si_signo  : int;               --  signal number
545       si_errno  : int;               --  errno association
546       si_code   : int;               --  signal code
547       si_pid    : int;               --  sending process
548       si_uid    : unsigned;          --  sender's ruid
549       si_status : int;               --  exit value
550       si_addr   : System.Address;    --  faulting instruction
551       si_value  : System.Address;    --  signal value
552       si_band   : long;              --  band event for SIGPOLL
553       pad       : Pad_Type;          --  RFU
554    end record;
555    pragma Convention (C, siginfo_t);
556
557    type mcontext_t is new System.Address;
558
559    type ucontext_t is record
560       uc_onstack  : int;
561       uc_sigmask  : sigset_t;         --  Signal Mask Used By This Context
562       uc_stack    : stack_t;          --  Stack Used By This Context
563       uc_link     : System.Address;   --  Pointer To Resuming Context
564       uc_mcsize   : size_t;           --  Size of The Machine Context
565       uc_mcontext : mcontext_t;       --  Machine Specific Context
566    end record;
567    pragma Convention (C, ucontext_t);
568
569    --
570    --  Darwin specific pthread implementation
571    --
572    type pthread_t is new System.Address;
573
574    type pthread_attr_t is record
575       sig    : long;
576       opaque : padding (1 .. System.OS_Constants.PTHREAD_ATTR_SIZE);
577    end record;
578    pragma Convention (C, pthread_attr_t);
579
580    type pthread_mutexattr_t is record
581       sig    : long;
582       opaque : padding (1 .. System.OS_Constants.PTHREAD_MUTEXATTR_SIZE);
583    end record;
584    pragma Convention (C, pthread_mutexattr_t);
585
586    type pthread_mutex_t is record
587       sig    : long;
588       opaque : padding (1 .. System.OS_Constants.PTHREAD_MUTEX_SIZE);
589    end record;
590    pragma Convention (C, pthread_mutex_t);
591
592    type pthread_condattr_t is record
593       sig    : long;
594       opaque : padding (1 .. System.OS_Constants.PTHREAD_CONDATTR_SIZE);
595    end record;
596    pragma Convention (C, pthread_condattr_t);
597
598    type pthread_cond_t is record
599       sig    : long;
600       opaque : padding (1 .. System.OS_Constants.PTHREAD_COND_SIZE);
601    end record;
602    pragma Convention (C, pthread_cond_t);
603
604    type pthread_once_t is record
605       sig    : long;
606       opaque : padding (1 .. System.OS_Constants.PTHREAD_ONCE_SIZE);
607    end record;
608    pragma Convention (C, pthread_once_t);
609
610    type pthread_key_t is new unsigned_long;
611
612 end System.OS_Interface;