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-2024, Free Software Foundation, Inc. --
11 -- GNARL is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
32 -- This is a Solaris (native) version of this package
34 -- This package contains all the GNULL primitives that interface directly with
39 with System
.Multiprocessors
;
40 with System
.Tasking
.Debug
;
41 with System
.Interrupt_Management
;
42 with System
.OS_Constants
;
43 with System
.OS_Primitives
;
44 with System
.Task_Info
;
46 pragma Warnings
(Off
);
50 with System
.Soft_Links
;
51 -- We use System.Soft_Links instead of System.Tasking.Initialization
52 -- because the later is a higher level package that we shouldn't depend on.
53 -- For example when using the restricted run time, it is replaced by
54 -- System.Tasking.Restricted.Stages.
56 package body System
.Task_Primitives
.Operations
is
58 package OSC
renames System
.OS_Constants
;
59 package SSL
renames System
.Soft_Links
;
61 use System
.Tasking
.Debug
;
64 use System
.OS_Interface
;
65 use System
.Parameters
;
66 use System
.OS_Primitives
;
72 -- The following are logically constants, but need to be initialized
75 Environment_Task_Id
: Task_Id
;
76 -- A variable to hold Task_Id for the environment task.
77 -- If we use this variable to get the Task_Id, we need the following
78 -- ATCB_Key only for non-Ada threads.
80 Unblocked_Signal_Mask
: aliased sigset_t
;
81 -- The set of signals that should unblocked in all tasks
83 ATCB_Key
: aliased thread_key_t
;
84 -- Key used to find the Ada Task_Id associated with a thread,
85 -- at least for C threads unknown to the Ada run-time system.
87 Single_RTS_Lock
: aliased RTS_Lock
;
88 -- This is a lock to allow only one thread of control in the RTS at
89 -- a time; it is used to execute in mutual exclusion from all other tasks.
90 -- Used to protect All_Tasks_List
92 Next_Serial_Number
: Task_Serial_Number
:= 100;
93 -- We start at 100, to reserve some special values for
94 -- using in error checking.
95 -- The following are internal configuration constants needed.
97 Abort_Handler_Installed
: Boolean := False;
98 -- True if a handler for the abort signal is installed
100 Null_Thread_Id
: constant Thread_Id
:= Thread_Id
'Last;
101 -- Constant to indicate that the thread identifier has not yet been
104 ----------------------
105 -- Priority Support --
106 ----------------------
108 Priority_Ceiling_Emulation
: constant Boolean := True;
109 -- controls whether we emulate priority ceiling locking
111 -- To get a scheduling close to annex D requirements, we use the real-time
112 -- class provided for LWPs and map each task/thread to a specific and
113 -- unique LWP (there is 1 thread per LWP, and 1 LWP per thread).
115 -- The real time class can only be set when the process has root
116 -- privileges, so in the other cases, we use the normal thread scheduling
117 -- and priority handling.
119 Using_Real_Time_Class
: Boolean := False;
120 -- indicates whether the real time class is being used (i.e. the process
121 -- has root privileges).
123 Prio_Param
: aliased struct_pcparms
;
124 -- Hold priority info (Real_Time) initialized during the package
127 -----------------------------------
128 -- External Configuration Values --
129 -----------------------------------
131 Time_Slice_Val
: constant Integer;
132 pragma Import
(C
, Time_Slice_Val
, "__gl_time_slice_val");
134 Locking_Policy
: constant Character;
135 pragma Import
(C
, Locking_Policy
, "__gl_locking_policy");
137 Dispatching_Policy
: constant Character;
138 pragma Import
(C
, Dispatching_Policy
, "__gl_task_dispatching_policy");
140 Foreign_Task_Elaborated
: aliased Boolean := True;
141 -- Used to identified fake tasks (i.e., non-Ada Threads)
143 -----------------------
144 -- Local Subprograms --
145 -----------------------
147 function sysconf
(name
: System
.OS_Interface
.int
) return processorid_t
;
148 pragma Import
(C
, sysconf
, "sysconf");
150 SC_NPROCESSORS_CONF
: constant System
.OS_Interface
.int
:= 14;
153 (name
: System
.OS_Interface
.int
:= SC_NPROCESSORS_CONF
)
154 return processorid_t
renames sysconf
;
156 procedure Abort_Handler
158 Code
: not null access siginfo_t
;
159 Context
: not null access ucontext_t
);
160 -- Target-dependent binding of inter-thread Abort signal to
161 -- the raising of the Abort_Signal exception.
162 -- See also comments in 7staprop.adb
168 function Check_Initialize_Lock
170 Level
: Lock_Level
) return Boolean;
171 pragma Inline
(Check_Initialize_Lock
);
173 function Check_Lock
(L
: Lock_Ptr
) return Boolean;
174 pragma Inline
(Check_Lock
);
176 function Record_Lock
(L
: Lock_Ptr
) return Boolean;
177 pragma Inline
(Record_Lock
);
179 function Check_Sleep
(Reason
: Task_States
) return Boolean;
180 pragma Inline
(Check_Sleep
);
182 function Record_Wakeup
184 Reason
: Task_States
) return Boolean;
185 pragma Inline
(Record_Wakeup
);
187 function Check_Wakeup
189 Reason
: Task_States
) return Boolean;
190 pragma Inline
(Check_Wakeup
);
192 function Check_Unlock
(L
: Lock_Ptr
) return Boolean;
193 pragma Inline
(Check_Unlock
);
195 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean;
196 pragma Inline
(Check_Finalize_Lock
);
204 procedure Initialize
(Environment_Task
: Task_Id
);
205 pragma Inline
(Initialize
);
206 -- Initialize various data needed by this package
208 function Is_Valid_Task
return Boolean;
209 pragma Inline
(Is_Valid_Task
);
210 -- Does executing thread have a TCB?
212 procedure Set
(Self_Id
: Task_Id
);
214 -- Set the self id for the current task
216 function Self
return Task_Id
;
217 pragma Inline
(Self
);
218 -- Return a pointer to the Ada Task Control Block of the calling task
222 package body Specific
is separate;
223 -- The body of this package is target specific
225 ----------------------------------
226 -- ATCB allocation/deallocation --
227 ----------------------------------
229 package body ATCB_Allocation
is separate;
230 -- The body of this package is shared across several targets
232 ---------------------------------
233 -- Support for foreign threads --
234 ---------------------------------
236 function Register_Foreign_Thread
238 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
) return Task_Id
;
239 -- Allocate and initialize a new ATCB for the current Thread. The size of
240 -- the secondary stack can be optionally specified.
242 function Register_Foreign_Thread
244 Sec_Stack_Size
: Size_Type
:= Unspecified_Size
)
245 return Task_Id
is separate;
251 Check_Count
: Integer := 0;
252 Lock_Count
: Integer := 0;
253 Unlock_Count
: Integer := 0;
259 procedure Abort_Handler
261 Code
: not null access siginfo_t
;
262 Context
: not null access ucontext_t
)
264 pragma Unreferenced
(Sig
);
265 pragma Unreferenced
(Code
);
266 pragma Unreferenced
(Context
);
268 Self_ID
: constant Task_Id
:= Self
;
269 Old_Set
: aliased sigset_t
;
271 Result
: Interfaces
.C
.int
;
272 pragma Warnings
(Off
, Result
);
275 -- It's not safe to raise an exception when using GCC ZCX mechanism.
276 -- Note that we still need to install a signal handler, since in some
277 -- cases (e.g. shutdown of the Server_Task in System.Interrupts) we
278 -- need to send the Abort signal to a task.
280 if ZCX_By_Default
then
284 if Self_ID
.Deferral_Level
= 0
285 and then Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
286 and then not Self_ID
.Aborting
288 Self_ID
.Aborting
:= True;
290 -- Make sure signals used for RTS internal purpose are unmasked
295 Unblocked_Signal_Mask
'Unchecked_Access,
296 Old_Set
'Unchecked_Access);
297 pragma Assert
(Result
= 0);
299 raise Standard
'Abort_Signal;
307 -- The underlying thread system sets a guard page at the
308 -- bottom of a thread stack, so nothing is needed.
310 procedure Stack_Guard
(T
: ST
.Task_Id
; On
: Boolean) is
311 pragma Unreferenced
(T
);
312 pragma Unreferenced
(On
);
321 function Get_Thread_Id
(T
: ST
.Task_Id
) return OSI
.Thread_Id
is
323 return T
.Common
.LL
.Thread
;
330 procedure Initialize
(Environment_Task
: ST
.Task_Id
) is
331 act
: aliased struct_sigaction
;
332 old_act
: aliased struct_sigaction
;
333 Tmp_Set
: aliased sigset_t
;
334 Result
: Interfaces
.C
.int
;
336 procedure Configure_Processors
;
337 -- Processors configuration
338 -- The user can specify a processor which the program should run
339 -- on to emulate a single-processor system. This can be easily
340 -- done by setting environment variable GNAT_PROCESSOR to one of
343 -- -2 : use the default configuration (run the program on all
344 -- available processors) - this is the same as having
345 -- GNAT_PROCESSOR unset
346 -- -1 : let the RTS choose one processor and run the program on
348 -- 0 .. Last_Proc : run the program on the specified processor
350 -- Last_Proc is equal to the value of the system variable
351 -- _SC_NPROCESSORS_CONF, minus one.
353 procedure Configure_Processors
is
354 Proc_Acc
: constant System
.OS_Lib
.String_Access
:=
355 System
.OS_Lib
.Getenv
("GNAT_PROCESSOR");
356 Proc
: aliased processorid_t
; -- User processor #
357 Last_Proc
: processorid_t
; -- Last processor #
360 if Proc_Acc
.all'Length /= 0 then
362 -- Environment variable is defined
364 Last_Proc
:= Num_Procs
- 1;
366 if Last_Proc
/= -1 then
367 Proc
:= processorid_t
'Value (Proc_Acc
.all);
369 if Proc
<= -2 or else Proc
> Last_Proc
then
371 -- Use the default configuration
377 -- Choose a processor
380 while Proc
< Last_Proc
loop
382 Result
:= p_online
(Proc
, PR_STATUS
);
383 exit when Result
= PR_ONLINE
;
386 pragma Assert
(Result
= PR_ONLINE
);
387 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
388 pragma Assert
(Result
= 0);
391 -- Use user processor
393 Result
:= processor_bind
(P_PID
, P_MYID
, Proc
, null);
394 pragma Assert
(Result
= 0);
400 when Constraint_Error
=>
402 -- Illegal environment variable GNAT_PROCESSOR - ignored
405 end Configure_Processors
;
408 (Int
: System
.Interrupt_Management
.Interrupt_ID
) return Character;
409 pragma Import
(C
, State
, "__gnat_get_interrupt_state");
410 -- Get interrupt state. Defined in a-init.c
411 -- The input argument is the interrupt number,
412 -- and the result is one of the following:
414 Default
: constant Character := 's';
415 -- 'n' this interrupt not set by any Interrupt_State pragma
416 -- 'u' Interrupt_State pragma set state to User
417 -- 'r' Interrupt_State pragma set state to Runtime
418 -- 's' Interrupt_State pragma set state to System (use "default"
421 -- Start of processing for Initialize
424 Environment_Task_Id
:= Environment_Task
;
426 Interrupt_Management
.Initialize
;
428 -- Prepare the set of signals that should unblocked in all tasks
430 Result
:= sigemptyset
(Unblocked_Signal_Mask
'Access);
431 pragma Assert
(Result
= 0);
433 for J
in Interrupt_Management
.Interrupt_ID
loop
434 if System
.Interrupt_Management
.Keep_Unmasked
(J
) then
435 Result
:= sigaddset
(Unblocked_Signal_Mask
'Access, Signal
(J
));
436 pragma Assert
(Result
= 0);
440 if Dispatching_Policy
= 'F' then
442 Result
: Interfaces
.C
.long
;
443 Class_Info
: aliased struct_pcinfo
;
444 Secs
, Nsecs
: Interfaces
.C
.long
;
447 -- If a pragma Time_Slice is specified, takes the value in account
449 if Time_Slice_Val
> 0 then
451 -- Convert Time_Slice_Val (microseconds) to seconds/nanosecs
453 Secs
:= Interfaces
.C
.long
(Time_Slice_Val
/ 1_000_000
);
455 Interfaces
.C
.long
((Time_Slice_Val
rem 1_000_000
) * 1_000
);
457 -- Otherwise, default to no time slicing (i.e run until blocked)
464 -- Get the real time class id
466 Class_Info
.pc_clname
(1) := 'R';
467 Class_Info
.pc_clname
(2) := 'T';
468 Class_Info
.pc_clname
(3) := ASCII
.NUL
;
470 Result
:= priocntl
(PC_VERSION
, P_LWPID
, P_MYID
, PC_GETCID
,
473 -- Request the real time class
475 Prio_Param
.pc_cid
:= Class_Info
.pc_cid
;
476 Prio_Param
.rt_pri
:= pri_t
(Class_Info
.rt_maxpri
);
477 Prio_Param
.rt_tqsecs
:= Secs
;
478 Prio_Param
.rt_tqnsecs
:= Nsecs
;
482 (PC_VERSION
, P_LWPID
, P_MYID
, PC_SETPARMS
, Prio_Param
'Address);
484 Using_Real_Time_Class
:= Result
/= -1;
488 Specific
.Initialize
(Environment_Task
);
490 -- The following is done in Enter_Task, but this is too late for the
491 -- Environment Task, since we need to call Self in Check_Locks when
492 -- the run time is compiled with assertions on.
494 Specific
.Set
(Environment_Task
);
496 -- Initialize the lock used to synchronize chain of all ATCBs
498 Initialize_Lock
(Single_RTS_Lock
'Access, RTS_Lock_Level
);
500 -- Make environment task known here because it doesn't go through
501 -- Activate_Tasks, which does it for all other tasks.
503 Known_Tasks
(Known_Tasks
'First) := Environment_Task
;
504 Environment_Task
.Known_Tasks_Index
:= Known_Tasks
'First;
506 Enter_Task
(Environment_Task
);
508 Configure_Processors
;
511 (System
.Interrupt_Management
.Abort_Task_Interrupt
) /= Default
513 -- Set sa_flags to SA_NODEFER so that during the handler execution
514 -- we do not change the Signal_Mask to be masked for the Abort_Signal
515 -- This is a temporary fix to the problem that the Signal_Mask is
516 -- not restored after the exception (longjmp) from the handler.
517 -- The right fix should be made in sigsetjmp so that we save
518 -- the Signal_Set and restore it after a longjmp.
519 -- In that case, this field should be changed back to 0. ???
523 act
.sa_handler
:= Abort_Handler
'Address;
524 Result
:= sigemptyset
(Tmp_Set
'Access);
525 pragma Assert
(Result
= 0);
526 act
.sa_mask
:= Tmp_Set
;
530 (Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
),
531 act
'Unchecked_Access,
532 old_act
'Unchecked_Access);
533 pragma Assert
(Result
= 0);
534 Abort_Handler_Installed
:= True;
538 ---------------------
539 -- Initialize_Lock --
540 ---------------------
542 -- Note: mutexes and cond_variables needed per-task basis are initialized
543 -- in Initialize_TCB and the Storage_Error is handled. Other mutexes (such
544 -- as RTS_Lock, Memory_Lock...) used in RTS is initialized before any
545 -- status change of RTS. Therefore raising Storage_Error in the following
546 -- routines should be able to be handled safely.
548 procedure Initialize_Lock
549 (Prio
: System
.Any_Priority
;
550 L
: not null access Lock
)
552 Result
: Interfaces
.C
.int
;
555 pragma Assert
(Check_Initialize_Lock
(Lock_Ptr
(L
), PO_Level
));
557 if Priority_Ceiling_Emulation
then
561 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
562 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
564 if Result
= ENOMEM
then
565 raise Storage_Error
with "Failed to allocate a lock";
569 procedure Initialize_Lock
570 (L
: not null access RTS_Lock
;
573 Result
: Interfaces
.C
.int
;
577 (Check_Initialize_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
)), Level
));
578 Result
:= mutex_init
(L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
579 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
581 if Result
= ENOMEM
then
582 raise Storage_Error
with "Failed to allocate a lock";
590 procedure Finalize_Lock
(L
: not null access Lock
) is
591 Result
: Interfaces
.C
.int
;
593 pragma Assert
(Check_Finalize_Lock
(Lock_Ptr
(L
)));
594 Result
:= mutex_destroy
(L
.L
'Access);
595 pragma Assert
(Result
= 0);
598 procedure Finalize_Lock
(L
: not null access RTS_Lock
) is
599 Result
: Interfaces
.C
.int
;
601 pragma Assert
(Check_Finalize_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
602 Result
:= mutex_destroy
(L
.L
'Access);
603 pragma Assert
(Result
= 0);
611 (L
: not null access Lock
;
612 Ceiling_Violation
: out Boolean)
614 Result
: Interfaces
.C
.int
;
617 pragma Assert
(Check_Lock
(Lock_Ptr
(L
)));
619 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
621 Self_Id
: constant Task_Id
:= Self
;
622 Saved_Priority
: System
.Any_Priority
;
625 if Self_Id
.Common
.LL
.Active_Priority
> L
.Ceiling
then
626 Ceiling_Violation
:= True;
630 Saved_Priority
:= Self_Id
.Common
.LL
.Active_Priority
;
632 if Self_Id
.Common
.LL
.Active_Priority
< L
.Ceiling
then
633 Set_Priority
(Self_Id
, L
.Ceiling
);
636 Result
:= mutex_lock
(L
.L
'Access);
637 pragma Assert
(Result
= 0);
638 Ceiling_Violation
:= False;
640 L
.Saved_Priority
:= Saved_Priority
;
644 Result
:= mutex_lock
(L
.L
'Access);
645 pragma Assert
(Result
= 0);
646 Ceiling_Violation
:= False;
649 pragma Assert
(Record_Lock
(Lock_Ptr
(L
)));
652 procedure Write_Lock
(L
: not null access RTS_Lock
) is
653 Result
: Interfaces
.C
.int
;
655 pragma Assert
(Check_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
656 Result
:= mutex_lock
(L
.L
'Access);
657 pragma Assert
(Result
= 0);
658 pragma Assert
(Record_Lock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
661 procedure Write_Lock
(T
: Task_Id
) is
662 Result
: Interfaces
.C
.int
;
664 pragma Assert
(Check_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
665 Result
:= mutex_lock
(T
.Common
.LL
.L
.L
'Access);
666 pragma Assert
(Result
= 0);
667 pragma Assert
(Record_Lock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
675 (L
: not null access Lock
;
676 Ceiling_Violation
: out Boolean) is
678 Write_Lock
(L
, Ceiling_Violation
);
685 procedure Unlock
(L
: not null access Lock
) is
686 Result
: Interfaces
.C
.int
;
689 pragma Assert
(Check_Unlock
(Lock_Ptr
(L
)));
691 if Priority_Ceiling_Emulation
and then Locking_Policy
= 'C' then
693 Self_Id
: constant Task_Id
:= Self
;
696 Result
:= mutex_unlock
(L
.L
'Access);
697 pragma Assert
(Result
= 0);
699 if Self_Id
.Common
.LL
.Active_Priority
> L
.Saved_Priority
then
700 Set_Priority
(Self_Id
, L
.Saved_Priority
);
704 Result
:= mutex_unlock
(L
.L
'Access);
705 pragma Assert
(Result
= 0);
709 procedure Unlock
(L
: not null access RTS_Lock
) is
710 Result
: Interfaces
.C
.int
;
712 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(RTS_Lock_Ptr
(L
))));
713 Result
:= mutex_unlock
(L
.L
'Access);
714 pragma Assert
(Result
= 0);
717 procedure Unlock
(T
: Task_Id
) is
718 Result
: Interfaces
.C
.int
;
720 pragma Assert
(Check_Unlock
(To_Lock_Ptr
(T
.Common
.LL
.L
'Access)));
721 Result
:= mutex_unlock
(T
.Common
.LL
.L
.L
'Access);
722 pragma Assert
(Result
= 0);
729 -- Dynamic priority ceilings are not supported by the underlying system
731 procedure Set_Ceiling
732 (L
: not null access Lock
;
733 Prio
: System
.Any_Priority
)
735 pragma Unreferenced
(L
, Prio
);
740 -- For the time delay implementation, we need to make sure we
741 -- achieve following criteria:
743 -- 1) We have to delay at least for the amount requested.
744 -- 2) We have to give up CPU even though the actual delay does not
745 -- result in blocking.
746 -- 3) Except for restricted run-time systems that do not support
747 -- ATC or task abort, the delay must be interrupted by the
748 -- abort_task operation.
749 -- 4) The implementation has to be efficient so that the delay overhead
750 -- is relatively cheap.
751 -- (1)-(3) are Ada requirements. Even though (2) is an Annex-D
752 -- requirement we still want to provide the effect in all cases.
753 -- The reason is that users may want to use short delays to implement
754 -- their own scheduling effect in the absence of language provided
755 -- scheduling policies.
757 ---------------------
758 -- Monotonic_Clock --
759 ---------------------
761 function Monotonic_Clock
return Duration is
762 TS
: aliased timespec
;
763 Result
: Interfaces
.C
.int
;
765 Result
:= clock_gettime
(OSC
.CLOCK_RT_Ada
, TS
'Unchecked_Access);
766 pragma Assert
(Result
= 0);
767 return To_Duration
(TS
);
774 function RT_Resolution
return Duration is
775 TS
: aliased timespec
;
776 Result
: Interfaces
.C
.int
;
778 Result
:= clock_getres
(OSC
.CLOCK_REALTIME
, TS
'Unchecked_Access);
779 pragma Assert
(Result
= 0);
781 return To_Duration
(TS
);
788 procedure Yield
(Do_Yield
: Boolean := True) is
791 System
.OS_Interface
.thr_yield
;
799 function Self
return Task_Id
renames Specific
.Self
;
805 procedure Set_Priority
807 Prio
: System
.Any_Priority
;
808 Loss_Of_Inheritance
: Boolean := False)
810 pragma Unreferenced
(Loss_Of_Inheritance
);
812 Result
: Interfaces
.C
.int
;
813 pragma Unreferenced
(Result
);
815 Param
: aliased struct_pcparms
;
820 T
.Common
.Current_Priority
:= Prio
;
822 if Priority_Ceiling_Emulation
then
823 T
.Common
.LL
.Active_Priority
:= Prio
;
826 if Using_Real_Time_Class
then
827 Param
.pc_cid
:= Prio_Param
.pc_cid
;
828 Param
.rt_pri
:= pri_t
(Prio
);
829 Param
.rt_tqsecs
:= Prio_Param
.rt_tqsecs
;
830 Param
.rt_tqnsecs
:= Prio_Param
.rt_tqnsecs
;
832 Result
:= Interfaces
.C
.int
(
833 priocntl
(PC_VERSION
, P_LWPID
, T
.Common
.LL
.LWP
, PC_SETPARMS
,
837 if T
.Common
.Task_Info
/= null
838 and then not T
.Common
.Task_Info
.Bound_To_LWP
840 -- The task is not bound to a LWP, so use thr_setprio
843 thr_setprio
(T
.Common
.LL
.Thread
, Interfaces
.C
.int
(Prio
));
846 -- The task is bound to a LWP, use priocntl
858 function Get_Priority
(T
: Task_Id
) return System
.Any_Priority
is
860 return T
.Common
.Current_Priority
;
867 procedure Enter_Task
(Self_ID
: Task_Id
) is
869 Self_ID
.Common
.LL
.Thread
:= thr_self
;
870 Self_ID
.Common
.LL
.LWP
:= lwp_self
;
872 Set_Task_Affinity
(Self_ID
);
873 Specific
.Set
(Self_ID
);
875 -- We need the above code even if we do direct fetch of Task_Id in Self
876 -- for the main task on Sun, x86 Solaris and for gcc 2.7.2.
883 function Is_Valid_Task
return Boolean renames Specific
.Is_Valid_Task
;
885 -----------------------------
886 -- Register_Foreign_Thread --
887 -----------------------------
889 function Register_Foreign_Thread
return Task_Id
is
891 if Is_Valid_Task
then
894 return Register_Foreign_Thread
(thr_self
);
896 end Register_Foreign_Thread
;
902 procedure Initialize_TCB
(Self_ID
: Task_Id
; Succeeded
: out Boolean) is
903 Result
: Interfaces
.C
.int
:= 0;
906 -- Give the task a unique serial number
908 Self_ID
.Serial_Number
:= Next_Serial_Number
;
909 Next_Serial_Number
:= Next_Serial_Number
+ 1;
910 pragma Assert
(Next_Serial_Number
/= 0);
912 Self_ID
.Common
.LL
.Thread
:= Null_Thread_Id
;
916 (Self_ID
.Common
.LL
.L
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
917 Self_ID
.Common
.LL
.L
.Level
:=
918 Private_Task_Serial_Number
(Self_ID
.Serial_Number
);
919 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
922 Result
:= cond_init
(Self_ID
.Common
.LL
.CV
'Access, USYNC_THREAD
, 0);
923 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
929 Result
:= mutex_destroy
(Self_ID
.Common
.LL
.L
.L
'Access);
930 pragma Assert
(Result
= 0);
940 procedure Create_Task
942 Wrapper
: System
.Address
;
943 Stack_Size
: System
.Parameters
.Size_Type
;
944 Priority
: System
.Any_Priority
;
945 Succeeded
: out Boolean)
947 pragma Unreferenced
(Priority
);
949 Result
: Interfaces
.C
.int
;
950 Adjusted_Stack_Size
: Interfaces
.C
.size_t
;
951 Opts
: Interfaces
.C
.int
:= THR_DETACHED
;
953 Page_Size
: constant System
.Parameters
.Size_Type
:= 4096;
954 -- This constant is for reserving extra space at the
955 -- end of the stack, which can be used by the stack
956 -- checking as guard page. The idea is that we need
957 -- to have at least Stack_Size bytes available for
960 use System
.Task_Info
;
961 use type System
.Multiprocessors
.CPU_Range
;
964 -- Check whether both Dispatching_Domain and CPU are specified for the
965 -- task, and the CPU value is not contained within the range of
966 -- processors for the domain.
968 if T
.Common
.Domain
/= null
969 and then T
.Common
.Base_CPU
/= System
.Multiprocessors
.Not_A_Specific_CPU
971 (T
.Common
.Base_CPU
not in T
.Common
.Domain
'Range
972 or else not T
.Common
.Domain
(T
.Common
.Base_CPU
))
978 Adjusted_Stack_Size
:= Interfaces
.C
.size_t
(Stack_Size
+ Page_Size
);
980 -- Since the initial signal mask of a thread is inherited from the
981 -- creator, and the Environment task has all its signals masked, we
982 -- do not need to manipulate caller's signal mask at this point.
983 -- All tasks in RTS will have All_Tasks_Mask initially.
985 if T
.Common
.Task_Info
/= null then
986 if T
.Common
.Task_Info
.New_LWP
then
987 Opts
:= Opts
+ THR_NEW_LWP
;
990 if T
.Common
.Task_Info
.Bound_To_LWP
then
991 Opts
:= Opts
+ THR_BOUND
;
995 Opts
:= THR_DETACHED
+ THR_BOUND
;
998 -- Note: the use of Unrestricted_Access in the following call is needed
999 -- because otherwise we have an error of getting a access-to-volatile
1000 -- value which points to a non-volatile object. But in this case it is
1001 -- safe to do this, since we know we have no problems with aliasing and
1002 -- Unrestricted_Access bypasses this check.
1006 (System
.Null_Address
,
1007 Adjusted_Stack_Size
,
1008 Thread_Body_Access
(Wrapper
),
1011 T
.Common
.LL
.Thread
'Unrestricted_Access);
1013 Succeeded
:= Result
= 0;
1016 or else Result
= ENOMEM
1017 or else Result
= EAGAIN
);
1024 procedure Finalize_TCB
(T
: Task_Id
) is
1025 Result
: Interfaces
.C
.int
;
1028 T
.Common
.LL
.Thread
:= Null_Thread_Id
;
1030 Result
:= mutex_destroy
(T
.Common
.LL
.L
.L
'Access);
1031 pragma Assert
(Result
= 0);
1033 Result
:= cond_destroy
(T
.Common
.LL
.CV
'Access);
1034 pragma Assert
(Result
= 0);
1036 if T
.Known_Tasks_Index
/= -1 then
1037 Known_Tasks
(T
.Known_Tasks_Index
) := null;
1040 ATCB_Allocation
.Free_ATCB
(T
);
1047 -- This procedure must be called with abort deferred. It can no longer
1048 -- call Self or access the current task's ATCB, since the ATCB has been
1051 procedure Exit_Task
is
1053 Specific
.Set
(null);
1060 procedure Abort_Task
(T
: Task_Id
) is
1061 Result
: Interfaces
.C
.int
;
1063 if Abort_Handler_Installed
then
1064 pragma Assert
(T
/= Self
);
1067 (T
.Common
.LL
.Thread
,
1068 Signal
(System
.Interrupt_Management
.Abort_Task_Interrupt
));
1069 pragma Assert
(Result
= 0);
1079 Reason
: Task_States
)
1081 Result
: Interfaces
.C
.int
;
1084 pragma Assert
(Check_Sleep
(Reason
));
1088 (Self_ID
.Common
.LL
.CV
'Access, Self_ID
.Common
.LL
.L
.L
'Access);
1091 (Record_Wakeup
(To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1092 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1095 -- Note that we are relying heavily here on GNAT representing
1096 -- Calendar.Time, System.Real_Time.Time, Duration,
1097 -- System.Real_Time.Time_Span in the same way, i.e., as a 64-bit count of
1100 -- This allows us to always pass the timeout value as a Duration
1103 -- We are taking liberties here with the semantics of the delays. That is,
1104 -- we make no distinction between delays on the Calendar clock and delays
1105 -- on the Real_Time clock. That is technically incorrect, if the Calendar
1106 -- clock happens to be reset or adjusted. To solve this defect will require
1107 -- modification to the compiler interface, so that it can pass through more
1108 -- information, to tell us here which clock to use.
1110 -- cond_timedwait will return if any of the following happens:
1111 -- 1) some other task did cond_signal on this condition variable
1112 -- In this case, the return value is 0
1113 -- 2) the call just returned, for no good reason
1114 -- This is called a "spurious wakeup".
1115 -- In this case, the return value may also be 0.
1116 -- 3) the time delay expires
1117 -- In this case, the return value is ETIME
1118 -- 4) this task received a signal, which was handled by some
1119 -- handler procedure, and now the thread is resuming execution
1120 -- UNIX calls this an "interrupted" system call.
1121 -- In this case, the return value is EINTR
1123 -- If the cond_timedwait returns 0 or EINTR, it is still possible that the
1124 -- time has actually expired, and by chance a signal or cond_signal
1125 -- occurred at around the same time.
1127 -- We have also observed that on some OS's the value ETIME will be
1128 -- returned, but the clock will show that the full delay has not yet
1131 -- For these reasons, we need to check the clock after return from
1132 -- cond_timedwait. If the time has expired, we will set Timedout = True.
1134 -- This check might be omitted for systems on which the cond_timedwait()
1135 -- never returns early or wakes up spuriously.
1137 -- Annex D requires that completion of a delay cause the task to go to the
1138 -- end of its priority queue, regardless of whether the task actually was
1139 -- suspended by the delay. Since cond_timedwait does not do this on
1140 -- Solaris, we add a call to thr_yield at the end. We might do this at the
1141 -- beginning, instead, but then the round-robin effect would not be the
1142 -- same; the delayed task would be ahead of other tasks of the same
1143 -- priority that awoke while it was sleeping.
1145 -- For Timed_Sleep, we are expecting possible cond_signals to indicate
1146 -- other events (e.g., completion of a RV or completion of the abortable
1147 -- part of an async. select), we want to always return if interrupted. The
1148 -- caller will be responsible for checking the task state to see whether
1149 -- the wakeup was spurious, and to go back to sleep again in that case. We
1150 -- don't need to check for pending abort or priority change on the way in
1151 -- our out; that is the caller's responsibility.
1153 -- For Timed_Delay, we are not expecting any cond_signals or other
1154 -- interruptions, except for priority changes and aborts. Therefore, we
1155 -- don't want to return unless the delay has actually expired, or the call
1156 -- has been aborted. In this case, since we want to implement the entire
1157 -- delay statement semantics, we do need to check for pending abort and
1158 -- priority changes. We can quietly handle priority changes inside the
1159 -- procedure, since there is no entry-queue reordering involved.
1165 procedure Timed_Sleep
1168 Mode
: ST
.Delay_Modes
;
1169 Reason
: System
.Tasking
.Task_States
;
1170 Timedout
: out Boolean;
1171 Yielded
: out Boolean)
1173 Base_Time
: constant Duration := Monotonic_Clock
;
1174 Check_Time
: Duration := Base_Time
;
1175 Abs_Time
: Duration;
1176 Request
: aliased timespec
;
1177 Result
: Interfaces
.C
.int
;
1180 pragma Assert
(Check_Sleep
(Reason
));
1186 then Duration'Min (Time
, Max_Sensible_Delay
) + Check_Time
1187 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
1189 if Abs_Time
> Check_Time
then
1190 Request
:= To_Timespec
(Abs_Time
);
1192 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
1196 (Self_ID
.Common
.LL
.CV
'Access,
1197 Self_ID
.Common
.LL
.L
.L
'Access, Request
'Access);
1199 Check_Time
:= Monotonic_Clock
;
1201 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
1203 if Result
= 0 or Result
= EINTR
then
1205 -- Somebody may have called Wakeup for us
1211 pragma Assert
(Result
= ETIME
);
1216 (Record_Wakeup
(To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Reason
));
1223 procedure Timed_Delay
1226 Mode
: ST
.Delay_Modes
)
1228 Base_Time
: constant Duration := Monotonic_Clock
;
1229 Check_Time
: Duration := Base_Time
;
1230 Abs_Time
: Duration;
1231 Request
: aliased timespec
;
1232 Result
: Interfaces
.C
.int
;
1233 Yielded
: Boolean := False;
1236 Write_Lock
(Self_ID
);
1240 then Time
+ Check_Time
1241 else Duration'Min (Check_Time
+ Max_Sensible_Delay
, Time
));
1243 if Abs_Time
> Check_Time
then
1244 Request
:= To_Timespec
(Abs_Time
);
1245 Self_ID
.Common
.State
:= Delay_Sleep
;
1247 pragma Assert
(Check_Sleep
(Delay_Sleep
));
1250 exit when Self_ID
.Pending_ATC_Level
< Self_ID
.ATC_Nesting_Level
;
1254 (Self_ID
.Common
.LL
.CV
'Access,
1255 Self_ID
.Common
.LL
.L
.L
'Access,
1258 Check_Time
:= Monotonic_Clock
;
1260 exit when Abs_Time
<= Check_Time
or else Check_Time
< Base_Time
;
1264 Result
= ETIME
or else
1270 (To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access), Delay_Sleep
));
1272 Self_ID
.Common
.State
:= Runnable
;
1288 Reason
: Task_States
)
1290 Result
: Interfaces
.C
.int
;
1292 pragma Assert
(Check_Wakeup
(T
, Reason
));
1293 Result
:= cond_signal
(T
.Common
.LL
.CV
'Access);
1294 pragma Assert
(Result
= 0);
1297 ---------------------------
1298 -- Check_Initialize_Lock --
1299 ---------------------------
1301 -- The following code is intended to check some of the invariant assertions
1302 -- related to lock usage, on which we depend.
1304 function Check_Initialize_Lock
1306 Level
: Lock_Level
) return Boolean
1308 Self_ID
: constant Task_Id
:= Self
;
1311 -- Check that caller is abort-deferred
1313 if Self_ID
.Deferral_Level
= 0 then
1317 -- Check that the lock is not yet initialized
1319 if L
.Level
/= 0 then
1323 L
.Level
:= Lock_Level
'Pos (Level
) + 1;
1325 end Check_Initialize_Lock
;
1331 function Check_Lock
(L
: Lock_Ptr
) return Boolean is
1332 Self_ID
: constant Task_Id
:= Self
;
1336 -- Check that the argument is not null
1342 -- Check that L is not frozen
1348 -- Check that caller is abort-deferred
1350 if Self_ID
.Deferral_Level
= 0 then
1354 -- Check that caller is not holding this lock already
1356 if L
.Owner
= To_Owner_ID
(To_Address
(Self_ID
)) then
1360 -- Check that TCB lock order rules are satisfied
1362 P
:= Self_ID
.Common
.LL
.Locks
;
1364 if P
.Level
>= L
.Level
1365 and then (P
.Level
> 2 or else L
.Level
> 2)
1378 function Record_Lock
(L
: Lock_Ptr
) return Boolean is
1379 Self_ID
: constant Task_Id
:= Self
;
1383 Lock_Count
:= Lock_Count
+ 1;
1385 -- There should be no owner for this lock at this point
1387 if L
.Owner
/= null then
1393 L
.Owner
:= To_Owner_ID
(To_Address
(Self_ID
));
1395 -- Check that TCB lock order rules are satisfied
1397 P
:= Self_ID
.Common
.LL
.Locks
;
1403 Self_ID
.Common
.LL
.Locking
:= null;
1404 Self_ID
.Common
.LL
.Locks
:= L
;
1412 function Check_Sleep
(Reason
: Task_States
) return Boolean is
1413 pragma Unreferenced
(Reason
);
1415 Self_ID
: constant Task_Id
:= Self
;
1419 -- Check that caller is abort-deferred
1421 if Self_ID
.Deferral_Level
= 0 then
1425 -- Check that caller is holding own lock, on top of list
1427 if Self_ID
.Common
.LL
.Locks
/=
1428 To_Lock_Ptr
(Self_ID
.Common
.LL
.L
'Access)
1433 -- Check that TCB lock order rules are satisfied
1435 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1439 Self_ID
.Common
.LL
.L
.Owner
:= null;
1440 P
:= Self_ID
.Common
.LL
.Locks
;
1441 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1450 function Record_Wakeup
1452 Reason
: Task_States
) return Boolean
1454 pragma Unreferenced
(Reason
);
1456 Self_ID
: constant Task_Id
:= Self
;
1462 L
.Owner
:= To_Owner_ID
(To_Address
(Self_ID
));
1464 -- Check that TCB lock order rules are satisfied
1466 P
:= Self_ID
.Common
.LL
.Locks
;
1472 Self_ID
.Common
.LL
.Locking
:= null;
1473 Self_ID
.Common
.LL
.Locks
:= L
;
1481 function Check_Wakeup
1483 Reason
: Task_States
) return Boolean
1485 Self_ID
: constant Task_Id
:= Self
;
1488 -- Is caller holding T's lock?
1490 if T
.Common
.LL
.L
.Owner
/= To_Owner_ID
(To_Address
(Self_ID
)) then
1494 -- Are reasons for wakeup and sleep consistent?
1496 if T
.Common
.State
/= Reason
then
1507 function Check_Unlock
(L
: Lock_Ptr
) return Boolean is
1508 Self_ID
: constant Task_Id
:= Self
;
1512 Unlock_Count
:= Unlock_Count
+ 1;
1518 if L
.Buddy
/= null then
1522 -- Magic constant 4???
1525 Check_Count
:= Unlock_Count
;
1528 -- Magic constant 1000???
1530 if Unlock_Count
- Check_Count
> 1000 then
1531 Check_Count
:= Unlock_Count
;
1534 -- Check that caller is abort-deferred
1536 if Self_ID
.Deferral_Level
= 0 then
1540 -- Check that caller is holding this lock, on top of list
1542 if Self_ID
.Common
.LL
.Locks
/= L
then
1546 -- Record there is no owner now
1549 P
:= Self_ID
.Common
.LL
.Locks
;
1550 Self_ID
.Common
.LL
.Locks
:= Self_ID
.Common
.LL
.Locks
.Next
;
1555 -------------------------
1556 -- Check_Finalize_Lock --
1557 -------------------------
1559 function Check_Finalize_Lock
(L
: Lock_Ptr
) return Boolean is
1560 Self_ID
: constant Task_Id
:= Self
;
1563 -- Check that caller is abort-deferred
1565 if Self_ID
.Deferral_Level
= 0 then
1569 -- Check that no one is holding this lock
1571 if L
.Owner
/= null then
1577 end Check_Finalize_Lock
;
1583 procedure Initialize
(S
: in out Suspension_Object
) is
1584 Result
: Interfaces
.C
.int
;
1587 -- Initialize internal state (always to zero (RM D.10(6)))
1592 -- Initialize internal mutex
1594 Result
:= mutex_init
(S
.L
'Access, USYNC_THREAD
, System
.Null_Address
);
1595 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1597 if Result
= ENOMEM
then
1598 raise Storage_Error
with "Failed to allocate a lock";
1601 -- Initialize internal condition variable
1603 Result
:= cond_init
(S
.CV
'Access, USYNC_THREAD
, 0);
1604 pragma Assert
(Result
= 0 or else Result
= ENOMEM
);
1607 Result
:= mutex_destroy
(S
.L
'Access);
1608 pragma Assert
(Result
= 0);
1610 if Result
= ENOMEM
then
1611 raise Storage_Error
;
1620 procedure Finalize
(S
: in out Suspension_Object
) is
1621 Result
: Interfaces
.C
.int
;
1624 -- Destroy internal mutex
1626 Result
:= mutex_destroy
(S
.L
'Access);
1627 pragma Assert
(Result
= 0);
1629 -- Destroy internal condition variable
1631 Result
:= cond_destroy
(S
.CV
'Access);
1632 pragma Assert
(Result
= 0);
1639 function Current_State
(S
: Suspension_Object
) return Boolean is
1641 -- We do not want to use lock on this read operation. State is marked
1642 -- as Atomic so that we ensure that the value retrieved is correct.
1651 procedure Set_False
(S
: in out Suspension_Object
) is
1652 Result
: Interfaces
.C
.int
;
1655 SSL
.Abort_Defer
.all;
1657 Result
:= mutex_lock
(S
.L
'Access);
1658 pragma Assert
(Result
= 0);
1662 Result
:= mutex_unlock
(S
.L
'Access);
1663 pragma Assert
(Result
= 0);
1665 SSL
.Abort_Undefer
.all;
1672 procedure Set_True
(S
: in out Suspension_Object
) is
1673 Result
: Interfaces
.C
.int
;
1676 SSL
.Abort_Defer
.all;
1678 Result
:= mutex_lock
(S
.L
'Access);
1679 pragma Assert
(Result
= 0);
1681 -- If there is already a task waiting on this suspension object then
1682 -- we resume it, leaving the state of the suspension object to False,
1683 -- as it is specified in ARM D.10 par. 9. Otherwise, it just leaves
1684 -- the state to True.
1690 Result
:= cond_signal
(S
.CV
'Access);
1691 pragma Assert
(Result
= 0);
1697 Result
:= mutex_unlock
(S
.L
'Access);
1698 pragma Assert
(Result
= 0);
1700 SSL
.Abort_Undefer
.all;
1703 ------------------------
1704 -- Suspend_Until_True --
1705 ------------------------
1707 procedure Suspend_Until_True
(S
: in out Suspension_Object
) is
1708 Result
: Interfaces
.C
.int
;
1711 SSL
.Abort_Defer
.all;
1713 Result
:= mutex_lock
(S
.L
'Access);
1714 pragma Assert
(Result
= 0);
1718 -- Program_Error must be raised upon calling Suspend_Until_True
1719 -- if another task is already waiting on that suspension object
1722 Result
:= mutex_unlock
(S
.L
'Access);
1723 pragma Assert
(Result
= 0);
1725 SSL
.Abort_Undefer
.all;
1727 raise Program_Error
;
1730 -- Suspend the task if the state is False. Otherwise, the task
1731 -- continues its execution, and the state of the suspension object
1732 -- is set to False (ARM D.10 par. 9).
1740 -- Loop in case pthread_cond_wait returns earlier than expected
1741 -- (e.g. in case of EINTR caused by a signal).
1743 Result
:= cond_wait
(S
.CV
'Access, S
.L
'Access);
1744 pragma Assert
(Result
= 0 or else Result
= EINTR
);
1746 exit when not S
.Waiting
;
1750 Result
:= mutex_unlock
(S
.L
'Access);
1751 pragma Assert
(Result
= 0);
1753 SSL
.Abort_Undefer
.all;
1755 end Suspend_Until_True
;
1761 function Check_Exit
(Self_ID
: Task_Id
) return Boolean is
1763 -- Check that caller is just holding Global_Task_Lock and no other locks
1765 if Self_ID
.Common
.LL
.Locks
= null then
1769 -- 2 = Global_Task_Level
1771 if Self_ID
.Common
.LL
.Locks
.Level
/= 2 then
1775 if Self_ID
.Common
.LL
.Locks
.Next
/= null then
1779 -- Check that caller is abort-deferred
1781 if Self_ID
.Deferral_Level
= 0 then
1788 --------------------
1789 -- Check_No_Locks --
1790 --------------------
1792 function Check_No_Locks
(Self_ID
: Task_Id
) return Boolean is
1794 return Self_ID
.Common
.LL
.Locks
= null;
1797 ----------------------
1798 -- Environment_Task --
1799 ----------------------
1801 function Environment_Task
return Task_Id
is
1803 return Environment_Task_Id
;
1804 end Environment_Task
;
1810 procedure Lock_RTS
is
1812 Write_Lock
(Single_RTS_Lock
'Access);
1819 procedure Unlock_RTS
is
1821 Unlock
(Single_RTS_Lock
'Access);
1828 function Suspend_Task
1830 Thread_Self
: Thread_Id
) return Boolean
1833 if T
.Common
.LL
.Thread
/= Thread_Self
then
1834 return thr_suspend
(T
.Common
.LL
.Thread
) = 0;
1844 function Resume_Task
1846 Thread_Self
: Thread_Id
) return Boolean
1849 if T
.Common
.LL
.Thread
/= Thread_Self
then
1850 return thr_continue
(T
.Common
.LL
.Thread
) = 0;
1856 --------------------
1857 -- Stop_All_Tasks --
1858 --------------------
1860 procedure Stop_All_Tasks
is
1869 function Stop_Task
(T
: ST
.Task_Id
) return Boolean is
1870 pragma Unreferenced
(T
);
1879 function Continue_Task
(T
: ST
.Task_Id
) return Boolean is
1880 pragma Unreferenced
(T
);
1885 -----------------------
1886 -- Set_Task_Affinity --
1887 -----------------------
1889 procedure Set_Task_Affinity
(T
: ST
.Task_Id
) is
1890 Result
: Interfaces
.C
.int
;
1891 Proc
: processorid_t
; -- User processor #
1892 Last_Proc
: processorid_t
; -- Last processor #
1894 use System
.Task_Info
;
1895 use type System
.Multiprocessors
.CPU_Range
;
1898 -- Do nothing if the underlying thread has not yet been created. If the
1899 -- thread has not yet been created then the proper affinity will be set
1900 -- during its creation.
1902 if T
.Common
.LL
.Thread
= Null_Thread_Id
then
1907 elsif T
.Common
.Base_CPU
/=
1908 System
.Multiprocessors
.Not_A_Specific_CPU
1910 -- The CPU numbering in pragma CPU starts at 1 while the subprogram
1911 -- to set the affinity starts at 0, therefore we must substract 1.
1915 (P_LWPID
, id_t
(T
.Common
.LL
.LWP
),
1916 processorid_t
(T
.Common
.Base_CPU
) - 1, null);
1917 pragma Assert
(Result
= 0);
1921 elsif T
.Common
.Task_Info
/= null then
1922 if T
.Common
.Task_Info
.New_LWP
1923 and then T
.Common
.Task_Info
.CPU
/= CPU_UNCHANGED
1925 Last_Proc
:= Num_Procs
- 1;
1927 if T
.Common
.Task_Info
.CPU
= ANY_CPU
then
1931 while Proc
< Last_Proc
loop
1932 Result
:= p_online
(Proc
, PR_STATUS
);
1933 exit when Result
= PR_ONLINE
;
1939 (P_LWPID
, id_t
(T
.Common
.LL
.LWP
), Proc
, null);
1940 pragma Assert
(Result
= 0);
1943 -- Use specified processor
1945 if T
.Common
.Task_Info
.CPU
< 0
1946 or else T
.Common
.Task_Info
.CPU
> Last_Proc
1948 raise Invalid_CPU_Number
;
1953 (P_LWPID
, id_t
(T
.Common
.LL
.LWP
),
1954 T
.Common
.Task_Info
.CPU
, null);
1955 pragma Assert
(Result
= 0);
1959 -- Handle dispatching domains
1961 elsif T
.Common
.Domain
/= null
1962 and then (T
.Common
.Domain
/= ST
.System_Domain
1963 or else T
.Common
.Domain
.all /=
1964 (Multiprocessors
.CPU
'First ..
1965 Multiprocessors
.Number_Of_CPUs
=> True))
1968 CPU_Set
: aliased psetid_t
;
1972 Result
:= pset_create
(CPU_Set
'Access);
1973 pragma Assert
(Result
= 0);
1975 -- Set the affinity to all the processors belonging to the
1976 -- dispatching domain.
1978 for Proc
in T
.Common
.Domain
'Range loop
1980 -- The Ada CPU numbering starts at 1 while the subprogram to
1981 -- set the affinity starts at 0, therefore we must substract 1.
1983 if T
.Common
.Domain
(Proc
) then
1985 pset_assign
(CPU_Set
, processorid_t
(Proc
) - 1, null);
1986 pragma Assert
(Result
= 0);
1991 pset_bind
(CPU_Set
, P_LWPID
, id_t
(T
.Common
.LL
.LWP
), null);
1992 pragma Assert
(Result
= 0);
1995 end Set_Task_Affinity
;
1997 end System
.Task_Primitives
.Operations
;