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-2021, 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 with Interfaces
.C
.Strings
;
40 with System
.Float_Control
;
41 with System
.Interrupt_Management
;
42 with System
.Multiprocessors
;
43 with System
.OS_Primitives
;
44 with System
.Task_Info
;
45 with System
.Tasking
.Debug
;
46 with System
.Win32
.Ext
;
48 with System
.Soft_Links
;
49 -- We use System.Soft_Links instead of System.Tasking.Initialization because
50 -- the later is a higher level package that we shouldn't depend on. For
51 -- example when using the restricted run time, it is replaced by
52 -- System.Tasking.Restricted.Stages.
54 package body System
.Task_Primitives
.Operations
is
56 package SSL
renames System
.Soft_Links
;
59 use Interfaces
.C
.Strings
;
60 use System
.OS_Interface
;
61 use System
.OS_Primitives
;
62 use System
.Parameters
;
65 use System
.Tasking
.Debug
;
69 pragma Link_With
("-Xlinker --stack=0x200000,0x1000");
70 -- Change the default stack size (2 MB) for tasking programs on Windows.
71 -- This allows about 1000 tasks running at the same time. Note that
72 -- we set the stack size for non tasking programs on System unit.
73 -- Also note that under Windows XP, we use a Windows XP extension to
74 -- specify the stack size on a per task basis, as done under other OSes.
80 procedure InitializeCriticalSection
(pCriticalSection
: access RTS_Lock
);
81 procedure InitializeCriticalSection
82 (pCriticalSection
: access CRITICAL_SECTION
);
84 (Stdcall
, InitializeCriticalSection
, "InitializeCriticalSection");
86 procedure EnterCriticalSection
(pCriticalSection
: access RTS_Lock
);
87 procedure EnterCriticalSection
88 (pCriticalSection
: access CRITICAL_SECTION
);
89 pragma Import
(Stdcall
, EnterCriticalSection
, "EnterCriticalSection");
91 procedure LeaveCriticalSection
(pCriticalSection
: access RTS_Lock
);
92 procedure LeaveCriticalSection
(pCriticalSection
: access CRITICAL_SECTION
);
93 pragma Import
(Stdcall
, LeaveCriticalSection
, "LeaveCriticalSection");
95 procedure DeleteCriticalSection
(pCriticalSection
: access RTS_Lock
);
96 procedure DeleteCriticalSection
97 (pCriticalSection
: access CRITICAL_SECTION
);
98 pragma Import
(Stdcall
, DeleteCriticalSection
, "DeleteCriticalSection");
104 Environment_Task_Id
: Task_Id
;
105 -- A variable to hold Task_Id for the environment task
107 Single_RTS_Lock
: aliased RTS_Lock
;
108 -- This is a lock to allow only one thread of control in the RTS at
109 -- a time; it is used to execute in mutual exclusion from all other tasks.
110 -- Used to protect All_Tasks_List
112 Time_Slice_Val
: constant Integer;
113 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
115 Dispatching_Policy
: constant Character;
116 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
118 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
119 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
120 -- Get priority specific dispatching policy
122 Foreign_Task_Elaborated
: aliased Boolean := True;
123 -- Used to identified fake tasks (i.e., non-Ada Threads)
125 Null_Thread_Id
: constant Thread_Id
:= 0;
126 -- Constant to indicate that the thread identifier has not yet been
129 ------------------------------------
130 -- The thread local storage index --
131 ------------------------------------
134 pragma Export
(Ada
, TlsIndex
);
135 -- To ensure that this variable won't be local to this package, since
136 -- in some cases, inlining forces this variable to be global anyway.
144 function Is_Valid_Task
return Boolean;
145 pragma Inline
(Is_Valid_Task
);
146 -- Does executing thread have a TCB?
148 procedure Set
(Self_Id
: Task_Id
);
150 -- Set the self id for the current task
154 package body Specific
is
160 function Is_Valid_Task
return Boolean is
162 return TlsGetValue
(TlsIndex
) /= System
.Null_Address
;
169 procedure Set
(Self_Id
: Task_Id
) is
172 Succeeded
:= TlsSetValue
(TlsIndex
, To_Address
(Self_Id
));
173 pragma Assert
(Succeeded
= Win32
.TRUE);
178 ----------------------------------
179 -- ATCB allocation/deallocation --
180 ----------------------------------
182 package body ATCB_Allocation
is separate;
183 -- The body of this package is shared across several targets
185 ---------------------------------
186 -- Support for foreign threads --
187 ---------------------------------
189 function Register_Foreign_Thread
191 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
192 -- Allocate and initialize a new ATCB for the current Thread. The size of
193 -- the secondary stack can be optionally specified.
195 function Register_Foreign_Thread
197 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
198 return Task_Id
is separate;
200 ----------------------------------
201 -- Condition Variable Functions --
202 ----------------------------------
204 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
);
205 -- Initialize given condition variable Cond
207 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
);
208 -- Finalize given condition variable Cond
210 procedure Cond_Signal
(Cond
: not null access Condition_Variable
);
211 -- Signal condition variable Cond
214 (Cond
: not null access Condition_Variable
;
215 L
: not null access RTS_Lock
);
216 -- Wait on conditional variable Cond, using lock L
218 procedure Cond_Timed_Wait
219 (Cond
: not null access Condition_Variable
;
220 L
: not null access RTS_Lock
;
222 Timed_Out
: out Boolean;
223 Status
: out Integer);
224 -- Do timed wait on condition variable Cond using lock L. The duration
225 -- of the timed wait is given by Rel_Time. When the condition is
226 -- signalled, Timed_Out shows whether or not a time out occurred.
227 -- Status is only valid if Timed_Out is False, in which case it
228 -- shows whether Cond_Timed_Wait completed successfully.
230 ---------------------
231 -- Initialize_Cond --
232 ---------------------
234 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
) is
237 hEvent
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
238 pragma Assert
(hEvent
/= 0);
239 Cond
.all := Condition_Variable
(hEvent
);
246 -- No such problem here, DosCloseEventSem has been derived.
247 -- What does such refer to in above comment???
249 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
) is
252 Result
:= CloseHandle
(HANDLE
(Cond
.all));
253 pragma Assert
(Result
= Win32
.TRUE);
260 procedure Cond_Signal
(Cond
: not null access Condition_Variable
) is
263 Result
:= SetEvent
(HANDLE
(Cond
.all));
264 pragma Assert
(Result
= Win32
.TRUE);
271 -- Pre-condition: Cond is posted
274 -- Post-condition: Cond is posted
278 (Cond
: not null access Condition_Variable
;
279 L
: not null access RTS_Lock
)
285 -- Must reset Cond BEFORE L is unlocked
287 Result_Bool
:= ResetEvent
(HANDLE
(Cond
.all));
288 pragma Assert
(Result_Bool
= Win32
.TRUE);
291 -- No problem if we are interrupted here: if the condition is signaled,
292 -- WaitForSingleObject will simply not block
294 Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Wait_Infinite
);
295 pragma Assert
(Result
= 0);
300 ---------------------
301 -- Cond_Timed_Wait --
302 ---------------------
304 -- Pre-condition: Cond is posted
307 -- Post-condition: Cond is posted
310 procedure Cond_Timed_Wait
311 (Cond
: not null access Condition_Variable
;
312 L
: not null access RTS_Lock
;
314 Timed_Out
: out Boolean;
315 Status
: out Integer)
317 Time_Out_Max
: constant DWORD
:= 16#FFFF0000#
;
318 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
325 -- Must reset Cond BEFORE L is unlocked
327 Result
:= ResetEvent
(HANDLE
(Cond
.all));
328 pragma Assert
(Result
= Win32
.TRUE);
331 -- No problem if we are interrupted here: if the condition is signaled,
332 -- WaitForSingleObject will simply not block.
334 if Rel_Time
<= 0.0 then
340 (if Rel_Time
>= Duration (Time_Out_Max
) / 1000
342 else DWORD
(Rel_Time
* 1000));
344 Wait_Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Time_Out
);
346 if Wait_Result
= WAIT_TIMEOUT
then
356 -- Ensure post-condition
359 Result
:= SetEvent
(HANDLE
(Cond
.all));
360 pragma Assert
(Result
= Win32
.TRUE);
363 Status
:= Integer (Wait_Result
);
370 -- The underlying thread system sets a guard page at the bottom of a thread
371 -- stack, so nothing is needed.
372 -- ??? Check the comment above
374 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
375 pragma Unreferenced
(T
, On
);
384 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
386 return T
.Common
.LL
.Thread
;
393 function Self
return Task_Id
is
394 Self_Id
: constant Task_Id
:= To_Task_Id
(TlsGetValue
(TlsIndex
));
396 if Self_Id
= null then
397 return Register_Foreign_Thread
(GetCurrentThread
);
403 ---------------------
404 -- Initialize_Lock --
405 ---------------------
407 -- Note: mutexes and cond_variables needed per-task basis are initialized
408 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
409 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
410 -- status change of RTS. Therefore raising Storage_Error in the following
411 -- routines should be able to be handled safely.
413 procedure Initialize_Lock
414 (Prio
: System
.Any_Priority
;
415 L
: not null access Lock
)
418 InitializeCriticalSection
(L
.Mutex
'Access);
419 L
.Owner_Priority
:= 0;
423 procedure Initialize_Lock
424 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
426 pragma Unreferenced
(Level
);
428 InitializeCriticalSection
(L
);
435 procedure Finalize_Lock
(L
: not null access Lock
) is
437 DeleteCriticalSection
(L
.Mutex
'Access);
440 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
442 DeleteCriticalSection
(L
);
450 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
452 L
.Owner_Priority
:= Get_Priority
(Self
);
454 if L
.Priority
< L
.Owner_Priority
then
455 Ceiling_Violation
:= True;
459 EnterCriticalSection
(L
.Mutex
'Access);
461 Ceiling_Violation
:= False;
464 procedure Write_Lock
(L
: not null access RTS_Lock
) is
466 EnterCriticalSection
(L
);
469 procedure Write_Lock
(T
: Task_Id
) is
471 EnterCriticalSection
(T
.Common
.LL
.L
'Access);
479 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
481 Write_Lock
(L
, Ceiling_Violation
);
488 procedure Unlock
(L
: not null access Lock
) is
490 LeaveCriticalSection
(L
.Mutex
'Access);
493 procedure Unlock
(L
: not null access RTS_Lock
) is
495 LeaveCriticalSection
(L
);
498 procedure Unlock
(T
: Task_Id
) is
500 LeaveCriticalSection
(T
.Common
.LL
.L
'Access);
507 -- Dynamic priority ceilings are not supported by the underlying system
509 procedure Set_Ceiling
510 (L
: not null access Lock
;
511 Prio
: System
.Any_Priority
)
513 pragma Unreferenced
(L
, Prio
);
524 Reason
: System
.Tasking
.Task_States
)
526 pragma Unreferenced
(Reason
);
529 pragma Assert
(Self_ID
= Self
);
531 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
533 if Self_ID
.Deferral_Level
= 0
534 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
537 raise Standard
'Abort_Signal;
545 -- This is for use within the run-time system, so abort is assumed to be
546 -- already deferred, and the caller should be holding its own ATCB lock.
548 procedure Timed_Sleep
551 Mode
: ST
.Delay_Modes
;
552 Reason
: System
.Tasking
.Task_States
;
553 Timedout
: out Boolean;
554 Yielded
: out Boolean)
556 pragma Unreferenced
(Reason
);
557 Check_Time
: Duration := Monotonic_Clock
;
563 Local_Timedout
: Boolean;
569 if Mode
= Relative
then
571 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
573 Rel_Time
:= Time
- Check_Time
;
577 if Rel_Time
> 0.0 then
579 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
582 (Self_ID
.Common
.LL
.CV
'Access,
583 Self_ID
.Common
.LL
.L
'Access,
584 Rel_Time
, Local_Timedout
, Result
);
585 Check_Time
:= Monotonic_Clock
;
587 exit when Abs_Time
<= Check_Time
;
589 if not Local_Timedout
then
591 -- Somebody may have called Wakeup for us
597 Rel_Time
:= Abs_Time
- Check_Time
;
606 procedure Timed_Delay
609 Mode
: ST
.Delay_Modes
)
611 Check_Time
: Duration := Monotonic_Clock
;
619 Write_Lock
(Self_ID
);
621 if Mode
= Relative
then
623 Abs_Time
:= Time
+ Check_Time
;
625 Rel_Time
:= Time
- Check_Time
;
629 if Rel_Time
> 0.0 then
630 Self_ID
.Common
.State
:= Delay_Sleep
;
633 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
636 (Self_ID
.Common
.LL
.CV
'Access,
637 Self_ID
.Common
.LL
.L
'Access,
638 Rel_Time
, Timedout
, Result
);
639 Check_Time
:= Monotonic_Clock
;
641 exit when Abs_Time
<= Check_Time
;
643 Rel_Time
:= Abs_Time
- Check_Time
;
646 Self_ID
.Common
.State
:= Runnable
;
657 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
658 pragma Unreferenced
(Reason
);
660 Cond_Signal
(T
.Common
.LL
.CV
'Access);
667 procedure Yield
(Do_Yield
: Boolean := True) is
669 -- Note: in a previous implementation if Do_Yield was False, then we
670 -- introduced a delay of 1 millisecond in an attempt to get closer to
671 -- annex D semantics, and in particular to make ACATS CXD8002 pass. But
672 -- this change introduced a huge performance regression evaluating the
673 -- Count attribute. So we decided to remove this processing.
675 -- Moreover, CXD8002 appears to pass on Windows (although we do not
676 -- guarantee full Annex D compliance on Windows in any case).
687 procedure Set_Priority
689 Prio
: System
.Any_Priority
;
690 Loss_Of_Inheritance
: Boolean := False)
693 pragma Unreferenced
(Loss_Of_Inheritance
);
699 Interfaces
.C
.int
(Underlying_Priorities
(Prio
)));
700 pragma Assert
(Res
= Win32
.TRUE);
702 -- Note: Annex D (RM D.2.3(5/2)) requires the task to be placed at the
703 -- head of its priority queue when decreasing its priority as a result
704 -- of a loss of inherited priority. This is not the case, but we
705 -- consider it an acceptable variation (RM 1.1.3(6)), given this is
706 -- the built-in behavior offered by the Windows operating system.
708 -- In older versions we attempted to better approximate the Annex D
709 -- required behavior, but this simulation was not entirely accurate,
710 -- and it seems better to live with the standard Windows semantics.
712 T
.Common
.Current_Priority
:= Prio
;
719 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
721 return T
.Common
.Current_Priority
;
728 -- There were two paths were we needed to call Enter_Task :
729 -- 1) from System.Task_Primitives.Operations.Initialize
730 -- 2) from System.Tasking.Stages.Task_Wrapper
732 -- The pseudo handle (LL.Thread) need not be closed when it is no
733 -- longer needed. Calling the CloseHandle function with this handle
736 procedure Enter_Task
(Self_ID
: Task_Id
) is
737 procedure Get_Stack_Bounds
(Base
: Address
; Limit
: Address
);
738 pragma Import
(C
, Get_Stack_Bounds
, "__gnat_get_stack_bounds");
739 -- Get stack boundaries
741 Specific
.Set
(Self_ID
);
743 -- Properly initializes the FPU for x86 systems
745 System
.Float_Control
.Reset
;
747 if Self_ID
.Common
.Task_Info
/= null
749 Self_ID
.Common
.Task_Info
.CPU
>= CPU_Number
(Number_Of_Processors
)
751 raise Invalid_CPU_Number
;
754 -- Initialize the thread here only if not set. This is done for a
755 -- foreign task but is not needed when a real thread-id is already
756 -- set in Create_Task. Note that we do want to keep the real thread-id
757 -- as it is the only way to free the associated resource. Another way
758 -- to say this is that a pseudo thread-id from a foreign thread won't
759 -- allow for freeing resources.
761 if Self_ID
.Common
.LL
.Thread
= Null_Thread_Id
then
762 Self_ID
.Common
.LL
.Thread
:= GetCurrentThread
;
765 Self_ID
.Common
.LL
.Thread_Id
:= GetCurrentThreadId
;
768 (Self_ID
.Common
.Compiler_Data
.Pri_Stack_Info
.Base
'Address,
769 Self_ID
.Common
.Compiler_Data
.Pri_Stack_Info
.Limit
'Address);
776 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
778 -----------------------------
779 -- Register_Foreign_Thread --
780 -----------------------------
782 function Register_Foreign_Thread
return Task_Id
is
784 if Is_Valid_Task
then
787 return Register_Foreign_Thread
(GetCurrentThread
);
789 end Register_Foreign_Thread
;
795 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
797 -- Initialize thread ID to 0, this is needed to detect threads that
798 -- are not yet activated.
800 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
802 Initialize_Cond
(Self_ID
.Common
.LL
.CV
'Access);
803 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
812 procedure Create_Task
814 Wrapper
: System
.Address
;
815 Stack_Size
: System
.Parameters
.Size_Type
;
816 Priority
: System
.Any_Priority
;
817 Succeeded
: out Boolean)
819 Initial_Stack_Size
: constant := 1024;
820 -- We set the initial stack size to 1024. On Windows version prior to XP
821 -- there is no way to fix a task stack size. Only the initial stack size
822 -- can be set, the operating system will raise the task stack size if
825 function Is_Windows_XP
return Integer;
826 pragma Import
(C
, Is_Windows_XP
, "__gnat_is_windows_xp");
827 -- Returns 1 if running on Windows XP
830 TaskId
: aliased DWORD
;
831 pTaskParameter
: Win32
.PVOID
;
833 Entry_Point
: PTHREAD_START_ROUTINE
;
835 use type System
.Multiprocessors
.CPU_Range
;
838 -- Check whether both Dispatching_Domain and CPU are specified for the
839 -- task, and the CPU value is not contained within the range of
840 -- processors for the domain.
842 if T
.Common
.Domain
/= null
843 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
845 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
846 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
852 pTaskParameter
:= To_Address
(T
);
854 Entry_Point
:= To_PTHREAD_START_ROUTINE
(Wrapper
);
856 if Is_Windows_XP
= 1 then
857 hTask
:= CreateThread
862 DWORD
(Create_Suspended
)
863 or DWORD
(Stack_Size_Param_Is_A_Reservation
),
864 TaskId
'Unchecked_Access);
866 hTask
:= CreateThread
871 DWORD
(Create_Suspended
),
872 TaskId
'Unchecked_Access);
875 -- Step 1: Create the thread in blocked mode
882 -- Step 2: set its TCB
884 T
.Common
.LL
.Thread
:= hTask
;
886 -- Note: it would be useful to initialize Thread_Id right away to avoid
887 -- a race condition in gdb where Thread_ID may not have the right value
888 -- yet, but GetThreadId is a Vista specific API, not available under XP:
889 -- T.Common.LL.Thread_Id := GetThreadId (hTask); so instead we set the
890 -- field to 0 to avoid having a random value. Thread_Id is initialized
891 -- in Enter_Task anyway.
893 T
.Common
.LL
.Thread_Id
:= 0;
895 -- Step 3: set its priority (child has inherited priority from parent)
897 Set_Priority
(T
, Priority
);
899 if Time_Slice_Val
= 0
900 or else Dispatching_Policy
= 'F'
901 or else Get_Policy
(Priority
) = 'F'
903 -- Here we need Annex D semantics so we disable the NT priority
904 -- boost. A priority boost is temporarily given by the system to
905 -- a thread when it is taken out of a wait state.
907 SetThreadPriorityBoost
(hTask
, DisablePriorityBoost
=> Win32
.TRUE);
910 -- Step 4: Handle pragma CPU and Task_Info
912 Set_Task_Affinity
(T
);
914 -- Step 5: Now, start it for good
916 Result
:= ResumeThread
(hTask
);
917 pragma Assert
(Result
= 1);
919 Succeeded
:= Result
= 1;
926 procedure Finalize_TCB
(T
: Task_Id
) is
928 pragma Unreferenced
(Succeeded
);
931 Finalize_Lock
(T
.Common
.LL
.L
'Access);
932 Finalize_Cond
(T
.Common
.LL
.CV
'Access);
934 if T
.Known_Tasks_Index
/= -1 then
935 Known_Tasks
(T
.Known_Tasks_Index
) := null;
938 if T
.Common
.LL
.Thread
/= Null_Thread_Id
then
940 -- This task has been activated. Close the thread handle. This
941 -- is needed to release system resources.
943 Succeeded
:= CloseHandle
(T
.Common
.LL
.Thread
);
944 -- Note that we do not check for the returned value, this is
945 -- because the above call will fail for a foreign thread. But
946 -- we still need to call it to properly close Ada tasks created
947 -- with CreateThread() in Create_Task above.
950 ATCB_Allocation
.Free_ATCB
(T
);
957 procedure Exit_Task
is
966 procedure Abort_Task
(T
: Task_Id
) is
967 pragma Unreferenced
(T
);
972 ----------------------
973 -- Environment_Task --
974 ----------------------
976 function Environment_Task
return Task_Id
is
978 return Environment_Task_Id
;
979 end Environment_Task
;
985 procedure Lock_RTS
is
987 Write_Lock
(Single_RTS_Lock
'Access);
994 procedure Unlock_RTS
is
996 Unlock
(Single_RTS_Lock
'Access);
1003 procedure Initialize
(Environment_Task
: Task_Id
) is
1007 Environment_Task_Id
:= Environment_Task
;
1008 OS_Primitives
.Initialize
;
1009 Interrupt_Management
.Initialize
;
1011 if Time_Slice_Val
= 0 or else Dispatching_Policy
= 'F' then
1012 -- Here we need Annex D semantics, switch the current process to the
1013 -- Realtime_Priority_Class.
1015 Discard
:= OS_Interface
.SetPriorityClass
1016 (GetCurrentProcess
, Realtime_Priority_Class
);
1019 TlsIndex
:= TlsAlloc
;
1021 -- Initialize the lock used to synchronize chain of all ATCBs
1023 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1025 Environment_Task
.Common
.LL
.Thread
:= GetCurrentThread
;
1027 -- Make environment task known here because it doesn't go through
1028 -- Activate_Tasks, which does it for all other tasks.
1030 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1031 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1033 Enter_Task
(Environment_Task
);
1035 -- pragma CPU and dispatching domains for the environment task
1037 Set_Task_Affinity
(Environment_Task
);
1040 ---------------------
1041 -- Monotonic_Clock --
1042 ---------------------
1044 function Monotonic_Clock
return Duration is
1045 function Internal_Clock
return Duration;
1046 pragma Import
(Ada
, Internal_Clock
, "__gnat_monotonic_clock");
1048 return Internal_Clock
;
1049 end Monotonic_Clock
;
1055 function RT_Resolution
return Duration is
1056 Ticks_Per_Second
: aliased LARGE_INTEGER
;
1058 QueryPerformanceFrequency
(Ticks_Per_Second
'Access);
1059 return Duration (1.0 / Ticks_Per_Second
);
1066 procedure Initialize
(S
: in out Suspension_Object
) is
1068 -- Initialize internal state. It is always initialized to False (ARM
1074 -- Initialize internal mutex
1076 InitializeCriticalSection
(S
.L
'Access);
1078 -- Initialize internal condition variable
1080 S
.CV
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
1081 pragma Assert
(S
.CV
/= 0);
1088 procedure Finalize
(S
: in out Suspension_Object
) is
1092 -- Destroy internal mutex
1094 DeleteCriticalSection
(S
.L
'Access);
1096 -- Destroy internal condition variable
1098 Result
:= CloseHandle
(S
.CV
);
1099 pragma Assert
(Result
= Win32
.TRUE);
1106 function Current_State
(S
: Suspension_Object
) return Boolean is
1108 -- We do not want to use lock on this read operation. State is marked
1109 -- as Atomic so that we ensure that the value retrieved is correct.
1118 procedure Set_False
(S
: in out Suspension_Object
) is
1120 SSL
.Abort_Defer
.all;
1122 EnterCriticalSection
(S
.L
'Access);
1126 LeaveCriticalSection
(S
.L
'Access);
1128 SSL
.Abort_Undefer
.all;
1135 procedure Set_True
(S
: in out Suspension_Object
) is
1139 SSL
.Abort_Defer
.all;
1141 EnterCriticalSection
(S
.L
'Access);
1143 -- If there is already a task waiting on this suspension object then
1144 -- we resume it, leaving the state of the suspension object to False,
1145 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1146 -- the state to True.
1152 Result
:= SetEvent
(S
.CV
);
1153 pragma Assert
(Result
= Win32
.TRUE);
1159 LeaveCriticalSection
(S
.L
'Access);
1161 SSL
.Abort_Undefer
.all;
1164 ------------------------
1165 -- Suspend_Until_True --
1166 ------------------------
1168 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1173 SSL
.Abort_Defer
.all;
1175 EnterCriticalSection
(S
.L
'Access);
1179 -- Program_Error must be raised upon calling Suspend_Until_True
1180 -- if another task is already waiting on that suspension object
1181 -- (ARM D.10 par. 10).
1183 LeaveCriticalSection
(S
.L
'Access);
1185 SSL
.Abort_Undefer
.all;
1187 raise Program_Error
;
1190 -- Suspend the task if the state is False. Otherwise, the task
1191 -- continues its execution, and the state of the suspension object
1192 -- is set to False (ARM D.10 par. 9).
1197 LeaveCriticalSection
(S
.L
'Access);
1199 SSL
.Abort_Undefer
.all;
1204 -- Must reset CV BEFORE L is unlocked
1206 Result_Bool
:= ResetEvent
(S
.CV
);
1207 pragma Assert
(Result_Bool
= Win32
.TRUE);
1209 LeaveCriticalSection
(S
.L
'Access);
1211 SSL
.Abort_Undefer
.all;
1213 Result
:= WaitForSingleObject
(S
.CV
, Wait_Infinite
);
1214 pragma Assert
(Result
= 0);
1217 end Suspend_Until_True
;
1223 -- Dummy versions, currently this only works for solaris (native)
1225 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1226 pragma Unreferenced
(Self_ID
);
1231 --------------------
1232 -- Check_No_Locks --
1233 --------------------
1235 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1236 pragma Unreferenced
(Self_ID
);
1245 function Suspend_Task
1247 Thread_Self
: Thread_Id
) return Boolean
1250 if T
.Common
.LL
.Thread
/= Thread_Self
then
1251 return SuspendThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1261 function Resume_Task
1263 Thread_Self
: Thread_Id
) return Boolean
1266 if T
.Common
.LL
.Thread
/= Thread_Self
then
1267 return ResumeThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1273 --------------------
1274 -- Stop_All_Tasks --
1275 --------------------
1277 procedure Stop_All_Tasks
is
1286 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1287 pragma Unreferenced
(T
);
1296 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1297 pragma Unreferenced
(T
);
1302 -----------------------
1303 -- Set_Task_Affinity --
1304 -----------------------
1306 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1309 use type System
.Multiprocessors
.CPU_Range
;
1312 -- Do nothing if the underlying thread has not yet been created. If the
1313 -- thread has not yet been created then the proper affinity will be set
1314 -- during its creation.
1316 if T
.Common
.LL
.Thread
= Null_Thread_Id
then
1321 elsif T
.Common
.Base_CPU
/= Multiprocessors
.Not_A_Specific_CPU
then
1323 -- The CPU numbering in pragma CPU starts at 1 while the subprogram
1324 -- to set the affinity starts at 0, therefore we must substract 1.
1327 SetThreadIdealProcessor
1328 (T
.Common
.LL
.Thread
, ProcessorId
(T
.Common
.Base_CPU
) - 1);
1329 pragma Assert
(Result
= 1);
1333 elsif T
.Common
.Task_Info
/= null then
1334 if T
.Common
.Task_Info
.CPU
/= Task_Info
.Any_CPU
then
1336 SetThreadIdealProcessor
1337 (T
.Common
.LL
.Thread
, T
.Common
.Task_Info
.CPU
);
1338 pragma Assert
(Result
= 1);
1341 -- Dispatching domains
1343 elsif T
.Common
.Domain
/= null
1344 and then (T
.Common
.Domain
/= ST
.System_Domain
1346 T
.Common
.Domain
.all /=
1347 (Multiprocessors
.CPU
'First ..
1348 Multiprocessors
.Number_Of_CPUs
=> True))
1351 CPU_Set
: DWORD
:= 0;
1354 for Proc
in T
.Common
.Domain
'Range loop
1355 if T
.Common
.Domain
(Proc
) then
1357 -- The thread affinity mask is a bit vector in which each
1358 -- bit represents a logical processor.
1360 CPU_Set
:= CPU_Set
+ 2 ** (Integer (Proc
) - 1);
1364 Result
:= SetThreadAffinityMask
(T
.Common
.LL
.Thread
, CPU_Set
);
1365 pragma Assert
(Result
= 1);
1368 end Set_Task_Affinity
;
1370 end System
.Task_Primitives
.Operations
;