1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
9 -- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a NT (native) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
38 -- Turn off polling, we do not want ATC polling to take place during tasking
39 -- operations. It causes infinite loops and other problems.
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;
63 use Interfaces.C.Strings;
64 use System.OS_Interface;
65 use System.OS_Primitives;
66 use System.Parameters;
69 use System.Tasking.Debug;
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.
84 procedure InitializeCriticalSection (pCriticalSection : access RTS_Lock);
85 procedure InitializeCriticalSection
86 (pCriticalSection : access CRITICAL_SECTION);
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");
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
133 ------------------------------------
134 -- The thread local storage index --
135 ------------------------------------
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.
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);
154 -- Set the self id for the current task
158 package body Specific is
160 function Is_Valid_Task return Boolean is
162 return TlsGetValue (TlsIndex) /= System.Null_Address;
165 procedure Set (Self_Id : Task_Id) is
168 Succeeded := TlsSetValue (TlsIndex, To_Address (Self_Id));
169 pragma Assert (Succeeded = Win32.TRUE);
174 ----------------------------------
175 -- ATCB allocation/deallocation --
176 ----------------------------------
178 package body ATCB_Allocation is separate;
179 -- The body of this package is shared across several targets
181 ---------------------------------
182 -- Support for foreign threads --
183 ---------------------------------
185 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
186 -- Allocate and Initialize a new ATCB for the current Thread
188 function Register_Foreign_Thread
189 (Thread : Thread_Id) return Task_Id is separate;
191 ----------------------------------
192 -- Condition Variable Functions --
193 ----------------------------------
195 procedure Initialize_Cond (Cond : not null access Condition_Variable);
196 -- Initialize given condition variable Cond
198 procedure Finalize_Cond (Cond : not null access Condition_Variable);
199 -- Finalize given condition variable Cond
201 procedure Cond_Signal (Cond : not null access Condition_Variable);
202 -- Signal condition variable Cond
205 (Cond : not null access Condition_Variable;
206 L : not null access RTS_Lock);
207 -- Wait on conditional variable Cond, using lock L
209 procedure Cond_Timed_Wait
210 (Cond : not null access Condition_Variable;
211 L : not null access RTS_Lock;
213 Timed_Out : out Boolean;
214 Status : out Integer);
215 -- Do timed wait on condition variable Cond using lock L. The duration
216 -- of the timed wait is given by Rel_Time. When the condition is
217 -- signalled, Timed_Out shows whether or not a time out occurred.
218 -- Status is only valid if Timed_Out is False, in which case it
219 -- shows whether Cond_Timed_Wait completed successfully.
221 ---------------------
222 -- Initialize_Cond --
223 ---------------------
225 procedure Initialize_Cond (Cond : not null access Condition_Variable) is
228 hEvent := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr);
229 pragma Assert (hEvent /= 0);
230 Cond.all := Condition_Variable (hEvent);
237 -- No such problem here, DosCloseEventSem has been derived.
238 -- What does such refer to in above comment???
240 procedure Finalize_Cond (Cond : not null access Condition_Variable) is
243 Result := CloseHandle (HANDLE (Cond.all));
244 pragma Assert (Result = Win32.TRUE);
251 procedure Cond_Signal (Cond : not null access Condition_Variable) is
254 Result := SetEvent (HANDLE (Cond.all));
255 pragma Assert (Result = Win32.TRUE);
262 -- Pre-condition: Cond is posted
265 -- Post-condition: Cond is posted
269 (Cond : not null access Condition_Variable;
270 L : not null access RTS_Lock)
276 -- Must reset Cond BEFORE L is unlocked
278 Result_Bool := ResetEvent (HANDLE (Cond.all));
279 pragma Assert (Result_Bool = Win32.TRUE);
280 Unlock (L, Global_Lock => True);
282 -- No problem if we are interrupted here: if the condition is signaled,
283 -- WaitForSingleObject will simply not block
285 Result := WaitForSingleObject (HANDLE (Cond.all), Wait_Infinite);
286 pragma Assert (Result = 0);
288 Write_Lock (L, Global_Lock => True);
291 ---------------------
292 -- Cond_Timed_Wait --
293 ---------------------
295 -- Pre-condition: Cond is posted
298 -- Post-condition: Cond is posted
301 procedure Cond_Timed_Wait
302 (Cond : not null access Condition_Variable;
303 L : not null access RTS_Lock;
305 Timed_Out : out Boolean;
306 Status : out Integer)
308 Time_Out_Max : constant DWORD := 16#FFFF0000#;
309 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
316 -- Must reset Cond BEFORE L is unlocked
318 Result := ResetEvent (HANDLE (Cond.all));
319 pragma Assert (Result = Win32.TRUE);
320 Unlock (L, Global_Lock => True);
322 -- No problem if we are interrupted here: if the condition is signaled,
323 -- WaitForSingleObject will simply not block.
325 if Rel_Time <= 0.0 then
331 (if Rel_Time >= Duration (Time_Out_Max) / 1000
333 else DWORD (Rel_Time * 1000));
335 Wait_Result := WaitForSingleObject (HANDLE (Cond.all), Time_Out);
337 if Wait_Result = WAIT_TIMEOUT then
345 Write_Lock (L, Global_Lock => True);
347 -- Ensure post-condition
350 Result := SetEvent (HANDLE (Cond.all));
351 pragma Assert (Result = Win32.TRUE);
354 Status := Integer (Wait_Result);
361 -- The underlying thread system sets a guard page at the bottom of a thread
362 -- stack, so nothing is needed.
363 -- ??? Check the comment above
365 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
366 pragma Unreferenced (T, On);
375 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
377 return T.Common.LL.Thread;
384 function Self return Task_Id is
385 Self_Id : constant Task_Id := To_Task_Id (TlsGetValue (TlsIndex));
387 if Self_Id = null then
388 return Register_Foreign_Thread (GetCurrentThread);
394 ---------------------
395 -- Initialize_Lock --
396 ---------------------
398 -- Note: mutexes and cond_variables needed per-task basis are initialized
399 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
400 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
401 -- status change of RTS. Therefore raising Storage_Error in the following
402 -- routines should be able to be handled safely.
404 procedure Initialize_Lock
405 (Prio : System.Any_Priority;
406 L : not null access Lock)
409 InitializeCriticalSection (L.Mutex'Access);
410 L.Owner_Priority := 0;
414 procedure Initialize_Lock
415 (L : not null access RTS_Lock; Level : Lock_Level)
417 pragma Unreferenced (Level);
419 InitializeCriticalSection (L);
426 procedure Finalize_Lock (L : not null access Lock) is
428 DeleteCriticalSection (L.Mutex'Access);
431 procedure Finalize_Lock (L : not null access RTS_Lock) is
433 DeleteCriticalSection (L);
441 (L : not null access Lock; Ceiling_Violation : out Boolean) is
443 L.Owner_Priority := Get_Priority (Self);
445 if L.Priority < L.Owner_Priority then
446 Ceiling_Violation := True;
450 EnterCriticalSection (L.Mutex'Access);
452 Ceiling_Violation := False;
456 (L : not null access RTS_Lock;
457 Global_Lock : Boolean := False)
460 if not Single_Lock or else Global_Lock then
461 EnterCriticalSection (L);
465 procedure Write_Lock (T : Task_Id) is
467 if not Single_Lock then
468 EnterCriticalSection (T.Common.LL.L'Access);
477 (L : not null access Lock; Ceiling_Violation : out Boolean) is
479 Write_Lock (L, Ceiling_Violation);
486 procedure Unlock (L : not null access Lock) is
488 LeaveCriticalSection (L.Mutex'Access);
492 (L : not null access RTS_Lock; Global_Lock : Boolean := False) is
494 if not Single_Lock or else Global_Lock then
495 LeaveCriticalSection (L);
499 procedure Unlock (T : Task_Id) is
501 if not Single_Lock then
502 LeaveCriticalSection (T.Common.LL.L'Access);
510 -- Dynamic priority ceilings are not supported by the underlying system
512 procedure Set_Ceiling
513 (L : not null access Lock;
514 Prio : System.Any_Priority)
516 pragma Unreferenced (L, Prio);
527 Reason : System.Tasking.Task_States)
529 pragma Unreferenced (Reason);
532 pragma Assert (Self_ID = Self);
535 Cond_Wait (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
537 Cond_Wait (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
540 if Self_ID.Deferral_Level = 0
541 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
544 raise Standard'Abort_Signal;
552 -- This is for use within the run-time system, so abort is assumed to be
553 -- already deferred, and the caller should be holding its own ATCB lock.
555 procedure Timed_Sleep
558 Mode : ST.Delay_Modes;
559 Reason : System.Tasking.Task_States;
560 Timedout : out Boolean;
561 Yielded : out Boolean)
563 pragma Unreferenced (Reason);
564 Check_Time : Duration := Monotonic_Clock;
569 pragma Unreferenced (Result);
571 Local_Timedout : Boolean;
577 if Mode = Relative then
579 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
581 Rel_Time := Time - Check_Time;
585 if Rel_Time > 0.0 then
587 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
591 (Self_ID.Common.LL.CV'Access,
592 Single_RTS_Lock'Access,
593 Rel_Time, Local_Timedout, Result);
596 (Self_ID.Common.LL.CV'Access,
597 Self_ID.Common.LL.L'Access,
598 Rel_Time, Local_Timedout, Result);
601 Check_Time := Monotonic_Clock;
602 exit when Abs_Time <= Check_Time;
604 if not Local_Timedout then
606 -- Somebody may have called Wakeup for us
612 Rel_Time := Abs_Time - Check_Time;
621 procedure Timed_Delay
624 Mode : ST.Delay_Modes)
626 Check_Time : Duration := Monotonic_Clock;
632 pragma Unreferenced (Timedout, Result);
639 Write_Lock (Self_ID);
641 if Mode = Relative then
643 Abs_Time := Time + Check_Time;
645 Rel_Time := Time - Check_Time;
649 if Rel_Time > 0.0 then
650 Self_ID.Common.State := Delay_Sleep;
653 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
657 (Self_ID.Common.LL.CV'Access,
658 Single_RTS_Lock'Access,
659 Rel_Time, Timedout, Result);
662 (Self_ID.Common.LL.CV'Access,
663 Self_ID.Common.LL.L'Access,
664 Rel_Time, Timedout, Result);
667 Check_Time := Monotonic_Clock;
668 exit when Abs_Time <= Check_Time;
670 Rel_Time := Abs_Time - Check_Time;
673 Self_ID.Common.State := Runnable;
689 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
690 pragma Unreferenced (Reason);
692 Cond_Signal (T.Common.LL.CV'Access);
699 procedure Yield (Do_Yield : Boolean := True) is
701 -- Note: in a previous implementation if Do_Yield was False, then we
702 -- introduced a delay of 1 millisecond in an attempt to get closer to
703 -- annex D semantics, and in particular to make ACATS CXD8002 pass. But
704 -- this change introduced a huge performance regression evaluating the
705 -- Count attribute. So we decided to remove this processing.
707 -- Moreover, CXD8002 appears to pass on Windows (although we do not
708 -- guarantee full Annex D compliance on Windows in any case).
719 type Prio_Array_Type is array (System.Any_Priority) of Integer;
720 pragma Atomic_Components (Prio_Array_Type);
722 Prio_Array : Prio_Array_Type;
723 -- Global array containing the id of the currently running task for
726 -- Note: we assume that we are on a single processor with run-til-blocked
729 procedure Set_Priority
731 Prio : System.Any_Priority;
732 Loss_Of_Inheritance : Boolean := False)
735 Array_Item : Integer;
738 Res := SetThreadPriority
739 (T.Common.LL.Thread, Interfaces.C.int (Underlying_Priorities (Prio)));
740 pragma Assert (Res = Win32.TRUE);
742 if Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F' then
744 -- Annex D requirement [RM D.2.2 par. 9]:
745 -- If the task drops its priority due to the loss of inherited
746 -- priority, it is added at the head of the ready queue for its
747 -- new active priority.
749 if Loss_Of_Inheritance
750 and then Prio < T.Common.Current_Priority
752 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
753 Prio_Array (T.Common.Base_Priority) := Array_Item;
756 -- Let some processes a chance to arrive
760 -- Then wait for our turn to proceed
762 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
763 or else Prio_Array (T.Common.Base_Priority) = 1;
766 Prio_Array (T.Common.Base_Priority) :=
767 Prio_Array (T.Common.Base_Priority) - 1;
771 T.Common.Current_Priority := Prio;
778 function Get_Priority (T : Task_Id) return System.Any_Priority is
780 return T.Common.Current_Priority;
787 -- There were two paths were we needed to call Enter_Task :
788 -- 1) from System.Task_Primitives.Operations.Initialize
789 -- 2) from System.Tasking.Stages.Task_Wrapper
791 -- The thread initialisation has to be done only for the first case
793 -- This is because the GetCurrentThread NT call does not return the real
794 -- thread handler but only a "pseudo" one. It is not possible to release
795 -- the thread handle and free the system resources from this "pseudo"
796 -- handle. So we really want to keep the real thread handle set in
797 -- System.Task_Primitives.Operations.Create_Task during thread creation.
799 procedure Enter_Task (Self_ID : Task_Id) is
800 procedure Get_Stack_Bounds (Base : Address; Limit : Address);
801 pragma Import (C, Get_Stack_Bounds, "__gnat_get_stack_bounds");
802 -- Get stack boundaries
804 Specific.Set (Self_ID);
806 -- Properly initializes the FPU for x86 systems
808 System.Float_Control.Reset;
810 if Self_ID.Common.Task_Info /= null
812 Self_ID.Common.Task_Info.CPU >= CPU_Number (Number_Of_Processors)
814 raise Invalid_CPU_Number;
817 Self_ID.Common.LL.Thread_Id := GetCurrentThreadId;
820 (Self_ID.Common.Compiler_Data.Pri_Stack_Info.Base'Address,
821 Self_ID.Common.Compiler_Data.Pri_Stack_Info.Limit'Address);
828 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
830 -----------------------------
831 -- Register_Foreign_Thread --
832 -----------------------------
834 function Register_Foreign_Thread return Task_Id is
836 if Is_Valid_Task then
839 return Register_Foreign_Thread (GetCurrentThread);
841 end Register_Foreign_Thread;
847 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
849 -- Initialize thread ID to 0, this is needed to detect threads that
850 -- are not yet activated.
852 Self_ID.Common.LL.Thread := Null_Thread_Id;
854 Initialize_Cond (Self_ID.Common.LL.CV'Access);
856 if not Single_Lock then
857 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
867 procedure Create_Task
869 Wrapper : System.Address;
870 Stack_Size : System.Parameters.Size_Type;
871 Priority : System.Any_Priority;
872 Succeeded : out Boolean)
874 Initial_Stack_Size : constant := 1024;
875 -- We set the initial stack size to 1024. On Windows version prior to XP
876 -- there is no way to fix a task stack size. Only the initial stack size
877 -- can be set, the operating system will raise the task stack size if
880 function Is_Windows_XP return Integer;
881 pragma Import (C, Is_Windows_XP, "__gnat_is_windows_xp");
882 -- Returns 1 if running on Windows XP
885 TaskId : aliased DWORD;
886 pTaskParameter : Win32.PVOID;
888 Entry_Point : PTHREAD_START_ROUTINE;
890 use type System.Multiprocessors.CPU_Range;
893 -- Check whether both Dispatching_Domain and CPU are specified for the
894 -- task, and the CPU value is not contained within the range of
895 -- processors for the domain.
897 if T.Common.Domain /= null
898 and then T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU
900 (T.Common.Base_CPU not in T.Common.Domain'Range
901 or else not T.Common.Domain (T.Common.Base_CPU))
907 pTaskParameter := To_Address (T);
909 Entry_Point := To_PTHREAD_START_ROUTINE (Wrapper);
911 if Is_Windows_XP = 1 then
912 hTask := CreateThread
917 DWORD (Create_Suspended) or
918 DWORD (Stack_Size_Param_Is_A_Reservation),
919 TaskId'Unchecked_Access);
921 hTask := CreateThread
926 DWORD (Create_Suspended),
927 TaskId'Unchecked_Access);
930 -- Step 1: Create the thread in blocked mode
937 -- Step 2: set its TCB
939 T.Common.LL.Thread := hTask;
941 -- Note: it would be useful to initialize Thread_Id right away to avoid
942 -- a race condition in gdb where Thread_ID may not have the right value
943 -- yet, but GetThreadId is a Vista specific API, not available under XP:
944 -- T.Common.LL.Thread_Id := GetThreadId (hTask); so instead we set the
945 -- field to 0 to avoid having a random value. Thread_Id is initialized
946 -- in Enter_Task anyway.
948 T.Common.LL.Thread_Id := 0;
950 -- Step 3: set its priority (child has inherited priority from parent)
952 Set_Priority (T, Priority);
954 if Time_Slice_Val = 0
955 or else Dispatching_Policy = 'F'
956 or else Get_Policy (Priority) = 'F'
958 -- Here we need Annex D semantics so we disable the NT priority
959 -- boost. A priority boost is temporarily given by the system to
960 -- a thread when it is taken out of a wait state.
962 SetThreadPriorityBoost (hTask, DisablePriorityBoost => Win32.TRUE);
965 -- Step 4: Handle pragma CPU and Task_Info
967 Set_Task_Affinity (T);
969 -- Step 5: Now, start it for good
971 Result := ResumeThread (hTask);
972 pragma Assert (Result = 1);
974 Succeeded := Result = 1;
981 procedure Finalize_TCB (T : Task_Id) is
985 if not Single_Lock then
986 Finalize_Lock (T.Common.LL.L'Access);
989 Finalize_Cond (T.Common.LL.CV'Access);
991 if T.Known_Tasks_Index /= -1 then
992 Known_Tasks (T.Known_Tasks_Index) := null;
995 if T.Common.LL.Thread /= 0 then
997 -- This task has been activated. Close the thread handle. This
998 -- is needed to release system resources.
1000 Succeeded := CloseHandle (T.Common.LL.Thread);
1001 pragma Assert (Succeeded = Win32.TRUE);
1004 ATCB_Allocation.Free_ATCB (T);
1011 procedure Exit_Task is
1013 Specific.Set (null);
1020 procedure Abort_Task (T : Task_Id) is
1021 pragma Unreferenced (T);
1026 ----------------------
1027 -- Environment_Task --
1028 ----------------------
1030 function Environment_Task return Task_Id is
1032 return Environment_Task_Id;
1033 end Environment_Task;
1039 procedure Lock_RTS is
1041 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1048 procedure Unlock_RTS is
1050 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1057 procedure Initialize (Environment_Task : Task_Id) is
1059 pragma Unreferenced (Discard);
1062 Environment_Task_Id := Environment_Task;
1063 OS_Primitives.Initialize;
1064 Interrupt_Management.Initialize;
1066 if Time_Slice_Val = 0 or else Dispatching_Policy = 'F' then
1067 -- Here we need Annex D semantics, switch the current process to the
1068 -- Realtime_Priority_Class.
1070 Discard := OS_Interface.SetPriorityClass
1071 (GetCurrentProcess, Realtime_Priority_Class);
1074 TlsIndex := TlsAlloc;
1076 -- Initialize the lock used to synchronize chain of all ATCBs
1078 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1080 Environment_Task.Common.LL.Thread := GetCurrentThread;
1082 -- Make environment task known here because it doesn't go through
1083 -- Activate_Tasks, which does it for all other tasks.
1085 Known_Tasks (Known_Tasks'First) := Environment_Task;
1086 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1088 Enter_Task (Environment_Task);
1090 -- pragma CPU and dispatching domains for the environment task
1092 Set_Task_Affinity (Environment_Task);
1095 ---------------------
1096 -- Monotonic_Clock --
1097 ---------------------
1099 function Monotonic_Clock return Duration
1100 renames System.OS_Primitives.Monotonic_Clock;
1106 function RT_Resolution return Duration is
1108 return 0.000_001; -- 1 micro-second
1115 procedure Initialize (S : in out Suspension_Object) is
1117 -- Initialize internal state. It is always initialized to False (ARM
1123 -- Initialize internal mutex
1125 InitializeCriticalSection (S.L'Access);
1127 -- Initialize internal condition variable
1129 S.CV := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr);
1130 pragma Assert (S.CV /= 0);
1137 procedure Finalize (S : in out Suspension_Object) is
1141 -- Destroy internal mutex
1143 DeleteCriticalSection (S.L'Access);
1145 -- Destroy internal condition variable
1147 Result := CloseHandle (S.CV);
1148 pragma Assert (Result = Win32.TRUE);
1155 function Current_State (S : Suspension_Object) return Boolean is
1157 -- We do not want to use lock on this read operation. State is marked
1158 -- as Atomic so that we ensure that the value retrieved is correct.
1167 procedure Set_False (S : in out Suspension_Object) is
1169 SSL.Abort_Defer.all;
1171 EnterCriticalSection (S.L'Access);
1175 LeaveCriticalSection (S.L'Access);
1177 SSL.Abort_Undefer.all;
1184 procedure Set_True (S : in out Suspension_Object) is
1188 SSL.Abort_Defer.all;
1190 EnterCriticalSection (S.L'Access);
1192 -- If there is already a task waiting on this suspension object then
1193 -- we resume it, leaving the state of the suspension object to False,
1194 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1195 -- the state to True.
1201 Result := SetEvent (S.CV);
1202 pragma Assert (Result = Win32.TRUE);
1208 LeaveCriticalSection (S.L'Access);
1210 SSL.Abort_Undefer.all;
1213 ------------------------
1214 -- Suspend_Until_True --
1215 ------------------------
1217 procedure Suspend_Until_True (S : in out Suspension_Object) is
1222 SSL.Abort_Defer.all;
1224 EnterCriticalSection (S.L'Access);
1228 -- Program_Error must be raised upon calling Suspend_Until_True
1229 -- if another task is already waiting on that suspension object
1230 -- (ARM D.10 par. 10).
1232 LeaveCriticalSection (S.L'Access);
1234 SSL.Abort_Undefer.all;
1236 raise Program_Error;
1239 -- Suspend the task if the state is False. Otherwise, the task
1240 -- continues its execution, and the state of the suspension object
1241 -- is set to False (ARM D.10 par. 9).
1246 LeaveCriticalSection (S.L'Access);
1248 SSL.Abort_Undefer.all;
1253 -- Must reset CV BEFORE L is unlocked
1255 Result_Bool := ResetEvent (S.CV);
1256 pragma Assert (Result_Bool = Win32.TRUE);
1258 LeaveCriticalSection (S.L'Access);
1260 SSL.Abort_Undefer.all;
1262 Result := WaitForSingleObject (S.CV, Wait_Infinite);
1263 pragma Assert (Result = 0);
1266 end Suspend_Until_True;
1272 -- Dummy versions, currently this only works for solaris (native)
1274 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1275 pragma Unreferenced (Self_ID);
1280 --------------------
1281 -- Check_No_Locks --
1282 --------------------
1284 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1285 pragma Unreferenced (Self_ID);
1294 function Suspend_Task
1296 Thread_Self : Thread_Id) return Boolean
1299 if T.Common.LL.Thread /= Thread_Self then
1300 return SuspendThread (T.Common.LL.Thread) = NO_ERROR;
1310 function Resume_Task
1312 Thread_Self : Thread_Id) return Boolean
1315 if T.Common.LL.Thread /= Thread_Self then
1316 return ResumeThread (T.Common.LL.Thread) = NO_ERROR;
1322 --------------------
1323 -- Stop_All_Tasks --
1324 --------------------
1326 procedure Stop_All_Tasks is
1335 function Stop_Task (T : ST.Task_Id) return Boolean is
1336 pragma Unreferenced (T);
1345 function Continue_Task (T : ST.Task_Id) return Boolean is
1346 pragma Unreferenced (T);
1351 -----------------------
1352 -- Set_Task_Affinity --
1353 -----------------------
1355 procedure Set_Task_Affinity (T : ST.Task_Id) is
1358 use type System.Multiprocessors.CPU_Range;
1361 -- Do nothing if the underlying thread has not yet been created. If the
1362 -- thread has not yet been created then the proper affinity will be set
1363 -- during its creation.
1365 if T.Common.LL.Thread = Null_Thread_Id then
1370 elsif T.Common.Base_CPU /= Multiprocessors.Not_A_Specific_CPU then
1372 -- The CPU numbering in pragma CPU starts at 1 while the subprogram
1373 -- to set the affinity starts at 0, therefore we must substract 1.
1376 SetThreadIdealProcessor
1377 (T.Common.LL.Thread, ProcessorId (T.Common.Base_CPU) - 1);
1378 pragma Assert (Result = 1);
1382 elsif T.Common.Task_Info /= null then
1383 if T.Common.Task_Info.CPU /= Task_Info.Any_CPU then
1385 SetThreadIdealProcessor
1386 (T.Common.LL.Thread, T.Common.Task_Info.CPU);
1387 pragma Assert (Result = 1);
1390 -- Dispatching domains
1392 elsif T.Common.Domain /= null
1393 and then (T.Common.Domain /= ST.System_Domain
1395 T.Common.Domain.all /=
1396 (Multiprocessors.CPU'First ..
1397 Multiprocessors.Number_Of_CPUs => True))
1400 CPU_Set : DWORD := 0;
1403 for Proc in T.Common.Domain'Range loop
1404 if T.Common.Domain (Proc) then
1406 -- The thread affinity mask is a bit vector in which each
1407 -- bit represents a logical processor.
1409 CPU_Set := CPU_Set + 2 ** (Integer (Proc) - 1);
1413 Result := SetThreadAffinityMask (T.Common.LL.Thread, CPU_Set);
1414 pragma Assert (Result = 1);
1417 end Set_Task_Affinity;
1419 end System.Task_Primitives.Operations;