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-2008, 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 type struct_timeval
is private;
233 function To_Duration
(TV
: struct_timeval
) return Duration;
234 pragma Inline
(To_Duration
);
236 function To_Timeval
(D
: Duration) return struct_timeval
;
237 pragma Inline
(To_Timeval
);
239 function gettimeofday
240 (tv
: access struct_timeval
;
241 tz
: System
.Address
:= System
.Null_Address
) return int
;
242 pragma Import
(C
, gettimeofday
, "gettimeofday");
244 function sysconf
(name
: int
) return long
;
245 pragma Import
(C
, sysconf
);
247 SC_CLK_TCK
: constant := 2;
248 SC_NPROCESSORS_ONLN
: constant := 84;
250 -------------------------
251 -- Priority Scheduling --
252 -------------------------
254 SCHED_OTHER
: constant := 0;
255 SCHED_FIFO
: constant := 1;
256 SCHED_RR
: constant := 2;
258 function To_Target_Priority
259 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
260 -- Maps System.Any_Priority to a POSIX priority
266 type pid_t
is private;
268 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
269 pragma Import
(C
, kill
, "kill");
271 function getpid
return pid_t
;
272 pragma Import
(C
, getpid
, "getpid");
278 type Thread_Body
is access
279 function (arg
: System
.Address
) return System
.Address
;
280 pragma Convention
(C
, Thread_Body
);
282 function Thread_Body_Access
is new
283 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
285 type pthread_t
is new unsigned_long
;
286 subtype Thread_Id
is pthread_t
;
288 function To_pthread_t
is new Ada
.Unchecked_Conversion
289 (unsigned_long
, pthread_t
);
291 type pthread_mutex_t
is limited private;
292 type pthread_cond_t
is limited private;
293 type pthread_attr_t
is limited private;
294 type pthread_mutexattr_t
is limited private;
295 type pthread_condattr_t
is limited private;
296 type pthread_key_t
is private;
298 PTHREAD_CREATE_DETACHED
: constant := 1;
304 type stack_t
is record
305 ss_sp
: System
.Address
;
309 pragma Convention
(C
, stack_t
);
312 (ss
: not null access stack_t
;
313 oss
: access stack_t
) return int
;
314 pragma Import
(C
, sigaltstack
, "sigaltstack");
316 Alternate_Stack
: aliased System
.Address
;
317 pragma Import
(C
, Alternate_Stack
, "__gnat_alternate_stack");
318 -- The alternate signal stack for stack overflows
320 Alternate_Stack_Size
: constant := 16 * 1024;
321 -- This must be in keeping with init.c:__gnat_alternate_stack
323 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
324 pragma Inline
(Get_Stack_Base
);
325 -- This is a dummy procedure to share some GNULLI files
327 ---------------------------------------
328 -- Nonstandard Thread Initialization --
329 ---------------------------------------
331 procedure pthread_init
;
332 pragma Inline
(pthread_init
);
333 -- This is a dummy procedure to share some GNULLI files
335 -------------------------
336 -- POSIX.1c Section 3 --
337 -------------------------
339 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
340 pragma Import
(C
, sigwait
, "sigwait");
342 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
343 pragma Import
(C
, pthread_kill
, "pthread_kill");
345 function pthread_sigmask
347 set
: access sigset_t
;
348 oset
: access sigset_t
) return int
;
349 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
351 --------------------------
352 -- POSIX.1c Section 11 --
353 --------------------------
355 function pthread_mutexattr_init
356 (attr
: access pthread_mutexattr_t
) return int
;
357 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
359 function pthread_mutexattr_destroy
360 (attr
: access pthread_mutexattr_t
) return int
;
361 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
363 function pthread_mutex_init
364 (mutex
: access pthread_mutex_t
;
365 attr
: access pthread_mutexattr_t
) return int
;
366 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
368 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
369 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
371 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
372 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
374 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
375 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
377 function pthread_condattr_init
378 (attr
: access pthread_condattr_t
) return int
;
379 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
381 function pthread_condattr_destroy
382 (attr
: access pthread_condattr_t
) return int
;
383 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
385 function pthread_cond_init
386 (cond
: access pthread_cond_t
;
387 attr
: access pthread_condattr_t
) return int
;
388 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
390 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
391 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
393 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
394 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
396 function pthread_cond_wait
397 (cond
: access pthread_cond_t
;
398 mutex
: access pthread_mutex_t
) return int
;
399 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
401 function pthread_cond_timedwait
402 (cond
: access pthread_cond_t
;
403 mutex
: access pthread_mutex_t
;
404 abstime
: access timespec
) return int
;
405 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
407 --------------------------
408 -- POSIX.1c Section 13 --
409 --------------------------
411 type struct_sched_param
is record
412 sched_priority
: int
; -- scheduling priority
414 pragma Convention
(C
, struct_sched_param
);
416 function pthread_setschedparam
419 param
: access struct_sched_param
) return int
;
420 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
422 function pthread_attr_setschedpolicy
423 (attr
: access pthread_attr_t
;
424 policy
: int
) return int
;
426 (C
, pthread_attr_setschedpolicy
, "pthread_attr_setschedpolicy");
428 function sched_yield
return int
;
429 pragma Import
(C
, sched_yield
, "sched_yield");
431 ---------------------------
432 -- P1003.1c - Section 16 --
433 ---------------------------
435 function pthread_attr_init
436 (attributes
: access pthread_attr_t
) return int
;
437 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
439 function pthread_attr_destroy
440 (attributes
: access pthread_attr_t
) return int
;
441 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
443 function pthread_attr_setdetachstate
444 (attr
: access pthread_attr_t
;
445 detachstate
: int
) return int
;
447 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
449 function pthread_attr_setstacksize
450 (attr
: access pthread_attr_t
;
451 stacksize
: size_t
) return int
;
452 pragma Import
(C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
454 function pthread_create
455 (thread
: access pthread_t
;
456 attributes
: access pthread_attr_t
;
457 start_routine
: Thread_Body
;
458 arg
: System
.Address
) return int
;
459 pragma Import
(C
, pthread_create
, "pthread_create");
461 procedure pthread_exit
(status
: System
.Address
);
462 pragma Import
(C
, pthread_exit
, "pthread_exit");
464 function pthread_self
return pthread_t
;
465 pragma Import
(C
, pthread_self
, "pthread_self");
467 --------------------------
468 -- POSIX.1c Section 17 --
469 --------------------------
471 function pthread_setspecific
472 (key
: pthread_key_t
;
473 value
: System
.Address
) return int
;
474 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
476 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
477 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
479 type destructor_pointer
is access procedure (arg
: System
.Address
);
480 pragma Convention
(C
, destructor_pointer
);
482 function pthread_key_create
483 (key
: access pthread_key_t
;
484 destructor
: destructor_pointer
) return int
;
485 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
487 CPU_SETSIZE
: constant := 1_024
;
489 type bit_field
is array (1 .. CPU_SETSIZE
) of Boolean;
490 for bit_field
'Size use CPU_SETSIZE
;
491 pragma Pack
(bit_field
);
492 pragma Convention
(C
, bit_field
);
494 type cpu_set_t
is record
497 pragma Convention
(C
, cpu_set_t
);
499 function pthread_setaffinity_np
502 cpuset
: access cpu_set_t
) return int
;
503 pragma Import
(C
, pthread_setaffinity_np
, "__gnat_pthread_setaffinity_np");
507 type sigset_t
is array (0 .. 127) of Interfaces
.C
.unsigned_char
;
508 pragma Convention
(C
, sigset_t
);
509 for sigset_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
511 pragma Warnings
(Off
);
512 for struct_sigaction
use record
513 sa_handler
at 0 range 0 .. Standard
'Address_Size - 1;
514 sa_mask
at Linux
.sa_mask_pos
range 0 .. 1023;
515 sa_flags
at Linux
.sa_flags_pos
range 0 .. Standard
'Address_Size - 1;
517 -- We intentionally leave sa_restorer unspecified and let the compiler
518 -- append it after the last field, so disable corresponding warning.
519 pragma Warnings
(On
);
521 type pid_t
is new int
;
523 type time_t
is new long
;
525 type timespec
is record
529 pragma Convention
(C
, timespec
);
531 type struct_timeval
is record
535 pragma Convention
(C
, struct_timeval
);
537 type pthread_attr_t
is record
540 schedparam
: struct_sched_param
;
545 stackaddr
: System
.Address
;
548 pragma Convention
(C
, pthread_attr_t
);
550 type pthread_condattr_t
is record
553 pragma Convention
(C
, pthread_condattr_t
);
555 type pthread_mutexattr_t
is record
558 pragma Convention
(C
, pthread_mutexattr_t
);
560 type pthread_mutex_t
is new System
.Linux
.pthread_mutex_t
;
562 type pthread_cond_t
is array (0 .. 47) of unsigned_char
;
563 pragma Convention
(C
, pthread_cond_t
);
565 type pthread_key_t
is new unsigned
;
567 end System
.OS_Interface
;