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) 1995-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a QNX/Neutrino version of this package
34 -- This package encapsulates all direct interfaces to OS services
35 -- that are needed by the tasking run-time (libgnarl).
37 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
38 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
40 with Ada
.Unchecked_Conversion
;
42 with System
.OS_Constants
;
44 package System
.OS_Interface
is
47 subtype int
is Interfaces
.C
.int
;
48 subtype char
is Interfaces
.C
.char
;
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 EPERM
: constant := 1;
66 EINTR
: constant := 4;
67 EAGAIN
: constant := 11;
68 ENOMEM
: constant := 12;
69 EINVAL
: constant := 22;
70 ETIMEDOUT
: constant := 260;
76 Max_Interrupt
: constant := 64;
77 type Signal
is new int
range 0 .. Max_Interrupt
;
78 for Signal
'Size use int
'Size;
80 SIGHUP
: constant := 1;
81 SIGINT
: constant := 2;
82 SIGQUIT
: constant := 3;
83 SIGILL
: constant := 4;
84 SIGTRAP
: constant := 5;
85 SIGIOT
: constant := 6;
86 SIGABRT
: constant := 6;
87 SIGDEADLK
: constant := 7;
88 SIGFPE
: constant := 8;
89 SIGKILL
: constant := 9;
90 SIGBUS
: constant := 10;
91 SIGSEGV
: constant := 11;
92 SIGSYS
: constant := 12;
93 SIGPIPE
: constant := 13;
94 SIGALRM
: constant := 14;
95 SIGTERM
: constant := 15;
96 SIGUSR1
: constant := 16;
97 SIGUSR2
: constant := 17;
98 SIGCLD
: constant := 18;
99 SIGCHLD
: constant := 18;
100 SIGPWR
: constant := 19;
101 SIGWINCH
: constant := 20;
102 SIGURG
: constant := 21;
103 SIGPOLL
: constant := 22;
104 SIGIO
: constant := 22;
105 SIGSTOP
: constant := 23;
106 SIGTSTP
: constant := 24;
107 SIGCONT
: constant := 25;
108 SIGTTIN
: constant := 26;
109 SIGTTOU
: constant := 27;
110 SIGVTALRM
: constant := 28;
111 SIGPROF
: constant := 29;
112 SIGXCPU
: constant := 30;
113 SIGXFSZ
: constant := 31;
115 SIGRTMIN
: constant := 41;
116 SITRTMAX
: constant := 56;
118 SIGSELECT
: constant := 57;
119 SIGPHOTON
: constant := 58;
121 SIGADAABORT
: constant := SIGABRT
;
122 -- Change this to use another signal for task abort. SIGTERM might be a
125 type Signal_Set
is array (Natural range <>) of Signal
;
127 Unmasked
: constant Signal_Set
:= (
129 -- To enable debugging on multithreaded applications, mark SIGTRAP to
134 SIGTTIN
, SIGTTOU
, SIGTSTP
,
135 -- Keep these three signals unmasked so that background processes and IO
136 -- behaves as normal "C" applications
139 -- To avoid confusing the profiler
142 -- These two signals actually can't be masked (POSIX won't allow it)
144 Reserved
: constant Signal_Set
:= (SIGABRT
, SIGKILL
, SIGSTOP
, SIGSEGV
);
146 type sigset_t
is private;
148 function sigaddset
(set
: access sigset_t
; sig
: Signal
) return int
;
149 pragma Import
(C
, sigaddset
, "sigaddset");
151 function sigdelset
(set
: access sigset_t
; sig
: Signal
) return int
;
152 pragma Import
(C
, sigdelset
, "sigdelset");
154 function sigfillset
(set
: access sigset_t
) return int
;
155 pragma Import
(C
, sigfillset
, "sigfillset");
157 function sigismember
(set
: access sigset_t
; sig
: Signal
) return int
;
158 pragma Import
(C
, sigismember
, "sigismember");
160 function sigemptyset
(set
: access sigset_t
) return int
;
161 pragma Import
(C
, sigemptyset
, "sigemptyset");
163 type pad7
is array (1 .. 7) of int
;
164 type siginfo_t
is record
170 pragma Convention
(C
, siginfo_t
);
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 := 0;
182 SIG_UNBLOCK
: constant := 1;
183 SIG_SETMASK
: constant := 2;
184 SIG_PENDING
: constant := 5;
186 SA_NOCLDSTOP
: constant := 16#
0001#
;
187 SA_SIGINFO
: constant := 16#
0002#
;
188 SA_RESETHAND
: constant := 16#
0004#
;
189 SA_ONSTACK
: constant := 16#
0008#
;
190 SA_NODEFER
: constant := 16#
0010#
;
191 SA_NOCLDWAIT
: constant := 16#
0020#
;
193 SS_ONSTACK
: constant := 1;
194 SS_DISABLE
: constant := 2;
196 SIG_DFL
: constant := 0;
197 SIG_IGN
: constant := 1;
201 act
: struct_sigaction_ptr
;
202 oact
: struct_sigaction_ptr
) return int
;
203 pragma Import
(C
, sigaction
, "sigaction");
209 Time_Slice_Supported
: constant Boolean := True;
210 -- Indicates whether time slicing is supported
212 type timespec
is private;
214 type clockid_t
is new int
;
216 function clock_gettime
217 (clock_id
: clockid_t
; tp
: access timespec
) return int
;
218 pragma Import
(C
, clock_gettime
, "clock_gettime");
220 function clock_getres
221 (clock_id
: clockid_t
;
222 res
: access timespec
) return int
;
223 pragma Import
(C
, clock_getres
, "clock_getres");
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 -------------------------
232 -- Priority Scheduling --
233 -------------------------
235 SCHED_FIFO
: constant := 1;
236 SCHED_RR
: constant := 2;
237 SCHED_OTHER
: constant := 3;
239 function To_Target_Priority
240 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
242 -- Maps System.Any_Priority to a POSIX priority
248 type pid_t
is private;
250 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
251 pragma Import
(C
, kill
, "kill");
253 function getpid
return pid_t
;
254 pragma Import
(C
, getpid
, "getpid");
260 type Thread_Body
is access
261 function (arg
: System
.Address
) return System
.Address
;
262 pragma Convention
(C
, Thread_Body
);
264 function Thread_Body_Access
is new
265 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
267 type pthread_t
is new int
;
268 subtype Thread_Id
is pthread_t
;
270 type pthread_mutex_t
is limited private;
271 type pthread_cond_t
is limited private;
272 type pthread_attr_t
is limited private;
273 type pthread_mutexattr_t
is limited private;
274 type pthread_condattr_t
is limited private;
275 type pthread_key_t
is private;
277 PTHREAD_CREATE_DETACHED
: constant := 1;
279 PTHREAD_SCOPE_PROCESS
: constant := 4;
280 PTHREAD_SCOPE_SYSTEM
: constant := 0;
282 PTHREAD_INHERIT_SCHED
: constant := 0;
283 PTHREAD_EXPLICIT_SCHED
: constant := 2;
285 -- Read/Write lock not supported on Android.
287 subtype pthread_rwlock_t
is pthread_mutex_t
;
288 subtype pthread_rwlockattr_t
is pthread_mutexattr_t
;
294 type stack_t
is record
295 ss_sp
: System
.Address
;
299 pragma Convention
(C
, stack_t
);
302 (ss
: not null access stack_t
;
303 oss
: access stack_t
) return int
305 -- Not supported on QNX
307 Alternate_Stack
: aliased System
.Address
;
308 -- Dummy definition: alternate stack not available due to missing
309 -- sigaltstack in QNX
311 Alternate_Stack_Size
: constant := 0;
312 -- This must be kept in sync with init.c:__gnat_alternate_stack
314 Stack_Base_Available
: constant Boolean := False;
315 -- Indicates whether the stack base is available on this target
317 function Get_Stack_Base
(thread
: pthread_t
) return System
.Address
319 -- This is a dummy procedure to share some GNULLI files
321 function Get_Page_Size
return int
;
322 pragma Import
(C
, Get_Page_Size
, "getpagesize");
323 -- Returns the size of a page
325 PROT_NONE
: constant := 16#
00_00#
;
326 PROT_READ
: constant := 16#
01_00#
;
327 PROT_WRITE
: constant := 16#
02_00#
;
328 PROT_EXEC
: constant := 16#
04_00#
;
329 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
330 PROT_ON
: constant := PROT_READ
;
331 PROT_OFF
: constant := PROT_ALL
;
333 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
334 pragma Import
(C
, mprotect
);
336 ---------------------------------------
337 -- Nonstandard Thread Initialization --
338 ---------------------------------------
340 procedure pthread_init
with Inline_Always
;
342 -------------------------
343 -- POSIX.1c Section 3 --
344 -------------------------
346 function sigwait
(set
: access sigset_t
; sig
: access Signal
) return int
;
347 pragma Import
(C
, sigwait
, "sigwait");
349 function pthread_kill
(thread
: pthread_t
; sig
: Signal
) return int
;
350 pragma Import
(C
, pthread_kill
, "pthread_kill");
352 function pthread_sigmask
354 set
: access sigset_t
;
355 oset
: access sigset_t
) return int
;
356 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
358 --------------------------
359 -- POSIX.1c Section 11 --
360 --------------------------
362 function pthread_mutexattr_init
363 (attr
: access pthread_mutexattr_t
) return int
;
364 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
366 function pthread_mutexattr_destroy
367 (attr
: access pthread_mutexattr_t
) return int
;
368 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
370 function pthread_mutex_init
371 (mutex
: access pthread_mutex_t
;
372 attr
: access pthread_mutexattr_t
) return int
;
373 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
375 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
376 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
378 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
379 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
381 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
382 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
384 function pthread_mutex_setprioceiling
385 (mutex
: access pthread_mutex_t
;
387 old_ceiling
: access int
) return int
;
388 pragma Import
(C
, pthread_mutex_setprioceiling
);
390 function pthread_condattr_init
391 (attr
: access pthread_condattr_t
) return int
;
392 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
394 function pthread_condattr_destroy
395 (attr
: access pthread_condattr_t
) return int
;
396 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
398 function pthread_cond_init
399 (cond
: access pthread_cond_t
;
400 attr
: access pthread_condattr_t
) return int
;
401 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
403 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
404 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
406 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
407 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
409 function pthread_cond_wait
410 (cond
: access pthread_cond_t
;
411 mutex
: access pthread_mutex_t
) return int
;
412 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
414 function pthread_cond_timedwait
415 (cond
: access pthread_cond_t
;
416 mutex
: access pthread_mutex_t
;
417 abstime
: access timespec
) return int
;
418 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
420 Relative_Timed_Wait
: constant Boolean := False;
421 -- pthread_cond_timedwait requires an absolute delay time
423 --------------------------
424 -- POSIX.1c Section 13 --
425 --------------------------
427 PTHREAD_PRIO_INHERIT
: constant := 0;
428 PTHREAD_PRIO_NONE
: constant := 1;
429 PTHREAD_PRIO_PROTECT
: constant := 2;
431 function pthread_mutexattr_setprotocol
432 (attr
: access pthread_mutexattr_t
;
433 protocol
: int
) return int
;
434 pragma Import
(C
, pthread_mutexattr_setprotocol
);
436 function pthread_mutexattr_getprotocol
437 (attr
: access pthread_mutexattr_t
;
438 protocol
: access int
) return int
;
439 pragma Import
(C
, pthread_mutexattr_getprotocol
);
441 function pthread_mutexattr_setprioceiling
442 (attr
: access pthread_mutexattr_t
;
443 prioceiling
: int
) return int
;
444 pragma Import
(C
, pthread_mutexattr_setprioceiling
);
446 function pthread_mutexattr_getprioceiling
447 (attr
: access pthread_mutexattr_t
;
448 prioceiling
: access int
) return int
;
449 pragma Import
(C
, pthread_mutexattr_getprioceiling
);
451 function pthread_mutex_getprioceiling
452 (attr
: access pthread_mutex_t
;
453 prioceiling
: access int
) return int
;
454 pragma Import
(C
, pthread_mutex_getprioceiling
);
456 type pad8
is array (1 .. 8) of int
;
457 pragma Convention
(C
, pad8
);
459 type struct_sched_param
is record
460 sched_priority
: int
:= 0; -- scheduling priority
461 sched_curpriority
: int
:= 0;
462 reserved
: pad8
:= (others => 0);
464 pragma Convention
(C
, struct_sched_param
);
466 function pthread_setschedparam
469 param
: access struct_sched_param
) return int
;
470 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
472 function pthread_getschedparam
475 param
: access struct_sched_param
) return int
;
476 pragma Import
(C
, pthread_getschedparam
, "pthread_getschedparam");
478 function pthread_setschedprio
480 priority
: int
) return int
;
481 pragma Import
(C
, pthread_setschedprio
);
483 function pthread_attr_setschedparam
484 (attr
: access pthread_attr_t
;
485 param
: access struct_sched_param
) return int
;
486 pragma Import
(C
, pthread_attr_setschedparam
);
488 function pthread_attr_setinheritsched
489 (attr
: access pthread_attr_t
;
490 inheritsched
: int
) return int
;
491 pragma Import
(C
, pthread_attr_setinheritsched
);
493 function pthread_attr_setscope
494 (attr
: access pthread_attr_t
;
495 scope
: int
) return int
;
496 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
498 function pthread_attr_setschedpolicy
499 (attr
: access pthread_attr_t
;
500 policy
: int
) return int
;
502 (C
, pthread_attr_setschedpolicy
, "pthread_attr_setschedpolicy");
504 function sched_yield
return int
;
505 pragma Import
(C
, sched_yield
, "sched_yield");
507 ---------------------------
508 -- P1003.1c - Section 16 --
509 ---------------------------
511 function pthread_attr_init
512 (attributes
: access pthread_attr_t
) return int
;
513 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
515 function pthread_attr_destroy
516 (attributes
: access pthread_attr_t
) return int
;
517 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
519 function pthread_attr_setdetachstate
520 (attr
: access pthread_attr_t
;
521 detachstate
: int
) return int
;
522 pragma Import
(C
, pthread_attr_setdetachstate
);
524 function pthread_attr_setstacksize
525 (attr
: access pthread_attr_t
;
526 stacksize
: size_t
) return int
;
527 pragma Import
(C
, pthread_attr_setstacksize
);
529 function pthread_create
530 (thread
: access pthread_t
;
531 attributes
: access pthread_attr_t
;
532 start_routine
: Thread_Body
;
533 arg
: System
.Address
) return int
;
534 pragma Import
(C
, pthread_create
, "pthread_create");
536 procedure pthread_exit
(status
: System
.Address
);
537 pragma Import
(C
, pthread_exit
, "pthread_exit");
539 function pthread_self
return pthread_t
;
540 pragma Import
(C
, pthread_self
, "pthread_self");
542 function lwp_self
return System
.Address
;
543 pragma Import
(C
, lwp_self
, "pthread_self");
545 --------------------------
546 -- POSIX.1c Section 17 --
547 --------------------------
549 function pthread_setspecific
550 (key
: pthread_key_t
;
551 value
: System
.Address
) return int
;
552 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
554 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
555 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
557 type destructor_pointer
is access procedure (arg
: System
.Address
);
558 pragma Convention
(C
, destructor_pointer
);
560 function pthread_key_create
561 (key
: access pthread_key_t
;
562 destructor
: destructor_pointer
) return int
;
563 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
567 type sigset_t
is array (1 .. 2) of Interfaces
.Unsigned_32
;
568 pragma Convention
(C
, sigset_t
);
570 type pid_t
is new int
;
572 type time_t
is new long
;
574 type timespec
is record
578 pragma Convention
(C
, timespec
);
580 type unsigned_long_long_t
is mod 2 ** 64;
581 -- Local type only used to get the alignment of this type below
583 subtype char_array
is Interfaces
.C
.char_array
;
585 type pthread_attr_t
is record
586 Data
: char_array
(1 .. OS_Constants
.PTHREAD_ATTR_SIZE
);
588 pragma Convention
(C
, pthread_attr_t
);
589 for pthread_attr_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
591 type pthread_condattr_t
is record
592 Data
: char_array
(1 .. OS_Constants
.PTHREAD_CONDATTR_SIZE
);
594 pragma Convention
(C
, pthread_condattr_t
);
595 for pthread_condattr_t
'Alignment use Interfaces
.C
.int
'Alignment;
597 type pthread_mutexattr_t
is record
598 Data
: char_array
(1 .. OS_Constants
.PTHREAD_MUTEXATTR_SIZE
);
600 pragma Convention
(C
, pthread_mutexattr_t
);
601 for pthread_mutexattr_t
'Alignment use Interfaces
.C
.int
'Alignment;
603 type pthread_mutex_t
is record
604 Data
: char_array
(1 .. OS_Constants
.PTHREAD_MUTEX_SIZE
);
606 pragma Convention
(C
, pthread_mutex_t
);
607 for pthread_mutex_t
'Alignment use Interfaces
.C
.unsigned_long
'Alignment;
609 type pthread_cond_t
is record
610 Data
: char_array
(1 .. OS_Constants
.PTHREAD_COND_SIZE
);
612 pragma Convention
(C
, pthread_cond_t
);
613 for pthread_cond_t
'Alignment use unsigned_long_long_t
'Alignment;
615 type pthread_key_t
is new int
;
617 end System
.OS_Interface
;