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-2018, 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
164 function Is_Valid_Task
return Boolean is
166 return TlsGetValue
(TlsIndex
) /= System
.Null_Address
;
173 procedure Set
(Self_Id
: Task_Id
) is
176 Succeeded
:= TlsSetValue
(TlsIndex
, To_Address
(Self_Id
));
177 pragma Assert
(Succeeded
= Win32
.TRUE);
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
195 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
196 -- Allocate and initialize a new ATCB for the current Thread. The size of
197 -- the secondary stack can be optionally specified.
199 function Register_Foreign_Thread
201 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
202 return Task_Id
is separate;
204 ----------------------------------
205 -- Condition Variable Functions --
206 ----------------------------------
208 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
);
209 -- Initialize given condition variable Cond
211 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
);
212 -- Finalize given condition variable Cond
214 procedure Cond_Signal
(Cond
: not null access Condition_Variable
);
215 -- Signal condition variable Cond
218 (Cond
: not null access Condition_Variable
;
219 L
: not null access RTS_Lock
);
220 -- Wait on conditional variable Cond, using lock L
222 procedure Cond_Timed_Wait
223 (Cond
: not null access Condition_Variable
;
224 L
: not null access RTS_Lock
;
226 Timed_Out
: out Boolean;
227 Status
: out Integer);
228 -- Do timed wait on condition variable Cond using lock L. The duration
229 -- of the timed wait is given by Rel_Time. When the condition is
230 -- signalled, Timed_Out shows whether or not a time out occurred.
231 -- Status is only valid if Timed_Out is False, in which case it
232 -- shows whether Cond_Timed_Wait completed successfully.
234 ---------------------
235 -- Initialize_Cond --
236 ---------------------
238 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
) is
241 hEvent
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
242 pragma Assert
(hEvent
/= 0);
243 Cond
.all := Condition_Variable
(hEvent
);
250 -- No such problem here, DosCloseEventSem has been derived.
251 -- What does such refer to in above comment???
253 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
) is
256 Result
:= CloseHandle
(HANDLE
(Cond
.all));
257 pragma Assert
(Result
= Win32
.TRUE);
264 procedure Cond_Signal
(Cond
: not null access Condition_Variable
) is
267 Result
:= SetEvent
(HANDLE
(Cond
.all));
268 pragma Assert
(Result
= Win32
.TRUE);
275 -- Pre-condition: Cond is posted
278 -- Post-condition: Cond is posted
282 (Cond
: not null access Condition_Variable
;
283 L
: not null access RTS_Lock
)
289 -- Must reset Cond BEFORE L is unlocked
291 Result_Bool
:= ResetEvent
(HANDLE
(Cond
.all));
292 pragma Assert
(Result_Bool
= Win32
.TRUE);
293 Unlock
(L
, Global_Lock
=> True);
295 -- No problem if we are interrupted here: if the condition is signaled,
296 -- WaitForSingleObject will simply not block
298 Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Wait_Infinite
);
299 pragma Assert
(Result
= 0);
301 Write_Lock
(L
, Global_Lock
=> True);
304 ---------------------
305 -- Cond_Timed_Wait --
306 ---------------------
308 -- Pre-condition: Cond is posted
311 -- Post-condition: Cond is posted
314 procedure Cond_Timed_Wait
315 (Cond
: not null access Condition_Variable
;
316 L
: not null access RTS_Lock
;
318 Timed_Out
: out Boolean;
319 Status
: out Integer)
321 Time_Out_Max
: constant DWORD
:= 16#FFFF0000#
;
322 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
329 -- Must reset Cond BEFORE L is unlocked
331 Result
:= ResetEvent
(HANDLE
(Cond
.all));
332 pragma Assert
(Result
= Win32
.TRUE);
333 Unlock
(L
, Global_Lock
=> True);
335 -- No problem if we are interrupted here: if the condition is signaled,
336 -- WaitForSingleObject will simply not block.
338 if Rel_Time
<= 0.0 then
344 (if Rel_Time
>= Duration (Time_Out_Max
) / 1000
346 else DWORD
(Rel_Time
* 1000));
348 Wait_Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Time_Out
);
350 if Wait_Result
= WAIT_TIMEOUT
then
358 Write_Lock
(L
, Global_Lock
=> True);
360 -- Ensure post-condition
363 Result
:= SetEvent
(HANDLE
(Cond
.all));
364 pragma Assert
(Result
= Win32
.TRUE);
367 Status
:= Integer (Wait_Result
);
374 -- The underlying thread system sets a guard page at the bottom of a thread
375 -- stack, so nothing is needed.
376 -- ??? Check the comment above
378 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
379 pragma Unreferenced
(T
, On
);
388 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
390 return T
.Common
.LL
.Thread
;
397 function Self
return Task_Id
is
398 Self_Id
: constant Task_Id
:= To_Task_Id
(TlsGetValue
(TlsIndex
));
400 if Self_Id
= null then
401 return Register_Foreign_Thread
(GetCurrentThread
);
407 ---------------------
408 -- Initialize_Lock --
409 ---------------------
411 -- Note: mutexes and cond_variables needed per-task basis are initialized
412 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
413 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
414 -- status change of RTS. Therefore raising Storage_Error in the following
415 -- routines should be able to be handled safely.
417 procedure Initialize_Lock
418 (Prio
: System
.Any_Priority
;
419 L
: not null access Lock
)
422 InitializeCriticalSection
(L
.Mutex
'Access);
423 L
.Owner_Priority
:= 0;
427 procedure Initialize_Lock
428 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
430 pragma Unreferenced
(Level
);
432 InitializeCriticalSection
(L
);
439 procedure Finalize_Lock
(L
: not null access Lock
) is
441 DeleteCriticalSection
(L
.Mutex
'Access);
444 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
446 DeleteCriticalSection
(L
);
454 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
456 L
.Owner_Priority
:= Get_Priority
(Self
);
458 if L
.Priority
< L
.Owner_Priority
then
459 Ceiling_Violation
:= True;
463 EnterCriticalSection
(L
.Mutex
'Access);
465 Ceiling_Violation
:= False;
469 (L
: not null access RTS_Lock
;
470 Global_Lock
: Boolean := False)
473 if not Single_Lock
or else Global_Lock
then
474 EnterCriticalSection
(L
);
478 procedure Write_Lock
(T
: Task_Id
) is
480 if not Single_Lock
then
481 EnterCriticalSection
(T
.Common
.LL
.L
'Access);
490 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
492 Write_Lock
(L
, Ceiling_Violation
);
499 procedure Unlock
(L
: not null access Lock
) is
501 LeaveCriticalSection
(L
.Mutex
'Access);
505 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False) is
507 if not Single_Lock
or else Global_Lock
then
508 LeaveCriticalSection
(L
);
512 procedure Unlock
(T
: Task_Id
) is
514 if not Single_Lock
then
515 LeaveCriticalSection
(T
.Common
.LL
.L
'Access);
523 -- Dynamic priority ceilings are not supported by the underlying system
525 procedure Set_Ceiling
526 (L
: not null access Lock
;
527 Prio
: System
.Any_Priority
)
529 pragma Unreferenced
(L
, Prio
);
540 Reason
: System
.Tasking
.Task_States
)
542 pragma Unreferenced
(Reason
);
545 pragma Assert
(Self_ID
= Self
);
548 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
550 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
553 if Self_ID
.Deferral_Level
= 0
554 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
557 raise Standard
'Abort_Signal;
565 -- This is for use within the run-time system, so abort is assumed to be
566 -- already deferred, and the caller should be holding its own ATCB lock.
568 procedure Timed_Sleep
571 Mode
: ST
.Delay_Modes
;
572 Reason
: System
.Tasking
.Task_States
;
573 Timedout
: out Boolean;
574 Yielded
: out Boolean)
576 pragma Unreferenced
(Reason
);
577 Check_Time
: Duration := Monotonic_Clock
;
582 pragma Unreferenced
(Result
);
584 Local_Timedout
: Boolean;
590 if Mode
= Relative
then
592 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
594 Rel_Time
:= Time
- Check_Time
;
598 if Rel_Time
> 0.0 then
600 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
604 (Self_ID
.Common
.LL
.CV
'Access,
605 Single_RTS_Lock
'Access,
606 Rel_Time
, Local_Timedout
, Result
);
609 (Self_ID
.Common
.LL
.CV
'Access,
610 Self_ID
.Common
.LL
.L
'Access,
611 Rel_Time
, Local_Timedout
, Result
);
614 Check_Time
:= Monotonic_Clock
;
615 exit when Abs_Time
<= Check_Time
;
617 if not Local_Timedout
then
619 -- Somebody may have called Wakeup for us
625 Rel_Time
:= Abs_Time
- Check_Time
;
634 procedure Timed_Delay
637 Mode
: ST
.Delay_Modes
)
639 Check_Time
: Duration := Monotonic_Clock
;
645 pragma Unreferenced
(Timedout
, Result
);
652 Write_Lock
(Self_ID
);
654 if Mode
= Relative
then
656 Abs_Time
:= Time
+ Check_Time
;
658 Rel_Time
:= Time
- Check_Time
;
662 if Rel_Time
> 0.0 then
663 Self_ID
.Common
.State
:= Delay_Sleep
;
666 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
670 (Self_ID
.Common
.LL
.CV
'Access,
671 Single_RTS_Lock
'Access,
672 Rel_Time
, Timedout
, Result
);
675 (Self_ID
.Common
.LL
.CV
'Access,
676 Self_ID
.Common
.LL
.L
'Access,
677 Rel_Time
, Timedout
, Result
);
680 Check_Time
:= Monotonic_Clock
;
681 exit when Abs_Time
<= Check_Time
;
683 Rel_Time
:= Abs_Time
- Check_Time
;
686 Self_ID
.Common
.State
:= Runnable
;
702 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
703 pragma Unreferenced
(Reason
);
705 Cond_Signal
(T
.Common
.LL
.CV
'Access);
712 procedure Yield
(Do_Yield
: Boolean := True) is
714 -- Note: in a previous implementation if Do_Yield was False, then we
715 -- introduced a delay of 1 millisecond in an attempt to get closer to
716 -- annex D semantics, and in particular to make ACATS CXD8002 pass. But
717 -- this change introduced a huge performance regression evaluating the
718 -- Count attribute. So we decided to remove this processing.
720 -- Moreover, CXD8002 appears to pass on Windows (although we do not
721 -- guarantee full Annex D compliance on Windows in any case).
732 procedure Set_Priority
734 Prio
: System
.Any_Priority
;
735 Loss_Of_Inheritance
: Boolean := False)
738 pragma Unreferenced
(Loss_Of_Inheritance
);
744 Interfaces
.C
.int
(Underlying_Priorities
(Prio
)));
745 pragma Assert
(Res
= Win32
.TRUE);
747 -- Note: Annex D (RM D.2.3(5/2)) requires the task to be placed at the
748 -- head of its priority queue when decreasing its priority as a result
749 -- of a loss of inherited priority. This is not the case, but we
750 -- consider it an acceptable variation (RM 1.1.3(6)), given this is
751 -- the built-in behavior offered by the Windows operating system.
753 -- In older versions we attempted to better approximate the Annex D
754 -- required behavior, but this simulation was not entirely accurate,
755 -- and it seems better to live with the standard Windows semantics.
757 T
.Common
.Current_Priority
:= Prio
;
764 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
766 return T
.Common
.Current_Priority
;
773 -- There were two paths were we needed to call Enter_Task :
774 -- 1) from System.Task_Primitives.Operations.Initialize
775 -- 2) from System.Tasking.Stages.Task_Wrapper
777 -- The pseudo handle (LL.Thread) need not be closed when it is no
778 -- longer needed. Calling the CloseHandle function with this handle
781 procedure Enter_Task
(Self_ID
: Task_Id
) is
782 procedure Get_Stack_Bounds
(Base
: Address
; Limit
: Address
);
783 pragma Import
(C
, Get_Stack_Bounds
, "__gnat_get_stack_bounds");
784 -- Get stack boundaries
786 Specific
.Set
(Self_ID
);
788 -- Properly initializes the FPU for x86 systems
790 System
.Float_Control
.Reset
;
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 -- Initialize the thread here only if not set. This is done for a
800 -- foreign task but is not needed when a real thread-id is already
801 -- set in Create_Task. Note that we do want to keep the real thread-id
802 -- as it is the only way to free the associated resource. Another way
803 -- to say this is that a pseudo thread-id from a foreign thread won't
804 -- allow for freeing resources.
806 if Self_ID
.Common
.LL
.Thread
= Null_Thread_Id
then
807 Self_ID
.Common
.LL
.Thread
:= GetCurrentThread
;
810 Self_ID
.Common
.LL
.Thread_Id
:= GetCurrentThreadId
;
813 (Self_ID
.Common
.Compiler_Data
.Pri_Stack_Info
.Base
'Address,
814 Self_ID
.Common
.Compiler_Data
.Pri_Stack_Info
.Limit
'Address);
821 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
823 -----------------------------
824 -- Register_Foreign_Thread --
825 -----------------------------
827 function Register_Foreign_Thread
return Task_Id
is
829 if Is_Valid_Task
then
832 return Register_Foreign_Thread
(GetCurrentThread
);
834 end Register_Foreign_Thread
;
840 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
842 -- Initialize thread ID to 0, this is needed to detect threads that
843 -- are not yet activated.
845 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
847 Initialize_Cond
(Self_ID
.Common
.LL
.CV
'Access);
849 if not Single_Lock
then
850 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
860 procedure Create_Task
862 Wrapper
: System
.Address
;
863 Stack_Size
: System
.Parameters
.Size_Type
;
864 Priority
: System
.Any_Priority
;
865 Succeeded
: out Boolean)
867 Initial_Stack_Size
: constant := 1024;
868 -- We set the initial stack size to 1024. On Windows version prior to XP
869 -- there is no way to fix a task stack size. Only the initial stack size
870 -- can be set, the operating system will raise the task stack size if
873 function Is_Windows_XP
return Integer;
874 pragma Import
(C
, Is_Windows_XP
, "__gnat_is_windows_xp");
875 -- Returns 1 if running on Windows XP
878 TaskId
: aliased DWORD
;
879 pTaskParameter
: Win32
.PVOID
;
881 Entry_Point
: PTHREAD_START_ROUTINE
;
883 use type System
.Multiprocessors
.CPU_Range
;
886 -- Check whether both Dispatching_Domain and CPU are specified for the
887 -- task, and the CPU value is not contained within the range of
888 -- processors for the domain.
890 if T
.Common
.Domain
/= null
891 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
893 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
894 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
900 pTaskParameter
:= To_Address
(T
);
902 Entry_Point
:= To_PTHREAD_START_ROUTINE
(Wrapper
);
904 if Is_Windows_XP
= 1 then
905 hTask
:= CreateThread
910 DWORD
(Create_Suspended
)
911 or DWORD
(Stack_Size_Param_Is_A_Reservation
),
912 TaskId
'Unchecked_Access);
914 hTask
:= CreateThread
919 DWORD
(Create_Suspended
),
920 TaskId
'Unchecked_Access);
923 -- Step 1: Create the thread in blocked mode
930 -- Step 2: set its TCB
932 T
.Common
.LL
.Thread
:= hTask
;
934 -- Note: it would be useful to initialize Thread_Id right away to avoid
935 -- a race condition in gdb where Thread_ID may not have the right value
936 -- yet, but GetThreadId is a Vista specific API, not available under XP:
937 -- T.Common.LL.Thread_Id := GetThreadId (hTask); so instead we set the
938 -- field to 0 to avoid having a random value. Thread_Id is initialized
939 -- in Enter_Task anyway.
941 T
.Common
.LL
.Thread_Id
:= 0;
943 -- Step 3: set its priority (child has inherited priority from parent)
945 Set_Priority
(T
, Priority
);
947 if Time_Slice_Val
= 0
948 or else Dispatching_Policy
= 'F'
949 or else Get_Policy
(Priority
) = 'F'
951 -- Here we need Annex D semantics so we disable the NT priority
952 -- boost. A priority boost is temporarily given by the system to
953 -- a thread when it is taken out of a wait state.
955 SetThreadPriorityBoost
(hTask
, DisablePriorityBoost
=> Win32
.TRUE);
958 -- Step 4: Handle pragma CPU and Task_Info
960 Set_Task_Affinity
(T
);
962 -- Step 5: Now, start it for good
964 Result
:= ResumeThread
(hTask
);
965 pragma Assert
(Result
= 1);
967 Succeeded
:= Result
= 1;
974 procedure Finalize_TCB
(T
: Task_Id
) is
976 pragma Unreferenced
(Succeeded
);
979 if not Single_Lock
then
980 Finalize_Lock
(T
.Common
.LL
.L
'Access);
983 Finalize_Cond
(T
.Common
.LL
.CV
'Access);
985 if T
.Known_Tasks_Index
/= -1 then
986 Known_Tasks
(T
.Known_Tasks_Index
) := null;
989 if T
.Common
.LL
.Thread
/= Null_Thread_Id
then
991 -- This task has been activated. Close the thread handle. This
992 -- is needed to release system resources.
994 Succeeded
:= CloseHandle
(T
.Common
.LL
.Thread
);
995 -- Note that we do not check for the returned value, this is
996 -- because the above call will fail for a foreign thread. But
997 -- we still need to call it to properly close Ada tasks created
998 -- with CreateThread() in Create_Task above.
1001 ATCB_Allocation
.Free_ATCB
(T
);
1008 procedure Exit_Task
is
1010 Specific
.Set
(null);
1017 procedure Abort_Task
(T
: Task_Id
) is
1018 pragma Unreferenced
(T
);
1023 ----------------------
1024 -- Environment_Task --
1025 ----------------------
1027 function Environment_Task
return Task_Id
is
1029 return Environment_Task_Id
;
1030 end Environment_Task
;
1036 procedure Lock_RTS
is
1038 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1045 procedure Unlock_RTS
is
1047 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1054 procedure Initialize
(Environment_Task
: Task_Id
) is
1058 Environment_Task_Id
:= Environment_Task
;
1059 OS_Primitives
.Initialize
;
1060 Interrupt_Management
.Initialize
;
1062 if Time_Slice_Val
= 0 or else Dispatching_Policy
= 'F' then
1063 -- Here we need Annex D semantics, switch the current process to the
1064 -- Realtime_Priority_Class.
1066 Discard
:= OS_Interface
.SetPriorityClass
1067 (GetCurrentProcess
, Realtime_Priority_Class
);
1070 TlsIndex
:= TlsAlloc
;
1072 -- Initialize the lock used to synchronize chain of all ATCBs
1074 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1076 Environment_Task
.Common
.LL
.Thread
:= GetCurrentThread
;
1078 -- Make environment task known here because it doesn't go through
1079 -- Activate_Tasks, which does it for all other tasks.
1081 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1082 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1084 Enter_Task
(Environment_Task
);
1086 -- pragma CPU and dispatching domains for the environment task
1088 Set_Task_Affinity
(Environment_Task
);
1091 ---------------------
1092 -- Monotonic_Clock --
1093 ---------------------
1095 function Monotonic_Clock
return Duration is
1096 function Internal_Clock
return Duration;
1097 pragma Import
(Ada
, Internal_Clock
, "__gnat_monotonic_clock");
1099 return Internal_Clock
;
1100 end Monotonic_Clock
;
1106 function RT_Resolution
return Duration is
1107 Ticks_Per_Second
: aliased LARGE_INTEGER
;
1109 QueryPerformanceFrequency
(Ticks_Per_Second
'Access);
1110 return Duration (1.0 / Ticks_Per_Second
);
1117 procedure Initialize
(S
: in out Suspension_Object
) is
1119 -- Initialize internal state. It is always initialized to False (ARM
1125 -- Initialize internal mutex
1127 InitializeCriticalSection
(S
.L
'Access);
1129 -- Initialize internal condition variable
1131 S
.CV
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
1132 pragma Assert
(S
.CV
/= 0);
1139 procedure Finalize
(S
: in out Suspension_Object
) is
1143 -- Destroy internal mutex
1145 DeleteCriticalSection
(S
.L
'Access);
1147 -- Destroy internal condition variable
1149 Result
:= CloseHandle
(S
.CV
);
1150 pragma Assert
(Result
= Win32
.TRUE);
1157 function Current_State
(S
: Suspension_Object
) return Boolean is
1159 -- We do not want to use lock on this read operation. State is marked
1160 -- as Atomic so that we ensure that the value retrieved is correct.
1169 procedure Set_False
(S
: in out Suspension_Object
) is
1171 SSL
.Abort_Defer
.all;
1173 EnterCriticalSection
(S
.L
'Access);
1177 LeaveCriticalSection
(S
.L
'Access);
1179 SSL
.Abort_Undefer
.all;
1186 procedure Set_True
(S
: in out Suspension_Object
) is
1190 SSL
.Abort_Defer
.all;
1192 EnterCriticalSection
(S
.L
'Access);
1194 -- If there is already a task waiting on this suspension object then
1195 -- we resume it, leaving the state of the suspension object to False,
1196 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1197 -- the state to True.
1203 Result
:= SetEvent
(S
.CV
);
1204 pragma Assert
(Result
= Win32
.TRUE);
1210 LeaveCriticalSection
(S
.L
'Access);
1212 SSL
.Abort_Undefer
.all;
1215 ------------------------
1216 -- Suspend_Until_True --
1217 ------------------------
1219 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1224 SSL
.Abort_Defer
.all;
1226 EnterCriticalSection
(S
.L
'Access);
1230 -- Program_Error must be raised upon calling Suspend_Until_True
1231 -- if another task is already waiting on that suspension object
1232 -- (ARM D.10 par. 10).
1234 LeaveCriticalSection
(S
.L
'Access);
1236 SSL
.Abort_Undefer
.all;
1238 raise Program_Error
;
1241 -- Suspend the task if the state is False. Otherwise, the task
1242 -- continues its execution, and the state of the suspension object
1243 -- is set to False (ARM D.10 par. 9).
1248 LeaveCriticalSection
(S
.L
'Access);
1250 SSL
.Abort_Undefer
.all;
1255 -- Must reset CV BEFORE L is unlocked
1257 Result_Bool
:= ResetEvent
(S
.CV
);
1258 pragma Assert
(Result_Bool
= Win32
.TRUE);
1260 LeaveCriticalSection
(S
.L
'Access);
1262 SSL
.Abort_Undefer
.all;
1264 Result
:= WaitForSingleObject
(S
.CV
, Wait_Infinite
);
1265 pragma Assert
(Result
= 0);
1268 end Suspend_Until_True
;
1274 -- Dummy versions, currently this only works for solaris (native)
1276 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1277 pragma Unreferenced
(Self_ID
);
1282 --------------------
1283 -- Check_No_Locks --
1284 --------------------
1286 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1287 pragma Unreferenced
(Self_ID
);
1296 function Suspend_Task
1298 Thread_Self
: Thread_Id
) return Boolean
1301 if T
.Common
.LL
.Thread
/= Thread_Self
then
1302 return SuspendThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1312 function Resume_Task
1314 Thread_Self
: Thread_Id
) return Boolean
1317 if T
.Common
.LL
.Thread
/= Thread_Self
then
1318 return ResumeThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1324 --------------------
1325 -- Stop_All_Tasks --
1326 --------------------
1328 procedure Stop_All_Tasks
is
1337 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1338 pragma Unreferenced
(T
);
1347 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1348 pragma Unreferenced
(T
);
1353 -----------------------
1354 -- Set_Task_Affinity --
1355 -----------------------
1357 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1360 use type System
.Multiprocessors
.CPU_Range
;
1363 -- Do nothing if the underlying thread has not yet been created. If the
1364 -- thread has not yet been created then the proper affinity will be set
1365 -- during its creation.
1367 if T
.Common
.LL
.Thread
= Null_Thread_Id
then
1372 elsif T
.Common
.Base_CPU
/= Multiprocessors
.Not_A_Specific_CPU
then
1374 -- The CPU numbering in pragma CPU starts at 1 while the subprogram
1375 -- to set the affinity starts at 0, therefore we must substract 1.
1378 SetThreadIdealProcessor
1379 (T
.Common
.LL
.Thread
, ProcessorId
(T
.Common
.Base_CPU
) - 1);
1380 pragma Assert
(Result
= 1);
1384 elsif T
.Common
.Task_Info
/= null then
1385 if T
.Common
.Task_Info
.CPU
/= Task_Info
.Any_CPU
then
1387 SetThreadIdealProcessor
1388 (T
.Common
.LL
.Thread
, T
.Common
.Task_Info
.CPU
);
1389 pragma Assert
(Result
= 1);
1392 -- Dispatching domains
1394 elsif T
.Common
.Domain
/= null
1395 and then (T
.Common
.Domain
/= ST
.System_Domain
1397 T
.Common
.Domain
.all /=
1398 (Multiprocessors
.CPU
'First ..
1399 Multiprocessors
.Number_Of_CPUs
=> True))
1402 CPU_Set
: DWORD
:= 0;
1405 for Proc
in T
.Common
.Domain
'Range loop
1406 if T
.Common
.Domain
(Proc
) then
1408 -- The thread affinity mask is a bit vector in which each
1409 -- bit represents a logical processor.
1411 CPU_Set
:= CPU_Set
+ 2 ** (Integer (Proc
) - 1);
1415 Result
:= SetThreadAffinityMask
(T
.Common
.LL
.Thread
, CPU_Set
);
1416 pragma Assert
(Result
= 1);
1419 end Set_Task_Affinity
;
1421 end System
.Task_Primitives
.Operations
;