| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNU ADA 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 GNU/Linux (GNU/LinuxThreads) version of this package |
| 33 |
|
| 34 |
-- This package contains all the GNULL primitives that interface directly with |
| 35 |
-- the underlying OS. |
| 36 |
|
| 37 |
with Interfaces.C; |
| 38 |
|
| 39 |
with System.Interrupt_Management; |
| 40 |
with System.Multiprocessors; |
| 41 |
with System.OS_Constants; |
| 42 |
with System.OS_Primitives; |
| 43 |
with System.Task_Info; |
| 44 |
with System.Tasking.Debug; |
| 45 |
|
| 46 |
with System.Soft_Links; |
| 47 |
-- We use System.Soft_Links instead of System.Tasking.Initialization |
| 48 |
-- because the later is a higher level package that we shouldn't depend on. |
| 49 |
-- For example when using the restricted run time, it is replaced by |
| 50 |
-- System.Tasking.Restricted.Stages. |
| 51 |
|
| 52 |
package body System.Task_Primitives.Operations is |
| 53 |
|
| 54 |
package OSC renames System.OS_Constants; |
| 55 |
package SSL renames System.Soft_Links; |
| 56 |
|
| 57 |
use Interfaces; |
| 58 |
|
| 59 |
use System.Parameters; |
| 60 |
use System.OS_Interface; |
| 61 |
use System.OS_Locks; |
| 62 |
use System.OS_Primitives; |
| 63 |
use System.Task_Info; |
| 64 |
use System.Tasking.Debug; |
| 65 |
use System.Tasking; |
| 66 |
|
| 67 |
use type Interfaces.C.int; |
| 68 |
|
| 69 |
---------------- |
| 70 |
-- Local Data -- |
| 71 |
---------------- |
| 72 |
|
| 73 |
-- The followings are logically constants, but need to be initialized |
| 74 |
-- at run time. |
| 75 |
|
| 76 |
Single_RTS_Lock : aliased RTS_Lock; |
| 77 |
-- This is a lock to allow only one thread of control in the RTS at |
| 78 |
-- a time; it is used to execute in mutual exclusion from all other tasks. |
| 79 |
-- Used to protect All_Tasks_List |
| 80 |
|
| 81 |
Environment_Task_Id : Task_Id; |
| 82 |
-- A variable to hold Task_Id for the environment task |
| 83 |
|
| 84 |
Unblocked_Signal_Mask : aliased sigset_t; |
| 85 |
-- The set of signals that should be unblocked in all tasks |
| 86 |
|
| 87 |
-- The followings are internal configuration constants needed |
| 88 |
|
| 89 |
Next_Serial_Number : Task_Serial_Number := 100; |
| 90 |
-- We start at 100 (reserve some special values for using in error checks) |
| 91 |
|
| 92 |
Time_Slice_Val : constant Integer; |
| 93 |
pragma Import (C, Time_Slice_Val, "__gl_time_slice_val"); |
| 94 |
|
| 95 |
Dispatching_Policy : constant Character; |
| 96 |
pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy"); |
| 97 |
|
| 98 |
Locking_Policy : constant Character; |
| 99 |
pragma Import (C, Locking_Policy, "__gl_locking_policy"); |
| 100 |
|
| 101 |
Foreign_Task_Elaborated : aliased Boolean := True; |
| 102 |
-- Used to identified fake tasks (i.e., non-Ada Threads) |
| 103 |
|
| 104 |
Use_Alternate_Stack : Boolean := Alternate_Stack_Size /= 0; |
| 105 |
-- Whether to use an alternate signal stack for stack overflows |
| 106 |
|
| 107 |
Abort_Handler_Installed : Boolean := False; |
| 108 |
-- True if a handler for the abort signal is installed |
| 109 |
|
| 110 |
Null_Thread_Id : constant pthread_t := pthread_t'Last; |
| 111 |
-- Constant to indicate that the thread identifier has not yet been |
| 112 |
-- initialized. |
| 113 |
|
| 114 |
-------------------- |
| 115 |
-- Local Packages -- |
| 116 |
-------------------- |
| 117 |
|
| 118 |
package Specific is |
| 119 |
|
| 120 |
procedure Initialize (Environment_Task : Task_Id); |
| 121 |
pragma Inline (Initialize); |
| 122 |
-- Initialize various data needed by this package |
| 123 |
|
| 124 |
function Is_Valid_Task return Boolean; |
| 125 |
pragma Inline (Is_Valid_Task); |
| 126 |
-- Does executing thread have a TCB? |
| 127 |
|
| 128 |
procedure Set (Self_Id : Task_Id); |
| 129 |
pragma Inline (Set); |
| 130 |
-- Set the self id for the current task |
| 131 |
|
| 132 |
function Self return Task_Id; |
| 133 |
pragma Inline (Self); |
| 134 |
-- Return a pointer to the Ada Task Control Block of the calling task |
| 135 |
|
| 136 |
end Specific; |
| 137 |
|
| 138 |
package body Specific is separate; |
| 139 |
-- The body of this package is target specific |
| 140 |
|
| 141 |
package Monotonic is |
| 142 |
|
| 143 |
function Monotonic_Clock return Duration; |
| 144 |
pragma Inline (Monotonic_Clock); |
| 145 |
-- Returns an absolute time, represented as an offset relative to some |
| 146 |
-- unspecified starting point, typically system boot time. This clock is |
| 147 |
-- not affected by discontinuous jumps in the system time. |
| 148 |
|
| 149 |
function RT_Resolution return Duration; |
| 150 |
pragma Inline (RT_Resolution); |
| 151 |
-- Returns resolution of the underlying clock used to implement RT_Clock |
| 152 |
|
| 153 |
procedure Timed_Sleep |
| 154 |
(Self_ID : ST.Task_Id; |
| 155 |
Time : Duration; |
| 156 |
Mode : ST.Delay_Modes; |
| 157 |
Reason : System.Tasking.Task_States; |
| 158 |
Timedout : out Boolean; |
| 159 |
Yielded : out Boolean); |
| 160 |
-- Combination of Sleep (above) and Timed_Delay |
| 161 |
|
| 162 |
procedure Timed_Delay |
| 163 |
(Self_ID : ST.Task_Id; |
| 164 |
Time : Duration; |
| 165 |
Mode : ST.Delay_Modes); |
| 166 |
-- Implement the semantics of the delay statement. |
| 167 |
-- The caller should be abort-deferred and should not hold any locks. |
| 168 |
|
| 169 |
end Monotonic; |
| 170 |
|
| 171 |
package body Monotonic is separate; |
| 172 |
|
| 173 |
---------------------------------- |
| 174 |
-- ATCB allocation/deallocation -- |
| 175 |
---------------------------------- |
| 176 |
|
| 177 |
package body ATCB_Allocation is separate; |
| 178 |
-- The body of this package is shared across several targets |
| 179 |
|
| 180 |
--------------------------------- |
| 181 |
-- Support for foreign threads -- |
| 182 |
--------------------------------- |
| 183 |
|
| 184 |
function Register_Foreign_Thread |
| 185 |
(Thread : Thread_Id; |
| 186 |
Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id; |
| 187 |
-- Allocate and initialize a new ATCB for the current Thread. The size of |
| 188 |
-- the secondary stack can be optionally specified. |
| 189 |
|
| 190 |
function Register_Foreign_Thread |
| 191 |
(Thread : Thread_Id; |
| 192 |
Sec_Stack_Size : Size_Type := Unspecified_Size) |
| 193 |
return Task_Id is separate; |
| 194 |
|
| 195 |
----------------------- |
| 196 |
-- Local Subprograms -- |
| 197 |
----------------------- |
| 198 |
|
| 199 |
procedure Abort_Handler (signo : Signal); |
| 200 |
|
| 201 |
function GNAT_pthread_condattr_setup |
| 202 |
(attr : access pthread_condattr_t) return C.int; |
| 203 |
pragma Import |
| 204 |
(C, GNAT_pthread_condattr_setup, "__gnat_pthread_condattr_setup"); |
| 205 |
|
| 206 |
function GNAT_has_cap_sys_nice return C.int; |
| 207 |
pragma Import |
| 208 |
(C, GNAT_has_cap_sys_nice, "__gnat_has_cap_sys_nice"); |
| 209 |
-- We do not have pragma Linker_Options ("-lcap"); here, because this |
| 210 |
-- library is not present on many Linux systems. 'libcap' is the Linux |
| 211 |
-- "capabilities" library, called by __gnat_has_cap_sys_nice. |
| 212 |
|
| 213 |
function Prio_To_Linux_Prio (Prio : Any_Priority) return C.int is |
| 214 |
(C.int (Prio) + 1); |
| 215 |
-- Convert Ada priority to Linux priority. Priorities are 1 .. 99 on |
| 216 |
-- GNU/Linux, so we map 0 .. 98 to 1 .. 99. |
| 217 |
|
| 218 |
function Get_Ceiling_Support return Boolean; |
| 219 |
-- Get the value of the Ceiling_Support constant (see below). |
| 220 |
-- Note well: If this function or related code is modified, it should be |
| 221 |
-- tested by hand, because automated testing doesn't exercise it. |
| 222 |
|
| 223 |
------------------------- |
| 224 |
-- Get_Ceiling_Support -- |
| 225 |
------------------------- |
| 226 |
|
| 227 |
function Get_Ceiling_Support return Boolean is |
| 228 |
Ceiling_Support : Boolean := False; |
| 229 |
begin |
| 230 |
if Locking_Policy /= 'C' then |
| 231 |
return False; |
| 232 |
end if; |
| 233 |
|
| 234 |
declare |
| 235 |
function geteuid return Integer; |
| 236 |
pragma Import (C, geteuid, "geteuid"); |
| 237 |
Superuser : constant Boolean := geteuid = 0; |
| 238 |
Has_Cap : constant C.int := GNAT_has_cap_sys_nice; |
| 239 |
pragma Assert (Has_Cap in 0 | 1); |
| 240 |
begin |
| 241 |
Ceiling_Support := Superuser or else Has_Cap = 1; |
| 242 |
end; |
| 243 |
|
| 244 |
return Ceiling_Support; |
| 245 |
end Get_Ceiling_Support; |
| 246 |
|
| 247 |
pragma Warnings (Off, "non-preelaborable call not allowed*"); |
| 248 |
Ceiling_Support : constant Boolean := Get_Ceiling_Support; |
| 249 |
pragma Warnings (On, "non-preelaborable call not allowed*"); |
| 250 |
-- True if the locking policy is Ceiling_Locking, and the current process |
| 251 |
-- has permission to use this policy. The process has permission if it is |
| 252 |
-- running as 'root', or if the capability was set by the setcap command, |
| 253 |
-- as in "sudo /sbin/setcap cap_sys_nice=ep exe_file". If it doesn't have |
| 254 |
-- permission, then a request for Ceiling_Locking is ignored. |
| 255 |
|
| 256 |
function Initialize_Lock |
| 257 |
(L : not null access RTS_Lock; |
| 258 |
Prio : Any_Priority) return C.int; |
| 259 |
-- Initialize the lock L. If Ceiling_Support is True, then set the ceiling |
| 260 |
-- to Prio. Returns 0 for success, or ENOMEM for out-of-memory. |
| 261 |
|
| 262 |
function Requires_Affinity_Change |
| 263 |
(Domain : Dispatching_Domain_Access) return Boolean; |
| 264 |
-- Returns whether a call to pthread_setaffinity_np is required to assign a |
| 265 |
-- task to Domain. |
| 266 |
|
| 267 |
------------------- |
| 268 |
-- Abort_Handler -- |
| 269 |
------------------- |
| 270 |
|
| 271 |
procedure Abort_Handler (signo : Signal) is |
| 272 |
pragma Unreferenced (signo); |
| 273 |
|
| 274 |
Self_Id : constant Task_Id := Self; |
| 275 |
Result : C.int; |
| 276 |
Old_Set : aliased sigset_t; |
| 277 |
|
| 278 |
begin |
| 279 |
-- It's not safe to raise an exception when using GCC ZCX mechanism. |
| 280 |
-- Note that we still need to install a signal handler, since in some |
| 281 |
-- cases (e.g. shutdown of the Server_Task in System.Interrupts) we |
| 282 |
-- need to send the Abort signal to a task. |
| 283 |
|
| 284 |
if ZCX_By_Default then |
| 285 |
return; |
| 286 |
end if; |
| 287 |
|
| 288 |
if Self_Id.Deferral_Level = 0 |
| 289 |
and then Self_Id.Pending_ATC_Level < Self_Id.ATC_Nesting_Level |
| 290 |
and then not Self_Id.Aborting |
| 291 |
then |
| 292 |
Self_Id.Aborting := True; |
| 293 |
|
| 294 |
-- Make sure signals used for RTS internal purpose are unmasked |
| 295 |
|
| 296 |
Result := |
| 297 |
pthread_sigmask |
| 298 |
(SIG_UNBLOCK, |
| 299 |
Unblocked_Signal_Mask'Access, |
| 300 |
Old_Set'Access); |
| 301 |
pragma Assert (Result = 0); |
| 302 |
|
| 303 |
raise Standard'Abort_Signal; |
| 304 |
end if; |
| 305 |
end Abort_Handler; |
| 306 |
|
| 307 |
-------------- |
| 308 |
-- Lock_RTS -- |
| 309 |
-------------- |
| 310 |
|
| 311 |
procedure Lock_RTS is |
| 312 |
begin |
| 313 |
Write_Lock (Single_RTS_Lock'Access); |
| 314 |
end Lock_RTS; |
| 315 |
|
| 316 |
---------------- |
| 317 |
-- Unlock_RTS -- |
| 318 |
---------------- |
| 319 |
|
| 320 |
procedure Unlock_RTS is |
| 321 |
begin |
| 322 |
Unlock (Single_RTS_Lock'Access); |
| 323 |
end Unlock_RTS; |
| 324 |
|
| 325 |
----------------- |
| 326 |
-- Stack_Guard -- |
| 327 |
----------------- |
| 328 |
|
| 329 |
-- The underlying thread system extends the memory (up to 2MB) when needed |
| 330 |
|
| 331 |
procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is |
| 332 |
pragma Unreferenced (T); |
| 333 |
pragma Unreferenced (On); |
| 334 |
begin |
| 335 |
null; |
| 336 |
end Stack_Guard; |
| 337 |
|
| 338 |
-------------------- |
| 339 |
-- Get_Thread_Id -- |
| 340 |
-------------------- |
| 341 |
|
| 342 |
function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is |
| 343 |
begin |
| 344 |
return T.Common.LL.Thread; |
| 345 |
end Get_Thread_Id; |
| 346 |
|
| 347 |
---------- |
| 348 |
-- Self -- |
| 349 |
---------- |
| 350 |
|
| 351 |
function Self return Task_Id renames Specific.Self; |
| 352 |
|
| 353 |
--------------------- |
| 354 |
-- Initialize_Lock -- |
| 355 |
--------------------- |
| 356 |
|
| 357 |
-- Note: mutexes and cond_variables needed per-task basis are initialized |
| 358 |
-- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such |
| 359 |
-- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any |
| 360 |
-- status change of RTS. Therefore raising Storage_Error in the following |
| 361 |
-- routines should be able to be handled safely. |
| 362 |
|
| 363 |
function Initialize_Lock |
| 364 |
(L : not null access RTS_Lock; |
| 365 |
Prio : Any_Priority) return C.int |
| 366 |
is |
| 367 |
Mutex_Attr : aliased pthread_mutexattr_t; |
| 368 |
Result, Result_2 : C.int; |
| 369 |
|
| 370 |
begin |
| 371 |
Result := pthread_mutexattr_init (Mutex_Attr'Access); |
| 372 |
pragma Assert (Result in 0 | ENOMEM); |
| 373 |
|
| 374 |
if Result = ENOMEM then |
| 375 |
return Result; |
| 376 |
end if; |
| 377 |
|
| 378 |
if Ceiling_Support then |
| 379 |
Result := pthread_mutexattr_setprotocol |
| 380 |
(Mutex_Attr'Access, PTHREAD_PRIO_PROTECT); |
| 381 |
pragma Assert (Result = 0); |
| 382 |
|
| 383 |
Result := pthread_mutexattr_setprioceiling |
| 384 |
(Mutex_Attr'Access, Prio_To_Linux_Prio (Prio)); |
| 385 |
pragma Assert (Result = 0); |
| 386 |
|
| 387 |
elsif Locking_Policy = 'I' then |
| 388 |
Result := pthread_mutexattr_setprotocol |
| 389 |
(Mutex_Attr'Access, PTHREAD_PRIO_INHERIT); |
| 390 |
pragma Assert (Result = 0); |
| 391 |
end if; |
| 392 |
|
| 393 |
Result := pthread_mutex_init (L, Mutex_Attr'Access); |
| 394 |
pragma Assert (Result in 0 | ENOMEM); |
| 395 |
|
| 396 |
Result_2 := pthread_mutexattr_destroy (Mutex_Attr'Access); |
| 397 |
pragma Assert (Result_2 = 0); |
| 398 |
return Result; -- of pthread_mutex_init, not pthread_mutexattr_destroy |
| 399 |
end Initialize_Lock; |
| 400 |
|
| 401 |
procedure Initialize_Lock |
| 402 |
(Prio : Any_Priority; |
| 403 |
L : not null access Lock) |
| 404 |
is |
| 405 |
begin |
| 406 |
if Locking_Policy = 'R' then |
| 407 |
declare |
| 408 |
RWlock_Attr : aliased pthread_rwlockattr_t; |
| 409 |
Result : C.int; |
| 410 |
|
| 411 |
begin |
| 412 |
-- Set the rwlock to prefer writer to avoid writers starvation |
| 413 |
|
| 414 |
Result := pthread_rwlockattr_init (RWlock_Attr'Access); |
| 415 |
pragma Assert (Result = 0); |
| 416 |
|
| 417 |
Result := pthread_rwlockattr_setkind_np |
| 418 |
(RWlock_Attr'Access, |
| 419 |
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); |
| 420 |
pragma Assert (Result = 0); |
| 421 |
|
| 422 |
Result := pthread_rwlock_init (L.RW'Access, RWlock_Attr'Access); |
| 423 |
|
| 424 |
pragma Assert (Result in 0 | ENOMEM); |
| 425 |
|
| 426 |
if Result = ENOMEM then |
| 427 |
raise Storage_Error with "Failed to allocate a lock"; |
| 428 |
end if; |
| 429 |
end; |
| 430 |
|
| 431 |
else |
| 432 |
if Initialize_Lock (L.WO'Access, Prio) = ENOMEM then |
| 433 |
raise Storage_Error with "Failed to allocate a lock"; |
| 434 |
end if; |
| 435 |
end if; |
| 436 |
end Initialize_Lock; |
| 437 |
|
| 438 |
procedure Initialize_Lock |
| 439 |
(L : not null access RTS_Lock; |
| 440 |
Level : Lock_Level) |
| 441 |
is |
| 442 |
pragma Unreferenced (Level); |
| 443 |
begin |
| 444 |
if Initialize_Lock (L, Any_Priority'Last) = ENOMEM then |
| 445 |
raise Storage_Error with "Failed to allocate a lock"; |
| 446 |
end if; |
| 447 |
end Initialize_Lock; |
| 448 |
|
| 449 |
------------------- |
| 450 |
-- Finalize_Lock -- |
| 451 |
------------------- |
| 452 |
|
| 453 |
procedure Finalize_Lock (L : not null access Lock) is |
| 454 |
Result : C.int; |
| 455 |
begin |
| 456 |
if Locking_Policy = 'R' then |
| 457 |
Result := pthread_rwlock_destroy (L.RW'Access); |
| 458 |
else |
| 459 |
Result := pthread_mutex_destroy (L.WO'Access); |
| 460 |
end if; |
| 461 |
pragma Assert (Result = 0); |
| 462 |
end Finalize_Lock; |
| 463 |
|
| 464 |
procedure Finalize_Lock (L : not null access RTS_Lock) is |
| 465 |
Result : C.int; |
| 466 |
begin |
| 467 |
Result := pthread_mutex_destroy (L); |
| 468 |
pragma Assert (Result = 0); |
| 469 |
end Finalize_Lock; |
| 470 |
|
| 471 |
---------------- |
| 472 |
-- Write_Lock -- |
| 473 |
---------------- |
| 474 |
|
| 475 |
procedure Write_Lock |
| 476 |
(L : not null access Lock; |
| 477 |
Ceiling_Violation : out Boolean) |
| 478 |
is |
| 479 |
Result : C.int; |
| 480 |
begin |
| 481 |
if Locking_Policy = 'R' then |
| 482 |
Result := pthread_rwlock_wrlock (L.RW'Access); |
| 483 |
else |
| 484 |
Result := pthread_mutex_lock (L.WO'Access); |
| 485 |
end if; |
| 486 |
|
| 487 |
-- The cause of EINVAL is a priority ceiling violation |
| 488 |
|
| 489 |
pragma Assert (Result in 0 | EINVAL); |
| 490 |
Ceiling_Violation := Result = EINVAL; |
| 491 |
end Write_Lock; |
| 492 |
|
| 493 |
procedure Write_Lock (L : not null access RTS_Lock) is |
| 494 |
Result : C.int; |
| 495 |
begin |
| 496 |
Result := pthread_mutex_lock (L); |
| 497 |
pragma Assert (Result = 0); |
| 498 |
end Write_Lock; |
| 499 |
|
| 500 |
procedure Write_Lock (T : Task_Id) is |
| 501 |
Result : C.int; |
| 502 |
begin |
| 503 |
Result := pthread_mutex_lock (T.Common.LL.L'Access); |
| 504 |
pragma Assert (Result = 0); |
| 505 |
end Write_Lock; |
| 506 |
|
| 507 |
--------------- |
| 508 |
-- Read_Lock -- |
| 509 |
--------------- |
| 510 |
|
| 511 |
procedure Read_Lock |
| 512 |
(L : not null access Lock; |
| 513 |
Ceiling_Violation : out Boolean) |
| 514 |
is |
| 515 |
Result : C.int; |
| 516 |
begin |
| 517 |
if Locking_Policy = 'R' then |
| 518 |
Result := pthread_rwlock_rdlock (L.RW'Access); |
| 519 |
else |
| 520 |
Result := pthread_mutex_lock (L.WO'Access); |
| 521 |
end if; |
| 522 |
|
| 523 |
-- The cause of EINVAL is a priority ceiling violation |
| 524 |
|
| 525 |
pragma Assert (Result in 0 | EINVAL); |
| 526 |
Ceiling_Violation := Result = EINVAL; |
| 527 |
end Read_Lock; |
| 528 |
|
| 529 |
------------------------------ |
| 530 |
-- Requires_Affinity_Change -- |
| 531 |
------------------------------ |
| 532 |
|
| 533 |
function Requires_Affinity_Change |
| 534 |
(Domain : Dispatching_Domain_Access) return Boolean is |
| 535 |
begin |
| 536 |
return |
| 537 |
Domain /= System_Domain |
| 538 |
or else Domain.all |
| 539 |
/= [Multiprocessors.CPU'First |
| 540 |
.. Multiprocessors.Number_Of_CPUs => True]; |
| 541 |
end Requires_Affinity_Change; |
| 542 |
|
| 543 |
------------ |
| 544 |
-- Unlock -- |
| 545 |
------------ |
| 546 |
|
| 547 |
procedure Unlock (L : not null access Lock) is |
| 548 |
Result : C.int; |
| 549 |
begin |
| 550 |
if Locking_Policy = 'R' then |
| 551 |
Result := pthread_rwlock_unlock (L.RW'Access); |
| 552 |
else |
| 553 |
Result := pthread_mutex_unlock (L.WO'Access); |
| 554 |
end if; |
| 555 |
pragma Assert (Result = 0); |
| 556 |
end Unlock; |
| 557 |
|
| 558 |
procedure Unlock (L : not null access RTS_Lock) is |
| 559 |
Result : C.int; |
| 560 |
begin |
| 561 |
Result := pthread_mutex_unlock (L); |
| 562 |
pragma Assert (Result = 0); |
| 563 |
end Unlock; |
| 564 |
|
| 565 |
procedure Unlock (T : Task_Id) is |
| 566 |
Result : C.int; |
| 567 |
begin |
| 568 |
Result := pthread_mutex_unlock (T.Common.LL.L'Access); |
| 569 |
pragma Assert (Result = 0); |
| 570 |
end Unlock; |
| 571 |
|
| 572 |
----------------- |
| 573 |
-- Set_Ceiling -- |
| 574 |
----------------- |
| 575 |
|
| 576 |
-- Dynamic priority ceilings are not supported by the underlying system |
| 577 |
|
| 578 |
procedure Set_Ceiling |
| 579 |
(L : not null access Lock; |
| 580 |
Prio : Any_Priority) |
| 581 |
is |
| 582 |
pragma Unreferenced (L, Prio); |
| 583 |
begin |
| 584 |
null; |
| 585 |
end Set_Ceiling; |
| 586 |
|
| 587 |
----------- |
| 588 |
-- Sleep -- |
| 589 |
----------- |
| 590 |
|
| 591 |
procedure Sleep |
| 592 |
(Self_ID : Task_Id; |
| 593 |
Reason : System.Tasking.Task_States) |
| 594 |
is |
| 595 |
pragma Unreferenced (Reason); |
| 596 |
|
| 597 |
Result : C.int; |
| 598 |
|
| 599 |
begin |
| 600 |
pragma Assert (Self_ID = Self); |
| 601 |
|
| 602 |
Result := |
| 603 |
pthread_cond_wait |
| 604 |
(cond => Self_ID.Common.LL.CV'Access, |
| 605 |
mutex => Self_ID.Common.LL.L'Access); |
| 606 |
|
| 607 |
-- EINTR is not considered a failure |
| 608 |
|
| 609 |
pragma Assert (Result in 0 | EINTR); |
| 610 |
end Sleep; |
| 611 |
|
| 612 |
----------------- |
| 613 |
-- Timed_Sleep -- |
| 614 |
----------------- |
| 615 |
|
| 616 |
-- This is for use within the run-time system, so abort is |
| 617 |
-- assumed to be already deferred, and the caller should be |
| 618 |
-- holding its own ATCB lock. |
| 619 |
|
| 620 |
procedure Timed_Sleep |
| 621 |
(Self_ID : Task_Id; |
| 622 |
Time : Duration; |
| 623 |
Mode : ST.Delay_Modes; |
| 624 |
Reason : System.Tasking.Task_States; |
| 625 |
Timedout : out Boolean; |
| 626 |
Yielded : out Boolean) renames Monotonic.Timed_Sleep; |
| 627 |
|
| 628 |
----------------- |
| 629 |
-- Timed_Delay -- |
| 630 |
----------------- |
| 631 |
|
| 632 |
-- This is for use in implementing delay statements, so we assume the |
| 633 |
-- caller is abort-deferred but is holding no locks. |
| 634 |
|
| 635 |
procedure Timed_Delay |
| 636 |
(Self_ID : Task_Id; |
| 637 |
Time : Duration; |
| 638 |
Mode : ST.Delay_Modes) renames Monotonic.Timed_Delay; |
| 639 |
|
| 640 |
--------------------- |
| 641 |
-- Monotonic_Clock -- |
| 642 |
--------------------- |
| 643 |
|
| 644 |
function Monotonic_Clock return Duration renames Monotonic.Monotonic_Clock; |
| 645 |
|
| 646 |
------------------- |
| 647 |
-- RT_Resolution -- |
| 648 |
------------------- |
| 649 |
|
| 650 |
function RT_Resolution return Duration renames Monotonic.RT_Resolution; |
| 651 |
|
| 652 |
------------ |
| 653 |
-- Wakeup -- |
| 654 |
------------ |
| 655 |
|
| 656 |
procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is |
| 657 |
pragma Unreferenced (Reason); |
| 658 |
Result : C.int; |
| 659 |
begin |
| 660 |
Result := pthread_cond_signal (T.Common.LL.CV'Access); |
| 661 |
pragma Assert (Result = 0); |
| 662 |
end Wakeup; |
| 663 |
|
| 664 |
----------- |
| 665 |
-- Yield -- |
| 666 |
----------- |
| 667 |
|
| 668 |
procedure Yield (Do_Yield : Boolean := True) is |
| 669 |
Result : C.int; |
| 670 |
pragma Unreferenced (Result); |
| 671 |
begin |
| 672 |
if Do_Yield then |
| 673 |
Result := sched_yield; |
| 674 |
end if; |
| 675 |
end Yield; |
| 676 |
|
| 677 |
------------------ |
| 678 |
-- Set_Priority -- |
| 679 |
------------------ |
| 680 |
|
| 681 |
procedure Set_Priority |
| 682 |
(T : Task_Id; |
| 683 |
Prio : Any_Priority; |
| 684 |
Loss_Of_Inheritance : Boolean := False) |
| 685 |
is |
| 686 |
pragma Unreferenced (Loss_Of_Inheritance); |
| 687 |
|
| 688 |
Result : C.int; |
| 689 |
Param : aliased struct_sched_param; |
| 690 |
|
| 691 |
function Get_Policy (Prio : Any_Priority) return Character; |
| 692 |
pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching"); |
| 693 |
-- Get priority specific dispatching policy |
| 694 |
|
| 695 |
Priority_Specific_Policy : constant Character := Get_Policy (Prio); |
| 696 |
-- Upper case first character of the policy name corresponding to the |
| 697 |
-- task as set by a Priority_Specific_Dispatching pragma. |
| 698 |
|
| 699 |
begin |
| 700 |
T.Common.Current_Priority := Prio; |
| 701 |
|
| 702 |
Param.sched_priority := Prio_To_Linux_Prio (Prio); |
| 703 |
|
| 704 |
if Dispatching_Policy = 'R' |
| 705 |
or else Priority_Specific_Policy = 'R' |
| 706 |
or else Time_Slice_Val > 0 |
| 707 |
then |
| 708 |
Result := |
| 709 |
pthread_setschedparam |
| 710 |
(T.Common.LL.Thread, SCHED_RR, Param'Access); |
| 711 |
|
| 712 |
elsif Dispatching_Policy = 'F' |
| 713 |
or else Priority_Specific_Policy = 'F' |
| 714 |
or else Time_Slice_Val = 0 |
| 715 |
then |
| 716 |
Result := |
| 717 |
pthread_setschedparam |
| 718 |
(T.Common.LL.Thread, SCHED_FIFO, Param'Access); |
| 719 |
|
| 720 |
else |
| 721 |
Param.sched_priority := 0; |
| 722 |
Result := |
| 723 |
pthread_setschedparam |
| 724 |
(T.Common.LL.Thread, |
| 725 |
SCHED_OTHER, Param'Access); |
| 726 |
end if; |
| 727 |
|
| 728 |
pragma Assert (Result in 0 | EPERM | EINVAL); |
| 729 |
end Set_Priority; |
| 730 |
|
| 731 |
------------------ |
| 732 |
-- Get_Priority -- |
| 733 |
------------------ |
| 734 |
|
| 735 |
function Get_Priority (T : Task_Id) return Any_Priority is |
| 736 |
begin |
| 737 |
return T.Common.Current_Priority; |
| 738 |
end Get_Priority; |
| 739 |
|
| 740 |
---------------- |
| 741 |
-- Enter_Task -- |
| 742 |
---------------- |
| 743 |
|
| 744 |
procedure Enter_Task (Self_ID : Task_Id) is |
| 745 |
begin |
| 746 |
if Self_ID.Common.Task_Info /= null |
| 747 |
and then Self_ID.Common.Task_Info.CPU_Affinity = No_CPU |
| 748 |
then |
| 749 |
raise Invalid_CPU_Number; |
| 750 |
end if; |
| 751 |
|
| 752 |
Self_ID.Common.LL.LWP := lwp_self; |
| 753 |
|
| 754 |
-- Set thread name to ease debugging. If the name of the task is |
| 755 |
-- "foreign thread" (as set by Register_Foreign_Thread) retrieve |
| 756 |
-- the name of the thread and update the name of the task instead. |
| 757 |
|
| 758 |
if Self_ID.Common.Task_Image_Len = 14 |
| 759 |
and then Self_ID.Common.Task_Image (1 .. 14) = "foreign thread" |
| 760 |
then |
| 761 |
declare |
| 762 |
Thread_Name : String (1 .. 16); |
| 763 |
-- PR_GET_NAME returns a string of up to 16 bytes |
| 764 |
|
| 765 |
Len : Natural := 0; |
| 766 |
-- Length of the task name contained in Task_Name |
| 767 |
|
| 768 |
Result : C.int; |
| 769 |
-- Result from the prctl call |
| 770 |
begin |
| 771 |
Result := prctl (PR_GET_NAME, unsigned_long (Thread_Name'Address)); |
| 772 |
pragma Assert (Result = 0); |
| 773 |
|
| 774 |
-- Find the length of the given name |
| 775 |
|
| 776 |
for J in Thread_Name'Range loop |
| 777 |
if Thread_Name (J) /= ASCII.NUL then |
| 778 |
Len := Len + 1; |
| 779 |
else |
| 780 |
exit; |
| 781 |
end if; |
| 782 |
end loop; |
| 783 |
|
| 784 |
-- Cover the odd situation where someone decides to change |
| 785 |
-- Parameters.Max_Task_Image_Length to less than 16 characters. |
| 786 |
|
| 787 |
if Len > Parameters.Max_Task_Image_Length then |
| 788 |
Len := Parameters.Max_Task_Image_Length; |
| 789 |
end if; |
| 790 |
|
| 791 |
-- Copy the name of the thread to the task's ATCB |
| 792 |
|
| 793 |
Self_ID.Common.Task_Image (1 .. Len) := Thread_Name (1 .. Len); |
| 794 |
Self_ID.Common.Task_Image_Len := Len; |
| 795 |
end; |
| 796 |
|
| 797 |
elsif Self_ID.Common.Task_Image_Len > 0 then |
| 798 |
declare |
| 799 |
Task_Name : String (1 .. Parameters.Max_Task_Image_Length + 1); |
| 800 |
Result : C.int; |
| 801 |
|
| 802 |
begin |
| 803 |
Task_Name (1 .. Self_ID.Common.Task_Image_Len) := |
| 804 |
Self_ID.Common.Task_Image (1 .. Self_ID.Common.Task_Image_Len); |
| 805 |
Task_Name (Self_ID.Common.Task_Image_Len + 1) := ASCII.NUL; |
| 806 |
|
| 807 |
Result := prctl (PR_SET_NAME, unsigned_long (Task_Name'Address)); |
| 808 |
pragma Assert (Result = 0); |
| 809 |
end; |
| 810 |
end if; |
| 811 |
|
| 812 |
Specific.Set (Self_ID); |
| 813 |
|
| 814 |
if Use_Alternate_Stack |
| 815 |
and then Self_ID.Common.Task_Alternate_Stack /= Null_Address |
| 816 |
then |
| 817 |
declare |
| 818 |
Stack : aliased stack_t; |
| 819 |
Result : C.int; |
| 820 |
begin |
| 821 |
Stack.ss_sp := Self_ID.Common.Task_Alternate_Stack; |
| 822 |
Stack.ss_size := Alternate_Stack_Size; |
| 823 |
Stack.ss_flags := 0; |
| 824 |
Result := sigaltstack (Stack'Access, null); |
| 825 |
pragma Assert (Result = 0); |
| 826 |
end; |
| 827 |
end if; |
| 828 |
end Enter_Task; |
| 829 |
|
| 830 |
------------------- |
| 831 |
-- Is_Valid_Task -- |
| 832 |
------------------- |
| 833 |
|
| 834 |
function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task; |
| 835 |
|
| 836 |
----------------------------- |
| 837 |
-- Register_Foreign_Thread -- |
| 838 |
----------------------------- |
| 839 |
|
| 840 |
function Register_Foreign_Thread return Task_Id is |
| 841 |
begin |
| 842 |
if Is_Valid_Task then |
| 843 |
return Self; |
| 844 |
else |
| 845 |
return Register_Foreign_Thread (pthread_self); |
| 846 |
end if; |
| 847 |
end Register_Foreign_Thread; |
| 848 |
|
| 849 |
-------------------- |
| 850 |
-- Initialize_TCB -- |
| 851 |
-------------------- |
| 852 |
|
| 853 |
procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is |
| 854 |
Result : C.int; |
| 855 |
Cond_Attr : aliased pthread_condattr_t; |
| 856 |
|
| 857 |
begin |
| 858 |
-- Give the task a unique serial number |
| 859 |
|
| 860 |
Self_ID.Serial_Number := Next_Serial_Number; |
| 861 |
Next_Serial_Number := Next_Serial_Number + 1; |
| 862 |
pragma Assert (Next_Serial_Number /= 0); |
| 863 |
|
| 864 |
Self_ID.Common.LL.Thread := Null_Thread_Id; |
| 865 |
|
| 866 |
if Initialize_Lock (Self_ID.Common.LL.L'Access, Any_Priority'Last) /= 0 |
| 867 |
then |
| 868 |
Succeeded := False; |
| 869 |
return; |
| 870 |
end if; |
| 871 |
|
| 872 |
Result := pthread_condattr_init (Cond_Attr'Access); |
| 873 |
pragma Assert (Result in 0 | ENOMEM); |
| 874 |
|
| 875 |
if Result = 0 then |
| 876 |
Result := GNAT_pthread_condattr_setup (Cond_Attr'Access); |
| 877 |
pragma Assert (Result = 0); |
| 878 |
|
| 879 |
Result := |
| 880 |
pthread_cond_init |
| 881 |
(Self_ID.Common.LL.CV'Access, Cond_Attr'Access); |
| 882 |
pragma Assert (Result in 0 | ENOMEM); |
| 883 |
end if; |
| 884 |
|
| 885 |
if Result = 0 then |
| 886 |
Succeeded := True; |
| 887 |
else |
| 888 |
Result := pthread_mutex_destroy (Self_ID.Common.LL.L'Access); |
| 889 |
pragma Assert (Result = 0); |
| 890 |
|
| 891 |
Succeeded := False; |
| 892 |
end if; |
| 893 |
|
| 894 |
Result := pthread_condattr_destroy (Cond_Attr'Access); |
| 895 |
pragma Assert (Result = 0); |
| 896 |
end Initialize_TCB; |
| 897 |
|
| 898 |
----------------- |
| 899 |
-- Create_Task -- |
| 900 |
----------------- |
| 901 |
|
| 902 |
procedure Create_Task |
| 903 |
(T : Task_Id; |
| 904 |
Wrapper : System.Address; |
| 905 |
Stack_Size : System.Parameters.Size_Type; |
| 906 |
Priority : Any_Priority; |
| 907 |
Succeeded : out Boolean) |
| 908 |
is |
| 909 |
Thread_Attr : aliased pthread_attr_t; |
| 910 |
Adjusted_Stack_Size : C.size_t; |
| 911 |
Result : C.int; |
| 912 |
|
| 913 |
use type Multiprocessors.CPU_Range, Interfaces.C.size_t; |
| 914 |
|
| 915 |
begin |
| 916 |
-- Check whether both Dispatching_Domain and CPU are specified for |
| 917 |
-- the task, and the CPU value is not contained within the range of |
| 918 |
-- processors for the domain. |
| 919 |
|
| 920 |
if T.Common.Domain /= null |
| 921 |
and then T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU |
| 922 |
and then |
| 923 |
(T.Common.Base_CPU not in T.Common.Domain'Range |
| 924 |
or else not T.Common.Domain (T.Common.Base_CPU)) |
| 925 |
then |
| 926 |
Succeeded := False; |
| 927 |
return; |
| 928 |
end if; |
| 929 |
|
| 930 |
Adjusted_Stack_Size := C.size_t (Stack_Size + Alternate_Stack_Size); |
| 931 |
|
| 932 |
Result := pthread_attr_init (Thread_Attr'Access); |
| 933 |
pragma Assert (Result in 0 | ENOMEM); |
| 934 |
|
| 935 |
if Result /= 0 then |
| 936 |
Succeeded := False; |
| 937 |
return; |
| 938 |
end if; |
| 939 |
|
| 940 |
Result := |
| 941 |
pthread_attr_setstacksize (Thread_Attr'Access, Adjusted_Stack_Size); |
| 942 |
pragma Assert (Result = 0); |
| 943 |
|
| 944 |
Result := |
| 945 |
pthread_attr_setdetachstate |
| 946 |
(Thread_Attr'Access, PTHREAD_CREATE_DETACHED); |
| 947 |
pragma Assert (Result = 0); |
| 948 |
|
| 949 |
-- Set the required attributes for the creation of the thread |
| 950 |
|
| 951 |
-- Note: Previously, we called pthread_setaffinity_np (after thread |
| 952 |
-- creation but before thread activation) to set the affinity but it was |
| 953 |
-- not behaving as expected. Setting the required attributes for the |
| 954 |
-- creation of the thread works correctly and it is more appropriate. |
| 955 |
|
| 956 |
-- Do nothing if required support not provided by the operating system |
| 957 |
|
| 958 |
if pthread_attr_setaffinity_np'Address = Null_Address then |
| 959 |
null; |
| 960 |
|
| 961 |
-- Support is available |
| 962 |
|
| 963 |
elsif T.Common.CPU_Is_Explicit |
| 964 |
and then T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU |
| 965 |
then |
| 966 |
declare |
| 967 |
CPUs : constant size_t := |
| 968 |
C.size_t (Multiprocessors.Number_Of_CPUs); |
| 969 |
CPU_Set : constant cpu_set_t_ptr := CPU_ALLOC (CPUs); |
| 970 |
Size : constant size_t := CPU_ALLOC_SIZE (CPUs); |
| 971 |
|
| 972 |
begin |
| 973 |
CPU_ZERO (Size, CPU_Set); |
| 974 |
System.OS_Interface.CPU_SET |
| 975 |
(int (T.Common.Base_CPU), Size, CPU_Set); |
| 976 |
Result := |
| 977 |
pthread_attr_setaffinity_np (Thread_Attr'Access, Size, CPU_Set); |
| 978 |
pragma Assert (Result = 0); |
| 979 |
|
| 980 |
CPU_FREE (CPU_Set); |
| 981 |
end; |
| 982 |
|
| 983 |
-- Handle Task_Info |
| 984 |
|
| 985 |
elsif T.Common.Task_Info /= null then |
| 986 |
Result := |
| 987 |
pthread_attr_setaffinity_np |
| 988 |
(Thread_Attr'Access, |
| 989 |
CPU_SETSIZE / 8, |
| 990 |
T.Common.Task_Info.CPU_Affinity'Access); |
| 991 |
pragma Assert (Result = 0); |
| 992 |
|
| 993 |
-- Handle dispatching domains |
| 994 |
|
| 995 |
elsif Requires_Affinity_Change (T.Common.Domain) then |
| 996 |
declare |
| 997 |
CPUs : constant size_t := |
| 998 |
C.size_t (Multiprocessors.Number_Of_CPUs); |
| 999 |
CPU_Set : constant cpu_set_t_ptr := CPU_ALLOC (CPUs); |
| 1000 |
Size : constant size_t := CPU_ALLOC_SIZE (CPUs); |
| 1001 |
|
| 1002 |
begin |
| 1003 |
CPU_ZERO (Size, CPU_Set); |
| 1004 |
|
| 1005 |
-- Set the affinity to all the processors belonging to the |
| 1006 |
-- dispatching domain. |
| 1007 |
|
| 1008 |
for Proc in T.Common.Domain'Range loop |
| 1009 |
if T.Common.Domain (Proc) then |
| 1010 |
System.OS_Interface.CPU_SET (int (Proc), Size, CPU_Set); |
| 1011 |
end if; |
| 1012 |
end loop; |
| 1013 |
|
| 1014 |
Result := |
| 1015 |
pthread_attr_setaffinity_np (Thread_Attr'Access, Size, CPU_Set); |
| 1016 |
pragma Assert (Result = 0); |
| 1017 |
|
| 1018 |
CPU_FREE (CPU_Set); |
| 1019 |
end; |
| 1020 |
end if; |
| 1021 |
|
| 1022 |
-- Since the initial signal mask of a thread is inherited from the |
| 1023 |
-- creator, and the Environment task has all its signals masked, we |
| 1024 |
-- do not need to manipulate caller's signal mask at this point. |
| 1025 |
-- All tasks in RTS will have All_Tasks_Mask initially. |
| 1026 |
|
| 1027 |
-- The write to T.Common.LL.Thread is not racy with regard to the |
| 1028 |
-- created thread because the created thread will not access it until |
| 1029 |
-- we release the RTS lock (or the current task's lock when |
| 1030 |
-- Restricted.Stages is used). One can verify that by inspecting the |
| 1031 |
-- Task_Wrapper procedures. |
| 1032 |
|
| 1033 |
Result := pthread_create |
| 1034 |
(T.Common.LL.Thread'Access, |
| 1035 |
Thread_Attr'Access, |
| 1036 |
Thread_Body_Access (Wrapper), |
| 1037 |
To_Address (T)); |
| 1038 |
|
| 1039 |
pragma Assert (Result in 0 | EAGAIN | ENOMEM); |
| 1040 |
|
| 1041 |
if Result /= 0 then |
| 1042 |
Succeeded := False; |
| 1043 |
Result := pthread_attr_destroy (Thread_Attr'Access); |
| 1044 |
pragma Assert (Result = 0); |
| 1045 |
return; |
| 1046 |
end if; |
| 1047 |
|
| 1048 |
Succeeded := True; |
| 1049 |
|
| 1050 |
Result := pthread_attr_destroy (Thread_Attr'Access); |
| 1051 |
pragma Assert (Result = 0); |
| 1052 |
|
| 1053 |
Set_Priority (T, Priority); |
| 1054 |
end Create_Task; |
| 1055 |
|
| 1056 |
------------------ |
| 1057 |
-- Finalize_TCB -- |
| 1058 |
------------------ |
| 1059 |
|
| 1060 |
procedure Finalize_TCB (T : Task_Id) is |
| 1061 |
Result : C.int; |
| 1062 |
|
| 1063 |
begin |
| 1064 |
Result := pthread_mutex_destroy (T.Common.LL.L'Access); |
| 1065 |
pragma Assert (Result = 0); |
| 1066 |
|
| 1067 |
Result := pthread_cond_destroy (T.Common.LL.CV'Access); |
| 1068 |
pragma Assert (Result = 0); |
| 1069 |
|
| 1070 |
if T.Known_Tasks_Index /= -1 then |
| 1071 |
Known_Tasks (T.Known_Tasks_Index) := null; |
| 1072 |
end if; |
| 1073 |
|
| 1074 |
ATCB_Allocation.Free_ATCB (T); |
| 1075 |
end Finalize_TCB; |
| 1076 |
|
| 1077 |
--------------- |
| 1078 |
-- Exit_Task -- |
| 1079 |
--------------- |
| 1080 |
|
| 1081 |
procedure Exit_Task is |
| 1082 |
begin |
| 1083 |
Specific.Set (null); |
| 1084 |
end Exit_Task; |
| 1085 |
|
| 1086 |
---------------- |
| 1087 |
-- Abort_Task -- |
| 1088 |
---------------- |
| 1089 |
|
| 1090 |
procedure Abort_Task (T : Task_Id) is |
| 1091 |
Result : C.int; |
| 1092 |
|
| 1093 |
ESRCH : constant := 3; -- No such process |
| 1094 |
-- It can happen that T has already vanished, in which case pthread_kill |
| 1095 |
-- returns ESRCH, so we don't consider that to be an error. |
| 1096 |
|
| 1097 |
begin |
| 1098 |
if Abort_Handler_Installed then |
| 1099 |
Result := |
| 1100 |
pthread_kill |
| 1101 |
(T.Common.LL.Thread, |
| 1102 |
Signal (System.Interrupt_Management.Abort_Task_Interrupt)); |
| 1103 |
pragma Assert (Result in 0 | ESRCH); |
| 1104 |
end if; |
| 1105 |
end Abort_Task; |
| 1106 |
|
| 1107 |
---------------- |
| 1108 |
-- Initialize -- |
| 1109 |
---------------- |
| 1110 |
|
| 1111 |
procedure Initialize (S : in out Suspension_Object) is |
| 1112 |
Result : C.int; |
| 1113 |
|
| 1114 |
begin |
| 1115 |
-- Initialize internal state (always to False (RM D.10(6))) |
| 1116 |
|
| 1117 |
S.State := False; |
| 1118 |
S.Waiting := False; |
| 1119 |
|
| 1120 |
-- Initialize internal mutex |
| 1121 |
|
| 1122 |
Result := pthread_mutex_init (S.L'Access, null); |
| 1123 |
|
| 1124 |
pragma Assert (Result in 0 | ENOMEM); |
| 1125 |
|
| 1126 |
if Result = ENOMEM then |
| 1127 |
raise Storage_Error; |
| 1128 |
end if; |
| 1129 |
|
| 1130 |
-- Initialize internal condition variable |
| 1131 |
|
| 1132 |
Result := pthread_cond_init (S.CV'Access, null); |
| 1133 |
|
| 1134 |
pragma Assert (Result in 0 | ENOMEM); |
| 1135 |
|
| 1136 |
if Result /= 0 then |
| 1137 |
Result := pthread_mutex_destroy (S.L'Access); |
| 1138 |
pragma Assert (Result = 0); |
| 1139 |
|
| 1140 |
if Result = ENOMEM then |
| 1141 |
raise Storage_Error; |
| 1142 |
end if; |
| 1143 |
end if; |
| 1144 |
end Initialize; |
| 1145 |
|
| 1146 |
-------------- |
| 1147 |
-- Finalize -- |
| 1148 |
-------------- |
| 1149 |
|
| 1150 |
procedure Finalize (S : in out Suspension_Object) is |
| 1151 |
Result : C.int; |
| 1152 |
|
| 1153 |
begin |
| 1154 |
-- Destroy internal mutex |
| 1155 |
|
| 1156 |
Result := pthread_mutex_destroy (S.L'Access); |
| 1157 |
pragma Assert (Result = 0); |
| 1158 |
|
| 1159 |
-- Destroy internal condition variable |
| 1160 |
|
| 1161 |
Result := pthread_cond_destroy (S.CV'Access); |
| 1162 |
pragma Assert (Result = 0); |
| 1163 |
end Finalize; |
| 1164 |
|
| 1165 |
------------------- |
| 1166 |
-- Current_State -- |
| 1167 |
------------------- |
| 1168 |
|
| 1169 |
function Current_State (S : Suspension_Object) return Boolean is |
| 1170 |
begin |
| 1171 |
-- We do not want to use lock on this read operation. State is marked |
| 1172 |
-- as Atomic so that we ensure that the value retrieved is correct. |
| 1173 |
|
| 1174 |
return S.State; |
| 1175 |
end Current_State; |
| 1176 |
|
| 1177 |
--------------- |
| 1178 |
-- Set_False -- |
| 1179 |
--------------- |
| 1180 |
|
| 1181 |
procedure Set_False (S : in out Suspension_Object) is |
| 1182 |
Result : C.int; |
| 1183 |
|
| 1184 |
begin |
| 1185 |
SSL.Abort_Defer.all; |
| 1186 |
|
| 1187 |
Result := pthread_mutex_lock (S.L'Access); |
| 1188 |
pragma Assert (Result = 0); |
| 1189 |
|
| 1190 |
S.State := False; |
| 1191 |
|
| 1192 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1193 |
pragma Assert (Result = 0); |
| 1194 |
|
| 1195 |
SSL.Abort_Undefer.all; |
| 1196 |
end Set_False; |
| 1197 |
|
| 1198 |
-------------- |
| 1199 |
-- Set_True -- |
| 1200 |
-------------- |
| 1201 |
|
| 1202 |
procedure Set_True (S : in out Suspension_Object) is |
| 1203 |
Result : C.int; |
| 1204 |
|
| 1205 |
begin |
| 1206 |
SSL.Abort_Defer.all; |
| 1207 |
|
| 1208 |
Result := pthread_mutex_lock (S.L'Access); |
| 1209 |
pragma Assert (Result = 0); |
| 1210 |
|
| 1211 |
-- If there is already a task waiting on this suspension object then |
| 1212 |
-- we resume it, leaving the state of the suspension object to False, |
| 1213 |
-- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves |
| 1214 |
-- the state to True. |
| 1215 |
|
| 1216 |
if S.Waiting then |
| 1217 |
S.Waiting := False; |
| 1218 |
S.State := False; |
| 1219 |
|
| 1220 |
Result := pthread_cond_signal (S.CV'Access); |
| 1221 |
pragma Assert (Result = 0); |
| 1222 |
|
| 1223 |
else |
| 1224 |
S.State := True; |
| 1225 |
end if; |
| 1226 |
|
| 1227 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1228 |
pragma Assert (Result = 0); |
| 1229 |
|
| 1230 |
SSL.Abort_Undefer.all; |
| 1231 |
end Set_True; |
| 1232 |
|
| 1233 |
------------------------ |
| 1234 |
-- Suspend_Until_True -- |
| 1235 |
------------------------ |
| 1236 |
|
| 1237 |
procedure Suspend_Until_True (S : in out Suspension_Object) is |
| 1238 |
Result : C.int; |
| 1239 |
|
| 1240 |
begin |
| 1241 |
SSL.Abort_Defer.all; |
| 1242 |
|
| 1243 |
Result := pthread_mutex_lock (S.L'Access); |
| 1244 |
pragma Assert (Result = 0); |
| 1245 |
|
| 1246 |
if S.Waiting then |
| 1247 |
|
| 1248 |
-- Program_Error must be raised upon calling Suspend_Until_True |
| 1249 |
-- if another task is already waiting on that suspension object |
| 1250 |
-- (RM D.10(10)). |
| 1251 |
|
| 1252 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1253 |
pragma Assert (Result = 0); |
| 1254 |
|
| 1255 |
SSL.Abort_Undefer.all; |
| 1256 |
|
| 1257 |
raise Program_Error; |
| 1258 |
|
| 1259 |
else |
| 1260 |
-- Suspend the task if the state is False. Otherwise, the task |
| 1261 |
-- continues its execution, and the state of the suspension object |
| 1262 |
-- is set to False (ARM D.10 par. 9). |
| 1263 |
|
| 1264 |
if S.State then |
| 1265 |
S.State := False; |
| 1266 |
else |
| 1267 |
S.Waiting := True; |
| 1268 |
|
| 1269 |
loop |
| 1270 |
-- Loop in case pthread_cond_wait returns earlier than expected |
| 1271 |
-- (e.g. in case of EINTR caused by a signal). This should not |
| 1272 |
-- happen with the current Linux implementation of pthread, but |
| 1273 |
-- POSIX does not guarantee it so this may change in future. |
| 1274 |
|
| 1275 |
Result := pthread_cond_wait (S.CV'Access, S.L'Access); |
| 1276 |
pragma Assert (Result in 0 | EINTR); |
| 1277 |
|
| 1278 |
exit when not S.Waiting; |
| 1279 |
end loop; |
| 1280 |
end if; |
| 1281 |
|
| 1282 |
Result := pthread_mutex_unlock (S.L'Access); |
| 1283 |
pragma Assert (Result = 0); |
| 1284 |
|
| 1285 |
SSL.Abort_Undefer.all; |
| 1286 |
end if; |
| 1287 |
end Suspend_Until_True; |
| 1288 |
|
| 1289 |
---------------- |
| 1290 |
-- Check_Exit -- |
| 1291 |
---------------- |
| 1292 |
|
| 1293 |
-- Dummy version |
| 1294 |
|
| 1295 |
function Check_Exit (Self_ID : ST.Task_Id) return Boolean is |
| 1296 |
pragma Unreferenced (Self_ID); |
| 1297 |
begin |
| 1298 |
return True; |
| 1299 |
end Check_Exit; |
| 1300 |
|
| 1301 |
-------------------- |
| 1302 |
-- Check_No_Locks -- |
| 1303 |
-------------------- |
| 1304 |
|
| 1305 |
function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is |
| 1306 |
pragma Unreferenced (Self_ID); |
| 1307 |
begin |
| 1308 |
return True; |
| 1309 |
end Check_No_Locks; |
| 1310 |
|
| 1311 |
---------------------- |
| 1312 |
-- Environment_Task -- |
| 1313 |
---------------------- |
| 1314 |
|
| 1315 |
function Environment_Task return Task_Id is |
| 1316 |
begin |
| 1317 |
return Environment_Task_Id; |
| 1318 |
end Environment_Task; |
| 1319 |
|
| 1320 |
------------------ |
| 1321 |
-- Suspend_Task -- |
| 1322 |
------------------ |
| 1323 |
|
| 1324 |
function Suspend_Task |
| 1325 |
(T : ST.Task_Id; |
| 1326 |
Thread_Self : Thread_Id) return Boolean |
| 1327 |
is |
| 1328 |
begin |
| 1329 |
if T.Common.LL.Thread /= Thread_Self then |
| 1330 |
return pthread_kill (T.Common.LL.Thread, SIGSTOP) = 0; |
| 1331 |
else |
| 1332 |
return True; |
| 1333 |
end if; |
| 1334 |
end Suspend_Task; |
| 1335 |
|
| 1336 |
----------------- |
| 1337 |
-- Resume_Task -- |
| 1338 |
----------------- |
| 1339 |
|
| 1340 |
function Resume_Task |
| 1341 |
(T : ST.Task_Id; |
| 1342 |
Thread_Self : Thread_Id) return Boolean |
| 1343 |
is |
| 1344 |
begin |
| 1345 |
if T.Common.LL.Thread /= Thread_Self then |
| 1346 |
return pthread_kill (T.Common.LL.Thread, SIGCONT) = 0; |
| 1347 |
else |
| 1348 |
return True; |
| 1349 |
end if; |
| 1350 |
end Resume_Task; |
| 1351 |
|
| 1352 |
-------------------- |
| 1353 |
-- Stop_All_Tasks -- |
| 1354 |
-------------------- |
| 1355 |
|
| 1356 |
procedure Stop_All_Tasks is |
| 1357 |
begin |
| 1358 |
null; |
| 1359 |
end Stop_All_Tasks; |
| 1360 |
|
| 1361 |
--------------- |
| 1362 |
-- Stop_Task -- |
| 1363 |
--------------- |
| 1364 |
|
| 1365 |
function Stop_Task (T : ST.Task_Id) return Boolean is |
| 1366 |
pragma Unreferenced (T); |
| 1367 |
begin |
| 1368 |
return False; |
| 1369 |
end Stop_Task; |
| 1370 |
|
| 1371 |
------------------- |
| 1372 |
-- Continue_Task -- |
| 1373 |
------------------- |
| 1374 |
|
| 1375 |
function Continue_Task (T : ST.Task_Id) return Boolean is |
| 1376 |
pragma Unreferenced (T); |
| 1377 |
begin |
| 1378 |
return False; |
| 1379 |
end Continue_Task; |
| 1380 |
|
| 1381 |
---------------- |
| 1382 |
-- Initialize -- |
| 1383 |
---------------- |
| 1384 |
|
| 1385 |
procedure Initialize (Environment_Task : Task_Id) is |
| 1386 |
act : aliased struct_sigaction; |
| 1387 |
old_act : aliased struct_sigaction; |
| 1388 |
Tmp_Set : aliased sigset_t; |
| 1389 |
Result : C.int; |
| 1390 |
-- Whether to use an alternate signal stack for stack overflows |
| 1391 |
|
| 1392 |
function State |
| 1393 |
(Int : System.Interrupt_Management.Interrupt_ID) return Character; |
| 1394 |
pragma Import (C, State, "__gnat_get_interrupt_state"); |
| 1395 |
-- Get interrupt state. Defined in init.c. |
| 1396 |
-- The input argument is the interrupt number, and the result is one of |
| 1397 |
-- the following: |
| 1398 |
|
| 1399 |
Default : constant Character := 's'; |
| 1400 |
-- 'n' this interrupt not set by any Interrupt_State pragma |
| 1401 |
-- 'u' Interrupt_State pragma set state to User |
| 1402 |
-- 'r' Interrupt_State pragma set state to Runtime |
| 1403 |
-- 's' Interrupt_State pragma set state to System (use "default" |
| 1404 |
-- system handler) |
| 1405 |
|
| 1406 |
begin |
| 1407 |
Environment_Task_Id := Environment_Task; |
| 1408 |
Environment_Task.Common.LL.Thread := pthread_self; |
| 1409 |
|
| 1410 |
Interrupt_Management.Initialize; |
| 1411 |
|
| 1412 |
-- Prepare the set of signals that should be unblocked in all tasks |
| 1413 |
|
| 1414 |
Result := sigemptyset (Unblocked_Signal_Mask'Access); |
| 1415 |
pragma Assert (Result = 0); |
| 1416 |
|
| 1417 |
for J in Interrupt_Management.Interrupt_ID loop |
| 1418 |
if System.Interrupt_Management.Keep_Unmasked (J) then |
| 1419 |
Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J)); |
| 1420 |
pragma Assert (Result = 0); |
| 1421 |
end if; |
| 1422 |
end loop; |
| 1423 |
|
| 1424 |
Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level); |
| 1425 |
|
| 1426 |
-- Initialize the global RTS lock |
| 1427 |
|
| 1428 |
Specific.Initialize (Environment_Task); |
| 1429 |
|
| 1430 |
-- Do not use an alternate stack if no handler for SEGV is installed |
| 1431 |
|
| 1432 |
if State (SIGSEGV) = Default then |
| 1433 |
Use_Alternate_Stack := False; |
| 1434 |
end if; |
| 1435 |
|
| 1436 |
if Use_Alternate_Stack then |
| 1437 |
Environment_Task.Common.Task_Alternate_Stack := |
| 1438 |
Alternate_Stack'Address; |
| 1439 |
end if; |
| 1440 |
|
| 1441 |
-- Make environment task known here because it doesn't go through |
| 1442 |
-- Activate_Tasks, which does it for all other tasks. |
| 1443 |
|
| 1444 |
Known_Tasks (Known_Tasks'First) := Environment_Task; |
| 1445 |
Environment_Task.Known_Tasks_Index := Known_Tasks'First; |
| 1446 |
|
| 1447 |
Enter_Task (Environment_Task); |
| 1448 |
|
| 1449 |
if State |
| 1450 |
(System.Interrupt_Management.Abort_Task_Interrupt) /= Default |
| 1451 |
then |
| 1452 |
act.sa_flags := 0; |
| 1453 |
act.sa_handler := Abort_Handler'Address; |
| 1454 |
|
| 1455 |
Result := sigemptyset (Tmp_Set'Access); |
| 1456 |
pragma Assert (Result = 0); |
| 1457 |
act.sa_mask := Tmp_Set; |
| 1458 |
|
| 1459 |
Result := |
| 1460 |
sigaction |
| 1461 |
(Signal (Interrupt_Management.Abort_Task_Interrupt), |
| 1462 |
act'Unchecked_Access, |
| 1463 |
old_act'Unchecked_Access); |
| 1464 |
pragma Assert (Result = 0); |
| 1465 |
Abort_Handler_Installed := True; |
| 1466 |
end if; |
| 1467 |
|
| 1468 |
-- pragma CPU and dispatching domains for the environment task |
| 1469 |
|
| 1470 |
Set_Task_Affinity (Environment_Task); |
| 1471 |
end Initialize; |
| 1472 |
|
| 1473 |
----------------------- |
| 1474 |
-- Set_Task_Affinity -- |
| 1475 |
----------------------- |
| 1476 |
|
| 1477 |
procedure Set_Task_Affinity (T : ST.Task_Id) is |
| 1478 |
use type Multiprocessors.CPU_Range; |
| 1479 |
|
| 1480 |
begin |
| 1481 |
-- Do nothing if there is no support for setting affinities or the |
| 1482 |
-- underlying thread has not yet been created. If the thread has not |
| 1483 |
-- yet been created then the proper affinity will be set during its |
| 1484 |
-- creation. |
| 1485 |
|
| 1486 |
if pthread_setaffinity_np'Address /= Null_Address |
| 1487 |
and then T.Common.LL.Thread /= Null_Thread_Id |
| 1488 |
and then (T.Common.CPU_Is_Explicit |
| 1489 |
or else Requires_Affinity_Change (T.Common.Domain)) |
| 1490 |
then |
| 1491 |
declare |
| 1492 |
CPUs : constant size_t := |
| 1493 |
C.size_t (Multiprocessors.Number_Of_CPUs); |
| 1494 |
CPU_Set : cpu_set_t_ptr := null; |
| 1495 |
Is_Set_Owned : Boolean := False; |
| 1496 |
Size : constant size_t := CPU_ALLOC_SIZE (CPUs); |
| 1497 |
|
| 1498 |
Result : C.int; |
| 1499 |
|
| 1500 |
begin |
| 1501 |
-- We look at the specific CPU (Base_CPU) first, then at the |
| 1502 |
-- Task_Info field, and finally at the assigned dispatching |
| 1503 |
-- domain, if any. |
| 1504 |
|
| 1505 |
if T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then |
| 1506 |
|
| 1507 |
-- Set the affinity to an unique CPU |
| 1508 |
|
| 1509 |
CPU_Set := CPU_ALLOC (CPUs); |
| 1510 |
Is_Set_Owned := True; |
| 1511 |
System.OS_Interface.CPU_ZERO (Size, CPU_Set); |
| 1512 |
System.OS_Interface.CPU_SET |
| 1513 |
(int (T.Common.Base_CPU), Size, CPU_Set); |
| 1514 |
|
| 1515 |
-- Handle Task_Info |
| 1516 |
|
| 1517 |
elsif T.Common.Task_Info /= null then |
| 1518 |
CPU_Set := T.Common.Task_Info.CPU_Affinity'Access; |
| 1519 |
|
| 1520 |
-- Handle dispatching domains |
| 1521 |
|
| 1522 |
else |
| 1523 |
-- Set the affinity to all the processors belonging to the |
| 1524 |
-- dispatching domain. |
| 1525 |
|
| 1526 |
CPU_Set := CPU_ALLOC (CPUs); |
| 1527 |
Is_Set_Owned := True; |
| 1528 |
System.OS_Interface.CPU_ZERO (Size, CPU_Set); |
| 1529 |
|
| 1530 |
for Proc in T.Common.Domain'Range loop |
| 1531 |
if T.Common.Domain (Proc) then |
| 1532 |
System.OS_Interface.CPU_SET (int (Proc), Size, CPU_Set); |
| 1533 |
end if; |
| 1534 |
end loop; |
| 1535 |
end if; |
| 1536 |
|
| 1537 |
Result := |
| 1538 |
pthread_setaffinity_np (T.Common.LL.Thread, Size, CPU_Set); |
| 1539 |
pragma Assert (Result = 0); |
| 1540 |
|
| 1541 |
if Is_Set_Owned then |
| 1542 |
CPU_FREE (CPU_Set); |
| 1543 |
end if; |
| 1544 |
end; |
| 1545 |
end if; |
| 1546 |
end Set_Task_Affinity; |
| 1547 |
|
| 1548 |
end System.Task_Primitives.Operations; |