1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . O S _ I N T E R F A C E --
9 -- Copyright (C) 1991-1994, Florida State University --
10 -- Copyright (C) 1995-2024, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
28 -- GNARL was developed by the GNARL team at Florida State University. --
29 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 ------------------------------------------------------------------------------
33 -- This is a LynxOS-178 Elf (POSIX-8 Threads) version of this package
35 -- This package encapsulates all direct interfaces to OS services that are
36 -- needed by the tasking run-time (libgnarl).
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.
41 with Ada
.Unchecked_Conversion
;
45 with System
.Multiprocessors
;
46 with System
.Parameters
;
48 package System
.OS_Interface
is
51 subtype int
is Interfaces
.C
.int
;
52 subtype short
is Interfaces
.C
.short
;
53 subtype long
is Interfaces
.C
.long
;
54 subtype unsigned
is Interfaces
.C
.unsigned
;
55 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
56 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
57 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
58 subtype plain_char
is Interfaces
.C
.plain_char
;
59 subtype size_t
is Interfaces
.C
.size_t
;
60 subtype int64
is Interfaces
.Integer_64
;
66 function errno
return int
;
67 pragma Import
(C
, errno
, "__get_errno");
69 EAGAIN
: constant := 11;
70 EINTR
: constant := 4;
71 EINVAL
: constant := 22;
72 ENOMEM
: constant := 12;
73 ETIMEDOUT
: constant := 60;
80 Max_Interrupt
: constant := 63;
81 -- Max_Interrupt is the number of OS signals, as defined in:
83 -- /usr/include/sys/signal.h
85 -- The lowest numbered signal is 1, but 0 is a valid argument to some
86 -- library functions, e.g. kill(2). However, 0 is not just another signal:
87 -- For instance 'I in Signal' and similar should be used with caution.
89 type Signal
is new int
range 0 .. Max_Interrupt
;
90 for Signal
'Size use int
'Size;
92 SIGHUP
: constant := 1; -- hangup
93 SIGINT
: constant := 2; -- interrupt (rubout)
94 SIGQUIT
: constant := 3; -- quit (ASCD FS)
95 SIGILL
: constant := 4; -- illegal instruction (not reset)
96 SIGTRAP
: constant := 5; -- trace trap (not reset)
97 SIGBRK
: constant := 6; -- break
98 SIGIOT
: constant := 6; -- IOT instruction
99 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in future
100 SIGCORE
: constant := 7; -- kill with core dump
101 SIGEMT
: constant := 7; -- EMT instruction
102 SIGFPE
: constant := 8; -- floating point exception
103 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
104 SIGBUS
: constant := 10; -- bus error
105 SIGSEGV
: constant := 11; -- segmentation violation
106 SIGSYS
: constant := 12; -- bad argument to system call
107 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
108 SIGALRM
: constant := 14; -- alarm clock
109 SIGTERM
: constant := 15; -- software termination signal from kill
110 SIGURG
: constant := 16; -- urgent condition on IO channel
111 SIGSTOP
: constant := 17; -- stop (cannot be caught or ignored)
112 SIGTSTP
: constant := 18; -- user stop requested from tty
113 SIGCONT
: constant := 19; -- stopped process has been continued
114 SIGCLD
: constant := 20; -- alias for SIGCHLD
115 SIGCHLD
: constant := 20; -- child status change
116 SIGTTIN
: constant := 21; -- background tty read attempted
117 SIGTTOU
: constant := 22; -- background tty write attempted
118 SIGIO
: constant := 23; -- I/O possible (Solaris SIGPOLL alias)
119 SIGPOLL
: constant := 23; -- pollable event occurred
120 SIGTHREADKILL
: constant := 24; -- Reserved by LynxOS runtime
121 SIGXCPU
: constant := 24; -- CPU time limit exceeded
122 SIGXFSZ
: constant := 25; -- filesize limit exceeded
123 SIGVTALRM
: constant := 26; -- virtual timer expired
124 SIGPROF
: constant := 27; -- profiling timer expired
125 SIGWINCH
: constant := 28; -- window size change
126 SIGLOST
: constant := 29; -- SUN 4.1 compatibility
127 SIGUSR1
: constant := 30; -- user defined signal 1
128 SIGUSR2
: constant := 31; -- user defined signal 2
130 SIGPRIO
: constant := 32;
131 -- Sent to a process with its priority or group is changed
133 SIGADAABORT
: constant := SIGABRT
;
134 -- Change this if you want to use another signal for task abort. SIGTERM
135 -- might be a good one.
137 type Signal_Set
is array (Natural range <>) of Signal
;
139 Unmasked
: constant Signal_Set
:=
140 (SIGTRAP
, SIGTTIN
, SIGTTOU
, SIGTSTP
, SIGPROF
, SIGTHREADKILL
);
141 Reserved
: constant Signal_Set
:= (SIGABRT
, SIGKILL
, SIGSTOP
, SIGPRIO
);
143 type sigset_t
is private;
145 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
146 pragma Import
(C
, sigaddset
, "sigaddset");
148 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
149 pragma Import
(C
, sigdelset
, "sigdelset");
151 function sigfillset
(set
: access sigset_t
) return int
;
152 pragma Import
(C
, sigfillset
, "sigfillset");
154 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
155 pragma Import
(C
, sigismember
, "sigismember");
157 function sigemptyset
(set
: access sigset_t
) return int
;
158 pragma Import
(C
, sigemptyset
, "sigemptyset");
160 type struct_sigaction
is record
161 sa_handler
: System
.Address
;
165 pragma Convention
(C
, struct_sigaction
);
166 type struct_sigaction_ptr
is access all struct_sigaction
;
168 SA_SIGINFO
: constant := 16#
80#
;
170 SA_ONSTACK
: constant := 16#
00#
;
171 -- SA_ONSTACK is not defined on LynxOS, but it is referred to in the POSIX
172 -- implementation of System.Interrupt_Management. Therefore we define a
173 -- dummy value of zero here so that setting this flag is a nop.
175 SIG_BLOCK
: constant := 0;
176 SIG_UNBLOCK
: constant := 1;
177 SIG_SETMASK
: constant := 2;
179 SIG_DFL
: constant := 0;
180 SIG_IGN
: constant := 1;
184 act
: struct_sigaction_ptr
;
185 oact
: struct_sigaction_ptr
) return int
;
186 pragma Import
(C
, sigaction
, "sigaction");
192 Time_Slice_Supported
: constant Boolean := True;
193 -- Indicates whether time slicing is supported
195 type timespec
is private;
197 type clockid_t
is new int
;
199 function clock_gettime
200 (clock_id
: clockid_t
;
201 tp
: access timespec
) return int
;
202 pragma Import
(C
, clock_gettime
, "clock_gettime");
204 function clock_getres
205 (clock_id
: clockid_t
;
206 res
: access timespec
) return int
;
207 pragma Import
(C
, clock_getres
, "clock_getres");
209 function To_Duration
(TS
: timespec
) return Duration;
210 pragma Inline
(To_Duration
);
212 function To_Timespec
(D
: Duration) return timespec
;
213 pragma Inline
(To_Timespec
);
215 type struct_timezone
is record
216 tz_minuteswest
: int
;
219 pragma Convention
(C
, struct_timezone
);
220 type struct_timezone_ptr
is access all struct_timezone
;
222 type struct_timeval
is private;
224 -------------------------
225 -- Priority Scheduling --
226 -------------------------
228 SCHED_RR
: constant := 16#
100_000#
;
229 SCHED_FIFO
: constant := 16#
200_000#
;
230 SCHED_OTHER
: constant := 16#
400_000#
;
232 function To_Target_Priority
233 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
234 -- Maps System.Any_Priority to a POSIX priority
240 type pid_t
is private;
242 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
243 pragma Import
(C
, kill
, "kill");
245 function getpid
return pid_t
;
246 pragma Import
(C
, getpid
, "getpid");
252 type pthread_t
is private;
254 function lwp_self
return pthread_t
;
255 pragma Import
(C
, lwp_self
, "pthread_self");
261 type Thread_Body
is access
262 function (arg
: System
.Address
) return System
.Address
;
263 pragma Convention
(C
, Thread_Body
);
265 function Thread_Body_Access
is new
266 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
268 subtype Thread_Id
is pthread_t
;
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;
277 PTHREAD_CREATE_DETACHED
: constant := 1;
278 PTHREAD_CREATE_JOINABLE
: constant := 0;
280 PTHREAD_SCOPE_PROCESS
: constant := 0; -- not supported by LynxOS178
281 PTHREAD_SCOPE_SYSTEM
: constant := 1;
283 -- Read/Write lock not supported on LynxOS. To add support both types
284 -- pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
285 -- with the associated routines pthread_rwlock_[init/destroy] and
286 -- pthread_rwlock_[rdlock/wrlock/unlock].
288 subtype pthread_rwlock_t
is pthread_mutex_t
;
289 subtype pthread_rwlockattr_t
is pthread_mutexattr_t
;
295 type stack_t
is record
296 ss_sp
: System
.Address
;
300 pragma Convention
(C
, stack_t
);
303 (ss
: not null access stack_t
;
304 oss
: access stack_t
) return int
;
305 pragma Import
(C
, sigaltstack
, "sigaltstack");
306 -- Neither stack_t nor sigaltstack are available on LynxOS-178
308 Alternate_Stack
: aliased System
.Address
;
309 -- This is a dummy definition, never used (Alternate_Stack_Size is 0)
311 Alternate_Stack_Size
: constant := 0;
312 -- No alternate signal stack is used on this platform
314 Stack_Base_Available
: constant Boolean := False;
315 -- Indicates whether the stack base is available on this target
317 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
318 pragma Inline
(Get_Stack_Base
);
319 -- Returns the stack base of the specified thread. Only call this function
320 -- when Stack_Base_Available is True.
322 function Get_Page_Size
return int
;
323 -- Returns the size of a page in bytes
325 PROT_NONE
: constant := 1;
326 PROT_READ
: constant := 2;
327 PROT_WRITE
: constant := 4;
328 PROT_EXEC
: constant := 8;
329 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
331 PROT_ON
: constant := PROT_READ
;
332 PROT_OFF
: constant := PROT_ALL
;
334 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
335 pragma Import
(C
, mprotect
);
337 ---------------------------------------
338 -- Nonstandard Thread Initialization --
339 ---------------------------------------
341 procedure pthread_init
;
342 -- This is a dummy procedure to share some GNULLI files
344 -------------------------
345 -- POSIX.1c Section 3 --
346 -------------------------
348 (set
: access sigset_t
;
349 sig
: access Signal
) return int
;
350 pragma Inline
(sigwait
);
351 -- LynxOS has non standard sigwait
353 function pthread_kill
355 sig
: Signal
) return int
;
356 pragma Import
(C
, pthread_kill
, "pthread_kill");
358 function pthread_sigmask
360 set
: access sigset_t
;
361 oset
: access sigset_t
) return int
;
362 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
364 ----------------------------
365 -- POSIX.1c Section 11 --
366 ----------------------------
368 function pthread_mutexattr_init
369 (attr
: access pthread_mutexattr_t
) return int
;
370 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
372 function pthread_mutexattr_destroy
373 (attr
: access pthread_mutexattr_t
) return int
;
374 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
376 function pthread_mutex_init
377 (mutex
: access pthread_mutex_t
;
378 attr
: access pthread_mutexattr_t
) return int
;
379 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
381 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
382 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
384 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
385 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
387 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
388 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
390 function pthread_condattr_init
391 (attr
: access pthread_condattr_t
) return int
;
392 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
394 function pthread_condattr_destroy
395 (attr
: access pthread_condattr_t
) return int
;
396 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
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");
403 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
404 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
406 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
407 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
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");
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");
420 --------------------------
421 -- POSIX.1c Section 13 --
422 --------------------------
424 PTHREAD_PRIO_NONE
: constant := 0;
425 PTHREAD_PRIO_INHERIT
: constant := 1;
426 PTHREAD_PRIO_PROTECT
: constant := 2;
428 function pthread_mutexattr_setprotocol
429 (attr
: access pthread_mutexattr_t
;
430 protocol
: int
) return int
;
431 pragma Import
(C
, pthread_mutexattr_setprotocol
);
433 function pthread_mutexattr_setprioceiling
434 (attr
: access pthread_mutexattr_t
;
435 prioceiling
: int
) return int
;
436 pragma Import
(C
, pthread_mutexattr_setprioceiling
);
438 type struct_sched_param
is record
439 sched_priority
: int
;
441 pragma Convention
(C
, struct_sched_param
);
443 function pthread_setschedparam
446 param
: access struct_sched_param
) return int
;
447 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
449 function pthread_attr_setscope
450 (Unused_attr
: access pthread_attr_t
;
451 Unused_contentionscope
: int
) return int
is (0);
452 -- pthread_attr_setscope is not implemented in production mode
454 function pthread_attr_setinheritsched
455 (attr
: access pthread_attr_t
;
456 inheritsched
: int
) return int
;
457 pragma Import
(C
, pthread_attr_setinheritsched
);
459 function pthread_attr_setschedpolicy
460 (attr
: access pthread_attr_t
;
461 policy
: int
) return int
;
462 pragma Import
(C
, pthread_attr_setschedpolicy
);
464 function sched_yield
return int
;
465 pragma Import
(C
, sched_yield
, "sched_yield");
467 --------------------------
468 -- P1003.1c Section 16 --
469 --------------------------
471 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
472 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
474 function pthread_attr_destroy
475 (attributes
: access pthread_attr_t
) return int
;
476 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
478 function pthread_attr_setdetachstate
479 (attr
: access pthread_attr_t
;
480 detachstate
: int
) return int
;
481 pragma Import
(C
, pthread_attr_setdetachstate
);
483 function pthread_attr_setstacksize
484 (attr
: access pthread_attr_t
;
485 stacksize
: size_t
) return int
;
486 pragma Import
(C
, pthread_attr_setstacksize
);
488 function pthread_create
489 (thread
: access pthread_t
;
490 attributes
: access pthread_attr_t
;
491 start_routine
: Thread_Body
;
492 arg
: System
.Address
) return int
;
493 pragma Import
(C
, pthread_create
, "pthread_create");
495 procedure pthread_exit
(status
: System
.Address
);
496 pragma Import
(C
, pthread_exit
, "pthread_exit");
498 function pthread_self
return pthread_t
;
499 pragma Import
(C
, pthread_self
, "pthread_self");
501 --------------------------
502 -- POSIX.1c Section 17 --
503 --------------------------
505 function pthread_setspecific
506 (key
: pthread_key_t
;
507 value
: System
.Address
) return int
;
508 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
510 function pthread_getspecific
511 (key
: pthread_key_t
) return System
.Address
;
512 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
514 type destructor_pointer
is access procedure (arg
: System
.Address
);
515 pragma Convention
(C
, destructor_pointer
);
517 function pthread_key_create
518 (key
: access pthread_key_t
;
519 destructor
: destructor_pointer
521 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
523 ---------------------
524 -- Multiprocessors --
525 ---------------------
527 function Current_CPU
return Multiprocessors
.CPU
;
528 -- Return the id of the current CPU
530 function Get_Affinity
(Id
: Thread_Id
) return Multiprocessors
.CPU_Range
;
531 -- Return CPU affinity of the given thread (maybe Not_A_Specific_CPU)
533 function Get_CPU
(Id
: Thread_Id
) return Multiprocessors
.CPU
;
534 -- Return the CPU in charge of the given thread (always a valid CPU)
538 type sigset_t
is array (1 .. 2) of long
;
539 pragma Convention
(C
, sigset_t
);
541 type pid_t
is new long
;
543 type time_t
is range -2 ** (System
.Parameters
.time_t_bits
- 1)
544 .. 2 ** (System
.Parameters
.time_t_bits
- 1) - 1;
546 type suseconds_t
is new int
;
548 type timespec
is record
552 pragma Convention
(C
, timespec
);
554 type struct_timeval
is record
556 tv_usec
: suseconds_t
;
558 pragma Convention
(C
, struct_timeval
);
560 type st_attr
is record
569 pragma Convention
(C
, st_attr
);
570 subtype st_attr_t
is st_attr
;
572 type pthread_attr_t
is record
573 pthread_attr_magic
: unsigned
;
575 pthread_attr_scope
: int
;
577 pragma Convention
(C
, pthread_attr_t
);
579 type pthread_condattr_t
is record
581 cv_pshared
: unsigned
;
583 pragma Convention
(C
, pthread_condattr_t
);
585 type pthread_mutexattr_t
is record
590 pragma Convention
(C
, pthread_mutexattr_t
);
592 type tid_t
is new short
;
593 type pthread_t
is new tid_t
;
595 type block_obj_t
is record
598 pragma Convention
(C
, block_obj_t
);
600 type pthread_mutex_t
is record
603 m_wait
: block_obj_t
;
609 pragma Convention
(C
, pthread_mutex_t
);
610 type pthread_mutex_t_ptr
is access all pthread_mutex_t
;
612 type pthread_cond_t
is record
614 cv_wait
: block_obj_t
;
615 cv_mutex
: pthread_mutex_t_ptr
;
618 pragma Convention
(C
, pthread_cond_t
);
620 type pthread_key_t
is new int
;
622 end System
.OS_Interface
;