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 Darwin pthreads version of this package
35 -- This package includes all direct interfaces to OS services that are needed
36 -- by the tasking run-time (libgnarl).
38 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
39 -- Elaborate_Body. It is designed to be a bottom-level (leaf) package.
42 with System
.OS_Constants
;
43 with System
.Parameters
;
45 package System
.OS_Interface
is
48 subtype int
is Interfaces
.C
.int
;
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
;
62 function errno
return int
;
63 pragma Import
(C
, errno
, "__get_errno");
65 EINTR
: constant := 4;
66 ENOMEM
: constant := 12;
67 EINVAL
: constant := 22;
68 EAGAIN
: constant := 35;
69 ETIMEDOUT
: constant := 60;
75 Max_Interrupt
: constant := 31;
76 type Signal
is new int
range 0 .. Max_Interrupt
;
77 for Signal
'Size use int
'Size;
79 SIGHUP
: constant := 1; -- hangup
80 SIGINT
: constant := 2; -- interrupt (rubout)
81 SIGQUIT
: constant := 3; -- quit (ASCD FS)
82 SIGILL
: constant := 4; -- illegal instruction (not reset)
83 SIGTRAP
: constant := 5; -- trace trap (not reset)
84 SIGIOT
: constant := 6; -- IOT instruction
85 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in the future
86 SIGEMT
: constant := 7; -- EMT instruction
87 SIGFPE
: constant := 8; -- floating point exception
88 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
89 SIGBUS
: constant := 10; -- bus error
90 SIGSEGV
: constant := 11; -- segmentation violation
91 SIGSYS
: constant := 12; -- bad argument to system call
92 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
93 SIGALRM
: constant := 14; -- alarm clock
94 SIGTERM
: constant := 15; -- software termination signal from kill
95 SIGURG
: constant := 16; -- urgent condition on IO channel
96 SIGSTOP
: constant := 17; -- stop (cannot be caught or ignored)
97 SIGTSTP
: constant := 18; -- user stop requested from tty
98 SIGCONT
: constant := 19; -- stopped process has been continued
99 SIGCHLD
: constant := 20; -- child status change
100 SIGTTIN
: constant := 21; -- background tty read attempted
101 SIGTTOU
: constant := 22; -- background tty write attempted
102 SIGIO
: constant := 23; -- I/O possible (Solaris SIGPOLL alias)
103 SIGXCPU
: constant := 24; -- CPU time limit exceeded
104 SIGXFSZ
: constant := 25; -- filesize limit exceeded
105 SIGVTALRM
: constant := 26; -- virtual timer expired
106 SIGPROF
: constant := 27; -- profiling timer expired
107 SIGWINCH
: constant := 28; -- window size change
108 SIGINFO
: constant := 29; -- information request
109 SIGUSR1
: constant := 30; -- user defined signal 1
110 SIGUSR2
: constant := 31; -- user defined signal 2
112 SIGADAABORT
: constant := SIGABRT
;
113 -- Change this if you want to use another signal for task abort.
114 -- SIGTERM might be a good one.
116 type Signal_Set
is array (Natural range <>) of Signal
;
118 Unmasked
: constant Signal_Set
:=
119 (SIGTTIN
, SIGTTOU
, SIGSTOP
, SIGTSTP
);
121 Reserved
: constant Signal_Set
:=
124 Exception_Signals
: constant Signal_Set
:=
125 (SIGFPE
, SIGILL
, SIGSEGV
, SIGBUS
);
126 -- These signals (when runtime or system) will be caught and converted
127 -- into an Ada exception.
129 type sigset_t
is private;
131 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
132 pragma Import
(C
, sigaddset
, "sigaddset");
134 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
135 pragma Import
(C
, sigdelset
, "sigdelset");
137 function sigfillset
(set
: access sigset_t
) return int
;
138 pragma Import
(C
, sigfillset
, "sigfillset");
140 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
141 pragma Import
(C
, sigismember
, "sigismember");
143 function sigemptyset
(set
: access sigset_t
) return int
;
144 pragma Import
(C
, sigemptyset
, "sigemptyset");
146 type siginfo_t
is private;
147 type ucontext_t
is private;
149 type Signal_Handler
is access procedure
151 info
: access siginfo_t
;
152 context
: access ucontext_t
);
154 type struct_sigaction
is record
155 sa_handler
: System
.Address
;
159 pragma Convention
(C
, struct_sigaction
);
160 type struct_sigaction_ptr
is access all struct_sigaction
;
162 SIG_BLOCK
: constant := 1;
163 SIG_UNBLOCK
: constant := 2;
164 SIG_SETMASK
: constant := 3;
166 SIG_DFL
: constant := 0;
167 SIG_IGN
: constant := 1;
169 SA_SIGINFO
: constant := 16#
0040#
;
170 SA_ONSTACK
: constant := 16#
0001#
;
174 act
: struct_sigaction_ptr
;
175 oact
: struct_sigaction_ptr
) return int
;
176 pragma Import
(C
, sigaction
, "sigaction");
182 Time_Slice_Supported
: constant Boolean := True;
183 -- Indicates whether time slicing is supported
185 type timespec
is private;
187 type clockid_t
is new int
;
189 function clock_gettime
190 (clock_id
: clockid_t
;
191 tp
: access timespec
) return int
;
193 function clock_getres
194 (clock_id
: clockid_t
;
195 res
: access timespec
) return int
;
197 function To_Duration
(TS
: timespec
) return Duration;
198 pragma Inline
(To_Duration
);
200 function To_Timespec
(D
: Duration) return timespec
;
201 pragma Inline
(To_Timespec
);
203 -------------------------
204 -- Priority Scheduling --
205 -------------------------
207 SCHED_OTHER
: constant := 1;
208 SCHED_RR
: constant := 2;
209 SCHED_FIFO
: constant := 4;
211 function To_Target_Priority
212 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
213 -- Maps System.Any_Priority to a POSIX priority
219 type pid_t
is private;
221 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
222 pragma Import
(C
, kill
, "kill");
224 function getpid
return pid_t
;
225 pragma Import
(C
, getpid
, "getpid");
231 function lwp_self
return System
.Address
;
232 pragma Import
(C
, lwp_self
, "__gnat_lwp_self");
233 -- Return the mach thread bound to the current thread. The value is not
234 -- used by the run-time library but made available to debuggers.
240 type Thread_Body
is access
241 function (arg
: System
.Address
) return System
.Address
;
242 pragma Convention
(C
, Thread_Body
);
244 type pthread_t
is private;
245 subtype Thread_Id
is pthread_t
;
247 type pthread_mutex_t
is limited private;
248 type pthread_cond_t
is limited private;
249 type pthread_attr_t
is limited private;
250 type pthread_mutexattr_t
is limited private;
251 type pthread_condattr_t
is limited private;
252 type pthread_key_t
is private;
254 type pthread_mutex_ptr
is access all pthread_mutex_t
;
255 type pthread_cond_ptr
is access all pthread_cond_t
;
257 PTHREAD_CREATE_DETACHED
: constant := 2;
259 PTHREAD_SCOPE_PROCESS
: constant := 2;
260 PTHREAD_SCOPE_SYSTEM
: constant := 1;
262 -- Read/Write lock not supported on Darwin. To add support both types
263 -- pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
264 -- with the associated routines pthread_rwlock_[init/destroy] and
265 -- pthread_rwlock_[rdlock/wrlock/unlock].
267 subtype pthread_rwlock_t
is pthread_mutex_t
;
268 subtype pthread_rwlockattr_t
is pthread_mutexattr_t
;
274 type stack_t
is record
275 ss_sp
: System
.Address
;
279 pragma Convention
(C
, stack_t
);
282 (ss
: not null access stack_t
;
283 oss
: access stack_t
) return int
;
284 pragma Import
(C
, sigaltstack
, "sigaltstack");
286 Alternate_Stack
: aliased System
.Address
;
287 pragma Import
(C
, Alternate_Stack
, "__gnat_alternate_stack");
288 -- The alternate signal stack for stack overflows
290 Alternate_Stack_Size
: constant := 32 * 1024;
291 -- This must be in keeping with init.c:__gnat_alternate_stack
293 Stack_Base_Available
: constant Boolean := False;
294 -- Indicates whether the stack base is available on this target. This
295 -- allows us to share s-osinte.adb between all the FSU run time. Note that
296 -- this value can only be true if pthread_t has a complete definition that
297 -- corresponds exactly to the C header files.
299 function Get_Stack_Base
(thread
: pthread_t
) return System
.Address
;
300 pragma Inline
(Get_Stack_Base
);
301 -- returns the stack base of the specified thread. Only call this function
302 -- when Stack_Base_Available is True.
304 function Get_Page_Size
return int
;
305 pragma Import
(C
, Get_Page_Size
, "getpagesize");
306 -- Returns the size of a page
308 PROT_NONE
: constant := 0;
309 PROT_READ
: constant := 1;
310 PROT_WRITE
: constant := 2;
311 PROT_EXEC
: constant := 4;
312 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
314 PROT_ON
: constant := PROT_NONE
;
315 PROT_OFF
: constant := PROT_ALL
;
318 (addr
: System
.Address
;
320 prot
: int
) return int
;
321 pragma Import
(C
, mprotect
);
323 ---------------------------------------
324 -- Nonstandard Thread Initialization --
325 ---------------------------------------
327 procedure pthread_init
;
329 -------------------------
330 -- POSIX.1c Section 3 --
331 -------------------------
333 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
334 pragma Import
(C
, sigwait
, "sigwait");
336 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
337 pragma Import
(C
, pthread_kill
, "pthread_kill");
339 function pthread_sigmask
341 set
: access sigset_t
;
342 oset
: access sigset_t
) return int
;
343 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
345 --------------------------
346 -- POSIX.1c Section 11 --
347 --------------------------
349 function pthread_mutexattr_init
350 (attr
: access pthread_mutexattr_t
) return int
;
351 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
353 function pthread_mutexattr_destroy
354 (attr
: access pthread_mutexattr_t
) return int
;
355 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
357 function pthread_mutex_init
358 (mutex
: access pthread_mutex_t
;
359 attr
: access pthread_mutexattr_t
) return int
;
360 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
362 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
363 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
365 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
366 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
368 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
369 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
371 function pthread_condattr_init
372 (attr
: access pthread_condattr_t
) return int
;
373 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
375 function pthread_condattr_destroy
376 (attr
: access pthread_condattr_t
) return int
;
377 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
379 function pthread_cond_init
380 (cond
: access pthread_cond_t
;
381 attr
: access pthread_condattr_t
) return int
;
382 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
384 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
385 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
387 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
388 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
390 function pthread_cond_wait
391 (cond
: access pthread_cond_t
;
392 mutex
: access pthread_mutex_t
) return int
;
393 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
395 function pthread_cond_timedwait
396 (cond
: access pthread_cond_t
;
397 mutex
: access pthread_mutex_t
;
398 abstime
: access timespec
) return int
;
399 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
401 --------------------------
402 -- POSIX.1c Section 13 --
403 --------------------------
405 PTHREAD_PRIO_NONE
: constant := 0;
406 PTHREAD_PRIO_INHERIT
: constant := 1;
407 PTHREAD_PRIO_PROTECT
: constant := 2;
409 function pthread_mutexattr_setprotocol
410 (attr
: access pthread_mutexattr_t
;
411 protocol
: int
) return int
;
413 (C
, pthread_mutexattr_setprotocol
, "pthread_mutexattr_setprotocol");
415 function pthread_mutexattr_setprioceiling
416 (attr
: access pthread_mutexattr_t
;
417 prioceiling
: int
) return int
;
419 (C
, pthread_mutexattr_setprioceiling
,
420 "pthread_mutexattr_setprioceiling");
422 type padding
is array (int
range <>) of Interfaces
.C
.char
;
424 type struct_sched_param
is record
425 sched_priority
: int
; -- scheduling priority
426 opaque
: padding
(1 .. 4);
428 pragma Convention
(C
, struct_sched_param
);
430 function pthread_setschedparam
433 param
: access struct_sched_param
) return int
;
434 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
436 function pthread_attr_setscope
437 (attr
: access pthread_attr_t
;
438 contentionscope
: int
) return int
;
439 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
441 function pthread_attr_setinheritsched
442 (attr
: access pthread_attr_t
;
443 inheritsched
: int
) return int
;
445 (C
, pthread_attr_setinheritsched
, "pthread_attr_setinheritsched");
447 function pthread_attr_setschedpolicy
448 (attr
: access pthread_attr_t
;
449 policy
: int
) return int
;
450 pragma Import
(C
, pthread_attr_setschedpolicy
, "pthread_attr_setsched");
452 function sched_yield
return int
;
454 ---------------------------
455 -- P1003.1c - Section 16 --
456 ---------------------------
458 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
459 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
461 function pthread_attr_destroy
462 (attributes
: access pthread_attr_t
) return int
;
463 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
465 function pthread_attr_setdetachstate
466 (attr
: access pthread_attr_t
;
467 detachstate
: int
) return int
;
469 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
471 function pthread_attr_setstacksize
472 (attr
: access pthread_attr_t
;
473 stacksize
: size_t
) return int
;
475 (C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
477 function pthread_create
478 (thread
: access pthread_t
;
479 attributes
: access pthread_attr_t
;
480 start_routine
: Thread_Body
;
481 arg
: System
.Address
) return int
;
482 pragma Import
(C
, pthread_create
, "pthread_create");
484 procedure pthread_exit
(status
: System
.Address
);
485 pragma Import
(C
, pthread_exit
, "pthread_exit");
487 function pthread_self
return pthread_t
;
488 pragma Import
(C
, pthread_self
, "pthread_self");
490 --------------------------
491 -- POSIX.1c Section 17 --
492 --------------------------
494 function pthread_setspecific
495 (key
: pthread_key_t
;
496 value
: System
.Address
) return int
;
497 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
499 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
500 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
502 type destructor_pointer
is access procedure (arg
: System
.Address
);
503 pragma Convention
(C
, destructor_pointer
);
505 function pthread_key_create
506 (key
: access pthread_key_t
;
507 destructor
: destructor_pointer
) return int
;
508 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
512 type sigset_t
is new unsigned
;
514 type int32_t
is new int
;
516 type pid_t
is new int32_t
;
518 type time_t
is range -2 ** (System
.Parameters
.time_t_bits
- 1)
519 .. 2 ** (System
.Parameters
.time_t_bits
- 1) - 1;
521 type timespec
is record
525 pragma Convention
(C
, timespec
);
528 -- Darwin specific signal implementation
530 type Pad_Type
is array (1 .. 7) of unsigned_long
;
531 type siginfo_t
is record
532 si_signo
: int
; -- signal number
533 si_errno
: int
; -- errno association
534 si_code
: int
; -- signal code
535 si_pid
: int
; -- sending process
536 si_uid
: unsigned
; -- sender's ruid
537 si_status
: int
; -- exit value
538 si_addr
: System
.Address
; -- faulting instruction
539 si_value
: System
.Address
; -- signal value
540 si_band
: long
; -- band event for SIGPOLL
541 pad
: Pad_Type
; -- RFU
543 pragma Convention
(C
, siginfo_t
);
545 type mcontext_t
is new System
.Address
;
547 type ucontext_t
is record
549 uc_sigmask
: sigset_t
; -- Signal Mask Used By This Context
550 uc_stack
: stack_t
; -- Stack Used By This Context
551 uc_link
: System
.Address
; -- Pointer To Resuming Context
552 uc_mcsize
: size_t
; -- Size of The Machine Context
553 uc_mcontext
: mcontext_t
; -- Machine Specific Context
555 pragma Convention
(C
, ucontext_t
);
558 -- Darwin specific pthread implementation
560 type pthread_t
is new System
.Address
;
562 type pthread_attr_t
is record
564 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_ATTR_SIZE
);
566 pragma Convention
(C
, pthread_attr_t
);
568 type pthread_mutexattr_t
is record
570 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_MUTEXATTR_SIZE
);
572 pragma Convention
(C
, pthread_mutexattr_t
);
574 type pthread_mutex_t
is record
576 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_MUTEX_SIZE
);
578 pragma Convention
(C
, pthread_mutex_t
);
580 type pthread_condattr_t
is record
582 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_CONDATTR_SIZE
);
584 pragma Convention
(C
, pthread_condattr_t
);
586 type pthread_cond_t
is record
588 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_COND_SIZE
);
590 pragma Convention
(C
, pthread_cond_t
);
592 type pthread_once_t
is record
594 opaque
: padding
(1 .. System
.OS_Constants
.PTHREAD_ONCE_SIZE
);
596 pragma Convention
(C
, pthread_once_t
);
598 type pthread_key_t
is new unsigned_long
;
600 end System
.OS_Interface
;