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-2014, 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
;
45 with System
.Multiprocessors
;
46 with System
.Tasking
.Debug
;
47 with System
.Interrupt_Management
;
48 with System
.Float_Control
;
49 with System
.OS_Constants
;
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 OSC
renames System
.OS_Constants
;
63 package SSL
renames System
.Soft_Links
;
65 use System
.Tasking
.Debug
;
67 use System
.OS_Interface
;
68 use System
.Parameters
;
69 use type System
.VxWorks
.Ext
.t_id
;
70 use type Interfaces
.C
.int
;
71 use type System
.OS_Interface
.unsigned
;
73 subtype int
is System
.OS_Interface
.int
;
74 subtype unsigned
is System
.OS_Interface
.unsigned
;
76 Relative
: constant := 0;
82 -- The followings are logically constants, but need to be initialized at
85 Environment_Task_Id
: Task_Id
;
86 -- A variable to hold Task_Id for the environment task
88 -- The followings are internal configuration constants needed
90 Dispatching_Policy
: Character;
91 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
93 Foreign_Task_Elaborated
: aliased Boolean := True;
94 -- Used to identified fake tasks (i.e., non-Ada Threads)
96 Locking_Policy
: Character;
97 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
99 Mutex_Protocol
: Priority_Type
;
101 Single_RTS_Lock
: aliased RTS_Lock
;
102 -- This is a lock to allow only one thread of control in the RTS at a
103 -- time; it is used to execute in mutual exclusion from all other tasks.
104 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
106 Time_Slice_Val
: Integer;
107 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
109 Null_Thread_Id
: constant Thread_Id
:= 0;
110 -- Constant to indicate that the thread identifier has not yet been
119 procedure Initialize
;
120 pragma Inline
(Initialize
);
121 -- Initialize task specific data
123 function Is_Valid_Task
return Boolean;
124 pragma Inline
(Is_Valid_Task
);
125 -- Does executing thread have a TCB?
127 procedure Set
(Self_Id
: Task_Id
);
129 -- Set the self id for the current task, unless Self_Id is null, in
130 -- which case the task specific data is deleted.
132 function Self
return Task_Id
;
133 pragma Inline
(Self
);
134 -- Return a pointer to the Ada Task Control Block of the calling task
138 package body Specific
is separate;
139 -- The body of this package is target specific
141 ----------------------------------
142 -- ATCB allocation/deallocation --
143 ----------------------------------
145 package body ATCB_Allocation
is separate;
146 -- The body of this package is shared across several targets
148 ---------------------------------
149 -- Support for foreign threads --
150 ---------------------------------
152 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
153 -- Allocate and Initialize a new ATCB for the current Thread
155 function Register_Foreign_Thread
156 (Thread
: Thread_Id
) return Task_Id
is separate;
158 -----------------------
159 -- Local Subprograms --
160 -----------------------
162 procedure Abort_Handler
(signo
: Signal
);
163 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
165 procedure Install_Signal_Handlers
;
166 -- Install the default signal handlers for the current task
168 function Is_Task_Context
return Boolean;
169 -- This function returns True if the current execution is in the context
170 -- of a task, and False if it is an interrupt context.
172 type Set_Stack_Limit_Proc_Acc
is access procedure;
173 pragma Convention
(C
, Set_Stack_Limit_Proc_Acc
);
175 Set_Stack_Limit_Hook
: Set_Stack_Limit_Proc_Acc
;
176 pragma Import
(C
, Set_Stack_Limit_Hook
, "__gnat_set_stack_limit_hook");
177 -- Procedure to be called when a task is created to set stack
178 -- limit. Used only for VxWorks 5 and VxWorks MILS guest OS.
180 function To_Address
is
181 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
187 procedure Abort_Handler
(signo
: Signal
) is
188 pragma Unreferenced
(signo
);
190 Self_ID
: constant Task_Id
:= Self
;
191 Old_Set
: aliased sigset_t
;
192 Unblocked_Mask
: aliased sigset_t
;
194 pragma Warnings
(Off
, Result
);
196 use System
.Interrupt_Management
;
199 -- It is not safe to raise an exception when using ZCX and the GCC
200 -- exception handling mechanism.
202 if ZCX_By_Default
then
206 if Self_ID
.Deferral_Level
= 0
207 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
208 and then not Self_ID
.Aborting
210 Self_ID
.Aborting
:= True;
212 -- Make sure signals used for RTS internal purposes are unmasked
214 Result
:= sigemptyset
(Unblocked_Mask
'Access);
215 pragma Assert
(Result
= 0);
218 (Unblocked_Mask
'Access,
219 Signal
(Abort_Task_Interrupt
));
220 pragma Assert
(Result
= 0);
221 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGBUS
);
222 pragma Assert
(Result
= 0);
223 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGFPE
);
224 pragma Assert
(Result
= 0);
225 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGILL
);
226 pragma Assert
(Result
= 0);
227 Result
:= sigaddset
(Unblocked_Mask
'Access, SIGSEGV
);
228 pragma Assert
(Result
= 0);
233 Unblocked_Mask
'Access,
235 pragma Assert
(Result
= 0);
237 raise Standard
'Abort_Signal;
245 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
246 pragma Unreferenced
(T
);
247 pragma Unreferenced
(On
);
250 -- Nothing needed (why not???)
259 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
261 return T
.Common
.LL
.Thread
;
268 function Self
return Task_Id
renames Specific
.Self
;
270 -----------------------------
271 -- Install_Signal_Handlers --
272 -----------------------------
274 procedure Install_Signal_Handlers
is
275 act
: aliased struct_sigaction
;
276 old_act
: aliased struct_sigaction
;
277 Tmp_Set
: aliased sigset_t
;
282 act
.sa_handler
:= Abort_Handler
'Address;
284 Result
:= sigemptyset
(Tmp_Set
'Access);
285 pragma Assert
(Result
= 0);
286 act
.sa_mask
:= Tmp_Set
;
290 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
291 act
'Unchecked_Access,
292 old_act
'Unchecked_Access);
293 pragma Assert
(Result
= 0);
295 Interrupt_Management
.Initialize_Interrupts
;
296 end Install_Signal_Handlers
;
298 ---------------------
299 -- Initialize_Lock --
300 ---------------------
302 procedure Initialize_Lock
303 (Prio
: System
.Any_Priority
;
304 L
: not null access Lock
)
307 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
308 L
.Prio_Ceiling
:= int
(Prio
);
309 L
.Protocol
:= Mutex_Protocol
;
310 pragma Assert
(L
.Mutex
/= 0);
313 procedure Initialize_Lock
314 (L
: not null access RTS_Lock
;
317 pragma Unreferenced
(Level
);
319 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
320 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
321 L
.Protocol
:= Mutex_Protocol
;
322 pragma Assert
(L
.Mutex
/= 0);
329 procedure Finalize_Lock
(L
: not null access Lock
) is
332 Result
:= semDelete
(L
.Mutex
);
333 pragma Assert
(Result
= 0);
336 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
339 Result
:= semDelete
(L
.Mutex
);
340 pragma Assert
(Result
= 0);
348 (L
: not null access Lock
;
349 Ceiling_Violation
: out Boolean)
354 if L
.Protocol
= Prio_Protect
355 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
357 Ceiling_Violation
:= True;
360 Ceiling_Violation
:= False;
363 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
364 pragma Assert
(Result
= 0);
368 (L
: not null access RTS_Lock
;
369 Global_Lock
: Boolean := False)
373 if not Single_Lock
or else Global_Lock
then
374 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
375 pragma Assert
(Result
= 0);
379 procedure Write_Lock
(T
: Task_Id
) is
382 if not Single_Lock
then
383 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
384 pragma Assert
(Result
= 0);
393 (L
: not null access Lock
;
394 Ceiling_Violation
: out Boolean)
397 Write_Lock
(L
, Ceiling_Violation
);
404 procedure Unlock
(L
: not null access Lock
) is
407 Result
:= semGive
(L
.Mutex
);
408 pragma Assert
(Result
= 0);
412 (L
: not null access RTS_Lock
;
413 Global_Lock
: Boolean := False)
417 if not Single_Lock
or else Global_Lock
then
418 Result
:= semGive
(L
.Mutex
);
419 pragma Assert
(Result
= 0);
423 procedure Unlock
(T
: Task_Id
) is
426 if not Single_Lock
then
427 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
428 pragma Assert
(Result
= 0);
436 -- Dynamic priority ceilings are not supported by the underlying system
438 procedure Set_Ceiling
439 (L
: not null access Lock
;
440 Prio
: System
.Any_Priority
)
442 pragma Unreferenced
(L
, Prio
);
451 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
452 pragma Unreferenced
(Reason
);
457 pragma Assert
(Self_ID
= Self
);
459 -- Release the mutex before sleeping
462 semGive
(if Single_Lock
463 then Single_RTS_Lock
.Mutex
464 else Self_ID
.Common
.LL
.L
.Mutex
);
465 pragma Assert
(Result
= 0);
467 -- Perform a blocking operation to take the CV semaphore. Note that a
468 -- blocking operation in VxWorks will reenable task scheduling. When we
469 -- are no longer blocked and control is returned, task scheduling will
470 -- again be disabled.
472 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, WAIT_FOREVER
);
473 pragma Assert
(Result
= 0);
475 -- Take the mutex back
478 semTake
((if Single_Lock
479 then Single_RTS_Lock
.Mutex
480 else Self_ID
.Common
.LL
.L
.Mutex
), WAIT_FOREVER
);
481 pragma Assert
(Result
= 0);
488 -- This is for use within the run-time system, so abort is assumed to be
489 -- already deferred, and the caller should be holding its own ATCB lock.
491 procedure Timed_Sleep
494 Mode
: ST
.Delay_Modes
;
495 Reason
: System
.Tasking
.Task_States
;
496 Timedout
: out Boolean;
497 Yielded
: out Boolean)
499 pragma Unreferenced
(Reason
);
501 Orig
: constant Duration := Monotonic_Clock
;
505 Wakeup
: Boolean := False;
511 if Mode
= Relative
then
512 Absolute
:= Orig
+ Time
;
514 -- Systematically add one since the first tick will delay *at most*
515 -- 1 / Rate_Duration seconds, so we need to add one to be on the
518 Ticks
:= To_Clock_Ticks
(Time
);
520 if Ticks
> 0 and then Ticks
< int
'Last then
526 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
531 -- Release the mutex before sleeping
534 semGive
(if Single_Lock
535 then Single_RTS_Lock
.Mutex
536 else Self_ID
.Common
.LL
.L
.Mutex
);
537 pragma Assert
(Result
= 0);
539 -- Perform a blocking operation to take the CV semaphore. Note
540 -- that a blocking operation in VxWorks will reenable task
541 -- scheduling. When we are no longer blocked and control is
542 -- returned, task scheduling will again be disabled.
544 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
548 -- Somebody may have called Wakeup for us
553 if errno
/= S_objLib_OBJ_TIMEOUT
then
557 -- If Ticks = int'last, it was most probably truncated so
558 -- let's make another round after recomputing Ticks from
559 -- the absolute time.
561 if Ticks
/= int
'Last then
565 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
574 -- Take the mutex back
577 semTake
((if Single_Lock
578 then Single_RTS_Lock
.Mutex
579 else Self_ID
.Common
.LL
.L
.Mutex
), WAIT_FOREVER
);
580 pragma Assert
(Result
= 0);
582 exit when Timedout
or Wakeup
;
588 -- Should never hold a lock while yielding
591 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
593 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
596 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
598 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
607 -- This is for use in implementing delay statements, so we assume the
608 -- caller is holding no locks.
610 procedure Timed_Delay
613 Mode
: ST
.Delay_Modes
)
615 Orig
: constant Duration := Monotonic_Clock
;
619 Aborted
: Boolean := False;
622 pragma Warnings
(Off
, Result
);
625 if Mode
= Relative
then
626 Absolute
:= Orig
+ Time
;
627 Ticks
:= To_Clock_Ticks
(Time
);
629 if Ticks
> 0 and then Ticks
< int
'Last then
631 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
632 -- seconds, so we need to add one to be on the safe side.
639 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
644 -- Modifying State, locking the TCB
647 semTake
((if Single_Lock
648 then Single_RTS_Lock
.Mutex
649 else Self_ID
.Common
.LL
.L
.Mutex
), WAIT_FOREVER
);
651 pragma Assert
(Result
= 0);
653 Self_ID
.Common
.State
:= Delay_Sleep
;
657 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
659 -- Release the TCB before sleeping
662 semGive
(if Single_Lock
663 then Single_RTS_Lock
.Mutex
664 else Self_ID
.Common
.LL
.L
.Mutex
);
665 pragma Assert
(Result
= 0);
669 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
673 -- If Ticks = int'last, it was most probably truncated
674 -- so let's make another round after recomputing Ticks
675 -- from the absolute time.
677 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
680 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
688 -- Take back the lock after having slept, to protect further
689 -- access to Self_ID.
694 then Single_RTS_Lock
.Mutex
695 else Self_ID
.Common
.LL
.L
.Mutex
), WAIT_FOREVER
);
697 pragma Assert
(Result
= 0);
702 Self_ID
.Common
.State
:= Runnable
;
707 then Single_RTS_Lock
.Mutex
708 else Self_ID
.Common
.LL
.L
.Mutex
);
715 ---------------------
716 -- Monotonic_Clock --
717 ---------------------
719 function Monotonic_Clock
return Duration is
720 TS
: aliased timespec
;
723 Result
:= clock_gettime
(OSC
.CLOCK_RT_Ada
, TS
'Unchecked_Access);
724 pragma Assert
(Result
= 0);
725 return To_Duration
(TS
);
732 function RT_Resolution
return Duration is
734 return 1.0 / Duration (sysClkRateGet
);
741 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
742 pragma Unreferenced
(Reason
);
745 Result
:= semGive
(T
.Common
.LL
.CV
);
746 pragma Assert
(Result
= 0);
753 procedure Yield
(Do_Yield
: Boolean := True) is
754 pragma Unreferenced
(Do_Yield
);
756 pragma Unreferenced
(Result
);
758 Result
:= taskDelay
(0);
765 procedure Set_Priority
767 Prio
: System
.Any_Priority
;
768 Loss_Of_Inheritance
: Boolean := False)
770 pragma Unreferenced
(Loss_Of_Inheritance
);
777 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
778 pragma Assert
(Result
= 0);
780 -- Note: in VxWorks 6.6 (or earlier), the task is placed at the end of
781 -- the priority queue instead of the head. This is not the behavior
782 -- required by Annex D (RM D.2.3(5/2)), but we consider it an acceptable
783 -- variation (RM 1.1.3(6)), given this is the built-in behavior of the
784 -- operating system. VxWorks versions starting from 6.7 implement the
785 -- required Annex D semantics.
787 -- In older versions we attempted to better approximate the Annex D
788 -- required behavior, but this simulation was not entirely accurate,
789 -- and it seems better to live with the standard VxWorks semantics.
791 T
.Common
.Current_Priority
:= Prio
;
798 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
800 return T
.Common
.Current_Priority
;
807 procedure Enter_Task
(Self_ID
: Task_Id
) is
809 -- Store the user-level task id in the Thread field (to be used
810 -- internally by the run-time system) and the kernel-level task id in
811 -- the LWP field (to be used by the debugger).
813 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
814 Self_ID
.Common
.LL
.LWP
:= getpid
;
816 Specific
.Set
(Self_ID
);
818 -- Properly initializes the FPU for PPC/MIPS systems
820 System
.Float_Control
.Reset
;
822 -- Install the signal handlers
824 -- This is called for each task since there is no signal inheritance
825 -- between VxWorks tasks.
827 Install_Signal_Handlers
;
829 -- If stack checking is enabled, set the stack limit for this task
831 if Set_Stack_Limit_Hook
/= null then
832 Set_Stack_Limit_Hook
.all;
840 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
842 -----------------------------
843 -- Register_Foreign_Thread --
844 -----------------------------
846 function Register_Foreign_Thread
return Task_Id
is
848 if Is_Valid_Task
then
851 return Register_Foreign_Thread
(taskIdSelf
);
853 end Register_Foreign_Thread
;
859 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
861 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
862 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
864 if Self_ID
.Common
.LL
.CV
= 0 then
870 if not Single_Lock
then
871 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
880 procedure Create_Task
882 Wrapper
: System
.Address
;
883 Stack_Size
: System
.Parameters
.Size_Type
;
884 Priority
: System
.Any_Priority
;
885 Succeeded
: out Boolean)
887 Adjusted_Stack_Size
: size_t
;
889 use type System
.Multiprocessors
.CPU_Range
;
892 -- Check whether both Dispatching_Domain and CPU are specified for the
893 -- task, and the CPU value is not contained within the range of
894 -- processors for the domain.
896 if T
.Common
.Domain
/= null
897 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
899 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
900 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
906 -- Ask for four extra bytes of stack space so that the ATCB pointer can
907 -- be stored below the stack limit, plus extra space for the frame of
908 -- Task_Wrapper. This is so the user gets the amount of stack requested
909 -- exclusive of the needs.
911 -- We also have to allocate n more bytes for the task name storage and
912 -- enough space for the Wind Task Control Block which is around 0x778
913 -- bytes. VxWorks also seems to carve out additional space, so use 2048
914 -- as a nice round number. We might want to increment to the nearest
915 -- page size in case we ever support VxVMI.
917 -- ??? - we should come back and visit this so we can set the task name
918 -- to something appropriate.
920 Adjusted_Stack_Size
:= size_t
(Stack_Size
) + 2048;
922 -- Since the initial signal mask of a thread is inherited from the
923 -- creator, and the Environment task has all its signals masked, we do
924 -- not need to manipulate caller's signal mask at this point. All tasks
925 -- in RTS will have All_Tasks_Mask initially.
927 -- We now compute the VxWorks task name and options, then spawn ...
930 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
931 Name_Address
: System
.Address
;
932 -- Task name we are going to hand down to VxWorks
934 function Get_Task_Options
return int
;
935 pragma Import
(C
, Get_Task_Options
, "__gnat_get_task_options");
936 -- Function that returns the options to be set for the task that we
937 -- are creating. We fetch the options assigned to the current task,
938 -- so offering some user level control over the options for a task
939 -- hierarchy, and force VX_FP_TASK because it is almost always
943 -- If there is no Ada task name handy, let VxWorks choose one.
944 -- Otherwise, tell VxWorks what the Ada task name is.
946 if T
.Common
.Task_Image_Len
= 0 then
947 Name_Address
:= System
.Null_Address
;
949 Name
(1 .. Name
'Last - 1) :=
950 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
951 Name
(Name
'Last) := ASCII
.NUL
;
952 Name_Address
:= Name
'Address;
955 -- Now spawn the VxWorks task for real
957 T
.Common
.LL
.Thread
:=
960 To_VxWorks_Priority
(int
(Priority
)),
967 -- Set processor affinity
969 Set_Task_Affinity
(T
);
971 if T
.Common
.LL
.Thread
<= Null_Thread_Id
then
975 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
976 Set_Priority
(T
, Priority
);
984 procedure Finalize_TCB
(T
: Task_Id
) is
988 if not Single_Lock
then
989 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
990 pragma Assert
(Result
= 0);
993 T
.Common
.LL
.Thread
:= Null_Thread_Id
;
995 Result
:= semDelete
(T
.Common
.LL
.CV
);
996 pragma Assert
(Result
= 0);
998 if T
.Known_Tasks_Index
/= -1 then
999 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1002 ATCB_Allocation
.Free_ATCB
(T
);
1009 procedure Exit_Task
is
1011 Specific
.Set
(null);
1018 procedure Abort_Task
(T
: Task_Id
) is
1023 (T
.Common
.LL
.Thread
,
1024 Signal
(Interrupt_Management
.Abort_Task_Interrupt
));
1025 pragma Assert
(Result
= 0);
1032 procedure Initialize
(S
: in out Suspension_Object
) is
1034 -- Initialize internal state (always to False (RM D.10(6)))
1039 -- Initialize internal mutex
1041 -- Use simpler binary semaphore instead of VxWorks
1042 -- mutual exclusion semaphore, because we don't need
1043 -- the fancier semantics and their overhead.
1045 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1047 -- Initialize internal condition variable
1049 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1056 procedure Finalize
(S
: in out Suspension_Object
) is
1057 pragma Unmodified
(S
);
1058 -- S may be modified on other targets, but not on VxWorks
1063 -- Destroy internal mutex
1065 Result
:= semDelete
(S
.L
);
1066 pragma Assert
(Result
= OK
);
1068 -- Destroy internal condition variable
1070 Result
:= semDelete
(S
.CV
);
1071 pragma Assert
(Result
= OK
);
1078 function Current_State
(S
: Suspension_Object
) return Boolean is
1080 -- We do not want to use lock on this read operation. State is marked
1081 -- as Atomic so that we ensure that the value retrieved is correct.
1090 procedure Set_False
(S
: in out Suspension_Object
) is
1094 SSL
.Abort_Defer
.all;
1096 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1097 pragma Assert
(Result
= OK
);
1101 Result
:= semGive
(S
.L
);
1102 pragma Assert
(Result
= OK
);
1104 SSL
.Abort_Undefer
.all;
1111 procedure Set_True
(S
: in out Suspension_Object
) is
1115 -- Set_True can be called from an interrupt context, in which case
1116 -- Abort_Defer is undefined.
1118 if Is_Task_Context
then
1119 SSL
.Abort_Defer
.all;
1122 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1123 pragma Assert
(Result
= OK
);
1125 -- If there is already a task waiting on this suspension object then
1126 -- we resume it, leaving the state of the suspension object to False,
1127 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1128 -- the state to True.
1134 Result
:= semGive
(S
.CV
);
1135 pragma Assert
(Result
= OK
);
1140 Result
:= semGive
(S
.L
);
1141 pragma Assert
(Result
= OK
);
1143 -- Set_True can be called from an interrupt context, in which case
1144 -- Abort_Undefer is undefined.
1146 if Is_Task_Context
then
1147 SSL
.Abort_Undefer
.all;
1152 ------------------------
1153 -- Suspend_Until_True --
1154 ------------------------
1156 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1160 SSL
.Abort_Defer
.all;
1162 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1166 -- Program_Error must be raised upon calling Suspend_Until_True
1167 -- if another task is already waiting on that suspension object
1168 -- (ARM D.10 par. 10).
1170 Result
:= semGive
(S
.L
);
1171 pragma Assert
(Result
= OK
);
1173 SSL
.Abort_Undefer
.all;
1175 raise Program_Error
;
1178 -- Suspend the task if the state is False. Otherwise, the task
1179 -- continues its execution, and the state of the suspension object
1180 -- is set to False (ARM D.10 par. 9).
1185 Result
:= semGive
(S
.L
);
1186 pragma Assert
(Result
= 0);
1188 SSL
.Abort_Undefer
.all;
1193 -- Release the mutex before sleeping
1195 Result
:= semGive
(S
.L
);
1196 pragma Assert
(Result
= OK
);
1198 SSL
.Abort_Undefer
.all;
1200 Result
:= semTake
(S
.CV
, WAIT_FOREVER
);
1201 pragma Assert
(Result
= 0);
1204 end Suspend_Until_True
;
1212 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1213 pragma Unreferenced
(Self_ID
);
1218 --------------------
1219 -- Check_No_Locks --
1220 --------------------
1222 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1223 pragma Unreferenced
(Self_ID
);
1228 ----------------------
1229 -- Environment_Task --
1230 ----------------------
1232 function Environment_Task
return Task_Id
is
1234 return Environment_Task_Id
;
1235 end Environment_Task
;
1241 procedure Lock_RTS
is
1243 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1250 procedure Unlock_RTS
is
1252 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1259 function Suspend_Task
1261 Thread_Self
: Thread_Id
) return Boolean
1264 if T
.Common
.LL
.Thread
/= Null_Thread_Id
1265 and then T
.Common
.LL
.Thread
/= Thread_Self
1267 return taskSuspend
(T
.Common
.LL
.Thread
) = 0;
1277 function Resume_Task
1279 Thread_Self
: Thread_Id
) return Boolean
1282 if T
.Common
.LL
.Thread
/= Null_Thread_Id
1283 and then T
.Common
.LL
.Thread
/= Thread_Self
1285 return taskResume
(T
.Common
.LL
.Thread
) = 0;
1291 --------------------
1292 -- Stop_All_Tasks --
1293 --------------------
1295 procedure Stop_All_Tasks
1297 Thread_Self
: constant Thread_Id
:= taskIdSelf
;
1305 C
:= All_Tasks_List
;
1306 while C
/= null loop
1307 if C
.Common
.LL
.Thread
/= Null_Thread_Id
1308 and then C
.Common
.LL
.Thread
/= Thread_Self
1310 Dummy
:= Task_Stop
(C
.Common
.LL
.Thread
);
1313 C
:= C
.Common
.All_Tasks_Link
;
1316 Dummy
:= Int_Unlock
;
1323 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1325 if T
.Common
.LL
.Thread
/= Null_Thread_Id
then
1326 return Task_Stop
(T
.Common
.LL
.Thread
) = 0;
1336 function Continue_Task
(T
: ST
.Task_Id
) return Boolean
1339 if T
.Common
.LL
.Thread
/= Null_Thread_Id
then
1340 return Task_Cont
(T
.Common
.LL
.Thread
) = 0;
1346 ---------------------
1347 -- Is_Task_Context --
1348 ---------------------
1350 function Is_Task_Context
return Boolean is
1352 return System
.OS_Interface
.Interrupt_Context
/= 1;
1353 end Is_Task_Context
;
1359 procedure Initialize
(Environment_Task
: Task_Id
) is
1361 pragma Unreferenced
(Result
);
1364 Environment_Task_Id
:= Environment_Task
;
1366 Interrupt_Management
.Initialize
;
1367 Specific
.Initialize
;
1369 if Locking_Policy
= 'C' then
1370 Mutex_Protocol
:= Prio_Protect
;
1371 elsif Locking_Policy
= 'I' then
1372 Mutex_Protocol
:= Prio_Inherit
;
1374 Mutex_Protocol
:= Prio_None
;
1377 if Time_Slice_Val
> 0 then
1381 (Duration (Time_Slice_Val
) / Duration (1_000_000
.0
)));
1383 elsif Dispatching_Policy
= 'R' then
1384 Result
:= Set_Time_Slice
(To_Clock_Ticks
(0.01));
1388 -- Initialize the lock used to synchronize chain of all ATCBs
1390 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1392 -- Make environment task known here because it doesn't go through
1393 -- Activate_Tasks, which does it for all other tasks.
1395 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1396 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1398 Enter_Task
(Environment_Task
);
1400 -- Set processor affinity
1402 Set_Task_Affinity
(Environment_Task
);
1405 -----------------------
1406 -- Set_Task_Affinity --
1407 -----------------------
1409 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1411 pragma Unreferenced
(Result
);
1413 use System
.Task_Info
;
1414 use type System
.Multiprocessors
.CPU_Range
;
1417 -- Do nothing if the underlying thread has not yet been created. If the
1418 -- thread has not yet been created then the proper affinity will be set
1419 -- during its creation.
1421 if T
.Common
.LL
.Thread
= Null_Thread_Id
then
1426 elsif T
.Common
.Base_CPU
/= Multiprocessors
.Not_A_Specific_CPU
then
1428 -- Ada 2012 pragma CPU uses CPU numbers starting from 1, while on
1429 -- VxWorks the first CPU is identified by a 0, so we need to adjust.
1433 (T
.Common
.LL
.Thread
, int
(T
.Common
.Base_CPU
) - 1);
1437 elsif T
.Common
.Task_Info
/= Unspecified_Task_Info
then
1438 Result
:= taskCpuAffinitySet
(T
.Common
.LL
.Thread
, T
.Common
.Task_Info
);
1440 -- Handle dispatching domains
1442 elsif T
.Common
.Domain
/= null
1443 and then (T
.Common
.Domain
/= ST
.System_Domain
1444 or else T
.Common
.Domain
.all /=
1445 (Multiprocessors
.CPU
'First ..
1446 Multiprocessors
.Number_Of_CPUs
=> True))
1449 CPU_Set
: unsigned
:= 0;
1452 -- Set the affinity to all the processors belonging to the
1453 -- dispatching domain.
1455 for Proc
in T
.Common
.Domain
'Range loop
1456 if T
.Common
.Domain
(Proc
) then
1458 -- The thread affinity mask is a bit vector in which each
1459 -- bit represents a logical processor.
1461 CPU_Set
:= CPU_Set
+ 2 ** (Integer (Proc
) - 1);
1465 Result
:= taskMaskAffinitySet
(T
.Common
.LL
.Thread
, CPU_Set
);
1468 end Set_Task_Affinity
;
1470 end System
.Task_Primitives
.Operations
;