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-2009, 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
.Tasking
.Debug
;
47 with System
.Interrupt_Management
;
49 with System
.Soft_Links
;
50 -- We use System.Soft_Links instead of System.Tasking.Initialization
51 -- because the later is a higher level package that we shouldn't depend
52 -- on. For example when using the restricted run time, it is replaced by
53 -- System.Tasking.Restricted.Stages.
55 with System
.VxWorks
.Ext
;
57 package body System
.Task_Primitives
.Operations
is
59 package SSL
renames System
.Soft_Links
;
61 use System
.Tasking
.Debug
;
63 use System
.OS_Interface
;
64 use System
.Parameters
;
65 use type System
.VxWorks
.Ext
.t_id
;
66 use type Interfaces
.C
.int
;
68 subtype int
is System
.OS_Interface
.int
;
70 Relative
: constant := 0;
76 -- The followings are logically constants, but need to be initialized at
79 Single_RTS_Lock
: aliased RTS_Lock
;
80 -- This is a lock to allow only one thread of control in the RTS at a
81 -- time; it is used to execute in mutual exclusion from all other tasks.
82 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
84 Environment_Task_Id
: Task_Id
;
85 -- A variable to hold Task_Id for the environment task
87 Unblocked_Signal_Mask
: aliased sigset_t
;
88 -- The set of signals that should unblocked in all tasks
90 -- The followings are internal configuration constants needed
92 Time_Slice_Val
: Integer;
93 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
95 Locking_Policy
: Character;
96 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
98 Dispatching_Policy
: Character;
99 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
101 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
102 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
103 -- Get priority specific dispatching policy
105 Mutex_Protocol
: Priority_Type
;
107 Foreign_Task_Elaborated
: aliased Boolean := True;
108 -- Used to identified fake tasks (i.e., non-Ada Threads)
110 type Set_Stack_Limit_Proc_Acc
is access procedure;
111 pragma Convention
(C
, Set_Stack_Limit_Proc_Acc
);
113 Set_Stack_Limit_Hook
: Set_Stack_Limit_Proc_Acc
;
114 pragma Import
(C
, Set_Stack_Limit_Hook
, "__gnat_set_stack_limit_hook");
115 -- Procedure to be called when a task is created to set stack
124 procedure Initialize
;
125 pragma Inline
(Initialize
);
126 -- Initialize task specific data
128 function Is_Valid_Task
return Boolean;
129 pragma Inline
(Is_Valid_Task
);
130 -- Does executing thread have a TCB?
132 procedure Set
(Self_Id
: Task_Id
);
134 -- Set the self id for the current task
137 pragma Inline
(Delete
);
138 -- Delete the task specific data associated with the current task
140 function Self
return Task_Id
;
141 pragma Inline
(Self
);
142 -- Return a pointer to the Ada Task Control Block of the calling task
146 package body Specific
is separate;
147 -- The body of this package is target specific
149 ---------------------------------
150 -- Support for foreign threads --
151 ---------------------------------
153 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
154 -- Allocate and Initialize a new ATCB for the current Thread
156 function Register_Foreign_Thread
157 (Thread
: Thread_Id
) return Task_Id
is separate;
159 -----------------------
160 -- Local Subprograms --
161 -----------------------
163 procedure Abort_Handler
(signo
: Signal
);
164 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
166 procedure Install_Signal_Handlers
;
167 -- Install the default signal handlers for the current task
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
;
183 pragma Warnings
(Off
, Result
);
186 -- It is not safe to raise an exception when using ZCX and the GCC
187 -- exception handling mechanism.
189 if ZCX_By_Default
and then GCC_ZCX_Support
then
193 if Self_ID
.Deferral_Level
= 0
194 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
195 and then not Self_ID
.Aborting
197 Self_ID
.Aborting
:= True;
199 -- Make sure signals used for RTS internal purpose are unmasked
204 Unblocked_Signal_Mask
'Access,
206 pragma Assert
(Result
= 0);
208 raise Standard
'Abort_Signal;
216 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
217 pragma Unreferenced
(T
);
218 pragma Unreferenced
(On
);
221 -- Nothing needed (why not???)
230 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
232 return T
.Common
.LL
.Thread
;
239 function Self
return Task_Id
renames Specific
.Self
;
241 -----------------------------
242 -- Install_Signal_Handlers --
243 -----------------------------
245 procedure Install_Signal_Handlers
is
246 act
: aliased struct_sigaction
;
247 old_act
: aliased struct_sigaction
;
248 Tmp_Set
: aliased sigset_t
;
253 act
.sa_handler
:= Abort_Handler
'Address;
255 Result
:= sigemptyset
(Tmp_Set
'Access);
256 pragma Assert
(Result
= 0);
257 act
.sa_mask
:= Tmp_Set
;
261 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
262 act
'Unchecked_Access,
263 old_act
'Unchecked_Access);
264 pragma Assert
(Result
= 0);
266 Interrupt_Management
.Initialize_Interrupts
;
267 end Install_Signal_Handlers
;
269 ---------------------
270 -- Initialize_Lock --
271 ---------------------
273 procedure Initialize_Lock
274 (Prio
: System
.Any_Priority
;
275 L
: not null access Lock
)
278 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
279 L
.Prio_Ceiling
:= int
(Prio
);
280 L
.Protocol
:= Mutex_Protocol
;
281 pragma Assert
(L
.Mutex
/= 0);
284 procedure Initialize_Lock
285 (L
: not null access RTS_Lock
;
288 pragma Unreferenced
(Level
);
290 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
291 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
292 L
.Protocol
:= Mutex_Protocol
;
293 pragma Assert
(L
.Mutex
/= 0);
300 procedure Finalize_Lock
(L
: not null access Lock
) is
303 Result
:= semDelete
(L
.Mutex
);
304 pragma Assert
(Result
= 0);
307 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
310 Result
:= semDelete
(L
.Mutex
);
311 pragma Assert
(Result
= 0);
319 (L
: not null access Lock
;
320 Ceiling_Violation
: out Boolean)
325 if L
.Protocol
= Prio_Protect
326 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
328 Ceiling_Violation
:= True;
331 Ceiling_Violation
:= False;
334 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
335 pragma Assert
(Result
= 0);
339 (L
: not null access RTS_Lock
;
340 Global_Lock
: Boolean := False)
344 if not Single_Lock
or else Global_Lock
then
345 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
346 pragma Assert
(Result
= 0);
350 procedure Write_Lock
(T
: Task_Id
) is
353 if not Single_Lock
then
354 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
355 pragma Assert
(Result
= 0);
364 (L
: not null access Lock
;
365 Ceiling_Violation
: out Boolean)
368 Write_Lock
(L
, Ceiling_Violation
);
375 procedure Unlock
(L
: not null access Lock
) is
378 Result
:= semGive
(L
.Mutex
);
379 pragma Assert
(Result
= 0);
383 (L
: not null access RTS_Lock
;
384 Global_Lock
: Boolean := False)
388 if not Single_Lock
or else Global_Lock
then
389 Result
:= semGive
(L
.Mutex
);
390 pragma Assert
(Result
= 0);
394 procedure Unlock
(T
: Task_Id
) is
397 if not Single_Lock
then
398 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
399 pragma Assert
(Result
= 0);
407 -- Dynamic priority ceilings are not supported by the underlying system
409 procedure Set_Ceiling
410 (L
: not null access Lock
;
411 Prio
: System
.Any_Priority
)
413 pragma Unreferenced
(L
, Prio
);
422 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
423 pragma Unreferenced
(Reason
);
428 pragma Assert
(Self_ID
= Self
);
430 -- Release the mutex before sleeping
433 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
435 Result
:= semGive
(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 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
453 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
456 pragma Assert
(Result
= 0);
463 -- This is for use within the run-time system, so abort is assumed to be
464 -- already deferred, and the caller should be holding its own ATCB lock.
466 procedure Timed_Sleep
469 Mode
: ST
.Delay_Modes
;
470 Reason
: System
.Tasking
.Task_States
;
471 Timedout
: out Boolean;
472 Yielded
: out Boolean)
474 pragma Unreferenced
(Reason
);
476 Orig
: constant Duration := Monotonic_Clock
;
480 Wakeup
: Boolean := False;
486 if Mode
= Relative
then
487 Absolute
:= Orig
+ Time
;
489 -- Systematically add one since the first tick will delay *at most*
490 -- 1 / Rate_Duration seconds, so we need to add one to be on the
493 Ticks
:= To_Clock_Ticks
(Time
);
495 if Ticks
> 0 and then Ticks
< int
'Last then
501 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
506 -- Release the mutex before sleeping
509 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
511 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
514 pragma Assert
(Result
= 0);
516 -- Perform a blocking operation to take the CV semaphore. Note
517 -- that a blocking operation in VxWorks will reenable task
518 -- scheduling. When we are no longer blocked and control is
519 -- returned, task scheduling will again be disabled.
521 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
525 -- Somebody may have called Wakeup for us
530 if errno
/= S_objLib_OBJ_TIMEOUT
then
534 -- If Ticks = int'last, it was most probably truncated so
535 -- let's make another round after recomputing Ticks from
536 -- the absolute time.
538 if Ticks
/= int
'Last then
542 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
551 -- Take the mutex back
554 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
556 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
559 pragma Assert
(Result
= 0);
561 exit when Timedout
or Wakeup
;
567 -- Should never hold a lock while yielding
570 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
572 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
575 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
577 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
586 -- This is for use in implementing delay statements, so we assume the
587 -- caller is holding no locks.
589 procedure Timed_Delay
592 Mode
: ST
.Delay_Modes
)
594 Orig
: constant Duration := Monotonic_Clock
;
598 Aborted
: Boolean := False;
601 pragma Warnings
(Off
, Result
);
604 if Mode
= Relative
then
605 Absolute
:= Orig
+ Time
;
606 Ticks
:= To_Clock_Ticks
(Time
);
608 if Ticks
> 0 and then Ticks
< int
'Last then
610 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
611 -- seconds, so we need to add one to be on the safe side.
618 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
623 -- Modifying State, locking the TCB
626 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
628 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
631 pragma Assert
(Result
= 0);
633 Self_ID
.Common
.State
:= Delay_Sleep
;
637 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
639 -- Release the TCB before sleeping
642 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
644 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
646 pragma Assert
(Result
= 0);
650 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
654 -- If Ticks = int'last, it was most probably truncated
655 -- so let's make another round after recomputing Ticks
656 -- from the absolute time.
658 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
661 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
669 -- Take back the lock after having slept, to protect further
670 -- access to Self_ID.
673 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
675 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
678 pragma Assert
(Result
= 0);
683 Self_ID
.Common
.State
:= Runnable
;
686 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
688 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
696 ---------------------
697 -- Monotonic_Clock --
698 ---------------------
700 function Monotonic_Clock
return Duration is
701 TS
: aliased timespec
;
704 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
705 pragma Assert
(Result
= 0);
706 return To_Duration
(TS
);
713 function RT_Resolution
return Duration is
715 return 1.0 / Duration (sysClkRateGet
);
722 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
723 pragma Unreferenced
(Reason
);
726 Result
:= semGive
(T
.Common
.LL
.CV
);
727 pragma Assert
(Result
= 0);
734 procedure Yield
(Do_Yield
: Boolean := True) is
735 pragma Unreferenced
(Do_Yield
);
737 pragma Unreferenced
(Result
);
739 Result
:= taskDelay
(0);
746 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
747 pragma Atomic_Components
(Prio_Array_Type
);
749 Prio_Array
: Prio_Array_Type
;
750 -- Global array containing the id of the currently running task for each
751 -- priority. Note that we assume that we are on a single processor with
752 -- run-till-blocked scheduling.
754 procedure Set_Priority
756 Prio
: System
.Any_Priority
;
757 Loss_Of_Inheritance
: Boolean := False)
759 Array_Item
: Integer;
765 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
766 pragma Assert
(Result
= 0);
768 if (Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F')
769 and then Loss_Of_Inheritance
770 and then Prio
< T
.Common
.Current_Priority
772 -- Annex D requirement (RM D.2.2(9)):
774 -- If the task drops its priority due to the loss of inherited
775 -- priority, it is added at the head of the ready queue for its
776 -- new active priority.
778 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
779 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
782 -- Give some processes a chance to arrive
786 -- Then wait for our turn to proceed
788 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
789 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
792 Prio_Array
(T
.Common
.Base_Priority
) :=
793 Prio_Array
(T
.Common
.Base_Priority
) - 1;
796 T
.Common
.Current_Priority
:= Prio
;
803 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
805 return T
.Common
.Current_Priority
;
812 procedure Enter_Task
(Self_ID
: Task_Id
) is
813 procedure Init_Float
;
814 pragma Import
(C
, Init_Float
, "__gnat_init_float");
815 -- Properly initializes the FPU for PPC/MIPS systems
818 -- Store the user-level task id in the Thread field (to be used
819 -- internally by the run-time system) and the kernel-level task id in
820 -- the LWP field (to be used by the debugger).
822 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
823 Self_ID
.Common
.LL
.LWP
:= getpid
;
825 Specific
.Set
(Self_ID
);
829 -- Install the signal handlers
831 -- This is called for each task since there is no signal inheritance
832 -- between VxWorks tasks.
834 Install_Signal_Handlers
;
836 -- If stack checking is enabled, set the stack limit for this task
838 if Set_Stack_Limit_Hook
/= null then
839 Set_Stack_Limit_Hook
.all;
847 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
849 return new Ada_Task_Control_Block
(Entry_Num
);
856 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
858 -----------------------------
859 -- Register_Foreign_Thread --
860 -----------------------------
862 function Register_Foreign_Thread
return Task_Id
is
864 if Is_Valid_Task
then
867 return Register_Foreign_Thread
(taskIdSelf
);
869 end Register_Foreign_Thread
;
875 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
877 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
878 Self_ID
.Common
.LL
.Thread
:= 0;
880 if Self_ID
.Common
.LL
.CV
= 0 then
886 if not Single_Lock
then
887 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
896 procedure Create_Task
898 Wrapper
: System
.Address
;
899 Stack_Size
: System
.Parameters
.Size_Type
;
900 Priority
: System
.Any_Priority
;
901 Succeeded
: out Boolean)
903 Adjusted_Stack_Size
: size_t
;
905 -- Ask for four extra bytes of stack space so that the ATCB pointer can
906 -- be stored below the stack limit, plus extra space for the frame of
907 -- Task_Wrapper. This is so the user gets the amount of stack requested
908 -- exclusive of the needs.
910 -- We also have to allocate n more bytes for the task name storage and
911 -- enough space for the Wind Task Control Block which is around 0x778
912 -- bytes. VxWorks also seems to carve out additional space, so use 2048
913 -- as a nice round number. We might want to increment to the nearest
914 -- page size in case we ever support VxVMI.
916 -- ??? - we should come back and visit this so we can set the task name
917 -- to something appropriate.
919 Adjusted_Stack_Size
:= size_t
(Stack_Size
) + 2048;
921 -- Since the initial signal mask of a thread is inherited from the
922 -- creator, and the Environment task has all its signals masked, we do
923 -- not need to manipulate caller's signal mask at this point. All tasks
924 -- in RTS will have All_Tasks_Mask initially.
926 -- We now compute the VxWorks task name and options, then spawn ...
929 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
930 Name_Address
: System
.Address
;
931 -- Task name we are going to hand down to VxWorks
933 function Get_Task_Options
return int
;
934 pragma Import
(C
, Get_Task_Options
, "__gnat_get_task_options");
935 -- Function that returns the options to be set for the task that we
936 -- are creating. We fetch the options assigned to the current task,
937 -- so offering some user level control over the options for a task
938 -- hierarchy, and force VX_FP_TASK because it is almost always
942 -- If there is no Ada task name handy, let VxWorks choose one.
943 -- Otherwise, tell VxWorks what the Ada task name is.
945 if T
.Common
.Task_Image_Len
= 0 then
946 Name_Address
:= System
.Null_Address
;
948 Name
(1 .. Name
'Last - 1) :=
949 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
950 Name
(Name
'Last) := ASCII
.NUL
;
951 Name_Address
:= Name
'Address;
954 -- Now spawn the VxWorks task for real
956 T
.Common
.LL
.Thread
:=
959 To_VxWorks_Priority
(int
(Priority
)),
966 if T
.Common
.LL
.Thread
= -1 then
970 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
971 Set_Priority
(T
, Priority
);
979 procedure Finalize_TCB
(T
: Task_Id
) is
982 Is_Self
: constant Boolean := (T
= Self
);
984 procedure Free
is new
985 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
988 if not Single_Lock
then
989 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
990 pragma Assert
(Result
= 0);
993 T
.Common
.LL
.Thread
:= 0;
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;
1013 procedure Exit_Task
is
1015 Specific
.Set
(null);
1022 procedure Abort_Task
(T
: Task_Id
) is
1027 (T
.Common
.LL
.Thread
,
1028 Signal
(Interrupt_Management
.Abort_Task_Interrupt
));
1029 pragma Assert
(Result
= 0);
1036 procedure Initialize
(S
: in out Suspension_Object
) is
1038 -- Initialize internal state (always to False (RM D.10(6)))
1043 -- Initialize internal mutex
1045 -- Use simpler binary semaphore instead of VxWorks
1046 -- mutual exclusion semaphore, because we don't need
1047 -- the fancier semantics and their overhead.
1049 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1051 -- Initialize internal condition variable
1053 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1060 procedure Finalize
(S
: in out Suspension_Object
) is
1061 pragma Unmodified
(S
);
1062 -- S may be modified on other targets, but not on VxWorks
1067 -- Destroy internal mutex
1069 Result
:= semDelete
(S
.L
);
1070 pragma Assert
(Result
= OK
);
1072 -- Destroy internal condition variable
1074 Result
:= semDelete
(S
.CV
);
1075 pragma Assert
(Result
= OK
);
1082 function Current_State
(S
: Suspension_Object
) return Boolean is
1084 -- We do not want to use lock on this read operation. State is marked
1085 -- as Atomic so that we ensure that the value retrieved is correct.
1094 procedure Set_False
(S
: in out Suspension_Object
) is
1098 SSL
.Abort_Defer
.all;
1100 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1101 pragma Assert
(Result
= OK
);
1105 Result
:= semGive
(S
.L
);
1106 pragma Assert
(Result
= OK
);
1108 SSL
.Abort_Undefer
.all;
1115 procedure Set_True
(S
: in out Suspension_Object
) is
1119 SSL
.Abort_Defer
.all;
1121 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1122 pragma Assert
(Result
= OK
);
1124 -- If there is already a task waiting on this suspension object then
1125 -- we resume it, leaving the state of the suspension object to False,
1126 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1127 -- the state to True.
1133 Result
:= semGive
(S
.CV
);
1134 pragma Assert
(Result
= OK
);
1139 Result
:= semGive
(S
.L
);
1140 pragma Assert
(Result
= OK
);
1142 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;
1344 procedure Initialize
(Environment_Task
: Task_Id
) is
1348 Environment_Task_Id
:= Environment_Task
;
1350 Interrupt_Management
.Initialize
;
1351 Specific
.Initialize
;
1353 if Locking_Policy
= 'C' then
1354 Mutex_Protocol
:= Prio_Protect
;
1355 elsif Locking_Policy
= 'I' then
1356 Mutex_Protocol
:= Prio_Inherit
;
1358 Mutex_Protocol
:= Prio_None
;
1361 if Time_Slice_Val
> 0 then
1365 (Duration (Time_Slice_Val
) / Duration (1_000_000
.0
)));
1367 elsif Dispatching_Policy
= 'R' then
1368 Result
:= Set_Time_Slice
(To_Clock_Ticks
(0.01));
1372 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1373 pragma Assert
(Result
= 0);
1375 for J
in Interrupt_Management
.Signal_ID
loop
1376 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1377 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1378 pragma Assert
(Result
= 0);
1382 -- Initialize the lock used to synchronize chain of all ATCBs
1384 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1386 -- Make environment task known here because it doesn't go through
1387 -- Activate_Tasks, which does it for all other tasks.
1389 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
1390 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
1392 Enter_Task
(Environment_Task
);
1395 end System
.Task_Primitives
.Operations
;