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-2011, 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. It is --
29 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
30 -- State University (http://www.gnat.com). --
32 ------------------------------------------------------------------------------
34 -- This is the FreeBSD PTHREADS version of this package
36 -- This package encapsulates all direct interfaces to OS services
37 -- that are needed by the tasking run-time (libgnarl).
39 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
40 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
42 with Ada
.Unchecked_Conversion
;
46 package System
.OS_Interface
is
49 pragma Linker_Options
("-pthread");
51 subtype int
is Interfaces
.C
.int
;
52 subtype short
is Interfaces
.C
.short
;
53 subtype long
is Interfaces
.C
.long
;
54 subtype unsigned
is Interfaces
.C
.unsigned
;
55 subtype unsigned_short
is Interfaces
.C
.unsigned_short
;
56 subtype unsigned_long
is Interfaces
.C
.unsigned_long
;
57 subtype unsigned_char
is Interfaces
.C
.unsigned_char
;
58 subtype plain_char
is Interfaces
.C
.plain_char
;
59 subtype size_t
is Interfaces
.C
.size_t
;
65 function Errno
return int
;
66 pragma Inline
(Errno
);
68 EAGAIN
: constant := 35;
69 EINTR
: constant := 4;
70 EINVAL
: constant := 22;
71 ENOMEM
: constant := 12;
72 ETIMEDOUT
: constant := 60;
78 Max_Interrupt
: constant := 31;
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
116 SIGADAABORT
: constant := SIGABRT
;
117 -- Change this if you want to use another signal for task abort.
118 -- SIGTERM might be a good one.
120 type Signal_Set
is array (Natural range <>) of Signal
;
122 -- Interrupts that must be unmasked at all times. FreeBSD
123 -- pthreads will not allow an application to mask out any
124 -- interrupt needed by the threads library.
125 Unmasked
: constant Signal_Set
:=
126 (SIGTRAP
, SIGBUS
, SIGTTIN
, SIGTTOU
, SIGTSTP
);
128 -- FreeBSD will uses SIGPROF for timing. Do not allow a
129 -- handler to attach to this signal.
130 Reserved
: constant Signal_Set
:= (0 .. 0 => SIGPROF
);
132 type sigset_t
is private;
135 (set
: access sigset_t
;
136 sig
: Signal
) return int
;
137 pragma Import
(C
, sigaddset
, "sigaddset");
140 (set
: access sigset_t
;
141 sig
: Signal
) return int
;
142 pragma Import
(C
, sigdelset
, "sigdelset");
144 function sigfillset
(set
: access sigset_t
) return int
;
145 pragma Import
(C
, sigfillset
, "sigfillset");
148 (set
: access sigset_t
;
149 sig
: Signal
) return int
;
150 pragma Import
(C
, sigismember
, "sigismember");
152 function sigemptyset
(set
: access sigset_t
) return int
;
153 pragma Import
(C
, sigemptyset
, "sigemptyset");
155 -- sigcontext is architecture dependent, so define it private
156 type struct_sigcontext
is private;
158 type old_struct_sigaction
is record
159 sa_handler
: System
.Address
;
163 pragma Convention
(C
, old_struct_sigaction
);
165 type new_struct_sigaction
is record
166 sa_handler
: System
.Address
;
170 pragma Convention
(C
, new_struct_sigaction
);
172 subtype struct_sigaction
is new_struct_sigaction
;
173 type struct_sigaction_ptr
is access all struct_sigaction
;
175 SIG_BLOCK
: constant := 1;
176 SIG_UNBLOCK
: constant := 2;
177 SIG_SETMASK
: constant := 3;
179 SIG_DFL
: constant := 0;
180 SIG_IGN
: constant := 1;
182 SA_SIGINFO
: constant := 16#
0040#
;
183 SA_ONSTACK
: constant := 16#
0001#
;
187 act
: struct_sigaction_ptr
;
188 oact
: struct_sigaction_ptr
) return int
;
189 pragma Import
(C
, sigaction
, "sigaction");
195 Time_Slice_Supported
: constant Boolean := True;
196 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported)
198 type timespec
is private;
200 function nanosleep
(rqtp
, rmtp
: access timespec
) return int
;
201 pragma Import
(C
, nanosleep
, "nanosleep");
203 type clockid_t
is new int
;
205 function clock_gettime
206 (clock_id
: clockid_t
;
207 tp
: access timespec
)
209 pragma Import
(C
, clock_gettime
, "clock_gettime");
211 function To_Duration
(TS
: timespec
) return Duration;
212 pragma Inline
(To_Duration
);
214 function To_Timespec
(D
: Duration) return timespec
;
215 pragma Inline
(To_Timespec
);
217 type struct_timezone
is record
218 tz_minuteswest
: int
;
221 pragma Convention
(C
, struct_timezone
);
223 procedure usleep
(useconds
: unsigned_long
);
224 pragma Import
(C
, usleep
, "usleep");
226 -------------------------
227 -- Priority Scheduling --
228 -------------------------
230 SCHED_FIFO
: constant := 1;
231 SCHED_OTHER
: constant := 2;
232 SCHED_RR
: constant := 3;
234 function To_Target_Priority
235 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
236 -- Maps System.Any_Priority to a POSIX priority
242 type pid_t
is private;
244 Self_PID
: constant pid_t
;
246 function kill
(pid
: pid_t
; sig
: Signal
) return int
;
247 pragma Import
(C
, kill
, "kill");
249 function getpid
return pid_t
;
250 pragma Import
(C
, getpid
, "getpid");
256 function lwp_self
return System
.Address
;
257 -- lwp_self does not exist on this thread library, revert to pthread_self
258 -- which is the closest approximation (with getpid). This function is
259 -- needed to share 7staprop.adb across POSIX-like targets.
260 pragma Import
(C
, lwp_self
, "pthread_self");
266 type Thread_Body
is access
267 function (arg
: System
.Address
) return System
.Address
;
268 pragma Convention
(C
, Thread_Body
);
270 function Thread_Body_Access
is new
271 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
273 type pthread_t
is private;
274 subtype Thread_Id
is pthread_t
;
276 type pthread_mutex_t
is limited private;
277 type pthread_cond_t
is limited private;
278 type pthread_attr_t
is limited private;
279 type pthread_mutexattr_t
is limited private;
280 type pthread_condattr_t
is limited private;
281 type pthread_key_t
is private;
283 PTHREAD_CREATE_DETACHED
: constant := 1;
284 PTHREAD_CREATE_JOINABLE
: constant := 0;
286 PTHREAD_SCOPE_PROCESS
: constant := 0;
287 PTHREAD_SCOPE_SYSTEM
: constant := 2;
289 -- Read/Write lock not supported on freebsd. To add support both types
290 -- pthread_rwlock_t and pthread_rwlockattr_t must properly be defined
291 -- with the associated routines pthread_rwlock_[init/destroy] and
292 -- pthread_rwlock_[rdlock/wrlock/unlock].
294 subtype pthread_rwlock_t
is pthread_mutex_t
;
295 subtype pthread_rwlockattr_t
is pthread_mutexattr_t
;
301 type stack_t
is record
302 ss_sp
: System
.Address
;
306 pragma Convention
(C
, stack_t
);
309 (ss
: not null access stack_t
;
310 oss
: access stack_t
) return int
;
311 pragma Import
(C
, sigaltstack
, "sigaltstack");
313 Alternate_Stack
: aliased System
.Address
;
314 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
316 Alternate_Stack_Size
: constant := 0;
317 -- No alternate signal stack is used on this platform
319 Stack_Base_Available
: constant Boolean := False;
320 -- Indicates whether the stack base is available on this target. This
321 -- allows us to share s-osinte.adb between all the FSU run time. Note that
322 -- this value can only be true if pthread_t has a complete definition that
323 -- corresponds exactly to the C header files.
325 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
326 pragma Inline
(Get_Stack_Base
);
327 -- returns the stack base of the specified thread. Only call this function
328 -- when Stack_Base_Available is True.
330 function Get_Page_Size
return size_t
;
331 function Get_Page_Size
return Address
;
332 pragma Import
(C
, Get_Page_Size
, "getpagesize");
333 -- Returns the size of a page
335 PROT_NONE
: constant := 0;
336 PROT_READ
: constant := 1;
337 PROT_WRITE
: constant := 2;
338 PROT_EXEC
: constant := 4;
339 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
340 PROT_ON
: constant := PROT_NONE
;
341 PROT_OFF
: constant := PROT_ALL
;
343 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
344 pragma Import
(C
, mprotect
);
346 ---------------------------------------
347 -- Nonstandard Thread Initialization --
348 ---------------------------------------
350 -- FSU_THREADS requires pthread_init, which is nonstandard and this should
351 -- be invoked during the elaboration of s-taprop.adb.
353 -- FreeBSD does not require this so we provide an empty Ada body
355 procedure pthread_init
;
357 -------------------------
358 -- POSIX.1c Section 3 --
359 -------------------------
362 (set
: access sigset_t
;
363 sig
: access Signal
) return int
;
364 pragma Import
(C
, sigwait
, "sigwait");
366 function pthread_kill
368 sig
: Signal
) return int
;
369 pragma Import
(C
, pthread_kill
, "pthread_kill");
371 function pthread_sigmask
373 set
: access sigset_t
;
374 oset
: access sigset_t
) return int
;
375 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
377 --------------------------
378 -- POSIX.1c Section 11 --
379 --------------------------
381 function pthread_mutexattr_init
382 (attr
: access pthread_mutexattr_t
) return int
;
383 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
385 function pthread_mutexattr_destroy
386 (attr
: access pthread_mutexattr_t
) return int
;
387 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
389 function pthread_mutex_init
390 (mutex
: access pthread_mutex_t
;
391 attr
: access pthread_mutexattr_t
) return int
;
392 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
394 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
395 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
397 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
398 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
400 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
401 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
403 function pthread_condattr_init
404 (attr
: access pthread_condattr_t
) return int
;
405 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
407 function pthread_condattr_destroy
408 (attr
: access pthread_condattr_t
) return int
;
409 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
411 function pthread_cond_init
412 (cond
: access pthread_cond_t
;
413 attr
: access pthread_condattr_t
) return int
;
414 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
416 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
417 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
419 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
420 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
422 function pthread_cond_wait
423 (cond
: access pthread_cond_t
;
424 mutex
: access pthread_mutex_t
) return int
;
425 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
427 function pthread_cond_timedwait
428 (cond
: access pthread_cond_t
;
429 mutex
: access pthread_mutex_t
;
430 abstime
: access timespec
) return int
;
431 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
433 Relative_Timed_Wait
: constant Boolean := False;
434 -- pthread_cond_timedwait requires an absolute delay time
436 --------------------------
437 -- POSIX.1c Section 13 --
438 --------------------------
440 PTHREAD_PRIO_NONE
: constant := 0;
441 PTHREAD_PRIO_PROTECT
: constant := 2;
442 PTHREAD_PRIO_INHERIT
: constant := 1;
444 function pthread_mutexattr_setprotocol
445 (attr
: access pthread_mutexattr_t
;
446 protocol
: int
) return int
;
448 (C
, pthread_mutexattr_setprotocol
, "pthread_mutexattr_setprotocol");
450 function pthread_mutexattr_getprotocol
451 (attr
: access pthread_mutexattr_t
;
452 protocol
: access int
) return int
;
454 (C
, pthread_mutexattr_getprotocol
, "pthread_mutexattr_getprotocol");
456 function pthread_mutexattr_setprioceiling
457 (attr
: access pthread_mutexattr_t
;
458 prioceiling
: int
) return int
;
460 (C
, pthread_mutexattr_setprioceiling
,
461 "pthread_mutexattr_setprioceiling");
463 function pthread_mutexattr_getprioceiling
464 (attr
: access pthread_mutexattr_t
;
465 prioceiling
: access int
) return int
;
467 (C
, pthread_mutexattr_getprioceiling
,
468 "pthread_mutexattr_getprioceiling");
470 type struct_sched_param
is record
471 sched_priority
: int
;
473 pragma Convention
(C
, struct_sched_param
);
475 function pthread_getschedparam
478 param
: access struct_sched_param
) return int
;
479 pragma Import
(C
, pthread_getschedparam
, "pthread_getschedparam");
481 function pthread_setschedparam
484 param
: access struct_sched_param
) return int
;
485 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
487 function pthread_attr_setscope
488 (attr
: access pthread_attr_t
;
489 contentionscope
: int
) return int
;
490 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
492 function pthread_attr_getscope
493 (attr
: access pthread_attr_t
;
494 contentionscope
: access int
) return int
;
495 pragma Import
(C
, pthread_attr_getscope
, "pthread_attr_getscope");
497 function pthread_attr_setinheritsched
498 (attr
: access pthread_attr_t
;
499 inheritsched
: int
) return int
;
501 (C
, pthread_attr_setinheritsched
, "pthread_attr_setinheritsched");
503 function pthread_attr_getinheritsched
504 (attr
: access pthread_attr_t
;
505 inheritsched
: access int
) return int
;
507 (C
, pthread_attr_getinheritsched
, "pthread_attr_getinheritsched");
509 function pthread_attr_setschedpolicy
510 (attr
: access pthread_attr_t
;
511 policy
: int
) return int
;
512 pragma Import
(C
, pthread_attr_setschedpolicy
,
513 "pthread_attr_setschedpolicy");
515 function pthread_attr_getschedpolicy
516 (attr
: access pthread_attr_t
;
517 policy
: access int
) return int
;
518 pragma Import
(C
, pthread_attr_getschedpolicy
,
519 "pthread_attr_getschedpolicy");
521 function pthread_attr_setschedparam
522 (attr
: access pthread_attr_t
;
523 sched_param
: int
) return int
;
524 pragma Import
(C
, pthread_attr_setschedparam
, "pthread_attr_setschedparam");
526 function pthread_attr_getschedparam
527 (attr
: access pthread_attr_t
;
528 sched_param
: access int
) return int
;
529 pragma Import
(C
, pthread_attr_getschedparam
, "pthread_attr_getschedparam");
531 function sched_yield
return int
;
532 pragma Import
(C
, sched_yield
, "pthread_yield");
534 --------------------------
535 -- P1003.1c Section 16 --
536 --------------------------
538 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
539 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
541 function pthread_attr_destroy
542 (attributes
: access pthread_attr_t
) return int
;
543 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
545 function pthread_attr_setdetachstate
546 (attr
: access pthread_attr_t
;
547 detachstate
: int
) return int
;
549 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
551 function pthread_attr_getdetachstate
552 (attr
: access pthread_attr_t
;
553 detachstate
: access int
) return int
;
555 (C
, pthread_attr_getdetachstate
, "pthread_attr_getdetachstate");
557 function pthread_attr_getstacksize
558 (attr
: access pthread_attr_t
;
559 stacksize
: access size_t
) return int
;
561 (C
, pthread_attr_getstacksize
, "pthread_attr_getstacksize");
563 function pthread_attr_setstacksize
564 (attr
: access pthread_attr_t
;
565 stacksize
: size_t
) return int
;
567 (C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
569 function pthread_create
570 (thread
: access pthread_t
;
571 attributes
: access pthread_attr_t
;
572 start_routine
: Thread_Body
;
573 arg
: System
.Address
) return int
;
574 pragma Import
(C
, pthread_create
, "pthread_create");
576 function pthread_detach
(thread
: pthread_t
) return int
;
577 pragma Import
(C
, pthread_detach
, "pthread_detach");
579 procedure pthread_exit
(status
: System
.Address
);
580 pragma Import
(C
, pthread_exit
, "pthread_exit");
582 function pthread_self
return pthread_t
;
583 pragma Import
(C
, pthread_self
, "pthread_self");
585 --------------------------
586 -- POSIX.1c Section 17 --
587 --------------------------
589 function pthread_setspecific
590 (key
: pthread_key_t
;
591 value
: System
.Address
) return int
;
592 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
594 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
595 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
597 type destructor_pointer
is access procedure (arg
: System
.Address
);
598 pragma Convention
(C
, destructor_pointer
);
600 function pthread_key_create
601 (key
: access pthread_key_t
;
602 destructor
: destructor_pointer
) return int
;
603 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
605 ------------------------------------
606 -- Non-portable Pthread Functions --
607 ------------------------------------
609 function pthread_set_name_np
611 name
: System
.Address
) return int
;
612 pragma Import
(C
, pthread_set_name_np
, "pthread_set_name_np");
616 type sigset_t
is array (1 .. 4) of unsigned
;
618 -- In FreeBSD the component sa_handler turns out to
619 -- be one a union type, and the selector is a macro:
620 -- #define sa_handler __sigaction_u._handler
621 -- #define sa_sigaction __sigaction_u._sigaction
623 -- Should we add a signal_context type here ???
624 -- How could it be done independent of the CPU architecture ???
625 -- sigcontext type is opaque, so it is architecturally neutral.
626 -- It is always passed as an access type, so define it as an empty record
627 -- since the contents are not used anywhere.
629 type struct_sigcontext
is null record;
630 pragma Convention
(C
, struct_sigcontext
);
632 type pid_t
is new int
;
633 Self_PID
: constant pid_t
:= 0;
635 type time_t
is new long
;
637 type timespec
is record
641 pragma Convention
(C
, timespec
);
643 type pthread_t
is new System
.Address
;
644 type pthread_attr_t
is new System
.Address
;
645 type pthread_mutex_t
is new System
.Address
;
646 type pthread_mutexattr_t
is new System
.Address
;
647 type pthread_cond_t
is new System
.Address
;
648 type pthread_condattr_t
is new System
.Address
;
649 type pthread_key_t
is new int
;
651 end System
.OS_Interface
;