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)
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
169 new Ada
.Unchecked_Conversion
(Task_Id
, System
.Address
);
175 procedure Abort_Handler
(signo
: Signal
) is
176 pragma Unreferenced
(signo
);
178 Self_ID
: constant Task_Id
:= Self
;
180 Old_Set
: aliased sigset_t
;
183 -- It is not safe to raise an exception when using ZCX and the GCC
184 -- exception handling mechanism.
186 if ZCX_By_Default
and then GCC_ZCX_Support
then
190 if Self_ID
.Deferral_Level
= 0
191 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
192 and then not Self_ID
.Aborting
194 Self_ID
.Aborting
:= True;
196 -- Make sure signals used for RTS internal purpose are unmasked
201 Unblocked_Signal_Mask
'Unchecked_Access,
202 Old_Set
'Unchecked_Access);
203 pragma Assert
(Result
= 0);
205 raise Standard
'Abort_Signal;
213 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
214 pragma Unreferenced
(T
);
215 pragma Unreferenced
(On
);
218 -- Nothing needed (why not???)
227 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
229 return T
.Common
.LL
.Thread
;
236 function Self
return Task_Id
renames Specific
.Self
;
238 -----------------------------
239 -- Install_Signal_Handlers --
240 -----------------------------
242 procedure Install_Signal_Handlers
is
243 act
: aliased struct_sigaction
;
244 old_act
: aliased struct_sigaction
;
245 Tmp_Set
: aliased sigset_t
;
250 act
.sa_handler
:= Abort_Handler
'Address;
252 Result
:= sigemptyset
(Tmp_Set
'Access);
253 pragma Assert
(Result
= 0);
254 act
.sa_mask
:= Tmp_Set
;
258 (Signal
(Interrupt_Management
.Abort_Task_Interrupt
),
259 act
'Unchecked_Access,
260 old_act
'Unchecked_Access);
261 pragma Assert
(Result
= 0);
263 Interrupt_Management
.Initialize_Interrupts
;
264 end Install_Signal_Handlers
;
266 ---------------------
267 -- Initialize_Lock --
268 ---------------------
270 procedure Initialize_Lock
271 (Prio
: System
.Any_Priority
;
272 L
: not null access Lock
)
275 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
276 L
.Prio_Ceiling
:= int
(Prio
);
277 L
.Protocol
:= Mutex_Protocol
;
278 pragma Assert
(L
.Mutex
/= 0);
281 procedure Initialize_Lock
282 (L
: not null access RTS_Lock
;
285 pragma Unreferenced
(Level
);
287 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
288 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
289 L
.Protocol
:= Mutex_Protocol
;
290 pragma Assert
(L
.Mutex
/= 0);
297 procedure Finalize_Lock
(L
: not null access Lock
) is
300 Result
:= semDelete
(L
.Mutex
);
301 pragma Assert
(Result
= 0);
304 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
307 Result
:= semDelete
(L
.Mutex
);
308 pragma Assert
(Result
= 0);
316 (L
: not null access Lock
;
317 Ceiling_Violation
: out Boolean)
322 if L
.Protocol
= Prio_Protect
323 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
325 Ceiling_Violation
:= True;
328 Ceiling_Violation
:= False;
331 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
332 pragma Assert
(Result
= 0);
336 (L
: not null access RTS_Lock
;
337 Global_Lock
: Boolean := False)
341 if not Single_Lock
or else Global_Lock
then
342 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
343 pragma Assert
(Result
= 0);
347 procedure Write_Lock
(T
: Task_Id
) is
350 if not Single_Lock
then
351 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
352 pragma Assert
(Result
= 0);
361 (L
: not null access Lock
;
362 Ceiling_Violation
: out Boolean)
365 Write_Lock
(L
, Ceiling_Violation
);
372 procedure Unlock
(L
: not null access Lock
) is
375 Result
:= semGive
(L
.Mutex
);
376 pragma Assert
(Result
= 0);
380 (L
: not null access RTS_Lock
;
381 Global_Lock
: Boolean := False)
385 if not Single_Lock
or else Global_Lock
then
386 Result
:= semGive
(L
.Mutex
);
387 pragma Assert
(Result
= 0);
391 procedure Unlock
(T
: Task_Id
) is
394 if not Single_Lock
then
395 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
396 pragma Assert
(Result
= 0);
404 -- Dynamic priority ceilings are not supported by the underlying system
406 procedure Set_Ceiling
407 (L
: not null access Lock
;
408 Prio
: System
.Any_Priority
)
410 pragma Unreferenced
(L
, Prio
);
419 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
420 pragma Unreferenced
(Reason
);
425 pragma Assert
(Self_ID
= Self
);
427 -- Release the mutex before sleeping
430 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
432 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
435 pragma Assert
(Result
= 0);
437 -- Perform a blocking operation to take the CV semaphore. Note that a
438 -- blocking operation in VxWorks will reenable task scheduling. When we
439 -- are no longer blocked and control is returned, task scheduling will
440 -- again be disabled.
442 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, WAIT_FOREVER
);
443 pragma Assert
(Result
= 0);
445 -- Take the mutex back
448 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
450 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
453 pragma Assert
(Result
= 0);
460 -- This is for use within the run-time system, so abort is assumed to be
461 -- already deferred, and the caller should be holding its own ATCB lock.
463 procedure Timed_Sleep
466 Mode
: ST
.Delay_Modes
;
467 Reason
: System
.Tasking
.Task_States
;
468 Timedout
: out Boolean;
469 Yielded
: out Boolean)
471 pragma Unreferenced
(Reason
);
473 Orig
: constant Duration := Monotonic_Clock
;
477 Wakeup
: Boolean := False;
483 if Mode
= Relative
then
484 Absolute
:= Orig
+ Time
;
486 -- Systematically add one since the first tick will delay *at most*
487 -- 1 / Rate_Duration seconds, so we need to add one to be on the
490 Ticks
:= To_Clock_Ticks
(Time
);
492 if Ticks
> 0 and then Ticks
< int
'Last then
498 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
503 -- Release the mutex before sleeping
506 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
508 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
511 pragma Assert
(Result
= 0);
513 -- Perform a blocking operation to take the CV semaphore. Note
514 -- that a blocking operation in VxWorks will reenable task
515 -- scheduling. When we are no longer blocked and control is
516 -- returned, task scheduling will again be disabled.
518 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
522 -- Somebody may have called Wakeup for us
527 if errno
/= S_objLib_OBJ_TIMEOUT
then
531 -- If Ticks = int'last, it was most probably truncated so
532 -- let's make another round after recomputing Ticks from
533 -- the the absolute time.
535 if Ticks
/= int
'Last then
539 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
548 -- Take the mutex back
551 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
553 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
556 pragma Assert
(Result
= 0);
558 exit when Timedout
or Wakeup
;
564 -- Should never hold a lock while yielding
567 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
569 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
572 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
574 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
583 -- This is for use in implementing delay statements, so we assume the
584 -- caller is holding no locks.
586 procedure Timed_Delay
589 Mode
: ST
.Delay_Modes
)
591 Orig
: constant Duration := Monotonic_Clock
;
595 Aborted
: Boolean := False;
598 pragma Warnings
(Off
, Result
);
601 if Mode
= Relative
then
602 Absolute
:= Orig
+ Time
;
603 Ticks
:= To_Clock_Ticks
(Time
);
605 if Ticks
> 0 and then Ticks
< int
'Last then
607 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
608 -- seconds, so we need to add one to be on the safe side.
615 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
620 -- Modifying State, locking the TCB
623 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
625 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
628 pragma Assert
(Result
= 0);
630 Self_ID
.Common
.State
:= Delay_Sleep
;
634 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
636 -- Release the TCB before sleeping
639 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
641 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
643 pragma Assert
(Result
= 0);
647 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
651 -- If Ticks = int'last, it was most probably truncated
652 -- so let's make another round after recomputing Ticks
653 -- from the the absolute time.
655 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
658 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
666 -- Take back the lock after having slept, to protect further
667 -- access to Self_ID.
670 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
672 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
675 pragma Assert
(Result
= 0);
680 Self_ID
.Common
.State
:= Runnable
;
683 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
685 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
693 ---------------------
694 -- Monotonic_Clock --
695 ---------------------
697 function Monotonic_Clock
return Duration is
698 TS
: aliased timespec
;
701 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
702 pragma Assert
(Result
= 0);
703 return To_Duration
(TS
);
710 function RT_Resolution
return Duration is
712 return 1.0 / Duration (sysClkRateGet
);
719 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
720 pragma Unreferenced
(Reason
);
723 Result
:= semGive
(T
.Common
.LL
.CV
);
724 pragma Assert
(Result
= 0);
731 procedure Yield
(Do_Yield
: Boolean := True) is
732 pragma Unreferenced
(Do_Yield
);
734 pragma Unreferenced
(Result
);
736 Result
:= taskDelay
(0);
743 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
744 pragma Atomic_Components
(Prio_Array_Type
);
746 Prio_Array
: Prio_Array_Type
;
747 -- Global array containing the id of the currently running task for
748 -- each priority. Note that we assume that we are on a single processor
749 -- with run-till-blocked scheduling.
751 procedure Set_Priority
753 Prio
: System
.Any_Priority
;
754 Loss_Of_Inheritance
: Boolean := False)
756 Array_Item
: Integer;
762 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
763 pragma Assert
(Result
= 0);
765 if (Dispatching_Policy
= 'F' or else Get_Policy
(Prio
) = 'F')
766 and then Loss_Of_Inheritance
767 and then Prio
< T
.Common
.Current_Priority
769 -- Annex D requirement (RM D.2.2(9))
771 -- If the task drops its priority due to the loss of inherited
772 -- priority, it is added at the head of the ready queue for its
773 -- new active priority.
775 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
776 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
779 -- Give some processes a chance to arrive
783 -- Then wait for our turn to proceed
785 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
786 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
789 Prio_Array
(T
.Common
.Base_Priority
) :=
790 Prio_Array
(T
.Common
.Base_Priority
) - 1;
793 T
.Common
.Current_Priority
:= Prio
;
800 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
802 return T
.Common
.Current_Priority
;
809 procedure Enter_Task
(Self_ID
: Task_Id
) is
810 procedure Init_Float
;
811 pragma Import
(C
, Init_Float
, "__gnat_init_float");
812 -- Properly initializes the FPU for PPC/MIPS systems
815 -- Store the user-level task id in the Thread field (to be used
816 -- internally by the run-time system) and the kernel-level task id in
817 -- the LWP field (to be used by the debugger).
819 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
820 Self_ID
.Common
.LL
.LWP
:= getpid
;
822 Specific
.Set
(Self_ID
);
826 -- Install the signal handlers
828 -- This is called for each task since there is no signal inheritance
829 -- between VxWorks tasks.
831 Install_Signal_Handlers
;
835 for J
in Known_Tasks
'Range loop
836 if Known_Tasks
(J
) = null then
837 Known_Tasks
(J
) := Self_ID
;
838 Self_ID
.Known_Tasks_Index
:= J
;
850 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
852 return new Ada_Task_Control_Block
(Entry_Num
);
859 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
861 -----------------------------
862 -- Register_Foreign_Thread --
863 -----------------------------
865 function Register_Foreign_Thread
return Task_Id
is
867 if Is_Valid_Task
then
870 return Register_Foreign_Thread
(taskIdSelf
);
872 end Register_Foreign_Thread
;
878 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
880 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
881 Self_ID
.Common
.LL
.Thread
:= 0;
883 if Self_ID
.Common
.LL
.CV
= 0 then
889 if not Single_Lock
then
890 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
899 procedure Create_Task
901 Wrapper
: System
.Address
;
902 Stack_Size
: System
.Parameters
.Size_Type
;
903 Priority
: System
.Any_Priority
;
904 Succeeded
: out Boolean)
906 Adjusted_Stack_Size
: size_t
;
908 -- Ask for four extra bytes of stack space so that the ATCB pointer can
909 -- be stored below the stack limit, plus extra space for the frame of
910 -- Task_Wrapper. This is so the user gets the amount of stack requested
911 -- exclusive of the needs.
913 -- We also have to allocate n more bytes for the task name storage and
914 -- enough space for the Wind Task Control Block which is around 0x778
915 -- bytes. VxWorks also seems to carve out additional space, so use 2048
916 -- as a nice round number. We might want to increment to the nearest
917 -- page size in case we ever support VxVMI.
919 -- ??? - we should come back and visit this so we can set the task name
920 -- to something appropriate.
922 Adjusted_Stack_Size
:= size_t
(Stack_Size
) + 2048;
924 -- Since the initial signal mask of a thread is inherited from the
925 -- creator, and the Environment task has all its signals masked, we do
926 -- not need to manipulate caller's signal mask at this point. All tasks
927 -- in RTS will have All_Tasks_Mask initially.
929 -- We now compute the VxWorks task name and options, then spawn ...
932 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
933 Name_Address
: System
.Address
;
934 -- Task name we are going to hand down to VxWorks
936 function Get_Task_Options
return int
;
937 pragma Import
(C
, Get_Task_Options
, "__gnat_get_task_options");
938 -- Function that returns the options to be set for the task that we
939 -- are creating. We fetch the options assigned to the current task,
940 -- so offering some user level control over the options for a task
941 -- hierarchy, and force VX_FP_TASK because it is almost always
945 -- If there is no Ada task name handy, let VxWorks choose one.
946 -- Otherwise, tell VxWorks what the Ada task name is.
948 if T
.Common
.Task_Image_Len
= 0 then
949 Name_Address
:= System
.Null_Address
;
951 Name
(1 .. Name
'Last - 1) :=
952 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
953 Name
(Name
'Last) := ASCII
.NUL
;
954 Name_Address
:= Name
'Address;
957 -- Now spawn the VxWorks task for real
959 T
.Common
.LL
.Thread
:=
962 To_VxWorks_Priority
(int
(Priority
)),
969 if T
.Common
.LL
.Thread
= -1 then
975 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
976 Set_Priority
(T
, Priority
);
983 procedure Finalize_TCB
(T
: Task_Id
) is
986 Is_Self
: constant Boolean := (T
= Self
);
988 procedure Free
is new
989 Ada
.Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
992 if not Single_Lock
then
993 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
994 pragma Assert
(Result
= 0);
997 T
.Common
.LL
.Thread
:= 0;
999 Result
:= semDelete
(T
.Common
.LL
.CV
);
1000 pragma Assert
(Result
= 0);
1002 if T
.Known_Tasks_Index
/= -1 then
1003 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1017 procedure Exit_Task
is
1019 Specific
.Set
(null);
1026 procedure Abort_Task
(T
: Task_Id
) is
1031 (T
.Common
.LL
.Thread
,
1032 Signal
(Interrupt_Management
.Abort_Task_Interrupt
));
1033 pragma Assert
(Result
= 0);
1040 procedure Initialize
(S
: in out Suspension_Object
) is
1042 -- Initialize internal state (always to False (RM D.10(6)))
1047 -- Initialize internal mutex
1049 -- Use simpler binary semaphore instead of VxWorks
1050 -- mutual exclusion semaphore, because we don't need
1051 -- the fancier semantics and their overhead.
1053 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1055 -- Initialize internal condition variable
1057 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1064 procedure Finalize
(S
: in out Suspension_Object
) is
1068 -- Destroy internal mutex
1070 Result
:= semDelete
(S
.L
);
1071 pragma Assert
(Result
= OK
);
1073 -- Destroy internal condition variable
1075 Result
:= semDelete
(S
.CV
);
1076 pragma Assert
(Result
= OK
);
1083 function Current_State
(S
: Suspension_Object
) return Boolean is
1085 -- We do not want to use lock on this read operation. State is marked
1086 -- as Atomic so that we ensure that the value retrieved is correct.
1095 procedure Set_False
(S
: in out Suspension_Object
) is
1099 SSL
.Abort_Defer
.all;
1101 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1102 pragma Assert
(Result
= OK
);
1106 Result
:= semGive
(S
.L
);
1107 pragma Assert
(Result
= OK
);
1109 SSL
.Abort_Undefer
.all;
1116 procedure Set_True
(S
: in out Suspension_Object
) is
1120 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 SSL
.Abort_Undefer
.all;
1146 ------------------------
1147 -- Suspend_Until_True --
1148 ------------------------
1150 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1154 SSL
.Abort_Defer
.all;
1156 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1160 -- Program_Error must be raised upon calling Suspend_Until_True
1161 -- if another task is already waiting on that suspension object
1162 -- (ARM D.10 par. 10).
1164 Result
:= semGive
(S
.L
);
1165 pragma Assert
(Result
= OK
);
1167 SSL
.Abort_Undefer
.all;
1169 raise Program_Error
;
1172 -- Suspend the task if the state is False. Otherwise, the task
1173 -- continues its execution, and the state of the suspension object
1174 -- is set to False (ARM D.10 par. 9).
1179 Result
:= semGive
(S
.L
);
1180 pragma Assert
(Result
= 0);
1182 SSL
.Abort_Undefer
.all;
1187 -- Release the mutex before sleeping
1189 Result
:= semGive
(S
.L
);
1190 pragma Assert
(Result
= OK
);
1192 SSL
.Abort_Undefer
.all;
1194 Result
:= semTake
(S
.CV
, WAIT_FOREVER
);
1195 pragma Assert
(Result
= 0);
1198 end Suspend_Until_True
;
1206 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1207 pragma Unreferenced
(Self_ID
);
1212 --------------------
1213 -- Check_No_Locks --
1214 --------------------
1216 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1217 pragma Unreferenced
(Self_ID
);
1222 ----------------------
1223 -- Environment_Task --
1224 ----------------------
1226 function Environment_Task
return Task_Id
is
1228 return Environment_Task_Id
;
1229 end Environment_Task
;
1235 procedure Lock_RTS
is
1237 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1244 procedure Unlock_RTS
is
1246 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1253 function Suspend_Task
1255 Thread_Self
: Thread_Id
) return Boolean
1258 if T
.Common
.LL
.Thread
/= 0
1259 and then T
.Common
.LL
.Thread
/= Thread_Self
1261 return taskSuspend
(T
.Common
.LL
.Thread
) = 0;
1271 function Resume_Task
1273 Thread_Self
: Thread_Id
) return Boolean
1276 if T
.Common
.LL
.Thread
/= 0
1277 and then T
.Common
.LL
.Thread
/= Thread_Self
1279 return taskResume
(T
.Common
.LL
.Thread
) = 0;
1285 --------------------
1286 -- Stop_All_Tasks --
1287 --------------------
1289 procedure Stop_All_Tasks
1291 Thread_Self
: constant Thread_Id
:= taskIdSelf
;
1295 pragma Unreferenced
(Dummy
);
1300 C
:= All_Tasks_List
;
1301 while C
/= null loop
1302 if C
.Common
.LL
.Thread
/= 0
1303 and then C
.Common
.LL
.Thread
/= Thread_Self
1305 Dummy
:= Task_Stop
(C
.Common
.LL
.Thread
);
1308 C
:= C
.Common
.All_Tasks_Link
;
1311 Dummy
:= Int_Unlock
;
1318 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1320 if T
.Common
.LL
.Thread
/= 0 then
1321 return Task_Stop
(T
.Common
.LL
.Thread
) = 0;
1331 function Continue_Task
(T
: ST
.Task_Id
) return Boolean
1334 if T
.Common
.LL
.Thread
/= 0 then
1335 return Task_Cont
(T
.Common
.LL
.Thread
) = 0;
1345 procedure Initialize
(Environment_Task
: Task_Id
) is
1349 Environment_Task_Id
:= Environment_Task
;
1351 Interrupt_Management
.Initialize
;
1352 Specific
.Initialize
;
1354 if Locking_Policy
= 'C' then
1355 Mutex_Protocol
:= Prio_Protect
;
1356 elsif Locking_Policy
= 'I' then
1357 Mutex_Protocol
:= Prio_Inherit
;
1359 Mutex_Protocol
:= Prio_None
;
1362 if Time_Slice_Val
> 0 then
1366 (Duration (Time_Slice_Val
) / Duration (1_000_000
.0
)));
1368 elsif Dispatching_Policy
= 'R' then
1369 Result
:= Set_Time_Slice
(To_Clock_Ticks
(0.01));
1373 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1374 pragma Assert
(Result
= 0);
1376 for J
in Interrupt_Management
.Signal_ID
loop
1377 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1378 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1379 pragma Assert
(Result
= 0);
1383 -- Initialize the lock used to synchronize chain of all ATCBs
1385 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1387 Enter_Task
(Environment_Task
);
1390 end System
.Task_Primitives
.Operations
;