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-2010, 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 the VxWorks 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_Conversion;
42 with Ada.Unchecked_Deallocation;
46 with System.Multiprocessors;
47 with System.Tasking.Debug;
48 with System.Interrupt_Management;
50 with System.Soft_Links;
51 -- We use System.Soft_Links instead of System.Tasking.Initialization
52 -- because the later is a higher level package that we shouldn't depend
53 -- on. For example when using the restricted run time, it is replaced by
54 -- System.Tasking.Restricted.Stages.
56 with System.Task_Info;
57 with System.VxWorks.Ext;
59 package body System.Task_Primitives.Operations is
61 package SSL renames System.Soft_Links;
63 use System.Tasking.Debug;
65 use System.OS_Interface;
66 use System.Parameters;
67 use type System.VxWorks.Ext.t_id;
68 use type Interfaces.C.int;
70 subtype int is System.OS_Interface.int;
72 Relative : constant := 0;
78 -- The followings are logically constants, but need to be initialized at
81 Single_RTS_Lock : aliased RTS_Lock;
82 -- This is a lock to allow only one thread of control in the RTS at a
83 -- time; it is used to execute in mutual exclusion from all other tasks.
84 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
86 Environment_Task_Id : Task_Id;
87 -- A variable to hold Task_Id for the environment task
89 Unblocked_Signal_Mask : aliased sigset_t;
90 -- The set of signals that should unblocked in all tasks
92 -- The followings are internal configuration constants needed
94 Time_Slice_Val : Integer;
95 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
97 Locking_Policy : Character;
98 pragma Import (C, Locking_Policy, "__gl_locking_policy");
100 Dispatching_Policy : Character;
101 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
103 Mutex_Protocol : Priority_Type;
105 Foreign_Task_Elaborated : aliased Boolean := True;
106 -- Used to identified fake tasks (i.e., non-Ada Threads)
108 type Set_Stack_Limit_Proc_Acc is access procedure;
109 pragma Convention (C, Set_Stack_Limit_Proc_Acc);
111 Set_Stack_Limit_Hook : Set_Stack_Limit_Proc_Acc;
112 pragma Import (C, Set_Stack_Limit_Hook, "__gnat_set_stack_limit_hook");
113 -- Procedure to be called when a task is created to set stack
122 procedure Initialize;
123 pragma Inline (Initialize);
124 -- Initialize task specific data
126 function Is_Valid_Task return Boolean;
127 pragma Inline (Is_Valid_Task);
128 -- Does executing thread have a TCB?
130 procedure Set (Self_Id : Task_Id);
132 -- Set the self id for the current task
135 pragma Inline (Delete);
136 -- Delete the task specific data associated with the current task
138 function Self return Task_Id;
139 pragma Inline (Self);
140 -- Return a pointer to the Ada Task Control Block of the calling task
144 package body Specific is separate;
145 -- The body of this package is target specific
147 ---------------------------------
148 -- Support for foreign threads --
149 ---------------------------------
151 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
152 -- Allocate and Initialize a new ATCB for the current Thread
154 function Register_Foreign_Thread
155 (Thread : Thread_Id) return Task_Id is separate;
157 -----------------------
158 -- Local Subprograms --
159 -----------------------
161 procedure Abort_Handler (signo : Signal);
162 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
164 procedure Install_Signal_Handlers;
165 -- Install the default signal handlers for the current task
167 function Is_Task_Context return Boolean;
168 -- This function returns True if the current execution is in the context
169 -- of a task, and False if it is an interrupt context.
171 function To_Address is
172 new Ada.Unchecked_Conversion (Task_Id, System.Address);
178 procedure Abort_Handler (signo : Signal) is
179 pragma Unreferenced (signo);
181 Self_ID : constant Task_Id := Self;
182 Old_Set : aliased sigset_t;
185 pragma Warnings (Off, Result);
188 -- It is not safe to raise an exception when using ZCX and the GCC
189 -- exception handling mechanism.
191 if ZCX_By_Default and then GCC_ZCX_Support then
195 if Self_ID.Deferral_Level = 0
196 and then Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
197 and then not Self_ID.Aborting
199 Self_ID.Aborting := True;
201 -- Make sure signals used for RTS internal purpose are unmasked
206 Unblocked_Signal_Mask'Access,
208 pragma Assert (Result = 0);
210 raise Standard'Abort_Signal;
218 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
219 pragma Unreferenced (T);
220 pragma Unreferenced (On);
223 -- Nothing needed (why not???)
232 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
234 return T.Common.LL.Thread;
241 function Self return Task_Id renames Specific.Self;
243 -----------------------------
244 -- Install_Signal_Handlers --
245 -----------------------------
247 procedure Install_Signal_Handlers is
248 act : aliased struct_sigaction;
249 old_act : aliased struct_sigaction;
250 Tmp_Set : aliased sigset_t;
255 act.sa_handler := Abort_Handler'Address;
257 Result := sigemptyset (Tmp_Set'Access);
258 pragma Assert (Result = 0);
259 act.sa_mask := Tmp_Set;
263 (Signal (Interrupt_Management.Abort_Task_Interrupt),
264 act'Unchecked_Access,
265 old_act'Unchecked_Access);
266 pragma Assert (Result = 0);
268 Interrupt_Management.Initialize_Interrupts;
269 end Install_Signal_Handlers;
271 ---------------------
272 -- Initialize_Lock --
273 ---------------------
275 procedure Initialize_Lock
276 (Prio : System.Any_Priority;
277 L : not null access Lock)
280 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
281 L.Prio_Ceiling := int (Prio);
282 L.Protocol := Mutex_Protocol;
283 pragma Assert (L.Mutex /= 0);
286 procedure Initialize_Lock
287 (L : not null access RTS_Lock;
290 pragma Unreferenced (Level);
292 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
293 L.Prio_Ceiling := int (System.Any_Priority'Last);
294 L.Protocol := Mutex_Protocol;
295 pragma Assert (L.Mutex /= 0);
302 procedure Finalize_Lock (L : not null access Lock) is
305 Result := semDelete (L.Mutex);
306 pragma Assert (Result = 0);
309 procedure Finalize_Lock (L : not null access RTS_Lock) is
312 Result := semDelete (L.Mutex);
313 pragma Assert (Result = 0);
321 (L : not null access Lock;
322 Ceiling_Violation : out Boolean)
327 if L.Protocol = Prio_Protect
328 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
330 Ceiling_Violation := True;
333 Ceiling_Violation := False;
336 Result := semTake (L.Mutex, WAIT_FOREVER);
337 pragma Assert (Result = 0);
341 (L : not null access RTS_Lock;
342 Global_Lock : Boolean := False)
346 if not Single_Lock or else Global_Lock then
347 Result := semTake (L.Mutex, WAIT_FOREVER);
348 pragma Assert (Result = 0);
352 procedure Write_Lock (T : Task_Id) is
355 if not Single_Lock then
356 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
357 pragma Assert (Result = 0);
366 (L : not null access Lock;
367 Ceiling_Violation : out Boolean)
370 Write_Lock (L, Ceiling_Violation);
377 procedure Unlock (L : not null access Lock) is
380 Result := semGive (L.Mutex);
381 pragma Assert (Result = 0);
385 (L : not null access RTS_Lock;
386 Global_Lock : Boolean := False)
390 if not Single_Lock or else Global_Lock then
391 Result := semGive (L.Mutex);
392 pragma Assert (Result = 0);
396 procedure Unlock (T : Task_Id) is
399 if not Single_Lock then
400 Result := semGive (T.Common.LL.L.Mutex);
401 pragma Assert (Result = 0);
409 -- Dynamic priority ceilings are not supported by the underlying system
411 procedure Set_Ceiling
412 (L : not null access Lock;
413 Prio : System.Any_Priority)
415 pragma Unreferenced (L, Prio);
424 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
425 pragma Unreferenced (Reason);
430 pragma Assert (Self_ID = Self);
432 -- Release the mutex before sleeping
435 semGive (if Single_Lock
436 then Single_RTS_Lock.Mutex
437 else Self_ID.Common.LL.L.Mutex);
438 pragma Assert (Result = 0);
440 -- Perform a blocking operation to take the CV semaphore. Note that a
441 -- blocking operation in VxWorks will reenable task scheduling. When we
442 -- are no longer blocked and control is returned, task scheduling will
443 -- again be disabled.
445 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
446 pragma Assert (Result = 0);
448 -- Take the mutex back
451 semTake ((if Single_Lock
452 then Single_RTS_Lock.Mutex
453 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
454 pragma Assert (Result = 0);
461 -- This is for use within the run-time system, so abort is assumed to be
462 -- already deferred, and the caller should be holding its own ATCB lock.
464 procedure Timed_Sleep
467 Mode : ST.Delay_Modes;
468 Reason : System.Tasking.Task_States;
469 Timedout : out Boolean;
470 Yielded : out Boolean)
472 pragma Unreferenced (Reason);
474 Orig : constant Duration := Monotonic_Clock;
478 Wakeup : Boolean := False;
484 if Mode = Relative then
485 Absolute := Orig + Time;
487 -- Systematically add one since the first tick will delay *at most*
488 -- 1 / Rate_Duration seconds, so we need to add one to be on the
491 Ticks := To_Clock_Ticks (Time);
493 if Ticks > 0 and then Ticks < int'Last then
499 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
504 -- Release the mutex before sleeping
507 semGive (if Single_Lock
508 then Single_RTS_Lock.Mutex
509 else Self_ID.Common.LL.L.Mutex);
510 pragma Assert (Result = 0);
512 -- Perform a blocking operation to take the CV semaphore. Note
513 -- that a blocking operation in VxWorks will reenable task
514 -- scheduling. When we are no longer blocked and control is
515 -- returned, task scheduling will again be disabled.
517 Result := semTake (Self_ID.Common.LL.CV, Ticks);
521 -- Somebody may have called Wakeup for us
526 if errno /= S_objLib_OBJ_TIMEOUT then
530 -- If Ticks = int'last, it was most probably truncated so
531 -- let's make another round after recomputing Ticks from
532 -- the absolute time.
534 if Ticks /= int'Last then
538 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
547 -- Take the mutex back
550 semTake ((if Single_Lock
551 then Single_RTS_Lock.Mutex
552 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
553 pragma Assert (Result = 0);
555 exit when Timedout or Wakeup;
561 -- Should never hold a lock while yielding
564 Result := semGive (Single_RTS_Lock.Mutex);
566 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
569 Result := semGive (Self_ID.Common.LL.L.Mutex);
571 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
580 -- This is for use in implementing delay statements, so we assume the
581 -- caller is holding no locks.
583 procedure Timed_Delay
586 Mode : ST.Delay_Modes)
588 Orig : constant Duration := Monotonic_Clock;
592 Aborted : Boolean := False;
595 pragma Warnings (Off, Result);
598 if Mode = Relative then
599 Absolute := Orig + Time;
600 Ticks := To_Clock_Ticks (Time);
602 if Ticks > 0 and then Ticks < int'Last then
604 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
605 -- seconds, so we need to add one to be on the safe side.
612 Ticks := To_Clock_Ticks (Time - Orig);
617 -- Modifying State, locking the TCB
620 semTake ((if Single_Lock
621 then Single_RTS_Lock.Mutex
622 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
624 pragma Assert (Result = 0);
626 Self_ID.Common.State := Delay_Sleep;
630 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
632 -- Release the TCB before sleeping
635 semGive (if Single_Lock
636 then Single_RTS_Lock.Mutex
637 else Self_ID.Common.LL.L.Mutex);
638 pragma Assert (Result = 0);
642 Result := semTake (Self_ID.Common.LL.CV, Ticks);
646 -- If Ticks = int'last, it was most probably truncated
647 -- so let's make another round after recomputing Ticks
648 -- from the absolute time.
650 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
653 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
661 -- Take back the lock after having slept, to protect further
662 -- access to Self_ID.
667 then Single_RTS_Lock.Mutex
668 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
670 pragma Assert (Result = 0);
675 Self_ID.Common.State := Runnable;
680 then Single_RTS_Lock.Mutex
681 else Self_ID.Common.LL.L.Mutex);
688 ---------------------
689 -- Monotonic_Clock --
690 ---------------------
692 function Monotonic_Clock return Duration is
693 TS : aliased timespec;
696 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
697 pragma Assert (Result = 0);
698 return To_Duration (TS);
705 function RT_Resolution return Duration is
707 return 1.0 / Duration (sysClkRateGet);
714 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
715 pragma Unreferenced (Reason);
718 Result := semGive (T.Common.LL.CV);
719 pragma Assert (Result = 0);
726 procedure Yield (Do_Yield : Boolean := True) is
727 pragma Unreferenced (Do_Yield);
729 pragma Unreferenced (Result);
731 Result := taskDelay (0);
738 procedure Set_Priority
740 Prio : System.Any_Priority;
741 Loss_Of_Inheritance : Boolean := False)
743 pragma Unreferenced (Loss_Of_Inheritance);
750 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
751 pragma Assert (Result = 0);
753 -- Note: in VxWorks 6.6 (or earlier), the task is placed at the end of
754 -- the priority queue instead of the head. This is not the behavior
755 -- required by Annex D (RM D.2.3(5/2)), but we consider it an acceptable
756 -- variation (RM 1.1.3(6)), given this is the built-in behavior of the
757 -- operating system. VxWorks versions starting from 6.7 implement the
758 -- required Annex D semantics.
760 -- In older versions we attempted to better approximate the Annex D
761 -- required behavior, but this simulation was not entirely accurate,
762 -- and it seems better to live with the standard VxWorks semantics.
764 T.Common.Current_Priority := Prio;
771 function Get_Priority (T : Task_Id) return System.Any_Priority is
773 return T.Common.Current_Priority;
780 procedure Enter_Task (Self_ID : Task_Id) is
781 procedure Init_Float;
782 pragma Import (C, Init_Float, "__gnat_init_float");
783 -- Properly initializes the FPU for PPC/MIPS systems
786 -- Store the user-level task id in the Thread field (to be used
787 -- internally by the run-time system) and the kernel-level task id in
788 -- the LWP field (to be used by the debugger).
790 Self_ID.Common.LL.Thread := taskIdSelf;
791 Self_ID.Common.LL.LWP := getpid;
793 Specific.Set (Self_ID);
797 -- Install the signal handlers
799 -- This is called for each task since there is no signal inheritance
800 -- between VxWorks tasks.
802 Install_Signal_Handlers;
804 -- If stack checking is enabled, set the stack limit for this task
806 if Set_Stack_Limit_Hook /= null then
807 Set_Stack_Limit_Hook.all;
815 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
817 return new Ada_Task_Control_Block (Entry_Num);
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
832 if Is_Valid_Task then
835 return Register_Foreign_Thread (taskIdSelf);
837 end Register_Foreign_Thread;
843 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
845 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
846 Self_ID.Common.LL.Thread := 0;
848 if Self_ID.Common.LL.CV = 0 then
854 if not Single_Lock then
855 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 Adjusted_Stack_Size : size_t;
874 use System.Task_Info;
875 use type System.Multiprocessors.CPU_Range;
878 -- Ask for four extra bytes of stack space so that the ATCB pointer can
879 -- be stored below the stack limit, plus extra space for the frame of
880 -- Task_Wrapper. This is so the user gets the amount of stack requested
881 -- exclusive of the needs.
883 -- We also have to allocate n more bytes for the task name storage and
884 -- enough space for the Wind Task Control Block which is around 0x778
885 -- bytes. VxWorks also seems to carve out additional space, so use 2048
886 -- as a nice round number. We might want to increment to the nearest
887 -- page size in case we ever support VxVMI.
889 -- ??? - we should come back and visit this so we can set the task name
890 -- to something appropriate.
892 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
894 -- Since the initial signal mask of a thread is inherited from the
895 -- creator, and the Environment task has all its signals masked, we do
896 -- not need to manipulate caller's signal mask at this point. All tasks
897 -- in RTS will have All_Tasks_Mask initially.
899 -- We now compute the VxWorks task name and options, then spawn ...
902 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
903 Name_Address : System.Address;
904 -- Task name we are going to hand down to VxWorks
906 function Get_Task_Options return int;
907 pragma Import (C, Get_Task_Options, "__gnat_get_task_options");
908 -- Function that returns the options to be set for the task that we
909 -- are creating. We fetch the options assigned to the current task,
910 -- so offering some user level control over the options for a task
911 -- hierarchy, and force VX_FP_TASK because it is almost always
915 -- If there is no Ada task name handy, let VxWorks choose one.
916 -- Otherwise, tell VxWorks what the Ada task name is.
918 if T.Common.Task_Image_Len = 0 then
919 Name_Address := System.Null_Address;
921 Name (1 .. Name'Last - 1) :=
922 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
923 Name (Name'Last) := ASCII.NUL;
924 Name_Address := Name'Address;
927 -- Now spawn the VxWorks task for real
929 T.Common.LL.Thread :=
932 To_VxWorks_Priority (int (Priority)),
939 -- Set processor affinity
941 if T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
943 taskCpuAffinitySet (T.Common.LL.Thread, int (T.Common.Base_CPU));
945 elsif T.Common.Task_Info /= Unspecified_Task_Info then
947 taskCpuAffinitySet (T.Common.LL.Thread, T.Common.Task_Info);
951 taskDelete (T.Common.LL.Thread);
952 T.Common.LL.Thread := -1;
955 if T.Common.LL.Thread = -1 then
959 Task_Creation_Hook (T.Common.LL.Thread);
960 Set_Priority (T, Priority);
968 procedure Finalize_TCB (T : Task_Id) is
971 Is_Self : constant Boolean := (T = Self);
973 procedure Free is new
974 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
977 if not Single_Lock then
978 Result := semDelete (T.Common.LL.L.Mutex);
979 pragma Assert (Result = 0);
982 T.Common.LL.Thread := 0;
984 Result := semDelete (T.Common.LL.CV);
985 pragma Assert (Result = 0);
987 if T.Known_Tasks_Index /= -1 then
988 Known_Tasks (T.Known_Tasks_Index) := null;
1002 procedure Exit_Task is
1004 Specific.Set (null);
1011 procedure Abort_Task (T : Task_Id) is
1016 (T.Common.LL.Thread,
1017 Signal (Interrupt_Management.Abort_Task_Interrupt));
1018 pragma Assert (Result = 0);
1025 procedure Initialize (S : in out Suspension_Object) is
1027 -- Initialize internal state (always to False (RM D.10(6)))
1032 -- Initialize internal mutex
1034 -- Use simpler binary semaphore instead of VxWorks
1035 -- mutual exclusion semaphore, because we don't need
1036 -- the fancier semantics and their overhead.
1038 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1040 -- Initialize internal condition variable
1042 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1049 procedure Finalize (S : in out Suspension_Object) is
1050 pragma Unmodified (S);
1051 -- S may be modified on other targets, but not on VxWorks
1056 -- Destroy internal mutex
1058 Result := semDelete (S.L);
1059 pragma Assert (Result = OK);
1061 -- Destroy internal condition variable
1063 Result := semDelete (S.CV);
1064 pragma Assert (Result = OK);
1071 function Current_State (S : Suspension_Object) return Boolean is
1073 -- We do not want to use lock on this read operation. State is marked
1074 -- as Atomic so that we ensure that the value retrieved is correct.
1083 procedure Set_False (S : in out Suspension_Object) is
1087 SSL.Abort_Defer.all;
1089 Result := semTake (S.L, WAIT_FOREVER);
1090 pragma Assert (Result = OK);
1094 Result := semGive (S.L);
1095 pragma Assert (Result = OK);
1097 SSL.Abort_Undefer.all;
1104 procedure Set_True (S : in out Suspension_Object) is
1108 -- Set_True can be called from an interrupt context, in which case
1109 -- Abort_Defer is undefined.
1111 if Is_Task_Context then
1112 SSL.Abort_Defer.all;
1115 Result := semTake (S.L, WAIT_FOREVER);
1116 pragma Assert (Result = OK);
1118 -- If there is already a task waiting on this suspension object then
1119 -- we resume it, leaving the state of the suspension object to False,
1120 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1121 -- the state to True.
1127 Result := semGive (S.CV);
1128 pragma Assert (Result = OK);
1133 Result := semGive (S.L);
1134 pragma Assert (Result = OK);
1136 -- Set_True can be called from an interrupt context, in which case
1137 -- Abort_Undefer is undefined.
1139 if Is_Task_Context then
1140 SSL.Abort_Undefer.all;
1145 ------------------------
1146 -- Suspend_Until_True --
1147 ------------------------
1149 procedure Suspend_Until_True (S : in out Suspension_Object) is
1153 SSL.Abort_Defer.all;
1155 Result := semTake (S.L, WAIT_FOREVER);
1159 -- Program_Error must be raised upon calling Suspend_Until_True
1160 -- if another task is already waiting on that suspension object
1161 -- (ARM D.10 par. 10).
1163 Result := semGive (S.L);
1164 pragma Assert (Result = OK);
1166 SSL.Abort_Undefer.all;
1168 raise Program_Error;
1171 -- Suspend the task if the state is False. Otherwise, the task
1172 -- continues its execution, and the state of the suspension object
1173 -- is set to False (ARM D.10 par. 9).
1178 Result := semGive (S.L);
1179 pragma Assert (Result = 0);
1181 SSL.Abort_Undefer.all;
1186 -- Release the mutex before sleeping
1188 Result := semGive (S.L);
1189 pragma Assert (Result = OK);
1191 SSL.Abort_Undefer.all;
1193 Result := semTake (S.CV, WAIT_FOREVER);
1194 pragma Assert (Result = 0);
1197 end Suspend_Until_True;
1205 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1206 pragma Unreferenced (Self_ID);
1211 --------------------
1212 -- Check_No_Locks --
1213 --------------------
1215 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1216 pragma Unreferenced (Self_ID);
1221 ----------------------
1222 -- Environment_Task --
1223 ----------------------
1225 function Environment_Task return Task_Id is
1227 return Environment_Task_Id;
1228 end Environment_Task;
1234 procedure Lock_RTS is
1236 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1243 procedure Unlock_RTS is
1245 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1252 function Suspend_Task
1254 Thread_Self : Thread_Id) return Boolean
1257 if T.Common.LL.Thread /= 0
1258 and then T.Common.LL.Thread /= Thread_Self
1260 return taskSuspend (T.Common.LL.Thread) = 0;
1270 function Resume_Task
1272 Thread_Self : Thread_Id) return Boolean
1275 if T.Common.LL.Thread /= 0
1276 and then T.Common.LL.Thread /= Thread_Self
1278 return taskResume (T.Common.LL.Thread) = 0;
1284 --------------------
1285 -- Stop_All_Tasks --
1286 --------------------
1288 procedure Stop_All_Tasks
1290 Thread_Self : constant Thread_Id := taskIdSelf;
1294 pragma Unreferenced (Dummy);
1299 C := All_Tasks_List;
1300 while C /= null loop
1301 if C.Common.LL.Thread /= 0
1302 and then C.Common.LL.Thread /= Thread_Self
1304 Dummy := Task_Stop (C.Common.LL.Thread);
1307 C := C.Common.All_Tasks_Link;
1310 Dummy := Int_Unlock;
1317 function Stop_Task (T : ST.Task_Id) return Boolean is
1319 if T.Common.LL.Thread /= 0 then
1320 return Task_Stop (T.Common.LL.Thread) = 0;
1330 function Continue_Task (T : ST.Task_Id) return Boolean
1333 if T.Common.LL.Thread /= 0 then
1334 return Task_Cont (T.Common.LL.Thread) = 0;
1340 ---------------------
1341 -- Is_Task_Context --
1342 ---------------------
1344 function Is_Task_Context return Boolean is
1346 return System.OS_Interface.Interrupt_Context /= 1;
1347 end Is_Task_Context;
1353 procedure Initialize (Environment_Task : Task_Id) is
1356 use type System.Multiprocessors.CPU_Range;
1359 Environment_Task_Id := Environment_Task;
1361 Interrupt_Management.Initialize;
1362 Specific.Initialize;
1364 if Locking_Policy = 'C' then
1365 Mutex_Protocol := Prio_Protect;
1366 elsif Locking_Policy = 'I' then
1367 Mutex_Protocol := Prio_Inherit;
1369 Mutex_Protocol := Prio_None;
1372 if Time_Slice_Val > 0 then
1376 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1378 elsif Dispatching_Policy = 'R' then
1379 Result := Set_Time_Slice (To_Clock_Ticks (0.01));
1383 Result := sigemptyset (Unblocked_Signal_Mask'Access);
1384 pragma Assert (Result = 0);
1386 for J in Interrupt_Management.Signal_ID loop
1387 if System.Interrupt_Management.Keep_Unmasked (J) then
1388 Result := sigaddset (Unblocked_Signal_Mask'Access, Signal (J));
1389 pragma Assert (Result = 0);
1393 -- Initialize the lock used to synchronize chain of all ATCBs
1395 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1397 -- Make environment task known here because it doesn't go through
1398 -- Activate_Tasks, which does it for all other tasks.
1400 Known_Tasks (Known_Tasks'First) := Environment_Task;
1401 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1403 Enter_Task (Environment_Task);
1405 -- Set processor affinity
1407 if Environment_Task.Common.Base_CPU /=
1408 System.Multiprocessors.Not_A_Specific_CPU
1412 (Environment_Task.Common.LL.Thread,
1413 int (Environment_Task.Common.Base_CPU));
1414 pragma Assert (Result /= -1);
1418 end System.Task_Primitives.Operations;