1------------------------------------------------------------------------------ 2-- -- 3-- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS -- 4-- -- 5-- S Y S T E M . O S _ I N T E R F A C E -- 6-- -- 7-- S p e c -- 8-- -- 9-- Copyright (C) 1991-1994, Florida State University -- 10-- Copyright (C) 1995-2020, Free Software Foundation, Inc. -- 11-- -- 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. -- 18-- -- 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. -- 22-- -- 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/>. -- 27-- -- 28-- GNARL was developed by the GNARL team at Florida State University. -- 29-- Extensive contributions were provided by Ada Core Technologies, Inc. -- 30-- -- 31------------------------------------------------------------------------------ 32 33-- This is the GNU/kFreeBSD (POSIX Threads) version of this package 34 35-- This package encapsulates all direct interfaces to OS services 36-- that are needed by children of System. 37 38-- PLEASE DO NOT add any with-clauses to this package or remove the pragma 39-- Preelaborate. This package is designed to be a bottom-level (leaf) package 40 41with Interfaces.C; 42with Unchecked_Conversion; 43 44package System.OS_Interface is 45 pragma Preelaborate; 46 47 pragma Linker_Options ("-lpthread"); 48 49 subtype int is Interfaces.C.int; 50 subtype char is Interfaces.C.char; 51 subtype short is Interfaces.C.short; 52 subtype long is Interfaces.C.long; 53 subtype unsigned is Interfaces.C.unsigned; 54 subtype unsigned_short is Interfaces.C.unsigned_short; 55 subtype unsigned_long is Interfaces.C.unsigned_long; 56 subtype unsigned_char is Interfaces.C.unsigned_char; 57 subtype plain_char is Interfaces.C.plain_char; 58 subtype size_t is Interfaces.C.size_t; 59 60 ----------- 61 -- Errno -- 62 ----------- 63 64 function errno return int; 65 pragma Import (C, errno, "__get_errno"); 66 67 EAGAIN : constant := 35; 68 EINTR : constant := 4; 69 EINVAL : constant := 22; 70 ENOMEM : constant := 12; 71 EPERM : constant := 1; 72 ETIMEDOUT : constant := 60; 73 74 ------------- 75 -- Signals -- 76 ------------- 77 78 Max_Interrupt : constant := 128; 79 type Signal is new int range 0 .. Max_Interrupt; 80 for Signal'Size use int'Size; 81 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 115 SIGLTHRRES : constant := 32; -- GNU/LinuxThreads restart signal 116 SIGLTHRCAN : constant := 33; -- GNU/LinuxThreads cancel signal 117 SIGLTHRDBG : constant := 34; -- GNU/LinuxThreads debugger signal 118 119 SIGADAABORT : constant := SIGABRT; 120 -- Change this if you want to use another signal for task abort. 121 -- SIGTERM might be a good one. 122 123 type Signal_Set is array (Natural range <>) of Signal; 124 125 Unmasked : constant Signal_Set := ( 126 SIGTRAP, 127 -- To enable debugging on multithreaded applications, mark SIGTRAP to 128 -- be kept unmasked. 129 130 SIGBUS, 131 132 SIGTTIN, SIGTTOU, SIGTSTP, 133 -- Keep these three signals unmasked so that background processes 134 -- and IO behaves as normal "C" applications 135 136 SIGPROF, 137 -- To avoid confusing the profiler 138 139 SIGKILL, SIGSTOP, 140 -- These two signals actually cannot be masked; 141 -- POSIX simply won't allow it. 142 143 SIGLTHRRES, SIGLTHRCAN, SIGLTHRDBG); 144 -- These three signals are used by GNU/LinuxThreads starting from 145 -- glibc 2.1 (future 2.2). 146 147 Reserved : constant Signal_Set := 148 -- I am not sure why the following signal is reserved. 149 -- I guess they are not supported by this version of GNU/kFreeBSD. 150 (0 .. 0 => SIGVTALRM); 151 152 type sigset_t is private; 153 154 function sigaddset (set : access sigset_t; sig : Signal) return int; 155 pragma Import (C, sigaddset, "sigaddset"); 156 157 function sigdelset (set : access sigset_t; sig : Signal) return int; 158 pragma Import (C, sigdelset, "sigdelset"); 159 160 function sigfillset (set : access sigset_t) return int; 161 pragma Import (C, sigfillset, "sigfillset"); 162 163 function sigismember (set : access sigset_t; sig : Signal) return int; 164 pragma Import (C, sigismember, "sigismember"); 165 166 function sigemptyset (set : access sigset_t) return int; 167 pragma Import (C, sigemptyset, "sigemptyset"); 168 169 -- sigcontext is architecture dependent, so define it private 170 type struct_sigcontext is private; 171 172 type struct_sigaction is record 173 sa_handler : System.Address; 174 sa_flags : int; 175 sa_mask : sigset_t; 176 end record; 177 pragma Convention (C, struct_sigaction); 178 179 type struct_sigaction_ptr is access all struct_sigaction; 180 181 SIG_BLOCK : constant := 1; 182 SIG_UNBLOCK : constant := 2; 183 SIG_SETMASK : constant := 3; 184 185 SIG_DFL : constant := 0; 186 SIG_IGN : constant := 1; 187 188 SA_SIGINFO : constant := 16#0040#; 189 SA_ONSTACK : constant := 16#0001#; 190 191 function sigaction 192 (sig : Signal; 193 act : struct_sigaction_ptr; 194 oact : struct_sigaction_ptr) return int; 195 pragma Import (C, sigaction, "sigaction"); 196 197 ---------- 198 -- Time -- 199 ---------- 200 201 Time_Slice_Supported : constant Boolean := True; 202 -- Indicates whether time slicing is supported (i.e SCHED_RR is supported) 203 204 type timespec is private; 205 206 function nanosleep (rqtp, rmtp : access timespec) return int; 207 pragma Import (C, nanosleep, "nanosleep"); 208 209 type clockid_t is new int; 210 CLOCK_REALTIME : constant clockid_t := 0; 211 212 function clock_gettime 213 (clock_id : clockid_t; 214 tp : access timespec) 215 return int; 216 pragma Import (C, clock_gettime, "clock_gettime"); 217 218 function clock_getres 219 (clock_id : clockid_t; 220 res : access timespec) return int; 221 pragma Import (C, clock_getres, "clock_getres"); 222 223 function To_Duration (TS : timespec) return Duration; 224 pragma Inline (To_Duration); 225 226 function To_Timespec (D : Duration) return timespec; 227 pragma Inline (To_Timespec); 228 229 function sysconf (name : int) return long; 230 pragma Import (C, sysconf); 231 232 SC_CLK_TCK : constant := 2; 233 SC_NPROCESSORS_ONLN : constant := 84; 234 235 ------------------------- 236 -- Priority Scheduling -- 237 ------------------------- 238 239 SCHED_FIFO : constant := 1; 240 SCHED_OTHER : constant := 2; 241 SCHED_RR : constant := 3; 242 243 function To_Target_Priority 244 (Prio : System.Any_Priority) return Interfaces.C.int; 245 -- Maps System.Any_Priority to a POSIX priority. 246 247 ------------- 248 -- Process -- 249 ------------- 250 251 type pid_t is private; 252 253 function kill (pid : pid_t; sig : Signal) return int; 254 pragma Import (C, kill, "kill"); 255 256 function getpid return pid_t; 257 pragma Import (C, getpid, "getpid"); 258 259 --------- 260 -- LWP -- 261 --------- 262 263 function lwp_self return System.Address; 264 -- lwp_self does not exist on this thread library, revert to pthread_self 265 -- which is the closest approximation (with getpid). This function is 266 -- needed to share 7staprop.adb across POSIX-like targets. 267 pragma Import (C, lwp_self, "pthread_self"); 268 269 ------------- 270 -- Threads -- 271 ------------- 272 273 type Thread_Body is access 274 function (arg : System.Address) return System.Address; 275 pragma Convention (C, Thread_Body); 276 277 function Thread_Body_Access is new 278 Unchecked_Conversion (System.Address, Thread_Body); 279 280 type pthread_t is new unsigned_long; 281 subtype Thread_Id is pthread_t; 282 283 function To_pthread_t is new Unchecked_Conversion 284 (unsigned_long, pthread_t); 285 286 type pthread_mutex_t is limited private; 287 type pthread_cond_t is limited private; 288 type pthread_attr_t is limited private; 289 type pthread_mutexattr_t is limited private; 290 type pthread_condattr_t is limited private; 291 type pthread_key_t is private; 292 293 PTHREAD_CREATE_DETACHED : constant := 1; 294 PTHREAD_CREATE_JOINABLE : constant := 0; 295 296 PTHREAD_SCOPE_PROCESS : constant := 0; 297 PTHREAD_SCOPE_SYSTEM : constant := 2; 298 299 -- Read/Write lock not supported on kfreebsd. To add support both types 300 -- pthread_rwlock_t and pthread_rwlockattr_t must properly be defined 301 -- with the associated routines pthread_rwlock_[init/destroy] and 302 -- pthread_rwlock_[rdlock/wrlock/unlock]. 303 304 subtype pthread_rwlock_t is pthread_mutex_t; 305 subtype pthread_rwlockattr_t is pthread_mutexattr_t; 306 307 ----------- 308 -- Stack -- 309 ----------- 310 311 type stack_t is record 312 ss_sp : System.Address; 313 ss_size : size_t; 314 ss_flags : int; 315 end record; 316 pragma Convention (C, stack_t); 317 318 function sigaltstack 319 (ss : not null access stack_t; 320 oss : access stack_t) return int; 321 pragma Import (C, sigaltstack, "sigaltstack"); 322 323 Alternate_Stack : aliased System.Address; 324 -- This is a dummy definition, never used (Alternate_Stack_Size is null) 325 326 Alternate_Stack_Size : constant := 0; 327 -- No alternate signal stack is used on this platform 328 329 Stack_Base_Available : constant Boolean := False; 330 -- Indicates whether the stack base is available on this target 331 332 function Get_Stack_Base (thread : pthread_t) return Address; 333 pragma Inline (Get_Stack_Base); 334 -- returns the stack base of the specified thread. Only call this function 335 -- when Stack_Base_Available is True. 336 337 function Get_Page_Size return int; 338 pragma Import (C, Get_Page_Size, "getpagesize"); 339 -- Returns the size of a page 340 341 PROT_NONE : constant := 0; 342 PROT_READ : constant := 1; 343 PROT_WRITE : constant := 2; 344 PROT_EXEC : constant := 4; 345 PROT_ALL : constant := PROT_READ + PROT_WRITE + PROT_EXEC; 346 PROT_ON : constant := PROT_NONE; 347 PROT_OFF : constant := PROT_ALL; 348 349 function mprotect (addr : Address; len : size_t; prot : int) return int; 350 pragma Import (C, mprotect); 351 352 --------------------------------------- 353 -- Nonstandard Thread Initialization -- 354 --------------------------------------- 355 356 procedure pthread_init; 357 pragma Inline (pthread_init); 358 -- This is a dummy procedure to share some GNULLI files 359 360 ------------------------- 361 -- POSIX.1c Section 3 -- 362 ------------------------- 363 364 function sigwait (set : access sigset_t; sig : access Signal) return int; 365 pragma Import (C, sigwait, "sigwait"); 366 367 function pthread_kill (thread : pthread_t; sig : Signal) return int; 368 pragma Import (C, pthread_kill, "pthread_kill"); 369 370 function pthread_sigmask 371 (how : int; 372 set : access sigset_t; 373 oset : access sigset_t) return int; 374 pragma Import (C, pthread_sigmask, "pthread_sigmask"); 375 376 -------------------------- 377 -- POSIX.1c Section 11 -- 378 -------------------------- 379 380 function pthread_mutexattr_init 381 (attr : access pthread_mutexattr_t) return int; 382 pragma Import (C, pthread_mutexattr_init, "pthread_mutexattr_init"); 383 384 function pthread_mutexattr_destroy 385 (attr : access pthread_mutexattr_t) return int; 386 pragma Import (C, pthread_mutexattr_destroy, "pthread_mutexattr_destroy"); 387 388 function pthread_mutex_init 389 (mutex : access pthread_mutex_t; 390 attr : access pthread_mutexattr_t) return int; 391 pragma Import (C, pthread_mutex_init, "pthread_mutex_init"); 392 393 function pthread_mutex_destroy (mutex : access pthread_mutex_t) return int; 394 pragma Import (C, pthread_mutex_destroy, "pthread_mutex_destroy"); 395 396 function pthread_mutex_lock (mutex : access pthread_mutex_t) return int; 397 pragma Import (C, pthread_mutex_lock, "pthread_mutex_lock"); 398 399 function pthread_mutex_unlock (mutex : access pthread_mutex_t) return int; 400 pragma Import (C, pthread_mutex_unlock, "pthread_mutex_unlock"); 401 402 function pthread_condattr_init 403 (attr : access pthread_condattr_t) return int; 404 pragma Import (C, pthread_condattr_init, "pthread_condattr_init"); 405 406 function pthread_condattr_destroy 407 (attr : access pthread_condattr_t) return int; 408 pragma Import (C, pthread_condattr_destroy, "pthread_condattr_destroy"); 409 410 function pthread_cond_init 411 (cond : access pthread_cond_t; 412 attr : access pthread_condattr_t) return int; 413 pragma Import (C, pthread_cond_init, "pthread_cond_init"); 414 415 function pthread_cond_destroy (cond : access pthread_cond_t) return int; 416 pragma Import (C, pthread_cond_destroy, "pthread_cond_destroy"); 417 418 function pthread_cond_signal (cond : access pthread_cond_t) return int; 419 pragma Import (C, pthread_cond_signal, "pthread_cond_signal"); 420 421 function pthread_cond_wait 422 (cond : access pthread_cond_t; 423 mutex : access pthread_mutex_t) return int; 424 pragma Import (C, pthread_cond_wait, "pthread_cond_wait"); 425 426 function pthread_cond_timedwait 427 (cond : access pthread_cond_t; 428 mutex : access pthread_mutex_t; 429 abstime : access timespec) return int; 430 pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait"); 431 432 -------------------------- 433 -- POSIX.1c Section 13 -- 434 -------------------------- 435 436 PTHREAD_PRIO_NONE : constant := 0; 437 PTHREAD_PRIO_PROTECT : constant := 2; 438 PTHREAD_PRIO_INHERIT : constant := 1; 439 440 function pthread_mutexattr_setprotocol 441 (attr : access pthread_mutexattr_t; 442 protocol : int) return int; 443 pragma Import 444 (C, pthread_mutexattr_setprotocol, "pthread_mutexattr_setprotocol"); 445 446 function pthread_mutexattr_getprotocol 447 (attr : access pthread_mutexattr_t; 448 protocol : access int) return int; 449 pragma Import 450 (C, pthread_mutexattr_getprotocol, "pthread_mutexattr_getprotocol"); 451 452 function pthread_mutexattr_setprioceiling 453 (attr : access pthread_mutexattr_t; 454 prioceiling : int) return int; 455 pragma Import 456 (C, pthread_mutexattr_setprioceiling, 457 "pthread_mutexattr_setprioceiling"); 458 459 function pthread_mutexattr_getprioceiling 460 (attr : access pthread_mutexattr_t; 461 prioceiling : access int) return int; 462 pragma Import 463 (C, pthread_mutexattr_getprioceiling, 464 "pthread_mutexattr_getprioceiling"); 465 466 type struct_sched_param is record 467 sched_priority : int; -- scheduling priority 468 end record; 469 pragma Convention (C, struct_sched_param); 470 471 function pthread_setschedparam 472 (thread : pthread_t; 473 policy : int; 474 param : access struct_sched_param) return int; 475 pragma Import (C, pthread_setschedparam, "pthread_setschedparam"); 476 477 function pthread_attr_setscope 478 (attr : access pthread_attr_t; 479 contentionscope : int) return int; 480 pragma Import (C, pthread_attr_setscope, "pthread_attr_setscope"); 481 482 function pthread_attr_getscope 483 (attr : access pthread_attr_t; 484 contentionscope : access int) return int; 485 pragma Import (C, pthread_attr_getscope, "pthread_attr_getscope"); 486 487 function pthread_attr_setinheritsched 488 (attr : access pthread_attr_t; 489 inheritsched : int) return int; 490 pragma Import 491 (C, pthread_attr_setinheritsched, "pthread_attr_setinheritsched"); 492 493 function pthread_attr_getinheritsched 494 (attr : access pthread_attr_t; 495 inheritsched : access int) return int; 496 pragma Import 497 (C, pthread_attr_getinheritsched, "pthread_attr_getinheritsched"); 498 499 function pthread_attr_setschedpolicy 500 (attr : access pthread_attr_t; 501 policy : int) return int; 502 pragma Import 503 (C, pthread_attr_setschedpolicy, "pthread_attr_setschedpolicy"); 504 505 function sched_yield return int; 506 pragma Import (C, sched_yield, "sched_yield"); 507 508 --------------------------- 509 -- P1003.1c - Section 16 -- 510 --------------------------- 511 512 function pthread_attr_init 513 (attributes : access pthread_attr_t) return int; 514 pragma Import (C, pthread_attr_init, "pthread_attr_init"); 515 516 function pthread_attr_destroy 517 (attributes : access pthread_attr_t) return int; 518 pragma Import (C, pthread_attr_destroy, "pthread_attr_destroy"); 519 520 function pthread_attr_setdetachstate 521 (attr : access pthread_attr_t; 522 detachstate : int) return int; 523 pragma Import 524 (C, pthread_attr_setdetachstate, "pthread_attr_setdetachstate"); 525 526 function pthread_attr_setstacksize 527 (attr : access pthread_attr_t; 528 stacksize : size_t) return int; 529 pragma Import (C, pthread_attr_setstacksize, "pthread_attr_setstacksize"); 530 531 function pthread_create 532 (thread : access pthread_t; 533 attributes : access pthread_attr_t; 534 start_routine : Thread_Body; 535 arg : System.Address) return int; 536 pragma Import (C, pthread_create, "pthread_create"); 537 538 procedure pthread_exit (status : System.Address); 539 pragma Import (C, pthread_exit, "pthread_exit"); 540 541 function pthread_self return pthread_t; 542 pragma Import (C, pthread_self, "pthread_self"); 543 544 -------------------------- 545 -- POSIX.1c Section 17 -- 546 -------------------------- 547 548 function pthread_setspecific 549 (key : pthread_key_t; 550 value : System.Address) return int; 551 pragma Import (C, pthread_setspecific, "pthread_setspecific"); 552 553 function pthread_getspecific (key : pthread_key_t) return System.Address; 554 pragma Import (C, pthread_getspecific, "pthread_getspecific"); 555 556 type destructor_pointer is access procedure (arg : System.Address); 557 pragma Convention (C, destructor_pointer); 558 559 function pthread_key_create 560 (key : access pthread_key_t; 561 destructor : destructor_pointer) return int; 562 pragma Import (C, pthread_key_create, "pthread_key_create"); 563 564 CPU_SETSIZE : constant := 1_024; 565 566 type bit_field is array (1 .. CPU_SETSIZE) of Boolean; 567 for bit_field'Size use CPU_SETSIZE; 568 pragma Pack (bit_field); 569 pragma Convention (C, bit_field); 570 571 type cpu_set_t is record 572 bits : bit_field; 573 end record; 574 pragma Convention (C, cpu_set_t); 575 576 function pthread_setaffinity_np 577 (thread : pthread_t; 578 cpusetsize : size_t; 579 cpuset : access cpu_set_t) return int; 580 pragma Import (C, pthread_setaffinity_np, "__gnat_pthread_setaffinity_np"); 581 582private 583 584 type sigset_t is array (1 .. 4) of unsigned; 585 586 -- In FreeBSD the component sa_handler turns out to 587 -- be one a union type, and the selector is a macro: 588 -- #define sa_handler __sigaction_u._handler 589 -- #define sa_sigaction __sigaction_u._sigaction 590 591 -- Should we add a signal_context type here ? 592 -- How could it be done independent of the CPU architecture ? 593 -- sigcontext type is opaque, so it is architecturally neutral. 594 -- It is always passed as an access type, so define it as an empty record 595 -- since the contents are not used anywhere. 596 type struct_sigcontext is null record; 597 pragma Convention (C, struct_sigcontext); 598 599 type pid_t is new int; 600 601 type time_t is new long; 602 603 type timespec is record 604 tv_sec : time_t; 605 tv_nsec : long; 606 end record; 607 pragma Convention (C, timespec); 608 609 type pthread_attr_t is record 610 detachstate : int; 611 schedpolicy : int; 612 schedparam : struct_sched_param; 613 inheritsched : int; 614 scope : int; 615 guardsize : size_t; 616 stackaddr_set : int; 617 stackaddr : System.Address; 618 stacksize : size_t; 619 end record; 620 pragma Convention (C, pthread_attr_t); 621 622 type pthread_condattr_t is record 623 dummy : int; 624 end record; 625 pragma Convention (C, pthread_condattr_t); 626 627 type pthread_mutexattr_t is record 628 mutexkind : int; 629 end record; 630 pragma Convention (C, pthread_mutexattr_t); 631 632 type struct_pthread_fast_lock is record 633 status : long; 634 spinlock : int; 635 end record; 636 pragma Convention (C, struct_pthread_fast_lock); 637 638 type pthread_mutex_t is record 639 m_reserved : int; 640 m_count : int; 641 m_owner : System.Address; 642 m_kind : int; 643 m_lock : struct_pthread_fast_lock; 644 end record; 645 pragma Convention (C, pthread_mutex_t); 646 647 type pthread_cond_t is array (0 .. 47) of unsigned_char; 648 pragma Convention (C, pthread_cond_t); 649 650 type pthread_key_t is new unsigned; 651 652end System.OS_Interface; 653