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) 2002-2003 Ada Core Technologies, 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 LynxOS (POSIX Threads) 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 ("-mthreads");
48   --  Selects the POSIX 1.c runtime, rather than the non-threading runtime
49   --  or the deprecated legacy threads library. The -mthreads flag is
50   --  defined in patch.LynxOS and matches the definition for Lynx's gcc.
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 Import (C, errno, "__get_errno");
68
69   EAGAIN    : constant := 11;
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 := 63;
80
81   --  Max_Interrupt is the number of OS signals, as defined in:
82   --
83   --   /usr/include/sys/signal.h
84   --
85   --
86   --  The lowest numbered signal is 1, but 0 is a valid argument to some
87   --  library functions, eg. kill(2). However, 0 is not just another
88   --  signal: For instance 'I in Signal' and similar should be used with
89   --  caution.
90
91   type Signal is new int range 0 .. Max_Interrupt;
92   for  Signal'Size use int'Size;
93
94   SIGHUP        : constant := 1;  --  hangup
95   SIGINT        : constant := 2;  --  interrupt (rubout)
96   SIGQUIT       : constant := 3;  --  quit (ASCD FS)
97   SIGILL        : constant := 4;  --  illegal instruction (not reset)
98   SIGTRAP       : constant := 5;  --  trace trap (not reset)
99   SIGBRK        : constant := 6;  --  break
100   SIGIOT        : constant := 6;  --  IOT instruction
101   SIGABRT       : constant := 6;  --  used by abort, replace SIGIOT in future
102   SIGCORE       : constant := 7;  --  kill with core dump
103   SIGEMT        : constant := 7;  --  EMT instruction
104   SIGFPE        : constant := 8;  --  floating point exception
105   SIGKILL       : constant := 9;  --  kill (cannot be caught or ignored)
106   SIGBUS        : constant := 10; --  bus error
107   SIGSEGV       : constant := 11; --  segmentation violation
108   SIGSYS        : constant := 12; --  bad argument to system call
109   SIGPIPE       : constant := 13; --  write on a pipe with no one to read it
110   SIGALRM       : constant := 14; --  alarm clock
111   SIGTERM       : constant := 15; --  software termination signal from kill
112   SIGURG        : constant := 16; --  urgent condition on IO channel
113   SIGSTOP       : constant := 17; --  stop (cannot be caught or ignored)
114   SIGTSTP       : constant := 18; --  user stop requested from tty
115   SIGCONT       : constant := 19; --  stopped process has been continued
116   SIGCLD        : constant := 20; --  alias for SIGCHLD
117   SIGCHLD       : constant := 20; --  child status change
118   SIGTTIN       : constant := 21; --  background tty read attempted
119   SIGTTOU       : constant := 22; --  background tty write attempted
120   SIGIO         : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
121   SIGPOLL       : constant := 23; --  pollable event occurred
122   SIGTHREADKILL : constant := 24; --  Reserved by LynxOS runtime
123   SIGXCPU       : constant := 24; --  CPU time limit exceeded
124   SIGXFSZ       : constant := 25; --  filesize limit exceeded
125   SIGVTALRM     : constant := 26; --  virtual timer expired
126   SIGPROF       : constant := 27; --  profiling timer expired
127   SIGWINCH      : constant := 28; --  window size change
128   SIGLOST       : constant := 29; --  SUN 4.1 compatibility
129   SIGUSR1       : constant := 30; --  user defined signal 1
130   SIGUSR2       : constant := 31; --  user defined signal 2
131
132   SIGPRIO       : constant := 32;
133   --  sent to a process with its priority or group is changed
134
135   SIGADAABORT : constant := SIGABRT;
136   --  Change this if you want to use another signal for task abort.
137   --  SIGTERM might be a good one.
138
139   type Signal_Set is array (Natural range <>) of Signal;
140
141   Unmasked    : constant Signal_Set :=
142     (SIGTRAP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF, SIGTHREADKILL);
143   Reserved    : constant Signal_Set := (SIGABRT, SIGKILL, SIGSTOP, SIGPRIO);
144
145   type sigset_t is private;
146
147   function sigaddset (set : access sigset_t; sig : Signal) return int;
148   pragma Import (C, sigaddset, "sigaddset");
149
150   function sigdelset (set : access sigset_t; sig : Signal) return int;
151   pragma Import (C, sigdelset, "sigdelset");
152
153   function sigfillset (set : access sigset_t) return int;
154   pragma Import (C, sigfillset, "sigfillset");
155
156   function sigismember (set : access sigset_t; sig : Signal) return int;
157   pragma Import (C, sigismember, "sigismember");
158
159   function sigemptyset (set : access sigset_t) return int;
160   pragma Import (C, sigemptyset, "sigemptyset");
161
162   type struct_sigaction is record
163      sa_handler   : System.Address;
164      sa_mask      : sigset_t;
165      sa_flags     : int;
166   end record;
167   pragma Convention (C, struct_sigaction);
168   type struct_sigaction_ptr is access all struct_sigaction;
169
170   SA_SIGINFO  : constant := 16#80#;
171
172   SIG_BLOCK   : constant := 0;
173   SIG_UNBLOCK : constant := 1;
174   SIG_SETMASK : constant := 2;
175
176   SIG_DFL : constant := 0;
177   SIG_IGN : constant := 1;
178
179   function sigaction
180     (sig  : Signal;
181      act  : struct_sigaction_ptr;
182      oact : struct_sigaction_ptr) return int;
183   pragma Import (C, sigaction, "sigaction");
184
185   ----------
186   -- Time --
187   ----------
188
189   Time_Slice_Supported : constant Boolean := True;
190   --  Indicates whether time slicing is supported
191
192   type timespec is private;
193
194   type clockid_t is private;
195
196   CLOCK_REALTIME : constant clockid_t;
197
198   function clock_gettime
199     (clock_id : clockid_t;
200      tp       : access timespec) return int;
201   pragma Import (C, clock_gettime, "clock_gettime");
202
203   function clock_getres
204     (clock_id : clockid_t;
205      res      : access timespec) return int;
206   pragma Import (C, clock_getres, "clock_getres");
207
208   function To_Duration (TS : timespec) return Duration;
209   pragma Inline (To_Duration);
210
211   function To_Timespec (D : Duration) return timespec;
212   pragma Inline (To_Timespec);
213
214   type struct_timezone is record
215      tz_minuteswest : int;
216      tz_dsttime     : int;
217   end record;
218   pragma Convention (C, struct_timezone);
219   type struct_timezone_ptr is access all struct_timezone;
220
221   type struct_timeval is private;
222   --  This is needed on systems that do not have clock_gettime()
223   --  but do have gettimeofday().
224
225   function To_Duration (TV : struct_timeval) return Duration;
226   pragma Inline (To_Duration);
227
228   function To_Timeval (D : Duration) return struct_timeval;
229   pragma Inline (To_Timeval);
230
231   -------------------------
232   -- Priority Scheduling --
233   -------------------------
234
235   SCHED_FIFO  : constant := 16#200000#;
236   SCHED_RR    : constant := 16#100000#;
237   SCHED_OTHER : constant := 16#400000#;
238
239   -------------
240   -- Process --
241   -------------
242
243   type pid_t is private;
244
245   function kill (pid : pid_t; sig : Signal) return int;
246   pragma Import (C, kill, "kill");
247
248   function getpid return pid_t;
249   pragma Import (C, getpid, "getpid");
250
251   ---------
252   -- LWP --
253   ---------
254
255   function lwp_self return System.Address;
256   pragma Import (C, lwp_self, "pthread_self");
257
258   -------------
259   -- Threads --
260   -------------
261
262   type Thread_Body is access
263     function (arg : System.Address) return System.Address;
264   type pthread_t           is private;
265   subtype Thread_Id        is pthread_t;
266
267   type pthread_mutex_t     is limited private;
268   type pthread_cond_t      is limited private;
269   type pthread_attr_t      is limited private;
270   type pthread_mutexattr_t is limited private;
271   type pthread_condattr_t  is limited private;
272   type pthread_key_t       is private;
273
274   PTHREAD_CREATE_DETACHED : constant := 1;
275   PTHREAD_CREATE_JOINABLE : constant := 0;
276
277   -----------
278   -- Stack --
279   -----------
280
281   Stack_Base_Available : constant Boolean := False;
282   --  Indicates whether the stack base is available on this target.
283
284   function Get_Stack_Base (thread : pthread_t) return Address;
285   pragma Inline (Get_Stack_Base);
286   --  Returns the stack base of the specified thread.
287   --  Only call this function when Stack_Base_Available is True.
288
289   function Get_Page_Size return size_t;
290   function Get_Page_Size return Address;
291   pragma Import (C, Get_Page_Size, "getpagesize");
292   --  Returns the size of a page, or 0 if this is not relevant on this
293   --  target
294
295   PROT_NONE  : constant := 1;
296   PROT_READ  : constant := 2;
297   PROT_WRITE : constant := 4;
298   PROT_EXEC  : constant := 8;
299   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
300
301   PROT_ON    : constant := PROT_READ;
302   PROT_OFF   : constant := PROT_ALL;
303
304   function mprotect (addr : Address; len : size_t; prot : int) return int;
305   pragma Import (C, mprotect);
306
307   -----------------------------------------
308   --  Nonstandard Thread Initialization  --
309   -----------------------------------------
310
311   procedure pthread_init;
312   --  This is a dummy procedure to share some GNULLI files
313
314   ---------------------------
315   --  POSIX.1c  Section 3  --
316   ---------------------------
317
318   function sigwait
319     (set : access sigset_t;
320      sig : access Signal) return int;
321   pragma Inline (sigwait);
322   --  LynxOS has non standard sigwait
323
324   function pthread_kill
325     (thread : pthread_t;
326      sig    : Signal) return int;
327   pragma Import (C, pthread_kill, "pthread_kill");
328
329   type sigset_t_ptr is access all sigset_t;
330
331   function pthread_sigmask
332     (how  : int;
333      set  : sigset_t_ptr;
334      oset : sigset_t_ptr) return int;
335   pragma Import (C, pthread_sigmask, "pthread_sigmask");
336   --  The behavior of pthread_sigmask on LynxOS requires
337   --  further investigation.
338
339   ----------------------------
340   --  POSIX.1c  Section 11  --
341   ----------------------------
342
343   function pthread_mutexattr_init
344     (attr : access pthread_mutexattr_t) return int;
345   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
346
347   function pthread_mutexattr_destroy
348     (attr : access pthread_mutexattr_t) return int;
349   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
350
351   function pthread_mutex_init
352     (mutex : access pthread_mutex_t;
353      attr  : access pthread_mutexattr_t) return int;
354   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
355
356   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
357   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
358
359   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
360   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
361
362   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
363   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
364
365   function pthread_condattr_init
366     (attr : access pthread_condattr_t) return int;
367   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
368
369   function pthread_condattr_destroy
370     (attr : access pthread_condattr_t) return int;
371   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
372
373   function pthread_cond_init
374     (cond : access pthread_cond_t;
375      attr : access pthread_condattr_t) return int;
376   pragma Import (C, pthread_cond_init, "pthread_cond_init");
377
378   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
379   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
380
381   function pthread_cond_signal (cond : access pthread_cond_t) return int;
382   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
383
384   function pthread_cond_wait
385     (cond  : access pthread_cond_t;
386      mutex : access pthread_mutex_t) return int;
387   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
388
389   function pthread_cond_timedwait
390     (cond    : access pthread_cond_t;
391      mutex   : access pthread_mutex_t;
392      abstime : access timespec) return int;
393   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
394
395   Relative_Timed_Wait : constant Boolean := False;
396   --  pthread_cond_timedwait requires an absolute delay time
397
398   --------------------------
399   -- POSIX.1c  Section 13 --
400   --------------------------
401
402   PTHREAD_PRIO_NONE    : constant := 0;
403   PTHREAD_PRIO_INHERIT : constant := 1;
404   PTHREAD_PRIO_PROTECT : constant := 2;
405
406   function pthread_mutexattr_setprotocol
407     (attr     : access pthread_mutexattr_t;
408      protocol : int) return int;
409   pragma Import (C, pthread_mutexattr_setprotocol);
410
411   function pthread_mutexattr_setprioceiling
412     (attr        : access pthread_mutexattr_t;
413      prioceiling : int) return int;
414   pragma Import (C, pthread_mutexattr_setprioceiling);
415
416   type struct_sched_param is record
417      sched_priority : int;
418   end record;
419
420   function pthread_setschedparam
421     (thread : pthread_t;
422      policy : int;
423      param  : access struct_sched_param) return int;
424   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
425
426   function pthread_attr_setscope
427     (attr            : access pthread_attr_t;
428      contentionscope : int) return int;
429   pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
430
431   function pthread_attr_setinheritsched
432     (attr         : access pthread_attr_t;
433      inheritsched : int) return int;
434   pragma Import (C, pthread_attr_setinheritsched);
435
436   function pthread_attr_setschedpolicy
437     (attr   : access pthread_attr_t;
438      policy : int) return int;
439   pragma Import (C, pthread_attr_setschedpolicy);
440
441   function sched_yield return int;
442   pragma Import (C, sched_yield, "sched_yield");
443
444   ---------------------------
445   -- P1003.1c - Section 16 --
446   ---------------------------
447
448   function pthread_attr_init (attributes : access pthread_attr_t) return int;
449   pragma Import (C, pthread_attr_init, "pthread_attr_init");
450
451   function pthread_attr_destroy
452     (attributes : access pthread_attr_t) return int;
453   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
454
455   function pthread_attr_setdetachstate
456     (attr        : access pthread_attr_t;
457      detachstate : int) return int;
458   pragma Import (C, pthread_attr_setdetachstate);
459
460   function pthread_attr_setstacksize
461     (attr      : access pthread_attr_t;
462      stacksize : size_t) return int;
463   pragma Import (C, pthread_attr_setstacksize);
464
465   function pthread_create
466     (thread        : access pthread_t;
467      attributes    : access pthread_attr_t;
468      start_routine : Thread_Body;
469      arg           : System.Address) return int;
470   pragma Import (C, pthread_create, "pthread_create");
471
472   procedure pthread_exit (status : System.Address);
473   pragma Import (C, pthread_exit, "pthread_exit");
474
475   function pthread_self return pthread_t;
476   pragma Import (C, pthread_self, "pthread_self");
477
478   --------------------------
479   -- POSIX.1c  Section 17 --
480   --------------------------
481
482   function st_setspecific
483     (key   : pthread_key_t;
484      value : System.Address) return int;
485   pragma Import (C, st_setspecific, "st_setspecific");
486
487   function st_getspecific
488     (key    : pthread_key_t;
489      retval : System.Address) return int;
490   pragma Import (C, st_getspecific, "st_getspecific");
491
492   type destructor_pointer is access procedure (arg : System.Address);
493
494   function st_keycreate
495     (destructor : destructor_pointer;
496      key        : access pthread_key_t) return int;
497   pragma Import (C, st_keycreate, "st_keycreate");
498
499private
500
501   type sigset_t is record
502      X1, X2 : long;
503   end record;
504   pragma Convention (C, sigset_t);
505
506   type pid_t is new long;
507
508   type time_t is new long;
509
510   type timespec is record
511      tv_sec  : time_t;
512      tv_nsec : long;
513   end record;
514   pragma Convention (C, timespec);
515
516   type clockid_t is new unsigned_char;
517   CLOCK_REALTIME : constant clockid_t := 0;
518
519   type struct_timeval is record
520      tv_sec  : time_t;
521      tv_usec : time_t;
522   end record;
523   pragma Convention (C, struct_timeval);
524
525   type st_attr_t is record
526      stksize      : int;
527      prio         : int;
528      inheritsched : int;
529      state        : int;
530      sched        : int;
531      detachstate  : int;
532      guardsize    : int;
533   end record;
534   pragma Convention (C, st_attr_t);
535
536   type pthread_attr_t is record
537      pthread_attr_magic : unsigned;
538      st                 : st_attr_t;
539      pthread_attr_scope : int;
540   end record;
541   pragma Convention (C, pthread_attr_t);
542
543   type pthread_condattr_t is record
544      cv_magic   : unsigned;
545      cv_pshared : unsigned;
546   end record;
547   pragma Convention (C, pthread_condattr_t);
548
549   type pthread_mutexattr_t is record
550      m_flags   : unsigned;
551      m_prio_c  : int;
552      m_pshared : int;
553   end record;
554   pragma Convention (C, pthread_mutexattr_t);
555
556   type tid_t is new short;
557   type pthread_t is new tid_t;
558
559   type block_obj_t is new System.Address;
560   --  typedef struct _block_obj_s {
561   --     struct st_entry *b_head;
562   --  } block_obj_t;
563
564   type pthread_mutex_t is record
565      m_flags      : unsigned;
566      m_owner      : tid_t;
567      m_wait       : block_obj_t;
568      m_prio_c     : int;
569      m_oldprio    : int;
570      m_count      : int;
571      m_referenced : int;
572   end record;
573   pragma Convention (C, pthread_mutex_t);
574   type pthread_mutex_t_ptr is access all pthread_mutex_t;
575
576   type pthread_cond_t is record
577      cv_magic  : unsigned;
578      cv_wait   : block_obj_t;
579      cv_mutex  : pthread_mutex_t_ptr;
580      cv_refcnt : int;
581   end record;
582   pragma Convention (C, pthread_cond_t);
583
584   type pthread_key_t is new int;
585
586end System.OS_Interface;
587