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-2006, 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
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 Unchecked_Conversion
;
63 with 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)
123 procedure Initialize
;
124 pragma Inline
(Initialize
);
125 -- Initialize task specific data
127 function Is_Valid_Task
return Boolean;
128 pragma Inline
(Is_Valid_Task
);
129 -- Does executing thread have a TCB?
131 procedure Set
(Self_Id
: Task_Id
);
133 -- Set the self id for the current task
136 pragma Inline
(Delete
);
137 -- Delete the task specific data associated with the current task
139 function Self
return Task_Id
;
140 pragma Inline
(Self
);
141 -- Return a pointer to the Ada Task Control Block of the calling task
145 package body Specific
is separate;
146 -- The body of this package is target specific
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 To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
174 procedure Abort_Handler
(signo
: Signal
) is
175 pragma Unreferenced
(signo
);
177 Self_ID
: constant Task_Id
:= Self
;
179 Old_Set
: aliased sigset_t
;
182 -- It is not safe to raise an exception when using ZCX and the GCC
183 -- exception handling mechanism.
185 if ZCX_By_Default
and then GCC_ZCX_Support
then
189 if Self_ID
.Deferral_Level
= 0
190 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
191 and then not Self_ID
.Aborting
193 Self_ID
.Aborting
:= True;
195 -- Make sure signals used for RTS internal purpose are unmasked
197 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
198 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
199 pragma Assert
(Result
= 0);
201 raise Standard
'Abort_Signal;
209 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
210 pragma Unreferenced
(T
);
211 pragma Unreferenced
(On
);
214 -- Nothing needed (why not???)
223 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
225 return T
.Common
.LL
.Thread
;
232 function Self
return Task_Id
renames Specific
.Self
;
234 -----------------------------
235 -- Install_Signal_Handlers --
236 -----------------------------
238 procedure Install_Signal_Handlers
is
239 act
: aliased struct_sigaction
;
240 old_act
: aliased struct_sigaction
;
241 Tmp_Set
: aliased sigset_t
;
246 act
.sa_handler
:= Abort_Handler
'Address;
248 Result
:= sigemptyset
(Tmp_Set
'Access);
249 pragma Assert
(Result
= 0);
250 act
.sa_mask
:= Tmp_Set
;
254 (Signal
(Interrupt_Management
.Abort_Task_Signal
),
255 act
'Unchecked_Access,
256 old_act
'Unchecked_Access);
257 pragma Assert
(Result
= 0);
259 Interrupt_Management
.Initialize_Interrupts
;
260 end Install_Signal_Handlers
;
262 ---------------------
263 -- Initialize_Lock --
264 ---------------------
266 procedure Initialize_Lock
(Prio
: System
.Any_Priority
; L
: access Lock
) is
268 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
269 L
.Prio_Ceiling
:= int
(Prio
);
270 L
.Protocol
:= Mutex_Protocol
;
271 pragma Assert
(L
.Mutex
/= 0);
274 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
275 pragma Unreferenced
(Level
);
278 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
279 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
280 L
.Protocol
:= Mutex_Protocol
;
281 pragma Assert
(L
.Mutex
/= 0);
288 procedure Finalize_Lock
(L
: access Lock
) is
291 Result
:= semDelete
(L
.Mutex
);
292 pragma Assert
(Result
= 0);
295 procedure Finalize_Lock
(L
: access RTS_Lock
) is
298 Result
:= semDelete
(L
.Mutex
);
299 pragma Assert
(Result
= 0);
306 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
309 if L
.Protocol
= Prio_Protect
310 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
312 Ceiling_Violation
:= True;
315 Ceiling_Violation
:= False;
318 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
319 pragma Assert
(Result
= 0);
323 (L
: access RTS_Lock
;
324 Global_Lock
: Boolean := False)
328 if not Single_Lock
or else Global_Lock
then
329 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
330 pragma Assert
(Result
= 0);
334 procedure Write_Lock
(T
: Task_Id
) is
337 if not Single_Lock
then
338 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
339 pragma Assert
(Result
= 0);
347 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
349 Write_Lock
(L
, Ceiling_Violation
);
356 procedure Unlock
(L
: access Lock
) is
359 Result
:= semGive
(L
.Mutex
);
360 pragma Assert
(Result
= 0);
363 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
366 if not Single_Lock
or else Global_Lock
then
367 Result
:= semGive
(L
.Mutex
);
368 pragma Assert
(Result
= 0);
372 procedure Unlock
(T
: Task_Id
) is
375 if not Single_Lock
then
376 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
377 pragma Assert
(Result
= 0);
385 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
386 pragma Unreferenced
(Reason
);
391 pragma Assert
(Self_ID
= Self
);
393 -- Release the mutex before sleeping
396 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
398 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
401 pragma Assert
(Result
= 0);
403 -- Perform a blocking operation to take the CV semaphore. Note that a
404 -- blocking operation in VxWorks will reenable task scheduling. When we
405 -- are no longer blocked and control is returned, task scheduling will
406 -- again be disabled.
408 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, WAIT_FOREVER
);
409 pragma Assert
(Result
= 0);
411 -- Take the mutex back
414 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
416 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
419 pragma Assert
(Result
= 0);
426 -- This is for use within the run-time system, so abort is assumed to be
427 -- already deferred, and the caller should be holding its own ATCB lock.
429 procedure Timed_Sleep
432 Mode
: ST
.Delay_Modes
;
433 Reason
: System
.Tasking
.Task_States
;
434 Timedout
: out Boolean;
435 Yielded
: out Boolean)
437 pragma Unreferenced
(Reason
);
439 Orig
: constant Duration := Monotonic_Clock
;
443 Wakeup
: Boolean := False;
449 if Mode
= Relative
then
450 Absolute
:= Orig
+ Time
;
452 -- Systematically add one since the first tick will delay *at most*
453 -- 1 / Rate_Duration seconds, so we need to add one to be on the
456 Ticks
:= To_Clock_Ticks
(Time
);
458 if Ticks
> 0 and then Ticks
< int
'Last then
464 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
469 -- Release the mutex before sleeping
472 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
474 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
477 pragma Assert
(Result
= 0);
479 -- Perform a blocking operation to take the CV semaphore. Note
480 -- that a blocking operation in VxWorks will reenable task
481 -- scheduling. When we are no longer blocked and control is
482 -- returned, task scheduling will again be disabled.
484 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
488 -- Somebody may have called Wakeup for us
493 if errno
/= S_objLib_OBJ_TIMEOUT
then
497 -- If Ticks = int'last, it was most probably truncated so
498 -- let's make another round after recomputing Ticks from
499 -- the the absolute time.
501 if Ticks
/= int
'Last then
504 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
513 -- Take the mutex back
516 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
518 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
521 pragma Assert
(Result
= 0);
523 exit when Timedout
or Wakeup
;
529 -- Should never hold a lock while yielding
532 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
534 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
537 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
539 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
548 -- This is for use in implementing delay statements, so we assume the
549 -- caller is holding no locks.
551 procedure Timed_Delay
554 Mode
: ST
.Delay_Modes
)
556 Orig
: constant Duration := Monotonic_Clock
;
560 Aborted
: Boolean := False;
563 pragma Warnings
(Off
, Result
);
566 if Mode
= Relative
then
567 Absolute
:= Orig
+ Time
;
568 Ticks
:= To_Clock_Ticks
(Time
);
570 if Ticks
> 0 and then Ticks
< int
'Last then
572 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
573 -- seconds, so we need to add one to be on the safe side.
580 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
585 -- Modifying State and Pending_Priority_Change, locking the TCB
588 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
590 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
593 pragma Assert
(Result
= 0);
595 Self_ID
.Common
.State
:= Delay_Sleep
;
599 if Self_ID
.Pending_Priority_Change
then
600 Self_ID
.Pending_Priority_Change
:= False;
601 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
602 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
605 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
607 -- Release the TCB before sleeping
610 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
612 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
614 pragma Assert
(Result
= 0);
618 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
622 -- If Ticks = int'last, it was most probably truncated
623 -- so let's make another round after recomputing Ticks
624 -- from the the absolute time.
626 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
629 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
637 -- Take back the lock after having slept, to protect further
638 -- access to Self_ID.
641 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
643 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
646 pragma Assert
(Result
= 0);
651 Self_ID
.Common
.State
:= Runnable
;
654 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
656 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
664 ---------------------
665 -- Monotonic_Clock --
666 ---------------------
668 function Monotonic_Clock
return Duration is
669 TS
: aliased timespec
;
672 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
673 pragma Assert
(Result
= 0);
674 return To_Duration
(TS
);
681 function RT_Resolution
return Duration is
683 return 1.0 / Duration (sysClkRateGet
);
690 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
691 pragma Unreferenced
(Reason
);
694 Result
:= semGive
(T
.Common
.LL
.CV
);
695 pragma Assert
(Result
= 0);
702 procedure Yield
(Do_Yield
: Boolean := True) is
703 pragma Unreferenced
(Do_Yield
);
705 pragma Unreferenced
(Result
);
707 Result
:= taskDelay
(0);
714 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
715 pragma Atomic_Components
(Prio_Array_Type
);
717 Prio_Array
: Prio_Array_Type
;
718 -- Global array containing the id of the currently running task for
719 -- each priority. Note that we assume that we are on a single processor
720 -- with run-till-blocked scheduling.
722 procedure Set_Priority
724 Prio
: System
.Any_Priority
;
725 Loss_Of_Inheritance
: Boolean := False)
727 Array_Item
: Integer;
733 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
734 pragma Assert
(Result
= 0);
736 if (Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F')
737 and then Loss_Of_Inheritance
738 and then Prio
< T
.Common
.Current_Priority
740 -- Annex D requirement [RM D.2.2 par. 9]:
742 -- If the task drops its priority due to the loss of inherited
743 -- priority, it is added at the head of the ready queue for its
744 -- new active priority.
746 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
747 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
750 -- Give some processes a chance to arrive
754 -- Then wait for our turn to proceed
756 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
757 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
760 Prio_Array
(T
.Common
.Base_Priority
) :=
761 Prio_Array
(T
.Common
.Base_Priority
) - 1;
764 T
.Common
.Current_Priority
:= Prio
;
771 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
773 return T
.Common
.Current_Priority
;
780 procedure Enter_Task
(Self_ID
: Task_Id
) is
781 procedure Init_Float
;
782 pragma Import
(C
, Init_Float
, "__gnat_init_float");
783 -- Properly initializes the FPU for PPC/MIPS systems
786 -- Store the user-level task id in the Thread field (to be used
787 -- internally by the run-time system) and the kernel-level task id in
788 -- the LWP field (to be used by the debugger).
790 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
791 Self_ID
.Common
.LL
.LWP
:= getpid
;
793 Specific
.Set
(Self_ID
);
797 -- Install the signal handlers
799 -- This is called for each task since there is no signal inheritance
800 -- between VxWorks tasks.
802 Install_Signal_Handlers
;
806 for J
in Known_Tasks
'Range loop
807 if Known_Tasks
(J
) = null then
808 Known_Tasks
(J
) := Self_ID
;
809 Self_ID
.Known_Tasks_Index
:= J
;
821 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
823 return new Ada_Task_Control_Block
(Entry_Num
);
830 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
832 -----------------------------
833 -- Register_Foreign_Thread --
834 -----------------------------
836 function Register_Foreign_Thread
return Task_Id
is
838 if Is_Valid_Task
then
841 return Register_Foreign_Thread
(taskIdSelf
);
843 end Register_Foreign_Thread
;
849 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
851 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
852 Self_ID
.Common
.LL
.Thread
:= 0;
854 if Self_ID
.Common
.LL
.CV
= 0 then
859 if not Single_Lock
then
860 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
869 procedure Create_Task
871 Wrapper
: System
.Address
;
872 Stack_Size
: System
.Parameters
.Size_Type
;
873 Priority
: System
.Any_Priority
;
874 Succeeded
: out Boolean)
876 Adjusted_Stack_Size
: size_t
;
878 -- Ask for four extra bytes of stack space so that the ATCB pointer can
879 -- be stored below the stack limit, plus extra space for the frame of
880 -- Task_Wrapper. This is so the user gets the amount of stack requested
881 -- exclusive of the needs.
883 -- We also have to allocate n more bytes for the task name storage and
884 -- enough space for the Wind Task Control Block which is around 0x778
885 -- bytes. VxWorks also seems to carve out additional space, so use 2048
886 -- as a nice round number. We might want to increment to the nearest
887 -- page size in case we ever support VxVMI.
889 -- ??? - we should come back and visit this so we can set the task name
890 -- to something appropriate.
892 Adjusted_Stack_Size
:= size_t
(Stack_Size
) + 2048;
894 -- Since the initial signal mask of a thread is inherited from the
895 -- creator, and the Environment task has all its signals masked, we do
896 -- not need to manipulate caller's signal mask at this point. All tasks
897 -- in RTS will have All_Tasks_Mask initially.
899 -- We now compute the VxWorks task name and options, then spawn ...
902 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
903 Name_Address
: System
.Address
;
904 -- Task name we are going to hand down to VxWorks
906 Task_Options
: aliased int
;
907 -- VxWorks options we are going to set for the created task,
908 -- a combination of VX_optname_TASK attributes.
910 function To_int
is new Unchecked_Conversion
(unsigned_int
, int
);
911 function To_uint
is new Unchecked_Conversion
(int
, unsigned_int
);
914 -- If there is no Ada task name handy, let VxWorks choose one.
915 -- Otherwise, tell VxWorks what the Ada task name is.
917 if T
.Common
.Task_Image_Len
= 0 then
918 Name_Address
:= System
.Null_Address
;
920 Name
(1 .. Name
'Last - 1) :=
921 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
922 Name
(Name
'Last) := ASCII
.NUL
;
923 Name_Address
:= Name
'Address;
926 -- For task options, we fetch the options assigned to the current
927 -- task, so offering some user level control over the options for a
928 -- task hierarchy, and force VX_FP_TASK because it is almost always
931 if taskOptionsGet
(taskIdSelf
, Task_Options
'Access) /= OK
then
936 To_int
(To_uint
(Task_Options
) or To_uint
(VX_FP_TASK
));
938 -- Now spawn the VxWorks task for real
940 T
.Common
.LL
.Thread
:= taskSpawn
942 To_VxWorks_Priority
(int
(Priority
)),
949 if T
.Common
.LL
.Thread
= -1 then
955 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
956 Set_Priority
(T
, Priority
);
963 procedure Finalize_TCB
(T
: Task_Id
) is
966 Is_Self
: constant Boolean := (T
= Self
);
968 procedure Free
is new
969 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
972 if not Single_Lock
then
973 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
974 pragma Assert
(Result
= 0);
977 T
.Common
.LL
.Thread
:= 0;
979 Result
:= semDelete
(T
.Common
.LL
.CV
);
980 pragma Assert
(Result
= 0);
982 if T
.Known_Tasks_Index
/= -1 then
983 Known_Tasks
(T
.Known_Tasks_Index
) := null;
997 procedure Exit_Task
is
1006 procedure Abort_Task
(T
: Task_Id
) is
1009 Result
:= kill
(T
.Common
.LL
.Thread
,
1010 Signal
(Interrupt_Management
.Abort_Task_Signal
));
1011 pragma Assert
(Result
= 0);
1018 procedure Initialize
(S
: in out Suspension_Object
) is
1020 -- Initialize internal state. It is always initialized to False (ARM
1026 -- Initialize internal mutex
1028 -- Use simpler binary semaphore instead of VxWorks
1029 -- mutual exclusion semaphore, because we don't need
1030 -- the fancier semantics and their overhead.
1032 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1034 -- Initialize internal condition variable
1036 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1043 procedure Finalize
(S
: in out Suspension_Object
) is
1046 -- Destroy internal mutex
1048 Result
:= semDelete
(S
.L
);
1049 pragma Assert
(Result
= OK
);
1051 -- Destroy internal condition variable
1053 Result
:= semDelete
(S
.CV
);
1054 pragma Assert
(Result
= OK
);
1061 function Current_State
(S
: Suspension_Object
) return Boolean is
1063 -- We do not want to use lock on this read operation. State is marked
1064 -- as Atomic so that we ensure that the value retrieved is correct.
1073 procedure Set_False
(S
: in out Suspension_Object
) is
1076 SSL
.Abort_Defer
.all;
1078 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1079 pragma Assert
(Result
= OK
);
1083 Result
:= semGive
(S
.L
);
1084 pragma Assert
(Result
= OK
);
1086 SSL
.Abort_Undefer
.all;
1093 procedure Set_True
(S
: in out Suspension_Object
) is
1096 SSL
.Abort_Defer
.all;
1098 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1099 pragma Assert
(Result
= OK
);
1101 -- If there is already a task waiting on this suspension object then
1102 -- we resume it, leaving the state of the suspension object to False,
1103 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1104 -- the state to True.
1110 Result
:= semGive
(S
.CV
);
1111 pragma Assert
(Result
= OK
);
1116 Result
:= semGive
(S
.L
);
1117 pragma Assert
(Result
= OK
);
1119 SSL
.Abort_Undefer
.all;
1122 ------------------------
1123 -- Suspend_Until_True --
1124 ------------------------
1126 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1129 SSL
.Abort_Defer
.all;
1131 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1134 -- Program_Error must be raised upon calling Suspend_Until_True
1135 -- if another task is already waiting on that suspension object
1136 -- (ARM D.10 par. 10).
1138 Result
:= semGive
(S
.L
);
1139 pragma Assert
(Result
= OK
);
1141 SSL
.Abort_Undefer
.all;
1143 raise Program_Error
;
1145 -- Suspend the task if the state is False. Otherwise, the task
1146 -- continues its execution, and the state of the suspension object
1147 -- is set to False (ARM D.10 par. 9).
1152 Result
:= semGive
(S
.L
);
1153 pragma Assert
(Result
= 0);
1155 SSL
.Abort_Undefer
.all;
1159 -- Release the mutex before sleeping
1161 Result
:= semGive
(S
.L
);
1162 pragma Assert
(Result
= OK
);
1164 SSL
.Abort_Undefer
.all;
1166 Result
:= semTake
(S
.CV
, WAIT_FOREVER
);
1167 pragma Assert
(Result
= 0);
1170 end Suspend_Until_True
;
1178 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1179 pragma Unreferenced
(Self_ID
);
1184 --------------------
1185 -- Check_No_Locks --
1186 --------------------
1188 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1189 pragma Unreferenced
(Self_ID
);
1194 ----------------------
1195 -- Environment_Task --
1196 ----------------------
1198 function Environment_Task
return Task_Id
is
1200 return Environment_Task_Id
;
1201 end Environment_Task
;
1207 procedure Lock_RTS
is
1209 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1216 procedure Unlock_RTS
is
1218 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1225 function Suspend_Task
1227 Thread_Self
: Thread_Id
) return Boolean
1230 if T
.Common
.LL
.Thread
/= 0
1231 and then T
.Common
.LL
.Thread
/= Thread_Self
1233 return taskSuspend
(T
.Common
.LL
.Thread
) = 0;
1243 function Resume_Task
1245 Thread_Self
: Thread_Id
) return Boolean
1248 if T
.Common
.LL
.Thread
/= 0
1249 and then T
.Common
.LL
.Thread
/= Thread_Self
1251 return taskResume
(T
.Common
.LL
.Thread
) = 0;
1261 procedure Initialize
(Environment_Task
: Task_Id
) is
1264 Environment_Task_Id
:= Environment_Task
;
1266 Interrupt_Management
.Initialize
;
1267 Specific
.Initialize
;
1269 if Locking_Policy
= 'C' then
1270 Mutex_Protocol
:= Prio_Protect
;
1271 elsif Locking_Policy
= 'I' then
1272 Mutex_Protocol
:= Prio_Inherit
;
1274 Mutex_Protocol
:= Prio_None
;
1277 if Time_Slice_Val
> 0 then
1278 Result
:= Set_Time_Slice
1280 (Duration (Time_Slice_Val
) / Duration (1_000_000
.0
)));
1282 elsif Dispatching_Policy
= 'R' then
1283 Result
:= Set_Time_Slice
(To_Clock_Ticks
(0.01));
1287 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1288 pragma Assert
(Result
= 0);
1290 for J
in Interrupt_Management
.Signal_ID
loop
1291 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1292 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1293 pragma Assert
(Result
= 0);
1297 -- Initialize the lock used to synchronize chain of all ATCBs
1299 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1301 Enter_Task
(Environment_Task
);
1304 end System
.Task_Primitives
.Operations
;