fixing pr42337
[official-gcc.git] / gcc / ada / s-taprop-mingw.adb
bloba3b19ab5c5df7aa0a8f150f4c1e69672643f2298
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is a NT (native) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
35 -- the underlying OS.
37 pragma Polling (Off);
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;
43 with Interfaces.C;
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;
63 use System.Tasking;
64 use Interfaces.C;
65 use Interfaces.C.Strings;
66 use System.OS_Interface;
67 use System.Parameters;
68 use System.OS_Primitives;
69 use System.Task_Info;
70 use System.Win32;
71 use System.Win32.Ext;
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.
80 ---------------------
81 -- Local Functions --
82 ---------------------
84 procedure InitializeCriticalSection (pCriticalSection : access RTS_Lock);
85 procedure InitializeCriticalSection
86 (pCriticalSection : access CRITICAL_SECTION);
87 pragma Import
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");
104 ----------------
105 -- Local Data --
106 ----------------
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 ------------------------------------
136 TlsIndex : DWORD;
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.
141 --------------------
142 -- Local Packages --
143 --------------------
145 package Specific is
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);
152 pragma Inline (Set);
153 -- Set the self id for the current task
155 end Specific;
157 package body Specific is
159 function Is_Valid_Task return Boolean is
160 begin
161 return TlsGetValue (TlsIndex) /= System.Null_Address;
162 end Is_Valid_Task;
164 procedure Set (Self_Id : Task_Id) is
165 Succeeded : BOOL;
166 begin
167 Succeeded := TlsSetValue (TlsIndex, To_Address (Self_Id));
168 pragma Assert (Succeeded = Win32.TRUE);
169 end Set;
171 end Specific;
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
196 procedure Cond_Wait
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;
204 Rel_Time : Duration;
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
218 hEvent : HANDLE;
219 begin
220 hEvent := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr);
221 pragma Assert (hEvent /= 0);
222 Cond.all := Condition_Variable (hEvent);
223 end Initialize_Cond;
225 -------------------
226 -- Finalize_Cond --
227 -------------------
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
233 Result : BOOL;
234 begin
235 Result := CloseHandle (HANDLE (Cond.all));
236 pragma Assert (Result = Win32.TRUE);
237 end Finalize_Cond;
239 -----------------
240 -- Cond_Signal --
241 -----------------
243 procedure Cond_Signal (Cond : not null access Condition_Variable) is
244 Result : BOOL;
245 begin
246 Result := SetEvent (HANDLE (Cond.all));
247 pragma Assert (Result = Win32.TRUE);
248 end Cond_Signal;
250 ---------------
251 -- Cond_Wait --
252 ---------------
254 -- Pre-condition: Cond is posted
255 -- L is locked.
257 -- Post-condition: Cond is posted
258 -- L is locked.
260 procedure Cond_Wait
261 (Cond : not null access Condition_Variable;
262 L : not null access RTS_Lock)
264 Result : DWORD;
265 Result_Bool : BOOL;
267 begin
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);
281 end Cond_Wait;
283 ---------------------
284 -- Cond_Timed_Wait --
285 ---------------------
287 -- Pre-condition: Cond is posted
288 -- L is locked.
290 -- Post-condition: Cond is posted
291 -- L is locked.
293 procedure Cond_Timed_Wait
294 (Cond : not null access Condition_Variable;
295 L : not null access RTS_Lock;
296 Rel_Time : Duration;
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)
303 Time_Out : DWORD;
304 Result : BOOL;
305 Wait_Result : DWORD;
307 begin
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
318 Timed_Out := True;
319 Wait_Result := 0;
321 else
322 Time_Out :=
323 (if Rel_Time >= Duration (Time_Out_Max) / 1000
324 then Time_Out_Max
325 else DWORD (Rel_Time * 1000));
327 Wait_Result := WaitForSingleObject (HANDLE (Cond.all), Time_Out);
329 if Wait_Result = WAIT_TIMEOUT then
330 Timed_Out := True;
331 Wait_Result := 0;
332 else
333 Timed_Out := False;
334 end if;
335 end if;
337 Write_Lock (L, Global_Lock => True);
339 -- Ensure post-condition
341 if Timed_Out then
342 Result := SetEvent (HANDLE (Cond.all));
343 pragma Assert (Result = Win32.TRUE);
344 end if;
346 Status := Integer (Wait_Result);
347 end Cond_Timed_Wait;
349 ------------------
350 -- Stack_Guard --
351 ------------------
353 -- The underlying thread system sets a guard page at the bottom of a thread
354 -- stack, so nothing is needed.
355 -- ??? Check the comment above
357 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
358 pragma Unreferenced (T, On);
359 begin
360 null;
361 end Stack_Guard;
363 --------------------
364 -- Get_Thread_Id --
365 --------------------
367 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
368 begin
369 return T.Common.LL.Thread;
370 end Get_Thread_Id;
372 ----------
373 -- Self --
374 ----------
376 function Self return Task_Id is
377 Self_Id : constant Task_Id := To_Task_Id (TlsGetValue (TlsIndex));
378 begin
379 if Self_Id = null then
380 return Register_Foreign_Thread (GetCurrentThread);
381 else
382 return Self_Id;
383 end if;
384 end Self;
386 ---------------------
387 -- Initialize_Lock --
388 ---------------------
390 -- Note: mutexes and cond_variables needed per-task basis are initialized
391 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
392 -- as RTS_Lock, Memory_Lock...) used in the RTS is initialized before any
393 -- status change of RTS. Therefore raising Storage_Error in the following
394 -- routines should be able to be handled safely.
396 procedure Initialize_Lock
397 (Prio : System.Any_Priority;
398 L : not null access Lock)
400 begin
401 InitializeCriticalSection (L.Mutex'Access);
402 L.Owner_Priority := 0;
403 L.Priority := Prio;
404 end Initialize_Lock;
406 procedure Initialize_Lock
407 (L : not null access RTS_Lock; Level : Lock_Level)
409 pragma Unreferenced (Level);
410 begin
411 InitializeCriticalSection (L);
412 end Initialize_Lock;
414 -------------------
415 -- Finalize_Lock --
416 -------------------
418 procedure Finalize_Lock (L : not null access Lock) is
419 begin
420 DeleteCriticalSection (L.Mutex'Access);
421 end Finalize_Lock;
423 procedure Finalize_Lock (L : not null access RTS_Lock) is
424 begin
425 DeleteCriticalSection (L);
426 end Finalize_Lock;
428 ----------------
429 -- Write_Lock --
430 ----------------
432 procedure Write_Lock
433 (L : not null access Lock; Ceiling_Violation : out Boolean) is
434 begin
435 L.Owner_Priority := Get_Priority (Self);
437 if L.Priority < L.Owner_Priority then
438 Ceiling_Violation := True;
439 return;
440 end if;
442 EnterCriticalSection (L.Mutex'Access);
444 Ceiling_Violation := False;
445 end Write_Lock;
447 procedure Write_Lock
448 (L : not null access RTS_Lock;
449 Global_Lock : Boolean := False)
451 begin
452 if not Single_Lock or else Global_Lock then
453 EnterCriticalSection (L);
454 end if;
455 end Write_Lock;
457 procedure Write_Lock (T : Task_Id) is
458 begin
459 if not Single_Lock then
460 EnterCriticalSection (T.Common.LL.L'Access);
461 end if;
462 end Write_Lock;
464 ---------------
465 -- Read_Lock --
466 ---------------
468 procedure Read_Lock
469 (L : not null access Lock; Ceiling_Violation : out Boolean) is
470 begin
471 Write_Lock (L, Ceiling_Violation);
472 end Read_Lock;
474 ------------
475 -- Unlock --
476 ------------
478 procedure Unlock (L : not null access Lock) is
479 begin
480 LeaveCriticalSection (L.Mutex'Access);
481 end Unlock;
483 procedure Unlock
484 (L : not null access RTS_Lock; Global_Lock : Boolean := False) is
485 begin
486 if not Single_Lock or else Global_Lock then
487 LeaveCriticalSection (L);
488 end if;
489 end Unlock;
491 procedure Unlock (T : Task_Id) is
492 begin
493 if not Single_Lock then
494 LeaveCriticalSection (T.Common.LL.L'Access);
495 end if;
496 end Unlock;
498 -----------------
499 -- Set_Ceiling --
500 -----------------
502 -- Dynamic priority ceilings are not supported by the underlying system
504 procedure Set_Ceiling
505 (L : not null access Lock;
506 Prio : System.Any_Priority)
508 pragma Unreferenced (L, Prio);
509 begin
510 null;
511 end Set_Ceiling;
513 -----------
514 -- Sleep --
515 -----------
517 procedure Sleep
518 (Self_ID : Task_Id;
519 Reason : System.Tasking.Task_States)
521 pragma Unreferenced (Reason);
523 begin
524 pragma Assert (Self_ID = Self);
526 if Single_Lock then
527 Cond_Wait (Self_ID.Common.LL.CV'Access, Single_RTS_Lock'Access);
528 else
529 Cond_Wait (Self_ID.Common.LL.CV'Access, Self_ID.Common.LL.L'Access);
530 end if;
532 if Self_ID.Deferral_Level = 0
533 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
534 then
535 Unlock (Self_ID);
536 raise Standard'Abort_Signal;
537 end if;
538 end Sleep;
540 -----------------
541 -- Timed_Sleep --
542 -----------------
544 -- This is for use within the run-time system, so abort is assumed to be
545 -- already deferred, and the caller should be holding its own ATCB lock.
547 procedure Timed_Sleep
548 (Self_ID : Task_Id;
549 Time : Duration;
550 Mode : ST.Delay_Modes;
551 Reason : System.Tasking.Task_States;
552 Timedout : out Boolean;
553 Yielded : out Boolean)
555 pragma Unreferenced (Reason);
556 Check_Time : Duration := Monotonic_Clock;
557 Rel_Time : Duration;
558 Abs_Time : Duration;
560 Result : Integer;
561 pragma Unreferenced (Result);
563 Local_Timedout : Boolean;
565 begin
566 Timedout := True;
567 Yielded := False;
569 if Mode = Relative then
570 Rel_Time := Time;
571 Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;
572 else
573 Rel_Time := Time - Check_Time;
574 Abs_Time := Time;
575 end if;
577 if Rel_Time > 0.0 then
578 loop
579 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
581 if Single_Lock then
582 Cond_Timed_Wait
583 (Self_ID.Common.LL.CV'Access,
584 Single_RTS_Lock'Access,
585 Rel_Time, Local_Timedout, Result);
586 else
587 Cond_Timed_Wait
588 (Self_ID.Common.LL.CV'Access,
589 Self_ID.Common.LL.L'Access,
590 Rel_Time, Local_Timedout, Result);
591 end if;
593 Check_Time := Monotonic_Clock;
594 exit when Abs_Time <= Check_Time;
596 if not Local_Timedout then
598 -- Somebody may have called Wakeup for us
600 Timedout := False;
601 exit;
602 end if;
604 Rel_Time := Abs_Time - Check_Time;
605 end loop;
606 end if;
607 end Timed_Sleep;
609 -----------------
610 -- Timed_Delay --
611 -----------------
613 procedure Timed_Delay
614 (Self_ID : Task_Id;
615 Time : Duration;
616 Mode : ST.Delay_Modes)
618 Check_Time : Duration := Monotonic_Clock;
619 Rel_Time : Duration;
620 Abs_Time : Duration;
622 Timedout : Boolean;
623 Result : Integer;
624 pragma Unreferenced (Timedout, Result);
626 begin
627 if Single_Lock then
628 Lock_RTS;
629 end if;
631 Write_Lock (Self_ID);
633 if Mode = Relative then
634 Rel_Time := Time;
635 Abs_Time := Time + Check_Time;
636 else
637 Rel_Time := Time - Check_Time;
638 Abs_Time := Time;
639 end if;
641 if Rel_Time > 0.0 then
642 Self_ID.Common.State := Delay_Sleep;
644 loop
645 exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
647 if Single_Lock then
648 Cond_Timed_Wait
649 (Self_ID.Common.LL.CV'Access,
650 Single_RTS_Lock'Access,
651 Rel_Time, Timedout, Result);
652 else
653 Cond_Timed_Wait
654 (Self_ID.Common.LL.CV'Access,
655 Self_ID.Common.LL.L'Access,
656 Rel_Time, Timedout, Result);
657 end if;
659 Check_Time := Monotonic_Clock;
660 exit when Abs_Time <= Check_Time;
662 Rel_Time := Abs_Time - Check_Time;
663 end loop;
665 Self_ID.Common.State := Runnable;
666 end if;
668 Unlock (Self_ID);
670 if Single_Lock then
671 Unlock_RTS;
672 end if;
674 Yield;
675 end Timed_Delay;
677 ------------
678 -- Wakeup --
679 ------------
681 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
682 pragma Unreferenced (Reason);
683 begin
684 Cond_Signal (T.Common.LL.CV'Access);
685 end Wakeup;
687 -----------
688 -- Yield --
689 -----------
691 procedure Yield (Do_Yield : Boolean := True) is
692 begin
693 if Do_Yield then
694 SwitchToThread;
696 elsif Annex_D then
697 -- If running with Annex-D semantics we need a delay
698 -- above 0 milliseconds here otherwise processes give
699 -- enough time to the other tasks to have a chance to
700 -- run.
702 -- This makes cxd8002 ACATS pass on Windows.
704 Sleep (1);
705 end if;
706 end Yield;
708 ------------------
709 -- Set_Priority --
710 ------------------
712 type Prio_Array_Type is array (System.Any_Priority) of Integer;
713 pragma Atomic_Components (Prio_Array_Type);
715 Prio_Array : Prio_Array_Type;
716 -- Global array containing the id of the currently running task for
717 -- each priority.
719 -- Note: we assume that we are on a single processor with run-til-blocked
720 -- scheduling.
722 procedure Set_Priority
723 (T : Task_Id;
724 Prio : System.Any_Priority;
725 Loss_Of_Inheritance : Boolean := False)
727 Res : BOOL;
728 Array_Item : Integer;
730 begin
731 Res := SetThreadPriority
732 (T.Common.LL.Thread, Interfaces.C.int (Underlying_Priorities (Prio)));
733 pragma Assert (Res = Win32.TRUE);
735 if Dispatching_Policy = 'F' or else Get_Policy (Prio) = 'F' then
737 -- Annex D requirement [RM D.2.2 par. 9]:
738 -- If the task drops its priority due to the loss of inherited
739 -- priority, it is added at the head of the ready queue for its
740 -- new active priority.
742 if Loss_Of_Inheritance
743 and then Prio < T.Common.Current_Priority
744 then
745 Array_Item := Prio_Array (T.Common.Base_Priority) + 1;
746 Prio_Array (T.Common.Base_Priority) := Array_Item;
748 loop
749 -- Let some processes a chance to arrive
751 Yield;
753 -- Then wait for our turn to proceed
755 exit when Array_Item = Prio_Array (T.Common.Base_Priority)
756 or else Prio_Array (T.Common.Base_Priority) = 1;
757 end loop;
759 Prio_Array (T.Common.Base_Priority) :=
760 Prio_Array (T.Common.Base_Priority) - 1;
761 end if;
762 end if;
764 T.Common.Current_Priority := Prio;
765 end Set_Priority;
767 ------------------
768 -- Get_Priority --
769 ------------------
771 function Get_Priority (T : Task_Id) return System.Any_Priority is
772 begin
773 return T.Common.Current_Priority;
774 end Get_Priority;
776 ----------------
777 -- Enter_Task --
778 ----------------
780 -- There were two paths were we needed to call Enter_Task :
781 -- 1) from System.Task_Primitives.Operations.Initialize
782 -- 2) from System.Tasking.Stages.Task_Wrapper
784 -- The thread initialisation has to be done only for the first case
786 -- This is because the GetCurrentThread NT call does not return the real
787 -- thread handler but only a "pseudo" one. It is not possible to release
788 -- the thread handle and free the system resources from this "pseudo"
789 -- handle. So we really want to keep the real thread handle set in
790 -- System.Task_Primitives.Operations.Create_Task during thread creation.
792 procedure Enter_Task (Self_ID : Task_Id) is
793 procedure Init_Float;
794 pragma Import (C, Init_Float, "__gnat_init_float");
795 -- Properly initializes the FPU for x86 systems
797 begin
798 Specific.Set (Self_ID);
799 Init_Float;
801 if Self_ID.Common.Task_Info /= null
802 and then
803 Self_ID.Common.Task_Info.CPU >= CPU_Number (Number_Of_Processors)
804 then
805 raise Invalid_CPU_Number;
806 end if;
808 Self_ID.Common.LL.Thread_Id := GetCurrentThreadId;
809 end Enter_Task;
811 --------------
812 -- New_ATCB --
813 --------------
815 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
816 begin
817 return new Ada_Task_Control_Block (Entry_Num);
818 end New_ATCB;
820 -------------------
821 -- Is_Valid_Task --
822 -------------------
824 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
826 -----------------------------
827 -- Register_Foreign_Thread --
828 -----------------------------
830 function Register_Foreign_Thread return Task_Id is
831 begin
832 if Is_Valid_Task then
833 return Self;
834 else
835 return Register_Foreign_Thread (GetCurrentThread);
836 end if;
837 end Register_Foreign_Thread;
839 --------------------
840 -- Initialize_TCB --
841 --------------------
843 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
844 begin
845 -- Initialize thread ID to 0, this is needed to detect threads that
846 -- are not yet activated.
848 Self_ID.Common.LL.Thread := 0;
850 Initialize_Cond (Self_ID.Common.LL.CV'Access);
852 if not Single_Lock then
853 Initialize_Lock (Self_ID.Common.LL.L'Access, ATCB_Level);
854 end if;
856 Succeeded := True;
857 end Initialize_TCB;
859 -----------------
860 -- Create_Task --
861 -----------------
863 procedure Create_Task
864 (T : Task_Id;
865 Wrapper : System.Address;
866 Stack_Size : System.Parameters.Size_Type;
867 Priority : System.Any_Priority;
868 Succeeded : out Boolean)
870 Initial_Stack_Size : constant := 1024;
871 -- We set the initial stack size to 1024. On Windows version prior to XP
872 -- there is no way to fix a task stack size. Only the initial stack size
873 -- can be set, the operating system will raise the task stack size if
874 -- needed.
876 function Is_Windows_XP return Integer;
877 pragma Import (C, Is_Windows_XP, "__gnat_is_windows_xp");
878 -- Returns 1 if running on Windows XP
880 hTask : HANDLE;
881 TaskId : aliased DWORD;
882 pTaskParameter : Win32.PVOID;
883 Result : DWORD;
884 Entry_Point : PTHREAD_START_ROUTINE;
886 begin
887 pTaskParameter := To_Address (T);
889 Entry_Point := To_PTHREAD_START_ROUTINE (Wrapper);
891 if Is_Windows_XP = 1 then
892 hTask := CreateThread
893 (null,
894 DWORD (Stack_Size),
895 Entry_Point,
896 pTaskParameter,
897 DWORD (Create_Suspended) or
898 DWORD (Stack_Size_Param_Is_A_Reservation),
899 TaskId'Unchecked_Access);
900 else
901 hTask := CreateThread
902 (null,
903 Initial_Stack_Size,
904 Entry_Point,
905 pTaskParameter,
906 DWORD (Create_Suspended),
907 TaskId'Unchecked_Access);
908 end if;
910 -- Step 1: Create the thread in blocked mode
912 if hTask = 0 then
913 Succeeded := False;
914 return;
915 end if;
917 -- Step 2: set its TCB
919 T.Common.LL.Thread := hTask;
921 -- Step 3: set its priority (child has inherited priority from parent)
923 Set_Priority (T, Priority);
925 if Time_Slice_Val = 0
926 or else Dispatching_Policy = 'F'
927 or else Get_Policy (Priority) = 'F'
928 then
929 -- Here we need Annex D semantics so we disable the NT priority
930 -- boost. A priority boost is temporarily given by the system to a
931 -- thread when it is taken out of a wait state.
933 SetThreadPriorityBoost (hTask, DisablePriorityBoost => Win32.TRUE);
934 end if;
936 -- Step 4: Handle Task_Info
938 if T.Common.Task_Info /= null then
939 if T.Common.Task_Info.CPU /= Task_Info.Any_CPU then
940 Result := SetThreadIdealProcessor (hTask, T.Common.Task_Info.CPU);
941 pragma Assert (Result = 1);
942 end if;
943 end if;
945 -- Step 5: Now, start it for good:
947 Result := ResumeThread (hTask);
948 pragma Assert (Result = 1);
950 Succeeded := Result = 1;
951 end Create_Task;
953 ------------------
954 -- Finalize_TCB --
955 ------------------
957 procedure Finalize_TCB (T : Task_Id) is
958 Self_ID : Task_Id := T;
959 Result : DWORD;
960 Succeeded : BOOL;
961 Is_Self : constant Boolean := T = Self;
963 procedure Free is new
964 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
966 begin
967 if not Single_Lock then
968 Finalize_Lock (T.Common.LL.L'Access);
969 end if;
971 Finalize_Cond (T.Common.LL.CV'Access);
973 if T.Known_Tasks_Index /= -1 then
974 Known_Tasks (T.Known_Tasks_Index) := null;
975 end if;
977 if Self_ID.Common.LL.Thread /= 0 then
979 -- This task has been activated. Wait for the thread to terminate
980 -- then close it. This is needed to release system resources.
982 Result := WaitForSingleObject (T.Common.LL.Thread, Wait_Infinite);
983 pragma Assert (Result /= WAIT_FAILED);
984 Succeeded := CloseHandle (T.Common.LL.Thread);
985 pragma Assert (Succeeded = Win32.TRUE);
986 end if;
988 Free (Self_ID);
990 if Is_Self then
991 Specific.Set (null);
992 end if;
993 end Finalize_TCB;
995 ---------------
996 -- Exit_Task --
997 ---------------
999 procedure Exit_Task is
1000 begin
1001 Specific.Set (null);
1002 end Exit_Task;
1004 ----------------
1005 -- Abort_Task --
1006 ----------------
1008 procedure Abort_Task (T : Task_Id) is
1009 pragma Unreferenced (T);
1010 begin
1011 null;
1012 end Abort_Task;
1014 ----------------------
1015 -- Environment_Task --
1016 ----------------------
1018 function Environment_Task return Task_Id is
1019 begin
1020 return Environment_Task_Id;
1021 end Environment_Task;
1023 --------------
1024 -- Lock_RTS --
1025 --------------
1027 procedure Lock_RTS is
1028 begin
1029 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1030 end Lock_RTS;
1032 ----------------
1033 -- Unlock_RTS --
1034 ----------------
1036 procedure Unlock_RTS is
1037 begin
1038 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1039 end Unlock_RTS;
1041 ----------------
1042 -- Initialize --
1043 ----------------
1045 procedure Initialize (Environment_Task : Task_Id) is
1046 Discard : BOOL;
1047 pragma Unreferenced (Discard);
1049 begin
1050 Environment_Task_Id := Environment_Task;
1051 OS_Primitives.Initialize;
1052 Interrupt_Management.Initialize;
1054 if Time_Slice_Val = 0 or else Dispatching_Policy = 'F' then
1055 -- Here we need Annex D semantics, switch the current process to the
1056 -- Realtime_Priority_Class.
1058 Discard := OS_Interface.SetPriorityClass
1059 (GetCurrentProcess, Realtime_Priority_Class);
1061 Annex_D := True;
1062 end if;
1064 TlsIndex := TlsAlloc;
1066 -- Initialize the lock used to synchronize chain of all ATCBs
1068 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1070 Environment_Task.Common.LL.Thread := GetCurrentThread;
1072 -- Make environment task known here because it doesn't go through
1073 -- Activate_Tasks, which does it for all other tasks.
1075 Known_Tasks (Known_Tasks'First) := Environment_Task;
1076 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1078 Enter_Task (Environment_Task);
1079 end Initialize;
1081 ---------------------
1082 -- Monotonic_Clock --
1083 ---------------------
1085 function Monotonic_Clock return Duration
1086 renames System.OS_Primitives.Monotonic_Clock;
1088 -------------------
1089 -- RT_Resolution --
1090 -------------------
1092 function RT_Resolution return Duration is
1093 begin
1094 return 0.000_001; -- 1 micro-second
1095 end RT_Resolution;
1097 ----------------
1098 -- Initialize --
1099 ----------------
1101 procedure Initialize (S : in out Suspension_Object) is
1102 begin
1103 -- Initialize internal state. It is always initialized to False (ARM
1104 -- D.10 par. 6).
1106 S.State := False;
1107 S.Waiting := False;
1109 -- Initialize internal mutex
1111 InitializeCriticalSection (S.L'Access);
1113 -- Initialize internal condition variable
1115 S.CV := CreateEvent (null, Win32.TRUE, Win32.FALSE, Null_Ptr);
1116 pragma Assert (S.CV /= 0);
1117 end Initialize;
1119 --------------
1120 -- Finalize --
1121 --------------
1123 procedure Finalize (S : in out Suspension_Object) is
1124 Result : BOOL;
1125 begin
1126 -- Destroy internal mutex
1128 DeleteCriticalSection (S.L'Access);
1130 -- Destroy internal condition variable
1132 Result := CloseHandle (S.CV);
1133 pragma Assert (Result = Win32.TRUE);
1134 end Finalize;
1136 -------------------
1137 -- Current_State --
1138 -------------------
1140 function Current_State (S : Suspension_Object) return Boolean is
1141 begin
1142 -- We do not want to use lock on this read operation. State is marked
1143 -- as Atomic so that we ensure that the value retrieved is correct.
1145 return S.State;
1146 end Current_State;
1148 ---------------
1149 -- Set_False --
1150 ---------------
1152 procedure Set_False (S : in out Suspension_Object) is
1153 begin
1154 SSL.Abort_Defer.all;
1156 EnterCriticalSection (S.L'Access);
1158 S.State := False;
1160 LeaveCriticalSection (S.L'Access);
1162 SSL.Abort_Undefer.all;
1163 end Set_False;
1165 --------------
1166 -- Set_True --
1167 --------------
1169 procedure Set_True (S : in out Suspension_Object) is
1170 Result : BOOL;
1171 begin
1172 SSL.Abort_Defer.all;
1174 EnterCriticalSection (S.L'Access);
1176 -- If there is already a task waiting on this suspension object then
1177 -- we resume it, leaving the state of the suspension object to False,
1178 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1179 -- the state to True.
1181 if S.Waiting then
1182 S.Waiting := False;
1183 S.State := False;
1185 Result := SetEvent (S.CV);
1186 pragma Assert (Result = Win32.TRUE);
1187 else
1188 S.State := True;
1189 end if;
1191 LeaveCriticalSection (S.L'Access);
1193 SSL.Abort_Undefer.all;
1194 end Set_True;
1196 ------------------------
1197 -- Suspend_Until_True --
1198 ------------------------
1200 procedure Suspend_Until_True (S : in out Suspension_Object) is
1201 Result : DWORD;
1202 Result_Bool : BOOL;
1203 begin
1204 SSL.Abort_Defer.all;
1206 EnterCriticalSection (S.L'Access);
1208 if S.Waiting then
1209 -- Program_Error must be raised upon calling Suspend_Until_True
1210 -- if another task is already waiting on that suspension object
1211 -- (ARM D.10 par. 10).
1213 LeaveCriticalSection (S.L'Access);
1215 SSL.Abort_Undefer.all;
1217 raise Program_Error;
1218 else
1219 -- Suspend the task if the state is False. Otherwise, the task
1220 -- continues its execution, and the state of the suspension object
1221 -- is set to False (ARM D.10 par. 9).
1223 if S.State then
1224 S.State := False;
1226 LeaveCriticalSection (S.L'Access);
1228 SSL.Abort_Undefer.all;
1229 else
1230 S.Waiting := True;
1232 -- Must reset CV BEFORE L is unlocked
1234 Result_Bool := ResetEvent (S.CV);
1235 pragma Assert (Result_Bool = Win32.TRUE);
1237 LeaveCriticalSection (S.L'Access);
1239 SSL.Abort_Undefer.all;
1241 Result := WaitForSingleObject (S.CV, Wait_Infinite);
1242 pragma Assert (Result = 0);
1243 end if;
1244 end if;
1245 end Suspend_Until_True;
1247 ----------------
1248 -- Check_Exit --
1249 ----------------
1251 -- Dummy versions. The only currently working versions is for solaris
1252 -- (native).
1254 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1255 pragma Unreferenced (Self_ID);
1256 begin
1257 return True;
1258 end Check_Exit;
1260 --------------------
1261 -- Check_No_Locks --
1262 --------------------
1264 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1265 pragma Unreferenced (Self_ID);
1266 begin
1267 return True;
1268 end Check_No_Locks;
1270 ------------------
1271 -- Suspend_Task --
1272 ------------------
1274 function Suspend_Task
1275 (T : ST.Task_Id;
1276 Thread_Self : Thread_Id) return Boolean
1278 begin
1279 if T.Common.LL.Thread /= Thread_Self then
1280 return SuspendThread (T.Common.LL.Thread) = NO_ERROR;
1281 else
1282 return True;
1283 end if;
1284 end Suspend_Task;
1286 -----------------
1287 -- Resume_Task --
1288 -----------------
1290 function Resume_Task
1291 (T : ST.Task_Id;
1292 Thread_Self : Thread_Id) return Boolean
1294 begin
1295 if T.Common.LL.Thread /= Thread_Self then
1296 return ResumeThread (T.Common.LL.Thread) = NO_ERROR;
1297 else
1298 return True;
1299 end if;
1300 end Resume_Task;
1302 --------------------
1303 -- Stop_All_Tasks --
1304 --------------------
1306 procedure Stop_All_Tasks is
1307 begin
1308 null;
1309 end Stop_All_Tasks;
1311 ---------------
1312 -- Stop_Task --
1313 ---------------
1315 function Stop_Task (T : ST.Task_Id) return Boolean is
1316 pragma Unreferenced (T);
1317 begin
1318 return False;
1319 end Stop_Task;
1321 -------------------
1322 -- Continue_Task --
1323 -------------------
1325 function Continue_Task (T : ST.Task_Id) return Boolean is
1326 pragma Unreferenced (T);
1327 begin
1328 return False;
1329 end Continue_Task;
1331 end System.Task_Primitives.Operations;