source: rtems/cpukit/score/include/rtems/score/schedulersmpimpl.h @ c0bff5e

4.115
Last change on this file since c0bff5e was c0bff5e, checked in by Sebastian Huber <sebastian.huber@…>, on May 15, 2014 at 8:31:22 AM

score: Split SMP scheduler enqueue function

Extract code from _Scheduler_SMP_Enqueue_ordered() and move it to the
new function _Scheduler_SMP_Enqueue_scheduled_ordered() to avoid
untestable execution paths.

Add and use function _Scheduler_SMP_Unblock().

  • Property mode set to 100644
File size: 16.5 KB
Line 
1/**
2 * @file
3 *
4 * @brief SMP Scheduler Implementation
5 *
6 * @ingroup ScoreSchedulerSMP
7 */
8
9/*
10 * Copyright (c) 2013-2014 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.org/license/LICENSE.
21 */
22
23#ifndef _RTEMS_SCORE_SCHEDULERSMPIMPL_H
24#define _RTEMS_SCORE_SCHEDULERSMPIMPL_H
25
26#include <rtems/score/schedulersmp.h>
27#include <rtems/score/assert.h>
28#include <rtems/score/chainimpl.h>
29#include <rtems/score/schedulersimpleimpl.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif /* __cplusplus */
34
35/**
36 * @addtogroup ScoreSchedulerSMP
37 *
38 * The scheduler nodes can be in four states
39 * - @ref SCHEDULER_SMP_NODE_BLOCKED,
40 * - @ref SCHEDULER_SMP_NODE_SCHEDULED, and
41 * - @ref SCHEDULER_SMP_NODE_READY.
42 *
43 * State transitions are triggered via basic operations
44 * - _Scheduler_SMP_Enqueue_ordered(),
45 * - _Scheduler_SMP_Enqueue_scheduled_ordered(), and
46 * - _Scheduler_SMP_Block().
47 *
48 * @dot
49 * digraph {
50 *   node [style="filled"];
51 *
52 *   bs [label="BLOCKED"];
53 *   ss [label="SCHEDULED", fillcolor="green"];
54 *   rs [label="READY", fillcolor="red"];
55 *
56 *   edge [label="enqueue"];
57 *   edge [fontcolor="darkgreen", color="darkgreen"];
58 *
59 *   bs -> ss;
60 *
61 *   edge [fontcolor="red", color="red"];
62 *
63 *   bs -> rs;
64 *
65 *   edge [label="enqueue other"];
66 *
67 *   ss -> rs;
68 *
69 *   edge [label="block"];
70 *   edge [fontcolor="black", color="black"];
71 *
72 *   rs -> bs;
73 *
74 *   edge [label="block other"];
75 *   edge [fontcolor="darkgreen", color="darkgreen"];
76 *
77 *   rs -> ss;
78 * }
79 * @enddot
80 *
81 * During system initialization each processor of the scheduler instance starts
82 * with an idle thread assigned to it.  Lets have a look at an example with two
83 * idle threads I and J with priority 5.  We also have blocked threads A, B and
84 * C with priorities 1, 2 and 3 respectively.
85 *
86 * @dot
87 * digraph {
88 *   node [style="filled"];
89 *   edge [dir="none"];
90 *   subgraph {
91 *     rank = same;
92 *
93 *     i [label="I (5)", fillcolor="green"];
94 *     j [label="J (5)", fillcolor="green"];
95 *     a [label="A (1)"];
96 *     b [label="B (2)"];
97 *     c [label="C (3)"];
98 *     i -> j;
99 *   }
100 *
101 *   subgraph {
102 *     rank = same;
103 *
104 *     p0 [label="PROCESSOR 0", shape="box"];
105 *     p1 [label="PROCESSOR 1", shape="box"];
106 *   }
107 *
108 *   i -> p0;
109 *   j -> p1;
110 * }
111 * @enddot
112 *
113 * Lets start A.  For this an enqueue operation is performed.
114 *
115 * @dot
116 * digraph {
117 *   node [style="filled"];
118 *   edge [dir="none"];
119 *
120 *   subgraph {
121 *     rank = same;
122 *
123 *     i [label="I (5)", fillcolor="green"];
124 *     j [label="J (5)", fillcolor="red"];
125 *     a [label="A (1)", fillcolor="green"];
126 *     b [label="B (2)"];
127 *     c [label="C (3)"];
128 *     a -> i;
129 *   }
130 *
131 *   subgraph {
132 *     rank = same;
133 *
134 *     p0 [label="PROCESSOR 0", shape="box"];
135 *     p1 [label="PROCESSOR 1", shape="box"];
136 *   }
137 *
138 *   i -> p0;
139 *   a -> p1;
140 * }
141 * @enddot
142 *
143 * Lets start C.
144 *
145 * @dot
146 * digraph {
147 *   node [style="filled"];
148 *   edge [dir="none"];
149 *
150 *   subgraph {
151 *     rank = same;
152 *
153 *     a [label="A (1)", fillcolor="green"];
154 *     c [label="C (3)", fillcolor="green"];
155 *     i [label="I (5)", fillcolor="red"];
156 *     j [label="J (5)", fillcolor="red"];
157 *     b [label="B (2)"];
158 *     a -> c;
159 *     i -> j;
160 *   }
161 *
162 *   subgraph {
163 *     rank = same;
164 *
165 *     p0 [label="PROCESSOR 0", shape="box"];
166 *     p1 [label="PROCESSOR 1", shape="box"];
167 *   }
168 *
169 *   c -> p0;
170 *   a -> p1;
171 * }
172 * @enddot
173 *
174 * Lets start B.
175 *
176 * @dot
177 * digraph {
178 *   node [style="filled"];
179 *   edge [dir="none"];
180 *
181 *   subgraph {
182 *     rank = same;
183 *
184 *     a [label="A (1)", fillcolor="green"];
185 *     b [label="B (2)", fillcolor="green"];
186 *     c [label="C (3)", fillcolor="red"];
187 *     i [label="I (5)", fillcolor="red"];
188 *     j [label="J (5)", fillcolor="red"];
189 *     a -> b;
190 *     c -> i -> j;
191 *   }
192 *
193 *   subgraph {
194 *     rank = same;
195 *
196 *     p0 [label="PROCESSOR 0", shape="box"];
197 *     p1 [label="PROCESSOR 1", shape="box"];
198 *   }
199 *
200 *   b -> p0;
201 *   a -> p1;
202 * }
203 * @enddot
204 *
205 * Lets change the priority of thread A to 4.
206 *
207 * @dot
208 * digraph {
209 *   node [style="filled"];
210 *   edge [dir="none"];
211 *
212 *   subgraph {
213 *     rank = same;
214 *
215 *     b [label="B (2)", fillcolor="green"];
216 *     c [label="C (3)", fillcolor="green"];
217 *     a [label="A (4)", fillcolor="red"];
218 *     i [label="I (5)", fillcolor="red"];
219 *     j [label="J (5)", fillcolor="red"];
220 *     b -> c;
221 *     a -> i -> j;
222 *   }
223 *
224 *   subgraph {
225 *     rank = same;
226 *
227 *     p0 [label="PROCESSOR 0", shape="box"];
228 *     p1 [label="PROCESSOR 1", shape="box"];
229 *   }
230 *
231 *   b -> p0;
232 *   c -> p1;
233 * }
234 * @enddot
235 *
236 * Now perform a blocking operation with thread B.  Please note that thread A
237 * migrated now from processor 0 to processor 1 and thread C still executes on
238 * processor 1.
239 *
240 * @dot
241 * digraph {
242 *   node [style="filled"];
243 *   edge [dir="none"];
244 *
245 *   subgraph {
246 *     rank = same;
247 *
248 *     c [label="C (3)", fillcolor="green"];
249 *     a [label="A (4)", fillcolor="green"];
250 *     i [label="I (5)", fillcolor="red"];
251 *     j [label="J (5)", fillcolor="red"];
252 *     b [label="B (2)"];
253 *     c -> a;
254 *     i -> j;
255 *   }
256 *
257 *   subgraph {
258 *     rank = same;
259 *
260 *     p0 [label="PROCESSOR 0", shape="box"];
261 *     p1 [label="PROCESSOR 1", shape="box"];
262 *   }
263 *
264 *   a -> p0;
265 *   c -> p1;
266 * }
267 * @enddot
268 *
269 * @{
270 */
271
272typedef Thread_Control *( *Scheduler_SMP_Get_highest_ready )(
273  Scheduler_Context *context
274);
275
276typedef void ( *Scheduler_SMP_Extract )(
277  Scheduler_Context *context,
278  Thread_Control *thread
279);
280
281typedef void ( *Scheduler_SMP_Insert )(
282  Scheduler_Context *context,
283  Thread_Control *thread_to_insert
284);
285
286typedef void ( *Scheduler_SMP_Move )(
287  Scheduler_Context *context,
288  Thread_Control *thread_to_move
289);
290
291typedef void ( *Scheduler_SMP_Update )(
292  Scheduler_Context *context,
293  Scheduler_Node *node,
294  Priority_Control new_priority
295);
296
297typedef void ( *Scheduler_SMP_Enqueue )(
298  Scheduler_Context *context,
299  Thread_Control *thread_to_enqueue
300);
301
302static inline Scheduler_SMP_Context *_Scheduler_SMP_Get_self(
303  Scheduler_Context *context
304)
305{
306  return (Scheduler_SMP_Context *) context;
307}
308
309static inline void _Scheduler_SMP_Initialize(
310  Scheduler_SMP_Context *self
311)
312{
313  _Chain_Initialize_empty( &self->Scheduled );
314}
315
316static inline Scheduler_SMP_Node *_Scheduler_SMP_Node_get(
317  Thread_Control *thread
318)
319{
320  return (Scheduler_SMP_Node *) _Scheduler_Node_get( thread );
321}
322
323static inline void _Scheduler_SMP_Node_initialize(
324  Scheduler_SMP_Node *node
325)
326{
327  node->state = SCHEDULER_SMP_NODE_BLOCKED;
328}
329
330extern const bool _Scheduler_SMP_Node_valid_state_changes[ 3 ][ 3 ];
331
332static inline void _Scheduler_SMP_Node_change_state(
333  Scheduler_SMP_Node *node,
334  Scheduler_SMP_Node_state new_state
335)
336{
337  _Assert(
338    _Scheduler_SMP_Node_valid_state_changes[ node->state ][ new_state ]
339  );
340
341  node->state = new_state;
342}
343
344static inline bool _Scheduler_SMP_Is_processor_owned_by_us(
345  const Scheduler_SMP_Context *self,
346  const Per_CPU_Control *cpu
347)
348{
349  return cpu->scheduler_context == &self->Base;
350}
351
352static inline void _Scheduler_SMP_Update_heir(
353  Per_CPU_Control *cpu_self,
354  Per_CPU_Control *cpu_for_heir,
355  Thread_Control *heir
356)
357{
358  cpu_for_heir->heir = heir;
359
360  /*
361   * It is critical that we first update the heir and then the dispatch
362   * necessary so that _Thread_Get_heir_and_make_it_executing() cannot miss an
363   * update.
364   */
365  _Atomic_Fence( ATOMIC_ORDER_SEQ_CST );
366
367  /*
368   * Only update the dispatch necessary indicator if not already set to
369   * avoid superfluous inter-processor interrupts.
370   */
371  if ( !cpu_for_heir->dispatch_necessary ) {
372    cpu_for_heir->dispatch_necessary = true;
373
374    if ( cpu_for_heir != cpu_self ) {
375      _Per_CPU_Send_interrupt( cpu_for_heir );
376    }
377  }
378}
379
380static inline void _Scheduler_SMP_Allocate_processor(
381  Scheduler_SMP_Context *self,
382  Thread_Control *scheduled,
383  Thread_Control *victim
384)
385{
386  Scheduler_SMP_Node *scheduled_node = _Scheduler_SMP_Node_get( scheduled );
387  Per_CPU_Control *cpu_of_scheduled = _Thread_Get_CPU( scheduled );
388  Per_CPU_Control *cpu_of_victim = _Thread_Get_CPU( victim );
389  Per_CPU_Control *cpu_self = _Per_CPU_Get();
390  Thread_Control *heir;
391
392  _Scheduler_SMP_Node_change_state(
393    scheduled_node,
394    SCHEDULER_SMP_NODE_SCHEDULED
395  );
396
397  _Assert( _ISR_Get_level() != 0 );
398
399  if ( _Thread_Is_executing_on_a_processor( scheduled ) ) {
400    if ( _Scheduler_SMP_Is_processor_owned_by_us( self, cpu_of_scheduled ) ) {
401      heir = cpu_of_scheduled->heir;
402      _Scheduler_SMP_Update_heir( cpu_self, cpu_of_scheduled, scheduled );
403    } else {
404      /* We have to force a migration to our processor set */
405      _Assert( scheduled->debug_real_cpu->heir != scheduled );
406      heir = scheduled;
407    }
408  } else {
409    heir = scheduled;
410  }
411
412  if ( heir != victim ) {
413    _Thread_Set_CPU( heir, cpu_of_victim );
414    _Scheduler_SMP_Update_heir( cpu_self, cpu_of_victim, heir );
415  }
416}
417
418static inline Thread_Control *_Scheduler_SMP_Get_lowest_scheduled(
419  Scheduler_SMP_Context *self
420)
421{
422  Thread_Control *lowest_ready = NULL;
423  Chain_Control *scheduled = &self->Scheduled;
424
425  if ( !_Chain_Is_empty( scheduled ) ) {
426    lowest_ready = (Thread_Control *) _Chain_Last( scheduled );
427  }
428
429  return lowest_ready;
430}
431
432/**
433 * @brief Enqueues a thread according to the specified order function.
434 *
435 * The thread must not be in the scheduled state.
436 *
437 * @param[in] context The scheduler instance context.
438 * @param[in] thread The thread to enqueue.
439 * @param[in] order The order function.
440 * @param[in] insert_ready Function to insert a node into the set of ready
441 * nodes.
442 * @param[in] insert_scheduled Function to insert a node into the set of
443 * scheduled nodes.
444 * @param[in] move_from_scheduled_to_ready Function to move a node from the set
445 * of scheduled nodes to the set of ready nodes.
446 */
447static inline void _Scheduler_SMP_Enqueue_ordered(
448  Scheduler_Context *context,
449  Thread_Control *thread,
450  Chain_Node_order order,
451  Scheduler_SMP_Insert insert_ready,
452  Scheduler_SMP_Insert insert_scheduled,
453  Scheduler_SMP_Move move_from_scheduled_to_ready
454)
455{
456  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
457  Thread_Control *lowest_scheduled =
458    _Scheduler_SMP_Get_lowest_scheduled( self );
459
460  _Assert( lowest_scheduled != NULL);
461
462  /*
463   * NOTE: Do not exchange parameters to do the negation of the order check.
464   */
465  if ( ( *order )( &thread->Object.Node, &lowest_scheduled->Object.Node ) ) {
466    Scheduler_SMP_Node *lowest_scheduled_node =
467      _Scheduler_SMP_Node_get( lowest_scheduled );
468
469    _Scheduler_SMP_Node_change_state(
470      lowest_scheduled_node,
471      SCHEDULER_SMP_NODE_READY
472    );
473    _Scheduler_SMP_Allocate_processor( self, thread, lowest_scheduled );
474    ( *insert_scheduled )( &self->Base, thread );
475    ( *move_from_scheduled_to_ready )( &self->Base, lowest_scheduled );
476  } else {
477    ( *insert_ready )( &self->Base, thread );
478  }
479}
480
481/**
482 * @brief Enqueues a scheduled thread according to the specified order
483 * function.
484 *
485 * @param[in] context The scheduler instance context.
486 * @param[in] thread The thread to enqueue.
487 * @param[in] order The order function.
488 * @param[in] get_highest_ready Function to get the highest ready node.
489 * @param[in] insert_ready Function to insert a node into the set of ready
490 * nodes.
491 * @param[in] insert_scheduled Function to insert a node into the set of
492 * scheduled nodes.
493 * @param[in] move_from_ready_to_scheduled Function to move a node from the set
494 * of ready nodes to the set of scheduled nodes.
495 */
496static inline void _Scheduler_SMP_Enqueue_scheduled_ordered(
497  Scheduler_Context *context,
498  Thread_Control *thread,
499  Chain_Node_order order,
500  Scheduler_SMP_Get_highest_ready get_highest_ready,
501  Scheduler_SMP_Insert insert_ready,
502  Scheduler_SMP_Insert insert_scheduled,
503  Scheduler_SMP_Move move_from_ready_to_scheduled
504)
505{
506  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
507  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
508  Thread_Control *highest_ready = ( *get_highest_ready )( &self->Base );
509
510  _Assert( highest_ready != NULL);
511
512  /*
513   * The thread has been extracted from the scheduled chain.  We have to place
514   * it now on the scheduled or ready set.
515   *
516   * NOTE: Do not exchange parameters to do the negation of the order check.
517   */
518  if ( !( *order )( &thread->Object.Node, &highest_ready->Object.Node ) ) {
519    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
520    _Scheduler_SMP_Allocate_processor( self, highest_ready, thread );
521    ( *insert_ready )( &self->Base, thread );
522    ( *move_from_ready_to_scheduled )( &self->Base, highest_ready );
523  } else {
524    ( *insert_scheduled )( &self->Base, thread );
525  }
526}
527
528static inline void _Scheduler_SMP_Extract_from_scheduled(
529  Thread_Control *thread
530)
531{
532  _Chain_Extract_unprotected( &thread->Object.Node );
533}
534
535static inline void _Scheduler_SMP_Schedule_highest_ready(
536  Scheduler_Context *context,
537  Thread_Control *victim,
538  Scheduler_SMP_Get_highest_ready get_highest_ready,
539  Scheduler_SMP_Move move_from_ready_to_scheduled
540)
541{
542  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
543  Thread_Control *highest_ready = ( *get_highest_ready )( &self->Base );
544
545  _Scheduler_SMP_Allocate_processor( self, highest_ready, victim );
546
547  ( *move_from_ready_to_scheduled )( &self->Base, highest_ready );
548}
549
550/**
551 * @brief Blocks a thread.
552 *
553 * @param[in] context The scheduler instance context.
554 * @param[in] thread The thread of the scheduling operation.
555 * @param[in] extract_from_ready Function to extract a node from the set of
556 * ready nodes.
557 * @param[in] get_highest_ready Function to get the highest ready node.
558 * @param[in] move_from_ready_to_scheduled Function to move a node from the set
559 * of ready nodes to the set of scheduled nodes.
560 */
561static inline void _Scheduler_SMP_Block(
562  Scheduler_Context *context,
563  Thread_Control *thread,
564  Scheduler_SMP_Extract extract_from_ready,
565  Scheduler_SMP_Get_highest_ready get_highest_ready,
566  Scheduler_SMP_Move move_from_ready_to_scheduled
567)
568{
569  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
570  bool is_scheduled = node->state == SCHEDULER_SMP_NODE_SCHEDULED;
571
572  _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );
573
574  if ( is_scheduled ) {
575    _Scheduler_SMP_Extract_from_scheduled( thread );
576
577    _Scheduler_SMP_Schedule_highest_ready(
578      context,
579      thread,
580      get_highest_ready,
581      move_from_ready_to_scheduled
582    );
583  } else {
584    ( *extract_from_ready )( context, thread );
585  }
586}
587
588static inline void _Scheduler_SMP_Unblock(
589  Scheduler_Context *context,
590  Thread_Control *thread,
591  Scheduler_SMP_Enqueue enqueue_fifo
592)
593{
594  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
595
596  _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
597
598  ( *enqueue_fifo )( context, thread );
599}
600
601static inline void _Scheduler_SMP_Change_priority(
602  Scheduler_Context *context,
603  Thread_Control *thread,
604  Priority_Control new_priority,
605  bool prepend_it,
606  Scheduler_SMP_Extract extract_from_ready,
607  Scheduler_SMP_Update update,
608  Scheduler_SMP_Enqueue enqueue_fifo,
609  Scheduler_SMP_Enqueue enqueue_lifo,
610  Scheduler_SMP_Enqueue enqueue_scheduled_fifo,
611  Scheduler_SMP_Enqueue enqueue_scheduled_lifo
612)
613{
614  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
615
616  if ( node->state == SCHEDULER_SMP_NODE_SCHEDULED ) {
617    _Scheduler_SMP_Extract_from_scheduled( thread );
618
619    ( *update )( context, &node->Base, new_priority );
620
621    if ( prepend_it ) {
622      ( *enqueue_scheduled_lifo )( context, thread );
623    } else {
624      ( *enqueue_scheduled_fifo )( context, thread );
625    }
626  } else {
627    ( *extract_from_ready )( context, thread );
628
629    ( *update )( context, &node->Base, new_priority );
630
631    if ( prepend_it ) {
632      ( *enqueue_lifo )( context, thread );
633    } else {
634      ( *enqueue_fifo )( context, thread );
635    }
636  }
637}
638
639static inline void _Scheduler_SMP_Insert_scheduled_lifo(
640  Scheduler_Context *context,
641  Thread_Control *thread
642)
643{
644  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
645
646  _Chain_Insert_ordered_unprotected(
647    &self->Scheduled,
648    &thread->Object.Node,
649    _Scheduler_simple_Insert_priority_lifo_order
650  );
651}
652
653static inline void _Scheduler_SMP_Insert_scheduled_fifo(
654  Scheduler_Context *context,
655  Thread_Control *thread
656)
657{
658  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
659
660  _Chain_Insert_ordered_unprotected(
661    &self->Scheduled,
662    &thread->Object.Node,
663    _Scheduler_simple_Insert_priority_fifo_order
664  );
665}
666
667/** @} */
668
669#ifdef __cplusplus
670}
671#endif /* __cplusplus */
672
673#endif /* _RTEMS_SCORE_SCHEDULERSMPIMPL_H */
Note: See TracBrowser for help on using the repository browser.