| 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 NT (native) 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 |
with Interfaces.C.Strings; |
| 39 |
|
| 40 |
with System.Float_Control; |
| 41 |
with System.Interrupt_Management; |
| 42 |
with System.Multiprocessors; |
| 43 |
with System.OS_Primitives; |
| 44 |
with System.Task_Info; |
| 45 |
with System.Tasking.Debug; |
| 46 |
with System.Win32.Ext; |
| 47 |
|
| 48 |
with System.Soft_Links; |
| 49 |
-- We use System.Soft_Links instead of System.Tasking.Initialization because |
| 50 |
-- the later is a higher level package that we shouldn't depend on. For |
| 51 |
-- example when using the restricted run time, it is replaced by |
| 52 |
-- System.Tasking.Restricted.Stages. |
| 53 |
|
| 54 |
package body System.Task_Primitives.Operations is |
| 55 |
|
| 56 |
package SSL renames System.Soft_Links; |
| 57 |
|
| 58 |
use Interfaces.C; |
| 59 |
use Interfaces.C.Strings; |
| 60 |
|
| 61 |
use System.OS_Interface; |
| 62 |
use System.OS_Locks; |
| 63 |
use System.OS_Primitives; |
| 64 |
use System.Parameters; |
| 65 |
use System.Task_Info; |
| 66 |
use System.Tasking; |
| 67 |
use System.Tasking.Debug; |
| 68 |
use System.Win32; |
| 69 |
use System.Win32.Ext; |
| 70 |
|
| 71 |
pragma Link_With ("-Xlinker --stack=0x800000,0x1000"); |
| 72 |
-- Change the default stack size (8 MB) for tasking programs on Windows. |
| 73 |
-- This allows at least 1000 tasks running at the same time. Note that |
| 74 |
-- we set the stack size for non tasking programs in the System unit. |
| 75 |
-- Also note that under Windows, we use a Windows extension to |
| 76 |
-- specify the stack size on a per task basis, as done under other OSes. |
| 77 |
|
| 78 |
---------------- |
| 79 |
-- Local Data -- |
| 80 |
---------------- |
| 81 |
|
| 82 |
Environment_Task_Id : Task_Id; |
| 83 |
-- A variable to hold Task_Id for the environment task |
| 84 |
|
| 85 |
Single_RTS_Lock : aliased RTS_Lock; |
| 86 |
-- This is a lock to allow only one thread of control in the RTS at |
| 87 |
-- a time; it is used to execute in mutual exclusion from all other tasks. |
| 88 |
-- Used to protect All_Tasks_List |
| 89 |
|
| 90 |
Time_Slice_Val : constant Integer; |
| 91 |
pragma Import (C, Time_Slice_Val, "__gl_time_slice_val"); |
| 92 |
|
| 93 |
Dispatching_Policy : constant Character; |
| 94 |
pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy"); |
| 95 |
|
| 96 |
function Get_Policy (Prio : System.Any_Priority) return Character; |
| 97 |
pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching"); |
| 98 |
-- Get priority specific dispatching policy |
| 99 |
|
| 100 |
Foreign_Task_Elaborated : aliased Boolean := True; |
| 101 |
-- Used to identified fake tasks (i.e., non-Ada Threads) |
| 102 |
|
| 103 |
Null_Thread_Id : constant Thread_Id := 0; |
| 104 |
-- Constant to indicate that the thread identifier has not yet been |
| 105 |
-- initialized. |
| 106 |
|
| 107 |
------------------------------------ |
| 108 |
-- The thread local storage index -- |
| 109 |
------------------------------------ |
| 110 |
|
| 111 |
TlsIndex : DWORD; |
| 112 |
pragma Export (Ada, TlsIndex); |
| 113 |
-- To ensure that this variable won't be local to this package, since |
| 114 |
-- in some cases, inlining forces this variable to be global anyway. |
| 115 |
|
| 116 |
-------------------- |
| 117 |
-- Local Packages -- |
| 118 |
-------------------- |
| 119 |
|
| 120 |
package Specific is |
| 121 |
|
| 122 |
function Is_Valid_Task return Boolean; |
| 123 |
pragma Inline (Is_Valid_Task); |
| 124 |
-- Does executing thread have a TCB? |
| 125 |
|
| 126 |
procedure Set (Self_Id : Task_Id); |
| 127 |
pragma Inline (Set); |
| 128 |
-- Set the self id for the current task |
| 129 |
|
| 130 |
end Specific; |
| 131 |
|
| 132 |
package body Specific is |
| 133 |
|
| 134 |
------------------- |
| 135 |
-- Is_Valid_Task -- |
| 136 |
------------------- |
| 137 |
|
| 138 |
function Is_Valid_Task return Boolean is |
| 139 |
begin |
| 140 |
return TlsGetValue (TlsIndex) /= System.Null_Address; |
| 141 |
end Is_Valid_Task; |
| 142 |
|
| 143 |
--------- |
| 144 |
-- Set -- |
| 145 |
--------- |
| 146 |
|
| 147 |
procedure Set (Self_Id : Task_Id) is |
| 148 |
Succeeded : BOOL; |
| 149 |
begin |
| 150 |
Succeeded := TlsSetValue (TlsIndex, To_Address (Self_Id)); |
| 151 |
pragma Assert (Succeeded = Win32.TRUE); |
| 152 |
end Set; |
| 153 |
|
| 154 |
end Specific; |
| 155 |
|
| 156 |
---------------------------------- |
| 157 |
-- ATCB allocation/deallocation -- |
| 158 |
---------------------------------- |
| 159 |
|
| 160 |
package body ATCB_Allocation is separate; |
| 161 |
-- The body of this package is shared across several targets |
| 162 |
|
| 163 |
--------------------------------- |
| 164 |
-- Support for foreign threads -- |
| 165 |
--------------------------------- |
| 166 |
|
| 167 |
function Register_Foreign_Thread |
| 168 |
(Thread : Thread_Id; |
| 169 |
Sec_Stack_Size : Size_Type := Unspecified_Size) return Task_Id; |
| 170 |
-- Allocate and initialize a new ATCB for the current Thread. The size of |
| 171 |
-- the secondary stack can be optionally specified. |
| 172 |
|
| 173 |
function Register_Foreign_Thread |
| 174 |
(Thread : Thread_Id; |
| 175 |
Sec_Stack_Size : Size_Type := Unspecified_Size) |
| 176 |
return Task_Id is separate; |
| 177 |
|
| 178 |
---------------------------------- |
| 179 |
-- Condition Variable Functions -- |
| 180 |
---------------------------------- |
| 181 |
|
| 182 |
procedure Initialize_Cond (Cond : not null access Condition_Variable); |
| 183 |
-- Initialize given condition variable Cond |
| 184 |
|
| 185 |
procedure Finalize_Cond (Cond : not null access Condition_Variable); |
| 186 |
-- Finalize given condition variable Cond |
| 187 |
|
| 188 |
procedure Cond_Signal (Cond : not null access Condition_Variable); |
| 189 |
-- Signal condition variable Cond |
| 190 |
|
| 191 |
procedure Cond_Wait |
| 192 |
(Cond : not null access Condition_Variable; |
| 193 |
L : not null access RTS_Lock); |
| 194 |
-- Wait on conditional variable Cond, using lock L |
| 195 |
|
| 196 |
procedure Cond_Timed_Wait |
| 197 |
(Cond : not null access Condition_Variable; |
| 198 |
L : not null access RTS_Lock; |
| 199 |
Rel_Time : Duration; |
| 200 |
Timed_Out : out Boolean; |
| 201 |
Status : out Integer); |
| 202 |
-- Do timed wait on condition variable Cond using lock L. The duration |
| 203 |
-- of the timed wait is given by Rel_Time. When the condition is |
| 204 |
-- signalled, Timed_Out shows whether or not a time out occurred. |
| 205 |
-- Status is only valid if Timed_Out is False, in which case it |
| 206 |
-- shows whether Cond_Timed_Wait completed successfully. |
| 207 |
|
| 208 |
--------------------- |
| 209 |
-- Initialize_Cond -- |
| 210 |
--------------------- |
| 211 |
|
| 212 |
procedure Initialize_Cond (Cond : not null access Condition_Variable) is |
| 213 |
hEvent : HANDLE; |
| 214 |
begin |
| 215 |
hEvent := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr); |
| 216 |
pragma Assert (hEvent /= 0); |
| 217 |
Cond.all := Condition_Variable (hEvent); |
| 218 |
end Initialize_Cond; |
| 219 |
|
| 220 |
------------------- |
| 221 |
-- Finalize_Cond -- |
| 222 |
------------------- |
| 223 |
|
| 224 |
-- No such problem here, DosCloseEventSem has been derived. |
| 225 |
-- What does such refer to in above comment??? |
| 226 |
|
| 227 |
procedure Finalize_Cond (Cond : not null access Condition_Variable) is |
| 228 |
Result : BOOL; |
| 229 |
begin |
| 230 |
Result := CloseHandle (HANDLE (Cond.all)); |
| 231 |
pragma Assert (Result = Win32.TRUE); |
| 232 |
end Finalize_Cond; |
| 233 |
|
| 234 |
----------------- |
| 235 |
-- Cond_Signal -- |
| 236 |
----------------- |
| 237 |
|
| 238 |
procedure Cond_Signal (Cond : not null access Condition_Variable) is |
| 239 |
Result : BOOL; |
| 240 |
begin |
| 241 |
Result := SetEvent (HANDLE (Cond.all)); |
| 242 |
pragma Assert (Result = Win32.TRUE); |
| 243 |
end Cond_Signal; |
| 244 |
|
| 245 |
--------------- |
| 246 |
-- Cond_Wait -- |
| 247 |
--------------- |
| 248 |
|
| 249 |
-- Pre-condition: Cond is posted |
| 250 |
-- L is locked. |
| 251 |
|
| 252 |
-- Post-condition: Cond is posted |
| 253 |
-- L is locked. |
| 254 |
|
| 255 |
procedure Cond_Wait |
| 256 |
(Cond : not null access Condition_Variable; |
| 257 |
L : not null access RTS_Lock) |
| 258 |
is |
| 259 |
Result : DWORD; |
| 260 |
Result_Bool : BOOL; |
| 261 |
|
| 262 |
begin |
| 263 |
-- Must reset Cond BEFORE L is unlocked |
| 264 |
|
| 265 |
Result_Bool := ResetEvent (HANDLE (Cond.all)); |
| 266 |
pragma Assert (Result_Bool = Win32.TRUE); |
| 267 |
Unlock (L); |
| 268 |
|
| 269 |
-- No problem if we are interrupted here: if the condition is signaled, |
| 270 |
-- WaitForSingleObject will simply not block |
| 271 |
|
| 272 |
Result := WaitForSingleObject (HANDLE (Cond.all), Wait_Infinite); |
| 273 |
pragma Assert (Result = 0); |
| 274 |
|
| 275 |
Write_Lock (L); |
| 276 |
end Cond_Wait; |
| 277 |
|
| 278 |
--------------------- |
| 279 |
-- Cond_Timed_Wait -- |
| 280 |
--------------------- |
| 281 |
|
| 282 |
-- Pre-condition: Cond is posted |
| 283 |
-- L is locked. |
| 284 |
|
| 285 |
-- Post-condition: Cond is posted |
| 286 |
-- L is locked. |
| 287 |
|
| 288 |
procedure Cond_Timed_Wait |
| 289 |
(Cond : not null access Condition_Variable; |
| 290 |
L : not null access RTS_Lock; |
| 291 |
Rel_Time : Duration; |
| 292 |
Timed_Out : out Boolean; |
| 293 |
Status : out Integer) |
| 294 |
is |
| 295 |
Time_Out_Max : constant DWORD := 16#FFFF0000#; |
| 296 |
-- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1) |
| 297 |
|
| 298 |
Time_Out : DWORD; |
| 299 |
Result : BOOL; |
| 300 |
Wait_Result : DWORD; |
| 301 |
|
| 302 |
begin |
| 303 |
-- Must reset Cond BEFORE L is unlocked |
| 304 |
|
| 305 |
Result := ResetEvent (HANDLE (Cond.all)); |
| 306 |
pragma Assert (Result = Win32.TRUE); |
| 307 |
Unlock (L); |
| 308 |
|
| 309 |
-- No problem if we are interrupted here: if the condition is signaled, |
| 310 |
-- WaitForSingleObject will simply not block. |
| 311 |
|
| 312 |
if Rel_Time <= 0.0 then |
| 313 |
Timed_Out := True; |
| 314 |
Wait_Result := 0; |
| 315 |
|
| 316 |
else |
| 317 |
Time_Out := |
| 318 |
(if Rel_Time >= Duration (Time_Out_Max) / 1000 |
| 319 |
then Time_Out_Max |
| 320 |
else DWORD (Rel_Time * 1000)); |
| 321 |
|
| 322 |
Wait_Result := WaitForSingleObject (HANDLE (Cond.all), Time_Out); |
| 323 |
|
| 324 |
if Wait_Result = WAIT_TIMEOUT then |
| 325 |
Timed_Out := True; |
| 326 |
Wait_Result := 0; |
| 327 |
else |
| 328 |
Timed_Out := False; |
| 329 |
end if; |
| 330 |
end if; |
| 331 |
|
| 332 |
Write_Lock (L); |
| 333 |
|
| 334 |
-- Ensure post-condition |
| 335 |
|
| 336 |
if Timed_Out then |
| 337 |
Result := SetEvent (HANDLE (Cond.all)); |
| 338 |
pragma Assert (Result = Win32.TRUE); |
| 339 |
end if; |
| 340 |
|
| 341 |
Status := Integer (Wait_Result); |
| 342 |
end Cond_Timed_Wait; |
| 343 |
|
| 344 |
------------------ |
| 345 |
-- Stack_Guard -- |
| 346 |
------------------ |
| 347 |
|
| 348 |
-- The underlying thread system sets a guard page at the bottom of a thread |
| 349 |
-- stack, so nothing is needed. |
| 350 |
-- ??? Check the comment above |
| 351 |
|
| 352 |
procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is |
| 353 |
pragma Unreferenced (T, On); |
| 354 |
begin |
| 355 |
null; |
| 356 |
end Stack_Guard; |
| 357 |
|
| 358 |
-------------------- |
| 359 |
-- Get_Thread_Id -- |
| 360 |
-------------------- |
| 361 |
|
| 362 |
function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is |
| 363 |
begin |
| 364 |
return T.Common.LL.Thread; |
| 365 |
end Get_Thread_Id; |
| 366 |
|
| 367 |
---------- |
| 368 |
-- Self -- |
| 369 |
---------- |
| 370 |
|
| 371 |
function Self return Task_Id is |
| 372 |
Self_Id : constant Task_Id := To_Task_Id (TlsGetValue (TlsIndex)); |
| 373 |
begin |
| 374 |
if Self_Id = null then |
| 375 |
return Register_Foreign_Thread (GetCurrentThread); |
| 376 |
else |
| 377 |
return Self_Id; |
| 378 |
end if; |
| 379 |
end Self; |
| 380 |
|
| 381 |
--------------------- |
| 382 |
-- Initialize_Lock -- |
| 383 |
--------------------- |
| 384 |
|
| 385 |
-- Note: mutexes and cond_variables needed per-task basis are initialized |
| 386 |
-- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such |
| 387 |
-- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any |
| 388 |
-- status change of RTS. Therefore raising Storage_Error in the following |
| 389 |
-- routines should be able to be handled safely. |
| 390 |
|
| 391 |
procedure Initialize_Lock |
| 392 |
(Prio : System.Any_Priority; |
| 393 |
L : not null access Lock) |
| 394 |
is |
| 395 |
begin |
| 396 |
InitializeCriticalSection (L.Mutex'Access); |
| 397 |
L.Owner_Priority := 0; |
| 398 |
L.Priority := Prio; |
| 399 |
end Initialize_Lock; |
| 400 |
|
| 401 |
procedure Initialize_Lock |
| 402 |
(L : not null access RTS_Lock; |
| 403 |
Level : Lock_Level) |
| 404 |
is |
| 405 |
pragma Unreferenced (Level); |
| 406 |
begin |
| 407 |
InitializeCriticalSection (L); |
| 408 |
end Initialize_Lock; |
| 409 |
|
| 410 |
------------------- |
| 411 |
-- Finalize_Lock -- |
| 412 |
------------------- |
| 413 |
|
| 414 |
procedure Finalize_Lock (L : not null access Lock) is |
| 415 |
begin |
| 416 |
DeleteCriticalSection (L.Mutex'Access); |
| 417 |
end Finalize_Lock; |
| 418 |
|
| 419 |
procedure Finalize_Lock (L : not null access RTS_Lock) is |
| 420 |
begin |
| 421 |
DeleteCriticalSection (L); |
| 422 |
end Finalize_Lock; |
| 423 |
|
| 424 |
---------------- |
| 425 |
-- Write_Lock -- |
| 426 |
---------------- |
| 427 |
|
| 428 |
procedure Write_Lock |
| 429 |
(L : not null access Lock; Ceiling_Violation : out Boolean) is |
| 430 |
begin |
| 431 |
L.Owner_Priority := Get_Priority (Self); |
| 432 |
|
| 433 |
if L.Priority < L.Owner_Priority then |
| 434 |
Ceiling_Violation := True; |
| 435 |
return; |
| 436 |
end if; |
| 437 |
|
| 438 |
EnterCriticalSection (L.Mutex'Access); |
| 439 |
|
| 440 |
Ceiling_Violation := False; |
| 441 |
end Write_Lock; |
| 442 |
|
| 443 |
procedure Write_Lock (L : not null access RTS_Lock) is |
| 444 |
begin |
| 445 |
EnterCriticalSection (L); |
| 446 |
end Write_Lock; |
| 447 |
|
| 448 |
procedure Write_Lock (T : Task_Id) is |
| 449 |
begin |
| 450 |
EnterCriticalSection (T.Common.LL.L'Access); |
| 451 |
end Write_Lock; |
| 452 |
|
| 453 |
--------------- |
| 454 |
-- Read_Lock -- |
| 455 |
--------------- |
| 456 |
|
| 457 |
procedure Read_Lock |
| 458 |
(L : not null access Lock; Ceiling_Violation : out Boolean) is |
| 459 |
begin |
| 460 |
Write_Lock (L, Ceiling_Violation); |
| 461 |
end Read_Lock; |
| 462 |
|
| 463 |
------------ |
| 464 |
-- Unlock -- |
| 465 |
------------ |
| 466 |
|
| 467 |
procedure Unlock (L : not null access Lock) is |
| 468 |
begin |
| 469 |
LeaveCriticalSection (L.Mutex'Access); |
| 470 |
end Unlock; |
| 471 |
|
| 472 |
procedure Unlock (L : not null access RTS_Lock) is |
| 473 |
begin |
| 474 |
LeaveCriticalSection (L); |
| 475 |
end Unlock; |
| 476 |
|
| 477 |
procedure Unlock (T : Task_Id) is |
| 478 |
begin |
| 479 |
LeaveCriticalSection (T.Common.LL.L'Access); |
| 480 |
end Unlock; |
| 481 |
|
| 482 |
----------------- |
| 483 |
-- Set_Ceiling -- |
| 484 |
----------------- |
| 485 |
|
| 486 |
-- Dynamic priority ceilings are not supported by the underlying system |
| 487 |
|
| 488 |
procedure Set_Ceiling |
| 489 |
(L : not null access Lock; |
| 490 |
Prio : System.Any_Priority) |
| 491 |
is |
| 492 |
pragma Unreferenced (L, Prio); |
| 493 |
begin |
| 494 |
null; |
| 495 |
end Set_Ceiling; |
| 496 |
|
| 497 |
----------- |
| 498 |
-- Sleep -- |
| 499 |
----------- |
| 500 |
|
| 501 |
procedure Sleep |
| 502 |
(Self_ID : Task_Id; |
| 503 |
Reason : System.Tasking.Task_States) |
| 504 |
is |
| 505 |
pragma Unreferenced (Reason); |
| 506 |
|
| 507 |
begin |
| 508 |
pragma Assert (Self_ID = Self); |
| 509 |
|
| 510 |
Cond_Wait (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access); |
| 511 |
|
| 512 |
if Self_ID.Deferral_Level = 0 |
| 513 |
and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level |
| 514 |
then |
| 515 |
Unlock (Self_ID); |
| 516 |
raise Standard'Abort_Signal; |
| 517 |
end if; |
| 518 |
end Sleep; |
| 519 |
|
| 520 |
----------------- |
| 521 |
-- Timed_Sleep -- |
| 522 |
----------------- |
| 523 |
|
| 524 |
-- This is for use within the run-time system, so abort is assumed to be |
| 525 |
-- already deferred, and the caller should be holding its own ATCB lock. |
| 526 |
|
| 527 |
procedure Timed_Sleep |
| 528 |
(Self_ID : Task_Id; |
| 529 |
Time : Duration; |
| 530 |
Mode : ST.Delay_Modes; |
| 531 |
Reason : System.Tasking.Task_States; |
| 532 |
Timedout : out Boolean; |
| 533 |
Yielded : out Boolean) |
| 534 |
is |
| 535 |
pragma Unreferenced (Reason); |
| 536 |
Check_Time : Duration := Monotonic_Clock; |
| 537 |
Rel_Time : Duration; |
| 538 |
Abs_Time : Duration; |
| 539 |
|
| 540 |
Result : Integer; |
| 541 |
|
| 542 |
Local_Timedout : Boolean; |
| 543 |
|
| 544 |
begin |
| 545 |
Timedout := True; |
| 546 |
Yielded := False; |
| 547 |
|
| 548 |
if Mode = Relative then |
| 549 |
Rel_Time := Time; |
| 550 |
Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time; |
| 551 |
else |
| 552 |
Rel_Time := Time - Check_Time; |
| 553 |
Abs_Time := Time; |
| 554 |
end if; |
| 555 |
|
| 556 |
if Rel_Time > 0.0 then |
| 557 |
loop |
| 558 |
exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level; |
| 559 |
|
| 560 |
Cond_Timed_Wait |
| 561 |
(Self_ID.Common.LL.CV'Access, |
| 562 |
Self_ID.Common.LL.L'Access, |
| 563 |
Rel_Time, Local_Timedout, Result); |
| 564 |
Check_Time := Monotonic_Clock; |
| 565 |
|
| 566 |
exit when Abs_Time <= Check_Time; |
| 567 |
|
| 568 |
if not Local_Timedout then |
| 569 |
|
| 570 |
-- Somebody may have called Wakeup for us |
| 571 |
|
| 572 |
Timedout := False; |
| 573 |
exit; |
| 574 |
end if; |
| 575 |
|
| 576 |
Rel_Time := Abs_Time - Check_Time; |
| 577 |
end loop; |
| 578 |
end if; |
| 579 |
end Timed_Sleep; |
| 580 |
|
| 581 |
----------------- |
| 582 |
-- Timed_Delay -- |
| 583 |
----------------- |
| 584 |
|
| 585 |
procedure Timed_Delay |
| 586 |
(Self_ID : Task_Id; |
| 587 |
Time : Duration; |
| 588 |
Mode : ST.Delay_Modes) |
| 589 |
is |
| 590 |
Check_Time : Duration := Monotonic_Clock; |
| 591 |
Rel_Time : Duration; |
| 592 |
Abs_Time : Duration; |
| 593 |
|
| 594 |
Timedout : Boolean; |
| 595 |
Result : Integer; |
| 596 |
|
| 597 |
begin |
| 598 |
Write_Lock (Self_ID); |
| 599 |
|
| 600 |
if Mode = Relative then |
| 601 |
Rel_Time := Time; |
| 602 |
Abs_Time := Time + Check_Time; |
| 603 |
else |
| 604 |
Rel_Time := Time - Check_Time; |
| 605 |
Abs_Time := Time; |
| 606 |
end if; |
| 607 |
|
| 608 |
if Rel_Time > 0.0 then |
| 609 |
Self_ID.Common.State := Delay_Sleep; |
| 610 |
|
| 611 |
loop |
| 612 |
exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level; |
| 613 |
|
| 614 |
Cond_Timed_Wait |
| 615 |
(Self_ID.Common.LL.CV'Access, |
| 616 |
Self_ID.Common.LL.L'Access, |
| 617 |
Rel_Time, Timedout, Result); |
| 618 |
Check_Time := Monotonic_Clock; |
| 619 |
|
| 620 |
exit when Abs_Time <= Check_Time; |
| 621 |
|
| 622 |
Rel_Time := Abs_Time - Check_Time; |
| 623 |
end loop; |
| 624 |
|
| 625 |
Self_ID.Common.State := Runnable; |
| 626 |
end if; |
| 627 |
|
| 628 |
Unlock (Self_ID); |
| 629 |
Yield; |
| 630 |
end Timed_Delay; |
| 631 |
|
| 632 |
------------ |
| 633 |
-- Wakeup -- |
| 634 |
------------ |
| 635 |
|
| 636 |
procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is |
| 637 |
pragma Unreferenced (Reason); |
| 638 |
begin |
| 639 |
Cond_Signal (T.Common.LL.CV'Access); |
| 640 |
end Wakeup; |
| 641 |
|
| 642 |
----------- |
| 643 |
-- Yield -- |
| 644 |
----------- |
| 645 |
|
| 646 |
procedure Yield (Do_Yield : Boolean := True) is |
| 647 |
begin |
| 648 |
-- Note: in a previous implementation if Do_Yield was False, then we |
| 649 |
-- introduced a delay of 1 millisecond in an attempt to get closer to |
| 650 |
-- annex D semantics, and in particular to make ACATS CXD8002 pass. But |
| 651 |
-- this change introduced a huge performance regression evaluating the |
| 652 |
-- Count attribute. So we decided to remove this processing. |
| 653 |
|
| 654 |
-- Moreover, CXD8002 appears to pass on Windows (although we do not |
| 655 |
-- guarantee full Annex D compliance on Windows in any case). |
| 656 |
|
| 657 |
if Do_Yield then |
| 658 |
SwitchToThread; |
| 659 |
end if; |
| 660 |
end Yield; |
| 661 |
|
| 662 |
------------------ |
| 663 |
-- Set_Priority -- |
| 664 |
------------------ |
| 665 |
|
| 666 |
procedure Set_Priority |
| 667 |
(T : Task_Id; |
| 668 |
Prio : System.Any_Priority; |
| 669 |
Loss_Of_Inheritance : Boolean := False) |
| 670 |
is |
| 671 |
Res : BOOL; |
| 672 |
pragma Unreferenced (Loss_Of_Inheritance); |
| 673 |
|
| 674 |
begin |
| 675 |
Res := |
| 676 |
SetThreadPriority |
| 677 |
(T.Common.LL.Thread, |
| 678 |
Interfaces.C.int ((if Dispatching_Policy = 'F' then |
| 679 |
FIFO_Underlying_Priorities (Prio) |
| 680 |
else |
| 681 |
Underlying_Priorities (Prio)))); |
| 682 |
pragma Assert (Res = Win32.TRUE); |
| 683 |
|
| 684 |
-- Note: Annex D (RM D.2.3(5/2)) requires the task to be placed at the |
| 685 |
-- head of its priority queue when decreasing its priority as a result |
| 686 |
-- of a loss of inherited priority. This is not the case, but we |
| 687 |
-- consider it an acceptable variation (RM 1.1.3(6)), given this is |
| 688 |
-- the built-in behavior offered by the Windows operating system. |
| 689 |
|
| 690 |
-- In older versions we attempted to better approximate the Annex D |
| 691 |
-- required behavior, but this simulation was not entirely accurate, |
| 692 |
-- and it seems better to live with the standard Windows semantics. |
| 693 |
|
| 694 |
T.Common.Current_Priority := Prio; |
| 695 |
end Set_Priority; |
| 696 |
|
| 697 |
------------------ |
| 698 |
-- Get_Priority -- |
| 699 |
------------------ |
| 700 |
|
| 701 |
function Get_Priority (T : Task_Id) return System.Any_Priority is |
| 702 |
begin |
| 703 |
return T.Common.Current_Priority; |
| 704 |
end Get_Priority; |
| 705 |
|
| 706 |
---------------- |
| 707 |
-- Enter_Task -- |
| 708 |
---------------- |
| 709 |
|
| 710 |
-- There were two paths were we needed to call Enter_Task : |
| 711 |
-- 1) from System.Task_Primitives.Operations.Initialize |
| 712 |
-- 2) from System.Tasking.Stages.Task_Wrapper |
| 713 |
|
| 714 |
-- The pseudo handle (LL.Thread) need not be closed when it is no |
| 715 |
-- longer needed. Calling the CloseHandle function with this handle |
| 716 |
-- has no effect. |
| 717 |
|
| 718 |
procedure Enter_Task (Self_ID : Task_Id) is |
| 719 |
procedure Get_Stack_Bounds (Base : Address; Limit : Address); |
| 720 |
pragma Import (C, Get_Stack_Bounds, "__gnat_get_stack_bounds"); |
| 721 |
-- Get stack boundaries |
| 722 |
begin |
| 723 |
Specific.Set (Self_ID); |
| 724 |
|
| 725 |
-- Properly initializes the FPU for x86 systems |
| 726 |
|
| 727 |
System.Float_Control.Reset; |
| 728 |
|
| 729 |
if Self_ID.Common.Task_Info /= null |
| 730 |
and then |
| 731 |
Self_ID.Common.Task_Info.CPU >= CPU_Number (Number_Of_Processors) |
| 732 |
then |
| 733 |
raise Invalid_CPU_Number; |
| 734 |
end if; |
| 735 |
|
| 736 |
-- Initialize the thread here only if not set. This is done for a |
| 737 |
-- foreign task but is not needed when a real thread-id is already |
| 738 |
-- set in Create_Task. Note that we do want to keep the real thread-id |
| 739 |
-- as it is the only way to free the associated resource. Another way |
| 740 |
-- to say this is that a pseudo thread-id from a foreign thread won't |
| 741 |
-- allow for freeing resources. |
| 742 |
|
| 743 |
if Self_ID.Common.LL.Thread = Null_Thread_Id then |
| 744 |
Self_ID.Common.LL.Thread := GetCurrentThread; |
| 745 |
end if; |
| 746 |
|
| 747 |
Self_ID.Common.LL.Thread_Id := GetCurrentThreadId; |
| 748 |
|
| 749 |
Get_Stack_Bounds |
| 750 |
(Self_ID.Common.Compiler_Data.Pri_Stack_Info.Base'Address, |
| 751 |
Self_ID.Common.Compiler_Data.Pri_Stack_Info.Limit'Address); |
| 752 |
end Enter_Task; |
| 753 |
|
| 754 |
------------------- |
| 755 |
-- Is_Valid_Task -- |
| 756 |
------------------- |
| 757 |
|
| 758 |
function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task; |
| 759 |
|
| 760 |
----------------------------- |
| 761 |
-- Register_Foreign_Thread -- |
| 762 |
----------------------------- |
| 763 |
|
| 764 |
function Register_Foreign_Thread return Task_Id is |
| 765 |
begin |
| 766 |
if Is_Valid_Task then |
| 767 |
return Self; |
| 768 |
else |
| 769 |
return Register_Foreign_Thread (GetCurrentThread); |
| 770 |
end if; |
| 771 |
end Register_Foreign_Thread; |
| 772 |
|
| 773 |
-------------------- |
| 774 |
-- Initialize_TCB -- |
| 775 |
-------------------- |
| 776 |
|
| 777 |
procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is |
| 778 |
begin |
| 779 |
-- Initialize thread ID to 0, this is needed to detect threads that |
| 780 |
-- are not yet activated. |
| 781 |
|
| 782 |
Self_ID.Common.LL.Thread := Null_Thread_Id; |
| 783 |
|
| 784 |
Initialize_Cond (Self_ID.Common.LL.CV'Access); |
| 785 |
Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level); |
| 786 |
|
| 787 |
Succeeded := True; |
| 788 |
end Initialize_TCB; |
| 789 |
|
| 790 |
----------------- |
| 791 |
-- Create_Task -- |
| 792 |
----------------- |
| 793 |
|
| 794 |
procedure Create_Task |
| 795 |
(T : Task_Id; |
| 796 |
Wrapper : System.Address; |
| 797 |
Stack_Size : System.Parameters.Size_Type; |
| 798 |
Priority : System.Any_Priority; |
| 799 |
Succeeded : out Boolean) |
| 800 |
is |
| 801 |
Initial_Stack_Size : constant := 1024; |
| 802 |
-- We set the initial stack size to 1024. On Windows version prior to XP |
| 803 |
-- there is no way to fix a task stack size. Only the initial stack size |
| 804 |
-- can be set, the operating system will raise the task stack size if |
| 805 |
-- needed. |
| 806 |
|
| 807 |
function Is_Windows_XP return Integer; |
| 808 |
pragma Import (C, Is_Windows_XP, "__gnat_is_windows_xp"); |
| 809 |
-- Returns 1 if running on Windows XP |
| 810 |
|
| 811 |
hTask : HANDLE; |
| 812 |
TaskId : aliased DWORD; |
| 813 |
pTaskParameter : Win32.PVOID; |
| 814 |
Result : DWORD; |
| 815 |
Entry_Point : PTHREAD_START_ROUTINE; |
| 816 |
|
| 817 |
use type System.Multiprocessors.CPU_Range; |
| 818 |
|
| 819 |
begin |
| 820 |
-- Check whether both Dispatching_Domain and CPU are specified for the |
| 821 |
-- task, and the CPU value is not contained within the range of |
| 822 |
-- processors for the domain. |
| 823 |
|
| 824 |
if T.Common.Domain /= null |
| 825 |
and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU |
| 826 |
and then |
| 827 |
(T.Common.Base_CPU not in T.Common.Domain'Range |
| 828 |
or else not T.Common.Domain (T.Common.Base_CPU)) |
| 829 |
then |
| 830 |
Succeeded := False; |
| 831 |
return; |
| 832 |
end if; |
| 833 |
|
| 834 |
pTaskParameter := To_Address (T); |
| 835 |
|
| 836 |
Entry_Point := To_PTHREAD_START_ROUTINE (Wrapper); |
| 837 |
|
| 838 |
if Is_Windows_XP = 1 then |
| 839 |
hTask := CreateThread |
| 840 |
(null, |
| 841 |
DWORD (Stack_Size), |
| 842 |
Entry_Point, |
| 843 |
pTaskParameter, |
| 844 |
DWORD (Create_Suspended) |
| 845 |
or DWORD (Stack_Size_Param_Is_A_Reservation), |
| 846 |
TaskId'Unchecked_Access); |
| 847 |
else |
| 848 |
hTask := CreateThread |
| 849 |
(null, |
| 850 |
Initial_Stack_Size, |
| 851 |
Entry_Point, |
| 852 |
pTaskParameter, |
| 853 |
DWORD (Create_Suspended), |
| 854 |
TaskId'Unchecked_Access); |
| 855 |
end if; |
| 856 |
|
| 857 |
-- Step 1: Create the thread in blocked mode |
| 858 |
|
| 859 |
if hTask = 0 then |
| 860 |
Succeeded := False; |
| 861 |
return; |
| 862 |
end if; |
| 863 |
|
| 864 |
-- Step 2: set its TCB |
| 865 |
|
| 866 |
T.Common.LL.Thread := hTask; |
| 867 |
|
| 868 |
-- Note: it would be useful to initialize Thread_Id right away to avoid |
| 869 |
-- a race condition in gdb where Thread_ID may not have the right value |
| 870 |
-- yet, but GetThreadId is a Vista specific API, not available under XP: |
| 871 |
-- T.Common.LL.Thread_Id := GetThreadId (hTask); so instead we set the |
| 872 |
-- field to 0 to avoid having a random value. Thread_Id is initialized |
| 873 |
-- in Enter_Task anyway. |
| 874 |
|
| 875 |
T.Common.LL.Thread_Id := 0; |
| 876 |
|
| 877 |
-- Step 3: set its priority (child has inherited priority from parent) |
| 878 |
|
| 879 |
Set_Priority (T, Priority); |
| 880 |
|
| 881 |
if Time_Slice_Val = 0 |
| 882 |
or else Dispatching_Policy = 'F' |
| 883 |
or else Get_Policy (Priority) = 'F' |
| 884 |
then |
| 885 |
-- Here we need Annex D semantics so we disable the NT priority |
| 886 |
-- boost. A priority boost is temporarily given by the system to |
| 887 |
-- a thread when it is taken out of a wait state. |
| 888 |
|
| 889 |
SetThreadPriorityBoost (hTask, DisablePriorityBoost => Win32.TRUE); |
| 890 |
end if; |
| 891 |
|
| 892 |
-- Step 4: Handle pragma CPU and Task_Info |
| 893 |
|
| 894 |
Set_Task_Affinity (T); |
| 895 |
|
| 896 |
-- Step 5: Now, start it for good |
| 897 |
|
| 898 |
Result := ResumeThread (hTask); |
| 899 |
pragma Assert (Result = 1); |
| 900 |
|
| 901 |
Succeeded := Result = 1; |
| 902 |
end Create_Task; |
| 903 |
|
| 904 |
------------------ |
| 905 |
-- Finalize_TCB -- |
| 906 |
------------------ |
| 907 |
|
| 908 |
procedure Finalize_TCB (T : Task_Id) is |
| 909 |
Succeeded : BOOL; |
| 910 |
pragma Unreferenced (Succeeded); |
| 911 |
|
| 912 |
begin |
| 913 |
Finalize_Lock (T.Common.LL.L'Access); |
| 914 |
Finalize_Cond (T.Common.LL.CV'Access); |
| 915 |
|
| 916 |
if T.Known_Tasks_Index /= -1 then |
| 917 |
Known_Tasks (T.Known_Tasks_Index) := null; |
| 918 |
end if; |
| 919 |
|
| 920 |
if T.Common.LL.Thread /= Null_Thread_Id then |
| 921 |
|
| 922 |
-- This task has been activated. Close the thread handle. This |
| 923 |
-- is needed to release system resources. |
| 924 |
|
| 925 |
Succeeded := CloseHandle (T.Common.LL.Thread); |
| 926 |
-- Note that we do not check for the returned value, this is |
| 927 |
-- because the above call will fail for a foreign thread. But |
| 928 |
-- we still need to call it to properly close Ada tasks created |
| 929 |
-- with CreateThread() in Create_Task above. |
| 930 |
end if; |
| 931 |
|
| 932 |
ATCB_Allocation.Free_ATCB (T); |
| 933 |
end Finalize_TCB; |
| 934 |
|
| 935 |
--------------- |
| 936 |
-- Exit_Task -- |
| 937 |
--------------- |
| 938 |
|
| 939 |
procedure Exit_Task is |
| 940 |
begin |
| 941 |
Specific.Set (null); |
| 942 |
end Exit_Task; |
| 943 |
|
| 944 |
---------------- |
| 945 |
-- Abort_Task -- |
| 946 |
---------------- |
| 947 |
|
| 948 |
procedure Abort_Task (T : Task_Id) is |
| 949 |
pragma Unreferenced (T); |
| 950 |
begin |
| 951 |
null; |
| 952 |
end Abort_Task; |
| 953 |
|
| 954 |
---------------------- |
| 955 |
-- Environment_Task -- |
| 956 |
---------------------- |
| 957 |
|
| 958 |
function Environment_Task return Task_Id is |
| 959 |
begin |
| 960 |
return Environment_Task_Id; |
| 961 |
end Environment_Task; |
| 962 |
|
| 963 |
-------------- |
| 964 |
-- Lock_RTS -- |
| 965 |
-------------- |
| 966 |
|
| 967 |
procedure Lock_RTS is |
| 968 |
begin |
| 969 |
Write_Lock (Single_RTS_Lock'Access); |
| 970 |
end Lock_RTS; |
| 971 |
|
| 972 |
---------------- |
| 973 |
-- Unlock_RTS -- |
| 974 |
---------------- |
| 975 |
|
| 976 |
procedure Unlock_RTS is |
| 977 |
begin |
| 978 |
Unlock (Single_RTS_Lock'Access); |
| 979 |
end Unlock_RTS; |
| 980 |
|
| 981 |
---------------- |
| 982 |
-- Initialize -- |
| 983 |
---------------- |
| 984 |
|
| 985 |
procedure Initialize (Environment_Task : Task_Id) is |
| 986 |
Discard : BOOL; |
| 987 |
|
| 988 |
begin |
| 989 |
Environment_Task_Id := Environment_Task; |
| 990 |
OS_Primitives.Initialize; |
| 991 |
Interrupt_Management.Initialize; |
| 992 |
|
| 993 |
if Time_Slice_Val = 0 or else Dispatching_Policy = 'F' then |
| 994 |
-- Here we need Annex D semantics, switch the current process to the |
| 995 |
-- Realtime_Priority_Class. |
| 996 |
|
| 997 |
Discard := OS_Interface.SetPriorityClass |
| 998 |
(GetCurrentProcess, Realtime_Priority_Class); |
| 999 |
end if; |
| 1000 |
|
| 1001 |
TlsIndex := TlsAlloc; |
| 1002 |
|
| 1003 |
-- Initialize the lock used to synchronize chain of all ATCBs |
| 1004 |
|
| 1005 |
Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level); |
| 1006 |
|
| 1007 |
Environment_Task.Common.LL.Thread := GetCurrentThread; |
| 1008 |
|
| 1009 |
-- Make environment task known here because it doesn't go through |
| 1010 |
-- Activate_Tasks, which does it for all other tasks. |
| 1011 |
|
| 1012 |
Known_Tasks (Known_Tasks'First) := Environment_Task; |
| 1013 |
Environment_Task.Known_Tasks_Index := Known_Tasks'First; |
| 1014 |
|
| 1015 |
Enter_Task (Environment_Task); |
| 1016 |
|
| 1017 |
-- pragma CPU and dispatching domains for the environment task |
| 1018 |
|
| 1019 |
Set_Task_Affinity (Environment_Task); |
| 1020 |
end Initialize; |
| 1021 |
|
| 1022 |
--------------------- |
| 1023 |
-- Monotonic_Clock -- |
| 1024 |
--------------------- |
| 1025 |
|
| 1026 |
function Monotonic_Clock return Duration is |
| 1027 |
function Internal_Clock return Duration; |
| 1028 |
pragma Import (Ada, Internal_Clock, "__gnat_monotonic_clock"); |
| 1029 |
begin |
| 1030 |
return Internal_Clock; |
| 1031 |
end Monotonic_Clock; |
| 1032 |
|
| 1033 |
------------------- |
| 1034 |
-- RT_Resolution -- |
| 1035 |
------------------- |
| 1036 |
|
| 1037 |
function RT_Resolution return Duration is |
| 1038 |
Ticks_Per_Second : aliased System.OS_Interface.LARGE_INTEGER; |
| 1039 |
begin |
| 1040 |
QueryPerformanceFrequency (Ticks_Per_Second'Access); |
| 1041 |
return Duration (1.0 / Ticks_Per_Second); |
| 1042 |
end RT_Resolution; |
| 1043 |
|
| 1044 |
---------------- |
| 1045 |
-- Initialize -- |
| 1046 |
---------------- |
| 1047 |
|
| 1048 |
procedure Initialize (S : in out Suspension_Object) is |
| 1049 |
begin |
| 1050 |
-- Initialize internal state. It is always initialized to False (ARM |
| 1051 |
-- D.10 par. 6). |
| 1052 |
|
| 1053 |
S.State := False; |
| 1054 |
S.Waiting := False; |
| 1055 |
|
| 1056 |
-- Initialize internal mutex |
| 1057 |
|
| 1058 |
InitializeCriticalSection (S.L'Access); |
| 1059 |
|
| 1060 |
-- Initialize internal condition variable |
| 1061 |
|
| 1062 |
S.CV := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr); |
| 1063 |
pragma Assert (S.CV /= 0); |
| 1064 |
end Initialize; |
| 1065 |
|
| 1066 |
-------------- |
| 1067 |
-- Finalize -- |
| 1068 |
-------------- |
| 1069 |
|
| 1070 |
procedure Finalize (S : in out Suspension_Object) is |
| 1071 |
Result : BOOL; |
| 1072 |
|
| 1073 |
begin |
| 1074 |
-- Destroy internal mutex |
| 1075 |
|
| 1076 |
DeleteCriticalSection (S.L'Access); |
| 1077 |
|
| 1078 |
-- Destroy internal condition variable |
| 1079 |
|
| 1080 |
Result := CloseHandle (S.CV); |
| 1081 |
pragma Assert (Result = Win32.TRUE); |
| 1082 |
end Finalize; |
| 1083 |
|
| 1084 |
------------------- |
| 1085 |
-- Current_State -- |
| 1086 |
------------------- |
| 1087 |
|
| 1088 |
function Current_State (S : Suspension_Object) return Boolean is |
| 1089 |
begin |
| 1090 |
-- We do not want to use lock on this read operation. State is marked |
| 1091 |
-- as Atomic so that we ensure that the value retrieved is correct. |
| 1092 |
|
| 1093 |
return S.State; |
| 1094 |
end Current_State; |
| 1095 |
|
| 1096 |
--------------- |
| 1097 |
-- Set_False -- |
| 1098 |
--------------- |
| 1099 |
|
| 1100 |
procedure Set_False (S : in out Suspension_Object) is |
| 1101 |
begin |
| 1102 |
SSL.Abort_Defer.all; |
| 1103 |
|
| 1104 |
EnterCriticalSection (S.L'Access); |
| 1105 |
|
| 1106 |
S.State := False; |
| 1107 |
|
| 1108 |
LeaveCriticalSection (S.L'Access); |
| 1109 |
|
| 1110 |
SSL.Abort_Undefer.all; |
| 1111 |
end Set_False; |
| 1112 |
|
| 1113 |
-------------- |
| 1114 |
-- Set_True -- |
| 1115 |
-------------- |
| 1116 |
|
| 1117 |
procedure Set_True (S : in out Suspension_Object) is |
| 1118 |
Result : BOOL; |
| 1119 |
|
| 1120 |
begin |
| 1121 |
SSL.Abort_Defer.all; |
| 1122 |
|
| 1123 |
EnterCriticalSection (S.L'Access); |
| 1124 |
|
| 1125 |
-- If there is already a task waiting on this suspension object then |
| 1126 |
-- we resume it, leaving the state of the suspension object to False, |
| 1127 |
-- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves |
| 1128 |
-- the state to True. |
| 1129 |
|
| 1130 |
if S.Waiting then |
| 1131 |
S.Waiting := False; |
| 1132 |
S.State := False; |
| 1133 |
|
| 1134 |
Result := SetEvent (S.CV); |
| 1135 |
pragma Assert (Result = Win32.TRUE); |
| 1136 |
|
| 1137 |
else |
| 1138 |
S.State := True; |
| 1139 |
end if; |
| 1140 |
|
| 1141 |
LeaveCriticalSection (S.L'Access); |
| 1142 |
|
| 1143 |
SSL.Abort_Undefer.all; |
| 1144 |
end Set_True; |
| 1145 |
|
| 1146 |
------------------------ |
| 1147 |
-- Suspend_Until_True -- |
| 1148 |
------------------------ |
| 1149 |
|
| 1150 |
procedure Suspend_Until_True (S : in out Suspension_Object) is |
| 1151 |
Result : DWORD; |
| 1152 |
Result_Bool : BOOL; |
| 1153 |
|
| 1154 |
begin |
| 1155 |
SSL.Abort_Defer.all; |
| 1156 |
|
| 1157 |
EnterCriticalSection (S.L'Access); |
| 1158 |
|
| 1159 |
if S.Waiting then |
| 1160 |
|
| 1161 |
-- Program_Error must be raised upon calling Suspend_Until_True |
| 1162 |
-- if another task is already waiting on that suspension object |
| 1163 |
-- (ARM D.10 par. 10). |
| 1164 |
|
| 1165 |
LeaveCriticalSection (S.L'Access); |
| 1166 |
|
| 1167 |
SSL.Abort_Undefer.all; |
| 1168 |
|
| 1169 |
raise Program_Error; |
| 1170 |
|
| 1171 |
else |
| 1172 |
-- Suspend the task if the state is False. Otherwise, the task |
| 1173 |
-- continues its execution, and the state of the suspension object |
| 1174 |
-- is set to False (ARM D.10 par. 9). |
| 1175 |
|
| 1176 |
if S.State then |
| 1177 |
S.State := False; |
| 1178 |
|
| 1179 |
LeaveCriticalSection (S.L'Access); |
| 1180 |
|
| 1181 |
SSL.Abort_Undefer.all; |
| 1182 |
|
| 1183 |
else |
| 1184 |
S.Waiting := True; |
| 1185 |
|
| 1186 |
-- Must reset CV BEFORE L is unlocked |
| 1187 |
|
| 1188 |
Result_Bool := ResetEvent (S.CV); |
| 1189 |
pragma Assert (Result_Bool = Win32.TRUE); |
| 1190 |
|
| 1191 |
LeaveCriticalSection (S.L'Access); |
| 1192 |
|
| 1193 |
SSL.Abort_Undefer.all; |
| 1194 |
|
| 1195 |
Result := WaitForSingleObject (S.CV, Wait_Infinite); |
| 1196 |
pragma Assert (Result = 0); |
| 1197 |
end if; |
| 1198 |
end if; |
| 1199 |
end Suspend_Until_True; |
| 1200 |
|
| 1201 |
---------------- |
| 1202 |
-- Check_Exit -- |
| 1203 |
---------------- |
| 1204 |
|
| 1205 |
-- Dummy versions, currently this only works for solaris (native) |
| 1206 |
|
| 1207 |
function Check_Exit (Self_ID : ST.Task_Id) return Boolean is |
| 1208 |
pragma Unreferenced (Self_ID); |
| 1209 |
begin |
| 1210 |
return True; |
| 1211 |
end Check_Exit; |
| 1212 |
|
| 1213 |
-------------------- |
| 1214 |
-- Check_No_Locks -- |
| 1215 |
-------------------- |
| 1216 |
|
| 1217 |
function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is |
| 1218 |
pragma Unreferenced (Self_ID); |
| 1219 |
begin |
| 1220 |
return True; |
| 1221 |
end Check_No_Locks; |
| 1222 |
|
| 1223 |
------------------ |
| 1224 |
-- Suspend_Task -- |
| 1225 |
------------------ |
| 1226 |
|
| 1227 |
function Suspend_Task |
| 1228 |
(T : ST.Task_Id; |
| 1229 |
Thread_Self : Thread_Id) return Boolean |
| 1230 |
is |
| 1231 |
begin |
| 1232 |
if T.Common.LL.Thread /= Thread_Self then |
| 1233 |
return SuspendThread (T.Common.LL.Thread) = NO_ERROR; |
| 1234 |
else |
| 1235 |
return True; |
| 1236 |
end if; |
| 1237 |
end Suspend_Task; |
| 1238 |
|
| 1239 |
----------------- |
| 1240 |
-- Resume_Task -- |
| 1241 |
----------------- |
| 1242 |
|
| 1243 |
function Resume_Task |
| 1244 |
(T : ST.Task_Id; |
| 1245 |
Thread_Self : Thread_Id) return Boolean |
| 1246 |
is |
| 1247 |
begin |
| 1248 |
if T.Common.LL.Thread /= Thread_Self then |
| 1249 |
return ResumeThread (T.Common.LL.Thread) = NO_ERROR; |
| 1250 |
else |
| 1251 |
return True; |
| 1252 |
end if; |
| 1253 |
end Resume_Task; |
| 1254 |
|
| 1255 |
-------------------- |
| 1256 |
-- Stop_All_Tasks -- |
| 1257 |
-------------------- |
| 1258 |
|
| 1259 |
procedure Stop_All_Tasks is |
| 1260 |
begin |
| 1261 |
null; |
| 1262 |
end Stop_All_Tasks; |
| 1263 |
|
| 1264 |
--------------- |
| 1265 |
-- Stop_Task -- |
| 1266 |
--------------- |
| 1267 |
|
| 1268 |
function Stop_Task (T : ST.Task_Id) return Boolean is |
| 1269 |
pragma Unreferenced (T); |
| 1270 |
begin |
| 1271 |
return False; |
| 1272 |
end Stop_Task; |
| 1273 |
|
| 1274 |
------------------- |
| 1275 |
-- Continue_Task -- |
| 1276 |
------------------- |
| 1277 |
|
| 1278 |
function Continue_Task (T : ST.Task_Id) return Boolean is |
| 1279 |
pragma Unreferenced (T); |
| 1280 |
begin |
| 1281 |
return False; |
| 1282 |
end Continue_Task; |
| 1283 |
|
| 1284 |
----------------------- |
| 1285 |
-- Set_Task_Affinity -- |
| 1286 |
----------------------- |
| 1287 |
|
| 1288 |
procedure Set_Task_Affinity (T : ST.Task_Id) is |
| 1289 |
Result : DWORD; |
| 1290 |
|
| 1291 |
use type System.Multiprocessors.CPU_Range; |
| 1292 |
|
| 1293 |
begin |
| 1294 |
-- Do nothing if the underlying thread has not yet been created. If the |
| 1295 |
-- thread has not yet been created then the proper affinity will be set |
| 1296 |
-- during its creation. |
| 1297 |
|
| 1298 |
if T.Common.LL.Thread = Null_Thread_Id then |
| 1299 |
null; |
| 1300 |
|
| 1301 |
-- pragma CPU |
| 1302 |
|
| 1303 |
elsif T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then |
| 1304 |
|
| 1305 |
-- The CPU numbering in pragma CPU starts at 1 while the subprogram |
| 1306 |
-- to set the affinity starts at 0, therefore we must substract 1. |
| 1307 |
|
| 1308 |
Result := |
| 1309 |
SetThreadIdealProcessor |
| 1310 |
(T.Common.LL.Thread, ProcessorId (T.Common.Base_CPU) - 1); |
| 1311 |
|
| 1312 |
-- The documentation for SetThreadIdealProcessor states: |
| 1313 |
-- |
| 1314 |
-- If the function fails, the return value is (DWORD) - 1. |
| 1315 |
-- |
| 1316 |
-- That should map to DWORD'Last in Ada. |
| 1317 |
pragma Assert (Result /= DWORD'Last); |
| 1318 |
|
| 1319 |
-- Task_Info |
| 1320 |
|
| 1321 |
elsif T.Common.Task_Info /= null then |
| 1322 |
if T.Common.Task_Info.CPU /= Task_Info.Any_CPU then |
| 1323 |
Result := |
| 1324 |
SetThreadIdealProcessor |
| 1325 |
(T.Common.LL.Thread, T.Common.Task_Info.CPU); |
| 1326 |
|
| 1327 |
-- See the comment above about the return value of |
| 1328 |
-- SetThreadIdealProcessor. |
| 1329 |
pragma Assert (Result /= DWORD'Last); |
| 1330 |
end if; |
| 1331 |
|
| 1332 |
-- Dispatching domains |
| 1333 |
|
| 1334 |
elsif T.Common.Domain /= null |
| 1335 |
and then (T.Common.Domain /= ST.System_Domain |
| 1336 |
or else |
| 1337 |
T.Common.Domain.all /= |
| 1338 |
(Multiprocessors.CPU'First .. |
| 1339 |
Multiprocessors.Number_Of_CPUs => True)) |
| 1340 |
then |
| 1341 |
declare |
| 1342 |
CPU_Set : DWORD := 0; |
| 1343 |
Mask_Result : DWORD_PTR; |
| 1344 |
begin |
| 1345 |
for Proc in T.Common.Domain'Range loop |
| 1346 |
if T.Common.Domain (Proc) then |
| 1347 |
|
| 1348 |
-- The thread affinity mask is a bit vector in which each |
| 1349 |
-- bit represents a logical processor. |
| 1350 |
|
| 1351 |
CPU_Set := CPU_Set + 2 ** (Integer (Proc) - 1); |
| 1352 |
end if; |
| 1353 |
end loop; |
| 1354 |
|
| 1355 |
Mask_Result := SetThreadAffinityMask (T.Common.LL.Thread, CPU_Set); |
| 1356 |
pragma Assert (Mask_Result /= 0); |
| 1357 |
end; |
| 1358 |
end if; |
| 1359 |
end Set_Task_Affinity; |
| 1360 |
|
| 1361 |
end System.Task_Primitives.Operations; |