1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreSchedulerSMPEDF |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Scheduler_EDF_SMP_Add_processor(), _Scheduler_EDF_SMP_Ask_for_help(), |
---|
10 | * _Scheduler_EDF_SMP_Block(), _Scheduler_EDF_SMP_Initialize(), |
---|
11 | * _Scheduler_EDF_SMP_Node_initialize(), _Scheduler_EDF_SMP_Pin(), |
---|
12 | * _Scheduler_EDF_SMP_Reconsider_help_request(), |
---|
13 | * _Scheduler_EDF_SMP_Remove_processor(), _Scheduler_EDF_SMP_Set_affinity(), |
---|
14 | * _Scheduler_EDF_SMP_Start_idle(), _Scheduler_EDF_SMP_Unblock(), |
---|
15 | * _Scheduler_EDF_SMP_Unpin(), _Scheduler_EDF_SMP_Update_priority(), |
---|
16 | * _Scheduler_EDF_SMP_Withdraw_node(), _Scheduler_EDF_SMP_Make_sticky(), |
---|
17 | * _Scheduler_EDF_SMP_Clean_sticky(), and _Scheduler_EDF_SMP_Yield(). |
---|
18 | */ |
---|
19 | |
---|
20 | /* |
---|
21 | * Copyright (c) 2017 embedded brains GmbH & Co. KG |
---|
22 | * |
---|
23 | * Redistribution and use in source and binary forms, with or without |
---|
24 | * modification, are permitted provided that the following conditions |
---|
25 | * are met: |
---|
26 | * 1. Redistributions of source code must retain the above copyright |
---|
27 | * notice, this list of conditions and the following disclaimer. |
---|
28 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
29 | * notice, this list of conditions and the following disclaimer in the |
---|
30 | * documentation and/or other materials provided with the distribution. |
---|
31 | * |
---|
32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
33 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
34 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
35 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
36 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
37 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
38 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
39 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
40 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
41 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
42 | * POSSIBILITY OF SUCH DAMAGE. |
---|
43 | */ |
---|
44 | |
---|
45 | #ifdef HAVE_CONFIG_H |
---|
46 | #include "config.h" |
---|
47 | #endif |
---|
48 | |
---|
49 | #include <rtems/score/scheduleredfsmp.h> |
---|
50 | #include <rtems/score/schedulersmpimpl.h> |
---|
51 | |
---|
52 | static inline Scheduler_EDF_SMP_Context * |
---|
53 | _Scheduler_EDF_SMP_Get_context( const Scheduler_Control *scheduler ) |
---|
54 | { |
---|
55 | return (Scheduler_EDF_SMP_Context *) _Scheduler_Get_context( scheduler ); |
---|
56 | } |
---|
57 | |
---|
58 | static inline Scheduler_EDF_SMP_Context * |
---|
59 | _Scheduler_EDF_SMP_Get_self( Scheduler_Context *context ) |
---|
60 | { |
---|
61 | return (Scheduler_EDF_SMP_Context *) context; |
---|
62 | } |
---|
63 | |
---|
64 | static inline Scheduler_EDF_SMP_Node * |
---|
65 | _Scheduler_EDF_SMP_Node_downcast( Scheduler_Node *node ) |
---|
66 | { |
---|
67 | return (Scheduler_EDF_SMP_Node *) node; |
---|
68 | } |
---|
69 | |
---|
70 | static inline bool _Scheduler_EDF_SMP_Priority_less_equal( |
---|
71 | const void *left, |
---|
72 | const RBTree_Node *right |
---|
73 | ) |
---|
74 | { |
---|
75 | const Priority_Control *the_left; |
---|
76 | const Scheduler_SMP_Node *the_right; |
---|
77 | Priority_Control prio_left; |
---|
78 | Priority_Control prio_right; |
---|
79 | |
---|
80 | the_left = left; |
---|
81 | the_right = RTEMS_CONTAINER_OF( right, Scheduler_SMP_Node, Base.Node.RBTree ); |
---|
82 | |
---|
83 | prio_left = *the_left; |
---|
84 | prio_right = the_right->priority; |
---|
85 | |
---|
86 | return prio_left <= prio_right; |
---|
87 | } |
---|
88 | |
---|
89 | static inline bool _Scheduler_EDF_SMP_Overall_less_equal( |
---|
90 | const void *key, |
---|
91 | const Chain_Node *to_insert, |
---|
92 | const Chain_Node *next |
---|
93 | ) |
---|
94 | { |
---|
95 | Priority_Control insert_priority; |
---|
96 | Priority_Control next_priority; |
---|
97 | const Scheduler_EDF_SMP_Node *node_to_insert; |
---|
98 | const Scheduler_EDF_SMP_Node *node_next; |
---|
99 | |
---|
100 | insert_priority = *(const Priority_Control *) key; |
---|
101 | insert_priority = SCHEDULER_PRIORITY_PURIFY( insert_priority ); |
---|
102 | node_to_insert = (const Scheduler_EDF_SMP_Node *) to_insert; |
---|
103 | node_next = (const Scheduler_EDF_SMP_Node *) next; |
---|
104 | next_priority = node_next->Base.priority; |
---|
105 | |
---|
106 | return insert_priority < next_priority || |
---|
107 | ( insert_priority == next_priority && |
---|
108 | node_to_insert->generation <= node_next->generation ); |
---|
109 | } |
---|
110 | |
---|
111 | void _Scheduler_EDF_SMP_Initialize( const Scheduler_Control *scheduler ) |
---|
112 | { |
---|
113 | Scheduler_EDF_SMP_Context *self = |
---|
114 | _Scheduler_EDF_SMP_Get_context( scheduler ); |
---|
115 | |
---|
116 | _Scheduler_SMP_Initialize( &self->Base ); |
---|
117 | _Chain_Initialize_empty( &self->Affine_queues ); |
---|
118 | /* The ready queues are zero initialized and thus empty */ |
---|
119 | } |
---|
120 | |
---|
121 | void _Scheduler_EDF_SMP_Node_initialize( |
---|
122 | const Scheduler_Control *scheduler, |
---|
123 | Scheduler_Node *node, |
---|
124 | Thread_Control *the_thread, |
---|
125 | Priority_Control priority |
---|
126 | ) |
---|
127 | { |
---|
128 | Scheduler_SMP_Node *smp_node; |
---|
129 | |
---|
130 | smp_node = _Scheduler_SMP_Node_downcast( node ); |
---|
131 | _Scheduler_SMP_Node_initialize( scheduler, smp_node, the_thread, priority ); |
---|
132 | } |
---|
133 | |
---|
134 | static inline void _Scheduler_EDF_SMP_Do_update( |
---|
135 | Scheduler_Context *context, |
---|
136 | Scheduler_Node *node, |
---|
137 | Priority_Control new_priority |
---|
138 | ) |
---|
139 | { |
---|
140 | Scheduler_SMP_Node *smp_node; |
---|
141 | |
---|
142 | (void) context; |
---|
143 | |
---|
144 | smp_node = _Scheduler_SMP_Node_downcast( node ); |
---|
145 | _Scheduler_SMP_Node_update_priority( smp_node, new_priority ); |
---|
146 | } |
---|
147 | |
---|
148 | static inline bool _Scheduler_EDF_SMP_Has_ready( Scheduler_Context *context ) |
---|
149 | { |
---|
150 | Scheduler_EDF_SMP_Context *self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
151 | |
---|
152 | return !_RBTree_Is_empty( &self->Ready[ 0 ].Queue ); |
---|
153 | } |
---|
154 | |
---|
155 | static inline bool _Scheduler_EDF_SMP_Overall_less( |
---|
156 | const Scheduler_EDF_SMP_Node *left, |
---|
157 | const Scheduler_EDF_SMP_Node *right |
---|
158 | ) |
---|
159 | { |
---|
160 | Priority_Control lp; |
---|
161 | Priority_Control rp; |
---|
162 | |
---|
163 | lp = left->Base.priority; |
---|
164 | rp = right->Base.priority; |
---|
165 | |
---|
166 | return lp < rp || (lp == rp && left->generation < right->generation ); |
---|
167 | } |
---|
168 | |
---|
169 | static inline Scheduler_EDF_SMP_Node * |
---|
170 | _Scheduler_EDF_SMP_Challenge_highest_ready( |
---|
171 | Scheduler_EDF_SMP_Context *self, |
---|
172 | Scheduler_EDF_SMP_Node *highest_ready, |
---|
173 | RBTree_Control *ready_queue |
---|
174 | ) |
---|
175 | { |
---|
176 | Scheduler_EDF_SMP_Node *other; |
---|
177 | |
---|
178 | other = (Scheduler_EDF_SMP_Node *) _RBTree_Minimum( ready_queue ); |
---|
179 | _Assert( other != NULL ); |
---|
180 | |
---|
181 | if ( _Scheduler_EDF_SMP_Overall_less( other, highest_ready ) ) { |
---|
182 | return other; |
---|
183 | } |
---|
184 | |
---|
185 | return highest_ready; |
---|
186 | } |
---|
187 | |
---|
188 | static inline Scheduler_Node *_Scheduler_EDF_SMP_Get_highest_ready( |
---|
189 | Scheduler_Context *context, |
---|
190 | Scheduler_Node *filter |
---|
191 | ) |
---|
192 | { |
---|
193 | Scheduler_EDF_SMP_Context *self; |
---|
194 | Scheduler_EDF_SMP_Node *highest_ready; |
---|
195 | Scheduler_EDF_SMP_Node *node; |
---|
196 | uint8_t rqi; |
---|
197 | const Chain_Node *tail; |
---|
198 | Chain_Node *next; |
---|
199 | |
---|
200 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
201 | highest_ready = (Scheduler_EDF_SMP_Node *) |
---|
202 | _RBTree_Minimum( &self->Ready[ 0 ].Queue ); |
---|
203 | _Assert( highest_ready != NULL ); |
---|
204 | |
---|
205 | /* |
---|
206 | * The filter node is a scheduled node which is no longer on the scheduled |
---|
207 | * chain. In case this is an affine thread, then we have to check the |
---|
208 | * corresponding affine ready queue. |
---|
209 | */ |
---|
210 | |
---|
211 | node = (Scheduler_EDF_SMP_Node *) filter; |
---|
212 | rqi = node->ready_queue_index; |
---|
213 | |
---|
214 | if ( rqi != 0 && !_RBTree_Is_empty( &self->Ready[ rqi ].Queue ) ) { |
---|
215 | highest_ready = _Scheduler_EDF_SMP_Challenge_highest_ready( |
---|
216 | self, |
---|
217 | highest_ready, |
---|
218 | &self->Ready[ rqi ].Queue |
---|
219 | ); |
---|
220 | } |
---|
221 | |
---|
222 | tail = _Chain_Immutable_tail( &self->Affine_queues ); |
---|
223 | next = _Chain_First( &self->Affine_queues ); |
---|
224 | |
---|
225 | while ( next != tail ) { |
---|
226 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
227 | |
---|
228 | ready_queue = (Scheduler_EDF_SMP_Ready_queue *) next; |
---|
229 | highest_ready = _Scheduler_EDF_SMP_Challenge_highest_ready( |
---|
230 | self, |
---|
231 | highest_ready, |
---|
232 | &ready_queue->Queue |
---|
233 | ); |
---|
234 | |
---|
235 | next = _Chain_Next( next ); |
---|
236 | } |
---|
237 | |
---|
238 | return &highest_ready->Base.Base; |
---|
239 | } |
---|
240 | |
---|
241 | static inline void _Scheduler_EDF_SMP_Set_allocated( |
---|
242 | Scheduler_EDF_SMP_Context *self, |
---|
243 | Scheduler_EDF_SMP_Node *allocated, |
---|
244 | const Per_CPU_Control *cpu |
---|
245 | ) |
---|
246 | { |
---|
247 | self->Ready[ _Per_CPU_Get_index( cpu ) + 1 ].allocated = allocated; |
---|
248 | } |
---|
249 | |
---|
250 | static inline Scheduler_EDF_SMP_Node *_Scheduler_EDF_SMP_Get_allocated( |
---|
251 | const Scheduler_EDF_SMP_Context *self, |
---|
252 | uint8_t rqi |
---|
253 | ) |
---|
254 | { |
---|
255 | return self->Ready[ rqi ].allocated; |
---|
256 | } |
---|
257 | |
---|
258 | static inline Scheduler_Node *_Scheduler_EDF_SMP_Get_lowest_scheduled( |
---|
259 | Scheduler_Context *context, |
---|
260 | Scheduler_Node *filter_base |
---|
261 | ) |
---|
262 | { |
---|
263 | Scheduler_EDF_SMP_Node *filter; |
---|
264 | uint8_t rqi; |
---|
265 | |
---|
266 | filter = _Scheduler_EDF_SMP_Node_downcast( filter_base ); |
---|
267 | rqi = filter->ready_queue_index; |
---|
268 | |
---|
269 | if ( rqi != 0 ) { |
---|
270 | Scheduler_EDF_SMP_Context *self; |
---|
271 | Scheduler_EDF_SMP_Node *affine_scheduled; |
---|
272 | |
---|
273 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
274 | affine_scheduled = self->Ready[ rqi ].affine_scheduled; |
---|
275 | |
---|
276 | if ( affine_scheduled != NULL ) { |
---|
277 | _Assert( affine_scheduled->ready_queue_index == rqi ); |
---|
278 | return &affine_scheduled->Base.Base; |
---|
279 | } |
---|
280 | } |
---|
281 | |
---|
282 | return _Scheduler_SMP_Get_lowest_scheduled( context, filter_base ); |
---|
283 | } |
---|
284 | |
---|
285 | static inline void _Scheduler_EDF_SMP_Update_generation( |
---|
286 | Scheduler_Context *context, |
---|
287 | Scheduler_Node *node_base, |
---|
288 | Priority_Control insert_priority |
---|
289 | ) |
---|
290 | { |
---|
291 | Scheduler_EDF_SMP_Context *self; |
---|
292 | Scheduler_EDF_SMP_Node *node; |
---|
293 | int generation_index; |
---|
294 | int increment; |
---|
295 | int64_t generation; |
---|
296 | |
---|
297 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
298 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
299 | generation_index = SCHEDULER_PRIORITY_IS_APPEND( insert_priority ); |
---|
300 | increment = ( generation_index << 1 ) - 1; |
---|
301 | |
---|
302 | generation = self->generations[ generation_index ]; |
---|
303 | node->generation = generation; |
---|
304 | self->generations[ generation_index ] = generation + increment; |
---|
305 | } |
---|
306 | |
---|
307 | static inline void _Scheduler_EDF_SMP_Insert_scheduled( |
---|
308 | Scheduler_Context *context, |
---|
309 | Scheduler_Node *node_base, |
---|
310 | Priority_Control priority_to_insert |
---|
311 | ) |
---|
312 | { |
---|
313 | Scheduler_EDF_SMP_Context *self; |
---|
314 | Scheduler_EDF_SMP_Node *node; |
---|
315 | uint8_t rqi; |
---|
316 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
317 | |
---|
318 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
319 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
320 | rqi = node->ready_queue_index; |
---|
321 | ready_queue = &self->Ready[ rqi ]; |
---|
322 | |
---|
323 | _Chain_Insert_ordered_unprotected( |
---|
324 | &self->Base.Scheduled, |
---|
325 | &node_base->Node.Chain, |
---|
326 | &priority_to_insert, |
---|
327 | _Scheduler_EDF_SMP_Overall_less_equal |
---|
328 | ); |
---|
329 | |
---|
330 | if ( rqi != 0 ) { |
---|
331 | ready_queue->affine_scheduled = node; |
---|
332 | |
---|
333 | if ( !_RBTree_Is_empty( &ready_queue->Queue ) ) { |
---|
334 | _Chain_Extract_unprotected( &ready_queue->Node ); |
---|
335 | } |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | static inline void _Scheduler_EDF_SMP_Activate_ready_queue_if_necessary( |
---|
340 | Scheduler_EDF_SMP_Context *self, |
---|
341 | uint8_t rqi, |
---|
342 | Scheduler_EDF_SMP_Ready_queue *ready_queue |
---|
343 | ) |
---|
344 | { |
---|
345 | if ( |
---|
346 | rqi != 0 && |
---|
347 | _RBTree_Is_empty( &ready_queue->Queue ) && |
---|
348 | ready_queue->affine_scheduled == NULL |
---|
349 | ) { |
---|
350 | _Chain_Append_unprotected( &self->Affine_queues, &ready_queue->Node ); |
---|
351 | } |
---|
352 | } |
---|
353 | |
---|
354 | static inline void _Scheduler_EDF_SMP_Insert_ready( |
---|
355 | Scheduler_Context *context, |
---|
356 | Scheduler_Node *node_base, |
---|
357 | Priority_Control insert_priority |
---|
358 | ) |
---|
359 | { |
---|
360 | Scheduler_EDF_SMP_Context *self; |
---|
361 | Scheduler_EDF_SMP_Node *node; |
---|
362 | uint8_t rqi; |
---|
363 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
364 | |
---|
365 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
366 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
367 | rqi = node->ready_queue_index; |
---|
368 | ready_queue = &self->Ready[ rqi ]; |
---|
369 | |
---|
370 | _Scheduler_EDF_SMP_Activate_ready_queue_if_necessary( self, rqi, ready_queue ); |
---|
371 | _RBTree_Initialize_node( &node->Base.Base.Node.RBTree ); |
---|
372 | _RBTree_Insert_inline( |
---|
373 | &ready_queue->Queue, |
---|
374 | &node->Base.Base.Node.RBTree, |
---|
375 | &insert_priority, |
---|
376 | _Scheduler_EDF_SMP_Priority_less_equal |
---|
377 | ); |
---|
378 | } |
---|
379 | |
---|
380 | static inline void _Scheduler_EDF_SMP_Extract_from_scheduled( |
---|
381 | Scheduler_Context *context, |
---|
382 | Scheduler_Node *node_to_extract |
---|
383 | ) |
---|
384 | { |
---|
385 | Scheduler_EDF_SMP_Context *self; |
---|
386 | Scheduler_EDF_SMP_Node *node; |
---|
387 | uint8_t rqi; |
---|
388 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
389 | |
---|
390 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
391 | node = _Scheduler_EDF_SMP_Node_downcast( node_to_extract ); |
---|
392 | |
---|
393 | _Scheduler_SMP_Extract_from_scheduled( &self->Base.Base, &node->Base.Base ); |
---|
394 | |
---|
395 | rqi = node->ready_queue_index; |
---|
396 | ready_queue = &self->Ready[ rqi ]; |
---|
397 | |
---|
398 | if ( rqi != 0 && !_RBTree_Is_empty( &ready_queue->Queue ) ) { |
---|
399 | _Chain_Append_unprotected( &self->Affine_queues, &ready_queue->Node ); |
---|
400 | } |
---|
401 | |
---|
402 | ready_queue->affine_scheduled = NULL; |
---|
403 | } |
---|
404 | |
---|
405 | static inline void _Scheduler_EDF_SMP_Extract_from_ready( |
---|
406 | Scheduler_Context *context, |
---|
407 | Scheduler_Node *node_to_extract |
---|
408 | ) |
---|
409 | { |
---|
410 | Scheduler_EDF_SMP_Context *self; |
---|
411 | Scheduler_EDF_SMP_Node *node; |
---|
412 | uint8_t rqi; |
---|
413 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
414 | |
---|
415 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
416 | node = _Scheduler_EDF_SMP_Node_downcast( node_to_extract ); |
---|
417 | rqi = node->ready_queue_index; |
---|
418 | ready_queue = &self->Ready[ rqi ]; |
---|
419 | |
---|
420 | _RBTree_Extract( &ready_queue->Queue, &node->Base.Base.Node.RBTree ); |
---|
421 | _Chain_Initialize_node( &node->Base.Base.Node.Chain ); |
---|
422 | |
---|
423 | if ( |
---|
424 | rqi != 0 |
---|
425 | && _RBTree_Is_empty( &ready_queue->Queue ) |
---|
426 | && ready_queue->affine_scheduled == NULL |
---|
427 | ) { |
---|
428 | _Chain_Extract_unprotected( &ready_queue->Node ); |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | static inline void _Scheduler_EDF_SMP_Move_from_scheduled_to_ready( |
---|
433 | Scheduler_Context *context, |
---|
434 | Scheduler_Node *scheduled_to_ready |
---|
435 | ) |
---|
436 | { |
---|
437 | Scheduler_EDF_SMP_Context *self; |
---|
438 | Scheduler_EDF_SMP_Node *node; |
---|
439 | uint8_t rqi; |
---|
440 | Scheduler_EDF_SMP_Ready_queue *ready_queue; |
---|
441 | |
---|
442 | _Scheduler_EDF_SMP_Extract_from_scheduled( context, scheduled_to_ready ); |
---|
443 | |
---|
444 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
445 | node = _Scheduler_EDF_SMP_Node_downcast( scheduled_to_ready ); |
---|
446 | rqi = node->ready_queue_index; |
---|
447 | ready_queue = &self->Ready[ rqi ]; |
---|
448 | |
---|
449 | _Scheduler_EDF_SMP_Activate_ready_queue_if_necessary( self, rqi, ready_queue ); |
---|
450 | _RBTree_Initialize_node( &node->Base.Base.Node.RBTree ); |
---|
451 | _RBTree_Prepend( &ready_queue->Queue, &node->Base.Base.Node.RBTree ); |
---|
452 | } |
---|
453 | |
---|
454 | static inline void _Scheduler_EDF_SMP_Move_from_ready_to_scheduled( |
---|
455 | Scheduler_Context *context, |
---|
456 | Scheduler_Node *ready_to_scheduled |
---|
457 | ) |
---|
458 | { |
---|
459 | Priority_Control insert_priority; |
---|
460 | |
---|
461 | _Scheduler_EDF_SMP_Extract_from_ready( context, ready_to_scheduled ); |
---|
462 | insert_priority = _Scheduler_SMP_Node_priority( ready_to_scheduled ); |
---|
463 | insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority ); |
---|
464 | _Scheduler_EDF_SMP_Insert_scheduled( |
---|
465 | context, |
---|
466 | ready_to_scheduled, |
---|
467 | insert_priority |
---|
468 | ); |
---|
469 | } |
---|
470 | |
---|
471 | static inline Scheduler_Node *_Scheduler_EDF_SMP_Get_idle( void *arg ) |
---|
472 | { |
---|
473 | Scheduler_EDF_SMP_Context *self; |
---|
474 | Scheduler_Node *lowest_ready; |
---|
475 | |
---|
476 | self = _Scheduler_EDF_SMP_Get_self( arg ); |
---|
477 | lowest_ready = (Scheduler_Node *) _RBTree_Maximum( &self->Ready[ 0 ].Queue ); |
---|
478 | _Assert( lowest_ready != NULL ); |
---|
479 | _RBTree_Extract( &self->Ready[ 0 ].Queue, &lowest_ready->Node.RBTree ); |
---|
480 | _Chain_Initialize_node( &lowest_ready->Node.Chain ); |
---|
481 | |
---|
482 | return lowest_ready; |
---|
483 | } |
---|
484 | |
---|
485 | static inline void _Scheduler_EDF_SMP_Release_idle( |
---|
486 | Scheduler_Node *node, |
---|
487 | void *arg |
---|
488 | ) |
---|
489 | { |
---|
490 | Scheduler_EDF_SMP_Context *self; |
---|
491 | |
---|
492 | self = _Scheduler_EDF_SMP_Get_self( arg ); |
---|
493 | _RBTree_Initialize_node( &node->Node.RBTree ); |
---|
494 | _RBTree_Append( &self->Ready[ 0 ].Queue, &node->Node.RBTree ); |
---|
495 | } |
---|
496 | |
---|
497 | static inline void _Scheduler_EDF_SMP_Allocate_processor( |
---|
498 | Scheduler_Context *context, |
---|
499 | Scheduler_Node *scheduled_base, |
---|
500 | Per_CPU_Control *cpu |
---|
501 | ) |
---|
502 | { |
---|
503 | Scheduler_EDF_SMP_Context *self; |
---|
504 | Scheduler_EDF_SMP_Node *scheduled; |
---|
505 | uint8_t rqi; |
---|
506 | |
---|
507 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
508 | scheduled = _Scheduler_EDF_SMP_Node_downcast( scheduled_base ); |
---|
509 | rqi = scheduled->ready_queue_index; |
---|
510 | |
---|
511 | if ( rqi != 0 ) { |
---|
512 | Per_CPU_Control *affine_cpu; |
---|
513 | |
---|
514 | affine_cpu = _Per_CPU_Get_by_index( rqi - 1 ); |
---|
515 | |
---|
516 | if ( cpu != affine_cpu ) { |
---|
517 | Scheduler_EDF_SMP_Node *node; |
---|
518 | |
---|
519 | node = _Scheduler_EDF_SMP_Get_allocated( self, rqi ); |
---|
520 | _Assert( node->ready_queue_index == 0 ); |
---|
521 | _Scheduler_EDF_SMP_Set_allocated( self, node, cpu ); |
---|
522 | _Scheduler_SMP_Allocate_processor_exact( |
---|
523 | context, |
---|
524 | &node->Base.Base, |
---|
525 | cpu |
---|
526 | ); |
---|
527 | cpu = affine_cpu; |
---|
528 | } |
---|
529 | } |
---|
530 | |
---|
531 | _Scheduler_EDF_SMP_Set_allocated( self, scheduled, cpu ); |
---|
532 | _Scheduler_SMP_Allocate_processor_exact( |
---|
533 | context, |
---|
534 | &scheduled->Base.Base, |
---|
535 | cpu |
---|
536 | ); |
---|
537 | } |
---|
538 | |
---|
539 | void _Scheduler_EDF_SMP_Block( |
---|
540 | const Scheduler_Control *scheduler, |
---|
541 | Thread_Control *thread, |
---|
542 | Scheduler_Node *node |
---|
543 | ) |
---|
544 | { |
---|
545 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
546 | |
---|
547 | _Scheduler_SMP_Block( |
---|
548 | context, |
---|
549 | thread, |
---|
550 | node, |
---|
551 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
552 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
553 | _Scheduler_EDF_SMP_Get_highest_ready, |
---|
554 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
555 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
556 | _Scheduler_EDF_SMP_Get_idle |
---|
557 | ); |
---|
558 | } |
---|
559 | |
---|
560 | static inline bool _Scheduler_EDF_SMP_Enqueue( |
---|
561 | Scheduler_Context *context, |
---|
562 | Scheduler_Node *node, |
---|
563 | Priority_Control insert_priority |
---|
564 | ) |
---|
565 | { |
---|
566 | _Scheduler_EDF_SMP_Update_generation( context, node, insert_priority ); |
---|
567 | |
---|
568 | return _Scheduler_SMP_Enqueue( |
---|
569 | context, |
---|
570 | node, |
---|
571 | insert_priority, |
---|
572 | _Scheduler_EDF_SMP_Overall_less_equal, |
---|
573 | _Scheduler_EDF_SMP_Insert_ready, |
---|
574 | _Scheduler_EDF_SMP_Insert_scheduled, |
---|
575 | _Scheduler_EDF_SMP_Move_from_scheduled_to_ready, |
---|
576 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
577 | _Scheduler_EDF_SMP_Get_lowest_scheduled, |
---|
578 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
579 | _Scheduler_EDF_SMP_Get_idle, |
---|
580 | _Scheduler_EDF_SMP_Release_idle |
---|
581 | ); |
---|
582 | } |
---|
583 | |
---|
584 | static inline void _Scheduler_EDF_SMP_Enqueue_scheduled( |
---|
585 | Scheduler_Context *context, |
---|
586 | Scheduler_Node *node, |
---|
587 | Priority_Control insert_priority |
---|
588 | ) |
---|
589 | { |
---|
590 | _Scheduler_EDF_SMP_Update_generation( context, node, insert_priority ); |
---|
591 | _Scheduler_SMP_Enqueue_scheduled( |
---|
592 | context, |
---|
593 | node, |
---|
594 | insert_priority, |
---|
595 | _Scheduler_EDF_SMP_Overall_less_equal, |
---|
596 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
597 | _Scheduler_EDF_SMP_Get_highest_ready, |
---|
598 | _Scheduler_EDF_SMP_Insert_ready, |
---|
599 | _Scheduler_EDF_SMP_Insert_scheduled, |
---|
600 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
601 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
602 | _Scheduler_EDF_SMP_Get_idle, |
---|
603 | _Scheduler_EDF_SMP_Release_idle |
---|
604 | ); |
---|
605 | } |
---|
606 | |
---|
607 | void _Scheduler_EDF_SMP_Unblock( |
---|
608 | const Scheduler_Control *scheduler, |
---|
609 | Thread_Control *thread, |
---|
610 | Scheduler_Node *node |
---|
611 | ) |
---|
612 | { |
---|
613 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
614 | |
---|
615 | _Scheduler_SMP_Unblock( |
---|
616 | context, |
---|
617 | thread, |
---|
618 | node, |
---|
619 | _Scheduler_EDF_SMP_Do_update, |
---|
620 | _Scheduler_EDF_SMP_Enqueue, |
---|
621 | _Scheduler_EDF_SMP_Release_idle |
---|
622 | ); |
---|
623 | } |
---|
624 | |
---|
625 | static inline bool _Scheduler_EDF_SMP_Do_ask_for_help( |
---|
626 | Scheduler_Context *context, |
---|
627 | Thread_Control *the_thread, |
---|
628 | Scheduler_Node *node |
---|
629 | ) |
---|
630 | { |
---|
631 | return _Scheduler_SMP_Ask_for_help( |
---|
632 | context, |
---|
633 | the_thread, |
---|
634 | node, |
---|
635 | _Scheduler_EDF_SMP_Overall_less_equal, |
---|
636 | _Scheduler_EDF_SMP_Insert_ready, |
---|
637 | _Scheduler_EDF_SMP_Insert_scheduled, |
---|
638 | _Scheduler_EDF_SMP_Move_from_scheduled_to_ready, |
---|
639 | _Scheduler_EDF_SMP_Get_lowest_scheduled, |
---|
640 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
641 | _Scheduler_EDF_SMP_Release_idle |
---|
642 | ); |
---|
643 | } |
---|
644 | |
---|
645 | void _Scheduler_EDF_SMP_Update_priority( |
---|
646 | const Scheduler_Control *scheduler, |
---|
647 | Thread_Control *thread, |
---|
648 | Scheduler_Node *node |
---|
649 | ) |
---|
650 | { |
---|
651 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
652 | |
---|
653 | _Scheduler_SMP_Update_priority( |
---|
654 | context, |
---|
655 | thread, |
---|
656 | node, |
---|
657 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
658 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
659 | _Scheduler_EDF_SMP_Do_update, |
---|
660 | _Scheduler_EDF_SMP_Enqueue, |
---|
661 | _Scheduler_EDF_SMP_Enqueue_scheduled, |
---|
662 | _Scheduler_EDF_SMP_Do_ask_for_help |
---|
663 | ); |
---|
664 | } |
---|
665 | |
---|
666 | bool _Scheduler_EDF_SMP_Ask_for_help( |
---|
667 | const Scheduler_Control *scheduler, |
---|
668 | Thread_Control *the_thread, |
---|
669 | Scheduler_Node *node |
---|
670 | ) |
---|
671 | { |
---|
672 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
673 | |
---|
674 | return _Scheduler_EDF_SMP_Do_ask_for_help( context, the_thread, node ); |
---|
675 | } |
---|
676 | |
---|
677 | void _Scheduler_EDF_SMP_Reconsider_help_request( |
---|
678 | const Scheduler_Control *scheduler, |
---|
679 | Thread_Control *the_thread, |
---|
680 | Scheduler_Node *node |
---|
681 | ) |
---|
682 | { |
---|
683 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
684 | |
---|
685 | _Scheduler_SMP_Reconsider_help_request( |
---|
686 | context, |
---|
687 | the_thread, |
---|
688 | node, |
---|
689 | _Scheduler_EDF_SMP_Extract_from_ready |
---|
690 | ); |
---|
691 | } |
---|
692 | |
---|
693 | void _Scheduler_EDF_SMP_Withdraw_node( |
---|
694 | const Scheduler_Control *scheduler, |
---|
695 | Thread_Control *the_thread, |
---|
696 | Scheduler_Node *node, |
---|
697 | Thread_Scheduler_state next_state |
---|
698 | ) |
---|
699 | { |
---|
700 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
701 | |
---|
702 | _Scheduler_SMP_Withdraw_node( |
---|
703 | context, |
---|
704 | the_thread, |
---|
705 | node, |
---|
706 | next_state, |
---|
707 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
708 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
709 | _Scheduler_EDF_SMP_Get_highest_ready, |
---|
710 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
711 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
712 | _Scheduler_EDF_SMP_Get_idle |
---|
713 | ); |
---|
714 | } |
---|
715 | |
---|
716 | void _Scheduler_EDF_SMP_Make_sticky( |
---|
717 | const Scheduler_Control *scheduler, |
---|
718 | Thread_Control *the_thread, |
---|
719 | Scheduler_Node *node |
---|
720 | ) |
---|
721 | { |
---|
722 | _Scheduler_SMP_Make_sticky( |
---|
723 | scheduler, |
---|
724 | the_thread, |
---|
725 | node, |
---|
726 | _Scheduler_EDF_SMP_Do_update, |
---|
727 | _Scheduler_EDF_SMP_Enqueue |
---|
728 | ); |
---|
729 | } |
---|
730 | |
---|
731 | void _Scheduler_EDF_SMP_Clean_sticky( |
---|
732 | const Scheduler_Control *scheduler, |
---|
733 | Thread_Control *the_thread, |
---|
734 | Scheduler_Node *node |
---|
735 | ) |
---|
736 | { |
---|
737 | _Scheduler_SMP_Clean_sticky( |
---|
738 | scheduler, |
---|
739 | the_thread, |
---|
740 | node, |
---|
741 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
742 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
743 | _Scheduler_EDF_SMP_Get_highest_ready, |
---|
744 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
745 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
746 | _Scheduler_EDF_SMP_Get_idle, |
---|
747 | _Scheduler_EDF_SMP_Release_idle |
---|
748 | ); |
---|
749 | } |
---|
750 | |
---|
751 | static inline void _Scheduler_EDF_SMP_Register_idle( |
---|
752 | Scheduler_Context *context, |
---|
753 | Scheduler_Node *idle_base, |
---|
754 | Per_CPU_Control *cpu |
---|
755 | ) |
---|
756 | { |
---|
757 | Scheduler_EDF_SMP_Context *self; |
---|
758 | Scheduler_EDF_SMP_Node *idle; |
---|
759 | |
---|
760 | self = _Scheduler_EDF_SMP_Get_self( context ); |
---|
761 | idle = _Scheduler_EDF_SMP_Node_downcast( idle_base ); |
---|
762 | _Scheduler_EDF_SMP_Set_allocated( self, idle, cpu ); |
---|
763 | _Scheduler_EDF_SMP_Update_generation( |
---|
764 | context, |
---|
765 | idle_base, |
---|
766 | PRIORITY_GROUP_LAST |
---|
767 | ); |
---|
768 | } |
---|
769 | |
---|
770 | void _Scheduler_EDF_SMP_Add_processor( |
---|
771 | const Scheduler_Control *scheduler, |
---|
772 | Thread_Control *idle |
---|
773 | ) |
---|
774 | { |
---|
775 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
776 | |
---|
777 | _Scheduler_SMP_Add_processor( |
---|
778 | context, |
---|
779 | idle, |
---|
780 | _Scheduler_EDF_SMP_Has_ready, |
---|
781 | _Scheduler_EDF_SMP_Enqueue_scheduled, |
---|
782 | _Scheduler_EDF_SMP_Register_idle |
---|
783 | ); |
---|
784 | } |
---|
785 | |
---|
786 | Thread_Control *_Scheduler_EDF_SMP_Remove_processor( |
---|
787 | const Scheduler_Control *scheduler, |
---|
788 | Per_CPU_Control *cpu |
---|
789 | ) |
---|
790 | { |
---|
791 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
792 | |
---|
793 | return _Scheduler_SMP_Remove_processor( |
---|
794 | context, |
---|
795 | cpu, |
---|
796 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
797 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
798 | _Scheduler_EDF_SMP_Enqueue, |
---|
799 | _Scheduler_EDF_SMP_Get_idle, |
---|
800 | _Scheduler_EDF_SMP_Release_idle |
---|
801 | ); |
---|
802 | } |
---|
803 | |
---|
804 | void _Scheduler_EDF_SMP_Yield( |
---|
805 | const Scheduler_Control *scheduler, |
---|
806 | Thread_Control *thread, |
---|
807 | Scheduler_Node *node |
---|
808 | ) |
---|
809 | { |
---|
810 | Scheduler_Context *context = _Scheduler_Get_context( scheduler ); |
---|
811 | |
---|
812 | _Scheduler_SMP_Yield( |
---|
813 | context, |
---|
814 | thread, |
---|
815 | node, |
---|
816 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
817 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
818 | _Scheduler_EDF_SMP_Enqueue, |
---|
819 | _Scheduler_EDF_SMP_Enqueue_scheduled |
---|
820 | ); |
---|
821 | } |
---|
822 | |
---|
823 | static inline void _Scheduler_EDF_SMP_Do_set_affinity( |
---|
824 | Scheduler_Context *context, |
---|
825 | Scheduler_Node *node_base, |
---|
826 | void *arg |
---|
827 | ) |
---|
828 | { |
---|
829 | Scheduler_EDF_SMP_Node *node; |
---|
830 | const uint8_t *rqi; |
---|
831 | |
---|
832 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
833 | rqi = arg; |
---|
834 | node->ready_queue_index = *rqi; |
---|
835 | } |
---|
836 | |
---|
837 | void _Scheduler_EDF_SMP_Start_idle( |
---|
838 | const Scheduler_Control *scheduler, |
---|
839 | Thread_Control *idle, |
---|
840 | Per_CPU_Control *cpu |
---|
841 | ) |
---|
842 | { |
---|
843 | Scheduler_Context *context; |
---|
844 | |
---|
845 | context = _Scheduler_Get_context( scheduler ); |
---|
846 | |
---|
847 | _Scheduler_SMP_Do_start_idle( |
---|
848 | context, |
---|
849 | idle, |
---|
850 | cpu, |
---|
851 | _Scheduler_EDF_SMP_Register_idle |
---|
852 | ); |
---|
853 | } |
---|
854 | |
---|
855 | void _Scheduler_EDF_SMP_Pin( |
---|
856 | const Scheduler_Control *scheduler, |
---|
857 | Thread_Control *thread, |
---|
858 | Scheduler_Node *node_base, |
---|
859 | struct Per_CPU_Control *cpu |
---|
860 | ) |
---|
861 | { |
---|
862 | Scheduler_EDF_SMP_Node *node; |
---|
863 | uint8_t rqi; |
---|
864 | |
---|
865 | (void) scheduler; |
---|
866 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
867 | |
---|
868 | _Assert( |
---|
869 | _Scheduler_SMP_Node_state( &node->Base.Base ) == SCHEDULER_SMP_NODE_BLOCKED |
---|
870 | ); |
---|
871 | |
---|
872 | rqi = (uint8_t) _Per_CPU_Get_index( cpu ) + 1; |
---|
873 | node->ready_queue_index = rqi; |
---|
874 | node->pinning_ready_queue_index = rqi; |
---|
875 | } |
---|
876 | |
---|
877 | void _Scheduler_EDF_SMP_Unpin( |
---|
878 | const Scheduler_Control *scheduler, |
---|
879 | Thread_Control *thread, |
---|
880 | Scheduler_Node *node_base, |
---|
881 | struct Per_CPU_Control *cpu |
---|
882 | ) |
---|
883 | { |
---|
884 | Scheduler_EDF_SMP_Node *node; |
---|
885 | |
---|
886 | (void) scheduler; |
---|
887 | (void) cpu; |
---|
888 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
889 | |
---|
890 | _Assert( |
---|
891 | _Scheduler_SMP_Node_state( &node->Base.Base ) == SCHEDULER_SMP_NODE_BLOCKED |
---|
892 | ); |
---|
893 | |
---|
894 | node->ready_queue_index = node->affinity_ready_queue_index; |
---|
895 | node->pinning_ready_queue_index = 0; |
---|
896 | } |
---|
897 | |
---|
898 | Status_Control _Scheduler_EDF_SMP_Set_affinity( |
---|
899 | const Scheduler_Control *scheduler, |
---|
900 | Thread_Control *thread, |
---|
901 | Scheduler_Node *node_base, |
---|
902 | const Processor_mask *affinity |
---|
903 | ) |
---|
904 | { |
---|
905 | Scheduler_Context *context; |
---|
906 | Scheduler_EDF_SMP_Node *node; |
---|
907 | uint8_t rqi; |
---|
908 | |
---|
909 | context = _Scheduler_Get_context( scheduler ); |
---|
910 | |
---|
911 | /* |
---|
912 | * We support a thread to processor affinity to all online processors and an |
---|
913 | * affinity to exactly one processor. This restriction is necessary to avoid |
---|
914 | * issues if processors are added or removed to or from the scheduler. |
---|
915 | */ |
---|
916 | |
---|
917 | if ( _Processor_mask_Is_equal( affinity, &_SMP_Online_processors ) ) { |
---|
918 | rqi = 0; |
---|
919 | } else { |
---|
920 | Processor_mask local_affinity; |
---|
921 | Processor_mask one_to_one; |
---|
922 | uint32_t last; |
---|
923 | |
---|
924 | _Processor_mask_And( &local_affinity, &context->Processors, affinity ); |
---|
925 | |
---|
926 | if ( _Processor_mask_Is_zero( &local_affinity ) ) { |
---|
927 | return STATUS_INVALID_NUMBER; |
---|
928 | } |
---|
929 | |
---|
930 | last = _Processor_mask_Find_last_set( affinity ); |
---|
931 | _Processor_mask_From_index( &one_to_one, last - 1 ); |
---|
932 | |
---|
933 | /* |
---|
934 | * Use the global affinity set and not the affinity set local to the |
---|
935 | * scheduler to check for a one-to-one affinity. |
---|
936 | */ |
---|
937 | if ( !_Processor_mask_Is_equal( &one_to_one, affinity ) ) { |
---|
938 | return STATUS_INVALID_NUMBER; |
---|
939 | } |
---|
940 | |
---|
941 | rqi = last; |
---|
942 | } |
---|
943 | |
---|
944 | node = _Scheduler_EDF_SMP_Node_downcast( node_base ); |
---|
945 | node->affinity_ready_queue_index = rqi; |
---|
946 | |
---|
947 | if ( node->pinning_ready_queue_index == 0 ) { |
---|
948 | _Scheduler_SMP_Set_affinity( |
---|
949 | context, |
---|
950 | thread, |
---|
951 | node_base, |
---|
952 | &rqi, |
---|
953 | _Scheduler_EDF_SMP_Do_set_affinity, |
---|
954 | _Scheduler_EDF_SMP_Extract_from_scheduled, |
---|
955 | _Scheduler_EDF_SMP_Extract_from_ready, |
---|
956 | _Scheduler_EDF_SMP_Get_highest_ready, |
---|
957 | _Scheduler_EDF_SMP_Move_from_ready_to_scheduled, |
---|
958 | _Scheduler_EDF_SMP_Enqueue, |
---|
959 | _Scheduler_EDF_SMP_Allocate_processor, |
---|
960 | _Scheduler_EDF_SMP_Get_idle, |
---|
961 | _Scheduler_EDF_SMP_Release_idle |
---|
962 | ); |
---|
963 | } |
---|
964 | |
---|
965 | return STATUS_SUCCESSFUL; |
---|
966 | } |
---|