1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- SYSTEM.MULTIPROCESSORS.DISPATCHING_DOMAINS --
9 -- Copyright (C) 2011, 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;
48 Dispatching_Domain_Tasks :
49 array (CPU'First .. Number_Of_CPUs) of Natural := (others => 0);
50 -- We need to store whether there are tasks allocated to concrete
51 -- processors in the default system dispatching domain because we need to
52 -- check it before creating a new dispatching domain.
53 -- ??? Tasks allocated with pragma CPU are not taken into account here.
55 Dispatching_Domains_Frozen : Boolean := False;
56 -- True when the main procedure has been called. Hence, no new dispatching
57 -- domains can be created when this flag is True.
59 -----------------------
60 -- Local subprograms --
61 -----------------------
63 function Convert_Ids is new
64 Ada.Unchecked_Conversion (Ada.Task_Identification.Task_Id, ST.Task_Id);
66 procedure Unchecked_Set_Affinity
67 (Domain : ST.Dispatching_Domain_Access;
70 -- Internal procedure to move a task to a target domain and CPU. No checks
71 -- are performed about the validity of the domain and the CPU because they
72 -- are done by the callers of this procedure (either Assign_Task or
75 procedure Freeze_Dispatching_Domains;
77 (Ada, Freeze_Dispatching_Domains, "__gnat_freeze_dispatching_domains");
78 -- Signal the time when no new dispatching domains can be created. It
79 -- should be called before the environment task calls the main procedure
80 -- (and after the elaboration code), so the binder-generated file needs to
81 -- import and call this procedure.
88 (Domain : in out Dispatching_Domain;
89 CPU : CPU_Range := Not_A_Specific_CPU;
90 T : Ada.Task_Identification.Task_Id :=
91 Ada.Task_Identification.Current_Task)
93 Target : constant ST.Task_Id := Convert_Ids (T);
95 use type System.Tasking.Dispatching_Domain_Access;
98 -- The exception Dispatching_Domain_Error is propagated if T is already
99 -- assigned to a Dispatching_Domain other than
100 -- System_Dispatching_Domain, or if CPU is not one of the processors of
101 -- Domain (and is not Not_A_Specific_CPU).
103 if Target.Common.Domain /= null and then
104 Dispatching_Domain (Target.Common.Domain) /= System_Dispatching_Domain
106 raise Dispatching_Domain_Error with
107 "task already in user-defined dispatching domain";
109 elsif CPU /= Not_A_Specific_CPU and then CPU not in Domain'Range then
110 raise Dispatching_Domain_Error with
111 "processor does not belong to dispatching domain";
114 -- Assigning a task to System_Dispatching_Domain that is already
115 -- assigned to that domain has no effect.
117 if Domain = System_Dispatching_Domain then
121 -- Set the task affinity once we know it is possible
123 Unchecked_Set_Affinity
124 (ST.Dispatching_Domain_Access (Domain), CPU, Target);
132 function Create (First, Last : CPU) return Dispatching_Domain is
133 use type System.Tasking.Dispatching_Domain;
134 use type System.Tasking.Dispatching_Domain_Access;
135 use type System.Tasking.Task_Id;
137 Valid_System_Domain : constant Boolean :=
138 (First > CPU'First and then
139 not (System_Dispatching_Domain (CPU'First .. First - 1) =
140 (CPU'First .. First - 1 => False)))
142 (Last < Number_Of_CPUs and then
143 not (System_Dispatching_Domain (Last + 1 .. Number_Of_CPUs) =
144 (Last + 1 .. Number_Of_CPUs => False)));
145 -- Constant that indicates whether there would exist a non-empty system
146 -- dispatching domain after the creation of this dispatching domain.
150 New_Domain : Dispatching_Domain;
153 -- The range of processors for creating a dispatching domain must
154 -- comply with the following restrictions:
156 -- - Not exceeding the range of available processors
157 -- - Range from the System_Dispatching_Domain
158 -- - Range does not contain a processor with a task assigned to it
159 -- - The allocation cannot leave System_Dispatching_Domain empty
160 -- - The calling task must be the environment task
161 -- - The call to Create must take place before the call to the main
165 raise Dispatching_Domain_Error with "empty dispatching domain";
167 elsif Last > Number_Of_CPUs then
168 raise Dispatching_Domain_Error with
169 "CPU range not supported by the target";
172 System_Dispatching_Domain (First .. Last) /= (First .. Last => True)
174 raise Dispatching_Domain_Error with
175 "CPU range not currently in System_Dispatching_Domain";
178 Dispatching_Domain_Tasks (First .. Last) /= (First .. Last => 0)
180 raise Dispatching_Domain_Error with "CPU range has tasks assigned";
182 elsif not Valid_System_Domain then
183 raise Dispatching_Domain_Error with
184 "would leave System_Dispatching_Domain empty";
186 elsif Self /= Environment_Task then
187 raise Dispatching_Domain_Error with
188 "only the environment task can create dispatching domains";
190 elsif Dispatching_Domains_Frozen then
191 raise Dispatching_Domain_Error with
192 "cannot create dispatching domain after call to main program";
195 New_Domain := new ST.Dispatching_Domain'(First .. Last => True);
197 -- At this point we need to fix the processors belonging to the system
198 -- domain, and change the affinity of every task that has been created
199 -- and assigned to the system domain.
201 ST.Initialization.Defer_Abort (Self);
205 System_Dispatching_Domain (First .. Last) := (First .. Last => False);
207 -- Iterate the list of tasks belonging to the default system
208 -- dispatching domain and set the appropriate affinity.
210 T := ST.All_Tasks_List;
213 if T.Common.Domain = null or else
214 T.Common.Domain = ST.System_Domain
216 Set_Task_Affinity (T);
219 T := T.Common.All_Tasks_Link;
224 ST.Initialization.Undefer_Abort (Self);
229 -----------------------------
230 -- Delay_Until_And_Set_CPU --
231 -----------------------------
233 procedure Delay_Until_And_Set_CPU
234 (Delay_Until_Time : Ada.Real_Time.Time; CPU : CPU_Range) is
236 -- Not supported atomically by the underlying operating systems.
237 -- Operating systems use to migrate the task immediately after the call
238 -- to set the affinity.
240 delay until Delay_Until_Time;
242 end Delay_Until_And_Set_CPU;
244 --------------------------------
245 -- Freeze_Dispatching_Domains --
246 --------------------------------
248 procedure Freeze_Dispatching_Domains is
250 -- Signal the end of the elaboration code
252 Dispatching_Domains_Frozen := True;
253 end Freeze_Dispatching_Domains;
260 (T : Ada.Task_Identification.Task_Id :=
261 Ada.Task_Identification.Current_Task)
264 return Convert_Ids (T).Common.Base_CPU;
267 ----------------------------
268 -- Get_Dispatching_Domain --
269 ----------------------------
271 function Get_Dispatching_Domain
272 (T : Ada.Task_Identification.Task_Id :=
273 Ada.Task_Identification.Current_Task)
274 return Dispatching_Domain is
276 return Dispatching_Domain (Convert_Ids (T).Common.Domain);
277 end Get_Dispatching_Domain;
283 function Get_First_CPU (Domain : Dispatching_Domain) return CPU is
285 for Proc in Domain'Range loop
286 if Domain (Proc) then
291 -- Should never reach the following return
300 function Get_Last_CPU (Domain : Dispatching_Domain) return CPU is
302 for Proc in reverse Domain'Range loop
303 if Domain (Proc) then
308 -- Should never reach the following return
319 T : Ada.Task_Identification.Task_Id :=
320 Ada.Task_Identification.Current_Task)
322 Target : constant ST.Task_Id := Convert_Ids (T);
324 use type ST.Dispatching_Domain_Access;
327 -- The exception Dispatching_Domain_Error is propagated if CPU is not
328 -- one of the processors of the Dispatching_Domain on which T is
329 -- assigned (and is not Not_A_Specific_CPU).
331 if CPU /= Not_A_Specific_CPU and then
332 (CPU not in Target.Common.Domain'Range or else
333 not Target.Common.Domain (CPU))
335 raise Dispatching_Domain_Error with
336 "CPU does not belong to the task's dispatching domain";
339 Unchecked_Set_Affinity (Target.Common.Domain, CPU, Target);
342 ----------------------------
343 -- Unchecked_Set_Affinity --
344 ----------------------------
346 procedure Unchecked_Set_Affinity
347 (Domain : ST.Dispatching_Domain_Access;
351 Source_CPU : constant CPU_Range := T.Common.Base_CPU;
353 use type System.Tasking.Dispatching_Domain_Access;
358 -- Move to the new domain
360 T.Common.Domain := Domain;
362 -- Attach the CPU to the task
364 T.Common.Base_CPU := CPU;
366 -- Change the number of tasks attached to a given task in the system
369 if not Dispatching_Domains_Frozen and then
370 (Domain = null or else Domain = ST.System_Domain)
372 -- Reduce the number of tasks attached to the CPU from which this
373 -- task is being moved, if needed.
375 if Source_CPU /= Not_A_Specific_CPU then
376 Dispatching_Domain_Tasks (Source_CPU) :=
377 Dispatching_Domain_Tasks (Source_CPU) - 1;
380 -- Increase the number of tasks attached to the CPU to which this
381 -- task is being moved, if needed.
383 if CPU /= Not_A_Specific_CPU then
384 Dispatching_Domain_Tasks (CPU) :=
385 Dispatching_Domain_Tasks (CPU) + 1;
389 -- Change the actual affinity calling the operating system level
391 Set_Task_Affinity (T);
394 end Unchecked_Set_Affinity;
396 end System.Multiprocessors.Dispatching_Domains;