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/thread.h> |
---|
23 | #if defined(RTEMS_SMP) |
---|
24 | #include <sys/cpuset.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" { |
---|
29 | #endif |
---|
30 | |
---|
31 | struct Per_CPU_Control; |
---|
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 | /** |
---|
46 | * @brief The scheduler operations. |
---|
47 | */ |
---|
48 | typedef struct { |
---|
49 | /** @see _Scheduler_Handler_initialization() */ |
---|
50 | void ( *initialize )( const Scheduler_Control * ); |
---|
51 | |
---|
52 | /** @see _Scheduler_Schedule() */ |
---|
53 | void ( *schedule )( const Scheduler_Control *, Thread_Control *); |
---|
54 | |
---|
55 | /** @see _Scheduler_Yield() */ |
---|
56 | void ( *yield )( |
---|
57 | const Scheduler_Control *, |
---|
58 | Thread_Control *, |
---|
59 | Scheduler_Node * |
---|
60 | ); |
---|
61 | |
---|
62 | /** @see _Scheduler_Block() */ |
---|
63 | void ( *block )( |
---|
64 | const Scheduler_Control *, |
---|
65 | Thread_Control *, |
---|
66 | Scheduler_Node * |
---|
67 | ); |
---|
68 | |
---|
69 | /** @see _Scheduler_Unblock() */ |
---|
70 | void ( *unblock )( |
---|
71 | const Scheduler_Control *, |
---|
72 | Thread_Control *, |
---|
73 | Scheduler_Node * |
---|
74 | ); |
---|
75 | |
---|
76 | /** @see _Scheduler_Update_priority() */ |
---|
77 | void ( *update_priority )( |
---|
78 | const Scheduler_Control *, |
---|
79 | Thread_Control *, |
---|
80 | Scheduler_Node * |
---|
81 | ); |
---|
82 | |
---|
83 | /** @see _Scheduler_Map_priority() */ |
---|
84 | Priority_Control ( *map_priority )( |
---|
85 | const Scheduler_Control *, |
---|
86 | Priority_Control |
---|
87 | ); |
---|
88 | |
---|
89 | /** @see _Scheduler_Unmap_priority() */ |
---|
90 | Priority_Control ( *unmap_priority )( |
---|
91 | const Scheduler_Control *, |
---|
92 | Priority_Control |
---|
93 | ); |
---|
94 | |
---|
95 | #if defined(RTEMS_SMP) |
---|
96 | /** |
---|
97 | * @brief Ask for help operation. |
---|
98 | * |
---|
99 | * @param[in] scheduler The scheduler instance to ask for help. |
---|
100 | * @param[in] the_thread The thread needing help. |
---|
101 | * @param[in] node The scheduler node. |
---|
102 | * |
---|
103 | * @retval true Ask for help was successful. |
---|
104 | * @retval false Otherwise. |
---|
105 | */ |
---|
106 | bool ( *ask_for_help )( |
---|
107 | const Scheduler_Control *scheduler, |
---|
108 | Thread_Control *the_thread, |
---|
109 | Scheduler_Node *node |
---|
110 | ); |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief Reconsider help operation. |
---|
114 | * |
---|
115 | * @param[in] scheduler The scheduler instance to reconsider the help |
---|
116 | * request. |
---|
117 | * @param[in] the_thread The thread reconsidering a help request. |
---|
118 | * @param[in] node The scheduler node. |
---|
119 | */ |
---|
120 | void ( *reconsider_help_request )( |
---|
121 | const Scheduler_Control *scheduler, |
---|
122 | Thread_Control *the_thread, |
---|
123 | Scheduler_Node *node |
---|
124 | ); |
---|
125 | |
---|
126 | /** |
---|
127 | * @brief Withdraw node operation. |
---|
128 | * |
---|
129 | * @param[in] scheduler The scheduler instance to withdraw the node. |
---|
130 | * @param[in] the_thread The thread using the node. |
---|
131 | * @param[in] node The scheduler node to withdraw. |
---|
132 | * @param[in] next_state The next thread scheduler state in case the node is |
---|
133 | * scheduled. |
---|
134 | */ |
---|
135 | void ( *withdraw_node )( |
---|
136 | const Scheduler_Control *scheduler, |
---|
137 | Thread_Control *the_thread, |
---|
138 | Scheduler_Node *node, |
---|
139 | Thread_Scheduler_state next_state |
---|
140 | ); |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Add processor operation. |
---|
144 | * |
---|
145 | * @param[in] scheduler The scheduler instance to add the processor. |
---|
146 | * @param[in] idle The idle thread of the processor to add. |
---|
147 | */ |
---|
148 | void ( *add_processor )( |
---|
149 | const Scheduler_Control *scheduler, |
---|
150 | Thread_Control *idle |
---|
151 | ); |
---|
152 | |
---|
153 | /** |
---|
154 | * @brief Remove processor operation. |
---|
155 | * |
---|
156 | * @param[in] scheduler The scheduler instance to remove the processor. |
---|
157 | * @param[in] cpu The processor to remove. |
---|
158 | * |
---|
159 | * @return The idle thread of the removed processor. |
---|
160 | */ |
---|
161 | Thread_Control *( *remove_processor )( |
---|
162 | const Scheduler_Control *scheduler, |
---|
163 | struct Per_CPU_Control *cpu |
---|
164 | ); |
---|
165 | #endif |
---|
166 | |
---|
167 | /** @see _Scheduler_Node_initialize() */ |
---|
168 | void ( *node_initialize )( |
---|
169 | const Scheduler_Control *, |
---|
170 | Scheduler_Node *, |
---|
171 | Thread_Control *, |
---|
172 | Priority_Control |
---|
173 | ); |
---|
174 | |
---|
175 | /** @see _Scheduler_Node_destroy() */ |
---|
176 | void ( *node_destroy )( const Scheduler_Control *, Scheduler_Node * ); |
---|
177 | |
---|
178 | /** @see _Scheduler_Release_job() */ |
---|
179 | void ( *release_job ) ( |
---|
180 | const Scheduler_Control *, |
---|
181 | Thread_Control *, |
---|
182 | Priority_Node *, |
---|
183 | uint64_t, |
---|
184 | Thread_queue_Context * |
---|
185 | ); |
---|
186 | |
---|
187 | /** @see _Scheduler_Cancel_job() */ |
---|
188 | void ( *cancel_job ) ( |
---|
189 | const Scheduler_Control *, |
---|
190 | Thread_Control *, |
---|
191 | Priority_Node *, |
---|
192 | Thread_queue_Context * |
---|
193 | ); |
---|
194 | |
---|
195 | /** @see _Scheduler_Tick() */ |
---|
196 | void ( *tick )( const Scheduler_Control *, Thread_Control * ); |
---|
197 | |
---|
198 | /** @see _Scheduler_Start_idle() */ |
---|
199 | void ( *start_idle )( |
---|
200 | const Scheduler_Control *, |
---|
201 | Thread_Control *, |
---|
202 | struct Per_CPU_Control * |
---|
203 | ); |
---|
204 | |
---|
205 | #if defined(RTEMS_SMP) |
---|
206 | /** @see _Scheduler_Set_affinity() */ |
---|
207 | bool ( *set_affinity )( |
---|
208 | const Scheduler_Control *, |
---|
209 | Thread_Control *, |
---|
210 | Scheduler_Node *, |
---|
211 | const Processor_mask * |
---|
212 | ); |
---|
213 | #endif |
---|
214 | } Scheduler_Operations; |
---|
215 | |
---|
216 | /** |
---|
217 | * @brief Scheduler context. |
---|
218 | * |
---|
219 | * The scheduler context of a particular scheduler implementation must place |
---|
220 | * this structure at the begin of its context structure. |
---|
221 | */ |
---|
222 | typedef struct Scheduler_Context { |
---|
223 | /** |
---|
224 | * @brief Lock to protect this scheduler instance. |
---|
225 | */ |
---|
226 | ISR_LOCK_MEMBER( Lock ) |
---|
227 | |
---|
228 | #if defined(RTEMS_SMP) |
---|
229 | /** |
---|
230 | * @brief The set of processors owned by this scheduler instance. |
---|
231 | */ |
---|
232 | Processor_mask Processors; |
---|
233 | #endif |
---|
234 | } Scheduler_Context; |
---|
235 | |
---|
236 | /** |
---|
237 | * @brief Scheduler control. |
---|
238 | */ |
---|
239 | struct Scheduler_Control { |
---|
240 | /** |
---|
241 | * @brief Reference to a statically allocated scheduler context. |
---|
242 | */ |
---|
243 | Scheduler_Context *context; |
---|
244 | |
---|
245 | /** |
---|
246 | * @brief The scheduler operations. |
---|
247 | */ |
---|
248 | Scheduler_Operations Operations; |
---|
249 | |
---|
250 | /** |
---|
251 | * @brief The maximum priority value of this scheduler. |
---|
252 | * |
---|
253 | * It defines the lowest (least important) thread priority for this |
---|
254 | * scheduler. For example the idle threads have this priority. |
---|
255 | */ |
---|
256 | Priority_Control maximum_priority; |
---|
257 | |
---|
258 | /** |
---|
259 | * @brief The scheduler name. |
---|
260 | */ |
---|
261 | uint32_t name; |
---|
262 | }; |
---|
263 | |
---|
264 | /** |
---|
265 | * @brief Registered schedulers. |
---|
266 | * |
---|
267 | * Application provided via <rtems/confdefs.h>. |
---|
268 | * |
---|
269 | * @see _Scheduler_Count. |
---|
270 | */ |
---|
271 | extern const Scheduler_Control _Scheduler_Table[]; |
---|
272 | |
---|
273 | /** |
---|
274 | * @brief Count of registered schedulers. |
---|
275 | * |
---|
276 | * Application provided via <rtems/confdefs.h> on SMP configurations. |
---|
277 | * |
---|
278 | * It is very important that this is a compile-time constant on uni-processor |
---|
279 | * configurations (in this case RTEMS_SMP is not defined) so that the compiler |
---|
280 | * can optimize the some loops away |
---|
281 | * |
---|
282 | * @see _Scheduler_Table. |
---|
283 | */ |
---|
284 | #if defined(RTEMS_SMP) |
---|
285 | extern const size_t _Scheduler_Count; |
---|
286 | #else |
---|
287 | #define _Scheduler_Count ( (size_t) 1 ) |
---|
288 | #endif |
---|
289 | |
---|
290 | #if defined(RTEMS_SMP) |
---|
291 | /** |
---|
292 | * @brief The scheduler assignment default attributes. |
---|
293 | */ |
---|
294 | #define SCHEDULER_ASSIGN_DEFAULT UINT32_C(0x0) |
---|
295 | |
---|
296 | /** |
---|
297 | * @brief The presence of this processor is optional. |
---|
298 | */ |
---|
299 | #define SCHEDULER_ASSIGN_PROCESSOR_OPTIONAL SCHEDULER_ASSIGN_DEFAULT |
---|
300 | |
---|
301 | /** |
---|
302 | * @brief The presence of this processor is mandatory. |
---|
303 | */ |
---|
304 | #define SCHEDULER_ASSIGN_PROCESSOR_MANDATORY UINT32_C(0x1) |
---|
305 | |
---|
306 | /** |
---|
307 | * @brief Scheduler assignment. |
---|
308 | */ |
---|
309 | typedef struct { |
---|
310 | /** |
---|
311 | * @brief The scheduler for this processor. |
---|
312 | */ |
---|
313 | const Scheduler_Control *scheduler; |
---|
314 | |
---|
315 | /** |
---|
316 | * @brief The scheduler assignment attributes. |
---|
317 | * |
---|
318 | * Use @ref SCHEDULER_ASSIGN_DEFAULT to select default attributes. |
---|
319 | * |
---|
320 | * The presence of a processor can be |
---|
321 | * - @ref SCHEDULER_ASSIGN_PROCESSOR_OPTIONAL, or |
---|
322 | * - @ref SCHEDULER_ASSIGN_PROCESSOR_MANDATORY. |
---|
323 | */ |
---|
324 | uint32_t attributes; |
---|
325 | } Scheduler_Assignment; |
---|
326 | |
---|
327 | /** |
---|
328 | * @brief The scheduler assignments. |
---|
329 | * |
---|
330 | * The length of this array must be equal to the maximum processors. |
---|
331 | * |
---|
332 | * Application provided via <rtems/confdefs.h>. |
---|
333 | * |
---|
334 | * @see _Scheduler_Table and rtems_configuration_get_maximum_processors(). |
---|
335 | */ |
---|
336 | extern const Scheduler_Assignment _Scheduler_Initial_assignments[]; |
---|
337 | #endif |
---|
338 | |
---|
339 | /** |
---|
340 | * @brief Returns the thread priority. |
---|
341 | * |
---|
342 | * @param[in] scheduler Unused. |
---|
343 | * @param[in] priority The thread priority. |
---|
344 | * |
---|
345 | * @return priority The thread priority. |
---|
346 | */ |
---|
347 | Priority_Control _Scheduler_default_Map_priority( |
---|
348 | const Scheduler_Control *scheduler, |
---|
349 | Priority_Control priority |
---|
350 | ); |
---|
351 | |
---|
352 | #define _Scheduler_default_Unmap_priority _Scheduler_default_Map_priority |
---|
353 | |
---|
354 | #if defined(RTEMS_SMP) |
---|
355 | /** |
---|
356 | * @brief Does nothing. |
---|
357 | * |
---|
358 | * @param[in] scheduler Unused. |
---|
359 | * @param[in] the_thread Unused. |
---|
360 | * @param[in] node Unused. |
---|
361 | * |
---|
362 | * @retval false Always. |
---|
363 | */ |
---|
364 | bool _Scheduler_default_Ask_for_help( |
---|
365 | const Scheduler_Control *scheduler, |
---|
366 | Thread_Control *the_thread, |
---|
367 | Scheduler_Node *node |
---|
368 | ); |
---|
369 | |
---|
370 | /** |
---|
371 | * @brief Does nothing. |
---|
372 | * |
---|
373 | * @param[in] scheduler Unused. |
---|
374 | * @param[in] the_thread Unused. |
---|
375 | * @param[in] node Unused. |
---|
376 | */ |
---|
377 | void _Scheduler_default_Reconsider_help_request( |
---|
378 | const Scheduler_Control *scheduler, |
---|
379 | Thread_Control *the_thread, |
---|
380 | Scheduler_Node *node |
---|
381 | ); |
---|
382 | |
---|
383 | /** |
---|
384 | * @brief Does nothing. |
---|
385 | * |
---|
386 | * @param[in] scheduler Unused. |
---|
387 | * @param[in] the_thread Unused. |
---|
388 | * @param[in] node Unused. |
---|
389 | * @param[in] next_state Unused. |
---|
390 | */ |
---|
391 | void _Scheduler_default_Withdraw_node( |
---|
392 | const Scheduler_Control *scheduler, |
---|
393 | Thread_Control *the_thread, |
---|
394 | Scheduler_Node *node, |
---|
395 | Thread_Scheduler_state next_state |
---|
396 | ); |
---|
397 | |
---|
398 | #define SCHEDULER_OPERATION_DEFAULT_ASK_FOR_HELP \ |
---|
399 | _Scheduler_default_Ask_for_help, \ |
---|
400 | _Scheduler_default_Reconsider_help_request, \ |
---|
401 | _Scheduler_default_Withdraw_node, \ |
---|
402 | NULL, \ |
---|
403 | NULL, |
---|
404 | #else |
---|
405 | #define SCHEDULER_OPERATION_DEFAULT_ASK_FOR_HELP |
---|
406 | #endif |
---|
407 | |
---|
408 | /** |
---|
409 | * @brief Does nothing. |
---|
410 | * |
---|
411 | * @param[in] scheduler Unused. |
---|
412 | * @param[in] the_thread Unused. |
---|
413 | */ |
---|
414 | void _Scheduler_default_Schedule( |
---|
415 | const Scheduler_Control *scheduler, |
---|
416 | Thread_Control *the_thread |
---|
417 | ); |
---|
418 | |
---|
419 | /** |
---|
420 | * @brief Performs the scheduler base node initialization. |
---|
421 | * |
---|
422 | * @param[in] scheduler Unused. |
---|
423 | * @param[in] node The node to initialize. |
---|
424 | * @param[in] the_thread Unused. |
---|
425 | * @param[in] priority The thread priority. |
---|
426 | */ |
---|
427 | void _Scheduler_default_Node_initialize( |
---|
428 | const Scheduler_Control *scheduler, |
---|
429 | Scheduler_Node *node, |
---|
430 | Thread_Control *the_thread, |
---|
431 | Priority_Control priority |
---|
432 | ); |
---|
433 | |
---|
434 | /** |
---|
435 | * @brief Does nothing. |
---|
436 | * |
---|
437 | * @param[in] scheduler Unused. |
---|
438 | * @param[in] node Unused. |
---|
439 | */ |
---|
440 | void _Scheduler_default_Node_destroy( |
---|
441 | const Scheduler_Control *scheduler, |
---|
442 | Scheduler_Node *node |
---|
443 | ); |
---|
444 | |
---|
445 | /** |
---|
446 | * @brief Does nothing. |
---|
447 | * |
---|
448 | * @param[in] scheduler Unused. |
---|
449 | * @param[in] the_thread Unused. |
---|
450 | * @param[in] priority_node Unused. |
---|
451 | * @param[in] deadline Unused. |
---|
452 | * @param[in] queue_context Unused. |
---|
453 | * |
---|
454 | * @retval NULL Always. |
---|
455 | */ |
---|
456 | void _Scheduler_default_Release_job( |
---|
457 | const Scheduler_Control *scheduler, |
---|
458 | Thread_Control *the_thread, |
---|
459 | Priority_Node *priority_node, |
---|
460 | uint64_t deadline, |
---|
461 | Thread_queue_Context *queue_context |
---|
462 | ); |
---|
463 | |
---|
464 | /** |
---|
465 | * @brief Does nothing. |
---|
466 | * |
---|
467 | * @param[in] scheduler Unused. |
---|
468 | * @param[in] the_thread Unused. |
---|
469 | * @param[in] priority_node Unused. |
---|
470 | * @param[in] queue_context Unused. |
---|
471 | * |
---|
472 | * @retval NULL Always. |
---|
473 | */ |
---|
474 | void _Scheduler_default_Cancel_job( |
---|
475 | const Scheduler_Control *scheduler, |
---|
476 | Thread_Control *the_thread, |
---|
477 | Priority_Node *priority_node, |
---|
478 | Thread_queue_Context *queue_context |
---|
479 | ); |
---|
480 | |
---|
481 | /** |
---|
482 | * @brief Performs tick operations depending on the CPU budget algorithm for |
---|
483 | * each executing thread. |
---|
484 | * |
---|
485 | * This routine is invoked as part of processing each clock tick. |
---|
486 | * |
---|
487 | * @param[in] scheduler The scheduler. |
---|
488 | * @param[in] executing An executing thread. |
---|
489 | */ |
---|
490 | void _Scheduler_default_Tick( |
---|
491 | const Scheduler_Control *scheduler, |
---|
492 | Thread_Control *executing |
---|
493 | ); |
---|
494 | |
---|
495 | /** |
---|
496 | * @brief Starts an idle thread. |
---|
497 | * |
---|
498 | * @param[in] scheduler The scheduler. |
---|
499 | * @param[in] the_thread An idle thread. |
---|
500 | * @param[in] cpu This parameter is unused. |
---|
501 | */ |
---|
502 | void _Scheduler_default_Start_idle( |
---|
503 | const Scheduler_Control *scheduler, |
---|
504 | Thread_Control *the_thread, |
---|
505 | struct Per_CPU_Control *cpu |
---|
506 | ); |
---|
507 | |
---|
508 | #if defined(RTEMS_SMP) |
---|
509 | /** |
---|
510 | * @brief Default implementation of the set affinity scheduler operation. |
---|
511 | * |
---|
512 | * @param[in] scheduler The scheduler instance. |
---|
513 | * @param[in] thread The associated thread. |
---|
514 | * @param[in] node The home scheduler node of the associated thread. |
---|
515 | * @param[in] affinity The new processor affinity set for the thread. |
---|
516 | * |
---|
517 | * @retval true The processor set of the scheduler is a subset of the affinity set. |
---|
518 | * @retval false Otherwise. |
---|
519 | */ |
---|
520 | bool _Scheduler_default_Set_affinity( |
---|
521 | const Scheduler_Control *scheduler, |
---|
522 | Thread_Control *thread, |
---|
523 | Scheduler_Node *node, |
---|
524 | const Processor_mask *affinity |
---|
525 | ); |
---|
526 | |
---|
527 | #define SCHEDULER_OPERATION_DEFAULT_GET_SET_AFFINITY \ |
---|
528 | , _Scheduler_default_Set_affinity |
---|
529 | #else |
---|
530 | #define SCHEDULER_OPERATION_DEFAULT_GET_SET_AFFINITY |
---|
531 | #endif |
---|
532 | |
---|
533 | /** |
---|
534 | * @brief This defines the lowest (least important) thread priority of the |
---|
535 | * first scheduler instance. |
---|
536 | */ |
---|
537 | #define PRIORITY_MAXIMUM ( _Scheduler_Table[ 0 ].maximum_priority ) |
---|
538 | |
---|
539 | /**@}*/ |
---|
540 | |
---|
541 | #ifdef __cplusplus |
---|
542 | } |
---|
543 | #endif |
---|
544 | |
---|
545 | #endif |
---|
546 | /* end of include file */ |
---|