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-2010, Free Software Foundation, Inc. --
12 -- GNARL 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 2, or (at your option) any later ver- --
15 -- sion. GNARL 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. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
21 -- Boston, MA 02110-1301, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
33 ------------------------------------------------------------------------------
35 -- This is a GNU/Linux (GNU/LinuxThreads) version of this package
37 -- This package encapsulates all direct interfaces to OS services
38 -- that are needed by the tasking run-time (libgnarl).
40 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
41 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
43 with Ada
.Unchecked_Conversion
;
47 package System
.OS_Interface
is
50 pragma Linker_Options
("-lpthread");
52 subtype int
is Interfaces
.C
.int
;
53 subtype char
is Interfaces
.C
.char
;
54 subtype short
is Interfaces
.C
.short
;
55 subtype long
is Interfaces
.C
.long
;
56 subtype unsigned
is Interfaces
.C
.unsigned
;
57 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
58 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
59 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
60 subtype plain_char
is Interfaces
.C
.plain_char
;
61 subtype size_t
is Interfaces
.C
.size_t
;
67 function errno
return int
;
68 pragma Import
(C
, errno
, "__get_errno");
70 EAGAIN
: constant := System
.Linux
.EAGAIN
;
71 EINTR
: constant := System
.Linux
.EINTR
;
72 EINVAL
: constant := System
.Linux
.EINVAL
;
73 ENOMEM
: constant := System
.Linux
.ENOMEM
;
74 EPERM
: constant := System
.Linux
.EPERM
;
75 ETIMEDOUT
: constant := System
.Linux
.ETIMEDOUT
;
81 Max_Interrupt
: constant := 63;
82 type Signal
is new int
range 0 .. Max_Interrupt
;
83 for Signal
'Size use int
'Size;
85 SIGHUP
: constant := System
.Linux
.SIGHUP
;
86 SIGINT
: constant := System
.Linux
.SIGINT
;
87 SIGQUIT
: constant := System
.Linux
.SIGQUIT
;
88 SIGILL
: constant := System
.Linux
.SIGILL
;
89 SIGTRAP
: constant := System
.Linux
.SIGTRAP
;
90 SIGIOT
: constant := System
.Linux
.SIGIOT
;
91 SIGABRT
: constant := System
.Linux
.SIGABRT
;
92 SIGFPE
: constant := System
.Linux
.SIGFPE
;
93 SIGKILL
: constant := System
.Linux
.SIGKILL
;
94 SIGBUS
: constant := System
.Linux
.SIGBUS
;
95 SIGSEGV
: constant := System
.Linux
.SIGSEGV
;
96 SIGPIPE
: constant := System
.Linux
.SIGPIPE
;
97 SIGALRM
: constant := System
.Linux
.SIGALRM
;
98 SIGTERM
: constant := System
.Linux
.SIGTERM
;
99 SIGUSR1
: constant := System
.Linux
.SIGUSR1
;
100 SIGUSR2
: constant := System
.Linux
.SIGUSR2
;
101 SIGCLD
: constant := System
.Linux
.SIGCLD
;
102 SIGCHLD
: constant := System
.Linux
.SIGCHLD
;
103 SIGPWR
: constant := System
.Linux
.SIGPWR
;
104 SIGWINCH
: constant := System
.Linux
.SIGWINCH
;
105 SIGURG
: constant := System
.Linux
.SIGURG
;
106 SIGPOLL
: constant := System
.Linux
.SIGPOLL
;
107 SIGIO
: constant := System
.Linux
.SIGIO
;
108 SIGLOST
: constant := System
.Linux
.SIGLOST
;
109 SIGSTOP
: constant := System
.Linux
.SIGSTOP
;
110 SIGTSTP
: constant := System
.Linux
.SIGTSTP
;
111 SIGCONT
: constant := System
.Linux
.SIGCONT
;
112 SIGTTIN
: constant := System
.Linux
.SIGTTIN
;
113 SIGTTOU
: constant := System
.Linux
.SIGTTOU
;
114 SIGVTALRM
: constant := System
.Linux
.SIGVTALRM
;
115 SIGPROF
: constant := System
.Linux
.SIGPROF
;
116 SIGXCPU
: constant := System
.Linux
.SIGXCPU
;
117 SIGXFSZ
: constant := System
.Linux
.SIGXFSZ
;
118 SIGUNUSED
: constant := System
.Linux
.SIGUNUSED
;
119 SIGSTKFLT
: constant := System
.Linux
.SIGSTKFLT
;
120 SIGLTHRRES
: constant := System
.Linux
.SIGLTHRRES
;
121 SIGLTHRCAN
: constant := System
.Linux
.SIGLTHRCAN
;
122 SIGLTHRDBG
: constant := System
.Linux
.SIGLTHRDBG
;
124 SIGADAABORT
: constant := SIGABRT
;
125 -- Change this if you want to use another signal for task abort.
126 -- SIGTERM might be a good one.
128 type Signal_Set
is array (Natural range <>) of Signal
;
130 Unmasked
: constant Signal_Set
:= (
132 -- To enable debugging on multithreaded applications, mark SIGTRAP to
137 SIGTTIN
, SIGTTOU
, SIGTSTP
,
138 -- Keep these three signals unmasked so that background processes
139 -- and IO behaves as normal "C" applications
142 -- To avoid confusing the profiler
145 -- These two signals actually cannot be masked;
146 -- POSIX simply won't allow it.
148 SIGLTHRRES
, SIGLTHRCAN
, SIGLTHRDBG
);
149 -- These three signals are used by GNU/LinuxThreads starting from
150 -- glibc 2.1 (future 2.2).
152 Reserved
: constant Signal_Set
:=
153 -- I am not sure why the following two signals are reserved.
154 -- I guess they are not supported by this version of GNU/Linux.
155 (SIGVTALRM
, SIGUNUSED
);
157 type sigset_t
is private;
159 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
160 pragma Import
(C
, sigaddset
, "sigaddset");
162 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
163 pragma Import
(C
, sigdelset
, "sigdelset");
165 function sigfillset
(set
: access sigset_t
) return int
;
166 pragma Import
(C
, sigfillset
, "sigfillset");
168 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
169 pragma Import
(C
, sigismember
, "sigismember");
171 function sigemptyset
(set
: access sigset_t
) return int
;
172 pragma Import
(C
, sigemptyset
, "sigemptyset");
174 type union_type_3
is new String (1 .. 116);
175 type siginfo_t
is record
179 X_data
: union_type_3
;
181 pragma Convention
(C
, siginfo_t
);
183 type struct_sigaction
is record
184 sa_handler
: System
.Address
;
186 sa_flags
: Interfaces
.C
.unsigned_long
;
187 sa_restorer
: System
.Address
;
189 pragma Convention
(C
, struct_sigaction
);
191 type struct_sigaction_ptr
is access all struct_sigaction
;
193 type Machine_State
is record
201 type Machine_State_Ptr
is access all Machine_State
;
203 SA_SIGINFO
: constant := System
.Linux
.SA_SIGINFO
;
204 SA_ONSTACK
: constant := System
.Linux
.SA_ONSTACK
;
206 SIG_BLOCK
: constant := 0;
207 SIG_UNBLOCK
: constant := 1;
208 SIG_SETMASK
: constant := 2;
210 SIG_DFL
: constant := 0;
211 SIG_IGN
: constant := 1;
215 act
: struct_sigaction_ptr
;
216 oact
: struct_sigaction_ptr
) return int
;
217 pragma Import
(C
, sigaction
, "sigaction");
223 type timespec
is private;
225 function To_Duration
(TS
: timespec
) return Duration;
226 pragma Inline
(To_Duration
);
228 function To_Timespec
(D
: Duration) return timespec
;
229 pragma Inline
(To_Timespec
);
231 function sysconf
(name
: int
) return long
;
232 pragma Import
(C
, sysconf
);
234 SC_CLK_TCK
: constant := 2;
235 SC_NPROCESSORS_ONLN
: constant := 84;
237 -------------------------
238 -- Priority Scheduling --
239 -------------------------
241 SCHED_OTHER
: constant := 0;
242 SCHED_FIFO
: constant := 1;
243 SCHED_RR
: constant := 2;
245 function To_Target_Priority
246 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
247 -- Maps System.Any_Priority to a POSIX priority
253 type pid_t
is private;
255 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
256 pragma Import
(C
, kill
, "kill");
258 function getpid
return pid_t
;
259 pragma Import
(C
, getpid
, "getpid");
265 type Thread_Body
is access
266 function (arg
: System
.Address
) return System
.Address
;
267 pragma Convention
(C
, Thread_Body
);
269 function Thread_Body_Access
is new
270 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
272 type pthread_t
is new unsigned_long
;
273 subtype Thread_Id
is pthread_t
;
275 function To_pthread_t
is new Ada
.Unchecked_Conversion
276 (unsigned_long
, pthread_t
);
278 type pthread_mutex_t
is limited private;
279 type pthread_cond_t
is limited private;
280 type pthread_attr_t
is limited private;
281 type pthread_mutexattr_t
is limited private;
282 type pthread_condattr_t
is limited private;
283 type pthread_key_t
is private;
285 PTHREAD_CREATE_DETACHED
: constant := 1;
291 type stack_t
is record
292 ss_sp
: System
.Address
;
296 pragma Convention
(C
, stack_t
);
299 (ss
: not null access stack_t
;
300 oss
: access stack_t
) return int
;
301 pragma Import
(C
, sigaltstack
, "sigaltstack");
303 Alternate_Stack
: aliased System
.Address
;
304 pragma Import
(C
, Alternate_Stack
, "__gnat_alternate_stack");
305 -- The alternate signal stack for stack overflows
307 Alternate_Stack_Size
: constant := 16 * 1024;
308 -- This must be in keeping with init.c:__gnat_alternate_stack
310 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
311 pragma Inline
(Get_Stack_Base
);
312 -- This is a dummy procedure to share some GNULLI files
314 ---------------------------------------
315 -- Nonstandard Thread Initialization --
316 ---------------------------------------
318 procedure pthread_init
;
319 pragma Inline
(pthread_init
);
320 -- This is a dummy procedure to share some GNULLI files
322 -------------------------
323 -- POSIX.1c Section 3 --
324 -------------------------
326 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
327 pragma Import
(C
, sigwait
, "sigwait");
329 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
330 pragma Import
(C
, pthread_kill
, "pthread_kill");
332 function pthread_sigmask
334 set
: access sigset_t
;
335 oset
: access sigset_t
) return int
;
336 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
338 --------------------------
339 -- POSIX.1c Section 11 --
340 --------------------------
342 function pthread_mutexattr_init
343 (attr
: access pthread_mutexattr_t
) return int
;
344 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
346 function pthread_mutexattr_destroy
347 (attr
: access pthread_mutexattr_t
) return int
;
348 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
350 function pthread_mutex_init
351 (mutex
: access pthread_mutex_t
;
352 attr
: access pthread_mutexattr_t
) return int
;
353 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
355 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
356 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
358 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
359 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
361 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
362 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
364 function pthread_condattr_init
365 (attr
: access pthread_condattr_t
) return int
;
366 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
368 function pthread_condattr_destroy
369 (attr
: access pthread_condattr_t
) return int
;
370 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
372 function pthread_cond_init
373 (cond
: access pthread_cond_t
;
374 attr
: access pthread_condattr_t
) return int
;
375 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
377 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
378 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
380 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
381 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
383 function pthread_cond_wait
384 (cond
: access pthread_cond_t
;
385 mutex
: access pthread_mutex_t
) return int
;
386 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
388 function pthread_cond_timedwait
389 (cond
: access pthread_cond_t
;
390 mutex
: access pthread_mutex_t
;
391 abstime
: access timespec
) return int
;
392 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
394 --------------------------
395 -- POSIX.1c Section 13 --
396 --------------------------
398 type struct_sched_param
is record
399 sched_priority
: int
; -- scheduling priority
401 pragma Convention
(C
, struct_sched_param
);
403 function pthread_setschedparam
406 param
: access struct_sched_param
) return int
;
407 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
409 function pthread_attr_setschedpolicy
410 (attr
: access pthread_attr_t
;
411 policy
: int
) return int
;
413 (C
, pthread_attr_setschedpolicy
, "pthread_attr_setschedpolicy");
415 function sched_yield
return int
;
416 pragma Import
(C
, sched_yield
, "sched_yield");
418 ---------------------------
419 -- P1003.1c - Section 16 --
420 ---------------------------
422 function pthread_attr_init
423 (attributes
: access pthread_attr_t
) return int
;
424 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
426 function pthread_attr_destroy
427 (attributes
: access pthread_attr_t
) return int
;
428 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
430 function pthread_attr_setdetachstate
431 (attr
: access pthread_attr_t
;
432 detachstate
: int
) return int
;
434 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
436 function pthread_attr_setstacksize
437 (attr
: access pthread_attr_t
;
438 stacksize
: size_t
) return int
;
439 pragma Import
(C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
441 function pthread_create
442 (thread
: access pthread_t
;
443 attributes
: access pthread_attr_t
;
444 start_routine
: Thread_Body
;
445 arg
: System
.Address
) return int
;
446 pragma Import
(C
, pthread_create
, "pthread_create");
448 procedure pthread_exit
(status
: System
.Address
);
449 pragma Import
(C
, pthread_exit
, "pthread_exit");
451 function pthread_self
return pthread_t
;
452 pragma Import
(C
, pthread_self
, "pthread_self");
454 function lwp_self
return System
.Address
;
455 pragma Import
(C
, lwp_self
, "__gnat_lwp_self");
457 --------------------------
458 -- POSIX.1c Section 17 --
459 --------------------------
461 function pthread_setspecific
462 (key
: pthread_key_t
;
463 value
: System
.Address
) return int
;
464 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
466 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
467 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
469 type destructor_pointer
is access procedure (arg
: System
.Address
);
470 pragma Convention
(C
, destructor_pointer
);
472 function pthread_key_create
473 (key
: access pthread_key_t
;
474 destructor
: destructor_pointer
) return int
;
475 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
477 CPU_SETSIZE
: constant := 1_024
;
479 type bit_field
is array (1 .. CPU_SETSIZE
) of Boolean;
480 for bit_field
'Size use CPU_SETSIZE
;
481 pragma Pack
(bit_field
);
482 pragma Convention
(C
, bit_field
);
484 type cpu_set_t
is record
487 pragma Convention
(C
, cpu_set_t
);
489 function pthread_setaffinity_np
492 cpuset
: access cpu_set_t
) return int
;
493 pragma Import
(C
, pthread_setaffinity_np
, "pthread_setaffinity_np");
494 pragma Weak_External
(pthread_setaffinity_np
);
495 -- Use a weak symbol because this function may be available or not,
496 -- depending on the version of the system.
498 function pthread_attr_setaffinity_np
499 (attr
: access pthread_attr_t
;
501 cpuset
: access cpu_set_t
) return int
;
502 pragma Import
(C
, pthread_attr_setaffinity_np
,
503 "pthread_attr_setaffinity_np");
504 pragma Weak_External
(pthread_attr_setaffinity_np
);
505 -- Use a weak symbol because this function may be available or not,
506 -- depending on the version of the system.
510 type sigset_t
is array (0 .. 127) of unsigned_char
;
511 pragma Convention
(C
, sigset_t
);
512 for sigset_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
514 pragma Warnings
(Off
);
515 for struct_sigaction
use record
516 sa_handler
at Linux
.sa_handler_pos
range 0 .. Standard
'Address_Size - 1;
517 sa_mask
at Linux
.sa_mask_pos
range 0 .. 1023;
518 sa_flags
at Linux
.sa_flags_pos
range 0 .. Standard
'Address_Size - 1;
520 -- We intentionally leave sa_restorer unspecified and let the compiler
521 -- append it after the last field, so disable corresponding warning.
522 pragma Warnings
(On
);
524 type pid_t
is new int
;
526 type time_t
is new long
;
528 type timespec
is record
532 pragma Convention
(C
, timespec
);
534 type pthread_attr_t
is record
537 schedparam
: struct_sched_param
;
542 stackaddr
: System
.Address
;
545 pragma Convention
(C
, pthread_attr_t
);
547 type pthread_condattr_t
is record
550 pragma Convention
(C
, pthread_condattr_t
);
552 type pthread_mutexattr_t
is record
555 pragma Convention
(C
, pthread_mutexattr_t
);
557 type pthread_mutex_t
is new System
.Linux
.pthread_mutex_t
;
559 type unsigned_long_long_t
is mod 2 ** 64;
560 -- Interfaces.C.Extensions isn't preelaborated so cannot be with-ed
562 type pthread_cond_t
is array (0 .. 47) of unsigned_char
;
563 pragma Convention
(C
, pthread_cond_t
);
564 for pthread_cond_t
'Alignment use unsigned_long_long_t
'Alignment;
566 type pthread_key_t
is new unsigned
;
568 end System
.OS_Interface
;