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-2007, 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 2, or (at your option) any later ver- --
14 -- sion. GNARL 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNARL; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 -- This is the VxWorks version of this package
36 -- This package contains all the GNULL primitives that interface directly
37 -- with the underlying OS.
40 -- Turn off polling, we do not want ATC polling to take place during
41 -- tasking operations. It causes infinite loops and other problems.
43 with System
.Tasking
.Debug
;
44 -- used for Known_Tasks
46 with System
.Interrupt_Management
;
47 -- used for Keep_Unmasked
48 -- Abort_Task_Interrupt
50 -- Initialize_Interrupts
54 with System
.Soft_Links
;
55 -- used for Abort_Defer/Undefer
57 -- We use System.Soft_Links instead of System.Tasking.Initialization
58 -- because the later is a higher level package that we shouldn't depend on.
59 -- For example when using the restricted run time, it is replaced by
60 -- System.Tasking.Restricted.Stages.
62 with Ada
.Unchecked_Conversion
;
63 with Ada
.Unchecked_Deallocation
;
65 package body System
.Task_Primitives
.Operations
is
67 package SSL
renames System
.Soft_Links
;
69 use System
.Tasking
.Debug
;
71 use System
.OS_Interface
;
72 use System
.Parameters
;
73 use type Interfaces
.C
.int
;
75 subtype int
is System
.OS_Interface
.int
;
77 Relative
: constant := 0;
83 -- The followings are logically constants, but need to be initialized at
86 Single_RTS_Lock
: aliased RTS_Lock
;
87 -- This is a lock to allow only one thread of control in the RTS at a
88 -- time; it is used to execute in mutual exclusion from all other tasks.
89 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
91 Environment_Task_Id
: Task_Id
;
92 -- A variable to hold Task_Id for the environment task
94 Unblocked_Signal_Mask
: aliased sigset_t
;
95 -- The set of signals that should unblocked in all tasks
97 -- The followings are internal configuration constants needed
99 Time_Slice_Val
: Integer;
100 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
102 Locking_Policy
: Character;
103 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
105 Dispatching_Policy
: Character;
106 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
108 function Get_Policy
(Prio
: System
.Any_Priority
) return Character;
109 pragma Import
(C
, Get_Policy
, "__gnat_get_specific_dispatching");
110 -- Get priority specific dispatching policy
112 Mutex_Protocol
: Priority_Type
;
114 Foreign_Task_Elaborated
: aliased Boolean := True;
115 -- Used to identified fake tasks (i.e., non-Ada Threads)
117 type Set_Stack_Limit_Proc_Acc
is access procedure;
118 pragma Convention
(C
, Set_Stack_Limit_Proc_Acc
);
120 Set_Stack_Limit_Hook
: Set_Stack_Limit_Proc_Acc
;
121 pragma Import
(C
, Set_Stack_Limit_Hook
, "__gnat_set_stack_limit_hook");
122 -- Procedure to be called when a task is created to set stack
131 procedure Initialize
;
132 pragma Inline
(Initialize
);
133 -- Initialize task specific data
135 function Is_Valid_Task
return Boolean;
136 pragma Inline
(Is_Valid_Task
);
137 -- Does executing thread have a TCB?
139 procedure Set
(Self_Id
: Task_Id
);
141 -- Set the self id for the current task
144 pragma Inline
(Delete
);
145 -- Delete the task specific data associated with the current task
147 function Self
return Task_Id
;
148 pragma Inline
(Self
);
149 -- Return a pointer to the Ada Task Control Block of the calling task
153 package body Specific
is separate;
154 -- The body of this package is target specific
156 ---------------------------------
157 -- Support for foreign threads --
158 ---------------------------------
160 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
161 -- Allocate and Initialize a new ATCB for the current Thread
163 function Register_Foreign_Thread
164 (Thread
: Thread_Id
) return Task_Id
is separate;
166 -----------------------
167 -- Local Subprograms --
168 -----------------------
170 procedure Abort_Handler
(signo
: Signal
);
171 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
173 procedure Install_Signal_Handlers
;
174 -- Install the default signal handlers for the current task
176 function To_Address
is
177 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
183 procedure Abort_Handler
(signo
: Signal
) is
184 pragma Unreferenced
(signo
);
186 Self_ID
: constant Task_Id
:= Self
;
187 Old_Set
: aliased sigset_t
;
190 pragma Warnings
(Off
, Result
);
193 -- It is not safe to raise an exception when using ZCX and the GCC
194 -- exception handling mechanism.
196 if ZCX_By_Default
and then GCC_ZCX_Support
then
200 if Self_ID
.Deferral_Level
= 0
201 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
202 and then not Self_ID
.Aborting
204 Self_ID
.Aborting
:= True;
206 -- Make sure signals used for RTS internal purpose are unmasked
211 Unblocked_Signal_Mask
'Access,
213 pragma Assert
(Result
= 0);
215 raise Standard
'Abort_Signal;
223 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
224 pragma Unreferenced
(T
);
225 pragma Unreferenced
(On
);
228 -- Nothing needed (why not???)
237 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
239 return T
.Common
.LL
.Thread
;
246 function Self
return Task_Id
renames Specific
.Self
;
248 -----------------------------
249 -- Install_Signal_Handlers --
250 -----------------------------
252 procedure Install_Signal_Handlers
is
253 act
: aliased struct_sigaction
;
254 old_act
: aliased struct_sigaction
;
255 Tmp_Set
: aliased sigset_t
;
260 act
.sa_handler
:= Abort_Handler
'Address;
262 Result
:= sigemptyset
(Tmp_Set
'Access);
263 pragma Assert
(Result
= 0);
264 act
.sa_mask
:= Tmp_Set
;
268 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
269 act
'Unchecked_Access,
270 old_act
'Unchecked_Access);
271 pragma Assert
(Result
= 0);
273 Interrupt_Management
.Initialize_Interrupts
;
274 end Install_Signal_Handlers
;
276 ---------------------
277 -- Initialize_Lock --
278 ---------------------
280 procedure Initialize_Lock
281 (Prio
: System
.Any_Priority
;
282 L
: not null access Lock
)
285 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
286 L
.Prio_Ceiling
:= int
(Prio
);
287 L
.Protocol
:= Mutex_Protocol
;
288 pragma Assert
(L
.Mutex
/= 0);
291 procedure Initialize_Lock
292 (L
: not null access RTS_Lock
;
295 pragma Unreferenced
(Level
);
297 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
298 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
299 L
.Protocol
:= Mutex_Protocol
;
300 pragma Assert
(L
.Mutex
/= 0);
307 procedure Finalize_Lock
(L
: not null access Lock
) is
310 Result
:= semDelete
(L
.Mutex
);
311 pragma Assert
(Result
= 0);
314 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
317 Result
:= semDelete
(L
.Mutex
);
318 pragma Assert
(Result
= 0);
326 (L
: not null access Lock
;
327 Ceiling_Violation
: out Boolean)
332 if L
.Protocol
= Prio_Protect
333 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
335 Ceiling_Violation
:= True;
338 Ceiling_Violation
:= False;
341 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
342 pragma Assert
(Result
= 0);
346 (L
: not null access RTS_Lock
;
347 Global_Lock
: Boolean := False)
351 if not Single_Lock
or else Global_Lock
then
352 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
353 pragma Assert
(Result
= 0);
357 procedure Write_Lock
(T
: Task_Id
) is
360 if not Single_Lock
then
361 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
362 pragma Assert
(Result
= 0);
371 (L
: not null access Lock
;
372 Ceiling_Violation
: out Boolean)
375 Write_Lock
(L
, Ceiling_Violation
);
382 procedure Unlock
(L
: not null access Lock
) is
385 Result
:= semGive
(L
.Mutex
);
386 pragma Assert
(Result
= 0);
390 (L
: not null access RTS_Lock
;
391 Global_Lock
: Boolean := False)
395 if not Single_Lock
or else Global_Lock
then
396 Result
:= semGive
(L
.Mutex
);
397 pragma Assert
(Result
= 0);
401 procedure Unlock
(T
: Task_Id
) is
404 if not Single_Lock
then
405 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
406 pragma Assert
(Result
= 0);
414 -- Dynamic priority ceilings are not supported by the underlying system
416 procedure Set_Ceiling
417 (L
: not null access Lock
;
418 Prio
: System
.Any_Priority
)
420 pragma Unreferenced
(L
, Prio
);
429 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
430 pragma Unreferenced
(Reason
);
435 pragma Assert
(Self_ID
= Self
);
437 -- Release the mutex before sleeping
440 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
442 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
445 pragma Assert
(Result
= 0);
447 -- Perform a blocking operation to take the CV semaphore. Note that a
448 -- blocking operation in VxWorks will reenable task scheduling. When we
449 -- are no longer blocked and control is returned, task scheduling will
450 -- again be disabled.
452 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, WAIT_FOREVER
);
453 pragma Assert
(Result
= 0);
455 -- Take the mutex back
458 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
460 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
463 pragma Assert
(Result
= 0);
470 -- This is for use within the run-time system, so abort is assumed to be
471 -- already deferred, and the caller should be holding its own ATCB lock.
473 procedure Timed_Sleep
476 Mode
: ST
.Delay_Modes
;
477 Reason
: System
.Tasking
.Task_States
;
478 Timedout
: out Boolean;
479 Yielded
: out Boolean)
481 pragma Unreferenced
(Reason
);
483 Orig
: constant Duration := Monotonic_Clock
;
487 Wakeup
: Boolean := False;
493 if Mode
= Relative
then
494 Absolute
:= Orig
+ Time
;
496 -- Systematically add one since the first tick will delay *at most*
497 -- 1 / Rate_Duration seconds, so we need to add one to be on the
500 Ticks
:= To_Clock_Ticks
(Time
);
502 if Ticks
> 0 and then Ticks
< int
'Last then
508 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
513 -- Release the mutex before sleeping
516 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
518 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
521 pragma Assert
(Result
= 0);
523 -- Perform a blocking operation to take the CV semaphore. Note
524 -- that a blocking operation in VxWorks will reenable task
525 -- scheduling. When we are no longer blocked and control is
526 -- returned, task scheduling will again be disabled.
528 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
532 -- Somebody may have called Wakeup for us
537 if errno
/= S_objLib_OBJ_TIMEOUT
then
541 -- If Ticks = int'last, it was most probably truncated so
542 -- let's make another round after recomputing Ticks from
543 -- the the absolute time.
545 if Ticks
/= int
'Last then
549 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
558 -- Take the mutex back
561 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
563 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
566 pragma Assert
(Result
= 0);
568 exit when Timedout
or Wakeup
;
574 -- Should never hold a lock while yielding
577 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
579 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
582 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
584 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
593 -- This is for use in implementing delay statements, so we assume the
594 -- caller is holding no locks.
596 procedure Timed_Delay
599 Mode
: ST
.Delay_Modes
)
601 Orig
: constant Duration := Monotonic_Clock
;
605 Aborted
: Boolean := False;
608 pragma Warnings
(Off
, Result
);
611 if Mode
= Relative
then
612 Absolute
:= Orig
+ Time
;
613 Ticks
:= To_Clock_Ticks
(Time
);
615 if Ticks
> 0 and then Ticks
< int
'Last then
617 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
618 -- seconds, so we need to add one to be on the safe side.
625 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
630 -- Modifying State, locking the TCB
633 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
635 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
638 pragma Assert
(Result
= 0);
640 Self_ID
.Common
.State
:= Delay_Sleep
;
644 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
646 -- Release the TCB before sleeping
649 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
651 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
653 pragma Assert
(Result
= 0);
657 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
661 -- If Ticks = int'last, it was most probably truncated
662 -- so let's make another round after recomputing Ticks
663 -- from the the absolute time.
665 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
668 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
676 -- Take back the lock after having slept, to protect further
677 -- access to Self_ID.
680 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
682 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
685 pragma Assert
(Result
= 0);
690 Self_ID
.Common
.State
:= Runnable
;
693 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
695 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
703 ---------------------
704 -- Monotonic_Clock --
705 ---------------------
707 function Monotonic_Clock
return Duration is
708 TS
: aliased timespec
;
711 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
712 pragma Assert
(Result
= 0);
713 return To_Duration
(TS
);
720 function RT_Resolution
return Duration is
722 return 1.0 / Duration (sysClkRateGet
);
729 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
730 pragma Unreferenced
(Reason
);
733 Result
:= semGive
(T
.Common
.LL
.CV
);
734 pragma Assert
(Result
= 0);
741 procedure Yield
(Do_Yield
: Boolean := True) is
742 pragma Unreferenced
(Do_Yield
);
744 pragma Unreferenced
(Result
);
746 Result
:= taskDelay
(0);
753 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
754 pragma Atomic_Components
(Prio_Array_Type
);
756 Prio_Array
: Prio_Array_Type
;
757 -- Global array containing the id of the currently running task for
758 -- each priority. Note that we assume that we are on a single processor
759 -- with run-till-blocked scheduling.
761 procedure Set_Priority
763 Prio
: System
.Any_Priority
;
764 Loss_Of_Inheritance
: Boolean := False)
766 Array_Item
: Integer;
772 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
773 pragma Assert
(Result
= 0);
775 if (Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F')
776 and then Loss_Of_Inheritance
777 and then Prio
< T
.Common
.Current_Priority
779 -- Annex D requirement (RM D.2.2(9))
781 -- If the task drops its priority due to the loss of inherited
782 -- priority, it is added at the head of the ready queue for its
783 -- new active priority.
785 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
786 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
789 -- Give some processes a chance to arrive
793 -- Then wait for our turn to proceed
795 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
796 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
799 Prio_Array
(T
.Common
.Base_Priority
) :=
800 Prio_Array
(T
.Common
.Base_Priority
) - 1;
803 T
.Common
.Current_Priority
:= Prio
;
810 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
812 return T
.Common
.Current_Priority
;
819 procedure Enter_Task
(Self_ID
: Task_Id
) is
820 procedure Init_Float
;
821 pragma Import
(C
, Init_Float
, "__gnat_init_float");
822 -- Properly initializes the FPU for PPC/MIPS systems
825 -- Store the user-level task id in the Thread field (to be used
826 -- internally by the run-time system) and the kernel-level task id in
827 -- the LWP field (to be used by the debugger).
829 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
830 Self_ID
.Common
.LL
.LWP
:= getpid
;
832 Specific
.Set
(Self_ID
);
836 -- Install the signal handlers
838 -- This is called for each task since there is no signal inheritance
839 -- between VxWorks tasks.
841 Install_Signal_Handlers
;
845 for J
in Known_Tasks
'Range loop
846 if Known_Tasks
(J
) = null then
847 Known_Tasks
(J
) := Self_ID
;
848 Self_ID
.Known_Tasks_Index
:= J
;
855 -- If stack checking is enabled set the stack limit for this task.
856 if Set_Stack_Limit_Hook
/= null then
857 Set_Stack_Limit_Hook
.all;
865 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
867 return new Ada_Task_Control_Block
(Entry_Num
);
874 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
876 -----------------------------
877 -- Register_Foreign_Thread --
878 -----------------------------
880 function Register_Foreign_Thread
return Task_Id
is
882 if Is_Valid_Task
then
885 return Register_Foreign_Thread
(taskIdSelf
);
887 end Register_Foreign_Thread
;
893 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
895 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
896 Self_ID
.Common
.LL
.Thread
:= 0;
898 if Self_ID
.Common
.LL
.CV
= 0 then
904 if not Single_Lock
then
905 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
914 procedure Create_Task
916 Wrapper
: System
.Address
;
917 Stack_Size
: System
.Parameters
.Size_Type
;
918 Priority
: System
.Any_Priority
;
919 Succeeded
: out Boolean)
921 Adjusted_Stack_Size
: size_t
;
923 -- Ask for four extra bytes of stack space so that the ATCB pointer can
924 -- be stored below the stack limit, plus extra space for the frame of
925 -- Task_Wrapper. This is so the user gets the amount of stack requested
926 -- exclusive of the needs.
928 -- We also have to allocate n more bytes for the task name storage and
929 -- enough space for the Wind Task Control Block which is around 0x778
930 -- bytes. VxWorks also seems to carve out additional space, so use 2048
931 -- as a nice round number. We might want to increment to the nearest
932 -- page size in case we ever support VxVMI.
934 -- ??? - we should come back and visit this so we can set the task name
935 -- to something appropriate.
937 Adjusted_Stack_Size
:= size_t
(Stack_Size
) + 2048;
939 -- Since the initial signal mask of a thread is inherited from the
940 -- creator, and the Environment task has all its signals masked, we do
941 -- not need to manipulate caller's signal mask at this point. All tasks
942 -- in RTS will have All_Tasks_Mask initially.
944 -- We now compute the VxWorks task name and options, then spawn ...
947 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
948 Name_Address
: System
.Address
;
949 -- Task name we are going to hand down to VxWorks
951 function Get_Task_Options
return int
;
952 pragma Import
(C
, Get_Task_Options
, "__gnat_get_task_options");
953 -- Function that returns the options to be set for the task that we
954 -- are creating. We fetch the options assigned to the current task,
955 -- so offering some user level control over the options for a task
956 -- hierarchy, and force VX_FP_TASK because it is almost always
960 -- If there is no Ada task name handy, let VxWorks choose one.
961 -- Otherwise, tell VxWorks what the Ada task name is.
963 if T
.Common
.Task_Image_Len
= 0 then
964 Name_Address
:= System
.Null_Address
;
966 Name
(1 .. Name
'Last - 1) :=
967 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
968 Name
(Name
'Last) := ASCII
.NUL
;
969 Name_Address
:= Name
'Address;
972 -- Now spawn the VxWorks task for real
974 T
.Common
.LL
.Thread
:=
977 To_VxWorks_Priority
(int
(Priority
)),
984 if T
.Common
.LL
.Thread
= -1 then
990 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
991 Set_Priority
(T
, Priority
);
998 procedure Finalize_TCB
(T
: Task_Id
) is
1001 Is_Self
: constant Boolean := (T
= Self
);
1003 procedure Free
is new
1004 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
1007 if not Single_Lock
then
1008 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
1009 pragma Assert
(Result
= 0);
1012 T
.Common
.LL
.Thread
:= 0;
1014 Result
:= semDelete
(T
.Common
.LL
.CV
);
1015 pragma Assert
(Result
= 0);
1017 if T
.Known_Tasks_Index
/= -1 then
1018 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1032 procedure Exit_Task
is
1034 Specific
.Set
(null);
1041 procedure Abort_Task
(T
: Task_Id
) is
1046 (T
.Common
.LL
.Thread
,
1047 Signal
(Interrupt_Management
.Abort_Task_Interrupt
));
1048 pragma Assert
(Result
= 0);
1055 procedure Initialize
(S
: in out Suspension_Object
) is
1057 -- Initialize internal state (always to False (RM D.10(6)))
1062 -- Initialize internal mutex
1064 -- Use simpler binary semaphore instead of VxWorks
1065 -- mutual exclusion semaphore, because we don't need
1066 -- the fancier semantics and their overhead.
1068 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1070 -- Initialize internal condition variable
1072 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1079 procedure Finalize
(S
: in out Suspension_Object
) is
1083 -- Destroy internal mutex
1085 Result
:= semDelete
(S
.L
);
1086 pragma Assert
(Result
= OK
);
1088 -- Destroy internal condition variable
1090 Result
:= semDelete
(S
.CV
);
1091 pragma Assert
(Result
= OK
);
1098 function Current_State
(S
: Suspension_Object
) return Boolean is
1100 -- We do not want to use lock on this read operation. State is marked
1101 -- as Atomic so that we ensure that the value retrieved is correct.
1110 procedure Set_False
(S
: in out Suspension_Object
) is
1114 SSL
.Abort_Defer
.all;
1116 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1117 pragma Assert
(Result
= OK
);
1121 Result
:= semGive
(S
.L
);
1122 pragma Assert
(Result
= OK
);
1124 SSL
.Abort_Undefer
.all;
1131 procedure Set_True
(S
: in out Suspension_Object
) is
1135 SSL
.Abort_Defer
.all;
1137 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1138 pragma Assert
(Result
= OK
);
1140 -- If there is already a task waiting on this suspension object then
1141 -- we resume it, leaving the state of the suspension object to False,
1142 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1143 -- the state to True.
1149 Result
:= semGive
(S
.CV
);
1150 pragma Assert
(Result
= OK
);
1155 Result
:= semGive
(S
.L
);
1156 pragma Assert
(Result
= OK
);
1158 SSL
.Abort_Undefer
.all;
1161 ------------------------
1162 -- Suspend_Until_True --
1163 ------------------------
1165 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1169 SSL
.Abort_Defer
.all;
1171 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1175 -- Program_Error must be raised upon calling Suspend_Until_True
1176 -- if another task is already waiting on that suspension object
1177 -- (ARM D.10 par. 10).
1179 Result
:= semGive
(S
.L
);
1180 pragma Assert
(Result
= OK
);
1182 SSL
.Abort_Undefer
.all;
1184 raise Program_Error
;
1187 -- Suspend the task if the state is False. Otherwise, the task
1188 -- continues its execution, and the state of the suspension object
1189 -- is set to False (ARM D.10 par. 9).
1194 Result
:= semGive
(S
.L
);
1195 pragma Assert
(Result
= 0);
1197 SSL
.Abort_Undefer
.all;
1202 -- Release the mutex before sleeping
1204 Result
:= semGive
(S
.L
);
1205 pragma Assert
(Result
= OK
);
1207 SSL
.Abort_Undefer
.all;
1209 Result
:= semTake
(S
.CV
, WAIT_FOREVER
);
1210 pragma Assert
(Result
= 0);
1213 end Suspend_Until_True
;
1221 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1222 pragma Unreferenced
(Self_ID
);
1227 --------------------
1228 -- Check_No_Locks --
1229 --------------------
1231 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1232 pragma Unreferenced
(Self_ID
);
1237 ----------------------
1238 -- Environment_Task --
1239 ----------------------
1241 function Environment_Task
return Task_Id
is
1243 return Environment_Task_Id
;
1244 end Environment_Task
;
1250 procedure Lock_RTS
is
1252 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1259 procedure Unlock_RTS
is
1261 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1268 function Suspend_Task
1270 Thread_Self
: Thread_Id
) return Boolean
1273 if T
.Common
.LL
.Thread
/= 0
1274 and then T
.Common
.LL
.Thread
/= Thread_Self
1276 return taskSuspend
(T
.Common
.LL
.Thread
) = 0;
1286 function Resume_Task
1288 Thread_Self
: Thread_Id
) return Boolean
1291 if T
.Common
.LL
.Thread
/= 0
1292 and then T
.Common
.LL
.Thread
/= Thread_Self
1294 return taskResume
(T
.Common
.LL
.Thread
) = 0;
1300 --------------------
1301 -- Stop_All_Tasks --
1302 --------------------
1304 procedure Stop_All_Tasks
1306 Thread_Self
: constant Thread_Id
:= taskIdSelf
;
1310 pragma Unreferenced
(Dummy
);
1315 C
:= All_Tasks_List
;
1316 while C
/= null loop
1317 if C
.Common
.LL
.Thread
/= 0
1318 and then C
.Common
.LL
.Thread
/= Thread_Self
1320 Dummy
:= Task_Stop
(C
.Common
.LL
.Thread
);
1323 C
:= C
.Common
.All_Tasks_Link
;
1326 Dummy
:= Int_Unlock
;
1333 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1335 if T
.Common
.LL
.Thread
/= 0 then
1336 return Task_Stop
(T
.Common
.LL
.Thread
) = 0;
1346 function Continue_Task
(T
: ST
.Task_Id
) return Boolean
1349 if T
.Common
.LL
.Thread
/= 0 then
1350 return Task_Cont
(T
.Common
.LL
.Thread
) = 0;
1360 procedure Initialize
(Environment_Task
: Task_Id
) is
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 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1389 pragma Assert
(Result
= 0);
1391 for J
in Interrupt_Management
.Signal_ID
loop
1392 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1393 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1394 pragma Assert
(Result
= 0);
1398 -- Initialize the lock used to synchronize chain of all ATCBs
1400 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1402 Enter_Task
(Environment_Task
);
1405 end System
.Task_Primitives
.Operations
;