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-2020, 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 LynxOS-178 Elf (POSIX-8 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
45with System.Multiprocessors;
46
47package System.OS_Interface is
48   pragma Preelaborate;
49
50   subtype int            is Interfaces.C.int;
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   subtype int64          is Interfaces.Integer_64;
60
61   -----------
62   -- Errno --
63   -----------
64
65   function errno return int;
66   pragma Import (C, errno, "__get_errno");
67
68   EAGAIN    : constant := 11;
69   EINTR     : constant := 4;
70   EINVAL    : constant := 22;
71   ENOMEM    : constant := 12;
72   ETIMEDOUT : constant := 60;
73   --  Error codes
74
75   -------------
76   -- Signals --
77   -------------
78
79   Max_Interrupt : constant := 63;
80   --  Max_Interrupt is the number of OS signals, as defined in:
81   --
82   --   /usr/include/sys/signal.h
83   --
84   --  The lowest numbered signal is 1, but 0 is a valid argument to some
85   --  library functions, e.g. kill(2). However, 0 is not just another signal:
86   --  For instance 'I in Signal' and similar should be used with caution.
87
88   type Signal is new int range 0 .. Max_Interrupt;
89   for  Signal'Size use int'Size;
90
91   SIGHUP        : constant := 1;  --  hangup
92   SIGINT        : constant := 2;  --  interrupt (rubout)
93   SIGQUIT       : constant := 3;  --  quit (ASCD FS)
94   SIGILL        : constant := 4;  --  illegal instruction (not reset)
95   SIGTRAP       : constant := 5;  --  trace trap (not reset)
96   SIGBRK        : constant := 6;  --  break
97   SIGIOT        : constant := 6;  --  IOT instruction
98   SIGABRT       : constant := 6;  --  used by abort, replace SIGIOT in future
99   SIGCORE       : constant := 7;  --  kill with core dump
100   SIGEMT        : constant := 7;  --  EMT instruction
101   SIGFPE        : constant := 8;  --  floating point exception
102   SIGKILL       : constant := 9;  --  kill (cannot be caught or ignored)
103   SIGBUS        : constant := 10; --  bus error
104   SIGSEGV       : constant := 11; --  segmentation violation
105   SIGSYS        : constant := 12; --  bad argument to system call
106   SIGPIPE       : constant := 13; --  write on a pipe with no one to read it
107   SIGALRM       : constant := 14; --  alarm clock
108   SIGTERM       : constant := 15; --  software termination signal from kill
109   SIGURG        : constant := 16; --  urgent condition on IO channel
110   SIGSTOP       : constant := 17; --  stop (cannot be caught or ignored)
111   SIGTSTP       : constant := 18; --  user stop requested from tty
112   SIGCONT       : constant := 19; --  stopped process has been continued
113   SIGCLD        : constant := 20; --  alias for SIGCHLD
114   SIGCHLD       : constant := 20; --  child status change
115   SIGTTIN       : constant := 21; --  background tty read attempted
116   SIGTTOU       : constant := 22; --  background tty write attempted
117   SIGIO         : constant := 23; --  I/O possible (Solaris SIGPOLL alias)
118   SIGPOLL       : constant := 23; --  pollable event occurred
119   SIGTHREADKILL : constant := 24; --  Reserved by LynxOS runtime
120   SIGXCPU       : constant := 24; --  CPU time limit exceeded
121   SIGXFSZ       : constant := 25; --  filesize limit exceeded
122   SIGVTALRM     : constant := 26; --  virtual timer expired
123   SIGPROF       : constant := 27; --  profiling timer expired
124   SIGWINCH      : constant := 28; --  window size change
125   SIGLOST       : constant := 29; --  SUN 4.1 compatibility
126   SIGUSR1       : constant := 30; --  user defined signal 1
127   SIGUSR2       : constant := 31; --  user defined signal 2
128
129   SIGPRIO       : constant := 32;
130   --  Sent to a process with its priority or group is changed
131
132   SIGADAABORT : constant := SIGABRT;
133   --  Change this if you want to use another signal for task abort. SIGTERM
134   --  might be a good one.
135
136   type Signal_Set is array (Natural range <>) of Signal;
137
138   Unmasked    : constant Signal_Set :=
139     (SIGTRAP, SIGTTIN, SIGTTOU, SIGTSTP, SIGPROF, SIGTHREADKILL);
140   Reserved    : constant Signal_Set := (SIGABRT, SIGKILL, SIGSTOP, SIGPRIO);
141
142   type sigset_t is private;
143
144   function sigaddset (set : access sigset_t; sig : Signal) return int;
145   pragma Import (C, sigaddset, "sigaddset");
146
147   function sigdelset (set : access sigset_t; sig : Signal) return int;
148   pragma Import (C, sigdelset, "sigdelset");
149
150   function sigfillset (set : access sigset_t) return int;
151   pragma Import (C, sigfillset, "sigfillset");
152
153   function sigismember (set : access sigset_t; sig : Signal) return int;
154   pragma Import (C, sigismember, "sigismember");
155
156   function sigemptyset (set : access sigset_t) return int;
157   pragma Import (C, sigemptyset, "sigemptyset");
158
159   type struct_sigaction is record
160      sa_handler   : System.Address;
161      sa_mask      : sigset_t;
162      sa_flags     : int;
163   end record;
164   pragma Convention (C, struct_sigaction);
165   type struct_sigaction_ptr is access all struct_sigaction;
166
167   SA_SIGINFO : constant := 16#80#;
168
169   SA_ONSTACK : constant := 16#00#;
170   --  SA_ONSTACK is not defined on LynxOS, but it is referred to in the POSIX
171   --  implementation of System.Interrupt_Management. Therefore we define a
172   --  dummy value of zero here so that setting this flag is a nop.
173
174   SIG_BLOCK   : constant := 0;
175   SIG_UNBLOCK : constant := 1;
176   SIG_SETMASK : constant := 2;
177
178   SIG_DFL : constant := 0;
179   SIG_IGN : constant := 1;
180
181   function sigaction
182     (sig  : Signal;
183      act  : struct_sigaction_ptr;
184      oact : struct_sigaction_ptr) return int;
185   pragma Import (C, sigaction, "sigaction");
186
187   ----------
188   -- Time --
189   ----------
190
191   Time_Slice_Supported : constant Boolean := True;
192   --  Indicates whether time slicing is supported
193
194   type timespec is private;
195
196   type clockid_t is new int;
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
223   -------------------------
224   -- Priority Scheduling --
225   -------------------------
226
227   SCHED_RR    : constant := 16#100_000#;
228   SCHED_FIFO  : constant := 16#200_000#;
229   SCHED_OTHER : constant := 16#400_000#;
230
231   function To_Target_Priority
232     (Prio : System.Any_Priority) return Interfaces.C.int;
233   --  Maps System.Any_Priority to a POSIX priority
234
235   -------------
236   -- Process --
237   -------------
238
239   type pid_t is private;
240
241   function kill (pid : pid_t; sig : Signal) return int;
242   pragma Import (C, kill, "kill");
243
244   function getpid return pid_t;
245   pragma Import (C, getpid, "getpid");
246
247   ---------
248   -- LWP --
249   ---------
250
251   type pthread_t is private;
252
253   function lwp_self return pthread_t;
254   pragma Import (C, lwp_self, "pthread_self");
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   subtype Thread_Id        is pthread_t;
268
269   type pthread_mutex_t     is limited private;
270   type pthread_cond_t      is limited private;
271   type pthread_attr_t      is limited private;
272   type pthread_mutexattr_t is limited private;
273   type pthread_condattr_t  is limited private;
274   type pthread_key_t       is private;
275
276   PTHREAD_CREATE_DETACHED : constant := 1;
277   PTHREAD_CREATE_JOINABLE : constant := 0;
278
279   PTHREAD_SCOPE_PROCESS : constant := 0; --  not supported by LynxOS178
280   PTHREAD_SCOPE_SYSTEM  : constant := 1;
281
282   --  Read/Write lock not supported on LynxOS. To add support both types
283   --  pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
284   --  with the associated routines pthread_rwlock_[init/destroy] and
285   --  pthread_rwlock_[rdlock/wrlock/unlock].
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   pragma Import (C, sigaltstack, "sigaltstack");
305   --  Neither stack_t nor sigaltstack are available on LynxOS-178
306
307   Alternate_Stack : aliased System.Address;
308   --  This is a dummy definition, never used (Alternate_Stack_Size is 0)
309
310   Alternate_Stack_Size : constant := 0;
311   --  No alternate signal stack is used on this platform
312
313   Stack_Base_Available : constant Boolean := False;
314   --  Indicates whether the stack base is available on this target
315
316   function Get_Stack_Base (thread : pthread_t) return Address;
317   pragma Inline (Get_Stack_Base);
318   --  Returns the stack base of the specified thread. Only call this function
319   --  when Stack_Base_Available is True.
320
321   function Get_Page_Size return int;
322   --  Returns the size of a page in bytes
323
324   PROT_NONE  : constant := 1;
325   PROT_READ  : constant := 2;
326   PROT_WRITE : constant := 4;
327   PROT_EXEC  : constant := 8;
328   PROT_ALL   : constant := PROT_READ + PROT_WRITE + PROT_EXEC;
329
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;
341   --  This is a dummy procedure to share some GNULLI files
342
343   -------------------------
344   -- POSIX.1c  Section 3 --
345   -------------------------
346   function sigwait
347     (set : access sigset_t;
348      sig : access Signal) return int;
349   pragma Inline (sigwait);
350   --  LynxOS has non standard sigwait
351
352   function pthread_kill
353     (thread : pthread_t;
354      sig    : Signal) return int;
355   pragma Import (C, pthread_kill, "pthread_kill");
356
357   function pthread_sigmask
358     (how  : int;
359      set  : access sigset_t;
360      oset : access sigset_t) return int;
361   pragma Import (C, pthread_sigmask, "pthread_sigmask");
362
363   ----------------------------
364   --  POSIX.1c  Section 11  --
365   ----------------------------
366
367   function pthread_mutexattr_init
368     (attr : access pthread_mutexattr_t) return int;
369   pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
370
371   function pthread_mutexattr_destroy
372     (attr : access pthread_mutexattr_t) return int;
373   pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
374
375   function pthread_mutex_init
376     (mutex : access pthread_mutex_t;
377      attr  : access pthread_mutexattr_t) return int;
378   pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
379
380   function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
381   pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
382
383   function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
384   pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
385
386   function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
387   pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
388
389   function pthread_condattr_init
390     (attr : access pthread_condattr_t) return int;
391   pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
392
393   function pthread_condattr_destroy
394     (attr : access pthread_condattr_t) return int;
395   pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
396
397   function pthread_cond_init
398     (cond : access pthread_cond_t;
399      attr : access pthread_condattr_t) return int;
400   pragma Import (C, pthread_cond_init, "pthread_cond_init");
401
402   function pthread_cond_destroy (cond : access pthread_cond_t) return int;
403   pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
404
405   function pthread_cond_signal (cond : access pthread_cond_t) return int;
406   pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
407
408   function pthread_cond_wait
409     (cond  : access pthread_cond_t;
410      mutex : access pthread_mutex_t) return int;
411   pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
412
413   function pthread_cond_timedwait
414     (cond    : access pthread_cond_t;
415      mutex   : access pthread_mutex_t;
416      abstime : access timespec) return int;
417   pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
418
419   --------------------------
420   -- POSIX.1c  Section 13 --
421   --------------------------
422
423   PTHREAD_PRIO_NONE    : constant := 0;
424   PTHREAD_PRIO_INHERIT : constant := 1;
425   PTHREAD_PRIO_PROTECT : constant := 2;
426
427   function pthread_mutexattr_setprotocol
428     (attr     : access pthread_mutexattr_t;
429      protocol : int) return int;
430   pragma Import (C, pthread_mutexattr_setprotocol);
431
432   function pthread_mutexattr_setprioceiling
433     (attr        : access pthread_mutexattr_t;
434      prioceiling : int) return int;
435   pragma Import (C, pthread_mutexattr_setprioceiling);
436
437   type struct_sched_param is record
438      sched_priority        : int;
439   end record;
440   pragma Convention (C, struct_sched_param);
441
442   function pthread_setschedparam
443     (thread : pthread_t;
444      policy : int;
445      param  : access struct_sched_param) return int;
446   pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
447
448   function pthread_attr_setscope
449     (Unused_attr            : access pthread_attr_t;
450      Unused_contentionscope : int) return int is (0);
451   --  pthread_attr_setscope is not implemented in production mode
452
453   function pthread_attr_setinheritsched
454     (attr         : access pthread_attr_t;
455      inheritsched : int) return int;
456   pragma Import (C, pthread_attr_setinheritsched);
457
458   function pthread_attr_setschedpolicy
459     (attr   : access pthread_attr_t;
460      policy : int) return int;
461   pragma Import (C, pthread_attr_setschedpolicy);
462
463   function sched_yield return int;
464   pragma Import (C, sched_yield, "sched_yield");
465
466   --------------------------
467   -- P1003.1c  Section 16 --
468   --------------------------
469
470   function pthread_attr_init (attributes : access pthread_attr_t) return int;
471   pragma Import (C, pthread_attr_init, "pthread_attr_init");
472
473   function pthread_attr_destroy
474     (attributes : access pthread_attr_t) return int;
475   pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
476
477   function pthread_attr_setdetachstate
478     (attr        : access pthread_attr_t;
479      detachstate : int) return int;
480   pragma Import (C, pthread_attr_setdetachstate);
481
482   function pthread_attr_setstacksize
483     (attr      : access pthread_attr_t;
484      stacksize : size_t) return int;
485   pragma Import (C, pthread_attr_setstacksize);
486
487   function pthread_create
488     (thread        : access pthread_t;
489      attributes    : access pthread_attr_t;
490      start_routine : Thread_Body;
491      arg           : System.Address) return int;
492   pragma Import (C, pthread_create, "pthread_create");
493
494   procedure pthread_exit (status : System.Address);
495   pragma Import (C, pthread_exit, "pthread_exit");
496
497   function pthread_self return pthread_t;
498   pragma Import (C, pthread_self, "pthread_self");
499
500   --------------------------
501   -- POSIX.1c  Section 17 --
502   --------------------------
503
504   function pthread_setspecific
505     (key   : pthread_key_t;
506      value : System.Address) return int;
507   pragma Import (C, pthread_setspecific, "pthread_setspecific");
508
509   function pthread_getspecific
510     (key    : pthread_key_t) return System.Address;
511   pragma Import (C, pthread_getspecific, "pthread_getspecific");
512
513   type destructor_pointer is access procedure (arg : System.Address);
514   pragma Convention (C, destructor_pointer);
515
516   function pthread_key_create
517     (key        : access pthread_key_t;
518      destructor : destructor_pointer
519     ) return int;
520   pragma Import (C, pthread_key_create, "pthread_key_create");
521
522   ---------------------
523   -- Multiprocessors --
524   ---------------------
525
526   function Current_CPU return Multiprocessors.CPU;
527   --  Return the id of the current CPU
528
529   function Get_Affinity (Id : Thread_Id) return Multiprocessors.CPU_Range;
530   --  Return CPU affinity of the given thread (maybe Not_A_Specific_CPU)
531
532   function Get_CPU (Id : Thread_Id) return Multiprocessors.CPU;
533   --  Return the CPU in charge of the given thread (always a valid CPU)
534
535private
536
537   type sigset_t is array (1 .. 2) of long;
538   pragma Convention (C, sigset_t);
539
540   type pid_t is new long;
541
542   type time_t is new int64;
543
544   type suseconds_t is new int;
545
546   type timespec is record
547      tv_sec  : time_t;
548      tv_nsec : long;
549   end record;
550   pragma Convention (C, timespec);
551
552   type struct_timeval is record
553      tv_sec  : time_t;
554      tv_usec : suseconds_t;
555   end record;
556   pragma Convention (C, struct_timeval);
557
558   type st_attr is record
559      stksize      : int;
560      prio         : int;
561      inheritsched : int;
562      state        : int;
563      sched        : int;
564      detachstate  : int;
565      guardsize    : int;
566   end record;
567   pragma Convention (C, st_attr);
568   subtype st_attr_t is st_attr;
569
570   type pthread_attr_t is record
571      pthread_attr_magic : unsigned;
572      st                 : st_attr_t;
573      pthread_attr_scope : int;
574   end record;
575   pragma Convention (C, pthread_attr_t);
576
577   type pthread_condattr_t is record
578      cv_magic   : unsigned;
579      cv_pshared : unsigned;
580   end record;
581   pragma Convention (C, pthread_condattr_t);
582
583   type pthread_mutexattr_t is record
584      m_flags   : unsigned;
585      m_prio_c  : int;
586      m_pshared : int;
587   end record;
588   pragma Convention (C, pthread_mutexattr_t);
589
590   type tid_t is new short;
591   type pthread_t is new tid_t;
592
593   type block_obj_t is record
594      b_head : int;
595   end record;
596   pragma Convention (C, block_obj_t);
597
598   type pthread_mutex_t is record
599      m_flags      : unsigned;
600      m_owner      : tid_t;
601      m_wait       : block_obj_t;
602      m_prio_c     : int;
603      m_oldprio    : int;
604      m_count      : int;
605      m_referenced : int;
606   end record;
607   pragma Convention (C, pthread_mutex_t);
608   type pthread_mutex_t_ptr is access all pthread_mutex_t;
609
610   type pthread_cond_t is record
611      cv_magic   : unsigned;
612      cv_wait    : block_obj_t;
613      cv_mutex   : pthread_mutex_t_ptr;
614      cv_refcnt  : int;
615   end record;
616   pragma Convention (C, pthread_cond_t);
617
618   type pthread_key_t is new int;
619
620end System.OS_Interface;
621