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