1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- SYSTEM.MULTIPROCESSORS.DISPATCHING_DOMAINS --
9 -- Copyright (C) 2011-2017, 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
);
81 -- The exception Dispatching_Domain_Error is propagated if T is already
82 -- assigned to a Dispatching_Domain other than
83 -- System_Dispatching_Domain, or if CPU is not one of the processors of
84 -- Domain (and is not Not_A_Specific_CPU).
86 if Dispatching_Domain
(Target
.Common
.Domain
) /= System_Dispatching_Domain
88 raise Dispatching_Domain_Error
with
89 "task already in user-defined dispatching domain";
91 elsif CPU
/= Not_A_Specific_CPU
and then CPU
not in Domain
'Range then
92 raise Dispatching_Domain_Error
with
93 "processor does not belong to dispatching domain";
96 -- Assigning a task to System_Dispatching_Domain that is already
97 -- assigned to that domain has no effect.
99 if Domain
= System_Dispatching_Domain
then
103 -- Set the task affinity once we know it is possible
105 Unchecked_Set_Affinity
106 (ST
.Dispatching_Domain_Access
(Domain
), CPU
, Target
);
114 function Create
(First
: CPU
; Last
: CPU_Range
) return Dispatching_Domain
is
116 return Create
((First
.. Last
=> True));
119 function Create
(Set
: CPU_Set
) return Dispatching_Domain
is
120 ST_DD
: aliased constant ST
.Dispatching_Domain
:=
121 ST
.Dispatching_Domain
(Set
);
122 First
: constant CPU
:= Get_First_CPU
(ST_DD
'Unrestricted_Access);
123 Last
: constant CPU_Range
:= Get_Last_CPU
(ST_DD
'Unrestricted_Access);
124 subtype Rng
is CPU_Range
range First
.. Last
;
126 use type ST
.Dispatching_Domain
;
127 use type ST
.Dispatching_Domain_Access
;
132 New_System_Domain
: ST
.Dispatching_Domain
:= ST
.System_Domain
.all;
134 ST_DD_Slice
: constant ST
.Dispatching_Domain
:= ST_DD
(Rng
);
137 -- The set of processors for creating a dispatching domain must
138 -- comply with the following restrictions:
139 -- - Not exceeding the range of available processors.
140 -- - CPUs from the System_Dispatching_Domain.
141 -- - The calling task must be the environment task.
142 -- - The call to Create must take place before the call to the main
144 -- - Set does not contain a processor with a task assigned to it.
145 -- - The allocation cannot leave System_Dispatching_Domain empty.
147 -- Note that a previous version of the language forbade empty domains.
149 if Rng
'Last > Number_Of_CPUs
then
150 raise Dispatching_Domain_Error
with
151 "CPU not supported by the target";
155 System_Domain_Slice
: constant ST
.Dispatching_Domain
:=
156 ST
.System_Domain
(Rng
);
157 Actual
: constant ST
.Dispatching_Domain
:=
158 ST_DD_Slice
and not System_Domain_Slice
;
159 Expected
: constant ST
.Dispatching_Domain
:= (Rng
=> False);
161 if Actual
/= Expected
then
162 raise Dispatching_Domain_Error
with
163 "CPU not currently in System_Dispatching_Domain";
167 if Self
/= Environment_Task
then
168 raise Dispatching_Domain_Error
with
169 "only the environment task can create dispatching domains";
172 if ST
.Dispatching_Domains_Frozen
then
173 raise Dispatching_Domain_Error
with
174 "cannot create dispatching domain after call to main procedure";
178 if ST_DD
(Proc
) and then
179 ST
.Dispatching_Domain_Tasks
(Proc
) /= 0
181 raise Dispatching_Domain_Error
with "CPU has tasks assigned";
185 New_System_Domain
(Rng
) := New_System_Domain
(Rng
) and not ST_DD_Slice
;
187 if New_System_Domain
= (New_System_Domain
'Range => False) then
188 raise Dispatching_Domain_Error
with
189 "would leave System_Dispatching_Domain empty";
192 return Result
: constant Dispatching_Domain
:=
193 new ST
.Dispatching_Domain
'(ST_DD_Slice)
195 -- At this point we need to fix the processors belonging to the
196 -- system domain, and change the affinity of every task that has
197 -- been created and assigned to the system domain.
199 ST.Initialization.Defer_Abort (Self);
203 ST.System_Domain (Rng) := New_System_Domain (Rng);
204 pragma Assert (ST.System_Domain.all = New_System_Domain);
206 -- Iterate the list of tasks belonging to the default system
207 -- dispatching domain and set the appropriate affinity.
209 T := ST.All_Tasks_List;
212 if T.Common.Domain = ST.System_Domain then
213 Set_Task_Affinity (T);
216 T := T.Common.All_Tasks_Link;
221 ST.Initialization.Undefer_Abort (Self);
225 -----------------------------
226 -- Delay_Until_And_Set_CPU --
227 -----------------------------
229 procedure Delay_Until_And_Set_CPU
230 (Delay_Until_Time : Ada.Real_Time.Time;
234 -- Not supported atomically by the underlying operating systems.
235 -- Operating systems use to migrate the task immediately after the call
236 -- to set the affinity.
238 delay until Delay_Until_Time;
240 end Delay_Until_And_Set_CPU;
242 --------------------------------
243 -- Freeze_Dispatching_Domains --
244 --------------------------------
246 procedure Freeze_Dispatching_Domains is
248 -- Signal the end of the elaboration code
250 ST.Dispatching_Domains_Frozen := True;
251 end Freeze_Dispatching_Domains;
258 (T : Ada.Task_Identification.Task_Id :=
259 Ada.Task_Identification.Current_Task) return CPU_Range
262 return Convert_Ids (T).Common.Base_CPU;
269 function Get_CPU_Set (Domain : Dispatching_Domain) return CPU_Set is
271 return CPU_Set (Domain.all);
274 ----------------------------
275 -- Get_Dispatching_Domain --
276 ----------------------------
278 function Get_Dispatching_Domain
279 (T : Ada.Task_Identification.Task_Id :=
280 Ada.Task_Identification.Current_Task) return Dispatching_Domain
283 return Result : constant Dispatching_Domain :=
284 Dispatching_Domain (Convert_Ids (T).Common.Domain)
286 pragma Assert (Result /= null);
288 end Get_Dispatching_Domain;
294 function Get_First_CPU (Domain : Dispatching_Domain) return CPU is
296 for Proc in Domain'Range loop
297 if Domain (Proc) then
309 function Get_Last_CPU (Domain : Dispatching_Domain) return CPU_Range is
311 for Proc in reverse Domain'Range loop
312 if Domain (Proc) then
317 return CPU_Range'First;
326 T : Ada.Task_Identification.Task_Id :=
327 Ada.Task_Identification.Current_Task)
329 Target : constant ST.Task_Id := Convert_Ids (T);
332 -- The exception Dispatching_Domain_Error is propagated if CPU is not
333 -- one of the processors of the Dispatching_Domain on which T is
334 -- assigned (and is not Not_A_Specific_CPU).
336 if CPU /= Not_A_Specific_CPU and then
337 (CPU not in Target.Common.Domain'Range or else
338 not Target.Common.Domain (CPU))
340 raise Dispatching_Domain_Error with
341 "processor does not belong to the task's dispatching domain";
344 Unchecked_Set_Affinity (Target.Common.Domain, CPU, Target);
347 ----------------------------
348 -- Unchecked_Set_Affinity --
349 ----------------------------
351 procedure Unchecked_Set_Affinity
352 (Domain : ST.Dispatching_Domain_Access;
356 Source_CPU : constant CPU_Range := T.Common.Base_CPU;
358 use type ST.Dispatching_Domain_Access;
363 -- Move to the new domain
365 T.Common.Domain := Domain;
367 -- Attach the CPU to the task
369 T.Common.Base_CPU := CPU;
371 -- Change the number of tasks attached to a given task in the system
374 if not ST.Dispatching_Domains_Frozen
375 and then (Domain = null or else Domain = ST.System_Domain)
377 -- Reduce the number of tasks attached to the CPU from which this
378 -- task is being moved, if needed.
380 if Source_CPU /= Not_A_Specific_CPU then
381 ST.Dispatching_Domain_Tasks (Source_CPU) :=
382 ST.Dispatching_Domain_Tasks (Source_CPU) - 1;
385 -- Increase the number of tasks attached to the CPU to which this
386 -- task is being moved, if needed.
388 if CPU /= Not_A_Specific_CPU then
389 ST.Dispatching_Domain_Tasks (CPU) :=
390 ST.Dispatching_Domain_Tasks (CPU) + 1;
394 -- Change the actual affinity calling the operating system level
396 Set_Task_Affinity (T);
399 end Unchecked_Set_Affinity;
401 end System.Multiprocessors.Dispatching_Domains;