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-2007, 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is a NT (native) version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System.Tasking.Debug;
44 -- used for Known_Tasks
46 with System.OS_Primitives;
47 -- used for Delay_Modes
53 with Interfaces.C.Strings;
56 with System.Task_Info;
57 -- used for Unspecified_Task_Info
59 with System.Interrupt_Management;
60 -- used for Initialize
62 with System.Soft_Links;
63 -- used for Abort_Defer/Undefer
65 -- We use System.Soft_Links instead of System.Tasking.Initialization because
66 -- the later is a higher level package that we shouldn't depend on. For
67 -- example when using the restricted run time, it is replaced by
68 -- System.Tasking.Restricted.Stages.
70 with Ada.Unchecked_Deallocation;
72 package body System.Task_Primitives.Operations is
74 package SSL renames System.Soft_Links;
76 use System.Tasking.Debug;
79 use Interfaces.C.Strings;
80 use System.OS_Interface;
81 use System.Parameters;
82 use System.OS_Primitives;
85 pragma Link_With ("-Xlinker --stack=0x200000,0x1000");
86 -- Change the default stack size (2 MB) for tasking programs on Windows.
87 -- This allows about 1000 tasks running at the same time. Note that
88 -- we set the stack size for non tasking programs on System unit.
89 -- Also note that under Windows XP, we use a Windows XP extension to
90 -- specify the stack size on a per task basis, as done under other OSes.
96 Environment_Task_Id : Task_Id;
97 -- A variable to hold Task_Id for the environment task
99 Single_RTS_Lock : aliased RTS_Lock;
100 -- This is a lock to allow only one thread of control in the RTS at
101 -- a time; it is used to execute in mutual exclusion from all other tasks.
102 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
104 Time_Slice_Val : Integer;
105 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
107 Dispatching_Policy : Character;
108 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
110 function Get_Policy (Prio : System.Any_Priority) return Character;
111 pragma Import (C, Get_Policy, "__gnat_get_specific_dispatching");
112 -- Get priority specific dispatching policy
114 Foreign_Task_Elaborated : aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 Annex_D : Boolean := False;
118 -- Set to True if running with Annex-D semantics
120 ------------------------------------
121 -- The thread local storage index --
122 ------------------------------------
125 pragma Export (Ada, TlsIndex);
126 -- To ensure that this variable won't be local to this package, since
127 -- in some cases, inlining forces this variable to be global anyway.
135 function Is_Valid_Task return Boolean;
136 pragma Inline (Is_Valid_Task);
137 -- Does executing thread have a TCB?
139 procedure Set (Self_Id : Task_Id);
141 -- Set the self id for the current task
145 package body Specific is
147 function Is_Valid_Task return Boolean is
149 return TlsGetValue (TlsIndex) /= System.Null_Address;
152 procedure Set (Self_Id : Task_Id) is
155 Succeeded := TlsSetValue (TlsIndex, To_Address (Self_Id));
156 pragma Assert (Succeeded = True);
161 ---------------------------------
162 -- Support for foreign threads --
163 ---------------------------------
165 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
166 -- Allocate and Initialize a new ATCB for the current Thread
168 function Register_Foreign_Thread
169 (Thread : Thread_Id) return Task_Id is separate;
171 ----------------------------------
172 -- Condition Variable Functions --
173 ----------------------------------
175 procedure Initialize_Cond (Cond : not null access Condition_Variable);
176 -- Initialize given condition variable Cond
178 procedure Finalize_Cond (Cond : not null access Condition_Variable);
179 -- Finalize given condition variable Cond
181 procedure Cond_Signal (Cond : not null access Condition_Variable);
182 -- Signal condition variable Cond
185 (Cond : not null access Condition_Variable;
186 L : not null access RTS_Lock);
187 -- Wait on conditional variable Cond, using lock L
189 procedure Cond_Timed_Wait
190 (Cond : not null access Condition_Variable;
191 L : not null access RTS_Lock;
193 Timed_Out : out Boolean;
194 Status : out Integer);
195 -- Do timed wait on condition variable Cond using lock L. The duration
196 -- of the timed wait is given by Rel_Time. When the condition is
197 -- signalled, Timed_Out shows whether or not a time out occurred.
198 -- Status is only valid if Timed_Out is False, in which case it
199 -- shows whether Cond_Timed_Wait completed successfully.
201 ---------------------
202 -- Initialize_Cond --
203 ---------------------
205 procedure Initialize_Cond (Cond : not null access Condition_Variable) is
208 hEvent := CreateEvent (null, True, False, Null_Ptr);
209 pragma Assert (hEvent /= 0);
210 Cond.all := Condition_Variable (hEvent);
217 -- No such problem here, DosCloseEventSem has been derived.
218 -- What does such refer to in above comment???
220 procedure Finalize_Cond (Cond : not null access Condition_Variable) is
223 Result := CloseHandle (HANDLE (Cond.all));
224 pragma Assert (Result = True);
231 procedure Cond_Signal (Cond : not null access Condition_Variable) is
234 Result := SetEvent (HANDLE (Cond.all));
235 pragma Assert (Result = True);
242 -- Pre-condition: Cond is posted
245 -- Post-condition: Cond is posted
249 (Cond : not null access Condition_Variable;
250 L : not null access RTS_Lock)
256 -- Must reset Cond BEFORE L is unlocked
258 Result_Bool := ResetEvent (HANDLE (Cond.all));
259 pragma Assert (Result_Bool = True);
260 Unlock (L, Global_Lock => True);
262 -- No problem if we are interrupted here: if the condition is signaled,
263 -- WaitForSingleObject will simply not block
265 Result := WaitForSingleObject (HANDLE (Cond.all), Wait_Infinite);
266 pragma Assert (Result = 0);
268 Write_Lock (L, Global_Lock => True);
271 ---------------------
272 -- Cond_Timed_Wait --
273 ---------------------
275 -- Pre-condition: Cond is posted
278 -- Post-condition: Cond is posted
281 procedure Cond_Timed_Wait
282 (Cond : not null access Condition_Variable;
283 L : not null access RTS_Lock;
285 Timed_Out : out Boolean;
286 Status : out Integer)
288 Time_Out_Max : constant DWORD := 16#FFFF0000#;
289 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
296 -- Must reset Cond BEFORE L is unlocked
298 Result := ResetEvent (HANDLE (Cond.all));
299 pragma Assert (Result = True);
300 Unlock (L, Global_Lock => True);
302 -- No problem if we are interrupted here: if the condition is signaled,
303 -- WaitForSingleObject will simply not block
305 if Rel_Time <= 0.0 then
310 if Rel_Time >= Duration (Time_Out_Max) / 1000 then
311 Time_Out := Time_Out_Max;
313 Time_Out := DWORD (Rel_Time * 1000);
316 Wait_Result := WaitForSingleObject (HANDLE (Cond.all), Time_Out);
318 if Wait_Result = WAIT_TIMEOUT then
326 Write_Lock (L, Global_Lock => True);
328 -- Ensure post-condition
331 Result := SetEvent (HANDLE (Cond.all));
332 pragma Assert (Result = True);
335 Status := Integer (Wait_Result);
342 -- The underlying thread system sets a guard page at the bottom of a thread
343 -- stack, so nothing is needed.
344 -- ??? Check the comment above
346 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
347 pragma Unreferenced (T, On);
356 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
358 return T.Common.LL.Thread;
365 function Self return Task_Id is
366 Self_Id : constant Task_Id := To_Task_Id (TlsGetValue (TlsIndex));
368 if Self_Id = null then
369 return Register_Foreign_Thread (GetCurrentThread);
375 ---------------------
376 -- Initialize_Lock --
377 ---------------------
379 -- Note: mutexes and cond_variables needed per-task basis are initialized
380 -- in Intialize_TCB and the Storage_Error is handled. Other mutexes (such
381 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
382 -- status change of RTS. Therefore raising Storage_Error in the following
383 -- routines should be able to be handled safely.
385 procedure Initialize_Lock
386 (Prio : System.Any_Priority;
387 L : not null access Lock)
390 InitializeCriticalSection (L.Mutex'Access);
391 L.Owner_Priority := 0;
395 procedure Initialize_Lock
396 (L : not null access RTS_Lock; Level : Lock_Level)
398 pragma Unreferenced (Level);
400 InitializeCriticalSection (CRITICAL_SECTION (L.all)'Unrestricted_Access);
407 procedure Finalize_Lock (L : not null access Lock) is
409 DeleteCriticalSection (L.Mutex'Access);
412 procedure Finalize_Lock (L : not null access RTS_Lock) is
414 DeleteCriticalSection (CRITICAL_SECTION (L.all)'Unrestricted_Access);
422 (L : not null access Lock; Ceiling_Violation : out Boolean) is
424 L.Owner_Priority := Get_Priority (Self);
426 if L.Priority < L.Owner_Priority then
427 Ceiling_Violation := True;
431 EnterCriticalSection (L.Mutex'Access);
433 Ceiling_Violation := False;
437 (L : not null access RTS_Lock;
438 Global_Lock : Boolean := False)
441 if not Single_Lock or else Global_Lock then
442 EnterCriticalSection (CRITICAL_SECTION (L.all)'Unrestricted_Access);
446 procedure Write_Lock (T : Task_Id) is
448 if not Single_Lock then
450 (CRITICAL_SECTION (T.Common.LL.L)'Unrestricted_Access);
459 (L : not null access Lock; Ceiling_Violation : out Boolean) is
461 Write_Lock (L, Ceiling_Violation);
468 procedure Unlock (L : not null access Lock) is
470 LeaveCriticalSection (L.Mutex'Access);
474 (L : not null access RTS_Lock; Global_Lock : Boolean := False) is
476 if not Single_Lock or else Global_Lock then
477 LeaveCriticalSection (CRITICAL_SECTION (L.all)'Unrestricted_Access);
481 procedure Unlock (T : Task_Id) is
483 if not Single_Lock then
485 (CRITICAL_SECTION (T.Common.LL.L)'Unrestricted_Access);
493 -- Dynamic priority ceilings are not supported by the underlying system
495 procedure Set_Ceiling
496 (L : not null access Lock;
497 Prio : System.Any_Priority)
499 pragma Unreferenced (L, Prio);
510 Reason : System.Tasking.Task_States)
512 pragma Unreferenced (Reason);
515 pragma Assert (Self_ID = Self);
518 Cond_Wait (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
520 Cond_Wait (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
523 if Self_ID.Deferral_Level = 0
524 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
527 raise Standard'Abort_Signal;
535 -- This is for use within the run-time system, so abort is assumed to be
536 -- already deferred, and the caller should be holding its own ATCB lock.
538 procedure Timed_Sleep
541 Mode : ST.Delay_Modes;
542 Reason : System.Tasking.Task_States;
543 Timedout : out Boolean;
544 Yielded : out Boolean)
546 pragma Unreferenced (Reason);
547 Check_Time : Duration := Monotonic_Clock;
552 pragma Unreferenced (Result);
554 Local_Timedout : Boolean;
560 if Mode = Relative then
562 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
564 Rel_Time := Time - Check_Time;
568 if Rel_Time > 0.0 then
570 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
574 (Self_ID.Common.LL.CV'Access,
575 Single_RTS_Lock'Access,
576 Rel_Time, Local_Timedout, Result);
579 (Self_ID.Common.LL.CV'Access,
580 Self_ID.Common.LL.L'Access,
581 Rel_Time, Local_Timedout, Result);
584 Check_Time := Monotonic_Clock;
585 exit when Abs_Time <= Check_Time;
587 if not Local_Timedout then
589 -- Somebody may have called Wakeup for us
595 Rel_Time := Abs_Time - Check_Time;
604 procedure Timed_Delay
607 Mode : ST.Delay_Modes)
609 Check_Time : Duration := Monotonic_Clock;
615 pragma Unreferenced (Timedout, Result);
622 Write_Lock (Self_ID);
624 if Mode = Relative then
626 Abs_Time := Time + Check_Time;
628 Rel_Time := Time - Check_Time;
632 if Rel_Time > 0.0 then
633 Self_ID.Common.State := Delay_Sleep;
636 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
640 (Self_ID.Common.LL.CV'Access,
641 Single_RTS_Lock'Access,
642 Rel_Time, Timedout, Result);
645 (Self_ID.Common.LL.CV'Access,
646 Self_ID.Common.LL.L'Access,
647 Rel_Time, Timedout, Result);
650 Check_Time := Monotonic_Clock;
651 exit when Abs_Time <= Check_Time;
653 Rel_Time := Abs_Time - Check_Time;
656 Self_ID.Common.State := Runnable;
672 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
673 pragma Unreferenced (Reason);
675 Cond_Signal (T.Common.LL.CV'Access);
682 procedure Yield (Do_Yield : Boolean := True) is
688 -- If running with Annex-D semantics we need a delay
689 -- above 0 milliseconds here otherwise processes give
690 -- enough time to the other tasks to have a chance to
693 -- This makes cxd8002 ACATS pass on Windows.
703 type Prio_Array_Type is array (System.Any_Priority) of Integer;
704 pragma Atomic_Components (Prio_Array_Type);
706 Prio_Array : Prio_Array_Type;
707 -- Global array containing the id of the currently running task for
710 -- Note: we assume that we are on a single processor with run-til-blocked
713 procedure Set_Priority
715 Prio : System.Any_Priority;
716 Loss_Of_Inheritance : Boolean := False)
719 Array_Item : Integer;
722 Res := SetThreadPriority
723 (T.Common.LL.Thread, Interfaces.C.int (Underlying_Priorities (Prio)));
724 pragma Assert (Res = True);
726 if Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F' then
728 -- Annex D requirement [RM D.2.2 par. 9]:
729 -- If the task drops its priority due to the loss of inherited
730 -- priority, it is added at the head of the ready queue for its
731 -- new active priority.
733 if Loss_Of_Inheritance
734 and then Prio < T.Common.Current_Priority
736 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
737 Prio_Array (T.Common.Base_Priority) := Array_Item;
740 -- Let some processes a chance to arrive
744 -- Then wait for our turn to proceed
746 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
747 or else Prio_Array (T.Common.Base_Priority) = 1;
750 Prio_Array (T.Common.Base_Priority) :=
751 Prio_Array (T.Common.Base_Priority) - 1;
755 T.Common.Current_Priority := Prio;
762 function Get_Priority (T : Task_Id) return System.Any_Priority is
764 return T.Common.Current_Priority;
771 -- There were two paths were we needed to call Enter_Task :
772 -- 1) from System.Task_Primitives.Operations.Initialize
773 -- 2) from System.Tasking.Stages.Task_Wrapper
775 -- The thread initialisation has to be done only for the first case
777 -- This is because the GetCurrentThread NT call does not return the real
778 -- thread handler but only a "pseudo" one. It is not possible to release
779 -- the thread handle and free the system ressources from this "pseudo"
780 -- handle. So we really want to keep the real thread handle set in
781 -- System.Task_Primitives.Operations.Create_Task during thread creation.
783 procedure Enter_Task (Self_ID : Task_Id) is
784 procedure Init_Float;
785 pragma Import (C, Init_Float, "__gnat_init_float");
786 -- Properly initializes the FPU for x86 systems
789 Specific.Set (Self_ID);
792 if Self_ID.Common.Task_Info /= null
794 Self_ID.Common.Task_Info.CPU >= CPU_Number (Number_Of_Processors)
796 raise Invalid_CPU_Number;
799 Self_ID.Common.LL.Thread_Id := GetCurrentThreadId;
803 for J in Known_Tasks'Range loop
804 if Known_Tasks (J) = null then
805 Known_Tasks (J) := Self_ID;
806 Self_ID.Known_Tasks_Index := J;
818 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
820 return new Ada_Task_Control_Block (Entry_Num);
827 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
829 -----------------------------
830 -- Register_Foreign_Thread --
831 -----------------------------
833 function Register_Foreign_Thread return Task_Id is
835 if Is_Valid_Task then
838 return Register_Foreign_Thread (GetCurrentThread);
840 end Register_Foreign_Thread;
846 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
848 -- Initialize thread ID to 0, this is needed to detect threads that
849 -- are not yet activated.
851 Self_ID.Common.LL.Thread := 0;
853 Initialize_Cond (Self_ID.Common.LL.CV'Access);
855 if not Single_Lock then
856 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
866 procedure Create_Task
868 Wrapper : System.Address;
869 Stack_Size : System.Parameters.Size_Type;
870 Priority : System.Any_Priority;
871 Succeeded : out Boolean)
873 Initial_Stack_Size : constant := 1024;
874 -- We set the initial stack size to 1024. On Windows version prior to XP
875 -- there is no way to fix a task stack size. Only the initial stack size
876 -- can be set, the operating system will raise the task stack size if
879 function Is_Windows_XP return Integer;
880 pragma Import (C, Is_Windows_XP, "__gnat_is_windows_xp");
881 -- Returns 1 if running on Windows XP
884 TaskId : aliased DWORD;
885 pTaskParameter : System.OS_Interface.PVOID;
887 Entry_Point : PTHREAD_START_ROUTINE;
890 pTaskParameter := To_Address (T);
892 Entry_Point := To_PTHREAD_START_ROUTINE (Wrapper);
894 if Is_Windows_XP = 1 then
895 hTask := CreateThread
900 DWORD (Create_Suspended) or
901 DWORD (Stack_Size_Param_Is_A_Reservation),
902 TaskId'Unchecked_Access);
904 hTask := CreateThread
909 DWORD (Create_Suspended),
910 TaskId'Unchecked_Access);
913 -- Step 1: Create the thread in blocked mode
919 -- Step 2: set its TCB
921 T.Common.LL.Thread := hTask;
923 -- Step 3: set its priority (child has inherited priority from parent)
925 Set_Priority (T, Priority);
927 if Time_Slice_Val = 0
928 or else Dispatching_Policy = 'F'
929 or else Get_Policy (Priority) = 'F'
931 -- Here we need Annex D semantics so we disable the NT priority
932 -- boost. A priority boost is temporarily given by the system to a
933 -- thread when it is taken out of a wait state.
935 SetThreadPriorityBoost (hTask, DisablePriorityBoost => True);
938 -- Step 4: Handle Task_Info
940 if T.Common.Task_Info /= null then
941 if T.Common.Task_Info.CPU /= Task_Info.Any_CPU then
942 Result := SetThreadIdealProcessor (hTask, T.Common.Task_Info.CPU);
943 pragma Assert (Result = 1);
947 -- Step 5: Now, start it for good:
949 Result := ResumeThread (hTask);
950 pragma Assert (Result = 1);
952 Succeeded := Result = 1;
959 procedure Finalize_TCB (T : Task_Id) is
960 Self_ID : Task_Id := T;
963 Is_Self : constant Boolean := T = Self;
965 procedure Free is new
966 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
969 if not Single_Lock then
970 Finalize_Lock (T.Common.LL.L'Access);
973 Finalize_Cond (T.Common.LL.CV'Access);
975 if T.Known_Tasks_Index /= -1 then
976 Known_Tasks (T.Known_Tasks_Index) := null;
979 if Self_ID.Common.LL.Thread /= 0 then
981 -- This task has been activated. Wait for the thread to terminate
982 -- then close it. this is needed to release system ressources.
984 Result := WaitForSingleObject (T.Common.LL.Thread, Wait_Infinite);
985 pragma Assert (Result /= WAIT_FAILED);
986 Succeeded := CloseHandle (T.Common.LL.Thread);
987 pragma Assert (Succeeded = True);
1001 procedure Exit_Task is
1003 Specific.Set (null);
1010 procedure Abort_Task (T : Task_Id) is
1011 pragma Unreferenced (T);
1016 ----------------------
1017 -- Environment_Task --
1018 ----------------------
1020 function Environment_Task return Task_Id is
1022 return Environment_Task_Id;
1023 end Environment_Task;
1029 procedure Lock_RTS is
1031 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1038 procedure Unlock_RTS is
1040 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1047 procedure Initialize (Environment_Task : Task_Id) is
1049 pragma Unreferenced (Discard);
1052 Environment_Task_Id := Environment_Task;
1053 OS_Primitives.Initialize;
1054 Interrupt_Management.Initialize;
1056 if Time_Slice_Val = 0 or else Dispatching_Policy = 'F' then
1057 -- Here we need Annex D semantics, switch the current process to the
1058 -- Realtime_Priority_Class.
1060 Discard := OS_Interface.SetPriorityClass
1061 (GetCurrentProcess, Realtime_Priority_Class);
1066 TlsIndex := TlsAlloc;
1068 -- Initialize the lock used to synchronize chain of all ATCBs
1070 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1072 Environment_Task.Common.LL.Thread := GetCurrentThread;
1073 Enter_Task (Environment_Task);
1076 ---------------------
1077 -- Monotonic_Clock --
1078 ---------------------
1080 function Monotonic_Clock return Duration
1081 renames System.OS_Primitives.Monotonic_Clock;
1087 function RT_Resolution return Duration is
1089 return 0.000_001; -- 1 micro-second
1096 procedure Initialize (S : in out Suspension_Object) is
1098 -- Initialize internal state. It is always initialized to False (ARM
1104 -- Initialize internal mutex
1106 InitializeCriticalSection (S.L'Access);
1108 -- Initialize internal condition variable
1110 S.CV := CreateEvent (null, True, False, Null_Ptr);
1111 pragma Assert (S.CV /= 0);
1118 procedure Finalize (S : in out Suspension_Object) is
1121 -- Destroy internal mutex
1123 DeleteCriticalSection (S.L'Access);
1125 -- Destroy internal condition variable
1127 Result := CloseHandle (S.CV);
1128 pragma Assert (Result = True);
1135 function Current_State (S : Suspension_Object) return Boolean is
1137 -- We do not want to use lock on this read operation. State is marked
1138 -- as Atomic so that we ensure that the value retrieved is correct.
1147 procedure Set_False (S : in out Suspension_Object) is
1149 SSL.Abort_Defer.all;
1151 EnterCriticalSection (S.L'Access);
1155 LeaveCriticalSection (S.L'Access);
1157 SSL.Abort_Undefer.all;
1164 procedure Set_True (S : in out Suspension_Object) is
1167 SSL.Abort_Defer.all;
1169 EnterCriticalSection (S.L'Access);
1171 -- If there is already a task waiting on this suspension object then
1172 -- we resume it, leaving the state of the suspension object to False,
1173 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1174 -- the state to True.
1180 Result := SetEvent (S.CV);
1181 pragma Assert (Result = True);
1186 LeaveCriticalSection (S.L'Access);
1188 SSL.Abort_Undefer.all;
1191 ------------------------
1192 -- Suspend_Until_True --
1193 ------------------------
1195 procedure Suspend_Until_True (S : in out Suspension_Object) is
1199 SSL.Abort_Defer.all;
1201 EnterCriticalSection (S.L'Access);
1204 -- Program_Error must be raised upon calling Suspend_Until_True
1205 -- if another task is already waiting on that suspension object
1206 -- (ARM D.10 par. 10).
1208 LeaveCriticalSection (S.L'Access);
1210 SSL.Abort_Undefer.all;
1212 raise Program_Error;
1214 -- Suspend the task if the state is False. Otherwise, the task
1215 -- continues its execution, and the state of the suspension object
1216 -- is set to False (ARM D.10 par. 9).
1221 LeaveCriticalSection (S.L'Access);
1223 SSL.Abort_Undefer.all;
1227 -- Must reset CV BEFORE L is unlocked
1229 Result_Bool := ResetEvent (S.CV);
1230 pragma Assert (Result_Bool = True);
1232 LeaveCriticalSection (S.L'Access);
1234 SSL.Abort_Undefer.all;
1236 Result := WaitForSingleObject (S.CV, Wait_Infinite);
1237 pragma Assert (Result = 0);
1240 end Suspend_Until_True;
1246 -- Dummy versions. The only currently working versions is for solaris
1249 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1250 pragma Unreferenced (Self_ID);
1255 --------------------
1256 -- Check_No_Locks --
1257 --------------------
1259 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1260 pragma Unreferenced (Self_ID);
1269 function Suspend_Task
1271 Thread_Self : Thread_Id) return Boolean
1274 if T.Common.LL.Thread /= Thread_Self then
1275 return SuspendThread (T.Common.LL.Thread) = NO_ERROR;
1285 function Resume_Task
1287 Thread_Self : Thread_Id) return Boolean
1290 if T.Common.LL.Thread /= Thread_Self then
1291 return ResumeThread (T.Common.LL.Thread) = NO_ERROR;
1297 --------------------
1298 -- Stop_All_Tasks --
1299 --------------------
1301 procedure Stop_All_Tasks is
1310 function Stop_Task (T : ST.Task_Id) return Boolean is
1311 pragma Unreferenced (T);
1320 function Continue_Task (T : ST.Task_Id) return Boolean is
1321 pragma Unreferenced (T);
1326 end System.Task_Primitives.Operations;