1 | /** |
---|
2 | * @file rtems/score/scheduler.h |
---|
3 | * |
---|
4 | * @brief Constants and Structures Associated with the Scheduler |
---|
5 | * |
---|
6 | * This include file contains all the constants and structures associated |
---|
7 | * with the scheduler. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * Copyright (C) 2010 Gedare Bloom. |
---|
12 | * Copyright (C) 2011 On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _RTEMS_SCORE_SCHEDULER_H |
---|
20 | #define _RTEMS_SCORE_SCHEDULER_H |
---|
21 | |
---|
22 | #include <rtems/score/percpu.h> |
---|
23 | #include <rtems/score/priority.h> |
---|
24 | #include <rtems/score/thread.h> |
---|
25 | #if defined(__RTEMS_HAVE_SYS_CPUSET_H__) && defined(RTEMS_SMP) |
---|
26 | #include <sys/cpuset.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif |
---|
32 | |
---|
33 | /** |
---|
34 | * @defgroup ScoreScheduler Scheduler Handler |
---|
35 | * |
---|
36 | * @ingroup Score |
---|
37 | * |
---|
38 | * This handler encapsulates functionality related to managing sets of threads |
---|
39 | * that are ready for execution. |
---|
40 | */ |
---|
41 | /**@{*/ |
---|
42 | |
---|
43 | typedef struct Scheduler_Control Scheduler_Control; |
---|
44 | |
---|
45 | typedef struct Scheduler_Node Scheduler_Node; |
---|
46 | |
---|
47 | /** |
---|
48 | * @brief The scheduler operations. |
---|
49 | */ |
---|
50 | typedef struct { |
---|
51 | /** @see _Scheduler_Handler_initialization() */ |
---|
52 | void ( *initialize )( const Scheduler_Control * ); |
---|
53 | |
---|
54 | /** @see _Scheduler_Schedule() */ |
---|
55 | void ( *schedule )( const Scheduler_Control *, Thread_Control *); |
---|
56 | |
---|
57 | /** @see _Scheduler_Yield() */ |
---|
58 | void ( *yield )( const Scheduler_Control *, Thread_Control *); |
---|
59 | |
---|
60 | /** @see _Scheduler_Block() */ |
---|
61 | void ( *block )( const Scheduler_Control *, Thread_Control * ); |
---|
62 | |
---|
63 | /** @see _Scheduler_Unblock() */ |
---|
64 | void ( *unblock )( const Scheduler_Control *, Thread_Control * ); |
---|
65 | |
---|
66 | /** @see _Scheduler_Change_priority() */ |
---|
67 | void ( *change_priority )( |
---|
68 | const Scheduler_Control *, |
---|
69 | Thread_Control *, |
---|
70 | Priority_Control, |
---|
71 | bool |
---|
72 | ); |
---|
73 | |
---|
74 | /** @see _Scheduler_Node_initialize() */ |
---|
75 | void ( *node_initialize )( const Scheduler_Control *, Thread_Control * ); |
---|
76 | |
---|
77 | /** @see _Scheduler_Node_destroy() */ |
---|
78 | void ( *node_destroy )( const Scheduler_Control *, Thread_Control * ); |
---|
79 | |
---|
80 | /** @see _Scheduler_Update_priority() */ |
---|
81 | void ( *update_priority )( |
---|
82 | const Scheduler_Control *, |
---|
83 | Thread_Control *, |
---|
84 | Priority_Control |
---|
85 | ); |
---|
86 | |
---|
87 | /** @see _Scheduler_Priority_compare() */ |
---|
88 | int ( *priority_compare )( |
---|
89 | Priority_Control, |
---|
90 | Priority_Control |
---|
91 | ); |
---|
92 | |
---|
93 | /** @see _Scheduler_Release_job() */ |
---|
94 | void ( *release_job ) ( |
---|
95 | const Scheduler_Control *, |
---|
96 | Thread_Control *, |
---|
97 | uint32_t |
---|
98 | ); |
---|
99 | |
---|
100 | /** @see _Scheduler_Tick() */ |
---|
101 | void ( *tick )( const Scheduler_Control *, Thread_Control * ); |
---|
102 | |
---|
103 | /** @see _Scheduler_Start_idle() */ |
---|
104 | void ( *start_idle )( |
---|
105 | const Scheduler_Control *, |
---|
106 | Thread_Control *, |
---|
107 | Per_CPU_Control * |
---|
108 | ); |
---|
109 | |
---|
110 | #if defined(__RTEMS_HAVE_SYS_CPUSET_H__) && defined(RTEMS_SMP) |
---|
111 | /** @see _Scheduler_Get_affinity() */ |
---|
112 | bool ( *get_affinity )( |
---|
113 | const Scheduler_Control *, |
---|
114 | Thread_Control *, |
---|
115 | size_t, |
---|
116 | cpu_set_t * |
---|
117 | ); |
---|
118 | |
---|
119 | /** @see _Scheduler_Set_affinity() */ |
---|
120 | bool ( *set_affinity )( |
---|
121 | const Scheduler_Control *, |
---|
122 | Thread_Control *, |
---|
123 | size_t, |
---|
124 | const cpu_set_t * |
---|
125 | ); |
---|
126 | #endif |
---|
127 | } Scheduler_Operations; |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief Scheduler context. |
---|
131 | * |
---|
132 | * The scheduler context of a particular scheduler implementation must place |
---|
133 | * this structure at the begin of its context structure. |
---|
134 | */ |
---|
135 | typedef struct Scheduler_Context { |
---|
136 | #if defined(RTEMS_SMP) |
---|
137 | /** |
---|
138 | * @brief Count of processors owned by this scheduler instance. |
---|
139 | */ |
---|
140 | uint32_t processor_count; |
---|
141 | #endif |
---|
142 | } Scheduler_Context; |
---|
143 | |
---|
144 | /** |
---|
145 | * @brief Scheduler control. |
---|
146 | */ |
---|
147 | struct Scheduler_Control { |
---|
148 | /** |
---|
149 | * @brief Reference to a statically allocated scheduler context. |
---|
150 | */ |
---|
151 | Scheduler_Context *context; |
---|
152 | |
---|
153 | /** |
---|
154 | * @brief The scheduler operations. |
---|
155 | */ |
---|
156 | Scheduler_Operations Operations; |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief The scheduler name. |
---|
160 | */ |
---|
161 | uint32_t name; |
---|
162 | }; |
---|
163 | |
---|
164 | /** |
---|
165 | * @brief Scheduler node for per-thread data. |
---|
166 | */ |
---|
167 | struct Scheduler_Node { |
---|
168 | #if defined(RTEMS_SMP) |
---|
169 | /** |
---|
170 | * @brief Chain node for usage in various scheduler data structures. |
---|
171 | * |
---|
172 | * Strictly this is the wrong place for this field since the data structures |
---|
173 | * to manage scheduler nodes belong to the particular scheduler |
---|
174 | * implementation. Currently all SMP scheduler implementations use chains. |
---|
175 | * The node is here to simplify things, just like the object node in the |
---|
176 | * thread control block. It may be replaced with a union to add a red-black |
---|
177 | * tree node in the future. |
---|
178 | */ |
---|
179 | Chain_Node Node; |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief The thread owning this node. |
---|
183 | */ |
---|
184 | Thread_Control *owner; |
---|
185 | #endif |
---|
186 | }; |
---|
187 | |
---|
188 | /** |
---|
189 | * @brief Registered schedulers. |
---|
190 | * |
---|
191 | * Application provided via <rtems/confdefs.h>. |
---|
192 | * |
---|
193 | * @see _Scheduler_Count. |
---|
194 | */ |
---|
195 | extern const Scheduler_Control _Scheduler_Table[]; |
---|
196 | |
---|
197 | /** |
---|
198 | * @brief Count of registered schedulers. |
---|
199 | * |
---|
200 | * Application provided via <rtems/confdefs.h> on SMP configurations. |
---|
201 | * |
---|
202 | * It is very important that this is a compile-time constant on uni-processor |
---|
203 | * configurations (in this case RTEMS_SMP is not defined) so that the compiler |
---|
204 | * can optimize the some loops away |
---|
205 | * |
---|
206 | * @see _Scheduler_Table. |
---|
207 | */ |
---|
208 | #if defined(RTEMS_SMP) |
---|
209 | extern const size_t _Scheduler_Count; |
---|
210 | #else |
---|
211 | #define _Scheduler_Count ( (size_t) 1 ) |
---|
212 | #endif |
---|
213 | |
---|
214 | #if defined(RTEMS_SMP) |
---|
215 | /** |
---|
216 | * @brief The scheduler assignment default attributes. |
---|
217 | */ |
---|
218 | #define SCHEDULER_ASSIGN_DEFAULT UINT32_C(0x0) |
---|
219 | |
---|
220 | /** |
---|
221 | * @brief The presence of this processor is optional. |
---|
222 | */ |
---|
223 | #define SCHEDULER_ASSIGN_PROCESSOR_OPTIONAL SCHEDULER_ASSIGN_DEFAULT |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief The presence of this processor is mandatory. |
---|
227 | */ |
---|
228 | #define SCHEDULER_ASSIGN_PROCESSOR_MANDATORY UINT32_C(0x1) |
---|
229 | |
---|
230 | /** |
---|
231 | * @brief Scheduler assignment. |
---|
232 | */ |
---|
233 | typedef struct { |
---|
234 | /** |
---|
235 | * @brief The scheduler for this processor. |
---|
236 | */ |
---|
237 | const Scheduler_Control *scheduler; |
---|
238 | |
---|
239 | /** |
---|
240 | * @brief The scheduler assignment attributes. |
---|
241 | * |
---|
242 | * Use @ref SCHEDULER_ASSIGN_DEFAULT to select default attributes. |
---|
243 | * |
---|
244 | * The presence of a processor can be |
---|
245 | * - @ref SCHEDULER_ASSIGN_PROCESSOR_OPTIONAL, or |
---|
246 | * - @ref SCHEDULER_ASSIGN_PROCESSOR_MANDATORY. |
---|
247 | */ |
---|
248 | uint32_t attributes; |
---|
249 | } Scheduler_Assignment; |
---|
250 | |
---|
251 | /** |
---|
252 | * @brief The scheduler assignments. |
---|
253 | * |
---|
254 | * The length of this array must be equal to the maximum processors. |
---|
255 | * |
---|
256 | * Application provided via <rtems/confdefs.h>. |
---|
257 | * |
---|
258 | * @see _Scheduler_Table and rtems_configuration_get_maximum_processors(). |
---|
259 | */ |
---|
260 | extern const Scheduler_Assignment _Scheduler_Assignments[]; |
---|
261 | #endif |
---|
262 | |
---|
263 | /** |
---|
264 | * @brief Does nothing. |
---|
265 | * |
---|
266 | * @param[in] scheduler Unused. |
---|
267 | * @param[in] the_thread Unused. |
---|
268 | */ |
---|
269 | void _Scheduler_default_Schedule( |
---|
270 | const Scheduler_Control *scheduler, |
---|
271 | Thread_Control *the_thread |
---|
272 | ); |
---|
273 | |
---|
274 | /** |
---|
275 | * @brief Does nothing. |
---|
276 | * |
---|
277 | * @param[in] scheduler Unused. |
---|
278 | * @param[in] the_thread Unused. |
---|
279 | */ |
---|
280 | void _Scheduler_default_Node_initialize( |
---|
281 | const Scheduler_Control *scheduler, |
---|
282 | Thread_Control *the_thread |
---|
283 | ); |
---|
284 | |
---|
285 | /** |
---|
286 | * @brief Does nothing. |
---|
287 | * |
---|
288 | * @param[in] scheduler Unused. |
---|
289 | * @param[in] the_thread Unused. |
---|
290 | */ |
---|
291 | void _Scheduler_default_Node_destroy( |
---|
292 | const Scheduler_Control *scheduler, |
---|
293 | Thread_Control *the_thread |
---|
294 | ); |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief Does nothing. |
---|
298 | * |
---|
299 | * @param[in] scheduler Unused. |
---|
300 | * @param[in] the_thread Unused. |
---|
301 | * @param[in] new_priority Unused. |
---|
302 | */ |
---|
303 | void _Scheduler_default_Update_priority( |
---|
304 | const Scheduler_Control *scheduler, |
---|
305 | Thread_Control *the_thread, |
---|
306 | Priority_Control new_priority |
---|
307 | ); |
---|
308 | |
---|
309 | /** |
---|
310 | * @brief Does nothing. |
---|
311 | * |
---|
312 | * @param[in] scheduler Unused. |
---|
313 | * @param[in] the_thread Unused. |
---|
314 | * @param[in] deadline Unused. |
---|
315 | */ |
---|
316 | void _Scheduler_default_Release_job( |
---|
317 | const Scheduler_Control *scheduler, |
---|
318 | Thread_Control *the_thread, |
---|
319 | uint32_t deadline |
---|
320 | ); |
---|
321 | |
---|
322 | /** |
---|
323 | * @brief Performs tick operations depending on the CPU budget algorithm for |
---|
324 | * each executing thread. |
---|
325 | * |
---|
326 | * This routine is invoked as part of processing each clock tick. |
---|
327 | * |
---|
328 | * @param[in] scheduler The scheduler. |
---|
329 | * @param[in] execution An executing thread. |
---|
330 | */ |
---|
331 | void _Scheduler_default_Tick( |
---|
332 | const Scheduler_Control *scheduler, |
---|
333 | Thread_Control *executing |
---|
334 | ); |
---|
335 | |
---|
336 | /** |
---|
337 | * @brief Starts an idle thread. |
---|
338 | * |
---|
339 | * @param[in] scheduler The scheduler. |
---|
340 | * @param[in] the_thread An idle thread. |
---|
341 | * @param[in] cpu This parameter is unused. |
---|
342 | */ |
---|
343 | void _Scheduler_default_Start_idle( |
---|
344 | const Scheduler_Control *scheduler, |
---|
345 | Thread_Control *the_thread, |
---|
346 | Per_CPU_Control *cpu |
---|
347 | ); |
---|
348 | |
---|
349 | #if defined(__RTEMS_HAVE_SYS_CPUSET_H__) && defined(RTEMS_SMP) |
---|
350 | /** |
---|
351 | * @brief Get affinity for the default scheduler. |
---|
352 | * |
---|
353 | * @param[in] thread The associated thread. |
---|
354 | * @param[in] cpusetsize The size of the cpuset. |
---|
355 | * @param[out] cpuset Affinity set containing all CPUs. |
---|
356 | * |
---|
357 | * @retval 0 Successfully got cpuset |
---|
358 | * @retval -1 The cpusetsize is invalid for the system |
---|
359 | */ |
---|
360 | bool _Scheduler_default_Get_affinity( |
---|
361 | const Scheduler_Control *scheduler, |
---|
362 | Thread_Control *thread, |
---|
363 | size_t cpusetsize, |
---|
364 | cpu_set_t *cpuset |
---|
365 | ); |
---|
366 | |
---|
367 | /** |
---|
368 | * @brief Set affinity for the default scheduler. |
---|
369 | * |
---|
370 | * @param[in] thread The associated thread. |
---|
371 | * @param[in] cpusetsize The size of the cpuset. |
---|
372 | * @param[in] cpuset Affinity new affinity set. |
---|
373 | * |
---|
374 | * @retval 0 Successful |
---|
375 | * |
---|
376 | * This method always returns successful and does not save |
---|
377 | * the cpuset. |
---|
378 | */ |
---|
379 | bool _Scheduler_default_Set_affinity( |
---|
380 | const Scheduler_Control *scheduler, |
---|
381 | Thread_Control *thread, |
---|
382 | size_t cpusetsize, |
---|
383 | const cpu_set_t *cpuset |
---|
384 | ); |
---|
385 | |
---|
386 | #define SCHEDULER_OPERATION_DEFAULT_GET_SET_AFFINITY \ |
---|
387 | , _Scheduler_default_Get_affinity \ |
---|
388 | , _Scheduler_default_Set_affinity |
---|
389 | #else |
---|
390 | #define SCHEDULER_OPERATION_DEFAULT_GET_SET_AFFINITY |
---|
391 | #endif |
---|
392 | |
---|
393 | /** |
---|
394 | * @brief Indicates if thread priority queues are broken with the configured |
---|
395 | * scheduler or not. |
---|
396 | * |
---|
397 | * See also PR2174: Memory corruption with EDF scheduler and thread priority |
---|
398 | * queues. |
---|
399 | */ |
---|
400 | extern const bool _Scheduler_FIXME_thread_priority_queues_are_broken; |
---|
401 | |
---|
402 | /**@}*/ |
---|
403 | |
---|
404 | #ifdef __cplusplus |
---|
405 | } |
---|
406 | #endif |
---|
407 | |
---|
408 | #endif |
---|
409 | /* end of include file */ |
---|