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) 1995-2024, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a QNX/Neutrino version of this package
34 -- This package encapsulates all direct interfaces to OS services
35 -- that are needed by the tasking run-time (libgnarl).
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.
40 with Ada
.Unchecked_Conversion
;
42 with System
.OS_Constants
;
43 with System
.Parameters
;
45 package System
.OS_Interface
is
48 subtype int
is Interfaces
.C
.int
;
49 subtype char
is Interfaces
.C
.char
;
50 subtype short
is Interfaces
.C
.short
;
51 subtype long
is Interfaces
.C
.long
;
52 subtype unsigned
is Interfaces
.C
.unsigned
;
53 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
54 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
55 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
56 subtype plain_char
is Interfaces
.C
.plain_char
;
57 subtype size_t
is Interfaces
.C
.size_t
;
63 function errno
return int
;
64 pragma Import
(C
, errno
, "__get_errno");
66 EPERM
: constant := 1;
67 EINTR
: constant := 4;
68 EAGAIN
: constant := 11;
69 ENOMEM
: constant := 12;
70 EINVAL
: constant := 22;
71 ETIMEDOUT
: constant := 260;
77 Max_Interrupt
: constant := 64;
78 type Signal
is new int
range 0 .. Max_Interrupt
;
79 for Signal
'Size use int
'Size;
81 SIGHUP
: constant := 1;
82 SIGINT
: constant := 2;
83 SIGQUIT
: constant := 3;
84 SIGILL
: constant := 4;
85 SIGTRAP
: constant := 5;
86 SIGIOT
: constant := 6;
87 SIGABRT
: constant := 6;
88 SIGDEADLK
: constant := 7;
89 SIGFPE
: constant := 8;
90 SIGKILL
: constant := 9;
91 SIGBUS
: constant := 10;
92 SIGSEGV
: constant := 11;
93 SIGSYS
: constant := 12;
94 SIGPIPE
: constant := 13;
95 SIGALRM
: constant := 14;
96 SIGTERM
: constant := 15;
97 SIGUSR1
: constant := 16;
98 SIGUSR2
: constant := 17;
99 SIGCLD
: constant := 18;
100 SIGCHLD
: constant := 18;
101 SIGPWR
: constant := 19;
102 SIGWINCH
: constant := 20;
103 SIGURG
: constant := 21;
104 SIGPOLL
: constant := 22;
105 SIGIO
: constant := 22;
106 SIGSTOP
: constant := 23;
107 SIGTSTP
: constant := 24;
108 SIGCONT
: constant := 25;
109 SIGTTIN
: constant := 26;
110 SIGTTOU
: constant := 27;
111 SIGVTALRM
: constant := 28;
112 SIGPROF
: constant := 29;
113 SIGXCPU
: constant := 30;
114 SIGXFSZ
: constant := 31;
116 SIGRTMIN
: constant := 41;
117 SITRTMAX
: constant := 56;
119 SIGSELECT
: constant := 57;
120 SIGPHOTON
: constant := 58;
122 SIGADAABORT
: constant := SIGABRT
;
123 -- Change this to use another signal for task abort. SIGTERM might be a
126 type Signal_Set
is array (Natural range <>) of Signal
;
128 Unmasked
: constant Signal_Set
:= (
130 -- To enable debugging on multithreaded applications, mark SIGTRAP to
135 SIGTTIN
, SIGTTOU
, SIGTSTP
,
136 -- Keep these three signals unmasked so that background processes and IO
137 -- behaves as normal "C" applications
140 -- To avoid confusing the profiler
143 -- These two signals actually can't be masked (POSIX won't allow it)
145 Reserved
: constant Signal_Set
:= (SIGABRT
, SIGKILL
, SIGSTOP
, SIGSEGV
);
147 type sigset_t
is private;
149 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
150 pragma Import
(C
, sigaddset
, "sigaddset");
152 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
153 pragma Import
(C
, sigdelset
, "sigdelset");
155 function sigfillset
(set
: access sigset_t
) return int
;
156 pragma Import
(C
, sigfillset
, "sigfillset");
158 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
159 pragma Import
(C
, sigismember
, "sigismember");
161 function sigemptyset
(set
: access sigset_t
) return int
;
162 pragma Import
(C
, sigemptyset
, "sigemptyset");
164 type pad7
is array (1 .. 7) of int
;
165 type siginfo_t
is record
171 pragma Convention
(C
, siginfo_t
);
173 type struct_sigaction
is record
174 sa_handler
: System
.Address
;
178 pragma Convention
(C
, struct_sigaction
);
180 type struct_sigaction_ptr
is access all struct_sigaction
;
182 SIG_BLOCK
: constant := 0;
183 SIG_UNBLOCK
: constant := 1;
184 SIG_SETMASK
: constant := 2;
185 SIG_PENDING
: constant := 5;
187 SA_NOCLDSTOP
: constant := 16#
0001#
;
188 SA_SIGINFO
: constant := 16#
0002#
;
189 SA_RESETHAND
: constant := 16#
0004#
;
190 SA_ONSTACK
: constant := 16#
0008#
;
191 SA_NODEFER
: constant := 16#
0010#
;
192 SA_NOCLDWAIT
: constant := 16#
0020#
;
194 SS_ONSTACK
: constant := 1;
195 SS_DISABLE
: constant := 2;
197 SIG_DFL
: constant := 0;
198 SIG_IGN
: constant := 1;
202 act
: struct_sigaction_ptr
;
203 oact
: struct_sigaction_ptr
) return int
;
204 pragma Import
(C
, sigaction
, "sigaction");
210 Time_Slice_Supported
: constant Boolean := True;
211 -- Indicates whether time slicing is supported
213 type timespec
is private;
215 type clockid_t
is new int
;
217 function clock_gettime
218 (clock_id
: clockid_t
; tp
: access timespec
) return int
;
219 pragma Import
(C
, clock_gettime
, "clock_gettime");
221 function clock_getres
222 (clock_id
: clockid_t
;
223 res
: access timespec
) return int
;
224 pragma Import
(C
, clock_getres
, "clock_getres");
226 function To_Duration
(TS
: timespec
) return Duration;
227 pragma Inline
(To_Duration
);
229 function To_Timespec
(D
: Duration) return timespec
;
230 pragma Inline
(To_Timespec
);
232 -------------------------
233 -- Priority Scheduling --
234 -------------------------
236 SCHED_FIFO
: constant := 1;
237 SCHED_RR
: constant := 2;
238 SCHED_OTHER
: constant := 3;
240 function To_Target_Priority
241 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
243 -- Maps System.Any_Priority to a POSIX priority
249 type pid_t
is private;
251 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
252 pragma Import
(C
, kill
, "kill");
254 function getpid
return pid_t
;
255 pragma Import
(C
, getpid
, "getpid");
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 type pthread_t
is new int
;
269 subtype Thread_Id
is pthread_t
;
271 type pthread_mutex_t
is limited private;
272 type pthread_cond_t
is limited private;
273 type pthread_attr_t
is limited private;
274 type pthread_mutexattr_t
is limited private;
275 type pthread_condattr_t
is limited private;
276 type pthread_key_t
is private;
278 PTHREAD_CREATE_DETACHED
: constant := 1;
280 PTHREAD_SCOPE_PROCESS
: constant := 4;
281 PTHREAD_SCOPE_SYSTEM
: constant := 0;
283 PTHREAD_INHERIT_SCHED
: constant := 0;
284 PTHREAD_EXPLICIT_SCHED
: constant := 2;
286 -- Read/Write lock not supported on Android.
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
306 -- Not supported on QNX
308 Alternate_Stack
: aliased System
.Address
;
309 -- Dummy definition: alternate stack not available due to missing
310 -- sigaltstack in QNX
312 Alternate_Stack_Size
: constant := 0;
313 -- This must be kept in sync with init.c:__gnat_alternate_stack
315 Stack_Base_Available
: constant Boolean := False;
316 -- Indicates whether the stack base is available on this target
318 function Get_Stack_Base
(thread
: pthread_t
) return System
.Address
320 -- This is a dummy procedure to share some GNULLI files
322 function Get_Page_Size
return int
;
323 pragma Import
(C
, Get_Page_Size
, "getpagesize");
324 -- Returns the size of a page
326 PROT_NONE
: constant := 16#
00_00#
;
327 PROT_READ
: constant := 16#
01_00#
;
328 PROT_WRITE
: constant := 16#
02_00#
;
329 PROT_EXEC
: constant := 16#
04_00#
;
330 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
with Inline_Always
;
343 -------------------------
344 -- POSIX.1c Section 3 --
345 -------------------------
347 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
348 pragma Import
(C
, sigwait
, "sigwait");
350 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
351 pragma Import
(C
, pthread_kill
, "pthread_kill");
353 function pthread_sigmask
355 set
: access sigset_t
;
356 oset
: access sigset_t
) return int
;
357 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
359 --------------------------
360 -- POSIX.1c Section 11 --
361 --------------------------
363 function pthread_mutexattr_init
364 (attr
: access pthread_mutexattr_t
) return int
;
365 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
367 function pthread_mutexattr_destroy
368 (attr
: access pthread_mutexattr_t
) return int
;
369 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
371 function pthread_mutex_init
372 (mutex
: access pthread_mutex_t
;
373 attr
: access pthread_mutexattr_t
) return int
;
374 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
376 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
377 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
379 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
380 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
382 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
383 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
385 function pthread_mutex_setprioceiling
386 (mutex
: access pthread_mutex_t
;
388 old_ceiling
: access int
) return int
;
389 pragma Import
(C
, pthread_mutex_setprioceiling
);
391 function pthread_condattr_init
392 (attr
: access pthread_condattr_t
) return int
;
393 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
395 function pthread_condattr_destroy
396 (attr
: access pthread_condattr_t
) return int
;
397 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
399 function pthread_cond_init
400 (cond
: access pthread_cond_t
;
401 attr
: access pthread_condattr_t
) return int
;
402 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
404 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
405 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
407 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
408 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
410 function pthread_cond_wait
411 (cond
: access pthread_cond_t
;
412 mutex
: access pthread_mutex_t
) return int
;
413 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
415 function pthread_cond_timedwait
416 (cond
: access pthread_cond_t
;
417 mutex
: access pthread_mutex_t
;
418 abstime
: access timespec
) return int
;
419 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
421 --------------------------
422 -- POSIX.1c Section 13 --
423 --------------------------
425 PTHREAD_PRIO_INHERIT
: constant := 0;
426 PTHREAD_PRIO_NONE
: constant := 1;
427 PTHREAD_PRIO_PROTECT
: constant := 2;
429 function pthread_mutexattr_setprotocol
430 (attr
: access pthread_mutexattr_t
;
431 protocol
: int
) return int
;
432 pragma Import
(C
, pthread_mutexattr_setprotocol
);
434 function pthread_mutexattr_getprotocol
435 (attr
: access pthread_mutexattr_t
;
436 protocol
: access int
) return int
;
437 pragma Import
(C
, pthread_mutexattr_getprotocol
);
439 function pthread_mutexattr_setprioceiling
440 (attr
: access pthread_mutexattr_t
;
441 prioceiling
: int
) return int
;
442 pragma Import
(C
, pthread_mutexattr_setprioceiling
);
444 function pthread_mutexattr_getprioceiling
445 (attr
: access pthread_mutexattr_t
;
446 prioceiling
: access int
) return int
;
447 pragma Import
(C
, pthread_mutexattr_getprioceiling
);
449 function pthread_mutex_getprioceiling
450 (attr
: access pthread_mutex_t
;
451 prioceiling
: access int
) return int
;
452 pragma Import
(C
, pthread_mutex_getprioceiling
);
454 type pad8
is array (1 .. 8) of int
;
455 pragma Convention
(C
, pad8
);
457 type struct_sched_param
is record
458 sched_priority
: int
:= 0; -- scheduling priority
459 sched_curpriority
: int
:= 0;
460 reserved
: pad8
:= (others => 0);
462 pragma Convention
(C
, struct_sched_param
);
464 function pthread_setschedparam
467 param
: access struct_sched_param
) return int
;
468 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
470 function pthread_getschedparam
473 param
: access struct_sched_param
) return int
;
474 pragma Import
(C
, pthread_getschedparam
, "pthread_getschedparam");
476 function pthread_setschedprio
478 priority
: int
) return int
;
479 pragma Import
(C
, pthread_setschedprio
);
481 function pthread_attr_setschedparam
482 (attr
: access pthread_attr_t
;
483 param
: access struct_sched_param
) return int
;
484 pragma Import
(C
, pthread_attr_setschedparam
);
486 function pthread_attr_setinheritsched
487 (attr
: access pthread_attr_t
;
488 inheritsched
: int
) return int
;
489 pragma Import
(C
, pthread_attr_setinheritsched
);
491 function pthread_attr_setscope
492 (attr
: access pthread_attr_t
;
493 scope
: int
) return int
;
494 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
496 function pthread_attr_setschedpolicy
497 (attr
: access pthread_attr_t
;
498 policy
: int
) return int
;
500 (C
, pthread_attr_setschedpolicy
, "pthread_attr_setschedpolicy");
502 function sched_yield
return int
;
503 pragma Import
(C
, sched_yield
, "sched_yield");
505 ---------------------------
506 -- P1003.1c - Section 16 --
507 ---------------------------
509 function pthread_attr_init
510 (attributes
: access pthread_attr_t
) return int
;
511 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
513 function pthread_attr_destroy
514 (attributes
: access pthread_attr_t
) return int
;
515 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
517 function pthread_attr_setdetachstate
518 (attr
: access pthread_attr_t
;
519 detachstate
: int
) return int
;
520 pragma Import
(C
, pthread_attr_setdetachstate
);
522 function pthread_attr_setstacksize
523 (attr
: access pthread_attr_t
;
524 stacksize
: size_t
) return int
;
525 pragma Import
(C
, pthread_attr_setstacksize
);
527 function pthread_create
528 (thread
: access pthread_t
;
529 attributes
: access pthread_attr_t
;
530 start_routine
: Thread_Body
;
531 arg
: System
.Address
) return int
;
532 pragma Import
(C
, pthread_create
, "pthread_create");
534 procedure pthread_exit
(status
: System
.Address
);
535 pragma Import
(C
, pthread_exit
, "pthread_exit");
537 function pthread_self
return pthread_t
;
538 pragma Import
(C
, pthread_self
, "pthread_self");
540 function lwp_self
return System
.Address
;
541 pragma Import
(C
, lwp_self
, "pthread_self");
543 --------------------------
544 -- POSIX.1c Section 17 --
545 --------------------------
547 function pthread_setspecific
548 (key
: pthread_key_t
;
549 value
: System
.Address
) return int
;
550 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
552 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
553 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
555 type destructor_pointer
is access procedure (arg
: System
.Address
);
556 pragma Convention
(C
, destructor_pointer
);
558 function pthread_key_create
559 (key
: access pthread_key_t
;
560 destructor
: destructor_pointer
) return int
;
561 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
566 array (0 .. OS_Constants
.SIZEOF_sigset
- 1) of unsigned_char
;
567 pragma Convention
(C
, sigset_t
);
568 for sigset_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
570 type pid_t
is new int
;
572 type time_t
is range -2 ** (System
.Parameters
.time_t_bits
- 1)
573 .. 2 ** (System
.Parameters
.time_t_bits
- 1) - 1;
575 type timespec
is record
579 pragma Convention
(C
, timespec
);
581 type unsigned_long_long_t
is mod 2 ** 64;
582 -- Local type only used to get the alignment of this type below
584 subtype char_array
is Interfaces
.C
.char_array
;
586 type pthread_attr_t
is record
587 Data
: char_array
(1 .. OS_Constants
.PTHREAD_ATTR_SIZE
);
589 pragma Convention
(C
, pthread_attr_t
);
590 for pthread_attr_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
592 type pthread_condattr_t
is record
593 Data
: char_array
(1 .. OS_Constants
.PTHREAD_CONDATTR_SIZE
);
595 pragma Convention
(C
, pthread_condattr_t
);
596 for pthread_condattr_t
'Alignment use Interfaces
.C
.int
'Alignment;
598 type pthread_mutexattr_t
is record
599 Data
: char_array
(1 .. OS_Constants
.PTHREAD_MUTEXATTR_SIZE
);
601 pragma Convention
(C
, pthread_mutexattr_t
);
602 for pthread_mutexattr_t
'Alignment use Interfaces
.C
.int
'Alignment;
604 type pthread_mutex_t
is record
605 Data
: char_array
(1 .. OS_Constants
.PTHREAD_MUTEX_SIZE
);
607 pragma Convention
(C
, pthread_mutex_t
);
608 for pthread_mutex_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
610 type pthread_cond_t
is record
611 Data
: char_array
(1 .. OS_Constants
.PTHREAD_COND_SIZE
);
613 pragma Convention
(C
, pthread_cond_t
);
614 for pthread_cond_t
'Alignment use unsigned_long_long_t
'Alignment;
616 type pthread_key_t
is new int
;
618 end System
.OS_Interface
;