1 ------------------------------------------------------------------------------
3 -- GNU ADA 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-2014, 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 the GNU/kFreeBSD (GNU/LinuxThreads) version of this package
35 -- This package encapsulates all direct interfaces to OS services
36 -- that are needed by children of System.
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
42 with Unchecked_Conversion
;
44 package System
.OS_Interface
is
47 pragma Linker_Options
("-lpthread");
49 subtype int
is Interfaces
.C
.int
;
50 subtype char
is Interfaces
.C
.char
;
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
;
64 function errno
return int
;
65 pragma Import
(C
, errno
, "__get_errno");
67 EAGAIN
: constant := 35;
68 EINTR
: constant := 4;
69 EINVAL
: constant := 22;
70 ENOMEM
: constant := 12;
71 EPERM
: constant := 1;
72 ETIMEDOUT
: constant := 60;
78 Max_Interrupt
: constant := 128;
79 type Signal
is new int
range 0 .. Max_Interrupt
;
80 for Signal
'Size use int
'Size;
82 SIGHUP
: constant := 1; -- hangup
83 SIGINT
: constant := 2; -- interrupt (rubout)
84 SIGQUIT
: constant := 3; -- quit (ASCD FS)
85 SIGILL
: constant := 4; -- illegal instruction (not reset)
86 SIGTRAP
: constant := 5; -- trace trap (not reset)
87 SIGIOT
: constant := 6; -- IOT instruction
88 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in the future
89 SIGEMT
: constant := 7; -- EMT instruction
90 SIGFPE
: constant := 8; -- floating point exception
91 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
92 SIGBUS
: constant := 10; -- bus error
93 SIGSEGV
: constant := 11; -- segmentation violation
94 SIGSYS
: constant := 12; -- bad argument to system call
95 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
96 SIGALRM
: constant := 14; -- alarm clock
97 SIGTERM
: constant := 15; -- software termination signal from kill
98 SIGURG
: constant := 16; -- urgent condition on IO channel
99 SIGSTOP
: constant := 17; -- stop (cannot be caught or ignored)
100 SIGTSTP
: constant := 18; -- user stop requested from tty
101 SIGCONT
: constant := 19; -- stopped process has been continued
102 SIGCLD
: constant := 20; -- alias for SIGCHLD
103 SIGCHLD
: constant := 20; -- child status change
104 SIGTTIN
: constant := 21; -- background tty read attempted
105 SIGTTOU
: constant := 22; -- background tty write attempted
106 SIGIO
: constant := 23; -- I/O possible (Solaris SIGPOLL alias)
107 SIGXCPU
: constant := 24; -- CPU time limit exceeded
108 SIGXFSZ
: constant := 25; -- filesize limit exceeded
109 SIGVTALRM
: constant := 26; -- virtual timer expired
110 SIGPROF
: constant := 27; -- profiling timer expired
111 SIGWINCH
: constant := 28; -- window size change
112 SIGINFO
: constant := 29; -- information request (NetBSD/FreeBSD)
113 SIGUSR1
: constant := 30; -- user defined signal 1
114 SIGUSR2
: constant := 31; -- user defined signal 2
115 SIGLTHRRES
: constant := 32; -- GNU/LinuxThreads restart signal
116 SIGLTHRCAN
: constant := 33; -- GNU/LinuxThreads cancel signal
117 SIGLTHRDBG
: constant := 34; -- GNU/LinuxThreads debugger signal
119 SIGADAABORT
: constant := SIGABRT
;
120 -- Change this if you want to use another signal for task abort.
121 -- SIGTERM might be a good one.
123 type Signal_Set
is array (Natural range <>) of Signal
;
125 Unmasked
: constant Signal_Set
:= (
127 -- To enable debugging on multithreaded applications, mark SIGTRAP to
132 SIGTTIN
, SIGTTOU
, SIGTSTP
,
133 -- Keep these three signals unmasked so that background processes
134 -- and IO behaves as normal "C" applications
137 -- To avoid confusing the profiler
140 -- These two signals actually cannot be masked;
141 -- POSIX simply won't allow it.
143 SIGLTHRRES
, SIGLTHRCAN
, SIGLTHRDBG
);
144 -- These three signals are used by GNU/LinuxThreads starting from
145 -- glibc 2.1 (future 2.2).
147 Reserved
: constant Signal_Set
:=
148 -- I am not sure why the following signal is reserved.
149 -- I guess they are not supported by this version of GNU/kFreeBSD.
150 (0 .. 0 => SIGVTALRM
);
152 type sigset_t
is private;
154 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
155 pragma Import
(C
, sigaddset
, "sigaddset");
157 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
158 pragma Import
(C
, sigdelset
, "sigdelset");
160 function sigfillset
(set
: access sigset_t
) return int
;
161 pragma Import
(C
, sigfillset
, "sigfillset");
163 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
164 pragma Import
(C
, sigismember
, "sigismember");
166 function sigemptyset
(set
: access sigset_t
) return int
;
167 pragma Import
(C
, sigemptyset
, "sigemptyset");
169 -- sigcontext is architecture dependent, so define it private
170 type struct_sigcontext
is private;
172 type struct_sigaction
is record
173 sa_handler
: System
.Address
;
177 pragma Convention
(C
, struct_sigaction
);
179 type struct_sigaction_ptr
is access all struct_sigaction
;
181 SIG_BLOCK
: constant := 1;
182 SIG_UNBLOCK
: constant := 2;
183 SIG_SETMASK
: constant := 3;
185 SIG_DFL
: constant := 0;
186 SIG_IGN
: constant := 1;
188 SA_SIGINFO
: constant := 16#
0040#
;
189 SA_ONSTACK
: constant := 16#
0001#
;
193 act
: struct_sigaction_ptr
;
194 oact
: struct_sigaction_ptr
) return int
;
195 pragma Import
(C
, sigaction
, "sigaction");
201 type timespec
is private;
203 function To_Duration
(TS
: timespec
) return Duration;
204 pragma Inline
(To_Duration
);
206 function To_Timespec
(D
: Duration) return timespec
;
207 pragma Inline
(To_Timespec
);
209 function sysconf
(name
: int
) return long
;
210 pragma Import
(C
, sysconf
);
212 SC_CLK_TCK
: constant := 2;
213 SC_NPROCESSORS_ONLN
: constant := 84;
215 -------------------------
216 -- Priority Scheduling --
217 -------------------------
219 SCHED_FIFO
: constant := 1;
220 SCHED_OTHER
: constant := 2;
221 SCHED_RR
: constant := 3;
223 function To_Target_Priority
224 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
225 -- Maps System.Any_Priority to a POSIX priority.
231 type pid_t
is private;
233 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
234 pragma Import
(C
, kill
, "kill");
236 function getpid
return pid_t
;
237 pragma Import
(C
, getpid
, "getpid");
243 type Thread_Body
is access
244 function (arg
: System
.Address
) return System
.Address
;
245 pragma Convention
(C
, Thread_Body
);
247 function Thread_Body_Access
is new
248 Unchecked_Conversion
(System
.Address
, Thread_Body
);
250 type pthread_t
is new unsigned_long
;
251 subtype Thread_Id
is pthread_t
;
253 function To_pthread_t
is new Unchecked_Conversion
254 (unsigned_long
, pthread_t
);
256 type pthread_mutex_t
is limited private;
257 type pthread_cond_t
is limited private;
258 type pthread_attr_t
is limited private;
259 type pthread_mutexattr_t
is limited private;
260 type pthread_condattr_t
is limited private;
261 type pthread_key_t
is private;
263 PTHREAD_CREATE_DETACHED
: constant := 1;
269 type stack_t
is record
270 ss_sp
: System
.Address
;
274 pragma Convention
(C
, stack_t
);
277 (ss
: not null access stack_t
;
278 oss
: access stack_t
) return int
;
279 pragma Import
(C
, sigaltstack
, "sigaltstack");
281 Alternate_Stack
: aliased System
.Address
;
282 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
284 Alternate_Stack_Size
: constant := 0;
285 -- No alternate signal stack is used on this platform
287 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
288 pragma Inline
(Get_Stack_Base
);
289 -- This is a dummy procedure to share some GNULLI files
291 ---------------------------------------
292 -- Nonstandard Thread Initialization --
293 ---------------------------------------
295 procedure pthread_init
;
296 pragma Inline
(pthread_init
);
297 -- This is a dummy procedure to share some GNULLI files
299 -------------------------
300 -- POSIX.1c Section 3 --
301 -------------------------
303 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
304 pragma Import
(C
, sigwait
, "sigwait");
306 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
307 pragma Import
(C
, pthread_kill
, "pthread_kill");
309 function pthread_sigmask
311 set
: access sigset_t
;
312 oset
: access sigset_t
) return int
;
313 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
315 --------------------------
316 -- POSIX.1c Section 11 --
317 --------------------------
319 function pthread_mutexattr_init
320 (attr
: access pthread_mutexattr_t
) return int
;
321 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
323 function pthread_mutexattr_destroy
324 (attr
: access pthread_mutexattr_t
) return int
;
325 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
327 function pthread_mutex_init
328 (mutex
: access pthread_mutex_t
;
329 attr
: access pthread_mutexattr_t
) return int
;
330 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
332 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
333 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
335 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
336 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
338 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
339 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
341 function pthread_condattr_init
342 (attr
: access pthread_condattr_t
) return int
;
343 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
345 function pthread_condattr_destroy
346 (attr
: access pthread_condattr_t
) return int
;
347 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
349 function pthread_cond_init
350 (cond
: access pthread_cond_t
;
351 attr
: access pthread_condattr_t
) return int
;
352 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
354 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
355 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
357 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
358 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
360 function pthread_cond_wait
361 (cond
: access pthread_cond_t
;
362 mutex
: access pthread_mutex_t
) return int
;
363 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
365 function pthread_cond_timedwait
366 (cond
: access pthread_cond_t
;
367 mutex
: access pthread_mutex_t
;
368 abstime
: access timespec
) return int
;
369 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
371 Relative_Timed_Wait
: constant Boolean := False;
372 -- pthread_cond_timedwait requires an absolute delay time
374 --------------------------
375 -- POSIX.1c Section 13 --
376 --------------------------
378 type struct_sched_param
is record
379 sched_priority
: int
; -- scheduling priority
381 pragma Convention
(C
, struct_sched_param
);
383 function pthread_setschedparam
386 param
: access struct_sched_param
) return int
;
387 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
389 function pthread_attr_setschedpolicy
390 (attr
: access pthread_attr_t
;
391 policy
: int
) return int
;
393 (C
, pthread_attr_setschedpolicy
, "pthread_attr_setschedpolicy");
395 function sched_yield
return int
;
396 pragma Import
(C
, sched_yield
, "sched_yield");
398 ---------------------------
399 -- P1003.1c - Section 16 --
400 ---------------------------
402 function pthread_attr_init
403 (attributes
: access pthread_attr_t
) return int
;
404 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
406 function pthread_attr_destroy
407 (attributes
: access pthread_attr_t
) return int
;
408 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
410 function pthread_attr_setdetachstate
411 (attr
: access pthread_attr_t
;
412 detachstate
: int
) return int
;
414 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
416 function pthread_attr_setstacksize
417 (attr
: access pthread_attr_t
;
418 stacksize
: size_t
) return int
;
419 pragma Import
(C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
421 function pthread_create
422 (thread
: access pthread_t
;
423 attributes
: access pthread_attr_t
;
424 start_routine
: Thread_Body
;
425 arg
: System
.Address
) return int
;
426 pragma Import
(C
, pthread_create
, "pthread_create");
428 procedure pthread_exit
(status
: System
.Address
);
429 pragma Import
(C
, pthread_exit
, "pthread_exit");
431 function pthread_self
return pthread_t
;
432 pragma Import
(C
, pthread_self
, "pthread_self");
434 --------------------------
435 -- POSIX.1c Section 17 --
436 --------------------------
438 function pthread_setspecific
439 (key
: pthread_key_t
;
440 value
: System
.Address
) return int
;
441 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
443 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
444 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
446 type destructor_pointer
is access procedure (arg
: System
.Address
);
447 pragma Convention
(C
, destructor_pointer
);
449 function pthread_key_create
450 (key
: access pthread_key_t
;
451 destructor
: destructor_pointer
) return int
;
452 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
454 CPU_SETSIZE
: constant := 1_024
;
456 type bit_field
is array (1 .. CPU_SETSIZE
) of Boolean;
457 for bit_field
'Size use CPU_SETSIZE
;
458 pragma Pack
(bit_field
);
459 pragma Convention
(C
, bit_field
);
461 type cpu_set_t
is record
464 pragma Convention
(C
, cpu_set_t
);
466 function pthread_setaffinity_np
469 cpuset
: access cpu_set_t
) return int
;
470 pragma Import
(C
, pthread_setaffinity_np
, "__gnat_pthread_setaffinity_np");
474 type sigset_t
is array (1 .. 4) of unsigned
;
476 -- In FreeBSD the component sa_handler turns out to
477 -- be one a union type, and the selector is a macro:
478 -- #define sa_handler __sigaction_u._handler
479 -- #define sa_sigaction __sigaction_u._sigaction
481 -- Should we add a signal_context type here ?
482 -- How could it be done independent of the CPU architecture ?
483 -- sigcontext type is opaque, so it is architecturally neutral.
484 -- It is always passed as an access type, so define it as an empty record
485 -- since the contents are not used anywhere.
486 type struct_sigcontext
is null record;
487 pragma Convention
(C
, struct_sigcontext
);
489 type pid_t
is new int
;
491 type time_t
is new long
;
493 type timespec
is record
497 pragma Convention
(C
, timespec
);
499 type pthread_attr_t
is record
502 schedparam
: struct_sched_param
;
507 stackaddr
: System
.Address
;
510 pragma Convention
(C
, pthread_attr_t
);
512 type pthread_condattr_t
is record
515 pragma Convention
(C
, pthread_condattr_t
);
517 type pthread_mutexattr_t
is record
520 pragma Convention
(C
, pthread_mutexattr_t
);
522 type struct_pthread_fast_lock
is record
526 pragma Convention
(C
, struct_pthread_fast_lock
);
528 type pthread_mutex_t
is record
531 m_owner
: System
.Address
;
533 m_lock
: struct_pthread_fast_lock
;
535 pragma Convention
(C
, pthread_mutex_t
);
537 type pthread_cond_t
is array (0 .. 47) of unsigned_char
;
538 pragma Convention
(C
, pthread_cond_t
);
540 type pthread_key_t
is new unsigned
;
542 end System
.OS_Interface
;