OSDN Git Service

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