source: rtems/cpukit/score/include/rtems/score/schedulersmpimpl.h @ 6359b68

4.115
Last change on this file since 6359b68 was 6359b68, checked in by Sebastian Huber <sebastian.huber@…>, on 05/15/14 at 06:47:50

score: Add and use _Scheduler_SMP_Start_idle()

  • Property mode set to 100644
File size: 15.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(), and
45 * - _Scheduler_SMP_Block().
46 *
47 * @dot
48 * digraph {
49 *   node [style="filled"];
50 *
51 *   bs [label="BLOCKED"];
52 *   ss [label="SCHEDULED", fillcolor="green"];
53 *   rs [label="READY", fillcolor="red"];
54 *
55 *   edge [label="enqueue"];
56 *   edge [fontcolor="darkgreen", color="darkgreen"];
57 *
58 *   bs -> ss;
59 *
60 *   edge [fontcolor="red", color="red"];
61 *
62 *   bs -> rs;
63 *
64 *   edge [label="enqueue other"];
65 *
66 *   ss -> rs;
67 *
68 *   edge [label="block"];
69 *   edge [fontcolor="black", color="black"];
70 *
71 *   rs -> bs;
72 *
73 *   edge [label="block other"];
74 *   edge [fontcolor="darkgreen", color="darkgreen"];
75 *
76 *   rs -> ss;
77 * }
78 * @enddot
79 *
80 * During system initialization each processor of the scheduler instance starts
81 * with an idle thread assigned to it.  Lets have a look at an example with two
82 * idle threads I and J with priority 5.  We also have blocked threads A, B and
83 * C with priorities 1, 2 and 3 respectively.
84 *
85 * @dot
86 * digraph {
87 *   node [style="filled"];
88 *   edge [dir="none"];
89 *   subgraph {
90 *     rank = same;
91 *
92 *     i [label="I (5)", fillcolor="green"];
93 *     j [label="J (5)", fillcolor="green"];
94 *     a [label="A (1)"];
95 *     b [label="B (2)"];
96 *     c [label="C (3)"];
97 *     i -> j;
98 *   }
99 *
100 *   subgraph {
101 *     rank = same;
102 *
103 *     p0 [label="PROCESSOR 0", shape="box"];
104 *     p1 [label="PROCESSOR 1", shape="box"];
105 *   }
106 *
107 *   i -> p0;
108 *   j -> p1;
109 * }
110 * @enddot
111 *
112 * Lets start A.  For this an enqueue operation is performed.
113 *
114 * @dot
115 * digraph {
116 *   node [style="filled"];
117 *   edge [dir="none"];
118 *
119 *   subgraph {
120 *     rank = same;
121 *
122 *     i [label="I (5)", fillcolor="green"];
123 *     j [label="J (5)", fillcolor="red"];
124 *     a [label="A (1)", fillcolor="green"];
125 *     b [label="B (2)"];
126 *     c [label="C (3)"];
127 *     a -> i;
128 *   }
129 *
130 *   subgraph {
131 *     rank = same;
132 *
133 *     p0 [label="PROCESSOR 0", shape="box"];
134 *     p1 [label="PROCESSOR 1", shape="box"];
135 *   }
136 *
137 *   i -> p0;
138 *   a -> p1;
139 * }
140 * @enddot
141 *
142 * Lets start C.
143 *
144 * @dot
145 * digraph {
146 *   node [style="filled"];
147 *   edge [dir="none"];
148 *
149 *   subgraph {
150 *     rank = same;
151 *
152 *     a [label="A (1)", fillcolor="green"];
153 *     c [label="C (3)", fillcolor="green"];
154 *     i [label="I (5)", fillcolor="red"];
155 *     j [label="J (5)", fillcolor="red"];
156 *     b [label="B (2)"];
157 *     a -> c;
158 *     i -> j;
159 *   }
160 *
161 *   subgraph {
162 *     rank = same;
163 *
164 *     p0 [label="PROCESSOR 0", shape="box"];
165 *     p1 [label="PROCESSOR 1", shape="box"];
166 *   }
167 *
168 *   c -> p0;
169 *   a -> p1;
170 * }
171 * @enddot
172 *
173 * Lets start B.
174 *
175 * @dot
176 * digraph {
177 *   node [style="filled"];
178 *   edge [dir="none"];
179 *
180 *   subgraph {
181 *     rank = same;
182 *
183 *     a [label="A (1)", fillcolor="green"];
184 *     b [label="B (2)", fillcolor="green"];
185 *     c [label="C (3)", fillcolor="red"];
186 *     i [label="I (5)", fillcolor="red"];
187 *     j [label="J (5)", fillcolor="red"];
188 *     a -> b;
189 *     c -> i -> j;
190 *   }
191 *
192 *   subgraph {
193 *     rank = same;
194 *
195 *     p0 [label="PROCESSOR 0", shape="box"];
196 *     p1 [label="PROCESSOR 1", shape="box"];
197 *   }
198 *
199 *   b -> p0;
200 *   a -> p1;
201 * }
202 * @enddot
203 *
204 * Lets change the priority of thread A to 4.
205 *
206 * @dot
207 * digraph {
208 *   node [style="filled"];
209 *   edge [dir="none"];
210 *
211 *   subgraph {
212 *     rank = same;
213 *
214 *     b [label="B (2)", fillcolor="green"];
215 *     c [label="C (3)", fillcolor="green"];
216 *     a [label="A (4)", fillcolor="red"];
217 *     i [label="I (5)", fillcolor="red"];
218 *     j [label="J (5)", fillcolor="red"];
219 *     b -> c;
220 *     a -> i -> j;
221 *   }
222 *
223 *   subgraph {
224 *     rank = same;
225 *
226 *     p0 [label="PROCESSOR 0", shape="box"];
227 *     p1 [label="PROCESSOR 1", shape="box"];
228 *   }
229 *
230 *   b -> p0;
231 *   c -> p1;
232 * }
233 * @enddot
234 *
235 * Now perform a blocking operation with thread B.  Please note that thread A
236 * migrated now from processor 0 to processor 1 and thread C still executes on
237 * processor 1.
238 *
239 * @dot
240 * digraph {
241 *   node [style="filled"];
242 *   edge [dir="none"];
243 *
244 *   subgraph {
245 *     rank = same;
246 *
247 *     c [label="C (3)", fillcolor="green"];
248 *     a [label="A (4)", fillcolor="green"];
249 *     i [label="I (5)", fillcolor="red"];
250 *     j [label="J (5)", fillcolor="red"];
251 *     b [label="B (2)"];
252 *     c -> a;
253 *     i -> j;
254 *   }
255 *
256 *   subgraph {
257 *     rank = same;
258 *
259 *     p0 [label="PROCESSOR 0", shape="box"];
260 *     p1 [label="PROCESSOR 1", shape="box"];
261 *   }
262 *
263 *   a -> p0;
264 *   c -> p1;
265 * }
266 * @enddot
267 *
268 * @{
269 */
270
271typedef Thread_Control *( *Scheduler_SMP_Get_highest_ready )(
272  Scheduler_Context *context
273);
274
275typedef void ( *Scheduler_SMP_Extract )(
276  Scheduler_Context *context,
277  Thread_Control *thread
278);
279
280typedef void ( *Scheduler_SMP_Insert )(
281  Scheduler_Context *context,
282  Thread_Control *thread_to_insert
283);
284
285typedef void ( *Scheduler_SMP_Move )(
286  Scheduler_Context *context,
287  Thread_Control *thread_to_move
288);
289
290typedef void ( *Scheduler_SMP_Update )(
291  Scheduler_Context *context,
292  Scheduler_Node *node,
293  Priority_Control new_priority
294);
295
296typedef void ( *Scheduler_SMP_Enqueue )(
297  Scheduler_Context *context,
298  Thread_Control *thread_to_enqueue,
299  bool has_processor_allocated
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 * @param[in] context The scheduler instance context.
436 * @param[in] thread The thread to enqueue.
437 * @param[in] has_processor_allocated The thread has a processor allocated.
438 * @param[in] order The order function.
439 * @param[in] get_highest_ready Function to get the highest ready node.
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_ready_to_scheduled Function to move a node from the set
445 * of ready nodes to the set of scheduled nodes.
446 * @param[in] move_from_scheduled_to_ready Function to move a node from the set
447 * of scheduled nodes to the set of ready nodes.
448 */
449static inline void _Scheduler_SMP_Enqueue_ordered(
450  Scheduler_Context *context,
451  Thread_Control *thread,
452  bool has_processor_allocated,
453  Chain_Node_order order,
454  Scheduler_SMP_Get_highest_ready get_highest_ready,
455  Scheduler_SMP_Insert insert_ready,
456  Scheduler_SMP_Insert insert_scheduled,
457  Scheduler_SMP_Move move_from_ready_to_scheduled,
458  Scheduler_SMP_Move move_from_scheduled_to_ready
459)
460{
461  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
462  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
463
464  if ( has_processor_allocated) {
465    Thread_Control *highest_ready = ( *get_highest_ready )( &self->Base );
466
467    _Assert( highest_ready != NULL);
468
469    /*
470     * The thread has been extracted from the scheduled chain.  We have to
471     * place it now on the scheduled or ready chain.
472     *
473     * NOTE: Do not exchange parameters to do the negation of the order check.
474     */
475    if ( !( *order )( &thread->Object.Node, &highest_ready->Object.Node ) ) {
476      _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
477      _Scheduler_SMP_Allocate_processor( self, highest_ready, thread );
478      ( *insert_ready )( &self->Base, thread );
479      ( *move_from_ready_to_scheduled )( &self->Base, highest_ready );
480    } else {
481      _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_SCHEDULED );
482      ( *insert_scheduled )( &self->Base, thread );
483    }
484  } else {
485    Thread_Control *lowest_scheduled =
486      _Scheduler_SMP_Get_lowest_scheduled( self );
487
488    _Assert( lowest_scheduled != NULL);
489
490    if ( ( *order )( &thread->Object.Node, &lowest_scheduled->Object.Node ) ) {
491      Scheduler_SMP_Node *lowest_scheduled_node =
492        _Scheduler_SMP_Node_get( lowest_scheduled );
493
494      _Scheduler_SMP_Node_change_state(
495        lowest_scheduled_node,
496        SCHEDULER_SMP_NODE_READY
497      );
498      _Scheduler_SMP_Allocate_processor( self, thread, lowest_scheduled );
499      ( *insert_scheduled )( &self->Base, thread );
500      ( *move_from_scheduled_to_ready )( &self->Base, lowest_scheduled );
501    } else {
502      _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
503      ( *insert_ready )( &self->Base, thread );
504    }
505  }
506}
507
508static inline void _Scheduler_SMP_Extract_from_scheduled( Thread_Control *thread )
509{
510  _Chain_Extract_unprotected( &thread->Object.Node );
511}
512
513static inline void _Scheduler_SMP_Schedule_highest_ready(
514  Scheduler_Context *context,
515  Thread_Control *victim,
516  Scheduler_SMP_Get_highest_ready get_highest_ready,
517  Scheduler_SMP_Move move_from_ready_to_scheduled
518)
519{
520  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
521  Thread_Control *highest_ready = ( *get_highest_ready )( &self->Base );
522
523  _Scheduler_SMP_Allocate_processor( self, highest_ready, victim );
524
525  ( *move_from_ready_to_scheduled )( &self->Base, highest_ready );
526}
527
528/**
529 * @brief Blocks a thread.
530 *
531 * @param[in] context The scheduler instance context.
532 * @param[in] thread The thread of the scheduling operation.
533 * @param[in] extract_from_ready Function to extract a node from the set of
534 * ready nodes.
535 * @param[in] get_highest_ready Function to get the highest ready node.
536 * @param[in] move_from_ready_to_scheduled Function to move a node from the set
537 * of ready nodes to the set of scheduled nodes.
538 */
539static inline void _Scheduler_SMP_Block(
540  Scheduler_Context *context,
541  Thread_Control *thread,
542  Scheduler_SMP_Extract extract_from_ready,
543  Scheduler_SMP_Get_highest_ready get_highest_ready,
544  Scheduler_SMP_Move move_from_ready_to_scheduled
545)
546{
547  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
548  bool is_scheduled = node->state == SCHEDULER_SMP_NODE_SCHEDULED;
549
550  _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_BLOCKED );
551
552  if ( is_scheduled ) {
553    _Scheduler_SMP_Extract_from_scheduled( thread );
554
555    _Scheduler_SMP_Schedule_highest_ready(
556      context,
557      thread,
558      get_highest_ready,
559      move_from_ready_to_scheduled
560    );
561  } else {
562    ( *extract_from_ready )( context, thread );
563  }
564}
565
566static inline void _Scheduler_SMP_Change_priority(
567  Scheduler_Context *context,
568  Thread_Control *thread,
569  Priority_Control new_priority,
570  bool prepend_it,
571  Scheduler_SMP_Extract extract_from_ready,
572  Scheduler_SMP_Update update,
573  Scheduler_SMP_Enqueue enqueue_fifo,
574  Scheduler_SMP_Enqueue enqueue_lifo
575)
576{
577  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
578  bool has_processor_allocated = node->state == SCHEDULER_SMP_NODE_SCHEDULED;
579
580  if ( has_processor_allocated ) {
581    _Scheduler_SMP_Extract_from_scheduled( thread );
582  } else {
583    ( *extract_from_ready )( context, thread );
584  }
585
586  ( *update )( context, &node->Base, new_priority );
587
588  if ( prepend_it ) {
589    ( *enqueue_lifo )( context, thread, has_processor_allocated );
590  } else {
591    ( *enqueue_fifo )( context, thread, has_processor_allocated );
592  }
593}
594
595static inline void _Scheduler_SMP_Insert_scheduled_lifo(
596  Scheduler_Context *context,
597  Thread_Control *thread
598)
599{
600  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
601
602  _Chain_Insert_ordered_unprotected(
603    &self->Scheduled,
604    &thread->Object.Node,
605    _Scheduler_simple_Insert_priority_lifo_order
606  );
607}
608
609static inline void _Scheduler_SMP_Insert_scheduled_fifo(
610  Scheduler_Context *context,
611  Thread_Control *thread
612)
613{
614  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
615
616  _Chain_Insert_ordered_unprotected(
617    &self->Scheduled,
618    &thread->Object.Node,
619    _Scheduler_simple_Insert_priority_fifo_order
620  );
621}
622
623/** @} */
624
625#ifdef __cplusplus
626}
627#endif /* __cplusplus */
628
629#endif /* _RTEMS_SCORE_SCHEDULERSMPIMPL_H */
Note: See TracBrowser for help on using the repository browser.