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-2008, 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 with
40 -- Turn off polling, we do not want ATC polling to take place during tasking
41 -- operations. It causes infinite loops and other problems.
43 with Ada
.Unchecked_Deallocation
;
46 with Interfaces
.C
.Strings
;
48 with System
.Tasking
.Debug
;
49 with System
.OS_Primitives
;
50 with System
.Task_Info
;
51 with System
.Interrupt_Management
;
52 with System
.Win32
.Ext
;
54 with System
.Soft_Links
;
55 -- We use System.Soft_Links instead of System.Tasking.Initialization because
56 -- the later is a higher level package that we shouldn't depend on. For
57 -- example when using the restricted run time, it is replaced by
58 -- System.Tasking.Restricted.Stages.
60 package body System
.Task_Primitives
.Operations
is
62 package SSL
renames System
.Soft_Links
;
64 use System
.Tasking
.Debug
;
67 use Interfaces
.C
.Strings
;
68 use System
.OS_Interface
;
69 use System
.Parameters
;
70 use System
.OS_Primitives
;
75 pragma Link_With
("-Xlinker --stack=0x200000,0x1000");
76 -- Change the default stack size (2 MB) for tasking programs on Windows.
77 -- This allows about 1000 tasks running at the same time. Note that
78 -- we set the stack size for non tasking programs on System unit.
79 -- Also note that under Windows XP, we use a Windows XP extension to
80 -- specify the stack size on a per task basis, as done under other OSes.
86 procedure InitializeCriticalSection
(pCriticalSection
: access RTS_Lock
);
87 procedure InitializeCriticalSection
88 (pCriticalSection
: access CRITICAL_SECTION
);
90 (Stdcall
, InitializeCriticalSection
, "InitializeCriticalSection");
92 procedure EnterCriticalSection
(pCriticalSection
: access RTS_Lock
);
93 procedure EnterCriticalSection
94 (pCriticalSection
: access CRITICAL_SECTION
);
95 pragma Import
(Stdcall
, EnterCriticalSection
, "EnterCriticalSection");
97 procedure LeaveCriticalSection
(pCriticalSection
: access RTS_Lock
);
98 procedure LeaveCriticalSection
(pCriticalSection
: access CRITICAL_SECTION
);
99 pragma Import
(Stdcall
, LeaveCriticalSection
, "LeaveCriticalSection");
101 procedure DeleteCriticalSection
(pCriticalSection
: access RTS_Lock
);
102 procedure DeleteCriticalSection
103 (pCriticalSection
: access CRITICAL_SECTION
);
104 pragma Import
(Stdcall
, DeleteCriticalSection
, "DeleteCriticalSection");
110 Environment_Task_Id
: Task_Id
;
111 -- A variable to hold Task_Id for the environment task
113 Single_RTS_Lock
: aliased RTS_Lock
;
114 -- This is a lock to allow only one thread of control in the RTS at
115 -- a time; it is used to execute in mutual exclusion from all other tasks.
116 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
118 Time_Slice_Val
: Integer;
119 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
121 Dispatching_Policy
: Character;
122 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
124 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
125 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
126 -- Get priority specific dispatching policy
128 Foreign_Task_Elaborated
: aliased Boolean := True;
129 -- Used to identified fake tasks (i.e., non-Ada Threads)
131 Annex_D
: Boolean := False;
132 -- Set to True if running with Annex-D semantics
134 ------------------------------------
135 -- The thread local storage index --
136 ------------------------------------
139 pragma Export
(Ada
, TlsIndex
);
140 -- To ensure that this variable won't be local to this package, since
141 -- in some cases, inlining forces this variable to be global anyway.
149 function Is_Valid_Task
return Boolean;
150 pragma Inline
(Is_Valid_Task
);
151 -- Does executing thread have a TCB?
153 procedure Set
(Self_Id
: Task_Id
);
155 -- Set the self id for the current task
159 package body Specific
is
161 function Is_Valid_Task
return Boolean is
163 return TlsGetValue
(TlsIndex
) /= System
.Null_Address
;
166 procedure Set
(Self_Id
: Task_Id
) is
169 Succeeded
:= TlsSetValue
(TlsIndex
, To_Address
(Self_Id
));
170 pragma Assert
(Succeeded
= Win32
.TRUE);
175 ---------------------------------
176 -- Support for foreign threads --
177 ---------------------------------
179 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
180 -- Allocate and Initialize a new ATCB for the current Thread
182 function Register_Foreign_Thread
183 (Thread
: Thread_Id
) return Task_Id
is separate;
185 ----------------------------------
186 -- Condition Variable Functions --
187 ----------------------------------
189 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
);
190 -- Initialize given condition variable Cond
192 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
);
193 -- Finalize given condition variable Cond
195 procedure Cond_Signal
(Cond
: not null access Condition_Variable
);
196 -- Signal condition variable Cond
199 (Cond
: not null access Condition_Variable
;
200 L
: not null access RTS_Lock
);
201 -- Wait on conditional variable Cond, using lock L
203 procedure Cond_Timed_Wait
204 (Cond
: not null access Condition_Variable
;
205 L
: not null access RTS_Lock
;
207 Timed_Out
: out Boolean;
208 Status
: out Integer);
209 -- Do timed wait on condition variable Cond using lock L. The duration
210 -- of the timed wait is given by Rel_Time. When the condition is
211 -- signalled, Timed_Out shows whether or not a time out occurred.
212 -- Status is only valid if Timed_Out is False, in which case it
213 -- shows whether Cond_Timed_Wait completed successfully.
215 ---------------------
216 -- Initialize_Cond --
217 ---------------------
219 procedure Initialize_Cond
(Cond
: not null access Condition_Variable
) is
222 hEvent
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
223 pragma Assert
(hEvent
/= 0);
224 Cond
.all := Condition_Variable
(hEvent
);
231 -- No such problem here, DosCloseEventSem has been derived.
232 -- What does such refer to in above comment???
234 procedure Finalize_Cond
(Cond
: not null access Condition_Variable
) is
237 Result
:= CloseHandle
(HANDLE
(Cond
.all));
238 pragma Assert
(Result
= Win32
.TRUE);
245 procedure Cond_Signal
(Cond
: not null access Condition_Variable
) is
248 Result
:= SetEvent
(HANDLE
(Cond
.all));
249 pragma Assert
(Result
= Win32
.TRUE);
256 -- Pre-condition: Cond is posted
259 -- Post-condition: Cond is posted
263 (Cond
: not null access Condition_Variable
;
264 L
: not null access RTS_Lock
)
270 -- Must reset Cond BEFORE L is unlocked
272 Result_Bool
:= ResetEvent
(HANDLE
(Cond
.all));
273 pragma Assert
(Result_Bool
= Win32
.TRUE);
274 Unlock
(L
, Global_Lock
=> True);
276 -- No problem if we are interrupted here: if the condition is signaled,
277 -- WaitForSingleObject will simply not block
279 Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Wait_Infinite
);
280 pragma Assert
(Result
= 0);
282 Write_Lock
(L
, Global_Lock
=> True);
285 ---------------------
286 -- Cond_Timed_Wait --
287 ---------------------
289 -- Pre-condition: Cond is posted
292 -- Post-condition: Cond is posted
295 procedure Cond_Timed_Wait
296 (Cond
: not null access Condition_Variable
;
297 L
: not null access RTS_Lock
;
299 Timed_Out
: out Boolean;
300 Status
: out Integer)
302 Time_Out_Max
: constant DWORD
:= 16#FFFF0000#
;
303 -- NT 4 can't handle excessive timeout values (e.g. DWORD'Last - 1)
310 -- Must reset Cond BEFORE L is unlocked
312 Result
:= ResetEvent
(HANDLE
(Cond
.all));
313 pragma Assert
(Result
= Win32
.TRUE);
314 Unlock
(L
, Global_Lock
=> True);
316 -- No problem if we are interrupted here: if the condition is signaled,
317 -- WaitForSingleObject will simply not block
319 if Rel_Time
<= 0.0 then
324 if Rel_Time
>= Duration (Time_Out_Max
) / 1000 then
325 Time_Out
:= Time_Out_Max
;
327 Time_Out
:= DWORD
(Rel_Time
* 1000);
330 Wait_Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Time_Out
);
332 if Wait_Result
= WAIT_TIMEOUT
then
340 Write_Lock
(L
, Global_Lock
=> True);
342 -- Ensure post-condition
345 Result
:= SetEvent
(HANDLE
(Cond
.all));
346 pragma Assert
(Result
= Win32
.TRUE);
349 Status
:= Integer (Wait_Result
);
356 -- The underlying thread system sets a guard page at the bottom of a thread
357 -- stack, so nothing is needed.
358 -- ??? Check the comment above
360 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
361 pragma Unreferenced
(T
, On
);
370 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
372 return T
.Common
.LL
.Thread
;
379 function Self
return Task_Id
is
380 Self_Id
: constant Task_Id
:= To_Task_Id
(TlsGetValue
(TlsIndex
));
382 if Self_Id
= null then
383 return Register_Foreign_Thread
(GetCurrentThread
);
389 ---------------------
390 -- Initialize_Lock --
391 ---------------------
393 -- Note: mutexes and cond_variables needed per-task basis are initialized
394 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
395 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
396 -- status change of RTS. Therefore raising Storage_Error in the following
397 -- routines should be able to be handled safely.
399 procedure Initialize_Lock
400 (Prio
: System
.Any_Priority
;
401 L
: not null access Lock
)
404 InitializeCriticalSection
(L
.Mutex
'Access);
405 L
.Owner_Priority
:= 0;
409 procedure Initialize_Lock
410 (L
: not null access RTS_Lock
; Level
: Lock_Level
)
412 pragma Unreferenced
(Level
);
414 InitializeCriticalSection
(L
);
421 procedure Finalize_Lock
(L
: not null access Lock
) is
423 DeleteCriticalSection
(L
.Mutex
'Access);
426 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
428 DeleteCriticalSection
(L
);
436 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
438 L
.Owner_Priority
:= Get_Priority
(Self
);
440 if L
.Priority
< L
.Owner_Priority
then
441 Ceiling_Violation
:= True;
445 EnterCriticalSection
(L
.Mutex
'Access);
447 Ceiling_Violation
:= False;
451 (L
: not null access RTS_Lock
;
452 Global_Lock
: Boolean := False)
455 if not Single_Lock
or else Global_Lock
then
456 EnterCriticalSection
(L
);
460 procedure Write_Lock
(T
: Task_Id
) is
462 if not Single_Lock
then
463 EnterCriticalSection
(T
.Common
.LL
.L
'Access);
472 (L
: not null access Lock
; Ceiling_Violation
: out Boolean) is
474 Write_Lock
(L
, Ceiling_Violation
);
481 procedure Unlock
(L
: not null access Lock
) is
483 LeaveCriticalSection
(L
.Mutex
'Access);
487 (L
: not null access RTS_Lock
; Global_Lock
: Boolean := False) is
489 if not Single_Lock
or else Global_Lock
then
490 LeaveCriticalSection
(L
);
494 procedure Unlock
(T
: Task_Id
) is
496 if not Single_Lock
then
497 LeaveCriticalSection
(T
.Common
.LL
.L
'Access);
505 -- Dynamic priority ceilings are not supported by the underlying system
507 procedure Set_Ceiling
508 (L
: not null access Lock
;
509 Prio
: System
.Any_Priority
)
511 pragma Unreferenced
(L
, Prio
);
522 Reason
: System
.Tasking
.Task_States
)
524 pragma Unreferenced
(Reason
);
527 pragma Assert
(Self_ID
= Self
);
530 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
532 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
535 if Self_ID
.Deferral_Level
= 0
536 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
539 raise Standard
'Abort_Signal;
547 -- This is for use within the run-time system, so abort is assumed to be
548 -- already deferred, and the caller should be holding its own ATCB lock.
550 procedure Timed_Sleep
553 Mode
: ST
.Delay_Modes
;
554 Reason
: System
.Tasking
.Task_States
;
555 Timedout
: out Boolean;
556 Yielded
: out Boolean)
558 pragma Unreferenced
(Reason
);
559 Check_Time
: Duration := Monotonic_Clock
;
564 pragma Unreferenced
(Result
);
566 Local_Timedout
: Boolean;
572 if Mode
= Relative
then
574 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
576 Rel_Time
:= Time
- Check_Time
;
580 if Rel_Time
> 0.0 then
582 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
586 (Self_ID
.Common
.LL
.CV
'Access,
587 Single_RTS_Lock
'Access,
588 Rel_Time
, Local_Timedout
, Result
);
591 (Self_ID
.Common
.LL
.CV
'Access,
592 Self_ID
.Common
.LL
.L
'Access,
593 Rel_Time
, Local_Timedout
, Result
);
596 Check_Time
:= Monotonic_Clock
;
597 exit when Abs_Time
<= Check_Time
;
599 if not Local_Timedout
then
601 -- Somebody may have called Wakeup for us
607 Rel_Time
:= Abs_Time
- Check_Time
;
616 procedure Timed_Delay
619 Mode
: ST
.Delay_Modes
)
621 Check_Time
: Duration := Monotonic_Clock
;
627 pragma Unreferenced
(Timedout
, Result
);
634 Write_Lock
(Self_ID
);
636 if Mode
= Relative
then
638 Abs_Time
:= Time
+ Check_Time
;
640 Rel_Time
:= Time
- Check_Time
;
644 if Rel_Time
> 0.0 then
645 Self_ID
.Common
.State
:= Delay_Sleep
;
648 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
652 (Self_ID
.Common
.LL
.CV
'Access,
653 Single_RTS_Lock
'Access,
654 Rel_Time
, Timedout
, Result
);
657 (Self_ID
.Common
.LL
.CV
'Access,
658 Self_ID
.Common
.LL
.L
'Access,
659 Rel_Time
, Timedout
, Result
);
662 Check_Time
:= Monotonic_Clock
;
663 exit when Abs_Time
<= Check_Time
;
665 Rel_Time
:= Abs_Time
- Check_Time
;
668 Self_ID
.Common
.State
:= Runnable
;
684 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
685 pragma Unreferenced
(Reason
);
687 Cond_Signal
(T
.Common
.LL
.CV
'Access);
694 procedure Yield
(Do_Yield
: Boolean := True) is
700 -- If running with Annex-D semantics we need a delay
701 -- above 0 milliseconds here otherwise processes give
702 -- enough time to the other tasks to have a chance to
705 -- This makes cxd8002 ACATS pass on Windows.
715 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
716 pragma Atomic_Components
(Prio_Array_Type
);
718 Prio_Array
: Prio_Array_Type
;
719 -- Global array containing the id of the currently running task for
722 -- Note: we assume that we are on a single processor with run-til-blocked
725 procedure Set_Priority
727 Prio
: System
.Any_Priority
;
728 Loss_Of_Inheritance
: Boolean := False)
731 Array_Item
: Integer;
734 Res
:= SetThreadPriority
735 (T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Underlying_Priorities
(Prio
)));
736 pragma Assert
(Res
= Win32
.TRUE);
738 if Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F' then
740 -- Annex D requirement [RM D.2.2 par. 9]:
741 -- If the task drops its priority due to the loss of inherited
742 -- priority, it is added at the head of the ready queue for its
743 -- new active priority.
745 if Loss_Of_Inheritance
746 and then Prio
< T
.Common
.Current_Priority
748 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
749 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
752 -- Let some processes a chance to arrive
756 -- Then wait for our turn to proceed
758 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
759 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
762 Prio_Array
(T
.Common
.Base_Priority
) :=
763 Prio_Array
(T
.Common
.Base_Priority
) - 1;
767 T
.Common
.Current_Priority
:= Prio
;
774 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
776 return T
.Common
.Current_Priority
;
783 -- There were two paths were we needed to call Enter_Task :
784 -- 1) from System.Task_Primitives.Operations.Initialize
785 -- 2) from System.Tasking.Stages.Task_Wrapper
787 -- The thread initialisation has to be done only for the first case
789 -- This is because the GetCurrentThread NT call does not return the real
790 -- thread handler but only a "pseudo" one. It is not possible to release
791 -- the thread handle and free the system resources from this "pseudo"
792 -- handle. So we really want to keep the real thread handle set in
793 -- System.Task_Primitives.Operations.Create_Task during thread creation.
795 procedure Enter_Task
(Self_ID
: Task_Id
) is
796 procedure Init_Float
;
797 pragma Import
(C
, Init_Float
, "__gnat_init_float");
798 -- Properly initializes the FPU for x86 systems
801 Specific
.Set
(Self_ID
);
804 if Self_ID
.Common
.Task_Info
/= null
806 Self_ID
.Common
.Task_Info
.CPU
>= CPU_Number
(Number_Of_Processors
)
808 raise Invalid_CPU_Number
;
811 Self_ID
.Common
.LL
.Thread_Id
:= GetCurrentThreadId
;
815 for J
in Known_Tasks
'Range loop
816 if Known_Tasks
(J
) = null then
817 Known_Tasks
(J
) := Self_ID
;
818 Self_ID
.Known_Tasks_Index
:= J
;
830 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
832 return new Ada_Task_Control_Block
(Entry_Num
);
839 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
841 -----------------------------
842 -- Register_Foreign_Thread --
843 -----------------------------
845 function Register_Foreign_Thread
return Task_Id
is
847 if Is_Valid_Task
then
850 return Register_Foreign_Thread
(GetCurrentThread
);
852 end Register_Foreign_Thread
;
858 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
860 -- Initialize thread ID to 0, this is needed to detect threads that
861 -- are not yet activated.
863 Self_ID
.Common
.LL
.Thread
:= 0;
865 Initialize_Cond
(Self_ID
.Common
.LL
.CV
'Access);
867 if not Single_Lock
then
868 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
878 procedure Create_Task
880 Wrapper
: System
.Address
;
881 Stack_Size
: System
.Parameters
.Size_Type
;
882 Priority
: System
.Any_Priority
;
883 Succeeded
: out Boolean)
885 Initial_Stack_Size
: constant := 1024;
886 -- We set the initial stack size to 1024. On Windows version prior to XP
887 -- there is no way to fix a task stack size. Only the initial stack size
888 -- can be set, the operating system will raise the task stack size if
891 function Is_Windows_XP
return Integer;
892 pragma Import
(C
, Is_Windows_XP
, "__gnat_is_windows_xp");
893 -- Returns 1 if running on Windows XP
896 TaskId
: aliased DWORD
;
897 pTaskParameter
: Win32
.PVOID
;
899 Entry_Point
: PTHREAD_START_ROUTINE
;
902 pTaskParameter
:= To_Address
(T
);
904 Entry_Point
:= To_PTHREAD_START_ROUTINE
(Wrapper
);
906 if Is_Windows_XP
= 1 then
907 hTask
:= CreateThread
912 DWORD
(Create_Suspended
) or
913 DWORD
(Stack_Size_Param_Is_A_Reservation
),
914 TaskId
'Unchecked_Access);
916 hTask
:= CreateThread
921 DWORD
(Create_Suspended
),
922 TaskId
'Unchecked_Access);
925 -- Step 1: Create the thread in blocked mode
932 -- Step 2: set its TCB
934 T
.Common
.LL
.Thread
:= hTask
;
936 -- Step 3: set its priority (child has inherited priority from parent)
938 Set_Priority
(T
, Priority
);
940 if Time_Slice_Val
= 0
941 or else Dispatching_Policy
= 'F'
942 or else Get_Policy
(Priority
) = 'F'
944 -- Here we need Annex D semantics so we disable the NT priority
945 -- boost. A priority boost is temporarily given by the system to a
946 -- thread when it is taken out of a wait state.
948 SetThreadPriorityBoost
(hTask
, DisablePriorityBoost
=> Win32
.TRUE);
951 -- Step 4: Handle Task_Info
953 if T
.Common
.Task_Info
/= null then
954 if T
.Common
.Task_Info
.CPU
/= Task_Info
.Any_CPU
then
955 Result
:= SetThreadIdealProcessor
(hTask
, T
.Common
.Task_Info
.CPU
);
956 pragma Assert
(Result
= 1);
960 -- Step 5: Now, start it for good:
962 Result
:= ResumeThread
(hTask
);
963 pragma Assert
(Result
= 1);
965 Succeeded
:= Result
= 1;
972 procedure Finalize_TCB
(T
: Task_Id
) is
973 Self_ID
: Task_Id
:= T
;
976 Is_Self
: constant Boolean := T
= Self
;
978 procedure Free
is new
979 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
982 if not Single_Lock
then
983 Finalize_Lock
(T
.Common
.LL
.L
'Access);
986 Finalize_Cond
(T
.Common
.LL
.CV
'Access);
988 if T
.Known_Tasks_Index
/= -1 then
989 Known_Tasks
(T
.Known_Tasks_Index
) := null;
992 if Self_ID
.Common
.LL
.Thread
/= 0 then
994 -- This task has been activated. Wait for the thread to terminate
995 -- then close it. This is needed to release system resources.
997 Result
:= WaitForSingleObject
(T
.Common
.LL
.Thread
, Wait_Infinite
);
998 pragma Assert
(Result
/= WAIT_FAILED
);
999 Succeeded
:= CloseHandle
(T
.Common
.LL
.Thread
);
1000 pragma Assert
(Succeeded
= Win32
.TRUE);
1006 Specific
.Set
(null);
1014 procedure Exit_Task
is
1016 Specific
.Set
(null);
1023 procedure Abort_Task
(T
: Task_Id
) is
1024 pragma Unreferenced
(T
);
1029 ----------------------
1030 -- Environment_Task --
1031 ----------------------
1033 function Environment_Task
return Task_Id
is
1035 return Environment_Task_Id
;
1036 end Environment_Task
;
1042 procedure Lock_RTS
is
1044 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1051 procedure Unlock_RTS
is
1053 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1060 procedure Initialize
(Environment_Task
: Task_Id
) is
1062 pragma Unreferenced
(Discard
);
1065 Environment_Task_Id
:= Environment_Task
;
1066 OS_Primitives
.Initialize
;
1067 Interrupt_Management
.Initialize
;
1069 if Time_Slice_Val
= 0 or else Dispatching_Policy
= 'F' then
1070 -- Here we need Annex D semantics, switch the current process to the
1071 -- Realtime_Priority_Class.
1073 Discard
:= OS_Interface
.SetPriorityClass
1074 (GetCurrentProcess
, Realtime_Priority_Class
);
1079 TlsIndex
:= TlsAlloc
;
1081 -- Initialize the lock used to synchronize chain of all ATCBs
1083 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1085 Environment_Task
.Common
.LL
.Thread
:= GetCurrentThread
;
1086 Enter_Task
(Environment_Task
);
1089 ---------------------
1090 -- Monotonic_Clock --
1091 ---------------------
1093 function Monotonic_Clock
return Duration
1094 renames System
.OS_Primitives
.Monotonic_Clock
;
1100 function RT_Resolution
return Duration is
1102 return 0.000_001
; -- 1 micro-second
1109 procedure Initialize
(S
: in out Suspension_Object
) is
1111 -- Initialize internal state. It is always initialized to False (ARM
1117 -- Initialize internal mutex
1119 InitializeCriticalSection
(S
.L
'Access);
1121 -- Initialize internal condition variable
1123 S
.CV
:= CreateEvent
(null, Win32
.TRUE, Win32
.FALSE, Null_Ptr
);
1124 pragma Assert
(S
.CV
/= 0);
1131 procedure Finalize
(S
: in out Suspension_Object
) is
1134 -- Destroy internal mutex
1136 DeleteCriticalSection
(S
.L
'Access);
1138 -- Destroy internal condition variable
1140 Result
:= CloseHandle
(S
.CV
);
1141 pragma Assert
(Result
= Win32
.TRUE);
1148 function Current_State
(S
: Suspension_Object
) return Boolean is
1150 -- We do not want to use lock on this read operation. State is marked
1151 -- as Atomic so that we ensure that the value retrieved is correct.
1160 procedure Set_False
(S
: in out Suspension_Object
) is
1162 SSL
.Abort_Defer
.all;
1164 EnterCriticalSection
(S
.L
'Access);
1168 LeaveCriticalSection
(S
.L
'Access);
1170 SSL
.Abort_Undefer
.all;
1177 procedure Set_True
(S
: in out Suspension_Object
) is
1180 SSL
.Abort_Defer
.all;
1182 EnterCriticalSection
(S
.L
'Access);
1184 -- If there is already a task waiting on this suspension object then
1185 -- we resume it, leaving the state of the suspension object to False,
1186 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1187 -- the state to True.
1193 Result
:= SetEvent
(S
.CV
);
1194 pragma Assert
(Result
= Win32
.TRUE);
1199 LeaveCriticalSection
(S
.L
'Access);
1201 SSL
.Abort_Undefer
.all;
1204 ------------------------
1205 -- Suspend_Until_True --
1206 ------------------------
1208 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1212 SSL
.Abort_Defer
.all;
1214 EnterCriticalSection
(S
.L
'Access);
1217 -- Program_Error must be raised upon calling Suspend_Until_True
1218 -- if another task is already waiting on that suspension object
1219 -- (ARM D.10 par. 10).
1221 LeaveCriticalSection
(S
.L
'Access);
1223 SSL
.Abort_Undefer
.all;
1225 raise Program_Error
;
1227 -- Suspend the task if the state is False. Otherwise, the task
1228 -- continues its execution, and the state of the suspension object
1229 -- is set to False (ARM D.10 par. 9).
1234 LeaveCriticalSection
(S
.L
'Access);
1236 SSL
.Abort_Undefer
.all;
1240 -- Must reset CV BEFORE L is unlocked
1242 Result_Bool
:= ResetEvent
(S
.CV
);
1243 pragma Assert
(Result_Bool
= Win32
.TRUE);
1245 LeaveCriticalSection
(S
.L
'Access);
1247 SSL
.Abort_Undefer
.all;
1249 Result
:= WaitForSingleObject
(S
.CV
, Wait_Infinite
);
1250 pragma Assert
(Result
= 0);
1253 end Suspend_Until_True
;
1259 -- Dummy versions. The only currently working versions is for solaris
1262 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1263 pragma Unreferenced
(Self_ID
);
1268 --------------------
1269 -- Check_No_Locks --
1270 --------------------
1272 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1273 pragma Unreferenced
(Self_ID
);
1282 function Suspend_Task
1284 Thread_Self
: Thread_Id
) return Boolean
1287 if T
.Common
.LL
.Thread
/= Thread_Self
then
1288 return SuspendThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1298 function Resume_Task
1300 Thread_Self
: Thread_Id
) return Boolean
1303 if T
.Common
.LL
.Thread
/= Thread_Self
then
1304 return ResumeThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1310 --------------------
1311 -- Stop_All_Tasks --
1312 --------------------
1314 procedure Stop_All_Tasks
is
1323 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1324 pragma Unreferenced
(T
);
1333 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1334 pragma Unreferenced
(T
);
1339 end System
.Task_Primitives
.Operations
;