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-2009, 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. It is --
31 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
32 -- State University (http://www.gnat.com). --
34 ------------------------------------------------------------------------------
36 -- This is the FreeBSD PTHREADS version of this package
38 -- This package encapsulates all direct interfaces to OS services
39 -- that are needed by the tasking run-time (libgnarl).
41 -- PLEASE DO NOT add any with-clauses to this package or remove the pragma
42 -- Preelaborate. This package is designed to be a bottom-level (leaf) package.
44 with Ada
.Unchecked_Conversion
;
48 package System
.OS_Interface
is
51 pragma Linker_Options
("-pthread");
53 subtype int
is Interfaces
.C
.int
;
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 Inline
(Errno
);
70 EAGAIN
: constant := 35;
71 EINTR
: constant := 4;
72 EINVAL
: constant := 22;
73 ENOMEM
: constant := 12;
74 ETIMEDOUT
: constant := 60;
80 Max_Interrupt
: constant := 31;
81 type Signal
is new int
range 0 .. Max_Interrupt
;
82 for Signal
'Size use int
'Size;
84 SIGHUP
: constant := 1; -- hangup
85 SIGINT
: constant := 2; -- interrupt (rubout)
86 SIGQUIT
: constant := 3; -- quit (ASCD FS)
87 SIGILL
: constant := 4; -- illegal instruction (not reset)
88 SIGTRAP
: constant := 5; -- trace trap (not reset)
89 SIGIOT
: constant := 6; -- IOT instruction
90 SIGABRT
: constant := 6; -- used by abort, replace SIGIOT in the future
91 SIGEMT
: constant := 7; -- EMT instruction
92 SIGFPE
: constant := 8; -- floating point exception
93 SIGKILL
: constant := 9; -- kill (cannot be caught or ignored)
94 SIGBUS
: constant := 10; -- bus error
95 SIGSEGV
: constant := 11; -- segmentation violation
96 SIGSYS
: constant := 12; -- bad argument to system call
97 SIGPIPE
: constant := 13; -- write on a pipe with no one to read it
98 SIGALRM
: constant := 14; -- alarm clock
99 SIGTERM
: constant := 15; -- software termination signal from kill
100 SIGURG
: constant := 16; -- urgent condition on IO channel
101 SIGSTOP
: constant := 17; -- stop (cannot be caught or ignored)
102 SIGTSTP
: constant := 18; -- user stop requested from tty
103 SIGCONT
: constant := 19; -- stopped process has been continued
104 SIGCLD
: constant := 20; -- alias for SIGCHLD
105 SIGCHLD
: constant := 20; -- child status change
106 SIGTTIN
: constant := 21; -- background tty read attempted
107 SIGTTOU
: constant := 22; -- background tty write attempted
108 SIGIO
: constant := 23; -- I/O possible (Solaris SIGPOLL alias)
109 SIGXCPU
: constant := 24; -- CPU time limit exceeded
110 SIGXFSZ
: constant := 25; -- filesize limit exceeded
111 SIGVTALRM
: constant := 26; -- virtual timer expired
112 SIGPROF
: constant := 27; -- profiling timer expired
113 SIGWINCH
: constant := 28; -- window size change
114 SIGINFO
: constant := 29; -- information request (NetBSD/FreeBSD)
115 SIGUSR1
: constant := 30; -- user defined signal 1
116 SIGUSR2
: constant := 31; -- user defined signal 2
118 SIGADAABORT
: constant := SIGABRT
;
119 -- Change this if you want to use another signal for task abort.
120 -- SIGTERM might be a good one.
122 type Signal_Set
is array (Natural range <>) of Signal
;
124 -- Interrupts that must be unmasked at all times. FreeBSD
125 -- pthreads will not allow an application to mask out any
126 -- interrupt needed by the threads library.
127 Unmasked
: constant Signal_Set
:=
128 (SIGTRAP
, SIGBUS
, SIGTTIN
, SIGTTOU
, SIGTSTP
);
130 -- FreeBSD will uses SIGPROF for timing. Do not allow a
131 -- handler to attach to this signal.
132 Reserved
: constant Signal_Set
:= (0 .. 0 => SIGPROF
);
134 type sigset_t
is private;
137 (set
: access sigset_t
;
138 sig
: Signal
) return int
;
139 pragma Import
(C
, sigaddset
, "sigaddset");
142 (set
: access sigset_t
;
143 sig
: Signal
) return int
;
144 pragma Import
(C
, sigdelset
, "sigdelset");
146 function sigfillset
(set
: access sigset_t
) return int
;
147 pragma Import
(C
, sigfillset
, "sigfillset");
150 (set
: access sigset_t
;
151 sig
: Signal
) return int
;
152 pragma Import
(C
, sigismember
, "sigismember");
154 function sigemptyset
(set
: access sigset_t
) return int
;
155 pragma Import
(C
, sigemptyset
, "sigemptyset");
157 -- sigcontext is architecture dependent, so define it private
158 type struct_sigcontext
is private;
160 type old_struct_sigaction
is record
161 sa_handler
: System
.Address
;
165 pragma Convention
(C
, old_struct_sigaction
);
167 type new_struct_sigaction
is record
168 sa_handler
: System
.Address
;
172 pragma Convention
(C
, new_struct_sigaction
);
174 subtype struct_sigaction
is new_struct_sigaction
;
175 type struct_sigaction_ptr
is access all struct_sigaction
;
177 SIG_BLOCK
: constant := 1;
178 SIG_UNBLOCK
: constant := 2;
179 SIG_SETMASK
: constant := 3;
181 SIG_DFL
: constant := 0;
182 SIG_IGN
: constant := 1;
184 SA_SIGINFO
: constant := 16#
0040#
;
185 SA_ONSTACK
: constant := 16#
0001#
;
189 act
: struct_sigaction_ptr
;
190 oact
: struct_sigaction_ptr
) return int
;
191 pragma Import
(C
, sigaction
, "sigaction");
197 Time_Slice_Supported
: constant Boolean := True;
198 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported)
200 type timespec
is private;
202 function nanosleep
(rqtp
, rmtp
: access timespec
) return int
;
203 pragma Import
(C
, nanosleep
, "nanosleep");
205 type clockid_t
is private;
207 CLOCK_REALTIME
: constant clockid_t
;
209 function clock_gettime
210 (clock_id
: clockid_t
;
211 tp
: access timespec
)
213 pragma Import
(C
, clock_gettime
, "clock_gettime");
215 function To_Duration
(TS
: timespec
) return Duration;
216 pragma Inline
(To_Duration
);
218 function To_Timespec
(D
: Duration) return timespec
;
219 pragma Inline
(To_Timespec
);
221 type struct_timezone
is record
222 tz_minuteswest
: int
;
225 pragma Convention
(C
, struct_timezone
);
227 procedure usleep
(useconds
: unsigned_long
);
228 pragma Import
(C
, usleep
, "usleep");
230 -------------------------
231 -- Priority Scheduling --
232 -------------------------
234 SCHED_FIFO
: constant := 1;
235 SCHED_OTHER
: constant := 2;
236 SCHED_RR
: constant := 3;
238 function To_Target_Priority
239 (Prio
: System
.Any_Priority
) return Interfaces
.C
.int
;
240 -- Maps System.Any_Priority to a POSIX priority
246 type pid_t
is private;
248 Self_PID
: constant pid_t
;
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 function lwp_self
return System
.Address
;
261 -- lwp_self does not exist on this thread library, revert to pthread_self
262 -- which is the closest approximation (with getpid). This function is
263 -- needed to share 7staprop.adb across POSIX-like targets.
264 pragma Import
(C
, lwp_self
, "pthread_self");
270 type Thread_Body
is access
271 function (arg
: System
.Address
) return System
.Address
;
272 pragma Convention
(C
, Thread_Body
);
274 function Thread_Body_Access
is new
275 Ada
.Unchecked_Conversion
(System
.Address
, Thread_Body
);
277 type pthread_t
is private;
278 subtype Thread_Id
is pthread_t
;
280 type pthread_mutex_t
is limited private;
281 type pthread_cond_t
is limited private;
282 type pthread_attr_t
is limited private;
283 type pthread_mutexattr_t
is limited private;
284 type pthread_condattr_t
is limited private;
285 type pthread_key_t
is private;
287 PTHREAD_CREATE_DETACHED
: constant := 1;
288 PTHREAD_CREATE_JOINABLE
: constant := 0;
290 PTHREAD_SCOPE_PROCESS
: constant := 0;
291 PTHREAD_SCOPE_SYSTEM
: constant := 2;
297 type stack_t
is record
298 ss_sp
: System
.Address
;
302 pragma Convention
(C
, stack_t
);
305 (ss
: not null access stack_t
;
306 oss
: access stack_t
) return int
;
307 pragma Import
(C
, sigaltstack
, "sigaltstack");
309 Alternate_Stack
: aliased System
.Address
;
310 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
312 Alternate_Stack_Size
: constant := 0;
313 -- No alternate signal stack is used on this platform
315 Stack_Base_Available
: constant Boolean := False;
316 -- Indicates whether the stack base is available on this target. This
317 -- allows us to share s-osinte.adb between all the FSU run time. Note that
318 -- this value can only be true if pthread_t has a complete definition that
319 -- corresponds exactly to the C header files.
321 function Get_Stack_Base
(thread
: pthread_t
) return Address
;
322 pragma Inline
(Get_Stack_Base
);
323 -- returns the stack base of the specified thread. Only call this function
324 -- when Stack_Base_Available is True.
326 function Get_Page_Size
return size_t
;
327 function Get_Page_Size
return Address
;
328 pragma Import
(C
, Get_Page_Size
, "getpagesize");
329 -- Returns the size of a page
331 PROT_NONE
: constant := 0;
332 PROT_READ
: constant := 1;
333 PROT_WRITE
: constant := 2;
334 PROT_EXEC
: constant := 4;
335 PROT_ALL
: constant := PROT_READ
+ PROT_WRITE
+ PROT_EXEC
;
336 PROT_ON
: constant := PROT_NONE
;
337 PROT_OFF
: constant := PROT_ALL
;
339 function mprotect
(addr
: Address
; len
: size_t
; prot
: int
) return int
;
340 pragma Import
(C
, mprotect
);
342 ---------------------------------------
343 -- Nonstandard Thread Initialization --
344 ---------------------------------------
346 -- FSU_THREADS requires pthread_init, which is nonstandard and this should
347 -- be invoked during the elaboration of s-taprop.adb.
349 -- FreeBSD does not require this so we provide an empty Ada body
351 procedure pthread_init
;
353 -------------------------
354 -- POSIX.1c Section 3 --
355 -------------------------
358 (set
: access sigset_t
;
359 sig
: access Signal
) return int
;
360 pragma Import
(C
, sigwait
, "sigwait");
362 function pthread_kill
364 sig
: Signal
) return int
;
365 pragma Import
(C
, pthread_kill
, "pthread_kill");
367 function pthread_sigmask
369 set
: access sigset_t
;
370 oset
: access sigset_t
) return int
;
371 pragma Import
(C
, pthread_sigmask
, "pthread_sigmask");
373 --------------------------
374 -- POSIX.1c Section 11 --
375 --------------------------
377 function pthread_mutexattr_init
378 (attr
: access pthread_mutexattr_t
) return int
;
379 pragma Import
(C
, pthread_mutexattr_init
, "pthread_mutexattr_init");
381 function pthread_mutexattr_destroy
382 (attr
: access pthread_mutexattr_t
) return int
;
383 pragma Import
(C
, pthread_mutexattr_destroy
, "pthread_mutexattr_destroy");
385 function pthread_mutex_init
386 (mutex
: access pthread_mutex_t
;
387 attr
: access pthread_mutexattr_t
) return int
;
388 pragma Import
(C
, pthread_mutex_init
, "pthread_mutex_init");
390 function pthread_mutex_destroy
(mutex
: access pthread_mutex_t
) return int
;
391 pragma Import
(C
, pthread_mutex_destroy
, "pthread_mutex_destroy");
393 function pthread_mutex_lock
(mutex
: access pthread_mutex_t
) return int
;
394 pragma Import
(C
, pthread_mutex_lock
, "pthread_mutex_lock");
396 function pthread_mutex_unlock
(mutex
: access pthread_mutex_t
) return int
;
397 pragma Import
(C
, pthread_mutex_unlock
, "pthread_mutex_unlock");
399 function pthread_condattr_init
400 (attr
: access pthread_condattr_t
) return int
;
401 pragma Import
(C
, pthread_condattr_init
, "pthread_condattr_init");
403 function pthread_condattr_destroy
404 (attr
: access pthread_condattr_t
) return int
;
405 pragma Import
(C
, pthread_condattr_destroy
, "pthread_condattr_destroy");
407 function pthread_cond_init
408 (cond
: access pthread_cond_t
;
409 attr
: access pthread_condattr_t
) return int
;
410 pragma Import
(C
, pthread_cond_init
, "pthread_cond_init");
412 function pthread_cond_destroy
(cond
: access pthread_cond_t
) return int
;
413 pragma Import
(C
, pthread_cond_destroy
, "pthread_cond_destroy");
415 function pthread_cond_signal
(cond
: access pthread_cond_t
) return int
;
416 pragma Import
(C
, pthread_cond_signal
, "pthread_cond_signal");
418 function pthread_cond_wait
419 (cond
: access pthread_cond_t
;
420 mutex
: access pthread_mutex_t
) return int
;
421 pragma Import
(C
, pthread_cond_wait
, "pthread_cond_wait");
423 function pthread_cond_timedwait
424 (cond
: access pthread_cond_t
;
425 mutex
: access pthread_mutex_t
;
426 abstime
: access timespec
) return int
;
427 pragma Import
(C
, pthread_cond_timedwait
, "pthread_cond_timedwait");
429 Relative_Timed_Wait
: constant Boolean := False;
430 -- pthread_cond_timedwait requires an absolute delay time
432 --------------------------
433 -- POSIX.1c Section 13 --
434 --------------------------
436 PTHREAD_PRIO_NONE
: constant := 0;
437 PTHREAD_PRIO_PROTECT
: constant := 2;
438 PTHREAD_PRIO_INHERIT
: constant := 1;
440 function pthread_mutexattr_setprotocol
441 (attr
: access pthread_mutexattr_t
;
442 protocol
: int
) return int
;
444 (C
, pthread_mutexattr_setprotocol
, "pthread_mutexattr_setprotocol");
446 function pthread_mutexattr_getprotocol
447 (attr
: access pthread_mutexattr_t
;
448 protocol
: access int
) return int
;
450 (C
, pthread_mutexattr_getprotocol
, "pthread_mutexattr_getprotocol");
452 function pthread_mutexattr_setprioceiling
453 (attr
: access pthread_mutexattr_t
;
454 prioceiling
: int
) return int
;
456 (C
, pthread_mutexattr_setprioceiling
,
457 "pthread_mutexattr_setprioceiling");
459 function pthread_mutexattr_getprioceiling
460 (attr
: access pthread_mutexattr_t
;
461 prioceiling
: access int
) return int
;
463 (C
, pthread_mutexattr_getprioceiling
,
464 "pthread_mutexattr_getprioceiling");
466 type struct_sched_param
is record
467 sched_priority
: int
;
469 pragma Convention
(C
, struct_sched_param
);
471 function pthread_getschedparam
474 param
: access struct_sched_param
) return int
;
475 pragma Import
(C
, pthread_getschedparam
, "pthread_getschedparam");
477 function pthread_setschedparam
480 param
: access struct_sched_param
) return int
;
481 pragma Import
(C
, pthread_setschedparam
, "pthread_setschedparam");
483 function pthread_attr_setscope
484 (attr
: access pthread_attr_t
;
485 contentionscope
: int
) return int
;
486 pragma Import
(C
, pthread_attr_setscope
, "pthread_attr_setscope");
488 function pthread_attr_getscope
489 (attr
: access pthread_attr_t
;
490 contentionscope
: access int
) return int
;
491 pragma Import
(C
, pthread_attr_getscope
, "pthread_attr_getscope");
493 function pthread_attr_setinheritsched
494 (attr
: access pthread_attr_t
;
495 inheritsched
: int
) return int
;
497 (C
, pthread_attr_setinheritsched
, "pthread_attr_setinheritsched");
499 function pthread_attr_getinheritsched
500 (attr
: access pthread_attr_t
;
501 inheritsched
: access int
) return int
;
503 (C
, pthread_attr_getinheritsched
, "pthread_attr_getinheritsched");
505 function pthread_attr_setschedpolicy
506 (attr
: access pthread_attr_t
;
507 policy
: int
) return int
;
508 pragma Import
(C
, pthread_attr_setschedpolicy
,
509 "pthread_attr_setschedpolicy");
511 function pthread_attr_getschedpolicy
512 (attr
: access pthread_attr_t
;
513 policy
: access int
) return int
;
514 pragma Import
(C
, pthread_attr_getschedpolicy
,
515 "pthread_attr_getschedpolicy");
517 function pthread_attr_setschedparam
518 (attr
: access pthread_attr_t
;
519 sched_param
: int
) return int
;
520 pragma Import
(C
, pthread_attr_setschedparam
, "pthread_attr_setschedparam");
522 function pthread_attr_getschedparam
523 (attr
: access pthread_attr_t
;
524 sched_param
: access int
) return int
;
525 pragma Import
(C
, pthread_attr_getschedparam
, "pthread_attr_getschedparam");
527 function sched_yield
return int
;
528 pragma Import
(C
, sched_yield
, "pthread_yield");
530 --------------------------
531 -- P1003.1c Section 16 --
532 --------------------------
534 function pthread_attr_init
(attributes
: access pthread_attr_t
) return int
;
535 pragma Import
(C
, pthread_attr_init
, "pthread_attr_init");
537 function pthread_attr_destroy
538 (attributes
: access pthread_attr_t
) return int
;
539 pragma Import
(C
, pthread_attr_destroy
, "pthread_attr_destroy");
541 function pthread_attr_setdetachstate
542 (attr
: access pthread_attr_t
;
543 detachstate
: int
) return int
;
545 (C
, pthread_attr_setdetachstate
, "pthread_attr_setdetachstate");
547 function pthread_attr_getdetachstate
548 (attr
: access pthread_attr_t
;
549 detachstate
: access int
) return int
;
551 (C
, pthread_attr_getdetachstate
, "pthread_attr_getdetachstate");
553 function pthread_attr_getstacksize
554 (attr
: access pthread_attr_t
;
555 stacksize
: access size_t
) return int
;
557 (C
, pthread_attr_getstacksize
, "pthread_attr_getstacksize");
559 function pthread_attr_setstacksize
560 (attr
: access pthread_attr_t
;
561 stacksize
: size_t
) return int
;
563 (C
, pthread_attr_setstacksize
, "pthread_attr_setstacksize");
565 function pthread_create
566 (thread
: access pthread_t
;
567 attributes
: access pthread_attr_t
;
568 start_routine
: Thread_Body
;
569 arg
: System
.Address
) return int
;
570 pragma Import
(C
, pthread_create
, "pthread_create");
572 function pthread_detach
(thread
: pthread_t
) return int
;
573 pragma Import
(C
, pthread_detach
, "pthread_detach");
575 procedure pthread_exit
(status
: System
.Address
);
576 pragma Import
(C
, pthread_exit
, "pthread_exit");
578 function pthread_self
return pthread_t
;
579 pragma Import
(C
, pthread_self
, "pthread_self");
581 --------------------------
582 -- POSIX.1c Section 17 --
583 --------------------------
585 function pthread_setspecific
586 (key
: pthread_key_t
;
587 value
: System
.Address
) return int
;
588 pragma Import
(C
, pthread_setspecific
, "pthread_setspecific");
590 function pthread_getspecific
(key
: pthread_key_t
) return System
.Address
;
591 pragma Import
(C
, pthread_getspecific
, "pthread_getspecific");
593 type destructor_pointer
is access procedure (arg
: System
.Address
);
594 pragma Convention
(C
, destructor_pointer
);
596 function pthread_key_create
597 (key
: access pthread_key_t
;
598 destructor
: destructor_pointer
) return int
;
599 pragma Import
(C
, pthread_key_create
, "pthread_key_create");
601 ------------------------------------
602 -- Non-portable Pthread Functions --
603 ------------------------------------
605 function pthread_set_name_np
607 name
: System
.Address
) return int
;
608 pragma Import
(C
, pthread_set_name_np
, "pthread_set_name_np");
612 type sigset_t
is array (1 .. 4) of unsigned
;
614 -- In FreeBSD the component sa_handler turns out to
615 -- be one a union type, and the selector is a macro:
616 -- #define sa_handler __sigaction_u._handler
617 -- #define sa_sigaction __sigaction_u._sigaction
619 -- Should we add a signal_context type here ???
620 -- How could it be done independent of the CPU architecture ???
621 -- sigcontext type is opaque, so it is architecturally neutral.
622 -- It is always passed as an access type, so define it as an empty record
623 -- since the contents are not used anywhere.
625 type struct_sigcontext
is null record;
626 pragma Convention
(C
, struct_sigcontext
);
628 type pid_t
is new int
;
629 Self_PID
: constant pid_t
:= 0;
631 type time_t
is new long
;
633 type timespec
is record
637 pragma Convention
(C
, timespec
);
639 type clockid_t
is new int
;
640 CLOCK_REALTIME
: constant clockid_t
:= 0;
642 type pthread_t
is new System
.Address
;
643 type pthread_attr_t
is new System
.Address
;
644 type pthread_mutex_t
is new System
.Address
;
645 type pthread_mutexattr_t
is new System
.Address
;
646 type pthread_cond_t
is new System
.Address
;
647 type pthread_condattr_t
is new System
.Address
;
648 type pthread_key_t
is new int
;
650 end System
.OS_Interface
;