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-2009, 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.
41 with Ada
.Unchecked_Deallocation
;
44 with Interfaces
.C
.Strings
;
46 with System
.Tasking
.Debug
;
47 with System
.OS_Primitives
;
48 with System
.Task_Info
;
49 with System
.Interrupt_Management
;
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
;
62 use System
.Tasking
.Debug
;
65 use Interfaces
.C
.Strings
;
66 use System
.OS_Interface
;
67 use System
.Parameters
;
68 use System
.OS_Primitives
;
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 Annex_D
: Boolean := False;
130 -- Set to True if running with Annex-D semantics
132 ------------------------------------
133 -- The thread local storage index --
134 ------------------------------------
137 pragma Export
(Ada
, TlsIndex
);
138 -- To ensure that this variable won't be local to this package, since
139 -- in some cases, inlining forces this variable to be global anyway.
147 function Is_Valid_Task
return Boolean;
148 pragma Inline
(Is_Valid_Task
);
149 -- Does executing thread have a TCB?
151 procedure Set
(Self_Id
: Task_Id
);
153 -- Set the self id for the current task
157 package body Specific
is
159 function Is_Valid_Task
return Boolean is
161 return TlsGetValue
(TlsIndex
) /= System
.Null_Address
;
164 procedure Set
(Self_Id
: Task_Id
) is
167 Succeeded
:= TlsSetValue
(TlsIndex
, To_Address
(Self_Id
));
168 pragma Assert
(Succeeded
= Win32
.TRUE);
173 ---------------------------------
174 -- Support for foreign threads --
175 ---------------------------------
177 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
178 -- Allocate and Initialize a new ATCB for the current Thread
180 function Register_Foreign_Thread
181 (Thread
: Thread_Id
) return Task_Id
is separate;
183 ----------------------------------
184 -- Condition Variable Functions --
185 ----------------------------------
187 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
);
188 -- Initialize given condition variable Cond
190 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
);
191 -- Finalize given condition variable Cond
193 procedure Cond_Signal
(Cond
: not null access Condition_Variable
);
194 -- Signal condition variable Cond
197 (Cond
: not null access Condition_Variable
;
198 L
: not null access RTS_Lock
);
199 -- Wait on conditional variable Cond, using lock L
201 procedure Cond_Timed_Wait
202 (Cond
: not null access Condition_Variable
;
203 L
: not null access RTS_Lock
;
205 Timed_Out
: out Boolean;
206 Status
: out Integer);
207 -- Do timed wait on condition variable Cond using lock L. The duration
208 -- of the timed wait is given by Rel_Time. When the condition is
209 -- signalled, Timed_Out shows whether or not a time out occurred.
210 -- Status is only valid if Timed_Out is False, in which case it
211 -- shows whether Cond_Timed_Wait completed successfully.
213 ---------------------
214 -- Initialize_Cond --
215 ---------------------
217 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
) is
220 hEvent
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
221 pragma Assert
(hEvent
/= 0);
222 Cond
.all := Condition_Variable
(hEvent
);
229 -- No such problem here, DosCloseEventSem has been derived.
230 -- What does such refer to in above comment???
232 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
) is
235 Result
:= CloseHandle
(HANDLE
(Cond
.all));
236 pragma Assert
(Result
= Win32
.TRUE);
243 procedure Cond_Signal
(Cond
: not null access Condition_Variable
) is
246 Result
:= SetEvent
(HANDLE
(Cond
.all));
247 pragma Assert
(Result
= Win32
.TRUE);
254 -- Pre-condition: Cond is posted
257 -- Post-condition: Cond is posted
261 (Cond
: not null access Condition_Variable
;
262 L
: not null access RTS_Lock
)
268 -- Must reset Cond BEFORE L is unlocked
270 Result_Bool
:= ResetEvent
(HANDLE
(Cond
.all));
271 pragma Assert
(Result_Bool
= Win32
.TRUE);
272 Unlock
(L
, Global_Lock
=> True);
274 -- No problem if we are interrupted here: if the condition is signaled,
275 -- WaitForSingleObject will simply not block
277 Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Wait_Infinite
);
278 pragma Assert
(Result
= 0);
280 Write_Lock
(L
, Global_Lock
=> True);
283 ---------------------
284 -- Cond_Timed_Wait --
285 ---------------------
287 -- Pre-condition: Cond is posted
290 -- Post-condition: Cond is posted
293 procedure Cond_Timed_Wait
294 (Cond
: not null access Condition_Variable
;
295 L
: not null access RTS_Lock
;
297 Timed_Out
: out Boolean;
298 Status
: out Integer)
300 Time_Out_Max
: constant DWORD
:= 16#FFFF0000#
;
301 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
308 -- Must reset Cond BEFORE L is unlocked
310 Result
:= ResetEvent
(HANDLE
(Cond
.all));
311 pragma Assert
(Result
= Win32
.TRUE);
312 Unlock
(L
, Global_Lock
=> True);
314 -- No problem if we are interrupted here: if the condition is signaled,
315 -- WaitForSingleObject will simply not block
317 if Rel_Time
<= 0.0 then
322 if Rel_Time
>= Duration (Time_Out_Max
) / 1000 then
323 Time_Out
:= Time_Out_Max
;
325 Time_Out
:= DWORD
(Rel_Time
* 1000);
328 Wait_Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Time_Out
);
330 if Wait_Result
= WAIT_TIMEOUT
then
338 Write_Lock
(L
, Global_Lock
=> True);
340 -- Ensure post-condition
343 Result
:= SetEvent
(HANDLE
(Cond
.all));
344 pragma Assert
(Result
= Win32
.TRUE);
347 Status
:= Integer (Wait_Result
);
354 -- The underlying thread system sets a guard page at the bottom of a thread
355 -- stack, so nothing is needed.
356 -- ??? Check the comment above
358 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
359 pragma Unreferenced
(T
, On
);
368 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
370 return T
.Common
.LL
.Thread
;
377 function Self
return Task_Id
is
378 Self_Id
: constant Task_Id
:= To_Task_Id
(TlsGetValue
(TlsIndex
));
380 if Self_Id
= null then
381 return Register_Foreign_Thread
(GetCurrentThread
);
387 ---------------------
388 -- Initialize_Lock --
389 ---------------------
391 -- Note: mutexes and cond_variables needed per-task basis are initialized
392 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
393 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
394 -- status change of RTS. Therefore raising Storage_Error in the following
395 -- routines should be able to be handled safely.
397 procedure Initialize_Lock
398 (Prio
: System
.Any_Priority
;
399 L
: not null access Lock
)
402 InitializeCriticalSection
(L
.Mutex
'Access);
403 L
.Owner_Priority
:= 0;
407 procedure Initialize_Lock
408 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
410 pragma Unreferenced
(Level
);
412 InitializeCriticalSection
(L
);
419 procedure Finalize_Lock
(L
: not null access Lock
) is
421 DeleteCriticalSection
(L
.Mutex
'Access);
424 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
426 DeleteCriticalSection
(L
);
434 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
436 L
.Owner_Priority
:= Get_Priority
(Self
);
438 if L
.Priority
< L
.Owner_Priority
then
439 Ceiling_Violation
:= True;
443 EnterCriticalSection
(L
.Mutex
'Access);
445 Ceiling_Violation
:= False;
449 (L
: not null access RTS_Lock
;
450 Global_Lock
: Boolean := False)
453 if not Single_Lock
or else Global_Lock
then
454 EnterCriticalSection
(L
);
458 procedure Write_Lock
(T
: Task_Id
) is
460 if not Single_Lock
then
461 EnterCriticalSection
(T
.Common
.LL
.L
'Access);
470 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
472 Write_Lock
(L
, Ceiling_Violation
);
479 procedure Unlock
(L
: not null access Lock
) is
481 LeaveCriticalSection
(L
.Mutex
'Access);
485 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False) is
487 if not Single_Lock
or else Global_Lock
then
488 LeaveCriticalSection
(L
);
492 procedure Unlock
(T
: Task_Id
) is
494 if not Single_Lock
then
495 LeaveCriticalSection
(T
.Common
.LL
.L
'Access);
503 -- Dynamic priority ceilings are not supported by the underlying system
505 procedure Set_Ceiling
506 (L
: not null access Lock
;
507 Prio
: System
.Any_Priority
)
509 pragma Unreferenced
(L
, Prio
);
520 Reason
: System
.Tasking
.Task_States
)
522 pragma Unreferenced
(Reason
);
525 pragma Assert
(Self_ID
= Self
);
528 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
530 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
;
562 pragma Unreferenced
(Result
);
564 Local_Timedout
: Boolean;
570 if Mode
= Relative
then
572 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
574 Rel_Time
:= Time
- Check_Time
;
578 if Rel_Time
> 0.0 then
580 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
584 (Self_ID
.Common
.LL
.CV
'Access,
585 Single_RTS_Lock
'Access,
586 Rel_Time
, Local_Timedout
, Result
);
589 (Self_ID
.Common
.LL
.CV
'Access,
590 Self_ID
.Common
.LL
.L
'Access,
591 Rel_Time
, Local_Timedout
, Result
);
594 Check_Time
:= Monotonic_Clock
;
595 exit when Abs_Time
<= Check_Time
;
597 if not Local_Timedout
then
599 -- Somebody may have called Wakeup for us
605 Rel_Time
:= Abs_Time
- Check_Time
;
614 procedure Timed_Delay
617 Mode
: ST
.Delay_Modes
)
619 Check_Time
: Duration := Monotonic_Clock
;
625 pragma Unreferenced
(Timedout
, Result
);
632 Write_Lock
(Self_ID
);
634 if Mode
= Relative
then
636 Abs_Time
:= Time
+ Check_Time
;
638 Rel_Time
:= Time
- Check_Time
;
642 if Rel_Time
> 0.0 then
643 Self_ID
.Common
.State
:= Delay_Sleep
;
646 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
650 (Self_ID
.Common
.LL
.CV
'Access,
651 Single_RTS_Lock
'Access,
652 Rel_Time
, Timedout
, Result
);
655 (Self_ID
.Common
.LL
.CV
'Access,
656 Self_ID
.Common
.LL
.L
'Access,
657 Rel_Time
, Timedout
, Result
);
660 Check_Time
:= Monotonic_Clock
;
661 exit when Abs_Time
<= Check_Time
;
663 Rel_Time
:= Abs_Time
- Check_Time
;
666 Self_ID
.Common
.State
:= Runnable
;
682 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
683 pragma Unreferenced
(Reason
);
685 Cond_Signal
(T
.Common
.LL
.CV
'Access);
692 procedure Yield
(Do_Yield
: Boolean := True) is
698 -- If running with Annex-D semantics we need a delay
699 -- above 0 milliseconds here otherwise processes give
700 -- enough time to the other tasks to have a chance to
703 -- This makes cxd8002 ACATS pass on Windows.
713 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
714 pragma Atomic_Components
(Prio_Array_Type
);
716 Prio_Array
: Prio_Array_Type
;
717 -- Global array containing the id of the currently running task for
720 -- Note: we assume that we are on a single processor with run-til-blocked
723 procedure Set_Priority
725 Prio
: System
.Any_Priority
;
726 Loss_Of_Inheritance
: Boolean := False)
729 Array_Item
: Integer;
732 Res
:= SetThreadPriority
733 (T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Underlying_Priorities
(Prio
)));
734 pragma Assert
(Res
= Win32
.TRUE);
736 if Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F' then
738 -- Annex D requirement [RM D.2.2 par. 9]:
739 -- If the task drops its priority due to the loss of inherited
740 -- priority, it is added at the head of the ready queue for its
741 -- new active priority.
743 if Loss_Of_Inheritance
744 and then Prio
< T
.Common
.Current_Priority
746 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
747 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
750 -- Let some processes a chance to arrive
754 -- Then wait for our turn to proceed
756 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
757 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
760 Prio_Array
(T
.Common
.Base_Priority
) :=
761 Prio_Array
(T
.Common
.Base_Priority
) - 1;
765 T
.Common
.Current_Priority
:= Prio
;
772 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
774 return T
.Common
.Current_Priority
;
781 -- There were two paths were we needed to call Enter_Task :
782 -- 1) from System.Task_Primitives.Operations.Initialize
783 -- 2) from System.Tasking.Stages.Task_Wrapper
785 -- The thread initialisation has to be done only for the first case
787 -- This is because the GetCurrentThread NT call does not return the real
788 -- thread handler but only a "pseudo" one. It is not possible to release
789 -- the thread handle and free the system resources from this "pseudo"
790 -- handle. So we really want to keep the real thread handle set in
791 -- System.Task_Primitives.Operations.Create_Task during thread creation.
793 procedure Enter_Task
(Self_ID
: Task_Id
) is
794 procedure Init_Float
;
795 pragma Import
(C
, Init_Float
, "__gnat_init_float");
796 -- Properly initializes the FPU for x86 systems
799 Specific
.Set
(Self_ID
);
802 if Self_ID
.Common
.Task_Info
/= null
804 Self_ID
.Common
.Task_Info
.CPU
>= CPU_Number
(Number_Of_Processors
)
806 raise Invalid_CPU_Number
;
809 Self_ID
.Common
.LL
.Thread_Id
:= GetCurrentThreadId
;
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
: Win32
.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
918 -- Step 2: set its TCB
920 T
.Common
.LL
.Thread
:= hTask
;
922 -- Step 3: set its priority (child has inherited priority from parent)
924 Set_Priority
(T
, Priority
);
926 if Time_Slice_Val
= 0
927 or else Dispatching_Policy
= 'F'
928 or else Get_Policy
(Priority
) = 'F'
930 -- Here we need Annex D semantics so we disable the NT priority
931 -- boost. A priority boost is temporarily given by the system to a
932 -- thread when it is taken out of a wait state.
934 SetThreadPriorityBoost
(hTask
, DisablePriorityBoost
=> Win32
.TRUE);
937 -- Step 4: Handle Task_Info
939 if T
.Common
.Task_Info
/= null then
940 if T
.Common
.Task_Info
.CPU
/= Task_Info
.Any_CPU
then
941 Result
:= SetThreadIdealProcessor
(hTask
, T
.Common
.Task_Info
.CPU
);
942 pragma Assert
(Result
= 1);
946 -- Step 5: Now, start it for good:
948 Result
:= ResumeThread
(hTask
);
949 pragma Assert
(Result
= 1);
951 Succeeded
:= Result
= 1;
958 procedure Finalize_TCB
(T
: Task_Id
) is
959 Self_ID
: Task_Id
:= T
;
962 Is_Self
: constant Boolean := T
= Self
;
964 procedure Free
is new
965 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
968 if not Single_Lock
then
969 Finalize_Lock
(T
.Common
.LL
.L
'Access);
972 Finalize_Cond
(T
.Common
.LL
.CV
'Access);
974 if T
.Known_Tasks_Index
/= -1 then
975 Known_Tasks
(T
.Known_Tasks_Index
) := null;
978 if Self_ID
.Common
.LL
.Thread
/= 0 then
980 -- This task has been activated. Wait for the thread to terminate
981 -- then close it. This is needed to release system resources.
983 Result
:= WaitForSingleObject
(T
.Common
.LL
.Thread
, Wait_Infinite
);
984 pragma Assert
(Result
/= WAIT_FAILED
);
985 Succeeded
:= CloseHandle
(T
.Common
.LL
.Thread
);
986 pragma Assert
(Succeeded
= Win32
.TRUE);
1000 procedure Exit_Task
is
1002 Specific
.Set
(null);
1009 procedure Abort_Task
(T
: Task_Id
) is
1010 pragma Unreferenced
(T
);
1015 ----------------------
1016 -- Environment_Task --
1017 ----------------------
1019 function Environment_Task
return Task_Id
is
1021 return Environment_Task_Id
;
1022 end Environment_Task
;
1028 procedure Lock_RTS
is
1030 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1037 procedure Unlock_RTS
is
1039 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1046 procedure Initialize
(Environment_Task
: Task_Id
) is
1048 pragma Unreferenced
(Discard
);
1051 Environment_Task_Id
:= Environment_Task
;
1052 OS_Primitives
.Initialize
;
1053 Interrupt_Management
.Initialize
;
1055 if Time_Slice_Val
= 0 or else Dispatching_Policy
= 'F' then
1056 -- Here we need Annex D semantics, switch the current process to the
1057 -- Realtime_Priority_Class.
1059 Discard
:= OS_Interface
.SetPriorityClass
1060 (GetCurrentProcess
, Realtime_Priority_Class
);
1065 TlsIndex
:= TlsAlloc
;
1067 -- Initialize the lock used to synchronize chain of all ATCBs
1069 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1071 Environment_Task
.Common
.LL
.Thread
:= GetCurrentThread
;
1073 -- Make environment task known here because it doesn't go through
1074 -- Activate_Tasks, which does it for all other tasks.
1076 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1077 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1079 Enter_Task
(Environment_Task
);
1082 ---------------------
1083 -- Monotonic_Clock --
1084 ---------------------
1086 function Monotonic_Clock
return Duration
1087 renames System
.OS_Primitives
.Monotonic_Clock
;
1093 function RT_Resolution
return Duration is
1095 return 0.000_001
; -- 1 micro-second
1102 procedure Initialize
(S
: in out Suspension_Object
) is
1104 -- Initialize internal state. It is always initialized to False (ARM
1110 -- Initialize internal mutex
1112 InitializeCriticalSection
(S
.L
'Access);
1114 -- Initialize internal condition variable
1116 S
.CV
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
1117 pragma Assert
(S
.CV
/= 0);
1124 procedure Finalize
(S
: in out Suspension_Object
) is
1127 -- Destroy internal mutex
1129 DeleteCriticalSection
(S
.L
'Access);
1131 -- Destroy internal condition variable
1133 Result
:= CloseHandle
(S
.CV
);
1134 pragma Assert
(Result
= Win32
.TRUE);
1141 function Current_State
(S
: Suspension_Object
) return Boolean is
1143 -- We do not want to use lock on this read operation. State is marked
1144 -- as Atomic so that we ensure that the value retrieved is correct.
1153 procedure Set_False
(S
: in out Suspension_Object
) is
1155 SSL
.Abort_Defer
.all;
1157 EnterCriticalSection
(S
.L
'Access);
1161 LeaveCriticalSection
(S
.L
'Access);
1163 SSL
.Abort_Undefer
.all;
1170 procedure Set_True
(S
: in out Suspension_Object
) is
1173 SSL
.Abort_Defer
.all;
1175 EnterCriticalSection
(S
.L
'Access);
1177 -- If there is already a task waiting on this suspension object then
1178 -- we resume it, leaving the state of the suspension object to False,
1179 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1180 -- the state to True.
1186 Result
:= SetEvent
(S
.CV
);
1187 pragma Assert
(Result
= Win32
.TRUE);
1192 LeaveCriticalSection
(S
.L
'Access);
1194 SSL
.Abort_Undefer
.all;
1197 ------------------------
1198 -- Suspend_Until_True --
1199 ------------------------
1201 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1205 SSL
.Abort_Defer
.all;
1207 EnterCriticalSection
(S
.L
'Access);
1210 -- Program_Error must be raised upon calling Suspend_Until_True
1211 -- if another task is already waiting on that suspension object
1212 -- (ARM D.10 par. 10).
1214 LeaveCriticalSection
(S
.L
'Access);
1216 SSL
.Abort_Undefer
.all;
1218 raise Program_Error
;
1220 -- Suspend the task if the state is False. Otherwise, the task
1221 -- continues its execution, and the state of the suspension object
1222 -- is set to False (ARM D.10 par. 9).
1227 LeaveCriticalSection
(S
.L
'Access);
1229 SSL
.Abort_Undefer
.all;
1233 -- Must reset CV BEFORE L is unlocked
1235 Result_Bool
:= ResetEvent
(S
.CV
);
1236 pragma Assert
(Result_Bool
= Win32
.TRUE);
1238 LeaveCriticalSection
(S
.L
'Access);
1240 SSL
.Abort_Undefer
.all;
1242 Result
:= WaitForSingleObject
(S
.CV
, Wait_Infinite
);
1243 pragma Assert
(Result
= 0);
1246 end Suspend_Until_True
;
1252 -- Dummy versions. The only currently working versions is for solaris
1255 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1256 pragma Unreferenced
(Self_ID
);
1261 --------------------
1262 -- Check_No_Locks --
1263 --------------------
1265 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1266 pragma Unreferenced
(Self_ID
);
1275 function Suspend_Task
1277 Thread_Self
: Thread_Id
) return Boolean
1280 if T
.Common
.LL
.Thread
/= Thread_Self
then
1281 return SuspendThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1291 function Resume_Task
1293 Thread_Self
: Thread_Id
) return Boolean
1296 if T
.Common
.LL
.Thread
/= Thread_Self
then
1297 return ResumeThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1303 --------------------
1304 -- Stop_All_Tasks --
1305 --------------------
1307 procedure Stop_All_Tasks
is
1316 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1317 pragma Unreferenced
(T
);
1326 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1327 pragma Unreferenced
(T
);
1332 end System
.Task_Primitives
.Operations
;