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-2017, Florida State University --
10 -- Copyright (C) 1995-2023, 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 AIX (Native 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
;
44 with Interfaces
.C
.Extensions
;
46 with System
.Parameters
;
48 package System
.OS_Interface
is
51 pragma Linker_Options
("-pthread");
52 -- This implies -lpthreads + other things depending on the GCC
53 -- configuration, such as the selection of a proper libgcc variant
54 -- for table-based exception handling when it is available.
56 pragma Linker_Options
("-lc_r");
58 subtype int
is Interfaces
.C
.int
;
59 subtype short
is Interfaces
.C
.short
;
60 subtype long
is Interfaces
.C
.long
;
61 subtype long_long
is Interfaces
.C
.Extensions
.long_long
;
62 subtype unsigned
is Interfaces
.C
.unsigned
;
63 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
64 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
65 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
66 subtype plain_char
is Interfaces
.C
.plain_char
;
67 subtype size_t
is Interfaces
.C
.size_t
;
73 function errno
return int
;
74 pragma Import
(C
, errno
, "__get_errno");
76 EAGAIN
: constant := 11;
77 EINTR
: constant := 4;
78 EINVAL
: constant := 22;
79 ENOMEM
: constant := 12;
80 ETIMEDOUT
: constant := 78;
86 Max_Interrupt
: constant := 63;
87 type Signal
is new int
range 0 .. Max_Interrupt
;
88 for Signal
'Size use int
'Size;
90 SIGHUP
: constant := 1; -- hangup
91 SIGINT
: constant := 2; -- interrupt (rubout)
92 SIGQUIT
: constant := 3; -- quit (ASCD FS)
93 SIGILL
: constant := 4; -- illegal instruction (not reset)
94 SIGTRAP
: constant := 5; -- trace trap (not reset)
95 SIGIOT
: constant := 6; -- IOT instruction
96 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in the future
97 SIGEMT
: constant := 7; -- EMT instruction
98 SIGFPE
: constant := 8; -- floating point exception
99 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
100 SIGBUS
: constant := 10; -- bus error
101 SIGSEGV
: constant := 11; -- segmentation violation
102 SIGSYS
: constant := 12; -- bad argument to system call
103 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
104 SIGALRM
: constant := 14; -- alarm clock
105 SIGTERM
: constant := 15; -- software termination signal from kill
106 SIGUSR1
: constant := 30; -- user defined signal 1
107 SIGUSR2
: constant := 31; -- user defined signal 2
108 SIGCLD
: constant := 20; -- alias for SIGCHLD
109 SIGCHLD
: constant := 20; -- child status change
110 SIGPWR
: constant := 29; -- power-fail restart
111 SIGWINCH
: constant := 28; -- window size change
112 SIGURG
: constant := 16; -- urgent condition on IO channel
113 SIGPOLL
: constant := 23; -- pollable event occurred
114 SIGIO
: constant := 23; -- I/O possible (Solaris SIGPOLL alias)
115 SIGSTOP
: constant := 17; -- stop (cannot be caught or ignored)
116 SIGTSTP
: constant := 18; -- user stop requested from tty
117 SIGCONT
: constant := 19; -- stopped process has been continued
118 SIGTTIN
: constant := 21; -- background tty read attempted
119 SIGTTOU
: constant := 22; -- background tty write attempted
120 SIGVTALRM
: constant := 34; -- virtual timer expired
121 SIGPROF
: constant := 32; -- profiling timer expired
122 SIGXCPU
: constant := 24; -- CPU time limit exceeded
123 SIGXFSZ
: constant := 25; -- filesize limit exceeded
124 SIGWAITING
: constant := 39; -- m:n scheduling
126 -- The following signals are AIX specific
128 SIGMSG
: constant := 27; -- input data is in the ring buffer
129 SIGDANGER
: constant := 33; -- system crash imminent
130 SIGMIGRATE
: constant := 35; -- migrate process
131 SIGPRE
: constant := 36; -- programming exception
132 SIGVIRT
: constant := 37; -- AIX virtual time alarm
133 SIGALRM1
: constant := 38; -- m:n condition variables
134 SIGCPUFAIL
: constant := 59; -- Predictive De-configuration of Processors
135 SIGKAP
: constant := 60; -- keep alive poll from native keyboard
136 SIGGRANT
: constant := SIGKAP
; -- monitor mode granted
137 SIGRETRACT
: constant := 61; -- monitor mode should be relinquished
138 SIGSOUND
: constant := 62; -- sound control has completed
139 SIGSAK
: constant := 63; -- secure attention key
141 SIGADAABORT
: constant := SIGEMT
;
142 -- Note: on other targets, we usually use SIGABRT, but on AIX, it appears
143 -- that SIGABRT can't be used in sigwait(), so we use SIGEMT.
144 -- SIGEMT is "Emulator Trap Instruction" from the PDP-11, and does not
145 -- have a standardized usage.
147 type Signal_Set
is array (Natural range <>) of Signal
;
149 Unmasked
: constant Signal_Set
:=
150 (SIGTRAP
, SIGTTIN
, SIGTTOU
, SIGTSTP
, SIGPROF
);
151 Reserved
: constant Signal_Set
:=
152 (SIGABRT
, SIGKILL
, SIGSTOP
, SIGALRM1
, SIGWAITING
, SIGCPUFAIL
);
154 type sigset_t
is private;
156 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
157 pragma Import
(C
, sigaddset
, "sigaddset");
159 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
160 pragma Import
(C
, sigdelset
, "sigdelset");
162 function sigfillset
(set
: access sigset_t
) return int
;
163 pragma Import
(C
, sigfillset
, "sigfillset");
165 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
166 pragma Import
(C
, sigismember
, "sigismember");
168 function sigemptyset
(set
: access sigset_t
) return int
;
169 pragma Import
(C
, sigemptyset
, "sigemptyset");
171 type struct_sigaction
is record
172 sa_handler
: System
.Address
;
176 pragma Convention
(C
, struct_sigaction
);
177 type struct_sigaction_ptr
is access all struct_sigaction
;
179 SA_SIGINFO
: constant := 16#
0100#
;
180 SA_ONSTACK
: constant := 16#
0001#
;
182 SIG_BLOCK
: constant := 0;
183 SIG_UNBLOCK
: constant := 1;
184 SIG_SETMASK
: constant := 2;
186 SIG_DFL
: constant := 0;
187 SIG_IGN
: constant := 1;
191 act
: struct_sigaction_ptr
;
192 oact
: struct_sigaction_ptr
) return int
;
193 pragma Import
(C
, sigaction
, "sigaction");
199 Time_Slice_Supported
: constant Boolean := True;
200 -- Indicates whether time slicing is supported
202 type timespec
is private;
204 type clockid_t
is new long_long
;
206 function clock_gettime
207 (clock_id
: clockid_t
;
208 tp
: access timespec
) return int
;
209 pragma Import
(C
, clock_gettime
, "clock_gettime");
211 function clock_getres
212 (clock_id
: clockid_t
;
213 res
: access timespec
) return int
;
214 pragma Import
(C
, clock_getres
, "clock_getres");
216 function To_Duration
(TS
: timespec
) return Duration;
217 pragma Inline
(To_Duration
);
219 function To_Timespec
(D
: Duration) return timespec
;
220 pragma Inline
(To_Timespec
);
222 type struct_timezone
is record
223 tz_minuteswest
: int
;
226 pragma Convention
(C
, struct_timezone
);
227 type struct_timezone_ptr
is access all struct_timezone
;
229 -------------------------
230 -- Priority Scheduling --
231 -------------------------
233 SCHED_FIFO
: constant := 1;
234 SCHED_RR
: constant := 2;
235 SCHED_OTHER
: constant := 0;
237 function To_Target_Priority
238 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
239 -- Maps System.Any_Priority to a POSIX priority
245 type pid_t
is private;
247 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
248 pragma Import
(C
, kill
, "kill");
250 function getpid
return pid_t
;
251 pragma Import
(C
, getpid
, "getpid");
257 function lwp_self
return System
.Address
;
258 pragma Import
(C
, lwp_self
, "thread_self");
264 type Thread_Body
is access
265 function (arg
: System
.Address
) return System
.Address
;
266 pragma Convention
(C
, Thread_Body
);
268 function Thread_Body_Access
is new
269 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
271 type pthread_t
is private;
272 subtype Thread_Id
is pthread_t
;
274 type pthread_mutex_t
is limited private;
275 type pthread_cond_t
is limited private;
276 type pthread_attr_t
is limited private;
277 type pthread_mutexattr_t
is limited private;
278 type pthread_condattr_t
is limited private;
279 type pthread_key_t
is private;
281 PTHREAD_CREATE_DETACHED
: constant := 1;
283 PTHREAD_SCOPE_PROCESS
: constant := 1;
284 PTHREAD_SCOPE_SYSTEM
: constant := 0;
286 -- Read/Write lock not supported on AIX. To add support both types
287 -- pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
288 -- with the associated routines pthread_rwlock_[init/destroy] and
289 -- pthread_rwlock_[rdlock/wrlock/unlock].
291 subtype pthread_rwlock_t
is pthread_mutex_t
;
292 subtype pthread_rwlockattr_t
is pthread_mutexattr_t
;
298 type stack_t
is record
299 ss_sp
: System
.Address
;
303 pragma Convention
(C
, stack_t
);
306 (ss
: not null access stack_t
;
307 oss
: access stack_t
) return int
;
308 pragma Import
(C
, sigaltstack
, "sigaltstack");
310 Alternate_Stack
: aliased System
.Address
;
311 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
313 Alternate_Stack_Size
: constant := 0;
314 -- No alternate signal stack is used on this platform
316 Stack_Base_Available
: constant Boolean := False;
317 -- Indicates whether the stack base is available on this target
319 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
320 pragma Inline
(Get_Stack_Base
);
321 -- Returns the stack base of the specified thread. Only call this function
322 -- when Stack_Base_Available is True.
324 function Get_Page_Size
return int
;
325 pragma Import
(C
, Get_Page_Size
, "getpagesize");
326 -- Returns the size of a page
328 PROT_NONE
: constant := 0;
329 PROT_READ
: constant := 1;
330 PROT_WRITE
: constant := 2;
331 PROT_EXEC
: constant := 4;
332 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
333 PROT_ON
: constant := PROT_READ
;
334 PROT_OFF
: constant := PROT_ALL
;
336 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
337 pragma Import
(C
, mprotect
);
339 ---------------------------------------
340 -- Nonstandard Thread Initialization --
341 ---------------------------------------
343 -- Though not documented, pthread_init *must* be called before any other
346 procedure pthread_init
;
347 pragma Import
(C
, pthread_init
, "pthread_init");
349 -------------------------
350 -- POSIX.1c Section 3 --
351 -------------------------
354 (set
: access sigset_t
;
355 sig
: access Signal
) return int
;
356 pragma Import
(C
, sigwait
, "sigwait");
358 function pthread_kill
360 sig
: Signal
) return int
;
361 pragma Import
(C
, pthread_kill
, "pthread_kill");
363 function pthread_sigmask
365 set
: access sigset_t
;
366 oset
: access sigset_t
) return int
;
367 pragma Import
(C
, pthread_sigmask
, "sigthreadmask");
369 --------------------------
370 -- POSIX.1c Section 11 --
371 --------------------------
373 function pthread_mutexattr_init
374 (attr
: access pthread_mutexattr_t
) return int
;
375 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
377 function pthread_mutexattr_destroy
378 (attr
: access pthread_mutexattr_t
) return int
;
379 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
381 function pthread_mutex_init
382 (mutex
: access pthread_mutex_t
;
383 attr
: access pthread_mutexattr_t
) return int
;
384 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
386 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
387 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
389 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
390 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
392 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
393 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
395 function pthread_condattr_init
396 (attr
: access pthread_condattr_t
) return int
;
397 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
399 function pthread_condattr_destroy
400 (attr
: access pthread_condattr_t
) return int
;
401 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
403 function pthread_cond_init
404 (cond
: access pthread_cond_t
;
405 attr
: access pthread_condattr_t
) return int
;
406 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
408 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
409 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
411 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
412 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
414 function pthread_cond_wait
415 (cond
: access pthread_cond_t
;
416 mutex
: access pthread_mutex_t
) return int
;
417 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
419 function pthread_cond_timedwait
420 (cond
: access pthread_cond_t
;
421 mutex
: access pthread_mutex_t
;
422 abstime
: access timespec
) return int
;
423 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
425 --------------------------
426 -- POSIX.1c Section 13 --
427 --------------------------
429 PTHREAD_PRIO_PROTECT
: constant := 2;
431 function PTHREAD_PRIO_INHERIT
return int
;
432 -- Return value of C macro PTHREAD_PRIO_INHERIT. This function is needed
433 -- since the value is different between AIX versions.
435 function pthread_mutexattr_setprotocol
436 (attr
: access pthread_mutexattr_t
;
437 protocol
: int
) return int
;
438 pragma Import
(C
, pthread_mutexattr_setprotocol
);
440 function pthread_mutexattr_setprioceiling
441 (attr
: access pthread_mutexattr_t
;
442 prioceiling
: int
) return int
;
443 pragma Import
(C
, pthread_mutexattr_setprioceiling
);
445 type Array_5_Int
is array (0 .. 5) of int
;
446 type struct_sched_param
is record
447 sched_priority
: int
;
449 sched_reserved
: Array_5_Int
;
452 function pthread_setschedparam
455 param
: access struct_sched_param
) return int
;
456 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
458 function pthread_attr_setscope
459 (attr
: access pthread_attr_t
;
460 contentionscope
: int
) return int
;
461 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
463 function pthread_attr_setinheritsched
464 (attr
: access pthread_attr_t
;
465 inheritsched
: int
) return int
;
466 pragma Import
(C
, pthread_attr_setinheritsched
);
468 function pthread_attr_setschedpolicy
469 (attr
: access pthread_attr_t
;
470 policy
: int
) return int
;
471 pragma Import
(C
, pthread_attr_setschedpolicy
);
473 function pthread_attr_setschedparam
474 (attr
: access pthread_attr_t
;
475 sched_param
: int
) return int
;
476 pragma Import
(C
, pthread_attr_setschedparam
);
478 function sched_yield
return int
;
479 -- AIX have a nonstandard sched_yield
481 --------------------------
482 -- P1003.1c Section 16 --
483 --------------------------
485 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
486 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
488 function pthread_attr_destroy
489 (attributes
: access pthread_attr_t
) return int
;
490 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
492 function pthread_attr_setdetachstate
493 (attr
: access pthread_attr_t
;
494 detachstate
: int
) return int
;
495 pragma Import
(C
, pthread_attr_setdetachstate
);
497 function pthread_attr_setstacksize
498 (attr
: access pthread_attr_t
;
499 stacksize
: size_t
) return int
;
500 pragma Import
(C
, pthread_attr_setstacksize
);
502 function pthread_create
503 (thread
: access pthread_t
;
504 attributes
: access pthread_attr_t
;
505 start_routine
: Thread_Body
;
506 arg
: System
.Address
)
508 pragma Import
(C
, pthread_create
, "pthread_create");
510 procedure pthread_exit
(status
: System
.Address
);
511 pragma Import
(C
, pthread_exit
, "pthread_exit");
513 function pthread_self
return pthread_t
;
514 pragma Import
(C
, pthread_self
, "pthread_self");
516 --------------------------
517 -- POSIX.1c Section 17 --
518 --------------------------
520 function pthread_setspecific
521 (key
: pthread_key_t
;
522 value
: System
.Address
) return int
;
523 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
525 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
526 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
528 type destructor_pointer
is access procedure (arg
: System
.Address
);
529 pragma Convention
(C
, destructor_pointer
);
531 function pthread_key_create
532 (key
: access pthread_key_t
;
533 destructor
: destructor_pointer
) return int
;
534 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
537 type sigset_t
is record
538 losigs
: unsigned_long
;
539 hisigs
: unsigned_long
;
541 pragma Convention
(C_Pass_By_Copy
, sigset_t
);
543 type pid_t
is new int
;
545 type time_t
is range -2 ** (System
.Parameters
.time_t_bits
- 1)
546 .. 2 ** (System
.Parameters
.time_t_bits
- 1) - 1;
548 type timespec
is record
552 pragma Convention
(C
, timespec
);
554 type pthread_attr_t
is new System
.Address
;
555 pragma Convention
(C
, pthread_attr_t
);
556 -- typedef struct __pt_attr *pthread_attr_t;
558 type pthread_condattr_t
is new System
.Address
;
559 pragma Convention
(C
, pthread_condattr_t
);
560 -- typedef struct __pt_attr *pthread_condattr_t;
562 type pthread_mutexattr_t
is new System
.Address
;
563 pragma Convention
(C
, pthread_mutexattr_t
);
564 -- typedef struct __pt_attr *pthread_mutexattr_t;
566 type pthread_t
is new System
.Address
;
567 pragma Convention
(C
, pthread_t
);
568 -- typedef void *pthread_t;
571 type ptq_queue_ptr
is access all ptq_queue
;
573 type ptq_queue
is record
574 ptq_next
: ptq_queue_ptr
;
575 ptq_prev
: ptq_queue_ptr
;
578 type Array_3_Int
is array (0 .. 3) of int
;
579 type pthread_mutex_t
is record
585 ptmtx_owner
: pthread_t
;
587 attr
: pthread_attr_t
;
590 reserved
: Array_3_Int
;
592 pragma Convention
(C
, pthread_mutex_t
);
593 type pthread_mutex_t_ptr
is access pthread_mutex_t
;
595 type pthread_cond_t
is record
599 ptcv_waiters
: ptq_queue
;
601 attr
: pthread_attr_t
;
602 mutex
: pthread_mutex_t_ptr
;
606 pragma Convention
(C
, pthread_cond_t
);
608 type pthread_key_t
is new unsigned
;
610 end System
.OS_Interface
;