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