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-2005, 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.
44 -- used for Ada_Task_Control_Block
46 -- ATCB components and types
48 with System
.Tasking
.Debug
;
49 -- used for Known_Tasks
51 with System
.Interrupt_Management
;
52 -- used for Keep_Unmasked
55 -- Initialize_Interrupts
57 with System
.OS_Interface
;
58 -- used for various type, constant, and operations
60 with System
.Parameters
;
65 with Unchecked_Conversion
;
66 with Unchecked_Deallocation
;
68 package body System
.Task_Primitives
.Operations
is
70 use System
.Tasking
.Debug
;
72 use System
.OS_Interface
;
73 use System
.Parameters
;
74 use type Interfaces
.C
.int
;
76 subtype int
is System
.OS_Interface
.int
;
78 Relative
: constant := 0;
84 -- The followings are logically constants, but need to be initialized at
87 Single_RTS_Lock
: aliased RTS_Lock
;
88 -- This is a lock to allow only one thread of control in the RTS at a
89 -- time; it is used to execute in mutual exclusion from all other tasks.
90 -- Used mainly in Single_Lock mode, but also to protect All_Tasks_List
92 Environment_Task_Id
: Task_Id
;
93 -- A variable to hold Task_Id for the environment task
95 Unblocked_Signal_Mask
: aliased sigset_t
;
96 -- The set of signals that should unblocked in all tasks
98 -- The followings are internal configuration constants needed
100 Time_Slice_Val
: Integer;
101 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
103 Locking_Policy
: Character;
104 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
106 Dispatching_Policy
: Character;
107 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
109 Mutex_Protocol
: Priority_Type
;
111 Foreign_Task_Elaborated
: aliased Boolean := True;
112 -- Used to identified fake tasks (i.e., non-Ada Threads)
120 procedure Initialize
;
121 pragma Inline
(Initialize
);
122 -- Initialize task specific data
124 function Is_Valid_Task
return Boolean;
125 pragma Inline
(Is_Valid_Task
);
126 -- Does executing thread have a TCB?
128 procedure Set
(Self_Id
: Task_Id
);
130 -- Set the self id for the current task
133 pragma Inline
(Delete
);
134 -- Delete the task specific data associated with the current task
136 function Self
return Task_Id
;
137 pragma Inline
(Self
);
138 -- Return a pointer to the Ada Task Control Block of the calling task
142 package body Specific
is separate;
143 -- The body of this package is target specific
145 ---------------------------------
146 -- Support for foreign threads --
147 ---------------------------------
149 function Register_Foreign_Thread
(Thread
: Thread_Id
) return Task_Id
;
150 -- Allocate and Initialize a new ATCB for the current Thread
152 function Register_Foreign_Thread
153 (Thread
: Thread_Id
) return Task_Id
is separate;
155 -----------------------
156 -- Local Subprograms --
157 -----------------------
159 procedure Abort_Handler
(signo
: Signal
);
160 -- Handler for the abort (SIGABRT) signal to handle asynchronous abort
162 procedure Install_Signal_Handlers
;
163 -- Install the default signal handlers for the current task
165 function To_Address
is new Unchecked_Conversion
(Task_Id
, System
.Address
);
171 procedure Abort_Handler
(signo
: Signal
) is
172 pragma Unreferenced
(signo
);
174 Self_ID
: constant Task_Id
:= Self
;
176 Old_Set
: aliased sigset_t
;
179 -- It is not safe to raise an exception when using ZCX and the GCC
180 -- exception handling mechanism.
182 if ZCX_By_Default
and then GCC_ZCX_Support
then
186 if Self_ID
.Deferral_Level
= 0
187 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
188 and then not Self_ID
.Aborting
190 Self_ID
.Aborting
:= True;
192 -- Make sure signals used for RTS internal purpose are unmasked
194 Result
:= pthread_sigmask
(SIG_UNBLOCK
,
195 Unblocked_Signal_Mask
'Unchecked_Access, Old_Set
'Unchecked_Access);
196 pragma Assert
(Result
= 0);
198 raise Standard
'Abort_Signal;
206 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
207 pragma Unreferenced
(T
);
208 pragma Unreferenced
(On
);
211 -- Nothing needed (why not???)
220 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
222 return T
.Common
.LL
.Thread
;
229 function Self
return Task_Id
renames Specific
.Self
;
231 -----------------------------
232 -- Install_Signal_Handlers --
233 -----------------------------
235 procedure Install_Signal_Handlers
is
236 act
: aliased struct_sigaction
;
237 old_act
: aliased struct_sigaction
;
238 Tmp_Set
: aliased sigset_t
;
243 act
.sa_handler
:= Abort_Handler
'Address;
245 Result
:= sigemptyset
(Tmp_Set
'Access);
246 pragma Assert
(Result
= 0);
247 act
.sa_mask
:= Tmp_Set
;
251 (Signal
(Interrupt_Management
.Abort_Task_Signal
),
252 act
'Unchecked_Access,
253 old_act
'Unchecked_Access);
254 pragma Assert
(Result
= 0);
256 Interrupt_Management
.Initialize_Interrupts
;
257 end Install_Signal_Handlers
;
259 ---------------------
260 -- Initialize_Lock --
261 ---------------------
263 procedure Initialize_Lock
(Prio
: System
.Any_Priority
; L
: access Lock
) is
265 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
266 L
.Prio_Ceiling
:= int
(Prio
);
267 L
.Protocol
:= Mutex_Protocol
;
268 pragma Assert
(L
.Mutex
/= 0);
271 procedure Initialize_Lock
(L
: access RTS_Lock
; Level
: Lock_Level
) is
272 pragma Unreferenced
(Level
);
275 L
.Mutex
:= semMCreate
(SEM_Q_PRIORITY
+ SEM_INVERSION_SAFE
);
276 L
.Prio_Ceiling
:= int
(System
.Any_Priority
'Last);
277 L
.Protocol
:= Mutex_Protocol
;
278 pragma Assert
(L
.Mutex
/= 0);
285 procedure Finalize_Lock
(L
: access Lock
) is
288 Result
:= semDelete
(L
.Mutex
);
289 pragma Assert
(Result
= 0);
292 procedure Finalize_Lock
(L
: access RTS_Lock
) is
295 Result
:= semDelete
(L
.Mutex
);
296 pragma Assert
(Result
= 0);
303 procedure Write_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
306 if L
.Protocol
= Prio_Protect
307 and then int
(Self
.Common
.Current_Priority
) > L
.Prio_Ceiling
309 Ceiling_Violation
:= True;
312 Ceiling_Violation
:= False;
315 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
316 pragma Assert
(Result
= 0);
320 (L
: access RTS_Lock
;
321 Global_Lock
: Boolean := False)
325 if not Single_Lock
or else Global_Lock
then
326 Result
:= semTake
(L
.Mutex
, WAIT_FOREVER
);
327 pragma Assert
(Result
= 0);
331 procedure Write_Lock
(T
: Task_Id
) is
334 if not Single_Lock
then
335 Result
:= semTake
(T
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
336 pragma Assert
(Result
= 0);
344 procedure Read_Lock
(L
: access Lock
; Ceiling_Violation
: out Boolean) is
346 Write_Lock
(L
, Ceiling_Violation
);
353 procedure Unlock
(L
: access Lock
) is
356 Result
:= semGive
(L
.Mutex
);
357 pragma Assert
(Result
= 0);
360 procedure Unlock
(L
: access RTS_Lock
; Global_Lock
: Boolean := False) is
363 if not Single_Lock
or else Global_Lock
then
364 Result
:= semGive
(L
.Mutex
);
365 pragma Assert
(Result
= 0);
369 procedure Unlock
(T
: Task_Id
) is
372 if not Single_Lock
then
373 Result
:= semGive
(T
.Common
.LL
.L
.Mutex
);
374 pragma Assert
(Result
= 0);
382 procedure Sleep
(Self_ID
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
383 pragma Unreferenced
(Reason
);
388 pragma Assert
(Self_ID
= Self
);
390 -- Release the mutex before sleeping
393 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
395 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
398 pragma Assert
(Result
= 0);
400 -- Perform a blocking operation to take the CV semaphore. Note that a
401 -- blocking operation in VxWorks will reenable task scheduling. When we
402 -- are no longer blocked and control is returned, task scheduling will
403 -- again be disabled.
405 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, WAIT_FOREVER
);
406 pragma Assert
(Result
= 0);
408 -- Take the mutex back
411 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
413 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
416 pragma Assert
(Result
= 0);
423 -- This is for use within the run-time system, so abort is assumed to be
424 -- already deferred, and the caller should be holding its own ATCB lock.
426 procedure Timed_Sleep
429 Mode
: ST
.Delay_Modes
;
430 Reason
: System
.Tasking
.Task_States
;
431 Timedout
: out Boolean;
432 Yielded
: out Boolean)
434 pragma Unreferenced
(Reason
);
436 Orig
: constant Duration := Monotonic_Clock
;
440 Wakeup
: Boolean := False;
446 if Mode
= Relative
then
447 Absolute
:= Orig
+ Time
;
449 -- Systematically add one since the first tick will delay *at most*
450 -- 1 / Rate_Duration seconds, so we need to add one to be on the
453 Ticks
:= To_Clock_Ticks
(Time
);
455 if Ticks
> 0 and then Ticks
< int
'Last then
461 Ticks
:= To_Clock_Ticks
(Time
- Monotonic_Clock
);
466 -- Release the mutex before sleeping
469 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
471 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
474 pragma Assert
(Result
= 0);
476 -- Perform a blocking operation to take the CV semaphore. Note
477 -- that a blocking operation in VxWorks will reenable task
478 -- scheduling. When we are no longer blocked and control is
479 -- returned, task scheduling will again be disabled.
481 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
485 -- Somebody may have called Wakeup for us
490 if errno
/= S_objLib_OBJ_TIMEOUT
then
494 -- If Ticks = int'last, it was most probably truncated so
495 -- let's make another round after recomputing Ticks from
496 -- the the absolute time.
498 if Ticks
/= int
'Last then
501 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
510 -- Take the mutex back
513 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
515 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
518 pragma Assert
(Result
= 0);
520 exit when Timedout
or Wakeup
;
526 -- Should never hold a lock while yielding
529 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
531 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
534 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
536 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
545 -- This is for use in implementing delay statements, so we assume the
546 -- caller is holding no locks.
548 procedure Timed_Delay
551 Mode
: ST
.Delay_Modes
)
553 Orig
: constant Duration := Monotonic_Clock
;
558 Aborted
: Boolean := False;
561 if Mode
= Relative
then
562 Absolute
:= Orig
+ Time
;
563 Ticks
:= To_Clock_Ticks
(Time
);
565 if Ticks
> 0 and then Ticks
< int
'Last then
567 -- First tick will delay anytime between 0 and 1 / sysClkRateGet
568 -- seconds, so we need to add one to be on the safe side.
575 Ticks
:= To_Clock_Ticks
(Time
- Orig
);
580 -- Modifying State and Pending_Priority_Change, locking the TCB
583 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
585 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
588 pragma Assert
(Result
= 0);
590 Self_ID
.Common
.State
:= Delay_Sleep
;
594 if Self_ID
.Pending_Priority_Change
then
595 Self_ID
.Pending_Priority_Change
:= False;
596 Self_ID
.Common
.Base_Priority
:= Self_ID
.New_Base_Priority
;
597 Set_Priority
(Self_ID
, Self_ID
.Common
.Base_Priority
);
600 Aborted
:= Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
602 -- Release the TCB before sleeping
605 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
607 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
609 pragma Assert
(Result
= 0);
613 Result
:= semTake
(Self_ID
.Common
.LL
.CV
, Ticks
);
617 -- If Ticks = int'last, it was most probably truncated
618 -- so let's make another round after recomputing Ticks
619 -- from the the absolute time.
621 if errno
= S_objLib_OBJ_TIMEOUT
and then Ticks
/= int
'Last then
624 Ticks
:= To_Clock_Ticks
(Absolute
- Monotonic_Clock
);
632 -- Take back the lock after having slept, to protect further
633 -- access to Self_ID.
636 Result
:= semTake
(Single_RTS_Lock
.Mutex
, WAIT_FOREVER
);
638 Result
:= semTake
(Self_ID
.Common
.LL
.L
.Mutex
, WAIT_FOREVER
);
641 pragma Assert
(Result
= 0);
646 Self_ID
.Common
.State
:= Runnable
;
649 Result
:= semGive
(Single_RTS_Lock
.Mutex
);
651 Result
:= semGive
(Self_ID
.Common
.LL
.L
.Mutex
);
659 ---------------------
660 -- Monotonic_Clock --
661 ---------------------
663 function Monotonic_Clock
return Duration is
664 TS
: aliased timespec
;
667 Result
:= clock_gettime
(CLOCK_REALTIME
, TS
'Unchecked_Access);
668 pragma Assert
(Result
= 0);
669 return To_Duration
(TS
);
676 function RT_Resolution
return Duration is
678 return 1.0 / Duration (sysClkRateGet
);
685 procedure Wakeup
(T
: Task_Id
; Reason
: System
.Tasking
.Task_States
) is
686 pragma Unreferenced
(Reason
);
689 Result
:= semGive
(T
.Common
.LL
.CV
);
690 pragma Assert
(Result
= 0);
697 procedure Yield
(Do_Yield
: Boolean := True) is
698 pragma Unreferenced
(Do_Yield
);
700 pragma Unreferenced
(Result
);
702 Result
:= taskDelay
(0);
709 type Prio_Array_Type
is array (System
.Any_Priority
) of Integer;
710 pragma Atomic_Components
(Prio_Array_Type
);
712 Prio_Array
: Prio_Array_Type
;
713 -- Global array containing the id of the currently running task for
714 -- each priority. Note that we assume that we are on a single processor
715 -- with run-till-blocked scheduling.
717 procedure Set_Priority
719 Prio
: System
.Any_Priority
;
720 Loss_Of_Inheritance
: Boolean := False)
722 Array_Item
: Integer;
728 (T
.Common
.LL
.Thread
, To_VxWorks_Priority
(int
(Prio
)));
729 pragma Assert
(Result
= 0);
731 if Dispatching_Policy
= 'F' then
733 -- Annex D requirement [RM D.2.2 par. 9]:
735 -- If the task drops its priority due to the loss of inherited
736 -- priority, it is added at the head of the ready queue for its
737 -- new active priority.
739 if Loss_Of_Inheritance
740 and then Prio
< T
.Common
.Current_Priority
742 Array_Item
:= Prio_Array
(T
.Common
.Base_Priority
) + 1;
743 Prio_Array
(T
.Common
.Base_Priority
) := Array_Item
;
746 -- Give some processes a chance to arrive
750 -- Then wait for our turn to proceed
752 exit when Array_Item
= Prio_Array
(T
.Common
.Base_Priority
)
753 or else Prio_Array
(T
.Common
.Base_Priority
) = 1;
756 Prio_Array
(T
.Common
.Base_Priority
) :=
757 Prio_Array
(T
.Common
.Base_Priority
) - 1;
761 T
.Common
.Current_Priority
:= Prio
;
768 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
770 return T
.Common
.Current_Priority
;
777 procedure Enter_Task
(Self_ID
: Task_Id
) is
778 procedure Init_Float
;
779 pragma Import
(C
, Init_Float
, "__gnat_init_float");
780 -- Properly initializes the FPU for PPC/MIPS systems
783 Self_ID
.Common
.LL
.Thread
:= taskIdSelf
;
784 Specific
.Set
(Self_ID
);
788 -- Install the signal handlers
790 -- This is called for each task since there is no signal inheritance
791 -- between VxWorks tasks.
793 Install_Signal_Handlers
;
797 for J
in Known_Tasks
'Range loop
798 if Known_Tasks
(J
) = null then
799 Known_Tasks
(J
) := Self_ID
;
800 Self_ID
.Known_Tasks_Index
:= J
;
812 function New_ATCB
(Entry_Num
: Task_Entry_Index
) return Task_Id
is
814 return new Ada_Task_Control_Block
(Entry_Num
);
821 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
823 -----------------------------
824 -- Register_Foreign_Thread --
825 -----------------------------
827 function Register_Foreign_Thread
return Task_Id
is
829 if Is_Valid_Task
then
832 return Register_Foreign_Thread
(taskIdSelf
);
834 end Register_Foreign_Thread
;
840 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
842 Self_ID
.Common
.LL
.CV
:= semBCreate
(SEM_Q_PRIORITY
, SEM_EMPTY
);
843 Self_ID
.Common
.LL
.Thread
:= 0;
845 if Self_ID
.Common
.LL
.CV
= 0 then
850 if not Single_Lock
then
851 Initialize_Lock
(Self_ID
.Common
.LL
.L
'Access, ATCB_Level
);
860 procedure Create_Task
862 Wrapper
: System
.Address
;
863 Stack_Size
: System
.Parameters
.Size_Type
;
864 Priority
: System
.Any_Priority
;
865 Succeeded
: out Boolean)
867 Adjusted_Stack_Size
: size_t
;
869 if Stack_Size
= Unspecified_Size
then
870 Adjusted_Stack_Size
:= size_t
(Default_Stack_Size
);
872 elsif Stack_Size
< Minimum_Stack_Size
then
873 Adjusted_Stack_Size
:= size_t
(Minimum_Stack_Size
);
876 Adjusted_Stack_Size
:= size_t
(Stack_Size
);
879 -- Ask for four extra bytes of stack space so that the ATCB pointer can
880 -- be stored below the stack limit, plus extra space for the frame of
881 -- Task_Wrapper. This is so the user gets the amount of stack requested
882 -- exclusive of the needs.
884 -- We also have to allocate n more bytes for the task name storage and
885 -- enough space for the Wind Task Control Block which is around 0x778
886 -- bytes. VxWorks also seems to carve out additional space, so use 2048
887 -- as a nice round number. We might want to increment to the nearest
888 -- page size in case we ever support VxVMI.
890 -- ??? - we should come back and visit this so we can set the task name
891 -- to something appropriate.
893 Adjusted_Stack_Size
:= Adjusted_Stack_Size
+ 2048;
895 -- Since the initial signal mask of a thread is inherited from the
896 -- creator, and the Environment task has all its signals masked, we do
897 -- not need to manipulate caller's signal mask at this point. All tasks
898 -- in RTS will have All_Tasks_Mask initially.
900 if T
.Common
.Task_Image_Len
= 0 then
901 T
.Common
.LL
.Thread
:= taskSpawn
902 (System
.Null_Address
,
903 To_VxWorks_Priority
(int
(Priority
)),
910 Name
: aliased String (1 .. T
.Common
.Task_Image_Len
+ 1);
913 Name
(1 .. Name
'Last - 1) :=
914 T
.Common
.Task_Image
(1 .. T
.Common
.Task_Image_Len
);
915 Name
(Name
'Last) := ASCII
.NUL
;
917 T
.Common
.LL
.Thread
:= taskSpawn
919 To_VxWorks_Priority
(int
(Priority
)),
927 if T
.Common
.LL
.Thread
= -1 then
933 Task_Creation_Hook
(T
.Common
.LL
.Thread
);
934 Set_Priority
(T
, Priority
);
941 procedure Finalize_TCB
(T
: Task_Id
) is
944 Is_Self
: constant Boolean := (T
= Self
);
946 procedure Free
is new
947 Unchecked_Deallocation
(Ada_Task_Control_Block
, Task_Id
);
950 if not Single_Lock
then
951 Result
:= semDelete
(T
.Common
.LL
.L
.Mutex
);
952 pragma Assert
(Result
= 0);
955 T
.Common
.LL
.Thread
:= 0;
957 Result
:= semDelete
(T
.Common
.LL
.CV
);
958 pragma Assert
(Result
= 0);
960 if T
.Known_Tasks_Index
/= -1 then
961 Known_Tasks
(T
.Known_Tasks_Index
) := null;
975 procedure Exit_Task
is
984 procedure Abort_Task
(T
: Task_Id
) is
987 Result
:= kill
(T
.Common
.LL
.Thread
,
988 Signal
(Interrupt_Management
.Abort_Task_Signal
));
989 pragma Assert
(Result
= 0);
996 procedure Initialize
(S
: in out Suspension_Object
) is
998 -- Initialize internal state. It is always initialized to False (ARM
1004 -- Initialize internal mutex
1006 -- Use simpler binary semaphore instead of VxWorks
1007 -- mutual exclusion semaphore, because we don't need
1008 -- the fancier semantics and their overhead.
1010 S
.L
:= semBCreate
(SEM_Q_FIFO
, SEM_FULL
);
1012 -- Initialize internal condition variable
1014 S
.CV
:= semBCreate
(SEM_Q_FIFO
, SEM_EMPTY
);
1021 procedure Finalize
(S
: in out Suspension_Object
) is
1024 -- Destroy internal mutex
1026 Result
:= semDelete
(S
.L
);
1027 pragma Assert
(Result
= OK
);
1029 -- Destroy internal condition variable
1031 Result
:= semDelete
(S
.CV
);
1032 pragma Assert
(Result
= OK
);
1039 function Current_State
(S
: Suspension_Object
) return Boolean is
1041 -- We do not want to use lock on this read operation. State is marked
1042 -- as Atomic so that we ensure that the value retrieved is correct.
1051 procedure Set_False
(S
: in out Suspension_Object
) is
1054 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1055 pragma Assert
(Result
= OK
);
1059 Result
:= semGive
(S
.L
);
1060 pragma Assert
(Result
= OK
);
1067 procedure Set_True
(S
: in out Suspension_Object
) is
1070 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1071 pragma Assert
(Result
= OK
);
1073 -- If there is already a task waiting on this suspension object then
1074 -- we resume it, leaving the state of the suspension object to False,
1075 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1076 -- the state to True.
1082 Result
:= semGive
(S
.CV
);
1083 pragma Assert
(Result
= OK
);
1088 Result
:= semGive
(S
.L
);
1089 pragma Assert
(Result
= OK
);
1092 ------------------------
1093 -- Suspend_Until_True --
1094 ------------------------
1096 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1099 Result
:= semTake
(S
.L
, WAIT_FOREVER
);
1102 -- Program_Error must be raised upon calling Suspend_Until_True
1103 -- if another task is already waiting on that suspension object
1104 -- (ARM D.10 par. 10).
1106 Result
:= semGive
(S
.L
);
1107 pragma Assert
(Result
= OK
);
1109 raise Program_Error
;
1111 -- Suspend the task if the state is False. Otherwise, the task
1112 -- continues its execution, and the state of the suspension object
1113 -- is set to False (ARM D.10 par. 9).
1118 Result
:= semGive
(S
.L
);
1119 pragma Assert
(Result
= 0);
1123 -- Release the mutex before sleeping
1125 Result
:= semGive
(S
.L
);
1126 pragma Assert
(Result
= OK
);
1128 Result
:= semTake
(S
.CV
, WAIT_FOREVER
);
1129 pragma Assert
(Result
= 0);
1132 end Suspend_Until_True
;
1140 function Check_Exit
(Self_ID
: ST
.Task_Id
) return Boolean is
1141 pragma Unreferenced
(Self_ID
);
1146 --------------------
1147 -- Check_No_Locks --
1148 --------------------
1150 function Check_No_Locks
(Self_ID
: ST
.Task_Id
) return Boolean is
1151 pragma Unreferenced
(Self_ID
);
1156 ----------------------
1157 -- Environment_Task --
1158 ----------------------
1160 function Environment_Task
return Task_Id
is
1162 return Environment_Task_Id
;
1163 end Environment_Task
;
1169 procedure Lock_RTS
is
1171 Write_Lock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1178 procedure Unlock_RTS
is
1180 Unlock
(Single_RTS_Lock
'Access, Global_Lock
=> True);
1187 function Suspend_Task
1189 Thread_Self
: Thread_Id
) return Boolean
1192 if T
.Common
.LL
.Thread
/= 0
1193 and then T
.Common
.LL
.Thread
/= Thread_Self
1195 return taskSuspend
(T
.Common
.LL
.Thread
) = 0;
1205 function Resume_Task
1207 Thread_Self
: Thread_Id
) return Boolean
1210 if T
.Common
.LL
.Thread
/= 0
1211 and then T
.Common
.LL
.Thread
/= Thread_Self
1213 return taskResume
(T
.Common
.LL
.Thread
) = 0;
1223 procedure Initialize
(Environment_Task
: Task_Id
) is
1226 Environment_Task_Id
:= Environment_Task
;
1228 Interrupt_Management
.Initialize
;
1229 Specific
.Initialize
;
1231 if Locking_Policy
= 'C' then
1232 Mutex_Protocol
:= Prio_Protect
;
1233 elsif Locking_Policy
= 'I' then
1234 Mutex_Protocol
:= Prio_Inherit
;
1236 Mutex_Protocol
:= Prio_None
;
1239 if Time_Slice_Val
> 0 then
1240 Result
:= Set_Time_Slice
1242 (Duration (Time_Slice_Val
) / Duration (1_000_000
.0
)));
1245 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
1246 pragma Assert
(Result
= 0);
1248 for J
in Interrupt_Management
.Signal_ID
loop
1249 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
1250 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
1251 pragma Assert
(Result
= 0);
1255 -- Initialize the lock used to synchronize chain of all ATCBs
1257 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
1259 Enter_Task
(Environment_Task
);
1262 end System
.Task_Primitives
.Operations
;