1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- SYSTEM.MULTIPROCESSORS.DISPATCHING_DOMAINS --
9 -- Copyright (C) 2011-2015, 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 -- Body used on targets where the operating system supports setting task
35 with System
.Tasking
.Initialization
;
36 with System
.Task_Primitives
.Operations
; use System
.Task_Primitives
.Operations
;
38 with Ada
.Unchecked_Conversion
;
40 package body System
.Multiprocessors
.Dispatching_Domains
is
42 package ST
renames System
.Tasking
;
44 -----------------------
45 -- Local subprograms --
46 -----------------------
48 function Convert_Ids
is new
49 Ada
.Unchecked_Conversion
(Ada
.Task_Identification
.Task_Id
, ST
.Task_Id
);
51 procedure Unchecked_Set_Affinity
52 (Domain
: ST
.Dispatching_Domain_Access
;
55 -- Internal procedure to move a task to a target domain and CPU. No checks
56 -- are performed about the validity of the domain and the CPU because they
57 -- are done by the callers of this procedure (either Assign_Task or
60 procedure Freeze_Dispatching_Domains
;
62 (Ada
, Freeze_Dispatching_Domains
, "__gnat_freeze_dispatching_domains");
63 -- Signal the time when no new dispatching domains can be created. It
64 -- should be called before the environment task calls the main procedure
65 -- (and after the elaboration code), so the binder-generated file needs to
66 -- import and call this procedure.
73 (Domain
: in out Dispatching_Domain
;
74 CPU
: CPU_Range
:= Not_A_Specific_CPU
;
75 T
: Ada
.Task_Identification
.Task_Id
:=
76 Ada
.Task_Identification
.Current_Task
)
78 Target
: constant ST
.Task_Id
:= Convert_Ids
(T
);
80 use type ST
.Dispatching_Domain_Access
;
83 -- The exception Dispatching_Domain_Error is propagated if T is already
84 -- assigned to a Dispatching_Domain other than
85 -- System_Dispatching_Domain, or if CPU is not one of the processors of
86 -- Domain (and is not Not_A_Specific_CPU).
88 if Dispatching_Domain
(Target
.Common
.Domain
) /= System_Dispatching_Domain
90 raise Dispatching_Domain_Error
with
91 "task already in user-defined dispatching domain";
93 elsif CPU
/= Not_A_Specific_CPU
and then CPU
not in Domain
'Range then
94 raise Dispatching_Domain_Error
with
95 "processor does not belong to dispatching domain";
98 -- Assigning a task to System_Dispatching_Domain that is already
99 -- assigned to that domain has no effect.
101 if Domain
= System_Dispatching_Domain
then
105 -- Set the task affinity once we know it is possible
107 Unchecked_Set_Affinity
108 (ST
.Dispatching_Domain_Access
(Domain
), CPU
, Target
);
116 function Create
(First
: CPU
; Last
: CPU_Range
) return Dispatching_Domain
is
118 return Create
((First
.. Last
=> True));
121 function Create
(Set
: CPU_Set
) return Dispatching_Domain
is
122 ST_DD
: aliased constant ST
.Dispatching_Domain
:=
123 ST
.Dispatching_Domain
(Set
);
124 First
: constant CPU
:= Get_First_CPU
(ST_DD
'Unrestricted_Access);
125 Last
: constant CPU_Range
:= Get_Last_CPU
(ST_DD
'Unrestricted_Access);
126 subtype Rng
is CPU_Range
range First
.. Last
;
128 use type ST
.Dispatching_Domain
;
129 use type ST
.Dispatching_Domain_Access
;
130 use type ST
.Array_Allocated_Tasks
;
135 New_System_Domain
: ST
.Dispatching_Domain
:= ST
.System_Domain
.all;
137 ST_DD_Slice
: constant ST
.Dispatching_Domain
:= ST_DD
(Rng
);
140 -- The set of processors for creating a dispatching domain must
141 -- comply with the following restrictions:
142 -- - Not exceeding the range of available processors.
143 -- - CPUs from the System_Dispatching_Domain.
144 -- - The calling task must be the environment task.
145 -- - The call to Create must take place before the call to the main
147 -- - Set does not contain a processor with a task assigned to it.
148 -- - The allocation cannot leave System_Dispatching_Domain empty.
150 -- Note that a previous version of the language forbade empty domains.
152 if Rng
'Last > Number_Of_CPUs
then
153 raise Dispatching_Domain_Error
with
154 "CPU not supported by the target";
158 System_Domain_Slice
: constant ST
.Dispatching_Domain
:=
159 ST
.System_Domain
(Rng
);
160 Actual
: constant ST
.Dispatching_Domain
:=
161 ST_DD_Slice
and not System_Domain_Slice
;
162 Expected
: constant ST
.Dispatching_Domain
:= (Rng
=> False);
164 if Actual
/= Expected
then
165 raise Dispatching_Domain_Error
with
166 "CPU not currently in System_Dispatching_Domain";
170 if Self
/= Environment_Task
then
171 raise Dispatching_Domain_Error
with
172 "only the environment task can create dispatching domains";
175 if ST
.Dispatching_Domains_Frozen
then
176 raise Dispatching_Domain_Error
with
177 "cannot create dispatching domain after call to main procedure";
181 if ST_DD
(Proc
) and then
182 ST
.Dispatching_Domain_Tasks
(Proc
) /= 0
184 raise Dispatching_Domain_Error
with "CPU has tasks assigned";
188 New_System_Domain
(Rng
) := New_System_Domain
(Rng
) and not ST_DD_Slice
;
190 if New_System_Domain
= (New_System_Domain
'Range => False) then
191 raise Dispatching_Domain_Error
with
192 "would leave System_Dispatching_Domain empty";
195 return Result
: constant Dispatching_Domain
:=
196 new ST
.Dispatching_Domain
'(ST_DD_Slice)
198 -- At this point we need to fix the processors belonging to the
199 -- system domain, and change the affinity of every task that has
200 -- been created and assigned to the system domain.
202 ST.Initialization.Defer_Abort (Self);
206 ST.System_Domain (Rng) := New_System_Domain (Rng);
207 pragma Assert (ST.System_Domain.all = New_System_Domain);
209 -- Iterate the list of tasks belonging to the default system
210 -- dispatching domain and set the appropriate affinity.
212 T := ST.All_Tasks_List;
215 if T.Common.Domain = ST.System_Domain then
216 Set_Task_Affinity (T);
219 T := T.Common.All_Tasks_Link;
224 ST.Initialization.Undefer_Abort (Self);
228 -----------------------------
229 -- Delay_Until_And_Set_CPU --
230 -----------------------------
232 procedure Delay_Until_And_Set_CPU
233 (Delay_Until_Time : Ada.Real_Time.Time;
237 -- Not supported atomically by the underlying operating systems.
238 -- Operating systems use to migrate the task immediately after the call
239 -- to set the affinity.
241 delay until Delay_Until_Time;
243 end Delay_Until_And_Set_CPU;
245 --------------------------------
246 -- Freeze_Dispatching_Domains --
247 --------------------------------
249 procedure Freeze_Dispatching_Domains is
251 -- Signal the end of the elaboration code
253 ST.Dispatching_Domains_Frozen := True;
254 end Freeze_Dispatching_Domains;
261 (T : Ada.Task_Identification.Task_Id :=
262 Ada.Task_Identification.Current_Task) return CPU_Range
265 return Convert_Ids (T).Common.Base_CPU;
272 function Get_CPU_Set (Domain : Dispatching_Domain) return CPU_Set is
274 return CPU_Set (Domain.all);
277 ----------------------------
278 -- Get_Dispatching_Domain --
279 ----------------------------
281 function Get_Dispatching_Domain
282 (T : Ada.Task_Identification.Task_Id :=
283 Ada.Task_Identification.Current_Task) return Dispatching_Domain
286 return Result : constant Dispatching_Domain :=
287 Dispatching_Domain (Convert_Ids (T).Common.Domain)
289 pragma Assert (Result /= null);
291 end Get_Dispatching_Domain;
297 function Get_First_CPU (Domain : Dispatching_Domain) return CPU is
299 for Proc in Domain'Range loop
300 if Domain (Proc) then
312 function Get_Last_CPU (Domain : Dispatching_Domain) return CPU_Range is
314 for Proc in reverse Domain'Range loop
315 if Domain (Proc) then
320 return CPU_Range'First;
329 T : Ada.Task_Identification.Task_Id :=
330 Ada.Task_Identification.Current_Task)
332 Target : constant ST.Task_Id := Convert_Ids (T);
334 use type ST.Dispatching_Domain_Access;
337 -- The exception Dispatching_Domain_Error is propagated if CPU is not
338 -- one of the processors of the Dispatching_Domain on which T is
339 -- assigned (and is not Not_A_Specific_CPU).
341 if CPU /= Not_A_Specific_CPU and then
342 (CPU not in Target.Common.Domain'Range or else
343 not Target.Common.Domain (CPU))
345 raise Dispatching_Domain_Error with
346 "processor does not belong to the task's dispatching domain";
349 Unchecked_Set_Affinity (Target.Common.Domain, CPU, Target);
352 ----------------------------
353 -- Unchecked_Set_Affinity --
354 ----------------------------
356 procedure Unchecked_Set_Affinity
357 (Domain : ST.Dispatching_Domain_Access;
361 Source_CPU : constant CPU_Range := T.Common.Base_CPU;
363 use type ST.Dispatching_Domain_Access;
368 -- Move to the new domain
370 T.Common.Domain := Domain;
372 -- Attach the CPU to the task
374 T.Common.Base_CPU := CPU;
376 -- Change the number of tasks attached to a given task in the system
379 if not ST.Dispatching_Domains_Frozen
380 and then (Domain = null or else Domain = ST.System_Domain)
382 -- Reduce the number of tasks attached to the CPU from which this
383 -- task is being moved, if needed.
385 if Source_CPU /= Not_A_Specific_CPU then
386 ST.Dispatching_Domain_Tasks (Source_CPU) :=
387 ST.Dispatching_Domain_Tasks (Source_CPU) - 1;
390 -- Increase the number of tasks attached to the CPU to which this
391 -- task is being moved, if needed.
393 if CPU /= Not_A_Specific_CPU then
394 ST.Dispatching_Domain_Tasks (CPU) :=
395 ST.Dispatching_Domain_Tasks (CPU) + 1;
399 -- Change the actual affinity calling the operating system level
401 Set_Task_Affinity (T);
404 end Unchecked_Set_Affinity;
406 end System.Multiprocessors.Dispatching_Domains;