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 Local_Timedout : Boolean;
558 if Mode = Relative then
560 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
562 Rel_Time := Time - Check_Time;
566 if Rel_Time > 0.0 then
568 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
572 (Self_ID.Common.LL.CV'Access,
573 Single_RTS_Lock'Access,
574 Rel_Time, Local_Timedout, Result);
577 (Self_ID.Common.LL.CV'Access,
578 Self_ID.Common.LL.L'Access,
579 Rel_Time, Local_Timedout, Result);
582 Check_Time := Monotonic_Clock;
583 exit when Abs_Time <= Check_Time;
585 if not Local_Timedout then
587 -- Somebody may have called Wakeup for us
593 Rel_Time := Abs_Time - Check_Time;
602 procedure Timed_Delay
605 Mode : ST.Delay_Modes)
607 Check_Time : Duration := Monotonic_Clock;
613 pragma Warnings (Off, Integer);
620 Write_Lock (Self_ID);
622 if Mode = Relative then
624 Abs_Time := Time + Check_Time;
626 Rel_Time := Time - Check_Time;
630 if Rel_Time > 0.0 then
631 Self_ID.Common.State := Delay_Sleep;
634 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
638 (Self_ID.Common.LL.CV'Access,
639 Single_RTS_Lock'Access,
640 Rel_Time, Timedout, Result);
643 (Self_ID.Common.LL.CV'Access,
644 Self_ID.Common.LL.L'Access,
645 Rel_Time, Timedout, Result);
648 Check_Time := Monotonic_Clock;
649 exit when Abs_Time <= Check_Time;
651 Rel_Time := Abs_Time - Check_Time;
654 Self_ID.Common.State := Runnable;
670 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
671 pragma Unreferenced (Reason);
673 Cond_Signal (T.Common.LL.CV'Access);
680 procedure Yield (Do_Yield : Boolean := True) is
686 -- If running with Annex-D semantics we need a delay
687 -- above 0 milliseconds here otherwise processes give
688 -- enough time to the other tasks to have a chance to
691 -- This makes cxd8002 ACATS pass on Windows.
701 type Prio_Array_Type is array (System.Any_Priority) of Integer;
702 pragma Atomic_Components (Prio_Array_Type);
704 Prio_Array : Prio_Array_Type;
705 -- Global array containing the id of the currently running task for
708 -- Note: we assume that we are on a single processor with run-til-blocked
711 procedure Set_Priority
713 Prio : System.Any_Priority;
714 Loss_Of_Inheritance : Boolean := False)
717 Array_Item : Integer;
720 Res := SetThreadPriority
721 (T.Common.LL.Thread, Interfaces.C.int (Underlying_Priorities (Prio)));
722 pragma Assert (Res = True);
724 if Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F' then
726 -- Annex D requirement [RM D.2.2 par. 9]:
727 -- If the task drops its priority due to the loss of inherited
728 -- priority, it is added at the head of the ready queue for its
729 -- new active priority.
731 if Loss_Of_Inheritance
732 and then Prio < T.Common.Current_Priority
734 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
735 Prio_Array (T.Common.Base_Priority) := Array_Item;
738 -- Let some processes a chance to arrive
742 -- Then wait for our turn to proceed
744 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
745 or else Prio_Array (T.Common.Base_Priority) = 1;
748 Prio_Array (T.Common.Base_Priority) :=
749 Prio_Array (T.Common.Base_Priority) - 1;
753 T.Common.Current_Priority := Prio;
760 function Get_Priority (T : Task_Id) return System.Any_Priority is
762 return T.Common.Current_Priority;
769 -- There were two paths were we needed to call Enter_Task :
770 -- 1) from System.Task_Primitives.Operations.Initialize
771 -- 2) from System.Tasking.Stages.Task_Wrapper
773 -- The thread initialisation has to be done only for the first case
775 -- This is because the GetCurrentThread NT call does not return the real
776 -- thread handler but only a "pseudo" one. It is not possible to release
777 -- the thread handle and free the system ressources from this "pseudo"
778 -- handle. So we really want to keep the real thread handle set in
779 -- System.Task_Primitives.Operations.Create_Task during thread creation.
781 procedure Enter_Task (Self_ID : Task_Id) is
782 procedure Init_Float;
783 pragma Import (C, Init_Float, "__gnat_init_float");
784 -- Properly initializes the FPU for x86 systems
787 Specific.Set (Self_ID);
790 if Self_ID.Common.Task_Info /= null
792 Self_ID.Common.Task_Info.CPU >= CPU_Number (Number_Of_Processors)
794 raise Invalid_CPU_Number;
797 Self_ID.Common.LL.Thread_Id := GetCurrentThreadId;
801 for J in Known_Tasks'Range loop
802 if Known_Tasks (J) = null then
803 Known_Tasks (J) := Self_ID;
804 Self_ID.Known_Tasks_Index := J;
816 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
818 return new Ada_Task_Control_Block (Entry_Num);
825 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
827 -----------------------------
828 -- Register_Foreign_Thread --
829 -----------------------------
831 function Register_Foreign_Thread return Task_Id is
833 if Is_Valid_Task then
836 return Register_Foreign_Thread (GetCurrentThread);
838 end Register_Foreign_Thread;
844 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
846 -- Initialize thread ID to 0, this is needed to detect threads that
847 -- are not yet activated.
849 Self_ID.Common.LL.Thread := 0;
851 Initialize_Cond (Self_ID.Common.LL.CV'Access);
853 if not Single_Lock then
854 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
864 procedure Create_Task
866 Wrapper : System.Address;
867 Stack_Size : System.Parameters.Size_Type;
868 Priority : System.Any_Priority;
869 Succeeded : out Boolean)
871 Initial_Stack_Size : constant := 1024;
872 -- We set the initial stack size to 1024. On Windows version prior to XP
873 -- there is no way to fix a task stack size. Only the initial stack size
874 -- can be set, the operating system will raise the task stack size if
877 function Is_Windows_XP return Integer;
878 pragma Import (C, Is_Windows_XP, "__gnat_is_windows_xp");
879 -- Returns 1 if running on Windows XP
882 TaskId : aliased DWORD;
883 pTaskParameter : System.OS_Interface.PVOID;
885 Entry_Point : PTHREAD_START_ROUTINE;
888 pTaskParameter := To_Address (T);
890 Entry_Point := To_PTHREAD_START_ROUTINE (Wrapper);
892 if Is_Windows_XP = 1 then
893 hTask := CreateThread
898 DWORD (Create_Suspended) or
899 DWORD (Stack_Size_Param_Is_A_Reservation),
900 TaskId'Unchecked_Access);
902 hTask := CreateThread
907 DWORD (Create_Suspended),
908 TaskId'Unchecked_Access);
911 -- Step 1: Create the thread in blocked mode
917 -- Step 2: set its TCB
919 T.Common.LL.Thread := hTask;
921 -- Step 3: set its priority (child has inherited priority from parent)
923 Set_Priority (T, Priority);
925 if Time_Slice_Val = 0
926 or else Dispatching_Policy = 'F'
927 or else Get_Policy (Priority) = 'F'
929 -- Here we need Annex D semantics so we disable the NT priority
930 -- boost. A priority boost is temporarily given by the system to a
931 -- thread when it is taken out of a wait state.
933 SetThreadPriorityBoost (hTask, DisablePriorityBoost => True);
936 -- Step 4: Handle Task_Info
938 if T.Common.Task_Info /= null then
939 if T.Common.Task_Info.CPU /= Task_Info.Any_CPU then
940 Result := SetThreadIdealProcessor (hTask, T.Common.Task_Info.CPU);
941 pragma Assert (Result = 1);
945 -- Step 5: Now, start it for good:
947 Result := ResumeThread (hTask);
948 pragma Assert (Result = 1);
950 Succeeded := Result = 1;
957 procedure Finalize_TCB (T : Task_Id) is
958 Self_ID : Task_Id := T;
961 Is_Self : constant Boolean := T = Self;
963 procedure Free is new
964 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
967 if not Single_Lock then
968 Finalize_Lock (T.Common.LL.L'Access);
971 Finalize_Cond (T.Common.LL.CV'Access);
973 if T.Known_Tasks_Index /= -1 then
974 Known_Tasks (T.Known_Tasks_Index) := null;
977 if Self_ID.Common.LL.Thread /= 0 then
979 -- This task has been activated. Wait for the thread to terminate
980 -- then close it. this is needed to release system ressources.
982 Result := WaitForSingleObject (T.Common.LL.Thread, Wait_Infinite);
983 pragma Assert (Result /= WAIT_FAILED);
984 Succeeded := CloseHandle (T.Common.LL.Thread);
985 pragma Assert (Succeeded = True);
999 procedure Exit_Task is
1001 Specific.Set (null);
1008 procedure Abort_Task (T : Task_Id) is
1009 pragma Unreferenced (T);
1014 ----------------------
1015 -- Environment_Task --
1016 ----------------------
1018 function Environment_Task return Task_Id is
1020 return Environment_Task_Id;
1021 end Environment_Task;
1027 procedure Lock_RTS is
1029 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1036 procedure Unlock_RTS is
1038 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1045 procedure Initialize (Environment_Task : Task_Id) is
1047 pragma Unreferenced (Discard);
1050 Environment_Task_Id := Environment_Task;
1051 OS_Primitives.Initialize;
1052 Interrupt_Management.Initialize;
1054 if Time_Slice_Val = 0 or else Dispatching_Policy = 'F' then
1055 -- Here we need Annex D semantics, switch the current process to the
1056 -- Realtime_Priority_Class.
1058 Discard := OS_Interface.SetPriorityClass
1059 (GetCurrentProcess, Realtime_Priority_Class);
1064 TlsIndex := TlsAlloc;
1066 -- Initialize the lock used to synchronize chain of all ATCBs
1068 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1070 Environment_Task.Common.LL.Thread := GetCurrentThread;
1071 Enter_Task (Environment_Task);
1074 ---------------------
1075 -- Monotonic_Clock --
1076 ---------------------
1078 function Monotonic_Clock return Duration
1079 renames System.OS_Primitives.Monotonic_Clock;
1085 function RT_Resolution return Duration is
1087 return 0.000_001; -- 1 micro-second
1094 procedure Initialize (S : in out Suspension_Object) is
1096 -- Initialize internal state. It is always initialized to False (ARM
1102 -- Initialize internal mutex
1104 InitializeCriticalSection (S.L'Access);
1106 -- Initialize internal condition variable
1108 S.CV := CreateEvent (null, True, False, Null_Ptr);
1109 pragma Assert (S.CV /= 0);
1116 procedure Finalize (S : in out Suspension_Object) is
1119 -- Destroy internal mutex
1121 DeleteCriticalSection (S.L'Access);
1123 -- Destroy internal condition variable
1125 Result := CloseHandle (S.CV);
1126 pragma Assert (Result = True);
1133 function Current_State (S : Suspension_Object) return Boolean is
1135 -- We do not want to use lock on this read operation. State is marked
1136 -- as Atomic so that we ensure that the value retrieved is correct.
1145 procedure Set_False (S : in out Suspension_Object) is
1147 SSL.Abort_Defer.all;
1149 EnterCriticalSection (S.L'Access);
1153 LeaveCriticalSection (S.L'Access);
1155 SSL.Abort_Undefer.all;
1162 procedure Set_True (S : in out Suspension_Object) is
1165 SSL.Abort_Defer.all;
1167 EnterCriticalSection (S.L'Access);
1169 -- If there is already a task waiting on this suspension object then
1170 -- we resume it, leaving the state of the suspension object to False,
1171 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1172 -- the state to True.
1178 Result := SetEvent (S.CV);
1179 pragma Assert (Result = True);
1184 LeaveCriticalSection (S.L'Access);
1186 SSL.Abort_Undefer.all;
1189 ------------------------
1190 -- Suspend_Until_True --
1191 ------------------------
1193 procedure Suspend_Until_True (S : in out Suspension_Object) is
1197 SSL.Abort_Defer.all;
1199 EnterCriticalSection (S.L'Access);
1202 -- Program_Error must be raised upon calling Suspend_Until_True
1203 -- if another task is already waiting on that suspension object
1204 -- (ARM D.10 par. 10).
1206 LeaveCriticalSection (S.L'Access);
1208 SSL.Abort_Undefer.all;
1210 raise Program_Error;
1212 -- Suspend the task if the state is False. Otherwise, the task
1213 -- continues its execution, and the state of the suspension object
1214 -- is set to False (ARM D.10 par. 9).
1219 LeaveCriticalSection (S.L'Access);
1221 SSL.Abort_Undefer.all;
1225 -- Must reset CV BEFORE L is unlocked
1227 Result_Bool := ResetEvent (S.CV);
1228 pragma Assert (Result_Bool = True);
1230 LeaveCriticalSection (S.L'Access);
1232 SSL.Abort_Undefer.all;
1234 Result := WaitForSingleObject (S.CV, Wait_Infinite);
1235 pragma Assert (Result = 0);
1238 end Suspend_Until_True;
1244 -- Dummy versions. The only currently working versions is for solaris
1247 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1248 pragma Unreferenced (Self_ID);
1253 --------------------
1254 -- Check_No_Locks --
1255 --------------------
1257 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1258 pragma Unreferenced (Self_ID);
1267 function Suspend_Task
1269 Thread_Self : Thread_Id) return Boolean
1272 if T.Common.LL.Thread /= Thread_Self then
1273 return SuspendThread (T.Common.LL.Thread) = NO_ERROR;
1283 function Resume_Task
1285 Thread_Self : Thread_Id) return Boolean
1288 if T.Common.LL.Thread /= Thread_Self then
1289 return ResumeThread (T.Common.LL.Thread) = NO_ERROR;
1295 --------------------
1296 -- Stop_All_Tasks --
1297 --------------------
1299 procedure Stop_All_Tasks is
1308 function Continue_Task (T : ST.Task_Id) return Boolean is
1309 pragma Unreferenced (T);
1314 end System.Task_Primitives.Operations;