| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . T A S K _ P R I M I T I V E S . O P E R A T I O N S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNARL was developed by the GNARL team at Florida State University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies, Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- This is a POSIX-like version of this package |
| 33 |
|
| 34 |
-- This package contains all the GNULL primitives that interface directly with |
| 35 |
-- the underlying OS. |
| 36 |
|
| 37 |
-- Note: this file can only be used for POSIX compliant systems that implement |
| 38 |
-- SCHED_FIFO and Ceiling Locking correctly. |
| 39 |
|
| 40 |
-- For configurations where SCHED_FIFO and priority ceiling are not a |
| 41 |
-- requirement, this file can also be used (e.g AiX threads) |
| 42 |
|
| 43 |
with Ada.Unchecked_Conversion; |
| 44 |
|
| 45 |
with Interfaces.C; |
| 46 |
|
| 47 |
with System.Interrupt_Management; |
| 48 |
with System.Multiprocessors; |
| 49 |
with System.OS_Constants; |
| 50 |
with System.OS_Primitives; |
| 51 |
with System.Task_Info; |
| 52 |
with System.Tasking.Debug; |
| 53 |
|
| 54 |
with System.Soft_Links; |
| 55 |
-- We use System.Soft_Links instead of System.Tasking.Initialization |
| 56 |
-- because the later is a higher level package that we shouldn't depend on. |
| 57 |
-- For example when using the restricted run time, it is replaced by |
| 58 |
-- System.Tasking.Restricted.Stages. |
| 59 |
|
| 60 |
package body System.Task_Primitives.Operations is |
| 61 |
|
| 62 |
package OSC renames System.OS_Constants; |
| 63 |
package SSL renames System.Soft_Links; |
| 64 |
|
| 65 |
use Interfaces.C; |
| 66 |
|
| 67 |
use System.OS_Interface; |
| 68 |
use System.OS_Locks; |
| 69 |
use System.OS_Primitives; |
| 70 |
use System.Parameters; |
| 71 |
use System.Tasking; |
| 72 |
use System.Tasking.Debug; |
| 73 |
|
| 74 |
---------------- |
| 75 |
-- Local Data -- |
| 76 |
---------------- |
| 77 |
|
| 78 |
-- The followings are logically constants, but need to be initialized |
| 79 |
-- at run time. |
| 80 |
|
| 81 |
Single_RTS_Lock : aliased RTS_Lock; |
| 82 |
-- This is a lock to allow only one thread of control in the RTS at |
| 83 |
-- a time; it is used to execute in mutual exclusion from all other tasks. |
| 84 |
-- Used to protect All_Tasks_List |
| 85 |
|
| 86 |
Environment_Task_Id : Task_Id; |
| 87 |
-- A variable to hold Task_Id for the environment task |
| 88 |
|
| 89 |
Locking_Policy : constant Character; |
| 90 |
pragma Import (C, Locking_Policy, "__gl_locking_policy"); |
| 91 |
-- Value of the pragma Locking_Policy: |
| 92 |
-- 'C' for Ceiling_Locking |
| 93 |
-- 'I' for Inherit_Locking |
| 94 |
-- ' ' for none. |
| 95 |
|
| 96 |
Unblocked_Signal_Mask : aliased sigset_t; |
| 97 |
-- The set of signals that should unblocked in all tasks |
| 98 |
|
| 99 |
-- The followings are internal configuration constants needed |
| 100 |
|
| 101 |
Next_Serial_Number : Task_Serial_Number := 100; |
| 102 |
-- We start at 100, to reserve some special values for |
| 103 |
-- using in error checking. |
| 104 |
|
| 105 |
Time_Slice_Val : constant Integer; |
| 106 |
pragma Import (C, Time_Slice_Val, "__gl_time_slice_val"); |
| 107 |
|
| 108 |
Dispatching_Policy : constant Character; |
| 109 |
pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy"); |
| 110 |
|
| 111 |
Foreign_Task_Elaborated : aliased Boolean := True; |
| 112 |
-- Used to identified fake tasks (i.e., non-Ada Threads) |
| 113 |
|
| 114 |
Use_Alternate_Stack : constant Boolean := Alternate_Stack_Size /= 0; |
| 115 |
-- Whether to use an alternate signal stack for stack overflows |
| 116 |
|
| 117 |
Abort_Handler_Installed : Boolean := False; |
| 118 |
-- True if a handler for the abort signal is installed |
| 119 |
|
| 120 |
function Initialize_Lock |
| 121 |
(L : not null access RTS_Lock; |
| 122 |
Prio : Any_Priority) return Interfaces.C.int; |
| 123 |
-- Initialize the lock L. If Ceiling_Support is True, then set the ceiling |
| 124 |
-- to Prio. Returns 0 for success, or ENOMEM for out-of-memory. |
| 125 |
|
| 126 |
function Get_Policy (Prio : System.Any_Priority) return Character; |
| 127 |
pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching"); |
| 128 |
-- Get priority specific dispatching policy |
| 129 |
|
| 130 |
-------------------- |
| 131 |
-- Local Packages -- |
| 132 |
-------------------- |
| 133 |
|
| 134 |
package Specific is |
| 135 |
|
| 136 |
procedure Initialize (Environment_Task : Task_Id); |
| 137 |
pragma Inline (Initialize); |
| 138 |
-- Initialize various data needed by this package |
| 139 |
|
| 140 |
function Is_Valid_Task return Boolean; |
| 141 |
pragma Inline (Is_Valid_Task); |
| 142 |
-- Does executing thread have a TCB? |
| 143 |
|
| 144 |
procedure Set (Self_Id : Task_Id); |
| 145 |
pragma Inline (Set); |
| 146 |
-- Set the self id for the current task |
| 147 |
|
| 148 |
function Self return Task_Id; |
| 149 |
pragma Inline (Self); |
| 150 |
-- Return a pointer to the Ada Task Control Block of the calling task |
| 151 |
|
| 152 |
end Specific; |
| 153 |
|
| 154 |
package body Specific is separate; |
| 155 |
-- The body of this package is target specific |
| 156 |
|
| 157 |
package Monotonic is |
| 158 |
|
| 159 |
function Monotonic_Clock return Duration; |
| 160 |
pragma Inline (Monotonic_Clock); |
| 161 |
-- Returns an absolute time, represented as an offset relative to some |
| 162 |
-- unspecified starting point, typically system boot time. This clock |
| 163 |
-- is not affected by discontinuous jumps in the system time. |
| 164 |
|
| 165 |
function RT_Resolution return Duration; |
| 166 |
pragma Inline (RT_Resolution); |
| 167 |
-- Returns resolution of the underlying clock used to implement RT_Clock |
| 168 |
|
| 169 |
procedure Timed_Sleep |
| 170 |
(Self_ID : ST.Task_Id; |
| 171 |
Time : Duration; |
| 172 |
Mode : ST.Delay_Modes; |
| 173 |
Reason : System.Tasking.Task_States; |
| 174 |
Timedout : out Boolean; |
| 175 |
Yielded : out Boolean); |
| 176 |
-- Combination of Sleep (above) and Timed_Delay |
| 177 |
|
| 178 |
procedure Timed_Delay |
| 179 |
(Self_ID : ST.Task_Id; |
| 180 |
Time : Duration; |
| 181 |
Mode : ST.Delay_Modes); |
| 182 |
-- Implement the semantics of the delay statement. |
| 183 |
-- The caller should be abort-deferred and should not hold any locks. |
| 184 |
|
| 185 |
end Monotonic; |
| 186 |
|
| 187 |
package body Monotonic is separate; |
| 188 |
|
| 189 |
---------------------------------- |
| 190 |
-- ATCB allocation/deallocation -- |
| 191 |
---------------------------------- |
| 192 |
|
| 193 |
package body ATCB_Allocation is separate; |
| 194 |
-- The body of this package is shared across several targets |
| 195 |
|
| 196 |
--------------------------------- |
| 197 |
-- Support for foreign threads -- |
| 198 |
--------------------------------- |
| 199 |
|
| 200 |
function Register_Foreign_Thread |
| 201 |
(Thread : Thread_Id; |
| 202 |
Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id; |
| 203 |
-- Allocate and initialize a new ATCB for the current Thread. The size of |
| 204 |
-- the secondary stack can be optionally specified. |
| 205 |
|
| 206 |
function Register_Foreign_Thread |
| 207 |
(Thread : Thread_Id; |
| 208 |
Sec_Stack_Size : Size_Type := Unspecified_Size) |
| 209 |
return Task_Id is separate; |
| 210 |
|
| 211 |
----------------------- |
| 212 |
-- Local Subprograms -- |
| 213 |
----------------------- |
| 214 |
|
| 215 |
procedure Abort_Handler (Sig : Signal); |
| 216 |
-- Signal handler used to implement asynchronous abort. |
| 217 |
-- See also comment before body, below. |
| 218 |
|
| 219 |
function To_Address is |
| 220 |
new Ada.Unchecked_Conversion (Task_Id, System.Address); |
| 221 |
|
| 222 |
function GNAT_pthread_condattr_setup |
| 223 |
(attr : access pthread_condattr_t) return Interfaces.C.int; |
| 224 |
pragma Import (C, |
| 225 |
GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup"); |
| 226 |
|
| 227 |
------------------- |
| 228 |
-- Abort_Handler -- |
| 229 |
------------------- |
| 230 |
|
| 231 |
-- Target-dependent binding of inter-thread Abort signal to the raising of |
| 232 |
-- the Abort_Signal exception. |
| 233 |
|
| 234 |
-- The technical issues and alternatives here are essentially the |
| 235 |
-- same as for raising exceptions in response to other signals |
| 236 |
-- (e.g. Storage_Error). See code and comments in the package body |
| 237 |
-- System.Interrupt_Management. |
| 238 |
|
| 239 |
-- Some implementations may not allow an exception to be propagated out of |
| 240 |
-- a handler, and others might leave the signal or interrupt that invoked |
| 241 |
-- this handler masked after the exceptional return to the application |
| 242 |
-- code. |
| 243 |
|
| 244 |
-- GNAT exceptions are originally implemented using setjmp()/longjmp(). On |
| 245 |
-- most UNIX systems, this will allow transfer out of a signal handler, |
| 246 |
-- which is usually the only mechanism available for implementing |
| 247 |
-- asynchronous handlers of this kind. However, some systems do not |
| 248 |
-- restore the signal mask on longjmp(), leaving the abort signal masked. |
| 249 |
|
| 250 |
procedure Abort_Handler (Sig : Signal) is |
| 251 |
pragma Unreferenced (Sig); |
| 252 |
|
| 253 |
T : constant Task_Id := Self; |
| 254 |
Old_Set : aliased sigset_t; |
| 255 |
|
| 256 |
Result : Interfaces.C.int; |
| 257 |
pragma Warnings (Off, Result); |
| 258 |
|
| 259 |
begin |
| 260 |
-- It's not safe to raise an exception when using GCC ZCX mechanism. |
| 261 |
-- Note that we still need to install a signal handler, since in some |
| 262 |
-- cases (e.g. shutdown of the Server_Task in System.Interrupts) we |
| 263 |
-- need to send the Abort signal to a task. |
| 264 |
|
| 265 |
if ZCX_By_Default then |
| 266 |
return; |
| 267 |
end if; |
| 268 |
|
| 269 |
if T.Deferral_Level = 0 |
| 270 |
and then T.Pending_ATC_Level < T.ATC_Nesting_Level and then |
| 271 |
not T.Aborting |
| 272 |
then |
| 273 |
T.Aborting := True; |
| 274 |
|
| 275 |
-- Make sure signals used for RTS internal purpose are unmasked |
| 276 |
|
| 277 |
Result := pthread_sigmask (SIG_UNBLOCK, |
| 278 |
Unblocked_Signal_Mask'Access, Old_Set'Access); |
| 279 |
pragma Assert (Result = 0); |
| 280 |
|
| 281 |
raise Standard'Abort_Signal; |
| 282 |
end if; |
| 283 |
end Abort_Handler; |
| 284 |
|
| 285 |
----------------- |
| 286 |
-- Stack_Guard -- |
| 287 |
----------------- |
| 288 |
|
| 289 |
procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is |
| 290 |
Stack_Base : constant Address := Get_Stack_Base (T.Common.LL.Thread); |
| 291 |
Page_Size : Address; |
| 292 |
Res : Interfaces.C.int; |
| 293 |
|
| 294 |
begin |
| 295 |
if Stack_Base_Available then |
| 296 |
|
| 297 |
-- Compute the guard page address |
| 298 |
|
| 299 |
Page_Size := Address (Get_Page_Size); |
| 300 |
Res := |
| 301 |
mprotect |
| 302 |
(Stack_Base - (Stack_Base mod Page_Size) + Page_Size, |
| 303 |
Interfaces.C.size_t (Page_Size), |
| 304 |
prot => (if On then PROT_ON else PROT_OFF)); |
| 305 |
pragma Assert (Res = 0); |
| 306 |
end if; |
| 307 |
end Stack_Guard; |
| 308 |
|
| 309 |
-------------------- |
| 310 |
-- Get_Thread_Id -- |
| 311 |
-------------------- |
| 312 |
|
| 313 |
function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is |
| 314 |
begin |
| 315 |
return T.Common.LL.Thread; |
| 316 |
end Get_Thread_Id; |
| 317 |
|
| 318 |
---------- |
| 319 |
-- Self -- |
| 320 |
---------- |
| 321 |
|
| 322 |
function Self return Task_Id renames Specific.Self; |
| 323 |
|
| 324 |
--------------------- |
| 325 |
-- Initialize_Lock -- |
| 326 |
--------------------- |
| 327 |
|
| 328 |
-- Note: mutexes and cond_variables needed per-task basis are initialized |
| 329 |
-- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such |
| 330 |
-- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any |
| 331 |
-- status change of RTS. Therefore raising Storage_Error in the following |
| 332 |
-- routines should be able to be handled safely. |
| 333 |
|
| 334 |
function Initialize_Lock |
| 335 |
(L : not null access RTS_Lock; |
| 336 |
Prio : Any_Priority) return Interfaces.C.int |
| 337 |
is |
| 338 |
Attributes : aliased pthread_mutexattr_t; |
| 339 |
Result : Interfaces.C.int; |
| 340 |
Result_2 : aliased Interfaces.C.int; |
| 341 |
|
| 342 |
begin |
| 343 |
Result := pthread_mutexattr_init (Attributes'Access); |
| 344 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 345 |
|
| 346 |
if Result = ENOMEM then |
| 347 |
return Result; |
| 348 |
end if; |
| 349 |
|
| 350 |
if Locking_Policy = 'C' then |
| 351 |
Result := pthread_mutexattr_setprotocol |
| 352 |
(Attributes'Access, PTHREAD_PRIO_PROTECT); |
| 353 |
pragma Assert (Result = 0); |
| 354 |
|
| 355 |
Result := pthread_mutexattr_getprotocol |
| 356 |
(Attributes'Access, Result_2'Access); |
| 357 |
if Result_2 /= PTHREAD_PRIO_PROTECT then |
| 358 |
raise Program_Error with "setprotocol failed"; |
| 359 |
end if; |
| 360 |
|
| 361 |
Result := pthread_mutexattr_setprioceiling |
| 362 |
(Attributes'Access, To_Target_Priority (Prio)); |
| 363 |
pragma Assert (Result = 0); |
| 364 |
|
| 365 |
elsif Locking_Policy = 'I' then |
| 366 |
Result := pthread_mutexattr_setprotocol |
| 367 |
(Attributes'Access, PTHREAD_PRIO_INHERIT); |
| 368 |
pragma Assert (Result = 0); |
| 369 |
end if; |
| 370 |
|
| 371 |
Result := pthread_mutex_init (L, Attributes'Access); |
| 372 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 373 |
|
| 374 |
Result_2 := pthread_mutexattr_destroy (Attributes'Access); |
| 375 |
pragma Assert (Result_2 = 0); |
| 376 |
|
| 377 |
return Result; |
| 378 |
end Initialize_Lock; |
| 379 |
|
| 380 |
procedure Initialize_Lock |
| 381 |
(Prio : System.Any_Priority; |
| 382 |
L : not null access Lock) |
| 383 |
is |
| 384 |
begin |
| 385 |
if Initialize_Lock (L.WO'Access, Prio) = ENOMEM then |
| 386 |
raise Storage_Error with "Failed to allocate a lock"; |
| 387 |
end if; |
| 388 |
end Initialize_Lock; |
| 389 |
|
| 390 |
procedure Initialize_Lock |
| 391 |
(L : not null access RTS_Lock; |
| 392 |
Level : Lock_Level) |
| 393 |
is |
| 394 |
pragma Unreferenced (Level); |
| 395 |
|
| 396 |
begin |
| 397 |
if Initialize_Lock (L, Any_Priority'Last) = ENOMEM then |
| 398 |
raise Storage_Error with "Failed to allocate a lock"; |
| 399 |
end if; |
| 400 |
end Initialize_Lock; |
| 401 |
|
| 402 |
------------------- |
| 403 |
-- Finalize_Lock -- |
| 404 |
------------------- |
| 405 |
|
| 406 |
procedure Finalize_Lock (L : not null access Lock) is |
| 407 |
Result : Interfaces.C.int; |
| 408 |
begin |
| 409 |
Result := pthread_mutex_destroy (L.WO'Access); |
| 410 |
pragma Assert (Result = 0); |
| 411 |
end Finalize_Lock; |
| 412 |
|
| 413 |
procedure Finalize_Lock (L : not null access RTS_Lock) is |
| 414 |
Result : Interfaces.C.int; |
| 415 |
begin |
| 416 |
Result := pthread_mutex_destroy (L); |
| 417 |
pragma Assert (Result = 0); |
| 418 |
end Finalize_Lock; |
| 419 |
|
| 420 |
---------------- |
| 421 |
-- Write_Lock -- |
| 422 |
---------------- |
| 423 |
|
| 424 |
procedure Write_Lock |
| 425 |
(L : not null access Lock; Ceiling_Violation : out Boolean) |
| 426 |
is |
| 427 |
Self : constant pthread_t := pthread_self; |
| 428 |
Result : Interfaces.C.int; |
| 429 |
Policy : aliased Interfaces.C.int; |
| 430 |
Ceiling : aliased Interfaces.C.int; |
| 431 |
Sched : aliased struct_sched_param; |
| 432 |
|
| 433 |
begin |
| 434 |
Result := pthread_mutex_lock (L.WO'Access); |
| 435 |
|
| 436 |
-- The cause of EINVAL is a priority ceiling violation |
| 437 |
|
| 438 |
Ceiling_Violation := Result = EINVAL; |
| 439 |
pragma Assert (Result = 0 or else Ceiling_Violation); |
| 440 |
|
| 441 |
-- Workaround bug in QNX on ceiling locks: tasks with priority higher |
| 442 |
-- than the ceiling priority don't receive EINVAL upon trying to lock. |
| 443 |
if Result = 0 and then Locking_Policy = 'C' then |
| 444 |
Result := pthread_getschedparam (Self, Policy'Access, Sched'Access); |
| 445 |
pragma Assert (Result = 0); |
| 446 |
Result := pthread_mutex_getprioceiling (L.WO'Access, Ceiling'Access); |
| 447 |
pragma Assert (Result = 0); |
| 448 |
|
| 449 |
-- Ceiling < current priority means Ceiling violation |
| 450 |
-- (otherwise the current priority == ceiling) |
| 451 |
if Ceiling < Sched.sched_curpriority then |
| 452 |
Ceiling_Violation := True; |
| 453 |
Result := pthread_mutex_unlock (L.WO'Access); |
| 454 |
pragma Assert (Result = 0); |
| 455 |
end if; |
| 456 |
end if; |
| 457 |
end Write_Lock; |
| 458 |
|
| 459 |
procedure Write_Lock (L : not null access RTS_Lock) is |
| 460 |
Result : Interfaces.C.int; |
| 461 |
begin |
| 462 |
Result := pthread_mutex_lock (L); |
| 463 |
pragma Assert (Result = 0); |
| 464 |
end Write_Lock; |
| 465 |
|
| 466 |
procedure Write_Lock (T : Task_Id) is |
| 467 |
Result : Interfaces.C.int; |
| 468 |
begin |
| 469 |
Result := pthread_mutex_lock (T.Common.LL.L'Access); |
| 470 |
pragma Assert (Result = 0); |
| 471 |
end Write_Lock; |
| 472 |
|
| 473 |
--------------- |
| 474 |
-- Read_Lock -- |
| 475 |
--------------- |
| 476 |
|
| 477 |
procedure Read_Lock |
| 478 |
(L : not null access Lock; Ceiling_Violation : out Boolean) is |
| 479 |
begin |
| 480 |
Write_Lock (L, Ceiling_Violation); |
| 481 |
end Read_Lock; |
| 482 |
|
| 483 |
------------ |
| 484 |
-- Unlock -- |
| 485 |
------------ |
| 486 |
|
| 487 |
procedure Unlock (L : not null access Lock) is |
| 488 |
Result : Interfaces.C.int; |
| 489 |
begin |
| 490 |
Result := pthread_mutex_unlock (L.WO'Access); |
| 491 |
pragma Assert (Result = 0); |
| 492 |
end Unlock; |
| 493 |
|
| 494 |
procedure Unlock (L : not null access RTS_Lock) is |
| 495 |
Result : Interfaces.C.int; |
| 496 |
begin |
| 497 |
Result := pthread_mutex_unlock (L); |
| 498 |
pragma Assert (Result = 0); |
| 499 |
end Unlock; |
| 500 |
|
| 501 |
procedure Unlock (T : Task_Id) is |
| 502 |
Result : Interfaces.C.int; |
| 503 |
begin |
| 504 |
Result := pthread_mutex_unlock (T.Common.LL.L'Access); |
| 505 |
pragma Assert (Result = 0); |
| 506 |
end Unlock; |
| 507 |
|
| 508 |
----------------- |
| 509 |
-- Set_Ceiling -- |
| 510 |
----------------- |
| 511 |
|
| 512 |
procedure Set_Ceiling |
| 513 |
(L : not null access Lock; |
| 514 |
Prio : System.Any_Priority) |
| 515 |
is |
| 516 |
Result : Interfaces.C.int; |
| 517 |
begin |
| 518 |
Result := pthread_mutex_setprioceiling |
| 519 |
(L.WO'Access, To_Target_Priority (Prio), null); |
| 520 |
pragma Assert (Result = 0); |
| 521 |
end Set_Ceiling; |
| 522 |
|
| 523 |
----------- |
| 524 |
-- Sleep -- |
| 525 |
----------- |
| 526 |
|
| 527 |
procedure Sleep |
| 528 |
(Self_ID : Task_Id; |
| 529 |
Reason : System.Tasking.Task_States) |
| 530 |
is |
| 531 |
pragma Unreferenced (Reason); |
| 532 |
|
| 533 |
Result : Interfaces.C.int; |
| 534 |
|
| 535 |
begin |
| 536 |
Result := |
| 537 |
pthread_cond_wait |
| 538 |
(cond => Self_ID.Common.LL.CV'Access, |
| 539 |
mutex => Self_ID.Common.LL.L'Access); |
| 540 |
|
| 541 |
-- EINTR is not considered a failure |
| 542 |
|
| 543 |
pragma Assert (Result = 0 or else Result = EINTR); |
| 544 |
end Sleep; |
| 545 |
|
| 546 |
----------------- |
| 547 |
-- Timed_Sleep -- |
| 548 |
----------------- |
| 549 |
|
| 550 |
-- This is for use within the run-time system, so abort is |
| 551 |
-- assumed to be already deferred, and the caller should be |
| 552 |
-- holding its own ATCB lock. |
| 553 |
|
| 554 |
procedure Timed_Sleep |
| 555 |
(Self_ID : Task_Id; |
| 556 |
Time : Duration; |
| 557 |
Mode : ST.Delay_Modes; |
| 558 |
Reason : Task_States; |
| 559 |
Timedout : out Boolean; |
| 560 |
Yielded : out Boolean) renames Monotonic.Timed_Sleep; |
| 561 |
|
| 562 |
----------------- |
| 563 |
-- Timed_Delay -- |
| 564 |
----------------- |
| 565 |
|
| 566 |
-- This is for use in implementing delay statements, so we assume the |
| 567 |
-- caller is abort-deferred but is holding no locks. |
| 568 |
|
| 569 |
procedure Timed_Delay |
| 570 |
(Self_ID : Task_Id; |
| 571 |
Time : Duration; |
| 572 |
Mode : ST.Delay_Modes) renames Monotonic.Timed_Delay; |
| 573 |
|
| 574 |
--------------------- |
| 575 |
-- Monotonic_Clock -- |
| 576 |
--------------------- |
| 577 |
|
| 578 |
function Monotonic_Clock return Duration renames Monotonic.Monotonic_Clock; |
| 579 |
|
| 580 |
------------------- |
| 581 |
-- RT_Resolution -- |
| 582 |
------------------- |
| 583 |
|
| 584 |
function RT_Resolution return Duration renames Monotonic.RT_Resolution; |
| 585 |
|
| 586 |
------------ |
| 587 |
-- Wakeup -- |
| 588 |
------------ |
| 589 |
|
| 590 |
procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is |
| 591 |
pragma Unreferenced (Reason); |
| 592 |
Result : Interfaces.C.int; |
| 593 |
begin |
| 594 |
Result := pthread_cond_signal (T.Common.LL.CV'Access); |
| 595 |
pragma Assert (Result = 0); |
| 596 |
end Wakeup; |
| 597 |
|
| 598 |
----------- |
| 599 |
-- Yield -- |
| 600 |
----------- |
| 601 |
|
| 602 |
procedure Yield (Do_Yield : Boolean := True) is |
| 603 |
Result : Interfaces.C.int; |
| 604 |
pragma Unreferenced (Result); |
| 605 |
begin |
| 606 |
if Do_Yield then |
| 607 |
Result := sched_yield; |
| 608 |
end if; |
| 609 |
end Yield; |
| 610 |
|
| 611 |
------------------ |
| 612 |
-- Set_Priority -- |
| 613 |
------------------ |
| 614 |
|
| 615 |
procedure Set_Priority |
| 616 |
(T : Task_Id; |
| 617 |
Prio : System.Any_Priority; |
| 618 |
Loss_Of_Inheritance : Boolean := False) |
| 619 |
is |
| 620 |
pragma Unreferenced (Loss_Of_Inheritance); |
| 621 |
Result : Interfaces.C.int; |
| 622 |
Old : constant System.Any_Priority := T.Common.Current_Priority; |
| 623 |
|
| 624 |
begin |
| 625 |
T.Common.Current_Priority := Prio; |
| 626 |
Result := pthread_setschedprio |
| 627 |
(T.Common.LL.Thread, To_Target_Priority (Prio)); |
| 628 |
pragma Assert (Result = 0); |
| 629 |
|
| 630 |
if T.Common.LL.Thread = pthread_self |
| 631 |
and then Old > Prio |
| 632 |
then |
| 633 |
-- When lowering the priority via a pthread_setschedprio, QNX ensures |
| 634 |
-- that the running thread remains in the head of the FIFO for tne |
| 635 |
-- new priority. Annex D expects the thread to be requeued so let's |
| 636 |
-- yield to the other threads of the same priority. |
| 637 |
Result := sched_yield; |
| 638 |
pragma Assert (Result = 0); |
| 639 |
end if; |
| 640 |
end Set_Priority; |
| 641 |
|
| 642 |
------------------ |
| 643 |
-- Get_Priority -- |
| 644 |
------------------ |
| 645 |
|
| 646 |
function Get_Priority (T : Task_Id) return System.Any_Priority is |
| 647 |
begin |
| 648 |
return T.Common.Current_Priority; |
| 649 |
end Get_Priority; |
| 650 |
|
| 651 |
---------------- |
| 652 |
-- Enter_Task -- |
| 653 |
---------------- |
| 654 |
|
| 655 |
procedure Enter_Task (Self_ID : Task_Id) is |
| 656 |
begin |
| 657 |
Self_ID.Common.LL.LWP := lwp_self; |
| 658 |
|
| 659 |
Specific.Set (Self_ID); |
| 660 |
|
| 661 |
if Use_Alternate_Stack then |
| 662 |
declare |
| 663 |
Stack : aliased stack_t; |
| 664 |
Result : Interfaces.C.int; |
| 665 |
begin |
| 666 |
Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack; |
| 667 |
Stack.ss_size := Alternate_Stack_Size; |
| 668 |
Stack.ss_flags := 0; |
| 669 |
Result := sigaltstack (Stack'Access, null); |
| 670 |
pragma Assert (Result = 0); |
| 671 |
end; |
| 672 |
end if; |
| 673 |
end Enter_Task; |
| 674 |
|
| 675 |
------------------- |
| 676 |
-- Is_Valid_Task -- |
| 677 |
------------------- |
| 678 |
|
| 679 |
function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task; |
| 680 |
|
| 681 |
----------------------------- |
| 682 |
-- Register_Foreign_Thread -- |
| 683 |
----------------------------- |
| 684 |
|
| 685 |
function Register_Foreign_Thread return Task_Id is |
| 686 |
begin |
| 687 |
if Is_Valid_Task then |
| 688 |
return Self; |
| 689 |
else |
| 690 |
return Register_Foreign_Thread (pthread_self); |
| 691 |
end if; |
| 692 |
end Register_Foreign_Thread; |
| 693 |
|
| 694 |
-------------------- |
| 695 |
-- Initialize_TCB -- |
| 696 |
-------------------- |
| 697 |
|
| 698 |
procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is |
| 699 |
Result : Interfaces.C.int; |
| 700 |
Cond_Attr : aliased pthread_condattr_t; |
| 701 |
|
| 702 |
begin |
| 703 |
-- Give the task a unique serial number |
| 704 |
|
| 705 |
Self_ID.Serial_Number := Next_Serial_Number; |
| 706 |
Next_Serial_Number := Next_Serial_Number + 1; |
| 707 |
pragma Assert (Next_Serial_Number /= 0); |
| 708 |
|
| 709 |
Result := |
| 710 |
Initialize_Lock (Self_ID.Common.LL.L'Access, Any_Priority'Last); |
| 711 |
pragma Assert (Result = 0); |
| 712 |
|
| 713 |
if Result /= 0 then |
| 714 |
Succeeded := False; |
| 715 |
return; |
| 716 |
end if; |
| 717 |
|
| 718 |
Result := pthread_condattr_init (Cond_Attr'Access); |
| 719 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 720 |
|
| 721 |
if Result = 0 then |
| 722 |
Result := GNAT_pthread_condattr_setup (Cond_Attr'Access); |
| 723 |
pragma Assert (Result = 0); |
| 724 |
|
| 725 |
Result := |
| 726 |
pthread_cond_init |
| 727 |
(Self_ID.Common.LL.CV'Access, Cond_Attr'Access); |
| 728 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 729 |
end if; |
| 730 |
|
| 731 |
if Result = 0 then |
| 732 |
Succeeded := True; |
| 733 |
else |
| 734 |
Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access); |
| 735 |
pragma Assert (Result = 0); |
| 736 |
|
| 737 |
Succeeded := False; |
| 738 |
end if; |
| 739 |
|
| 740 |
Result := pthread_condattr_destroy (Cond_Attr'Access); |
| 741 |
pragma Assert (Result = 0); |
| 742 |
end Initialize_TCB; |
| 743 |
|
| 744 |
----------------- |
| 745 |
-- Create_Task -- |
| 746 |
----------------- |
| 747 |
|
| 748 |
procedure Create_Task |
| 749 |
(T : Task_Id; |
| 750 |
Wrapper : System.Address; |
| 751 |
Stack_Size : System.Parameters.Size_Type; |
| 752 |
Priority : System.Any_Priority; |
| 753 |
Succeeded : out Boolean) |
| 754 |
is |
| 755 |
Attributes : aliased pthread_attr_t; |
| 756 |
Adjusted_Stack_Size : Interfaces.C.size_t; |
| 757 |
Page_Size : constant Interfaces.C.size_t := |
| 758 |
Interfaces.C.size_t (Get_Page_Size); |
| 759 |
Sched_Param : aliased struct_sched_param; |
| 760 |
Result : Interfaces.C.int; |
| 761 |
|
| 762 |
Priority_Specific_Policy : constant Character := Get_Policy (Priority); |
| 763 |
-- Upper case first character of the policy name corresponding to the |
| 764 |
-- task as set by a Priority_Specific_Dispatching pragma. |
| 765 |
|
| 766 |
function Thread_Body_Access is new |
| 767 |
Ada.Unchecked_Conversion (System.Address, Thread_Body); |
| 768 |
|
| 769 |
begin |
| 770 |
Adjusted_Stack_Size := |
| 771 |
Interfaces.C.size_t (Stack_Size + Alternate_Stack_Size); |
| 772 |
|
| 773 |
if Stack_Base_Available then |
| 774 |
|
| 775 |
-- If Stack Checking is supported then allocate 2 additional pages: |
| 776 |
|
| 777 |
-- In the worst case, stack is allocated at something like |
| 778 |
-- N * Get_Page_Size - epsilon, we need to add the size for 2 pages |
| 779 |
-- to be sure the effective stack size is greater than what |
| 780 |
-- has been asked. |
| 781 |
|
| 782 |
Adjusted_Stack_Size := Adjusted_Stack_Size + 2 * Page_Size; |
| 783 |
end if; |
| 784 |
|
| 785 |
-- Round stack size as this is required by some OSes (Darwin) |
| 786 |
|
| 787 |
Adjusted_Stack_Size := Adjusted_Stack_Size + Page_Size - 1; |
| 788 |
Adjusted_Stack_Size := |
| 789 |
Adjusted_Stack_Size - Adjusted_Stack_Size mod Page_Size; |
| 790 |
|
| 791 |
Result := pthread_attr_init (Attributes'Access); |
| 792 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 793 |
|
| 794 |
if Result /= 0 then |
| 795 |
Succeeded := False; |
| 796 |
return; |
| 797 |
end if; |
| 798 |
|
| 799 |
Result := |
| 800 |
pthread_attr_setdetachstate |
| 801 |
(Attributes'Access, PTHREAD_CREATE_DETACHED); |
| 802 |
pragma Assert (Result = 0); |
| 803 |
|
| 804 |
Result := |
| 805 |
pthread_attr_setstacksize |
| 806 |
(Attributes'Access, Adjusted_Stack_Size); |
| 807 |
pragma Assert (Result = 0); |
| 808 |
|
| 809 |
-- Set thread priority |
| 810 |
T.Common.Current_Priority := Priority; |
| 811 |
Sched_Param.sched_priority := To_Target_Priority (Priority); |
| 812 |
|
| 813 |
Result := pthread_attr_setinheritsched |
| 814 |
(Attributes'Access, PTHREAD_EXPLICIT_SCHED); |
| 815 |
pragma Assert (Result = 0); |
| 816 |
|
| 817 |
Result := pthread_attr_setschedparam |
| 818 |
(Attributes'Access, Sched_Param'Access); |
| 819 |
pragma Assert (Result = 0); |
| 820 |
|
| 821 |
if Time_Slice_Supported |
| 822 |
and then (Dispatching_Policy = 'R' |
| 823 |
or else Priority_Specific_Policy = 'R' |
| 824 |
or else Time_Slice_Val > 0) |
| 825 |
then |
| 826 |
Result := pthread_attr_setschedpolicy |
| 827 |
(Attributes'Access, SCHED_RR); |
| 828 |
|
| 829 |
elsif Dispatching_Policy = 'F' |
| 830 |
or else Priority_Specific_Policy = 'F' |
| 831 |
or else Time_Slice_Val = 0 |
| 832 |
then |
| 833 |
Result := pthread_attr_setschedpolicy |
| 834 |
(Attributes'Access, SCHED_FIFO); |
| 835 |
|
| 836 |
else |
| 837 |
Result := pthread_attr_setschedpolicy |
| 838 |
(Attributes'Access, SCHED_OTHER); |
| 839 |
end if; |
| 840 |
|
| 841 |
pragma Assert (Result = 0); |
| 842 |
|
| 843 |
-- Since the initial signal mask of a thread is inherited from the |
| 844 |
-- creator, and the Environment task has all its signals masked, we |
| 845 |
-- do not need to manipulate caller's signal mask at this point. |
| 846 |
-- All tasks in RTS will have All_Tasks_Mask initially. |
| 847 |
|
| 848 |
-- The write to T.Common.LL.Thread is not racy with regard to the |
| 849 |
-- created thread because the created thread will not access it until |
| 850 |
-- we release the RTS lock (or the current task's lock when |
| 851 |
-- Restricted.Stages is used). One can verify that by inspecting the |
| 852 |
-- Task_Wrapper procedures. |
| 853 |
|
| 854 |
Result := pthread_create |
| 855 |
(T.Common.LL.Thread'Access, |
| 856 |
Attributes'Access, |
| 857 |
Thread_Body_Access (Wrapper), |
| 858 |
To_Address (T)); |
| 859 |
pragma Assert (Result = 0 or else Result = EAGAIN); |
| 860 |
|
| 861 |
Succeeded := Result = 0; |
| 862 |
|
| 863 |
Result := pthread_attr_destroy (Attributes'Access); |
| 864 |
pragma Assert (Result = 0); |
| 865 |
end Create_Task; |
| 866 |
|
| 867 |
------------------ |
| 868 |
-- Finalize_TCB -- |
| 869 |
------------------ |
| 870 |
|
| 871 |
procedure Finalize_TCB (T : Task_Id) is |
| 872 |
Result : Interfaces.C.int; |
| 873 |
|
| 874 |
begin |
| 875 |
Result := pthread_mutex_destroy (T.Common.LL.L'Access); |
| 876 |
pragma Assert (Result = 0); |
| 877 |
|
| 878 |
Result := pthread_cond_destroy (T.Common.LL.CV'Access); |
| 879 |
pragma Assert (Result = 0); |
| 880 |
|
| 881 |
if T.Known_Tasks_Index /= -1 then |
| 882 |
Known_Tasks (T.Known_Tasks_Index) := null; |
| 883 |
end if; |
| 884 |
|
| 885 |
ATCB_Allocation.Free_ATCB (T); |
| 886 |
end Finalize_TCB; |
| 887 |
|
| 888 |
--------------- |
| 889 |
-- Exit_Task -- |
| 890 |
--------------- |
| 891 |
|
| 892 |
procedure Exit_Task is |
| 893 |
begin |
| 894 |
-- Mark this task as unknown, so that if Self is called, it won't |
| 895 |
-- return a dangling pointer. |
| 896 |
|
| 897 |
Specific.Set (null); |
| 898 |
end Exit_Task; |
| 899 |
|
| 900 |
---------------- |
| 901 |
-- Abort_Task -- |
| 902 |
---------------- |
| 903 |
|
| 904 |
procedure Abort_Task (T : Task_Id) is |
| 905 |
Result : Interfaces.C.int; |
| 906 |
begin |
| 907 |
if Abort_Handler_Installed then |
| 908 |
Result := |
| 909 |
pthread_kill |
| 910 |
(T.Common.LL.Thread, |
| 911 |
Signal (System.Interrupt_Management.Abort_Task_Interrupt)); |
| 912 |
pragma Assert (Result = 0); |
| 913 |
end if; |
| 914 |
end Abort_Task; |
| 915 |
|
| 916 |
---------------- |
| 917 |
-- Initialize -- |
| 918 |
---------------- |
| 919 |
|
| 920 |
procedure Initialize (S : in out Suspension_Object) is |
| 921 |
Mutex_Attr : aliased pthread_mutexattr_t; |
| 922 |
Cond_Attr : aliased pthread_condattr_t; |
| 923 |
Result : Interfaces.C.int; |
| 924 |
|
| 925 |
begin |
| 926 |
-- Initialize internal state (always to False (RM D.10 (6))) |
| 927 |
|
| 928 |
S.State := False; |
| 929 |
S.Waiting := False; |
| 930 |
|
| 931 |
-- Initialize internal mutex |
| 932 |
|
| 933 |
Result := pthread_mutexattr_init (Mutex_Attr'Access); |
| 934 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 935 |
|
| 936 |
if Result = ENOMEM then |
| 937 |
raise Storage_Error; |
| 938 |
end if; |
| 939 |
|
| 940 |
Result := pthread_mutex_init (S.L'Access, Mutex_Attr'Access); |
| 941 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 942 |
|
| 943 |
if Result = ENOMEM then |
| 944 |
Result := pthread_mutexattr_destroy (Mutex_Attr'Access); |
| 945 |
pragma Assert (Result = 0); |
| 946 |
|
| 947 |
raise Storage_Error; |
| 948 |
end if; |
| 949 |
|
| 950 |
Result := pthread_mutexattr_destroy (Mutex_Attr'Access); |
| 951 |
pragma Assert (Result = 0); |
| 952 |
|
| 953 |
-- Initialize internal condition variable |
| 954 |
|
| 955 |
Result := pthread_condattr_init (Cond_Attr'Access); |
| 956 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 957 |
|
| 958 |
if Result /= 0 then |
| 959 |
Result := pthread_mutex_destroy (S.L'Access); |
| 960 |
pragma Assert (Result = 0); |
| 961 |
|
| 962 |
-- Storage_Error is propagated as intended if the allocation of the |
| 963 |
-- underlying OS entities fails. |
| 964 |
|
| 965 |
raise Storage_Error; |
| 966 |
|
| 967 |
else |
| 968 |
Result := GNAT_pthread_condattr_setup (Cond_Attr'Access); |
| 969 |
pragma Assert (Result = 0); |
| 970 |
end if; |
| 971 |
|
| 972 |
Result := pthread_cond_init (S.CV'Access, Cond_Attr'Access); |
| 973 |
pragma Assert (Result = 0 or else Result = ENOMEM); |
| 974 |
|
| 975 |
if Result /= 0 then |
| 976 |
Result := pthread_mutex_destroy (S.L'Access); |
| 977 |
pragma Assert (Result = 0); |
| 978 |
|
| 979 |
Result := pthread_condattr_destroy (Cond_Attr'Access); |
| 980 |
pragma Assert (Result = 0); |
| 981 |
|
| 982 |
-- Storage_Error is propagated as intended if the allocation of the |
| 983 |
-- underlying OS entities fails. |
| 984 |
|
| 985 |
raise Storage_Error; |
| 986 |
end if; |
| 987 |
|
| 988 |
Result := pthread_condattr_destroy (Cond_Attr'Access); |
| 989 |
pragma Assert (Result = 0); |
| 990 |
end Initialize; |
| 991 |
|
| 992 |
-------------- |
| 993 |
-- Finalize -- |
| 994 |
-------------- |
| 995 |
|
| 996 |
procedure Finalize (S : in out Suspension_Object) is |
| 997 |
Result : Interfaces.C.int; |
| 998 |
|
| 999 |
begin |
| 1000 |
-- Destroy internal mutex |
| 1001 |
|
| 1002 |
Result := pthread_mutex_destroy (S.L'Access); |
| 1003 |
pragma Assert (Result = 0); |
| 1004 |
|
| 1005 |
-- Destroy internal condition variable |
| 1006 |
|
| 1007 |
Result := pthread_cond_destroy (S.CV'Access); |
| 1008 |
pragma Assert (Result = 0); |
| 1009 |
end Finalize; |
| 1010 |
|
| 1011 |
------------------- |
| 1012 |
-- Current_State -- |
| 1013 |
------------------- |
| 1014 |
|
| 1015 |
function Current_State (S : Suspension_Object) return Boolean is |
| 1016 |
begin |
| 1017 |
-- We do not want to use lock on this read operation. State is marked |
| 1018 |
-- as Atomic so that we ensure that the value retrieved is correct. |
| 1019 |
|
| 1020 |
return S.State; |
| 1021 |
end Current_State; |
| 1022 |
|
| 1023 |
--------------- |
| 1024 |
-- Set_False -- |
| 1025 |
--------------- |
| 1026 |
|
| 1027 |
procedure Set_False (S : in out Suspension_Object) is |
| 1028 |
Result : Interfaces.C.int; |
| 1029 |
|
| 1030 |
begin |
| 1031 |
SSL.Abort_Defer.all; |
| 1032 |
|
| 1033 |
Result := pthread_mutex_lock (S.L'Access); |
| 1034 |
pragma Assert (Result = 0); |
| 1035 |
|
| 1036 |
S.State := False; |
| 1037 |
|
| 1038 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1039 |
pragma Assert (Result = 0); |
| 1040 |
|
| 1041 |
SSL.Abort_Undefer.all; |
| 1042 |
end Set_False; |
| 1043 |
|
| 1044 |
-------------- |
| 1045 |
-- Set_True -- |
| 1046 |
-------------- |
| 1047 |
|
| 1048 |
procedure Set_True (S : in out Suspension_Object) is |
| 1049 |
Result : Interfaces.C.int; |
| 1050 |
|
| 1051 |
begin |
| 1052 |
SSL.Abort_Defer.all; |
| 1053 |
|
| 1054 |
Result := pthread_mutex_lock (S.L'Access); |
| 1055 |
pragma Assert (Result = 0); |
| 1056 |
|
| 1057 |
-- If there is already a task waiting on this suspension object then |
| 1058 |
-- we resume it, leaving the state of the suspension object to False, |
| 1059 |
-- as it is specified in (RM D.10(9)). Otherwise, it just leaves |
| 1060 |
-- the state to True. |
| 1061 |
|
| 1062 |
if S.Waiting then |
| 1063 |
S.Waiting := False; |
| 1064 |
S.State := False; |
| 1065 |
|
| 1066 |
Result := pthread_cond_signal (S.CV'Access); |
| 1067 |
pragma Assert (Result = 0); |
| 1068 |
|
| 1069 |
else |
| 1070 |
S.State := True; |
| 1071 |
end if; |
| 1072 |
|
| 1073 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1074 |
pragma Assert (Result = 0); |
| 1075 |
|
| 1076 |
SSL.Abort_Undefer.all; |
| 1077 |
end Set_True; |
| 1078 |
|
| 1079 |
------------------------ |
| 1080 |
-- Suspend_Until_True -- |
| 1081 |
------------------------ |
| 1082 |
|
| 1083 |
procedure Suspend_Until_True (S : in out Suspension_Object) is |
| 1084 |
Result : Interfaces.C.int; |
| 1085 |
|
| 1086 |
begin |
| 1087 |
SSL.Abort_Defer.all; |
| 1088 |
|
| 1089 |
Result := pthread_mutex_lock (S.L'Access); |
| 1090 |
pragma Assert (Result = 0); |
| 1091 |
|
| 1092 |
if S.Waiting then |
| 1093 |
|
| 1094 |
-- Program_Error must be raised upon calling Suspend_Until_True |
| 1095 |
-- if another task is already waiting on that suspension object |
| 1096 |
-- (RM D.10(10)). |
| 1097 |
|
| 1098 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1099 |
pragma Assert (Result = 0); |
| 1100 |
|
| 1101 |
SSL.Abort_Undefer.all; |
| 1102 |
|
| 1103 |
raise Program_Error; |
| 1104 |
|
| 1105 |
else |
| 1106 |
-- Suspend the task if the state is False. Otherwise, the task |
| 1107 |
-- continues its execution, and the state of the suspension object |
| 1108 |
-- is set to False (ARM D.10 par. 9). |
| 1109 |
|
| 1110 |
if S.State then |
| 1111 |
S.State := False; |
| 1112 |
else |
| 1113 |
S.Waiting := True; |
| 1114 |
|
| 1115 |
loop |
| 1116 |
-- Loop in case pthread_cond_wait returns earlier than expected |
| 1117 |
-- (e.g. in case of EINTR caused by a signal). |
| 1118 |
|
| 1119 |
Result := pthread_cond_wait (S.CV'Access, S.L'Access); |
| 1120 |
pragma Assert (Result = 0 or else Result = EINTR); |
| 1121 |
|
| 1122 |
exit when not S.Waiting; |
| 1123 |
end loop; |
| 1124 |
end if; |
| 1125 |
|
| 1126 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1127 |
pragma Assert (Result = 0); |
| 1128 |
|
| 1129 |
SSL.Abort_Undefer.all; |
| 1130 |
end if; |
| 1131 |
end Suspend_Until_True; |
| 1132 |
|
| 1133 |
---------------- |
| 1134 |
-- Check_Exit -- |
| 1135 |
---------------- |
| 1136 |
|
| 1137 |
-- Dummy version |
| 1138 |
|
| 1139 |
function Check_Exit (Self_ID : ST.Task_Id) return Boolean is |
| 1140 |
pragma Unreferenced (Self_ID); |
| 1141 |
begin |
| 1142 |
return True; |
| 1143 |
end Check_Exit; |
| 1144 |
|
| 1145 |
-------------------- |
| 1146 |
-- Check_No_Locks -- |
| 1147 |
-------------------- |
| 1148 |
|
| 1149 |
function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is |
| 1150 |
pragma Unreferenced (Self_ID); |
| 1151 |
begin |
| 1152 |
return True; |
| 1153 |
end Check_No_Locks; |
| 1154 |
|
| 1155 |
---------------------- |
| 1156 |
-- Environment_Task -- |
| 1157 |
---------------------- |
| 1158 |
|
| 1159 |
function Environment_Task return Task_Id is |
| 1160 |
begin |
| 1161 |
return Environment_Task_Id; |
| 1162 |
end Environment_Task; |
| 1163 |
|
| 1164 |
-------------- |
| 1165 |
-- Lock_RTS -- |
| 1166 |
-------------- |
| 1167 |
|
| 1168 |
procedure Lock_RTS is |
| 1169 |
begin |
| 1170 |
Write_Lock (Single_RTS_Lock'Access); |
| 1171 |
end Lock_RTS; |
| 1172 |
|
| 1173 |
---------------- |
| 1174 |
-- Unlock_RTS -- |
| 1175 |
---------------- |
| 1176 |
|
| 1177 |
procedure Unlock_RTS is |
| 1178 |
begin |
| 1179 |
Unlock (Single_RTS_Lock'Access); |
| 1180 |
end Unlock_RTS; |
| 1181 |
|
| 1182 |
------------------ |
| 1183 |
-- Suspend_Task -- |
| 1184 |
------------------ |
| 1185 |
|
| 1186 |
function Suspend_Task |
| 1187 |
(T : ST.Task_Id; |
| 1188 |
Thread_Self : Thread_Id) return Boolean |
| 1189 |
is |
| 1190 |
pragma Unreferenced (T, Thread_Self); |
| 1191 |
begin |
| 1192 |
return False; |
| 1193 |
end Suspend_Task; |
| 1194 |
|
| 1195 |
----------------- |
| 1196 |
-- Resume_Task -- |
| 1197 |
----------------- |
| 1198 |
|
| 1199 |
function Resume_Task |
| 1200 |
(T : ST.Task_Id; |
| 1201 |
Thread_Self : Thread_Id) return Boolean |
| 1202 |
is |
| 1203 |
pragma Unreferenced (T, Thread_Self); |
| 1204 |
begin |
| 1205 |
return False; |
| 1206 |
end Resume_Task; |
| 1207 |
|
| 1208 |
-------------------- |
| 1209 |
-- Stop_All_Tasks -- |
| 1210 |
-------------------- |
| 1211 |
|
| 1212 |
procedure Stop_All_Tasks is |
| 1213 |
begin |
| 1214 |
null; |
| 1215 |
end Stop_All_Tasks; |
| 1216 |
|
| 1217 |
--------------- |
| 1218 |
-- Stop_Task -- |
| 1219 |
--------------- |
| 1220 |
|
| 1221 |
function Stop_Task (T : ST.Task_Id) return Boolean is |
| 1222 |
pragma Unreferenced (T); |
| 1223 |
begin |
| 1224 |
return False; |
| 1225 |
end Stop_Task; |
| 1226 |
|
| 1227 |
------------------- |
| 1228 |
-- Continue_Task -- |
| 1229 |
------------------- |
| 1230 |
|
| 1231 |
function Continue_Task (T : ST.Task_Id) return Boolean is |
| 1232 |
pragma Unreferenced (T); |
| 1233 |
begin |
| 1234 |
return False; |
| 1235 |
end Continue_Task; |
| 1236 |
|
| 1237 |
---------------- |
| 1238 |
-- Initialize -- |
| 1239 |
---------------- |
| 1240 |
|
| 1241 |
procedure Initialize (Environment_Task : Task_Id) is |
| 1242 |
act : aliased struct_sigaction; |
| 1243 |
old_act : aliased struct_sigaction; |
| 1244 |
Tmp_Set : aliased sigset_t; |
| 1245 |
Result : Interfaces.C.int; |
| 1246 |
|
| 1247 |
function State |
| 1248 |
(Int : System.Interrupt_Management.Interrupt_ID) return Character; |
| 1249 |
pragma Import (C, State, "__gnat_get_interrupt_state"); |
| 1250 |
-- Get interrupt state. Defined in a-init.c |
| 1251 |
-- The input argument is the interrupt number, |
| 1252 |
-- and the result is one of the following: |
| 1253 |
|
| 1254 |
Default : constant Character := 's'; |
| 1255 |
-- 'n' this interrupt not set by any Interrupt_State pragma |
| 1256 |
-- 'u' Interrupt_State pragma set state to User |
| 1257 |
-- 'r' Interrupt_State pragma set state to Runtime |
| 1258 |
-- 's' Interrupt_State pragma set state to System (use "default" |
| 1259 |
-- system handler) |
| 1260 |
|
| 1261 |
begin |
| 1262 |
Environment_Task_Id := Environment_Task; |
| 1263 |
Environment_Task.Common.LL.Thread := pthread_self; |
| 1264 |
|
| 1265 |
Interrupt_Management.Initialize; |
| 1266 |
|
| 1267 |
-- Prepare the set of signals that should unblocked in all tasks |
| 1268 |
|
| 1269 |
Result := sigemptyset (Unblocked_Signal_Mask'Access); |
| 1270 |
pragma Assert (Result = 0); |
| 1271 |
|
| 1272 |
for J in Interrupt_Management.Interrupt_ID loop |
| 1273 |
if System.Interrupt_Management.Keep_Unmasked (J) then |
| 1274 |
Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J)); |
| 1275 |
pragma Assert (Result = 0); |
| 1276 |
end if; |
| 1277 |
end loop; |
| 1278 |
|
| 1279 |
-- Initialize the lock used to synchronize chain of all ATCBs |
| 1280 |
|
| 1281 |
Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level); |
| 1282 |
|
| 1283 |
Specific.Initialize (Environment_Task); |
| 1284 |
|
| 1285 |
if Use_Alternate_Stack then |
| 1286 |
Environment_Task.Common.Task_Alternate_Stack := |
| 1287 |
Alternate_Stack'Address; |
| 1288 |
end if; |
| 1289 |
|
| 1290 |
-- Make environment task known here because it doesn't go through |
| 1291 |
-- Activate_Tasks, which does it for all other tasks. |
| 1292 |
|
| 1293 |
Known_Tasks (Known_Tasks'First) := Environment_Task; |
| 1294 |
Environment_Task.Known_Tasks_Index := Known_Tasks'First; |
| 1295 |
|
| 1296 |
Enter_Task (Environment_Task); |
| 1297 |
|
| 1298 |
if State |
| 1299 |
(System.Interrupt_Management.Abort_Task_Interrupt) /= Default |
| 1300 |
then |
| 1301 |
act.sa_flags := 0; |
| 1302 |
act.sa_handler := Abort_Handler'Address; |
| 1303 |
|
| 1304 |
Result := sigemptyset (Tmp_Set'Access); |
| 1305 |
pragma Assert (Result = 0); |
| 1306 |
act.sa_mask := Tmp_Set; |
| 1307 |
|
| 1308 |
Result := |
| 1309 |
sigaction |
| 1310 |
(Signal (System.Interrupt_Management.Abort_Task_Interrupt), |
| 1311 |
act'Unchecked_Access, |
| 1312 |
old_act'Unchecked_Access); |
| 1313 |
pragma Assert (Result = 0); |
| 1314 |
Abort_Handler_Installed := True; |
| 1315 |
end if; |
| 1316 |
end Initialize; |
| 1317 |
|
| 1318 |
----------------------- |
| 1319 |
-- Set_Task_Affinity -- |
| 1320 |
----------------------- |
| 1321 |
|
| 1322 |
procedure Set_Task_Affinity (T : ST.Task_Id) is |
| 1323 |
use type Multiprocessors.CPU_Range; |
| 1324 |
|
| 1325 |
function Thread_Ctl_Ext |
| 1326 |
(Pid : pid_t; |
| 1327 |
Tid : Thread_Id; |
| 1328 |
Command : Interfaces.C.unsigned; |
| 1329 |
Runmask : Interfaces.C.size_t) return Interfaces.C.int |
| 1330 |
with |
| 1331 |
Import, Convention => C, External_Name => "ThreadCtlExt"; |
| 1332 |
-- Thread_Ctl_Ext is a generic thread control function in QNX. |
| 1333 |
-- It is defined locally because in the C API its second |
| 1334 |
-- argument is a void pointer that takes different actual |
| 1335 |
-- pointer types or values depending on the command. This |
| 1336 |
-- particular instance of this function only accepts the |
| 1337 |
-- NTO_TCTL_RUNMASK command. The void * pointer in the C |
| 1338 |
-- interface is interpreted as bitmask for this command. |
| 1339 |
-- In the binding size_t is used as an integer type that |
| 1340 |
-- always has the same size as a pointer. |
| 1341 |
|
| 1342 |
NTO_TCTL_RUNMASK : constant := 4; |
| 1343 |
-- Command for Thread_Ctl. Using this command in Thread_Ctl |
| 1344 |
-- allows the caller to pass a bitmask that describes on |
| 1345 |
-- which CPU the current thread is allowed to run on. |
| 1346 |
|
| 1347 |
Pid : constant pid_t := getpid; |
| 1348 |
Result : Interfaces.C.int; |
| 1349 |
Runmask : Interfaces.C.size_t; |
| 1350 |
-- Each set bit in runmask represents a processor that the thread |
| 1351 |
-- can run on. If all bits are set to one the thread can run on any CPU. |
| 1352 |
begin |
| 1353 |
if T.Common.Base_CPU = Multiprocessors.Not_A_Specific_CPU then |
| 1354 |
Runmask := Interfaces.C.size_t'Last; |
| 1355 |
else |
| 1356 |
Runmask := |
| 1357 |
Interfaces.C.size_t |
| 1358 |
(2 ** Natural (T.Common.Base_CPU - Multiprocessors.CPU'First)); |
| 1359 |
end if; |
| 1360 |
Result := |
| 1361 |
Thread_Ctl_Ext (Pid, Get_Thread_Id (T), NTO_TCTL_RUNMASK, Runmask); |
| 1362 |
pragma Assert (Result = 0); |
| 1363 |
end Set_Task_Affinity; |
| 1364 |
|
| 1365 |
end System.Task_Primitives.Operations; |