1 ------------------------------------------------------------------------------
3 -- GNU ADA 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 --
10 -- Copyright (C) 1992-2002, Free Software Foundation, Inc. --
12 -- GNARL is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNARL; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- As a special exception, if other files instantiate generics from this --
24 -- unit, or you link this unit with other files to produce an executable, --
25 -- this unit does not by itself cause the resulting executable to be --
26 -- covered by the GNU General Public License. This exception does not --
27 -- however invalidate any other reasons why the executable file might be --
28 -- covered by the GNU Public License. --
30 -- GNARL was developed by the GNARL team at Florida State University. --
31 -- Extensive contributions were provided by Ada Core Technologies Inc. --
33 ------------------------------------------------------------------------------
35 -- This is a NT (native) version of this package.
37 -- This package contains all the GNULL primitives that interface directly
38 -- with the underlying OS.
41 -- Turn off polling, we do not want ATC polling to take place during
42 -- tasking operations. It causes infinite loops and other problems.
44 with System
.Tasking
.Debug
;
45 -- used for Known_Tasks
51 with Interfaces
.C
.Strings
;
54 with System
.OS_Interface
;
55 -- used for various type, constant, and operations
57 with System
.Parameters
;
61 -- used for Ada_Task_Control_Block
64 with System
.Soft_Links
;
65 -- used for Defer/Undefer_Abort
66 -- to initialize TSD for a C thread, in function Self
68 -- Note that we do not use System.Tasking.Initialization directly since
69 -- this is a higher level package that we shouldn't depend on. For example
70 -- when using the restricted run time, it is replaced by
71 -- System.Tasking.Restricted.Initialization
73 with System
.OS_Primitives
;
74 -- used for Delay_Modes
76 with System
.Task_Info
;
77 -- used for Unspecified_Task_Info
79 with Unchecked_Conversion
;
80 with Unchecked_Deallocation
;
82 package body System
.Task_Primitives
.Operations
is
84 use System
.Tasking
.Debug
;
87 use Interfaces
.C
.Strings
;
88 use System
.OS_Interface
;
89 use System
.Parameters
;
90 use System
.OS_Primitives
;
92 pragma Link_With
("-Xlinker --stack=0x800000,0x1000");
93 -- Change the stack size (8 MB) for tasking programs on Windows. This
94 -- permit to have more than 30 tasks running at the same time. Note that
95 -- we set the stack size for non tasking programs on System unit.
97 package SSL
renames System
.Soft_Links
;
103 Environment_Task_ID
: Task_ID
;
104 -- A variable to hold Task_ID for the environment task.
106 Single_RTS_Lock
: aliased RTS_Lock
;
107 -- This is a lock to allow only one thread of control in the RTS at
108 -- a time; it is used to execute in mutual exclusion from all other tasks.
109 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
111 Time_Slice_Val
: Integer;
112 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
114 Dispatching_Policy
: Character;
115 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
117 FIFO_Within_Priorities
: constant Boolean := Dispatching_Policy
= 'F';
118 -- Indicates whether FIFO_Within_Priorities is set.
120 ---------------------------------
121 -- Foreign Threads Detection --
122 ---------------------------------
124 -- The following are used to allow the Self function to
125 -- automatically generate ATCB's for C threads that happen to call
126 -- Ada procedure, which in turn happen to call the Ada run-time system.
129 type Fake_ATCB_Ptr
is access Fake_ATCB
;
130 type Fake_ATCB
is record
131 Stack_Base
: Interfaces
.C
.unsigned
:= 0;
132 -- A value of zero indicates the node is not in use.
133 Next
: Fake_ATCB_Ptr
;
134 Real_ATCB
: aliased Ada_Task_Control_Block
(0);
137 Fake_ATCB_List
: Fake_ATCB_Ptr
;
138 -- A linear linked list.
139 -- The list is protected by Single_RTS_Lock;
140 -- Nodes are added to this list from the front.
141 -- Once a node is added to this list, it is never removed.
143 Fake_Task_Elaborated
: aliased Boolean := True;
144 -- Used to identified fake tasks (i.e., non-Ada Threads).
146 Next_Fake_ATCB
: Fake_ATCB_Ptr
;
147 -- Used to allocate one Fake_ATCB in advance. See comment in New_Fake_ATCB
149 ---------------------------------
150 -- Support for New_Fake_ATCB --
151 ---------------------------------
153 function New_Fake_ATCB
return Task_ID
;
154 -- Allocate and Initialize a new ATCB. This code can safely be called from
155 -- a foreign thread, as it doesn't access implicitly or explicitly
156 -- "self" before having initialized the new ATCB.
158 ------------------------------------
159 -- The thread local storage index --
160 ------------------------------------
163 pragma Export
(Ada
, TlsIndex
);
164 -- To ensure that this variable won't be local to this package, since
165 -- in some cases, inlining forces this variable to be global anyway.
167 ----------------------------------
168 -- Utility Conversion Functions --
169 ----------------------------------
171 function To_Task_Id
is new Unchecked_Conversion
(System
.Address
, Task_ID
);
173 function To_Address
is new Unchecked_Conversion
(Task_ID
, System
.Address
);
179 function New_Fake_ATCB
return Task_ID
is
181 P
, Q
: Fake_ATCB_Ptr
;
186 -- This section is ticklish.
187 -- We dare not call anything that might require an ATCB, until
188 -- we have the new ATCB in place.
195 if P
.Stack_Base
= 0 then
204 -- Create a new ATCB with zero entries.
206 Self_ID
:= Next_Fake_ATCB
.Real_ATCB
'Access;
207 Next_Fake_ATCB
.Stack_Base
:= 1;
208 Next_Fake_ATCB
.Next
:= Fake_ATCB_List
;
209 Fake_ATCB_List
:= Next_Fake_ATCB
;
210 Next_Fake_ATCB
:= null;
213 -- Reuse an existing fake ATCB.
215 Self_ID
:= Q
.Real_ATCB
'Access;
219 -- Record this as the Task_ID for the current thread.
221 Self_ID
.Common
.LL
.Thread
:= GetCurrentThread
;
223 Res
:= TlsSetValue
(TlsIndex
, To_Address
(Self_ID
));
224 pragma Assert
(Res
= True);
226 -- Do the standard initializations
228 System
.Tasking
.Initialize_ATCB
229 (Self_ID
, null, Null_Address
, Null_Task
, Fake_Task_Elaborated
'Access,
230 System
.Priority
'First, Task_Info
.Unspecified_Task_Info
, 0, Self_ID
,
232 pragma Assert
(Succeeded
);
234 -- Finally, it is safe to use an allocator in this thread.
236 if Next_Fake_ATCB
= null then
237 Next_Fake_ATCB
:= new Fake_ATCB
;
240 Self_ID
.Master_of_Task
:= 0;
241 Self_ID
.Master_Within
:= Self_ID
.Master_of_Task
+ 1;
243 for L
in Self_ID
.Entry_Calls
'Range loop
244 Self_ID
.Entry_Calls
(L
).Self
:= Self_ID
;
245 Self_ID
.Entry_Calls
(L
).Level
:= L
;
248 Self_ID
.Common
.State
:= Runnable
;
249 Self_ID
.Awake_Count
:= 1;
251 -- Since this is not an ordinary Ada task, we will start out undeferred
253 Self_ID
.Deferral_Level
:= 0;
255 System
.Soft_Links
.Create_TSD
(Self_ID
.Common
.Compiler_Data
);
258 -- The following call is commented out to avoid dependence on
259 -- the System.Tasking.Initialization package.
260 -- It seems that if we want Ada.Task_Attributes to work correctly
261 -- for C threads we will need to raise the visibility of this soft
262 -- link to System.Soft_Links.
263 -- We are putting that off until this new functionality is otherwise
265 -- System.Tasking.Initialization.Initialize_Attributes_Link.all (T);
267 -- Must not unlock until Next_ATCB is again allocated.
273 ----------------------------------
274 -- Condition Variable Functions --
275 ----------------------------------
277 procedure Initialize_Cond
(Cond
: access Condition_Variable
);
278 -- Initialize given condition variable Cond
280 procedure Finalize_Cond
(Cond
: access Condition_Variable
);
281 -- Finalize given condition variable Cond.
283 procedure Cond_Signal
(Cond
: access Condition_Variable
);
284 -- Signal condition variable Cond
287 (Cond
: access Condition_Variable
;
288 L
: access RTS_Lock
);
289 -- Wait on conditional variable Cond, using lock L
291 procedure Cond_Timed_Wait
292 (Cond
: access Condition_Variable
;
295 Timed_Out
: out Boolean;
296 Status
: out Integer);
297 -- Do timed wait on condition variable Cond using lock L. The duration
298 -- of the timed wait is given by Rel_Time. When the condition is
299 -- signalled, Timed_Out shows whether or not a time out occurred.
300 -- Status shows whether Cond_Timed_Wait completed successfully.
302 ---------------------
303 -- Initialize_Cond --
304 ---------------------
306 procedure Initialize_Cond
(Cond
: access Condition_Variable
) is
310 hEvent
:= CreateEvent
(null, True, False, Null_Ptr
);
311 pragma Assert
(hEvent
/= 0);
312 Cond
.all := Condition_Variable
(hEvent
);
319 -- No such problem here, DosCloseEventSem has been derived.
320 -- What does such refer to in above comment???
322 procedure Finalize_Cond
(Cond
: access Condition_Variable
) is
326 Result
:= CloseHandle
(HANDLE
(Cond
.all));
327 pragma Assert
(Result
= True);
334 procedure Cond_Signal
(Cond
: access Condition_Variable
) is
338 Result
:= SetEvent
(HANDLE
(Cond
.all));
339 pragma Assert
(Result
= True);
346 -- Pre-assertion: Cond is posted
349 -- Post-assertion: Cond is posted
353 (Cond
: access Condition_Variable
;
360 -- Must reset Cond BEFORE L is unlocked.
362 Result_Bool
:= ResetEvent
(HANDLE
(Cond
.all));
363 pragma Assert
(Result_Bool
= True);
366 -- No problem if we are interrupted here: if the condition is signaled,
367 -- WaitForSingleObject will simply not block
369 Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Wait_Infinite
);
370 pragma Assert
(Result
= 0);
375 ---------------------
376 -- Cond_Timed_Wait --
377 ---------------------
379 -- Pre-assertion: Cond is posted
382 -- Post-assertion: Cond is posted
385 procedure Cond_Timed_Wait
386 (Cond
: access Condition_Variable
;
389 Timed_Out
: out Boolean;
390 Status
: out Integer)
395 Int_Rel_Time
: DWORD
;
399 -- Must reset Cond BEFORE L is unlocked.
401 Result
:= ResetEvent
(HANDLE
(Cond
.all));
402 pragma Assert
(Result
= True);
405 -- No problem if we are interrupted here: if the condition is signaled,
406 -- WaitForSingleObject will simply not block
408 if Rel_Time
<= 0.0 then
411 Int_Rel_Time
:= DWORD
(Rel_Time
);
412 Time_Out
:= Int_Rel_Time
* 1000 +
413 DWORD
((Rel_Time
- Duration (Int_Rel_Time
)) * 1000.0);
414 Wait_Result
:= WaitForSingleObject
(HANDLE
(Cond
.all), Time_Out
);
416 if Wait_Result
= WAIT_TIMEOUT
then
426 -- Ensure post-condition
429 Result
:= SetEvent
(HANDLE
(Cond
.all));
430 pragma Assert
(Result
= True);
433 Status
:= Integer (Wait_Result
);
440 -- The underlying thread system sets a guard page at the
441 -- bottom of a thread stack, so nothing is needed.
442 -- ??? Check the comment above
444 procedure Stack_Guard
(T
: ST
.Task_ID
; On
: Boolean) is
453 function Get_Thread_Id
(T
: ST
.Task_ID
) return OSI
.Thread_Id
is
455 return T
.Common
.LL
.Thread
;
462 function Self
return Task_ID
is
466 Self_Id
:= To_Task_Id
(TlsGetValue
(TlsIndex
));
468 if Self_Id
= null then
469 return New_Fake_ATCB
;
475 ---------------------
476 -- Initialize_Lock --
477 ---------------------
479 -- Note: mutexes and cond_variables needed per-task basis are
480 -- initialized in Initialize_TCB and the Storage_Error is handled.
481 -- Other mutexes (such as RTS_Lock, Memory_Lock...) used in
482 -- the RTS is initialized before any status change of RTS.
483 -- Therefore raising Storage_Error in the following routines
484 -- should be able to be handled safely.
486 procedure Initialize_Lock
487 (Prio
: System
.Any_Priority
;
490 InitializeCriticalSection
(L
.Mutex
'Access);
491 L
.Owner_Priority
:= 0;
495 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
497 InitializeCriticalSection
(CRITICAL_SECTION
(L
.all)'Unrestricted_Access);
504 procedure Finalize_Lock
(L
: access Lock
) is
506 DeleteCriticalSection
(L
.Mutex
'Access);
509 procedure Finalize_Lock
(L
: access RTS_Lock
) is
511 DeleteCriticalSection
(CRITICAL_SECTION
(L
.all)'Unrestricted_Access);
518 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
520 L
.Owner_Priority
:= Get_Priority
(Self
);
522 if L
.Priority
< L
.Owner_Priority
then
523 Ceiling_Violation
:= True;
527 EnterCriticalSection
(L
.Mutex
'Access);
529 Ceiling_Violation
:= False;
533 (L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
535 if not Single_Lock
or else Global_Lock
then
536 EnterCriticalSection
(CRITICAL_SECTION
(L
.all)'Unrestricted_Access);
540 procedure Write_Lock
(T
: Task_ID
) is
542 if not Single_Lock
then
544 (CRITICAL_SECTION
(T
.Common
.LL
.L
)'Unrestricted_Access);
552 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
554 Write_Lock
(L
, Ceiling_Violation
);
561 procedure Unlock
(L
: access Lock
) is
563 LeaveCriticalSection
(L
.Mutex
'Access);
566 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
568 if not Single_Lock
or else Global_Lock
then
569 LeaveCriticalSection
(CRITICAL_SECTION
(L
.all)'Unrestricted_Access);
573 procedure Unlock
(T
: Task_ID
) is
575 if not Single_Lock
then
577 (CRITICAL_SECTION
(T
.Common
.LL
.L
)'Unrestricted_Access);
587 Reason
: System
.Tasking
.Task_States
) is
589 pragma Assert
(Self_ID
= Self
);
592 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Single_RTS_Lock
'Access);
594 Cond_Wait
(Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
'Access);
597 if Self_ID
.Deferral_Level
= 0
598 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
601 raise Standard
'Abort_Signal;
609 -- This is for use within the run-time system, so abort is
610 -- assumed to be already deferred, and the caller should be
611 -- holding its own ATCB lock.
613 procedure Timed_Sleep
616 Mode
: ST
.Delay_Modes
;
617 Reason
: System
.Tasking
.Task_States
;
618 Timedout
: out Boolean;
619 Yielded
: out Boolean)
621 Check_Time
: constant Duration := Monotonic_Clock
;
626 Local_Timedout
: Boolean;
632 if Mode
= Relative
then
634 Abs_Time
:= Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
;
636 Rel_Time
:= Time
- Check_Time
;
640 if Rel_Time
> 0.0 then
642 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
643 or else Self_ID
.Pending_Priority_Change
;
646 Cond_Timed_Wait
(Self_ID
.Common
.LL
.CV
'Access,
647 Single_RTS_Lock
'Access, Rel_Time
, Local_Timedout
, Result
);
649 Cond_Timed_Wait
(Self_ID
.Common
.LL
.CV
'Access,
650 Self_ID
.Common
.LL
.L
'Access, Rel_Time
, Local_Timedout
, Result
);
653 exit when Abs_Time
<= Monotonic_Clock
;
655 if not Local_Timedout
then
656 -- somebody may have called Wakeup for us
661 Rel_Time
:= Abs_Time
- Monotonic_Clock
;
670 procedure Timed_Delay
673 Mode
: ST
.Delay_Modes
)
675 Check_Time
: constant Duration := Monotonic_Clock
;
682 -- Only the little window between deferring abort and
683 -- locking Self_ID is the reason we need to
684 -- check for pending abort and priority change below!
692 Write_Lock
(Self_ID
);
694 if Mode
= Relative
then
696 Abs_Time
:= Time
+ Check_Time
;
698 Rel_Time
:= Time
- Check_Time
;
702 if Rel_Time
> 0.0 then
703 Self_ID
.Common
.State
:= Delay_Sleep
;
706 if Self_ID
.Pending_Priority_Change
then
707 Self_ID
.Pending_Priority_Change
:= False;
708 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
709 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
712 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
715 Cond_Timed_Wait
(Self_ID
.Common
.LL
.CV
'Access,
716 Single_RTS_Lock
'Access, Rel_Time
, Timedout
, Result
);
718 Cond_Timed_Wait
(Self_ID
.Common
.LL
.CV
'Access,
719 Self_ID
.Common
.LL
.L
'Access, Rel_Time
, Timedout
, Result
);
722 exit when Abs_Time
<= Monotonic_Clock
;
724 Rel_Time
:= Abs_Time
- Monotonic_Clock
;
727 Self_ID
.Common
.State
:= Runnable
;
737 SSL
.Abort_Undefer
.all;
744 procedure Wakeup
(T
: Task_ID
; Reason
: System
.Tasking
.Task_States
) is
746 Cond_Signal
(T
.Common
.LL
.CV
'Access);
753 procedure Yield
(Do_Yield
: Boolean := True) is
764 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
765 pragma Atomic_Components
(Prio_Array_Type
);
767 Prio_Array
: Prio_Array_Type
;
768 -- Global array containing the id of the currently running task for
771 -- Note: we assume that we are on a single processor with run-til-blocked
774 procedure Set_Priority
776 Prio
: System
.Any_Priority
;
777 Loss_Of_Inheritance
: Boolean := False)
780 Array_Item
: Integer;
783 Res
:= SetThreadPriority
784 (T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Underlying_Priorities
(Prio
)));
785 pragma Assert
(Res
= True);
787 -- ??? Work around a bug in NT 4.0 SP3 scheduler
788 -- It looks like when a task with Thread_Priority_Idle (using RT class)
789 -- never reaches its time slice (e.g by doing multiple and simple RV,
790 -- see CXD8002), the scheduler never gives higher priority task a
792 -- Note that this works fine on NT 4.0 SP1
794 if Time_Slice_Val
= 0
795 and then Underlying_Priorities
(Prio
) = Thread_Priority_Idle
796 and then Loss_Of_Inheritance
801 if FIFO_Within_Priorities
then
803 -- Annex D requirement [RM D.2.2 par. 9]:
804 -- If the task drops its priority due to the loss of inherited
805 -- priority, it is added at the head of the ready queue for its
806 -- new active priority.
808 if Loss_Of_Inheritance
809 and then Prio
< T
.Common
.Current_Priority
811 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
812 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
815 -- Let some processes a chance to arrive
819 -- Then wait for our turn to proceed
821 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
822 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
825 Prio_Array
(T
.Common
.Base_Priority
) :=
826 Prio_Array
(T
.Common
.Base_Priority
) - 1;
830 T
.Common
.Current_Priority
:= Prio
;
837 function Get_Priority
(T
: Task_ID
) return System
.Any_Priority
is
839 return T
.Common
.Current_Priority
;
846 -- There were two paths were we needed to call Enter_Task :
847 -- 1) from System.Task_Primitives.Operations.Initialize
848 -- 2) from System.Tasking.Stages.Task_Wrapper
850 -- The thread initialisation has to be done only for the first case.
852 -- This is because the GetCurrentThread NT call does not return the
853 -- real thread handler but only a "pseudo" one. It is not possible to
854 -- release the thread handle and free the system ressources from this
855 -- "pseudo" handle. So we really want to keep the real thread handle
856 -- set in System.Task_Primitives.Operations.Create_Task during the
859 procedure Enter_Task
(Self_ID
: Task_ID
) is
860 procedure Init_Float
;
861 pragma Import
(C
, Init_Float
, "__gnat_init_float");
862 -- Properly initializes the FPU for x86 systems.
867 Succeeded
:= TlsSetValue
(TlsIndex
, To_Address
(Self_ID
));
868 pragma Assert
(Succeeded
= True);
871 Self_ID
.Common
.LL
.Thread_Id
:= GetCurrentThreadId
;
875 for J
in Known_Tasks
'Range loop
876 if Known_Tasks
(J
) = null then
877 Known_Tasks
(J
) := Self_ID
;
878 Self_ID
.Known_Tasks_Index
:= J
;
890 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_ID
is
892 return new Ada_Task_Control_Block
(Entry_Num
);
899 procedure Initialize_TCB
(Self_ID
: Task_ID
; Succeeded
: out Boolean) is
901 Initialize_Cond
(Self_ID
.Common
.LL
.CV
'Access);
903 if not Single_Lock
then
904 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
914 procedure Create_Task
916 Wrapper
: System
.Address
;
917 Stack_Size
: System
.Parameters
.Size_Type
;
918 Priority
: System
.Any_Priority
;
919 Succeeded
: out Boolean)
922 TaskId
: aliased DWORD
;
923 pTaskParameter
: System
.OS_Interface
.PVOID
;
926 Entry_Point
: PTHREAD_START_ROUTINE
;
928 function To_PTHREAD_START_ROUTINE
is new
929 Unchecked_Conversion
(System
.Address
, PTHREAD_START_ROUTINE
);
932 pTaskParameter
:= To_Address
(T
);
934 if Stack_Size
= Unspecified_Size
then
935 dwStackSize
:= DWORD
(Default_Stack_Size
);
937 elsif Stack_Size
< Minimum_Stack_Size
then
938 dwStackSize
:= DWORD
(Minimum_Stack_Size
);
941 dwStackSize
:= DWORD
(Stack_Size
);
944 Entry_Point
:= To_PTHREAD_START_ROUTINE
(Wrapper
);
946 hTask
:= CreateThread
951 DWORD
(Create_Suspended
),
952 TaskId
'Unchecked_Access);
954 -- Step 1: Create the thread in blocked mode
960 -- Step 2: set its TCB
962 T
.Common
.LL
.Thread
:= hTask
;
964 -- Step 3: set its priority (child has inherited priority from parent)
966 Set_Priority
(T
, Priority
);
968 -- Step 4: Now, start it for good:
970 Result
:= ResumeThread
(hTask
);
971 pragma Assert
(Result
= 1);
973 Succeeded
:= Result
= 1;
980 procedure Finalize_TCB
(T
: Task_ID
) is
981 Self_ID
: Task_ID
:= T
;
985 procedure Free
is new
986 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_ID
);
989 if not Single_Lock
then
990 Finalize_Lock
(T
.Common
.LL
.L
'Access);
993 Finalize_Cond
(T
.Common
.LL
.CV
'Access);
995 if T
.Known_Tasks_Index
/= -1 then
996 Known_Tasks
(T
.Known_Tasks_Index
) := null;
999 -- Wait for the thread to terminate then close it. this is needed
1000 -- to release system ressources.
1002 Result
:= WaitForSingleObject
(T
.Common
.LL
.Thread
, Wait_Infinite
);
1003 pragma Assert
(Result
/= WAIT_FAILED
);
1004 Succeeded
:= CloseHandle
(T
.Common
.LL
.Thread
);
1005 pragma Assert
(Succeeded
= True);
1014 procedure Exit_Task
is
1023 procedure Abort_Task
(T
: Task_ID
) is
1028 ----------------------
1029 -- Environment_Task --
1030 ----------------------
1032 function Environment_Task
return Task_ID
is
1034 return Environment_Task_ID
;
1035 end Environment_Task
;
1041 procedure Lock_RTS
is
1043 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1050 procedure Unlock_RTS
is
1052 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1059 procedure Initialize
(Environment_Task
: Task_ID
) is
1062 Environment_Task_ID
:= Environment_Task
;
1064 if Time_Slice_Val
= 0 or else FIFO_Within_Priorities
then
1065 Res
:= OS_Interface
.SetPriorityClass
1066 (GetCurrentProcess
, Realtime_Priority_Class
);
1069 TlsIndex
:= TlsAlloc
;
1071 -- Initialize the lock used to synchronize chain of all ATCBs.
1073 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1075 Environment_Task
.Common
.LL
.Thread
:= GetCurrentThread
;
1076 Enter_Task
(Environment_Task
);
1078 -- Create a free ATCB for use on the Fake_ATCB_List
1080 Next_Fake_ATCB
:= new Fake_ATCB
;
1083 ---------------------
1084 -- Monotonic_Clock --
1085 ---------------------
1087 function Monotonic_Clock
return Duration
1088 renames System
.OS_Primitives
.Monotonic_Clock
;
1094 function RT_Resolution
return Duration is
1096 return 0.000_001
; -- 1 micro-second
1103 -- Dummy versions. The only currently working versions is for solaris
1106 function Check_Exit
(Self_ID
: ST
.Task_ID
) return Boolean is
1111 --------------------
1112 -- Check_No_Locks --
1113 --------------------
1115 function Check_No_Locks
(Self_ID
: ST
.Task_ID
) return Boolean is
1124 function Suspend_Task
1126 Thread_Self
: Thread_Id
) return Boolean is
1128 if T
.Common
.LL
.Thread
/= Thread_Self
then
1129 return SuspendThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1139 function Resume_Task
1141 Thread_Self
: Thread_Id
) return Boolean is
1143 if T
.Common
.LL
.Thread
/= Thread_Self
then
1144 return ResumeThread
(T
.Common
.LL
.Thread
) = NO_ERROR
;
1150 end System
.Task_Primitives
.Operations
;