1 ------------------------------------------------------------------------------
3 -- GNU ADA 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) 1997-2008 Free Software Foundation, Inc. --
11 -- GNARL 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 -- The GNARL files that were developed for RTEMS are maintained by On-Line --
33 -- Applications Research Corporation (http://www.oarcorp.com) in coopera- --
34 -- tion with Ada Core Technologies Inc. and Florida State University. --
36 ------------------------------------------------------------------------------
38 -- This is the RTEMS version of this package.
40 -- RTEMS target names are of the form CPU-rtems.
41 -- This implementation is designed to work on ALL RTEMS targets.
42 -- The RTEMS implementation is primarily based upon the POSIX threads
43 -- API but there are also bindings to GNAT/RTEMS support routines
44 -- to insulate this code from C API specific details and, in some
45 -- cases, obtain target architecture and BSP specific information
46 -- that is unavailable at the time this package is built.
48 -- This package encapsulates all direct interfaces to OS services
49 -- that are needed by children of System.
51 -- PLEASE DO NOT add any with-clauses to this package
52 -- or remove the pragma Preelaborate.
53 -- It is designed to be a bottom-level (leaf) package.
56 package System.OS_Interface is
59 -- This interface assumes that "unsigned" is a 32-bit entity. This
60 -- will correspond to RTEMS object ids.
62 subtype rtems_id is Interfaces.C.unsigned;
64 subtype int is Interfaces.C.int;
65 subtype short is Interfaces.C.short;
66 subtype long is Interfaces.C.long;
67 subtype unsigned is Interfaces.C.unsigned;
68 subtype unsigned_short is Interfaces.C.unsigned_short;
69 subtype unsigned_long is Interfaces.C.unsigned_long;
70 subtype unsigned_char is Interfaces.C.unsigned_char;
71 subtype plain_char is Interfaces.C.plain_char;
72 subtype size_t is Interfaces.C.size_t;
78 function errno return int;
79 pragma Import (C, errno, "__get_errno");
81 EAGAIN : constant := 11;
82 EINTR : constant := 4;
83 EINVAL : constant := 22;
84 ENOMEM : constant := 12;
85 ETIMEDOUT : constant := 116;
91 Max_Interrupt : constant := 31;
92 type Signal is new int range 0 .. Max_Interrupt;
94 SIGXCPU : constant := 0; -- XCPU
95 SIGHUP : constant := 1; -- hangup
96 SIGINT : constant := 2; -- interrupt (rubout)
97 SIGQUIT : constant := 3; -- quit (ASCD FS)
98 SIGILL : constant := 4; -- illegal instruction (not reset)
99 SIGTRAP : constant := 5; -- trace trap (not reset)
100 SIGIOT : constant := 6; -- IOT instruction
101 SIGABRT : constant := 6; -- used by abort, replace SIGIOT in the future
102 SIGEMT : constant := 7; -- EMT instruction
103 SIGFPE : constant := 8; -- floating point exception
104 SIGKILL : constant := 9; -- kill (cannot be caught or ignored)
105 SIGBUS : constant := 10; -- bus error
106 SIGSEGV : constant := 11; -- segmentation violation
107 SIGSYS : constant := 12; -- bad argument to system call
108 SIGPIPE : constant := 13; -- write on a pipe with no one to read it
109 SIGALRM : constant := 14; -- alarm clock
110 SIGTERM : constant := 15; -- software termination signal from kill
111 SIGUSR1 : constant := 16; -- user defined signal 1
112 SIGUSR2 : constant := 17; -- user defined signal 2
114 SIGADAABORT : constant := SIGABRT;
116 type Signal_Set is array (Natural range <>) of Signal;
118 Unmasked : constant Signal_Set := (SIGTRAP, SIGALRM, SIGEMT);
119 Reserved : constant Signal_Set := (1 .. 1 => SIGKILL);
121 type sigset_t is private;
123 function sigaddset (set : access sigset_t; sig : Signal) return int;
124 pragma Import (C, sigaddset, "sigaddset");
126 function sigdelset (set : access sigset_t; sig : Signal) return int;
127 pragma Import (C, sigdelset, "sigdelset");
129 function sigfillset (set : access sigset_t) return int;
130 pragma Import (C, sigfillset, "sigfillset");
132 function sigismember (set : access sigset_t; sig : Signal) return int;
133 pragma Import (C, sigismember, "sigismember");
135 function sigemptyset (set : access sigset_t) return int;
136 pragma Import (C, sigemptyset, "sigemptyset");
138 type struct_sigaction is record
141 sa_handler : System.Address;
143 pragma Convention (C, struct_sigaction);
144 type struct_sigaction_ptr is access all struct_sigaction;
146 SA_SIGINFO : constant := 16#02#;
148 SA_ONSTACK : constant := 16#00#;
149 -- SA_ONSTACK is not defined on RTEMS, but it is refered to in the POSIX
150 -- implementation of System.Interrupt_Management. Therefore we define a
151 -- dummy value of zero here so that setting this flag is a nop.
153 SIG_BLOCK : constant := 1;
154 SIG_UNBLOCK : constant := 2;
155 SIG_SETMASK : constant := 3;
157 SIG_DFL : constant := 0;
158 SIG_IGN : constant := 1;
162 act : struct_sigaction_ptr;
163 oact : struct_sigaction_ptr) return int;
164 pragma Import (C, sigaction, "sigaction");
170 Time_Slice_Supported : constant Boolean := True;
171 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported)
173 type timespec is private;
175 type clockid_t is private;
177 CLOCK_REALTIME : constant clockid_t;
179 function clock_gettime
180 (clock_id : clockid_t;
181 tp : access timespec) return int;
182 pragma Import (C, clock_gettime, "clock_gettime");
184 function To_Duration (TS : timespec) return Duration;
185 pragma Inline (To_Duration);
187 function To_Timespec (D : Duration) return timespec;
188 pragma Inline (To_Timespec);
190 type struct_timeval is private;
192 function To_Duration (TV : struct_timeval) return Duration;
193 pragma Inline (To_Duration);
195 function To_Timeval (D : Duration) return struct_timeval;
196 pragma Inline (To_Timeval);
198 -------------------------
199 -- Priority Scheduling --
200 -------------------------
202 SCHED_FIFO : constant := 1;
203 SCHED_RR : constant := 2;
204 SCHED_OTHER : constant := 0;
206 function To_Target_Priority
207 (Prio : System.Any_Priority) return Interfaces.C.int;
208 -- Maps System.Any_Priority to a POSIX priority
214 type pid_t is private;
216 function kill (pid : pid_t; sig : Signal) return int;
217 pragma Import (C, kill, "kill");
219 function getpid return pid_t;
220 pragma Import (C, getpid, "getpid");
226 function lwp_self return System.Address;
227 -- lwp_self does not exist on this thread library, revert to pthread_self
228 -- which is the closest approximation (with getpid). This function is
229 -- needed to share 7staprop.adb across POSIX-like targets.
230 pragma Import (C, lwp_self, "pthread_self");
236 type Thread_Body is access
237 function (arg : System.Address) return System.Address;
238 pragma Convention (C, Thread_Body);
240 type pthread_t is private;
241 subtype Thread_Id is pthread_t;
243 type pthread_mutex_t is limited private;
244 type pthread_cond_t is limited private;
245 type pthread_attr_t is limited private;
246 type pthread_mutexattr_t is limited private;
247 type pthread_condattr_t is limited private;
248 type pthread_key_t is private;
250 No_Key : constant pthread_key_t;
252 PTHREAD_CREATE_DETACHED : constant := 0;
254 PTHREAD_SCOPE_PROCESS : constant := 0;
255 PTHREAD_SCOPE_SYSTEM : constant := 1;
261 type stack_t is record
262 ss_sp : System.Address;
266 pragma Convention (C, stack_t);
269 (ss : not null access stack_t;
270 oss : access stack_t) return int;
272 Alternate_Stack : aliased System.Address;
273 -- This is a dummy definition, never used (Alternate_Stack_Size is null)
275 Alternate_Stack_Size : constant := 0;
276 -- No alternate signal stack is used on this platform
278 Stack_Base_Available : constant Boolean := False;
279 -- Indicates whether the stack base is available on this target.
280 -- This allows us to share s-osinte.adb between all the FSU/RTEMS
282 -- Note that this value can only be true if pthread_t has a complete
283 -- definition that corresponds exactly to the C header files.
285 function Get_Stack_Base (thread : pthread_t) return Address;
286 pragma Inline (Get_Stack_Base);
287 -- returns the stack base of the specified thread.
288 -- Only call this function when Stack_Base_Available is True.
290 -- These two functions are only needed to share s-taprop.adb with
293 function Get_Page_Size return size_t;
294 function Get_Page_Size return Address;
295 -- returns the size of a page, or 0 if this is not relevant on this
296 -- target (which is the case for RTEMS)
298 PROT_ON : constant := 0;
299 PROT_OFF : constant := 0;
301 function mprotect (addr : Address; len : size_t; prot : int) return int;
302 pragma Import (C, mprotect);
304 -----------------------------------------
305 -- Nonstandard Thread Initialization --
306 -----------------------------------------
308 procedure pthread_init;
309 -- FSU_THREADS requires pthread_init, which is nonstandard
310 -- and this should be invoked during the elaboration of s-taprop.adb
312 -- RTEMS does not require this so we provide an empty Ada body.
314 -------------------------
315 -- POSIX.1c Section 3 --
316 -------------------------
319 (set : access sigset_t;
320 sig : access Signal) return int;
321 pragma Import (C, sigwait, "sigwait");
323 function pthread_kill
325 sig : Signal) return int;
326 pragma Import (C, pthread_kill, "pthread_kill");
328 function pthread_sigmask
330 set : access sigset_t;
331 oset : access sigset_t) return int;
332 pragma Import (C, pthread_sigmask, "pthread_sigmask");
334 ----------------------------
335 -- POSIX.1c Section 11 --
336 ----------------------------
338 function pthread_mutexattr_init
339 (attr : access pthread_mutexattr_t) return int;
340 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init");
342 function pthread_mutexattr_destroy
343 (attr : access pthread_mutexattr_t) return int;
344 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy");
346 function pthread_mutex_init
347 (mutex : access pthread_mutex_t;
348 attr : access pthread_mutexattr_t) return int;
349 pragma Import (C, pthread_mutex_init, "pthread_mutex_init");
351 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int;
352 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy");
354 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int;
355 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock");
357 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int;
358 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock");
360 function pthread_condattr_init
361 (attr : access pthread_condattr_t) return int;
362 pragma Import (C, pthread_condattr_init, "pthread_condattr_init");
364 function pthread_condattr_destroy
365 (attr : access pthread_condattr_t) return int;
366 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy");
368 function pthread_cond_init
369 (cond : access pthread_cond_t;
370 attr : access pthread_condattr_t) return int;
371 pragma Import (C, pthread_cond_init, "pthread_cond_init");
373 function pthread_cond_destroy (cond : access pthread_cond_t) return int;
374 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy");
376 function pthread_cond_signal (cond : access pthread_cond_t) return int;
377 pragma Import (C, pthread_cond_signal, "pthread_cond_signal");
379 function pthread_cond_wait
380 (cond : access pthread_cond_t;
381 mutex : access pthread_mutex_t) return int;
382 pragma Import (C, pthread_cond_wait, "pthread_cond_wait");
384 function pthread_cond_timedwait
385 (cond : access pthread_cond_t;
386 mutex : access pthread_mutex_t;
387 abstime : access timespec) return int;
388 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait");
390 Relative_Timed_Wait : constant Boolean := False;
391 -- pthread_cond_timedwait requires an absolute delay time
393 --------------------------
394 -- POSIX.1c Section 13 --
395 --------------------------
397 PTHREAD_PRIO_NONE : constant := 0;
398 PTHREAD_PRIO_PROTECT : constant := 2;
399 PTHREAD_PRIO_INHERIT : constant := 1;
401 function pthread_mutexattr_setprotocol
402 (attr : access pthread_mutexattr_t;
403 protocol : int) return int;
404 pragma Import (C, pthread_mutexattr_setprotocol);
406 function pthread_mutexattr_setprioceiling
407 (attr : access pthread_mutexattr_t;
408 prioceiling : int) return int;
410 (C, pthread_mutexattr_setprioceiling,
411 "pthread_mutexattr_setprioceiling");
413 type struct_sched_param is record
414 sched_priority : int;
415 ss_low_priority : int;
416 ss_replenish_period : timespec;
417 ss_initial_budget : timespec;
419 pragma Convention (C, struct_sched_param);
421 function pthread_setschedparam
424 param : access struct_sched_param) return int;
425 pragma Import (C, pthread_setschedparam, "pthread_setschedparam");
427 function pthread_attr_setscope
428 (attr : access pthread_attr_t;
429 contentionscope : int) return int;
430 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope");
432 function pthread_attr_setinheritsched
433 (attr : access pthread_attr_t;
434 inheritsched : int) return int;
435 pragma Import (C, pthread_attr_setinheritsched);
437 function pthread_attr_setschedpolicy
438 (attr : access pthread_attr_t;
439 policy : int) return int;
440 pragma Import (C, pthread_attr_setschedpolicy);
442 function pthread_attr_setschedparam
443 (attr : access pthread_attr_t;
444 sched_param : int) return int;
445 pragma Import (C, pthread_attr_setschedparam);
447 function sched_yield return int;
448 pragma Import (C, sched_yield, "sched_yield");
450 ---------------------------
451 -- P1003.1c - Section 16 --
452 ---------------------------
454 function pthread_attr_init (attributes : access pthread_attr_t) return int;
455 pragma Import (C, pthread_attr_init, "pthread_attr_init");
457 function pthread_attr_destroy
458 (attributes : access pthread_attr_t) return int;
459 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy");
461 function pthread_attr_setdetachstate
462 (attr : access pthread_attr_t;
463 detachstate : int) return int;
464 pragma Import (C, pthread_attr_setdetachstate);
466 function pthread_attr_setstacksize
467 (attr : access pthread_attr_t;
468 stacksize : size_t) return int;
469 pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize");
471 function pthread_create
472 (thread : access pthread_t;
473 attributes : access pthread_attr_t;
474 start_routine : Thread_Body;
475 arg : System.Address) return int;
476 pragma Import (C, pthread_create, "pthread_create");
478 procedure pthread_exit (status : System.Address);
479 pragma Import (C, pthread_exit, "pthread_exit");
481 function pthread_self return pthread_t;
482 pragma Import (C, pthread_self, "pthread_self");
484 --------------------------
485 -- POSIX.1c Section 17 --
486 --------------------------
488 function pthread_setspecific
489 (key : pthread_key_t;
490 value : System.Address) return int;
491 pragma Import (C, pthread_setspecific, "pthread_setspecific");
493 function pthread_getspecific (key : pthread_key_t) return System.Address;
494 pragma Import (C, pthread_getspecific, "pthread_getspecific");
496 type destructor_pointer is access procedure (arg : System.Address);
497 pragma Convention (C, destructor_pointer);
499 function pthread_key_create
500 (key : access pthread_key_t;
501 destructor : destructor_pointer) return int;
502 pragma Import (C, pthread_key_create, "pthread_key_create");
506 type sigset_t is new int;
508 type pid_t is new int;
510 type time_t is new long;
512 type timespec is record
516 pragma Convention (C, timespec);
518 type clockid_t is new rtems_id;
519 CLOCK_REALTIME : constant clockid_t := 1;
521 type struct_timeval is record
525 pragma Convention (C, struct_timeval);
527 type pthread_attr_t is record
528 is_initialized : int;
529 stackaddr : System.Address;
531 contentionscope : int;
534 schedparam : struct_sched_param;
535 cputime_clocked_allowed : int;
538 pragma Convention (C, pthread_attr_t);
540 type pthread_condattr_t is record
542 process_shared : int;
544 pragma Convention (C, pthread_condattr_t);
546 type pthread_mutexattr_t is record
547 is_initialized : int;
548 process_shared : int;
553 pragma Convention (C, pthread_mutexattr_t);
555 type pthread_t is new rtems_id;
557 type pthread_mutex_t is new rtems_id;
559 type pthread_cond_t is new rtems_id;
561 type pthread_key_t is new rtems_id;
563 No_Key : constant pthread_key_t := 0;
565 end System.OS_Interface;