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