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) 1995-2020, Free Software Foundation, Inc.         --
10--                                                                          --
11-- GNAT 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 3,  or (at your option) any later ver- --
14-- sion.  GNAT 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.                                     --
17--                                                                          --
18-- As a special exception under Section 7 of GPL version 3, you are granted --
19-- additional permissions described in the GCC Runtime Library Exception,   --
20-- version 3.1, as published by the Free Software Foundation.               --
21--                                                                          --
22-- You should have received a copy of the GNU General Public License and    --
23-- a copy of the GCC Runtime Library Exception along with this program;     --
24-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25-- <http://www.gnu.org/licenses/>.                                          --
26--                                                                          --
27-- GNARL was developed by the GNARL team at Florida State University.       --
28-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29--                                                                          --
30------------------------------------------------------------------------------
31
32--  This is a QNX/Neutrino version of this package
33
34--  This package encapsulates all direct interfaces to OS services
35--  that are needed by the tasking run-time (libgnarl).
36
37--  PLEASE DO NOT add any with-clauses to this package or remove the pragma
38--  Preelaborate. This package is designed to be a bottom-level (leaf) package.
39
40with Ada.Unchecked_Conversion;
41with Interfaces.C;
42with System.OS_Constants;
43
44package System.OS_Interface is
45   pragma Preelaborate;
46
47   subtype int            is Interfaces.C.int;
48   subtype char           is Interfaces.C.char;
49   subtype short          is Interfaces.C.short;
50   subtype long           is Interfaces.C.long;
51   subtype unsigned       is Interfaces.C.unsigned;
52   subtype unsigned_short is Interfaces.C.unsigned_short;
53   subtype unsigned_long  is Interfaces.C.unsigned_long;
54   subtype unsigned_char  is Interfaces.C.unsigned_char;
55   subtype plain_char     is Interfaces.C.plain_char;
56   subtype size_t         is Interfaces.C.size_t;
57
58   -----------
59   -- Errno --
60   -----------
61
62   function errno return int;
63   pragma Import (C, errno, "__get_errno");
64
65   EPERM     : constant := 1;
66   EINTR     : constant := 4;
67   EAGAIN    : constant := 11;
68   ENOMEM    : constant := 12;
69   EINVAL    : constant := 22;
70   ETIMEDOUT : constant := 260;
71
72   -------------
73   -- Signals --
74   -------------
75
76   Max_Interrupt : constant := 64;
77   type Signal is new int range 0 .. Max_Interrupt;
78   for Signal'Size use int'Size;
79
80   SIGHUP     : constant := 1;
81   SIGINT     : constant := 2;
82   SIGQUIT    : constant := 3;
83   SIGILL     : constant := 4;
84   SIGTRAP    : constant := 5;
85   SIGIOT     : constant := 6;
86   SIGABRT    : constant := 6;
87   SIGDEADLK  : constant := 7;
88   SIGFPE     : constant := 8;
89   SIGKILL    : constant := 9;
90   SIGBUS     : constant := 10;
91   SIGSEGV    : constant := 11;
92   SIGSYS     : constant := 12;
93   SIGPIPE    : constant := 13;
94   SIGALRM    : constant := 14;
95   SIGTERM    : constant := 15;
96   SIGUSR1    : constant := 16;
97   SIGUSR2    : constant := 17;
98   SIGCLD     : constant := 18;
99   SIGCHLD    : constant := 18;
100   SIGPWR     : constant := 19;
101   SIGWINCH   : constant := 20;
102   SIGURG     : constant := 21;
103   SIGPOLL    : constant := 22;
104   SIGIO      : constant := 22;
105   SIGSTOP    : constant := 23;
106   SIGTSTP    : constant := 24;
107   SIGCONT    : constant := 25;
108   SIGTTIN    : constant := 26;
109   SIGTTOU    : constant := 27;
110   SIGVTALRM  : constant := 28;
111   SIGPROF    : constant := 29;
112   SIGXCPU    : constant := 30;
113   SIGXFSZ    : constant := 31;
114
115   SIGRTMIN   : constant := 41;
116   SITRTMAX   : constant := 56;
117
118   SIGSELECT  : constant := 57;
119   SIGPHOTON  : constant := 58;
120
121   SIGADAABORT : constant := SIGABRT;
122   --  Change this to use another signal for task abort. SIGTERM might be a
123   --  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 and IO
136      --  behaves as normal "C" applications
137
138      SIGPROF,
139      --  To avoid confusing the profiler
140
141      SIGKILL, SIGSTOP);
142      --  These two signals actually can't be masked (POSIX won't allow it)
143
144   Reserved : constant Signal_Set := (SIGABRT, SIGKILL, SIGSTOP, SIGSEGV);
145
146   type sigset_t is private;
147
148   function sigaddset (set : access sigset_t; sig : Signal) return int;
149   pragma Import (C, sigaddset, "sigaddset");
150
151   function sigdelset (set : access sigset_t; sig : Signal) return int;
152   pragma Import (C, sigdelset, "sigdelset");
153
154   function sigfillset (set : access sigset_t) return int;
155   pragma Import (C, sigfillset, "sigfillset");
156
157   function sigismember (set : access sigset_t; sig : Signal) return int;
158   pragma Import (C, sigismember, "sigismember");
159
160   function sigemptyset (set : access sigset_t) return int;
161   pragma Import (C, sigemptyset, "sigemptyset");
162
163   type pad7 is array (1 .. 7) of int;
164   type siginfo_t is record
165      si_signo : int;
166      si_code  : int;
167      si_errno : int;
168      X_data   : pad7;
169   end record;
170   pragma Convention (C, siginfo_t);
171
172   type struct_sigaction is record
173      sa_handler  : System.Address;
174      sa_flags    : int;
175      sa_mask     : sigset_t;
176   end record;
177   pragma Convention (C, struct_sigaction);
178
179   type struct_sigaction_ptr is access all struct_sigaction;
180
181   SIG_BLOCK   : constant := 0;
182   SIG_UNBLOCK : constant := 1;
183   SIG_SETMASK : constant := 2;
184   SIG_PENDING : constant := 5;
185
186   SA_NOCLDSTOP : constant := 16#0001#;
187   SA_SIGINFO   : constant := 16#0002#;
188   SA_RESETHAND : constant := 16#0004#;
189   SA_ONSTACK   : constant := 16#0008#;
190   SA_NODEFER   : constant := 16#0010#;
191   SA_NOCLDWAIT : constant := 16#0020#;
192
193   SS_ONSTACK   : constant := 1;
194   SS_DISABLE   : constant := 2;
195
196   SIG_DFL : constant := 0;
197   SIG_IGN : constant := 1;
198
199   function sigaction
200     (sig  : Signal;
201      act  : struct_sigaction_ptr;
202      oact : struct_sigaction_ptr) return int;
203   pragma Import (C, sigaction, "sigaction");
204
205   ----------
206   -- Time --
207   ----------
208
209   Time_Slice_Supported : constant Boolean := True;
210   --  Indicates whether time slicing is supported
211
212   type timespec is private;
213
214   type clockid_t is new int;
215
216   function clock_gettime
217     (clock_id : clockid_t; tp : access timespec) return int;
218   pragma Import (C, clock_gettime, "clock_gettime");
219
220   function clock_getres
221     (clock_id : clockid_t;
222      res      : access timespec) return int;
223   pragma Import (C, clock_getres, "clock_getres");
224
225   function To_Duration (TS : timespec) return Duration;
226   pragma Inline (To_Duration);
227
228   function To_Timespec (D : Duration) return timespec;
229   pragma Inline (To_Timespec);
230
231   -------------------------
232   -- Priority Scheduling --
233   -------------------------
234
235   SCHED_FIFO  : constant := 1;
236   SCHED_RR    : constant := 2;
237   SCHED_OTHER : constant := 3;
238
239   function To_Target_Priority
240     (Prio : System.Any_Priority) return Interfaces.C.int
241     with Inline_Always;
242   --  Maps System.Any_Priority to a POSIX priority
243
244   -------------
245   -- Process --
246   -------------
247
248   type pid_t is private;
249
250   function kill (pid : pid_t; sig : Signal) return int;
251   pragma Import (C, kill, "kill");
252
253   function getpid return pid_t;
254   pragma Import (C, getpid, "getpid");
255
256   -------------
257   -- Threads --
258   -------------
259
260   type Thread_Body is access
261     function (arg : System.Address) return System.Address;
262   pragma Convention (C, Thread_Body);
263
264   function Thread_Body_Access is new
265     Ada.Unchecked_Conversion (System.Address, Thread_Body);
266
267   type pthread_t is new int;
268   subtype Thread_Id is pthread_t;
269
270   type pthread_mutex_t      is limited private;
271   type pthread_cond_t       is limited private;
272   type pthread_attr_t       is limited private;
273   type pthread_mutexattr_t  is limited private;
274   type pthread_condattr_t   is limited private;
275   type pthread_key_t        is private;
276
277   PTHREAD_CREATE_DETACHED : constant := 1;
278
279   PTHREAD_SCOPE_PROCESS  : constant := 4;
280   PTHREAD_SCOPE_SYSTEM   : constant := 0;
281
282   PTHREAD_INHERIT_SCHED  : constant := 0;
283   PTHREAD_EXPLICIT_SCHED : constant := 2;
284
285   --  Read/Write lock not supported on Android.
286
287   subtype pthread_rwlock_t     is pthread_mutex_t;
288   subtype pthread_rwlockattr_t is pthread_mutexattr_t;
289
290   -----------
291   -- Stack --
292   -----------
293
294   type stack_t is record
295      ss_sp    : System.Address;
296      ss_flags : int;
297      ss_size  : size_t;
298   end record;
299   pragma Convention (C, stack_t);
300
301   function sigaltstack
302     (ss  : not null access stack_t;
303      oss : access stack_t) return int
304     with Inline;
305   --  Not supported on QNX
306
307   Alternate_Stack : aliased System.Address;
308   --  Dummy definition: alternate stack not available due to missing
309   --  sigaltstack in QNX
310
311   Alternate_Stack_Size : constant := 0;
312   --  This must be kept in sync with init.c:__gnat_alternate_stack
313
314   Stack_Base_Available : constant Boolean := False;
315   --  Indicates whether the stack base is available on this target
316
317   function Get_Stack_Base (thread : pthread_t) return System.Address
318     with Inline;
319   --  This is a dummy procedure to share some GNULLI files
320
321   function Get_Page_Size return int;
322   pragma Import (C, Get_Page_Size, "getpagesize");
323   --  Returns the size of a page
324
325   PROT_NONE  : constant := 16#00_00#;
326   PROT_READ  : constant := 16#01_00#;
327   PROT_WRITE : constant := 16#02_00#;
328   PROT_EXEC  : constant := 16#04_00#;
329   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
330   PROT_ON    : constant := PROT_READ;
331   PROT_OFF   : constant := PROT_ALL;
332
333   function mprotect (addr : Address; len : size_t; prot : int) return int;
334   pragma Import (C, mprotect);
335
336   ---------------------------------------
337   -- Nonstandard Thread Initialization --
338   ---------------------------------------
339
340   procedure pthread_init with Inline_Always;
341
342   -------------------------
343   -- POSIX.1c  Section 3 --
344   -------------------------
345
346   function sigwait (set : access sigset_t; sig : access Signal) return int;
347   pragma Import (C, sigwait, "sigwait");
348
349   function pthread_kill (thread : pthread_t; sig : Signal) return int;
350   pragma Import (C, pthread_kill, "pthread_kill");
351
352   function pthread_sigmask
353     (how  : int;
354      set  : access sigset_t;
355      oset : access sigset_t) return int;
356   pragma Import (C, pthread_sigmask, "pthread_sigmask");
357
358   --------------------------
359   -- POSIX.1c  Section 11 --
360   --------------------------
361
362   function pthread_mutexattr_init
363     (attr : access pthread_mutexattr_t) return int;
364   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
365
366   function pthread_mutexattr_destroy
367     (attr : access pthread_mutexattr_t) return int;
368   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
369
370   function pthread_mutex_init
371     (mutex : access pthread_mutex_t;
372      attr  : access pthread_mutexattr_t) return int;
373   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
374
375   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
376   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
377
378   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
379   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
380
381   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
382   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
383
384   function pthread_mutex_setprioceiling
385     (mutex       : access pthread_mutex_t;
386      prioceiling : int;
387      old_ceiling : access int) return int;
388   pragma Import (C, pthread_mutex_setprioceiling);
389
390   function pthread_condattr_init
391     (attr : access pthread_condattr_t) return int;
392   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
393
394   function pthread_condattr_destroy
395     (attr : access pthread_condattr_t) return int;
396   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
397
398   function pthread_cond_init
399     (cond : access pthread_cond_t;
400      attr : access pthread_condattr_t) return int;
401   pragma Import (C, pthread_cond_init, "pthread_cond_init");
402
403   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
404   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
405
406   function pthread_cond_signal (cond : access pthread_cond_t) return int;
407   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
408
409   function pthread_cond_wait
410     (cond  : access pthread_cond_t;
411      mutex : access pthread_mutex_t) return int;
412   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
413
414   function pthread_cond_timedwait
415     (cond    : access pthread_cond_t;
416      mutex   : access pthread_mutex_t;
417      abstime : access timespec) return int;
418   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
419
420   --------------------------
421   -- POSIX.1c  Section 13 --
422   --------------------------
423
424   PTHREAD_PRIO_INHERIT : constant := 0;
425   PTHREAD_PRIO_NONE    : constant := 1;
426   PTHREAD_PRIO_PROTECT : constant := 2;
427
428   function pthread_mutexattr_setprotocol
429     (attr     : access pthread_mutexattr_t;
430      protocol : int) return int;
431   pragma Import (C, pthread_mutexattr_setprotocol);
432
433   function pthread_mutexattr_getprotocol
434     (attr     : access pthread_mutexattr_t;
435      protocol : access int) return int;
436   pragma Import (C, pthread_mutexattr_getprotocol);
437
438   function pthread_mutexattr_setprioceiling
439     (attr        : access pthread_mutexattr_t;
440      prioceiling : int) return int;
441   pragma Import (C, pthread_mutexattr_setprioceiling);
442
443   function pthread_mutexattr_getprioceiling
444     (attr        : access pthread_mutexattr_t;
445      prioceiling : access int) return int;
446   pragma Import (C, pthread_mutexattr_getprioceiling);
447
448   function pthread_mutex_getprioceiling
449     (attr        : access pthread_mutex_t;
450      prioceiling : access int) return int;
451   pragma Import (C, pthread_mutex_getprioceiling);
452
453   type pad8 is array (1 .. 8) of int;
454   pragma Convention (C, pad8);
455
456   type struct_sched_param is record
457      sched_priority    : int := 0;  --  scheduling priority
458      sched_curpriority : int := 0;
459      reserved          : pad8 := (others => 0);
460   end record;
461   pragma Convention (C, struct_sched_param);
462
463   function pthread_setschedparam
464     (thread : pthread_t;
465      policy : int;
466      param  : access struct_sched_param) return int;
467   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
468
469   function pthread_getschedparam
470     (thread : pthread_t;
471      policy : access int;
472      param  : access struct_sched_param) return int;
473   pragma Import (C, pthread_getschedparam, "pthread_getschedparam");
474
475   function pthread_setschedprio
476     (thread   : pthread_t;
477      priority : int) return int;
478   pragma Import (C, pthread_setschedprio);
479
480   function pthread_attr_setschedparam
481     (attr   : access pthread_attr_t;
482      param  : access struct_sched_param) return int;
483   pragma Import (C, pthread_attr_setschedparam);
484
485   function pthread_attr_setinheritsched
486     (attr         : access pthread_attr_t;
487      inheritsched : int) return int;
488   pragma Import (C, pthread_attr_setinheritsched);
489
490   function pthread_attr_setscope
491     (attr  : access pthread_attr_t;
492      scope : int) return int;
493   pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
494
495   function pthread_attr_setschedpolicy
496     (attr   : access pthread_attr_t;
497      policy : int) return int;
498   pragma Import
499     (C, pthread_attr_setschedpolicy, "pthread_attr_setschedpolicy");
500
501   function sched_yield return int;
502   pragma Import (C, sched_yield, "sched_yield");
503
504   ---------------------------
505   -- P1003.1c - Section 16 --
506   ---------------------------
507
508   function pthread_attr_init
509     (attributes : access pthread_attr_t) return int;
510   pragma Import (C, pthread_attr_init, "pthread_attr_init");
511
512   function pthread_attr_destroy
513     (attributes : access pthread_attr_t) return int;
514   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
515
516   function pthread_attr_setdetachstate
517     (attr        : access pthread_attr_t;
518      detachstate : int) return int;
519   pragma Import (C, pthread_attr_setdetachstate);
520
521   function pthread_attr_setstacksize
522     (attr      : access pthread_attr_t;
523      stacksize : size_t) return int;
524   pragma Import (C, pthread_attr_setstacksize);
525
526   function pthread_create
527     (thread        : access pthread_t;
528      attributes    : access pthread_attr_t;
529      start_routine : Thread_Body;
530      arg           : System.Address) return int;
531   pragma Import (C, pthread_create, "pthread_create");
532
533   procedure pthread_exit (status : System.Address);
534   pragma Import (C, pthread_exit, "pthread_exit");
535
536   function pthread_self return pthread_t;
537   pragma Import (C, pthread_self, "pthread_self");
538
539   function lwp_self return System.Address;
540   pragma Import (C, lwp_self, "pthread_self");
541
542   --------------------------
543   -- POSIX.1c  Section 17 --
544   --------------------------
545
546   function pthread_setspecific
547     (key   : pthread_key_t;
548      value : System.Address) return int;
549   pragma Import (C, pthread_setspecific, "pthread_setspecific");
550
551   function pthread_getspecific (key : pthread_key_t) return System.Address;
552   pragma Import (C, pthread_getspecific, "pthread_getspecific");
553
554   type destructor_pointer is access procedure (arg : System.Address);
555   pragma Convention (C, destructor_pointer);
556
557   function pthread_key_create
558     (key        : access pthread_key_t;
559      destructor : destructor_pointer) return int;
560   pragma Import (C, pthread_key_create, "pthread_key_create");
561
562private
563
564   type sigset_t is array (1 .. 2) of Interfaces.Unsigned_32;
565   pragma Convention (C, sigset_t);
566
567   type pid_t is new int;
568
569   type time_t is new long;
570
571   type timespec is record
572      tv_sec  : time_t;
573      tv_nsec : long;
574   end record;
575   pragma Convention (C, timespec);
576
577   type unsigned_long_long_t is mod 2 ** 64;
578   --  Local type only used to get the alignment of this type below
579
580   subtype char_array is Interfaces.C.char_array;
581
582   type pthread_attr_t is record
583      Data : char_array (1 .. OS_Constants.PTHREAD_ATTR_SIZE);
584   end record;
585   pragma Convention (C, pthread_attr_t);
586   for pthread_attr_t'Alignment use Interfaces.C.unsigned_long'Alignment;
587
588   type pthread_condattr_t is record
589      Data : char_array (1 .. OS_Constants.PTHREAD_CONDATTR_SIZE);
590   end record;
591   pragma Convention (C, pthread_condattr_t);
592   for pthread_condattr_t'Alignment use Interfaces.C.int'Alignment;
593
594   type pthread_mutexattr_t is record
595      Data : char_array (1 .. OS_Constants.PTHREAD_MUTEXATTR_SIZE);
596   end  record;
597   pragma Convention (C, pthread_mutexattr_t);
598   for pthread_mutexattr_t'Alignment use Interfaces.C.int'Alignment;
599
600   type pthread_mutex_t is record
601      Data : char_array (1 .. OS_Constants.PTHREAD_MUTEX_SIZE);
602   end record;
603   pragma Convention (C, pthread_mutex_t);
604   for pthread_mutex_t'Alignment use Interfaces.C.unsigned_long'Alignment;
605
606   type pthread_cond_t is record
607      Data : char_array (1 .. OS_Constants.PTHREAD_COND_SIZE);
608   end record;
609   pragma Convention (C, pthread_cond_t);
610   for pthread_cond_t'Alignment use unsigned_long_long_t'Alignment;
611
612   type pthread_key_t is new int;
613
614end System.OS_Interface;
615