1------------------------------------------------------------------------------
2--                                                                          --
3--                 GNU ADA RUNTIME 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-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. It is --
30-- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
31-- State University (http://www.gnat.com).                                  --
32--                                                                          --
33------------------------------------------------------------------------------
34
35--  This is the FreeBSD PTHREADS version of this package
36
37with Interfaces.C;
38package System.OS_Interface is
39   pragma Preelaborate;
40
41   pragma Linker_Options ("-pthread");
42
43   subtype int            is Interfaces.C.int;
44   subtype short          is Interfaces.C.short;
45   subtype long           is Interfaces.C.long;
46   subtype unsigned       is Interfaces.C.unsigned;
47   subtype unsigned_short is Interfaces.C.unsigned_short;
48   subtype unsigned_long  is Interfaces.C.unsigned_long;
49   subtype unsigned_char  is Interfaces.C.unsigned_char;
50   subtype plain_char     is Interfaces.C.plain_char;
51   subtype size_t         is Interfaces.C.size_t;
52
53   -----------
54   -- Errno --
55   -----------
56
57   function Errno return int;
58   pragma Inline (Errno);
59
60   EAGAIN   : constant := 35;
61   EINTR    : constant := 4;
62   EINVAL   : constant := 22;
63   ENOMEM   : constant := 12;
64   ETIMEDOUT    : constant := 60;
65
66   -------------
67   -- Signals --
68   -------------
69
70   Max_Interrupt : constant := 31;
71   type Signal is new int range 0 .. Max_Interrupt;
72   for Signal'Size use int'Size;
73
74   SIGHUP     : constant := 1; --  hangup
75   SIGINT     : constant := 2; --  interrupt (rubout)
76   SIGQUIT    : constant := 3; --  quit (ASCD FS)
77   SIGILL     : constant := 4; --  illegal instruction (not reset)
78   SIGTRAP    : constant := 5; --  trace trap (not reset)
79   SIGIOT     : constant := 6; --  IOT instruction
80   SIGABRT    : constant := 6; --  used by abort, replace SIGIOT in the  future
81   SIGEMT     : constant := 7; --  EMT instruction
82   SIGFPE     : constant := 8; --  floating point exception
83   SIGKILL    : constant := 9; --  kill (cannot be caught or ignored)
84   SIGBUS     : constant := 10; --  bus error
85   SIGSEGV    : constant := 11; --  segmentation violation
86   SIGSYS     : constant := 12; --  bad argument to system call
87   SIGPIPE    : constant := 13; --  write on a pipe with no one to read it
88   SIGALRM    : constant := 14; --  alarm clock
89   SIGTERM    : constant := 15; --  software termination signal from kill
90   SIGURG     : constant := 16; --  urgent condition on IO channel
91   SIGSTOP    : constant := 17; --  stop (cannot be caught or ignored)
92   SIGTSTP    : constant := 18; --  user stop requested from tty
93   SIGCONT    : constant := 19; --  stopped process has been continued
94   SIGCLD     : constant := 20; --  alias for SIGCHLD
95   SIGCHLD    : constant := 20; --  child status change
96   SIGTTIN    : constant := 21; --  background tty read attempted
97   SIGTTOU    : constant := 22; --  background tty write attempted
98   SIGIO      : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
99   SIGXCPU    : constant := 24; --  CPU time limit exceeded
100   SIGXFSZ    : constant := 25; --  filesize limit exceeded
101   SIGVTALRM  : constant := 26; --  virtual timer expired
102   SIGPROF    : constant := 27; --  profiling timer expired
103   SIGWINCH   : constant := 28; --  window size change
104   SIGINFO    : constant := 29; --  information request (NetBSD/FreeBSD)
105   SIGUSR1    : constant := 30; --  user defined signal 1
106   SIGUSR2    : constant := 31; --  user defined signal 2
107
108   SIGADAABORT : constant := SIGABRT;
109   --  Change this if you want to use another signal for task abort.
110   --  SIGTERM might be a good one.
111
112   type Signal_Set is array (Natural range <>) of Signal;
113
114   --  Interrupts that must be unmasked at all times.  FreeBSD
115   --  pthreads will not allow an application to mask out any
116   --  interrupt needed by the threads library.
117   Unmasked : constant Signal_Set :=
118     (SIGTRAP, SIGBUS, SIGTTIN, SIGTTOU, SIGTSTP);
119
120   --  FreeBSD will uses SIGPROF for timing.  Do not allow a
121   --  handler to attach to this signal.
122   Reserved : constant Signal_Set := (0 .. 0 => SIGPROF);
123
124   type sigset_t is private;
125
126   function sigaddset
127     (set : access sigset_t;
128      sig : Signal) return int;
129   pragma Import (C, sigaddset, "sigaddset");
130
131   function sigdelset
132     (set : access sigset_t;
133      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
140     (set : access sigset_t;
141      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   --  sigcontext is architecture dependent, so define it private
148   type struct_sigcontext is private;
149
150   type old_struct_sigaction is record
151      sa_handler : System.Address;
152      sa_mask    : sigset_t;
153      sa_flags   : int;
154   end record;
155   pragma Convention (C, old_struct_sigaction);
156
157   type new_struct_sigaction is record
158      sa_handler : System.Address;
159      sa_flags   : int;
160      sa_mask    : sigset_t;
161   end record;
162   pragma Convention (C, new_struct_sigaction);
163
164   subtype struct_sigaction is new_struct_sigaction;
165   type struct_sigaction_ptr is access all struct_sigaction;
166
167   SIG_BLOCK   : constant := 1;
168   SIG_UNBLOCK : constant := 2;
169   SIG_SETMASK : constant := 3;
170
171   SIG_DFL : constant := 0;
172   SIG_IGN : constant := 1;
173
174   SA_SIGINFO : constant := 16#0040#;
175
176   function sigaction
177     (sig  : Signal;
178      act  : struct_sigaction_ptr;
179      oact : struct_sigaction_ptr) return int;
180   pragma Import (C, sigaction, "sigaction");
181
182   ----------
183   -- Time --
184   ----------
185
186   Time_Slice_Supported : constant Boolean := True;
187   --  Indicates wether time slicing is supported (i.e SCHED_RR is supported)
188
189   type timespec is private;
190
191   function nanosleep (rqtp, rmtp : access timespec)  return int;
192   pragma Import (C, nanosleep, "nanosleep");
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)
201      return int;
202   pragma Import (C, clock_gettime, "clock_gettime");
203
204   function To_Duration (TS : timespec) return Duration;
205   pragma Inline (To_Duration);
206
207   function To_Timespec (D : Duration) return timespec;
208   pragma Inline (To_Timespec);
209
210   type struct_timezone is record
211      tz_minuteswest : int;
212      tz_dsttime     : int;
213   end record;
214   pragma Convention (C, struct_timezone);
215   type struct_timeval is private;
216   --  This is needed on systems that do not have clock_gettime()
217   --  but do have gettimeofday().
218
219   function To_Duration (TV : struct_timeval) return Duration;
220   pragma Inline (To_Duration);
221
222   function To_Timeval (D : Duration) return struct_timeval;
223   pragma Inline (To_Timeval);
224
225   function gettimeofday
226     (tv : access struct_timeval;
227      tz : System.Address) return int;
228   pragma Import (C, gettimeofday, "gettimeofday");
229
230   procedure usleep (useconds : unsigned_long);
231   pragma Import (C, usleep, "usleep");
232
233   -------------------------
234   -- Priority Scheduling --
235   -------------------------
236
237   SCHED_FIFO  : constant := 1;
238   SCHED_OTHER : constant := 2;
239   SCHED_RR    : constant := 3;
240
241   -------------
242   -- Process --
243   -------------
244
245   type pid_t is private;
246
247   Self_PID : constant pid_t;
248
249   function kill (pid : pid_t; sig : Signal) return int;
250   pragma Import (C, kill, "kill");
251
252   function getpid return pid_t;
253   pragma Import (C, getpid, "getpid");
254
255   ---------
256   -- LWP --
257   ---------
258
259   function lwp_self return System.Address;
260   --  lwp_self does not exist on this thread library, revert to pthread_self
261   --  which is the closest approximation (with getpid). This function is
262   --  needed to share 7staprop.adb across POSIX-like targets.
263   pragma Import (C, lwp_self, "pthread_self");
264
265   -------------
266   -- Threads --
267   -------------
268
269   type Thread_Body is access
270     function (arg : System.Address) return System.Address;
271
272   type pthread_t           is private;
273   subtype Thread_Id        is pthread_t;
274
275   type pthread_mutex_t     is limited private;
276   type pthread_cond_t      is limited private;
277   type pthread_attr_t      is limited private;
278   type pthread_mutexattr_t is limited private;
279   type pthread_condattr_t  is limited private;
280   type pthread_key_t       is private;
281
282   PTHREAD_CREATE_DETACHED : constant := 1;
283   PTHREAD_CREATE_JOINABLE : constant := 0;
284
285   -----------
286   -- Stack --
287   -----------
288
289   Stack_Base_Available : constant Boolean := False;
290   --  Indicates wether the stack base is available on this target.
291   --  This allows us to share s-osinte.adb between all the FSU run time.
292   --  Note that this value can only be true if pthread_t has a complete
293   --  definition that corresponds exactly to the C header files.
294
295   function Get_Stack_Base (thread : pthread_t) return Address;
296   pragma Inline (Get_Stack_Base);
297   --  returns the stack base of the specified thread.
298   --  Only call this function when Stack_Base_Available is True.
299
300   function Get_Page_Size return size_t;
301   function Get_Page_Size return Address;
302   pragma Import (C, Get_Page_Size, "getpagesize");
303   --  returns the size of a page, or 0 if this is not relevant on this
304   --  target
305
306   PROT_NONE  : constant := 0;
307   PROT_READ  : constant := 1;
308   PROT_WRITE : constant := 2;
309   PROT_EXEC  : constant := 4;
310   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
311
312   PROT_ON    : constant := PROT_NONE;
313   PROT_OFF   : constant := PROT_ALL;
314
315   function mprotect
316     (addr : Address; len : size_t; prot : int) return int;
317   pragma Import (C, mprotect);
318
319   -----------------------------------------
320   --  Nonstandard Thread Initialization  --
321   -----------------------------------------
322   --  FSU_THREADS requires pthread_init, which is nonstandard
323   --  and this should be invoked during the elaboration of s-taprop.adb
324   --
325   --  FreeBSD does not require this so we provide an empty Ada body.
326   procedure pthread_init;
327
328   ---------------------------
329   --  POSIX.1c  Section 3  --
330   ---------------------------
331
332   function sigwait
333     (set : access sigset_t;
334      sig : access Signal) return int;
335   pragma Import (C, sigwait, "sigwait");
336
337   function pthread_kill
338     (thread : pthread_t;
339      sig    : Signal) return   int;
340   pragma Import (C, pthread_kill, "pthread_kill");
341
342   type sigset_t_ptr is access all sigset_t;
343
344   function pthread_sigmask
345     (how  : int;
346      set  : sigset_t_ptr;
347      oset : sigset_t_ptr) return int;
348   pragma Import (C, pthread_sigmask, "pthread_sigmask");
349
350   ----------------------------
351   --  POSIX.1c  Section 11  --
352   ----------------------------
353
354   function pthread_mutexattr_init
355     (attr : access pthread_mutexattr_t) return int;
356   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
357
358   function pthread_mutexattr_destroy
359     (attr : access pthread_mutexattr_t) return int;
360   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
361
362   function pthread_mutex_init
363     (mutex : access pthread_mutex_t;
364      attr  : access pthread_mutexattr_t) return int;
365   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
366
367   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
368   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
369
370   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
371   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
372
373   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
374   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
375
376   function pthread_condattr_init
377     (attr : access pthread_condattr_t) return int;
378   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
379
380   function pthread_condattr_destroy
381     (attr : access pthread_condattr_t) return int;
382   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
383
384   function pthread_cond_init
385     (cond : access pthread_cond_t;
386      attr : access pthread_condattr_t) return int;
387   pragma Import (C, pthread_cond_init, "pthread_cond_init");
388
389   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
390   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
391
392   function pthread_cond_signal (cond : access pthread_cond_t) return int;
393   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
394
395   function pthread_cond_wait
396     (cond  : access pthread_cond_t;
397      mutex : access pthread_mutex_t) return int;
398   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
399
400   function pthread_cond_timedwait
401     (cond    : access pthread_cond_t;
402      mutex   : access pthread_mutex_t;
403      abstime : access timespec) return int;
404   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
405
406   Relative_Timed_Wait : constant Boolean := False;
407   --  pthread_cond_timedwait requires an absolute delay time
408
409   ----------------------------
410   --  POSIX.1c  Section 13  --
411   ----------------------------
412
413   PTHREAD_PRIO_NONE    : constant := 0;
414   PTHREAD_PRIO_PROTECT : constant := 2;
415   PTHREAD_PRIO_INHERIT : constant := 1;
416
417   function pthread_mutexattr_setprotocol
418     (attr     : access pthread_mutexattr_t;
419      protocol : int) return int;
420   pragma Import
421      (C, pthread_mutexattr_setprotocol, "pthread_mutexattr_setprotocol");
422
423   function pthread_mutexattr_getprotocol
424     (attr     : access pthread_mutexattr_t;
425      protocol : access int) return int;
426   pragma Import
427     (C, pthread_mutexattr_getprotocol, "pthread_mutexattr_getprotocol");
428
429   function pthread_mutexattr_setprioceiling
430     (attr     : access pthread_mutexattr_t;
431      prioceiling : int) return int;
432   pragma Import
433     (C, pthread_mutexattr_setprioceiling,
434      "pthread_mutexattr_setprioceiling");
435
436   function pthread_mutexattr_getprioceiling
437     (attr     : access pthread_mutexattr_t;
438      prioceiling : access int) return int;
439   pragma Import
440     (C, pthread_mutexattr_getprioceiling,
441      "pthread_mutexattr_getprioceiling");
442
443   type struct_sched_param is record
444      sched_priority : int;
445   end record;
446   pragma Convention (C, struct_sched_param);
447
448   function pthread_getschedparam
449     (thread : pthread_t;
450      policy : access int;
451      param  : access struct_sched_param) return int;
452   pragma Import (C, pthread_getschedparam, "pthread_getschedparam");
453
454   function pthread_setschedparam
455     (thread : pthread_t;
456      policy : int;
457      param  : access struct_sched_param) return int;
458   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
459
460   function pthread_attr_setscope
461     (attr            : access pthread_attr_t;
462      contentionscope : int) return int;
463   pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
464
465   function pthread_attr_getscope
466     (attr            : access pthread_attr_t;
467      contentionscope : access int) return int;
468   pragma Import (C, pthread_attr_getscope, "pthread_attr_getscope");
469
470   function pthread_attr_setinheritsched
471     (attr            : access pthread_attr_t;
472      inheritsched : int) return int;
473   pragma Import
474     (C, pthread_attr_setinheritsched, "pthread_attr_setinheritsched");
475
476   function pthread_attr_getinheritsched
477     (attr         : access pthread_attr_t;
478      inheritsched : access int) return int;
479   pragma Import
480     (C, pthread_attr_getinheritsched, "pthread_attr_getinheritsched");
481
482   function pthread_attr_setschedpolicy
483     (attr   : access pthread_attr_t;
484      policy : int) return int;
485   pragma Import (C, pthread_attr_setschedpolicy,
486     "pthread_attr_setschedpolicy");
487
488   function pthread_attr_getschedpolicy
489     (attr   : access pthread_attr_t;
490      policy : access int) return int;
491   pragma Import (C, pthread_attr_getschedpolicy,
492     "pthread_attr_getschedpolicy");
493
494   function pthread_attr_setschedparam
495     (attr        : access pthread_attr_t;
496      sched_param : int) return int;
497   pragma Import (C, pthread_attr_setschedparam, "pthread_attr_setschedparam");
498
499   function pthread_attr_getschedparam
500     (attr        : access pthread_attr_t;
501      sched_param : access int) return int;
502   pragma Import (C, pthread_attr_getschedparam, "pthread_attr_getschedparam");
503
504   function sched_yield return int;
505   pragma Import (C, sched_yield, "pthread_yield");
506
507   -----------------------------
508   --  P1003.1c - Section 16  --
509   -----------------------------
510
511   function pthread_attr_init (attributes : access pthread_attr_t) return int;
512   pragma Import (C, pthread_attr_init, "pthread_attr_init");
513
514   function pthread_attr_destroy
515     (attributes : access pthread_attr_t) return int;
516   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
517
518   function pthread_attr_setdetachstate
519     (attr        : access pthread_attr_t;
520      detachstate : int) return int;
521   pragma Import
522     (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate");
523
524   function pthread_attr_getdetachstate
525     (attr        : access pthread_attr_t;
526      detachstate : access int) return int;
527   pragma Import
528     (C, pthread_attr_getdetachstate, "pthread_attr_getdetachstate");
529
530   function pthread_attr_getstacksize
531     (attr      : access pthread_attr_t;
532      stacksize : access size_t) return int;
533   pragma Import
534     (C, pthread_attr_getstacksize, "pthread_attr_getstacksize");
535
536   function pthread_attr_setstacksize
537     (attr      : access pthread_attr_t;
538      stacksize : size_t) return int;
539   pragma Import
540     (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
541
542   function pthread_create
543     (thread        : access pthread_t;
544      attributes    : access pthread_attr_t;
545      start_routine : Thread_Body;
546      arg           : System.Address) return int;
547   pragma Import (C, pthread_create, "pthread_create");
548
549   function pthread_detach (thread : pthread_t) return int;
550   pragma Import (C, pthread_detach, "pthread_detach");
551
552   procedure pthread_exit (status : System.Address);
553   pragma Import (C, pthread_exit, "pthread_exit");
554
555   function pthread_self return pthread_t;
556   pragma Import (C, pthread_self, "pthread_self");
557
558   ----------------------------
559   --  POSIX.1c  Section 17  --
560   ----------------------------
561
562   function pthread_setspecific
563     (key   : pthread_key_t;
564      value : System.Address) return  int;
565   pragma Import (C, pthread_setspecific, "pthread_setspecific");
566
567   function pthread_getspecific (key : pthread_key_t) return System.Address;
568   pragma Import (C, pthread_getspecific, "pthread_getspecific");
569
570   type destructor_pointer is access
571      procedure (arg : System.Address);
572
573   function pthread_key_create
574     (key        : access pthread_key_t;
575      destructor : destructor_pointer) return int;
576   pragma Import (C, pthread_key_create, "pthread_key_create");
577
578   --------------------------------------
579   --  Non-portable pthread functions  --
580   --------------------------------------
581
582   function pthread_set_name_np
583     (thread : pthread_t;
584      name   : System.Address) return int;
585   pragma Import (C, pthread_set_name_np, "pthread_set_name_np");
586
587private
588
589   type sigset_t is array (1 .. 4) of unsigned;
590
591   --  In FreeBSD the component sa_handler turns out to
592   --  be one a union type, and the selector is a macro:
593   --  #define sa_handler __sigaction_u._handler
594   --  #define sa_sigaction __sigaction_u._sigaction
595
596   --  Should we add a signal_context type here ?
597   --  How could it be done independent of the CPU architecture ?
598   --  sigcontext type is opaque, so it is architecturally neutral.
599   --  It is always passed as an access type, so define it as an empty record
600   --  since the contents are not used anywhere.
601   type struct_sigcontext is null record;
602   pragma Convention (C, struct_sigcontext);
603
604   type pid_t is new int;
605   Self_PID : constant pid_t := 0;
606
607   type time_t is new long;
608
609   type timespec is record
610      ts_sec  : time_t;
611      ts_nsec : long;
612   end record;
613   pragma Convention (C, timespec);
614
615   type clockid_t is new int;
616   CLOCK_REALTIME : constant clockid_t := 0;
617
618   type struct_timeval is record
619      tv_sec  : long;
620      tv_usec : long;
621   end record;
622   pragma Convention (C, struct_timeval);
623
624   type pthread_t           is new System.Address;
625   type pthread_attr_t      is new System.Address;
626   type pthread_mutex_t     is new System.Address;
627   type pthread_mutexattr_t is new System.Address;
628   type pthread_cond_t      is new System.Address;
629   type pthread_condattr_t  is new System.Address;
630   type pthread_key_t       is new int;
631
632end System.OS_Interface;
633