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-2011, Free Software Foundation, Inc.         --
11--                                                                          --
12-- GNAT is free software;  you can  redistribute it  and/or modify it under --
13-- terms of the  GNU General Public License as published  by the Free Soft- --
14-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
15-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
18--                                                                          --
19-- As a special exception under Section 7 of GPL version 3, you are granted --
20-- additional permissions described in the GCC Runtime Library Exception,   --
21-- version 3.1, as published by the Free Software Foundation.               --
22--                                                                          --
23-- You should have received a copy of the GNU General Public License and    --
24-- a copy of the GCC Runtime Library Exception along with this program;     --
25-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
26-- <http://www.gnu.org/licenses/>.                                          --
27--                                                                          --
28-- GNARL was developed by the GNARL team at Florida State University.       --
29-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
30--                                                                          --
31------------------------------------------------------------------------------
32
33--  This is a AIX (Native THREADS) version of this package
34
35--  This package encapsulates all direct interfaces to OS services that are
36--  needed by the tasking run-time (libgnarl).
37
38--  PLEASE DO NOT add any with-clauses to this package or remove the pragma
39--  Preelaborate. This package is designed to be a bottom-level (leaf) package.
40
41with Ada.Unchecked_Conversion;
42
43with Interfaces.C;
44
45package System.OS_Interface is
46   pragma Preelaborate;
47
48   pragma Linker_Options ("-pthread");
49   --  This implies -lpthreads + other things depending on the GCC
50   --  configuration, such as the selection of a proper libgcc variant
51   --  for table-based exception handling when it is available.
52
53   pragma Linker_Options ("-lc_r");
54
55   subtype int            is Interfaces.C.int;
56   subtype short          is Interfaces.C.short;
57   subtype long           is Interfaces.C.long;
58   subtype unsigned       is Interfaces.C.unsigned;
59   subtype unsigned_short is Interfaces.C.unsigned_short;
60   subtype unsigned_long  is Interfaces.C.unsigned_long;
61   subtype unsigned_char  is Interfaces.C.unsigned_char;
62   subtype plain_char     is Interfaces.C.plain_char;
63   subtype size_t         is Interfaces.C.size_t;
64
65   -----------
66   -- Errno --
67   -----------
68
69   function errno return int;
70   pragma Import (C, errno, "__get_errno");
71
72   EAGAIN    : constant := 11;
73   EINTR     : constant := 4;
74   EINVAL    : constant := 22;
75   ENOMEM    : constant := 12;
76   ETIMEDOUT : constant := 78;
77
78   -------------
79   -- Signals --
80   -------------
81
82   Max_Interrupt : constant := 63;
83   type Signal is new int range 0 .. Max_Interrupt;
84   for Signal'Size use int'Size;
85
86   SIGHUP      : constant := 1; --  hangup
87   SIGINT      : constant := 2; --  interrupt (rubout)
88   SIGQUIT     : constant := 3; --  quit (ASCD FS)
89   SIGILL      : constant := 4; --  illegal instruction (not reset)
90   SIGTRAP     : constant := 5; --  trace trap (not reset)
91   SIGIOT      : constant := 6; --  IOT instruction
92   SIGABRT     : constant := 6; --  used by abort, replace SIGIOT in the future
93   SIGEMT      : constant := 7; --  EMT instruction
94   SIGFPE      : constant := 8; --  floating point exception
95   SIGKILL     : constant := 9; --  kill (cannot be caught or ignored)
96   SIGBUS      : constant := 10; --  bus error
97   SIGSEGV     : constant := 11; --  segmentation violation
98   SIGSYS      : constant := 12; --  bad argument to system call
99   SIGPIPE     : constant := 13; --  write on a pipe with no one to read it
100   SIGALRM     : constant := 14; --  alarm clock
101   SIGTERM     : constant := 15; --  software termination signal from kill
102   SIGUSR1     : constant := 30; --  user defined signal 1
103   SIGUSR2     : constant := 31; --  user defined signal 2
104   SIGCLD      : constant := 20; --  alias for SIGCHLD
105   SIGCHLD     : constant := 20; --  child status change
106   SIGPWR      : constant := 29; --  power-fail restart
107   SIGWINCH    : constant := 28; --  window size change
108   SIGURG      : constant := 16; --  urgent condition on IO channel
109   SIGPOLL     : constant := 23; --  pollable event occurred
110   SIGIO       : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
111   SIGSTOP     : constant := 17; --  stop (cannot be caught or ignored)
112   SIGTSTP     : constant := 18; --  user stop requested from tty
113   SIGCONT     : constant := 19; --  stopped process has been continued
114   SIGTTIN     : constant := 21; --  background tty read attempted
115   SIGTTOU     : constant := 22; --  background tty write attempted
116   SIGVTALRM   : constant := 34; --  virtual timer expired
117   SIGPROF     : constant := 32; --  profiling timer expired
118   SIGXCPU     : constant := 24; --  CPU time limit exceeded
119   SIGXFSZ     : constant := 25; --  filesize limit exceeded
120   SIGWAITING  : constant := 39; --  m:n scheduling
121
122   --  The following signals are AIX specific
123
124   SIGMSG      : constant := 27; -- input data is in the ring buffer
125   SIGDANGER   : constant := 33; -- system crash imminent
126   SIGMIGRATE  : constant := 35; -- migrate process
127   SIGPRE      : constant := 36; -- programming exception
128   SIGVIRT     : constant := 37; -- AIX virtual time alarm
129   SIGALRM1    : constant := 38; -- m:n condition variables
130   SIGCPUFAIL  : constant := 59; -- Predictive De-configuration of Processors
131   SIGKAP      : constant := 60; -- keep alive poll from native keyboard
132   SIGGRANT    : constant := SIGKAP; -- monitor mode granted
133   SIGRETRACT  : constant := 61; -- monitor mode should be relinquished
134   SIGSOUND    : constant := 62; -- sound control has completed
135   SIGSAK      : constant := 63; -- secure attention key
136
137   SIGADAABORT : constant := SIGEMT;
138   --  Note: on other targets, we usually use SIGABRT, but on AIX, it appears
139   --  that SIGABRT can't be used in sigwait(), so we use SIGEMT.
140   --  SIGEMT is "Emulator Trap Instruction" from the PDP-11, and does not
141   --  have a standardized usage.
142
143   type Signal_Set is array (Natural range <>) of Signal;
144
145   Unmasked : constant Signal_Set :=
146                (SIGTRAP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF);
147   Reserved : constant Signal_Set :=
148                (SIGABRT, SIGKILL, SIGSTOP, SIGALRM1, SIGWAITING, SIGCPUFAIL);
149
150   type sigset_t is private;
151
152   function sigaddset (set : access sigset_t; sig : Signal) return int;
153   pragma Import (C, sigaddset, "sigaddset");
154
155   function sigdelset (set : access sigset_t; sig : Signal) return int;
156   pragma Import (C, sigdelset, "sigdelset");
157
158   function sigfillset (set : access sigset_t) return int;
159   pragma Import (C, sigfillset, "sigfillset");
160
161   function sigismember (set : access sigset_t; sig : Signal) return int;
162   pragma Import (C, sigismember, "sigismember");
163
164   function sigemptyset (set : access sigset_t) return int;
165   pragma Import (C, sigemptyset, "sigemptyset");
166
167   type struct_sigaction is record
168      sa_handler : System.Address;
169      sa_mask    : sigset_t;
170      sa_flags   : int;
171   end record;
172   pragma Convention (C, struct_sigaction);
173   type struct_sigaction_ptr is access all struct_sigaction;
174
175   SA_SIGINFO : constant := 16#0100#;
176   SA_ONSTACK : constant := 16#0001#;
177
178   SIG_BLOCK   : constant := 0;
179   SIG_UNBLOCK : constant := 1;
180   SIG_SETMASK : constant := 2;
181
182   SIG_DFL : constant := 0;
183   SIG_IGN : constant := 1;
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 whether time slicing is supported
197
198   type timespec is private;
199
200   type clockid_t is new int;
201
202   function clock_gettime
203     (clock_id : clockid_t;
204      tp       : access timespec) return int;
205
206   function To_Duration (TS : timespec) return Duration;
207   pragma Inline (To_Duration);
208
209   function To_Timespec (D : Duration) return timespec;
210   pragma Inline (To_Timespec);
211
212   type struct_timezone is record
213      tz_minuteswest : int;
214      tz_dsttime     : int;
215   end record;
216   pragma Convention (C, struct_timezone);
217   type struct_timezone_ptr is access all struct_timezone;
218
219   -------------------------
220   -- Priority Scheduling --
221   -------------------------
222
223   SCHED_FIFO  : constant := 1;
224   SCHED_RR    : constant := 2;
225   SCHED_OTHER : constant := 0;
226
227   function To_Target_Priority
228     (Prio : System.Any_Priority) return Interfaces.C.int;
229   --  Maps System.Any_Priority to a POSIX priority
230
231   -------------
232   -- Process --
233   -------------
234
235   type pid_t is private;
236
237   function kill (pid : pid_t; sig : Signal) return int;
238   pragma Import (C, kill, "kill");
239
240   function getpid return pid_t;
241   pragma Import (C, getpid, "getpid");
242
243   ---------
244   -- LWP --
245   ---------
246
247   function lwp_self return System.Address;
248   pragma Import (C, lwp_self, "thread_self");
249
250   -------------
251   -- Threads --
252   -------------
253
254   type Thread_Body is access
255     function (arg : System.Address) return System.Address;
256   pragma Convention (C, Thread_Body);
257
258   function Thread_Body_Access is new
259     Ada.Unchecked_Conversion (System.Address, Thread_Body);
260
261   type pthread_t           is private;
262   subtype Thread_Id        is pthread_t;
263
264   type pthread_mutex_t     is limited private;
265   type pthread_cond_t      is limited private;
266   type pthread_attr_t      is limited private;
267   type pthread_mutexattr_t is limited private;
268   type pthread_condattr_t  is limited private;
269   type pthread_key_t       is private;
270
271   PTHREAD_CREATE_DETACHED : constant := 1;
272
273   PTHREAD_SCOPE_PROCESS : constant := 1;
274   PTHREAD_SCOPE_SYSTEM  : constant := 0;
275
276   --  Read/Write lock not supported on AIX. To add support both types
277   --  pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
278   --  with the associated routines pthread_rwlock_[init/destroy] and
279   --  pthread_rwlock_[rdlock/wrlock/unlock].
280
281   subtype pthread_rwlock_t     is pthread_mutex_t;
282   subtype pthread_rwlockattr_t is pthread_mutexattr_t;
283
284   -----------
285   -- Stack --
286   -----------
287
288   type stack_t is record
289      ss_sp    : System.Address;
290      ss_size  : size_t;
291      ss_flags : int;
292   end record;
293   pragma Convention (C, stack_t);
294
295   function sigaltstack
296     (ss  : not null access stack_t;
297      oss : access stack_t) return int;
298   pragma Import (C, sigaltstack, "sigaltstack");
299
300   Alternate_Stack : aliased System.Address;
301   --  This is a dummy definition, never used (Alternate_Stack_Size is null)
302
303   Alternate_Stack_Size : constant := 0;
304   --  No alternate signal stack is used on this platform
305
306   Stack_Base_Available : constant Boolean := False;
307   --  Indicates whether the stack base is available on this target
308
309   function Get_Stack_Base (thread : pthread_t) return Address;
310   pragma Inline (Get_Stack_Base);
311   --  Returns the stack base of the specified thread. Only call this function
312   --  when Stack_Base_Available is True.
313
314   function Get_Page_Size return size_t;
315   function Get_Page_Size return Address;
316   pragma Import (C, Get_Page_Size, "getpagesize");
317   --  Returns the size of a page
318
319   PROT_NONE  : constant := 0;
320   PROT_READ  : constant := 1;
321   PROT_WRITE : constant := 2;
322   PROT_EXEC  : constant := 4;
323   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
324   PROT_ON    : constant := PROT_READ;
325   PROT_OFF   : constant := PROT_ALL;
326
327   function mprotect (addr : Address; len : size_t; prot : int) return int;
328   pragma Import (C, mprotect);
329
330   ---------------------------------------
331   -- Nonstandard Thread Initialization --
332   ---------------------------------------
333
334   --  Though not documented, pthread_init *must* be called before any other
335   --  pthread call.
336
337   procedure pthread_init;
338   pragma Import (C, pthread_init, "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   function pthread_sigmask
355     (how  : int;
356      set  : access sigset_t;
357      oset : access sigset_t) return int;
358   pragma Import (C, pthread_sigmask, "sigthreadmask");
359
360   --------------------------
361   -- POSIX.1c  Section 11 --
362   --------------------------
363
364   function pthread_mutexattr_init
365     (attr : access pthread_mutexattr_t) return int;
366   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
367
368   function pthread_mutexattr_destroy
369     (attr : access pthread_mutexattr_t) return int;
370   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
371
372   function pthread_mutex_init
373     (mutex : access pthread_mutex_t;
374      attr  : access pthread_mutexattr_t) return int;
375   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
376
377   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
378   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
379
380   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
381   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
382
383   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
384   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
385
386   function pthread_condattr_init
387     (attr : access pthread_condattr_t) return int;
388   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
389
390   function pthread_condattr_destroy
391     (attr : access pthread_condattr_t) return int;
392   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
393
394   function pthread_cond_init
395     (cond : access pthread_cond_t;
396      attr : access pthread_condattr_t) return int;
397   pragma Import (C, pthread_cond_init, "pthread_cond_init");
398
399   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
400   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
401
402   function pthread_cond_signal (cond : access pthread_cond_t) return int;
403   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
404
405   function pthread_cond_wait
406     (cond  : access pthread_cond_t;
407      mutex : access pthread_mutex_t) return int;
408   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
409
410   function pthread_cond_timedwait
411     (cond    : access pthread_cond_t;
412      mutex   : access pthread_mutex_t;
413      abstime : access timespec) return int;
414   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
415
416   Relative_Timed_Wait : constant Boolean := False;
417   --  pthread_cond_timedwait requires an absolute delay time
418
419   --------------------------
420   -- POSIX.1c  Section 13 --
421   --------------------------
422
423   PTHREAD_PRIO_PROTECT : constant := 2;
424
425   function PTHREAD_PRIO_INHERIT return int;
426   --  Return value of C macro PTHREAD_PRIO_INHERIT. This function is needed
427   --  since the value is different between AIX versions.
428
429   function pthread_mutexattr_setprotocol
430     (attr     : access pthread_mutexattr_t;
431      protocol : int) return int;
432   pragma Import (C, pthread_mutexattr_setprotocol);
433
434   function pthread_mutexattr_setprioceiling
435     (attr        : access pthread_mutexattr_t;
436      prioceiling : int) return int;
437   pragma Import (C, pthread_mutexattr_setprioceiling);
438
439   type Array_5_Int is array (0 .. 5) of int;
440   type struct_sched_param is record
441      sched_priority : int;
442      sched_policy   : int;
443      sched_reserved : Array_5_Int;
444   end record;
445
446   function pthread_setschedparam
447     (thread : pthread_t;
448      policy : int;
449      param  : access struct_sched_param) return int;
450   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
451
452   function pthread_attr_setscope
453     (attr            : access pthread_attr_t;
454      contentionscope : int) return int;
455   pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
456
457   function pthread_attr_setinheritsched
458     (attr            : access pthread_attr_t;
459      inheritsched : int) return int;
460   pragma Import (C, pthread_attr_setinheritsched);
461
462   function pthread_attr_setschedpolicy
463     (attr   : access pthread_attr_t;
464      policy : int) return int;
465   pragma Import (C, pthread_attr_setschedpolicy);
466
467   function pthread_attr_setschedparam
468     (attr        : access pthread_attr_t;
469      sched_param : int) return int;
470   pragma Import (C, pthread_attr_setschedparam);
471
472   function sched_yield return int;
473   --  AIX have a nonstandard sched_yield
474
475   --------------------------
476   -- P1003.1c  Section 16 --
477   --------------------------
478
479   function pthread_attr_init (attributes : access pthread_attr_t) return int;
480   pragma Import (C, pthread_attr_init, "pthread_attr_init");
481
482   function pthread_attr_destroy
483     (attributes : access pthread_attr_t) return int;
484   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
485
486   function pthread_attr_setdetachstate
487     (attr        : access pthread_attr_t;
488      detachstate : int) return int;
489   pragma Import (C, pthread_attr_setdetachstate);
490
491   function pthread_attr_setstacksize
492     (attr      : access pthread_attr_t;
493      stacksize : size_t) return int;
494   pragma Import (C, pthread_attr_setstacksize);
495
496   function pthread_create
497     (thread        : access pthread_t;
498      attributes    : access pthread_attr_t;
499      start_routine : Thread_Body;
500      arg           : System.Address)
501     return int;
502   pragma Import (C, pthread_create, "pthread_create");
503
504   procedure pthread_exit (status : System.Address);
505   pragma Import (C, pthread_exit, "pthread_exit");
506
507   function pthread_self return pthread_t;
508   pragma Import (C, pthread_self, "pthread_self");
509
510   --------------------------
511   -- POSIX.1c  Section 17 --
512   --------------------------
513
514   function pthread_setspecific
515     (key   : pthread_key_t;
516      value : System.Address) return int;
517   pragma Import (C, pthread_setspecific, "pthread_setspecific");
518
519   function pthread_getspecific (key : pthread_key_t) return System.Address;
520   pragma Import (C, pthread_getspecific, "pthread_getspecific");
521
522   type destructor_pointer is access procedure (arg : System.Address);
523   pragma Convention (C, destructor_pointer);
524
525   function pthread_key_create
526     (key        : access pthread_key_t;
527      destructor : destructor_pointer) return int;
528   pragma Import (C, pthread_key_create, "pthread_key_create");
529
530private
531   type sigset_t is record
532      losigs : unsigned_long;
533      hisigs : unsigned_long;
534   end record;
535   pragma Convention (C_Pass_By_Copy, sigset_t);
536
537   type pid_t is new int;
538
539   type time_t is new long;
540
541   type timespec is record
542      tv_sec  : time_t;
543      tv_nsec : long;
544   end record;
545   pragma Convention (C, timespec);
546
547   type pthread_attr_t is new System.Address;
548   pragma Convention (C, pthread_attr_t);
549   --  typedef struct __pt_attr        *pthread_attr_t;
550
551   type pthread_condattr_t is new System.Address;
552   pragma Convention (C, pthread_condattr_t);
553   --  typedef struct __pt_attr        *pthread_condattr_t;
554
555   type pthread_mutexattr_t is new System.Address;
556   pragma Convention (C, pthread_mutexattr_t);
557   --  typedef struct __pt_attr        *pthread_mutexattr_t;
558
559   type pthread_t is new System.Address;
560   pragma Convention (C, pthread_t);
561   --  typedef void    *pthread_t;
562
563   type ptq_queue;
564   type ptq_queue_ptr is access all ptq_queue;
565
566   type ptq_queue is record
567      ptq_next : ptq_queue_ptr;
568      ptq_prev : ptq_queue_ptr;
569   end record;
570
571   type Array_3_Int is array (0 .. 3) of int;
572   type pthread_mutex_t is record
573        link        : ptq_queue;
574        ptmtx_lock  : int;
575        ptmtx_flags : long;
576        protocol    : int;
577        prioceiling : int;
578        ptmtx_owner : pthread_t;
579        mtx_id      : int;
580        attr        : pthread_attr_t;
581        mtx_kind    : int;
582        lock_cpt    : int;
583        reserved    : Array_3_Int;
584   end record;
585   pragma Convention (C, pthread_mutex_t);
586   type pthread_mutex_t_ptr is access pthread_mutex_t;
587
588   type pthread_cond_t is record
589      link         : ptq_queue;
590      ptcv_lock    : int;
591      ptcv_flags   : long;
592      ptcv_waiters : ptq_queue;
593      cv_id        : int;
594      attr         : pthread_attr_t;
595      mutex        : pthread_mutex_t_ptr;
596      cptwait      : int;
597      reserved     : int;
598   end record;
599   pragma Convention (C, pthread_cond_t);
600
601   type pthread_key_t is new unsigned;
602
603end System.OS_Interface;
604