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-2011, 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;
49 with System.Float_Control;
51 with System.Soft_Links;
52 -- We use System.Soft_Links instead of System.Tasking.Initialization
53 -- because the later is a higher level package that we shouldn't depend
54 -- on. For example when using the restricted run time, it is replaced by
55 -- System.Tasking.Restricted.Stages.
57 with System.Task_Info;
58 with System.VxWorks.Ext;
60 package body System.Task_Primitives.Operations is
62 package SSL renames System.Soft_Links;
64 use System.Tasking.Debug;
66 use System.OS_Interface;
67 use System.Parameters;
68 use type System.VxWorks.Ext.t_id;
69 use type Interfaces.C.int;
71 subtype int is System.OS_Interface.int;
73 Relative : constant := 0;
79 -- The followings are logically constants, but need to be initialized at
82 Environment_Task_Id : Task_Id;
83 -- A variable to hold Task_Id for the environment task
85 -- The followings are internal configuration constants needed
87 Dispatching_Policy : Character;
88 pragma Import (C, Dispatching_Policy, "__gl_task_dispatching_policy");
90 Foreign_Task_Elaborated : aliased Boolean := True;
91 -- Used to identified fake tasks (i.e., non-Ada Threads)
93 Locking_Policy : Character;
94 pragma Import (C, Locking_Policy, "__gl_locking_policy");
96 Mutex_Protocol : Priority_Type;
98 Single_RTS_Lock : aliased RTS_Lock;
99 -- This is a lock to allow only one thread of control in the RTS at a
100 -- time; it is used to execute in mutual exclusion from all other tasks.
101 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
103 Time_Slice_Val : Integer;
104 pragma Import (C, Time_Slice_Val, "__gl_time_slice_val");
112 procedure Initialize;
113 pragma Inline (Initialize);
114 -- Initialize task specific data
116 function Is_Valid_Task return Boolean;
117 pragma Inline (Is_Valid_Task);
118 -- Does executing thread have a TCB?
120 procedure Set (Self_Id : Task_Id);
122 -- Set the self id for the current task
125 pragma Inline (Delete);
126 -- Delete the task specific data associated with the current task
128 function Self return Task_Id;
129 pragma Inline (Self);
130 -- Return a pointer to the Ada Task Control Block of the calling task
134 package body Specific is separate;
135 -- The body of this package is target specific
137 ---------------------------------
138 -- Support for foreign threads --
139 ---------------------------------
141 function Register_Foreign_Thread (Thread : Thread_Id) return Task_Id;
142 -- Allocate and Initialize a new ATCB for the current Thread
144 function Register_Foreign_Thread
145 (Thread : Thread_Id) return Task_Id is separate;
147 -----------------------
148 -- Local Subprograms --
149 -----------------------
151 procedure Abort_Handler (signo : Signal);
152 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
154 procedure Install_Signal_Handlers;
155 -- Install the default signal handlers for the current task
157 function Is_Task_Context return Boolean;
158 -- This function returns True if the current execution is in the context
159 -- of a task, and False if it is an interrupt context.
161 type Set_Stack_Limit_Proc_Acc is access procedure;
162 pragma Convention (C, Set_Stack_Limit_Proc_Acc);
164 Set_Stack_Limit_Hook : Set_Stack_Limit_Proc_Acc;
165 pragma Import (C, Set_Stack_Limit_Hook, "__gnat_set_stack_limit_hook");
166 -- Procedure to be called when a task is created to set stack
167 -- limit. Used only for VxWorks 5 and VxWorks MILS guest OS.
169 function To_Address is
170 new Ada.Unchecked_Conversion (Task_Id, System.Address);
176 procedure Abort_Handler (signo : Signal) is
177 pragma Unreferenced (signo);
179 Self_ID : constant Task_Id := Self;
180 Old_Set : aliased sigset_t;
181 Unblocked_Mask : aliased sigset_t;
183 pragma Warnings (Off, Result);
185 use System.Interrupt_Management;
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 purposes are unmasked
203 Result := sigemptyset (Unblocked_Mask'Access);
204 pragma Assert (Result = 0);
207 (Unblocked_Mask'Access,
208 Signal (Abort_Task_Interrupt));
209 pragma Assert (Result = 0);
210 Result := sigaddset (Unblocked_Mask'Access, SIGBUS);
211 pragma Assert (Result = 0);
212 Result := sigaddset (Unblocked_Mask'Access, SIGFPE);
213 pragma Assert (Result = 0);
214 Result := sigaddset (Unblocked_Mask'Access, SIGILL);
215 pragma Assert (Result = 0);
216 Result := sigaddset (Unblocked_Mask'Access, SIGSEGV);
217 pragma Assert (Result = 0);
222 Unblocked_Mask'Access,
224 pragma Assert (Result = 0);
226 raise Standard'Abort_Signal;
234 procedure Stack_Guard (T : ST.Task_Id; On : Boolean) is
235 pragma Unreferenced (T);
236 pragma Unreferenced (On);
239 -- Nothing needed (why not???)
248 function Get_Thread_Id (T : ST.Task_Id) return OSI.Thread_Id is
250 return T.Common.LL.Thread;
257 function Self return Task_Id renames Specific.Self;
259 -----------------------------
260 -- Install_Signal_Handlers --
261 -----------------------------
263 procedure Install_Signal_Handlers is
264 act : aliased struct_sigaction;
265 old_act : aliased struct_sigaction;
266 Tmp_Set : aliased sigset_t;
271 act.sa_handler := Abort_Handler'Address;
273 Result := sigemptyset (Tmp_Set'Access);
274 pragma Assert (Result = 0);
275 act.sa_mask := Tmp_Set;
279 (Signal (Interrupt_Management.Abort_Task_Interrupt),
280 act'Unchecked_Access,
281 old_act'Unchecked_Access);
282 pragma Assert (Result = 0);
284 Interrupt_Management.Initialize_Interrupts;
285 end Install_Signal_Handlers;
287 ---------------------
288 -- Initialize_Lock --
289 ---------------------
291 procedure Initialize_Lock
292 (Prio : System.Any_Priority;
293 L : not null access Lock)
296 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
297 L.Prio_Ceiling := int (Prio);
298 L.Protocol := Mutex_Protocol;
299 pragma Assert (L.Mutex /= 0);
302 procedure Initialize_Lock
303 (L : not null access RTS_Lock;
306 pragma Unreferenced (Level);
308 L.Mutex := semMCreate (SEM_Q_PRIORITY + SEM_INVERSION_SAFE);
309 L.Prio_Ceiling := int (System.Any_Priority'Last);
310 L.Protocol := Mutex_Protocol;
311 pragma Assert (L.Mutex /= 0);
318 procedure Finalize_Lock (L : not null access Lock) is
321 Result := semDelete (L.Mutex);
322 pragma Assert (Result = 0);
325 procedure Finalize_Lock (L : not null access RTS_Lock) is
328 Result := semDelete (L.Mutex);
329 pragma Assert (Result = 0);
337 (L : not null access Lock;
338 Ceiling_Violation : out Boolean)
343 if L.Protocol = Prio_Protect
344 and then int (Self.Common.Current_Priority) > L.Prio_Ceiling
346 Ceiling_Violation := True;
349 Ceiling_Violation := False;
352 Result := semTake (L.Mutex, WAIT_FOREVER);
353 pragma Assert (Result = 0);
357 (L : not null access RTS_Lock;
358 Global_Lock : Boolean := False)
362 if not Single_Lock or else Global_Lock then
363 Result := semTake (L.Mutex, WAIT_FOREVER);
364 pragma Assert (Result = 0);
368 procedure Write_Lock (T : Task_Id) is
371 if not Single_Lock then
372 Result := semTake (T.Common.LL.L.Mutex, WAIT_FOREVER);
373 pragma Assert (Result = 0);
382 (L : not null access Lock;
383 Ceiling_Violation : out Boolean)
386 Write_Lock (L, Ceiling_Violation);
393 procedure Unlock (L : not null access Lock) is
396 Result := semGive (L.Mutex);
397 pragma Assert (Result = 0);
401 (L : not null access RTS_Lock;
402 Global_Lock : Boolean := False)
406 if not Single_Lock or else Global_Lock then
407 Result := semGive (L.Mutex);
408 pragma Assert (Result = 0);
412 procedure Unlock (T : Task_Id) is
415 if not Single_Lock then
416 Result := semGive (T.Common.LL.L.Mutex);
417 pragma Assert (Result = 0);
425 -- Dynamic priority ceilings are not supported by the underlying system
427 procedure Set_Ceiling
428 (L : not null access Lock;
429 Prio : System.Any_Priority)
431 pragma Unreferenced (L, Prio);
440 procedure Sleep (Self_ID : Task_Id; Reason : System.Tasking.Task_States) is
441 pragma Unreferenced (Reason);
446 pragma Assert (Self_ID = Self);
448 -- Release the mutex before sleeping
451 semGive (if Single_Lock
452 then Single_RTS_Lock.Mutex
453 else Self_ID.Common.LL.L.Mutex);
454 pragma Assert (Result = 0);
456 -- Perform a blocking operation to take the CV semaphore. Note that a
457 -- blocking operation in VxWorks will reenable task scheduling. When we
458 -- are no longer blocked and control is returned, task scheduling will
459 -- again be disabled.
461 Result := semTake (Self_ID.Common.LL.CV, WAIT_FOREVER);
462 pragma Assert (Result = 0);
464 -- Take the mutex back
467 semTake ((if Single_Lock
468 then Single_RTS_Lock.Mutex
469 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
470 pragma Assert (Result = 0);
477 -- This is for use within the run-time system, so abort is assumed to be
478 -- already deferred, and the caller should be holding its own ATCB lock.
480 procedure Timed_Sleep
483 Mode : ST.Delay_Modes;
484 Reason : System.Tasking.Task_States;
485 Timedout : out Boolean;
486 Yielded : out Boolean)
488 pragma Unreferenced (Reason);
490 Orig : constant Duration := Monotonic_Clock;
494 Wakeup : Boolean := False;
500 if Mode = Relative then
501 Absolute := Orig + Time;
503 -- Systematically add one since the first tick will delay *at most*
504 -- 1 / Rate_Duration seconds, so we need to add one to be on the
507 Ticks := To_Clock_Ticks (Time);
509 if Ticks > 0 and then Ticks < int'Last then
515 Ticks := To_Clock_Ticks (Time - Monotonic_Clock);
520 -- Release the mutex before sleeping
523 semGive (if Single_Lock
524 then Single_RTS_Lock.Mutex
525 else Self_ID.Common.LL.L.Mutex);
526 pragma Assert (Result = 0);
528 -- Perform a blocking operation to take the CV semaphore. Note
529 -- that a blocking operation in VxWorks will reenable task
530 -- scheduling. When we are no longer blocked and control is
531 -- returned, task scheduling will again be disabled.
533 Result := semTake (Self_ID.Common.LL.CV, Ticks);
537 -- Somebody may have called Wakeup for us
542 if errno /= S_objLib_OBJ_TIMEOUT then
546 -- If Ticks = int'last, it was most probably truncated so
547 -- let's make another round after recomputing Ticks from
548 -- the absolute time.
550 if Ticks /= int'Last then
554 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
563 -- Take the mutex back
566 semTake ((if Single_Lock
567 then Single_RTS_Lock.Mutex
568 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
569 pragma Assert (Result = 0);
571 exit when Timedout or Wakeup;
577 -- Should never hold a lock while yielding
580 Result := semGive (Single_RTS_Lock.Mutex);
582 Result := semTake (Single_RTS_Lock.Mutex, WAIT_FOREVER);
585 Result := semGive (Self_ID.Common.LL.L.Mutex);
587 Result := semTake (Self_ID.Common.LL.L.Mutex, WAIT_FOREVER);
596 -- This is for use in implementing delay statements, so we assume the
597 -- caller is holding no locks.
599 procedure Timed_Delay
602 Mode : ST.Delay_Modes)
604 Orig : constant Duration := Monotonic_Clock;
608 Aborted : Boolean := False;
611 pragma Warnings (Off, Result);
614 if Mode = Relative then
615 Absolute := Orig + Time;
616 Ticks := To_Clock_Ticks (Time);
618 if Ticks > 0 and then Ticks < int'Last then
620 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
621 -- seconds, so we need to add one to be on the safe side.
628 Ticks := To_Clock_Ticks (Time - Orig);
633 -- Modifying State, locking the TCB
636 semTake ((if Single_Lock
637 then Single_RTS_Lock.Mutex
638 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
640 pragma Assert (Result = 0);
642 Self_ID.Common.State := Delay_Sleep;
646 Aborted := Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;
648 -- Release the TCB before sleeping
651 semGive (if Single_Lock
652 then Single_RTS_Lock.Mutex
653 else Self_ID.Common.LL.L.Mutex);
654 pragma Assert (Result = 0);
658 Result := semTake (Self_ID.Common.LL.CV, Ticks);
662 -- If Ticks = int'last, it was most probably truncated
663 -- so let's make another round after recomputing Ticks
664 -- from the absolute time.
666 if errno = S_objLib_OBJ_TIMEOUT and then Ticks /= int'Last then
669 Ticks := To_Clock_Ticks (Absolute - Monotonic_Clock);
677 -- Take back the lock after having slept, to protect further
678 -- access to Self_ID.
683 then Single_RTS_Lock.Mutex
684 else Self_ID.Common.LL.L.Mutex), WAIT_FOREVER);
686 pragma Assert (Result = 0);
691 Self_ID.Common.State := Runnable;
696 then Single_RTS_Lock.Mutex
697 else Self_ID.Common.LL.L.Mutex);
704 ---------------------
705 -- Monotonic_Clock --
706 ---------------------
708 function Monotonic_Clock return Duration is
709 TS : aliased timespec;
712 Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access);
713 pragma Assert (Result = 0);
714 return To_Duration (TS);
721 function RT_Resolution return Duration is
723 return 1.0 / Duration (sysClkRateGet);
730 procedure Wakeup (T : Task_Id; Reason : System.Tasking.Task_States) is
731 pragma Unreferenced (Reason);
734 Result := semGive (T.Common.LL.CV);
735 pragma Assert (Result = 0);
742 procedure Yield (Do_Yield : Boolean := True) is
743 pragma Unreferenced (Do_Yield);
745 pragma Unreferenced (Result);
747 Result := taskDelay (0);
754 procedure Set_Priority
756 Prio : System.Any_Priority;
757 Loss_Of_Inheritance : Boolean := False)
759 pragma Unreferenced (Loss_Of_Inheritance);
766 (T.Common.LL.Thread, To_VxWorks_Priority (int (Prio)));
767 pragma Assert (Result = 0);
769 -- Note: in VxWorks 6.6 (or earlier), the task is placed at the end of
770 -- the priority queue instead of the head. This is not the behavior
771 -- required by Annex D (RM D.2.3(5/2)), but we consider it an acceptable
772 -- variation (RM 1.1.3(6)), given this is the built-in behavior of the
773 -- operating system. VxWorks versions starting from 6.7 implement the
774 -- required Annex D semantics.
776 -- In older versions we attempted to better approximate the Annex D
777 -- required behavior, but this simulation was not entirely accurate,
778 -- and it seems better to live with the standard VxWorks semantics.
780 T.Common.Current_Priority := Prio;
787 function Get_Priority (T : Task_Id) return System.Any_Priority is
789 return T.Common.Current_Priority;
796 procedure Enter_Task (Self_ID : Task_Id) is
798 -- Store the user-level task id in the Thread field (to be used
799 -- internally by the run-time system) and the kernel-level task id in
800 -- the LWP field (to be used by the debugger).
802 Self_ID.Common.LL.Thread := taskIdSelf;
803 Self_ID.Common.LL.LWP := getpid;
805 Specific.Set (Self_ID);
807 -- Properly initializes the FPU for PPC/MIPS systems
809 System.Float_Control.Reset;
811 -- Install the signal handlers
813 -- This is called for each task since there is no signal inheritance
814 -- between VxWorks tasks.
816 Install_Signal_Handlers;
818 -- If stack checking is enabled, set the stack limit for this task
820 if Set_Stack_Limit_Hook /= null then
821 Set_Stack_Limit_Hook.all;
829 function New_ATCB (Entry_Num : Task_Entry_Index) return Task_Id is
831 return new Ada_Task_Control_Block (Entry_Num);
838 function Is_Valid_Task return Boolean renames Specific.Is_Valid_Task;
840 -----------------------------
841 -- Register_Foreign_Thread --
842 -----------------------------
844 function Register_Foreign_Thread return Task_Id is
846 if Is_Valid_Task then
849 return Register_Foreign_Thread (taskIdSelf);
851 end Register_Foreign_Thread;
857 procedure Initialize_TCB (Self_ID : Task_Id; Succeeded : out Boolean) is
859 Self_ID.Common.LL.CV := semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
860 Self_ID.Common.LL.Thread := 0;
862 if Self_ID.Common.LL.CV = 0 then
868 if not Single_Lock then
869 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 Adjusted_Stack_Size : size_t;
888 use System.Task_Info;
889 use type System.Multiprocessors.CPU_Range;
892 -- Ask for four extra bytes of stack space so that the ATCB pointer can
893 -- be stored below the stack limit, plus extra space for the frame of
894 -- Task_Wrapper. This is so the user gets the amount of stack requested
895 -- exclusive of the needs.
897 -- We also have to allocate n more bytes for the task name storage and
898 -- enough space for the Wind Task Control Block which is around 0x778
899 -- bytes. VxWorks also seems to carve out additional space, so use 2048
900 -- as a nice round number. We might want to increment to the nearest
901 -- page size in case we ever support VxVMI.
903 -- ??? - we should come back and visit this so we can set the task name
904 -- to something appropriate.
906 Adjusted_Stack_Size := size_t (Stack_Size) + 2048;
908 -- Since the initial signal mask of a thread is inherited from the
909 -- creator, and the Environment task has all its signals masked, we do
910 -- not need to manipulate caller's signal mask at this point. All tasks
911 -- in RTS will have All_Tasks_Mask initially.
913 -- We now compute the VxWorks task name and options, then spawn ...
916 Name : aliased String (1 .. T.Common.Task_Image_Len + 1);
917 Name_Address : System.Address;
918 -- Task name we are going to hand down to VxWorks
920 function Get_Task_Options return int;
921 pragma Import (C, Get_Task_Options, "__gnat_get_task_options");
922 -- Function that returns the options to be set for the task that we
923 -- are creating. We fetch the options assigned to the current task,
924 -- so offering some user level control over the options for a task
925 -- hierarchy, and force VX_FP_TASK because it is almost always
929 -- If there is no Ada task name handy, let VxWorks choose one.
930 -- Otherwise, tell VxWorks what the Ada task name is.
932 if T.Common.Task_Image_Len = 0 then
933 Name_Address := System.Null_Address;
935 Name (1 .. Name'Last - 1) :=
936 T.Common.Task_Image (1 .. T.Common.Task_Image_Len);
937 Name (Name'Last) := ASCII.NUL;
938 Name_Address := Name'Address;
941 -- Now spawn the VxWorks task for real
943 T.Common.LL.Thread :=
946 To_VxWorks_Priority (int (Priority)),
953 -- Set processor affinity
955 if T.Common.Base_CPU /= System.Multiprocessors.Not_A_Specific_CPU then
956 -- Ada 2012 pragma CPU uses CPU numbers starting from 1, while
957 -- on VxWorks the first CPU is identified by a 0, so we need to
962 (T.Common.LL.Thread, int (T.Common.Base_CPU) - 1);
964 elsif T.Common.Task_Info /= Unspecified_Task_Info then
966 taskCpuAffinitySet (T.Common.LL.Thread, T.Common.Task_Info);
970 taskDelete (T.Common.LL.Thread);
971 T.Common.LL.Thread := -1;
974 if T.Common.LL.Thread = -1 then
978 Task_Creation_Hook (T.Common.LL.Thread);
979 Set_Priority (T, Priority);
987 procedure Finalize_TCB (T : Task_Id) is
990 Is_Self : constant Boolean := (T = Self);
992 procedure Free is new
993 Ada.Unchecked_Deallocation (Ada_Task_Control_Block, Task_Id);
996 if not Single_Lock then
997 Result := semDelete (T.Common.LL.L.Mutex);
998 pragma Assert (Result = 0);
1001 T.Common.LL.Thread := 0;
1003 Result := semDelete (T.Common.LL.CV);
1004 pragma Assert (Result = 0);
1006 if T.Known_Tasks_Index /= -1 then
1007 Known_Tasks (T.Known_Tasks_Index) := null;
1021 procedure Exit_Task is
1023 Specific.Set (null);
1030 procedure Abort_Task (T : Task_Id) is
1035 (T.Common.LL.Thread,
1036 Signal (Interrupt_Management.Abort_Task_Interrupt));
1037 pragma Assert (Result = 0);
1044 procedure Initialize (S : in out Suspension_Object) is
1046 -- Initialize internal state (always to False (RM D.10(6)))
1051 -- Initialize internal mutex
1053 -- Use simpler binary semaphore instead of VxWorks
1054 -- mutual exclusion semaphore, because we don't need
1055 -- the fancier semantics and their overhead.
1057 S.L := semBCreate (SEM_Q_FIFO, SEM_FULL);
1059 -- Initialize internal condition variable
1061 S.CV := semBCreate (SEM_Q_FIFO, SEM_EMPTY);
1068 procedure Finalize (S : in out Suspension_Object) is
1069 pragma Unmodified (S);
1070 -- S may be modified on other targets, but not on VxWorks
1075 -- Destroy internal mutex
1077 Result := semDelete (S.L);
1078 pragma Assert (Result = OK);
1080 -- Destroy internal condition variable
1082 Result := semDelete (S.CV);
1083 pragma Assert (Result = OK);
1090 function Current_State (S : Suspension_Object) return Boolean is
1092 -- We do not want to use lock on this read operation. State is marked
1093 -- as Atomic so that we ensure that the value retrieved is correct.
1102 procedure Set_False (S : in out Suspension_Object) is
1106 SSL.Abort_Defer.all;
1108 Result := semTake (S.L, WAIT_FOREVER);
1109 pragma Assert (Result = OK);
1113 Result := semGive (S.L);
1114 pragma Assert (Result = OK);
1116 SSL.Abort_Undefer.all;
1123 procedure Set_True (S : in out Suspension_Object) is
1127 -- Set_True can be called from an interrupt context, in which case
1128 -- Abort_Defer is undefined.
1130 if Is_Task_Context then
1131 SSL.Abort_Defer.all;
1134 Result := semTake (S.L, WAIT_FOREVER);
1135 pragma Assert (Result = OK);
1137 -- If there is already a task waiting on this suspension object then
1138 -- we resume it, leaving the state of the suspension object to False,
1139 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1140 -- the state to True.
1146 Result := semGive (S.CV);
1147 pragma Assert (Result = OK);
1152 Result := semGive (S.L);
1153 pragma Assert (Result = OK);
1155 -- Set_True can be called from an interrupt context, in which case
1156 -- Abort_Undefer is undefined.
1158 if Is_Task_Context then
1159 SSL.Abort_Undefer.all;
1164 ------------------------
1165 -- Suspend_Until_True --
1166 ------------------------
1168 procedure Suspend_Until_True (S : in out Suspension_Object) is
1172 SSL.Abort_Defer.all;
1174 Result := semTake (S.L, WAIT_FOREVER);
1178 -- Program_Error must be raised upon calling Suspend_Until_True
1179 -- if another task is already waiting on that suspension object
1180 -- (ARM D.10 par. 10).
1182 Result := semGive (S.L);
1183 pragma Assert (Result = OK);
1185 SSL.Abort_Undefer.all;
1187 raise Program_Error;
1190 -- Suspend the task if the state is False. Otherwise, the task
1191 -- continues its execution, and the state of the suspension object
1192 -- is set to False (ARM D.10 par. 9).
1197 Result := semGive (S.L);
1198 pragma Assert (Result = 0);
1200 SSL.Abort_Undefer.all;
1205 -- Release the mutex before sleeping
1207 Result := semGive (S.L);
1208 pragma Assert (Result = OK);
1210 SSL.Abort_Undefer.all;
1212 Result := semTake (S.CV, WAIT_FOREVER);
1213 pragma Assert (Result = 0);
1216 end Suspend_Until_True;
1224 function Check_Exit (Self_ID : ST.Task_Id) return Boolean is
1225 pragma Unreferenced (Self_ID);
1230 --------------------
1231 -- Check_No_Locks --
1232 --------------------
1234 function Check_No_Locks (Self_ID : ST.Task_Id) return Boolean is
1235 pragma Unreferenced (Self_ID);
1240 ----------------------
1241 -- Environment_Task --
1242 ----------------------
1244 function Environment_Task return Task_Id is
1246 return Environment_Task_Id;
1247 end Environment_Task;
1253 procedure Lock_RTS is
1255 Write_Lock (Single_RTS_Lock'Access, Global_Lock => True);
1262 procedure Unlock_RTS is
1264 Unlock (Single_RTS_Lock'Access, Global_Lock => True);
1271 function Suspend_Task
1273 Thread_Self : Thread_Id) return Boolean
1276 if T.Common.LL.Thread /= 0
1277 and then T.Common.LL.Thread /= Thread_Self
1279 return taskSuspend (T.Common.LL.Thread) = 0;
1289 function Resume_Task
1291 Thread_Self : Thread_Id) return Boolean
1294 if T.Common.LL.Thread /= 0
1295 and then T.Common.LL.Thread /= Thread_Self
1297 return taskResume (T.Common.LL.Thread) = 0;
1303 --------------------
1304 -- Stop_All_Tasks --
1305 --------------------
1307 procedure Stop_All_Tasks
1309 Thread_Self : constant Thread_Id := taskIdSelf;
1313 pragma Unreferenced (Dummy);
1318 C := All_Tasks_List;
1319 while C /= null loop
1320 if C.Common.LL.Thread /= 0
1321 and then C.Common.LL.Thread /= Thread_Self
1323 Dummy := Task_Stop (C.Common.LL.Thread);
1326 C := C.Common.All_Tasks_Link;
1329 Dummy := Int_Unlock;
1336 function Stop_Task (T : ST.Task_Id) return Boolean is
1338 if T.Common.LL.Thread /= 0 then
1339 return Task_Stop (T.Common.LL.Thread) = 0;
1349 function Continue_Task (T : ST.Task_Id) return Boolean
1352 if T.Common.LL.Thread /= 0 then
1353 return Task_Cont (T.Common.LL.Thread) = 0;
1359 ---------------------
1360 -- Is_Task_Context --
1361 ---------------------
1363 function Is_Task_Context return Boolean is
1365 return System.OS_Interface.Interrupt_Context /= 1;
1366 end Is_Task_Context;
1372 procedure Initialize (Environment_Task : Task_Id) is
1375 use type System.Multiprocessors.CPU_Range;
1378 Environment_Task_Id := Environment_Task;
1380 Interrupt_Management.Initialize;
1381 Specific.Initialize;
1383 if Locking_Policy = 'C' then
1384 Mutex_Protocol := Prio_Protect;
1385 elsif Locking_Policy = 'I' then
1386 Mutex_Protocol := Prio_Inherit;
1388 Mutex_Protocol := Prio_None;
1391 if Time_Slice_Val > 0 then
1395 (Duration (Time_Slice_Val) / Duration (1_000_000.0)));
1397 elsif Dispatching_Policy = 'R' then
1398 Result := Set_Time_Slice (To_Clock_Ticks (0.01));
1402 -- Initialize the lock used to synchronize chain of all ATCBs
1404 Initialize_Lock (Single_RTS_Lock'Access, RTS_Lock_Level);
1406 -- Make environment task known here because it doesn't go through
1407 -- Activate_Tasks, which does it for all other tasks.
1409 Known_Tasks (Known_Tasks'First) := Environment_Task;
1410 Environment_Task.Known_Tasks_Index := Known_Tasks'First;
1412 Enter_Task (Environment_Task);
1414 -- Set processor affinity
1416 if Environment_Task.Common.Base_CPU /=
1417 System.Multiprocessors.Not_A_Specific_CPU
1419 -- Ada 2012 pragma CPU uses CPU numbers starting from 1, while
1420 -- on VxWorks the first CPU is identified by a 0, so we need to
1425 (Environment_Task.Common.LL.Thread,
1426 int (Environment_Task.Common.Base_CPU) - 1);
1427 pragma Assert (Result /= -1);
1431 end System.Task_Primitives.Operations;