source: rtems/cpukit/score/include/rtems/score/schedulersmpimpl.h @ 1bf0666

4.115
Last change on this file since 1bf0666 was 1bf0666, checked in by Joel Sherrill <joel.sherrill@…>, on 05/21/14 at 20:08:23

schedulersmpimpl.h: Fix spacing

  • Property mode set to 100644
File size: 16.7 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 *   ss -> bs;
73 *   rs -> bs;
74 *
75 *   edge [label="block other"];
76 *   edge [fontcolor="darkgreen", color="darkgreen"];
77 *
78 *   rs -> ss;
79 * }
80 * @enddot
81 *
82 * During system initialization each processor of the scheduler instance starts
83 * with an idle thread assigned to it.  Lets have a look at an example with two
84 * idle threads I and J with priority 5.  We also have blocked threads A, B and
85 * C with priorities 1, 2 and 3 respectively.  The scheduler nodes are ordered
86 * with respect to the thread priority from left to right in the below
87 * diagrams.  The highest priority node (lowest priority number) is the
88 * leftmost node.  Since the processor assignment is independent of the thread
89 * priority the processor indices may move from one state to the other.
90 *
91 * @dot
92 * digraph {
93 *   node [style="filled"];
94 *   edge [dir="none"];
95 *   subgraph {
96 *     rank = same;
97 *
98 *     i [label="I (5)", fillcolor="green"];
99 *     j [label="J (5)", fillcolor="green"];
100 *     a [label="A (1)"];
101 *     b [label="B (2)"];
102 *     c [label="C (3)"];
103 *     i -> j;
104 *   }
105 *
106 *   subgraph {
107 *     rank = same;
108 *
109 *     p0 [label="PROCESSOR 0", shape="box"];
110 *     p1 [label="PROCESSOR 1", shape="box"];
111 *   }
112 *
113 *   i -> p0;
114 *   j -> p1;
115 * }
116 * @enddot
117 *
118 * Lets start A.  For this an enqueue operation is performed.
119 *
120 * @dot
121 * digraph {
122 *   node [style="filled"];
123 *   edge [dir="none"];
124 *
125 *   subgraph {
126 *     rank = same;
127 *
128 *     i [label="I (5)", fillcolor="green"];
129 *     j [label="J (5)", fillcolor="red"];
130 *     a [label="A (1)", fillcolor="green"];
131 *     b [label="B (2)"];
132 *     c [label="C (3)"];
133 *     a -> i;
134 *   }
135 *
136 *   subgraph {
137 *     rank = same;
138 *
139 *     p0 [label="PROCESSOR 0", shape="box"];
140 *     p1 [label="PROCESSOR 1", shape="box"];
141 *   }
142 *
143 *   i -> p0;
144 *   a -> p1;
145 * }
146 * @enddot
147 *
148 * Lets start C.
149 *
150 * @dot
151 * digraph {
152 *   node [style="filled"];
153 *   edge [dir="none"];
154 *
155 *   subgraph {
156 *     rank = same;
157 *
158 *     a [label="A (1)", fillcolor="green"];
159 *     c [label="C (3)", fillcolor="green"];
160 *     i [label="I (5)", fillcolor="red"];
161 *     j [label="J (5)", fillcolor="red"];
162 *     b [label="B (2)"];
163 *     a -> c;
164 *     i -> j;
165 *   }
166 *
167 *   subgraph {
168 *     rank = same;
169 *
170 *     p0 [label="PROCESSOR 0", shape="box"];
171 *     p1 [label="PROCESSOR 1", shape="box"];
172 *   }
173 *
174 *   c -> p0;
175 *   a -> p1;
176 * }
177 * @enddot
178 *
179 * Lets start B.
180 *
181 * @dot
182 * digraph {
183 *   node [style="filled"];
184 *   edge [dir="none"];
185 *
186 *   subgraph {
187 *     rank = same;
188 *
189 *     a [label="A (1)", fillcolor="green"];
190 *     b [label="B (2)", fillcolor="green"];
191 *     c [label="C (3)", fillcolor="red"];
192 *     i [label="I (5)", fillcolor="red"];
193 *     j [label="J (5)", fillcolor="red"];
194 *     a -> b;
195 *     c -> i -> j;
196 *   }
197 *
198 *   subgraph {
199 *     rank = same;
200 *
201 *     p0 [label="PROCESSOR 0", shape="box"];
202 *     p1 [label="PROCESSOR 1", shape="box"];
203 *   }
204 *
205 *   b -> p0;
206 *   a -> p1;
207 * }
208 * @enddot
209 *
210 * Lets change the priority of thread A to 4.
211 *
212 * @dot
213 * digraph {
214 *   node [style="filled"];
215 *   edge [dir="none"];
216 *
217 *   subgraph {
218 *     rank = same;
219 *
220 *     b [label="B (2)", fillcolor="green"];
221 *     c [label="C (3)", fillcolor="green"];
222 *     a [label="A (4)", fillcolor="red"];
223 *     i [label="I (5)", fillcolor="red"];
224 *     j [label="J (5)", fillcolor="red"];
225 *     b -> c;
226 *     a -> i -> j;
227 *   }
228 *
229 *   subgraph {
230 *     rank = same;
231 *
232 *     p0 [label="PROCESSOR 0", shape="box"];
233 *     p1 [label="PROCESSOR 1", shape="box"];
234 *   }
235 *
236 *   b -> p0;
237 *   c -> p1;
238 * }
239 * @enddot
240 *
241 * Now perform a blocking operation with thread B.  Please note that thread A
242 * migrated now from processor 0 to processor 1 and thread C still executes on
243 * processor 1.
244 *
245 * @dot
246 * digraph {
247 *   node [style="filled"];
248 *   edge [dir="none"];
249 *
250 *   subgraph {
251 *     rank = same;
252 *
253 *     c [label="C (3)", fillcolor="green"];
254 *     a [label="A (4)", fillcolor="green"];
255 *     i [label="I (5)", fillcolor="red"];
256 *     j [label="J (5)", fillcolor="red"];
257 *     b [label="B (2)"];
258 *     c -> a;
259 *     i -> j;
260 *   }
261 *
262 *   subgraph {
263 *     rank = same;
264 *
265 *     p0 [label="PROCESSOR 0", shape="box"];
266 *     p1 [label="PROCESSOR 1", shape="box"];
267 *   }
268 *
269 *   a -> p0;
270 *   c -> p1;
271 * }
272 * @enddot
273 *
274 * @{
275 */
276
277typedef Thread_Control *( *Scheduler_SMP_Get_highest_ready )(
278  Scheduler_Context *context
279);
280
281typedef void ( *Scheduler_SMP_Extract )(
282  Scheduler_Context *context,
283  Thread_Control *thread
284);
285
286typedef void ( *Scheduler_SMP_Insert )(
287  Scheduler_Context *context,
288  Thread_Control *thread_to_insert
289);
290
291typedef void ( *Scheduler_SMP_Move )(
292  Scheduler_Context *context,
293  Thread_Control *thread_to_move
294);
295
296typedef void ( *Scheduler_SMP_Update )(
297  Scheduler_Context *context,
298  Scheduler_Node *node,
299  Priority_Control new_priority
300);
301
302typedef void ( *Scheduler_SMP_Enqueue )(
303  Scheduler_Context *context,
304  Thread_Control *thread_to_enqueue
305);
306
307static inline Scheduler_SMP_Context *_Scheduler_SMP_Get_self(
308  Scheduler_Context *context
309)
310{
311  return (Scheduler_SMP_Context *) context;
312}
313
314static inline void _Scheduler_SMP_Initialize(
315  Scheduler_SMP_Context *self
316)
317{
318  _Chain_Initialize_empty( &self->Scheduled );
319}
320
321static inline Scheduler_SMP_Node *_Scheduler_SMP_Node_get(
322  Thread_Control *thread
323)
324{
325  return (Scheduler_SMP_Node *) _Scheduler_Node_get( thread );
326}
327
328static inline void _Scheduler_SMP_Node_initialize(
329  Scheduler_SMP_Node *node
330)
331{
332  node->state = SCHEDULER_SMP_NODE_BLOCKED;
333}
334
335extern const bool _Scheduler_SMP_Node_valid_state_changes[ 3 ][ 3 ];
336
337static inline void _Scheduler_SMP_Node_change_state(
338  Scheduler_SMP_Node *node,
339  Scheduler_SMP_Node_state new_state
340)
341{
342  _Assert(
343    _Scheduler_SMP_Node_valid_state_changes[ node->state ][ new_state ]
344  );
345
346  node->state = new_state;
347}
348
349static inline bool _Scheduler_SMP_Is_processor_owned_by_us(
350  const Scheduler_SMP_Context *self,
351  const Per_CPU_Control *cpu
352)
353{
354  return cpu->scheduler_context == &self->Base;
355}
356
357static inline void _Scheduler_SMP_Update_heir(
358  Per_CPU_Control *cpu_self,
359  Per_CPU_Control *cpu_for_heir,
360  Thread_Control *heir
361)
362{
363  cpu_for_heir->heir = heir;
364
365  /*
366   * It is critical that we first update the heir and then the dispatch
367   * necessary so that _Thread_Get_heir_and_make_it_executing() cannot miss an
368   * update.
369   */
370  _Atomic_Fence( ATOMIC_ORDER_SEQ_CST );
371
372  /*
373   * Only update the dispatch necessary indicator if not already set to
374   * avoid superfluous inter-processor interrupts.
375   */
376  if ( !cpu_for_heir->dispatch_necessary ) {
377    cpu_for_heir->dispatch_necessary = true;
378
379    if ( cpu_for_heir != cpu_self ) {
380      _Per_CPU_Send_interrupt( cpu_for_heir );
381    }
382  }
383}
384
385static inline void _Scheduler_SMP_Allocate_processor(
386  Scheduler_SMP_Context *self,
387  Thread_Control *scheduled,
388  Thread_Control *victim
389)
390{
391  Scheduler_SMP_Node *scheduled_node = _Scheduler_SMP_Node_get( scheduled );
392  Per_CPU_Control *cpu_of_scheduled = _Thread_Get_CPU( scheduled );
393  Per_CPU_Control *cpu_of_victim = _Thread_Get_CPU( victim );
394  Per_CPU_Control *cpu_self = _Per_CPU_Get();
395  Thread_Control *heir;
396
397  _Scheduler_SMP_Node_change_state(
398    scheduled_node,
399    SCHEDULER_SMP_NODE_SCHEDULED
400  );
401
402  _Assert( _ISR_Get_level() != 0 );
403
404  if ( _Thread_Is_executing_on_a_processor( scheduled ) ) {
405    if ( _Scheduler_SMP_Is_processor_owned_by_us( self, cpu_of_scheduled ) ) {
406      heir = cpu_of_scheduled->heir;
407      _Scheduler_SMP_Update_heir( cpu_self, cpu_of_scheduled, scheduled );
408    } else {
409      /* We have to force a migration to our processor set */
410      _Assert( scheduled->debug_real_cpu->heir != scheduled );
411      heir = scheduled;
412    }
413  } else {
414    heir = scheduled;
415  }
416
417  if ( heir != victim ) {
418    _Thread_Set_CPU( heir, cpu_of_victim );
419    _Scheduler_SMP_Update_heir( cpu_self, cpu_of_victim, heir );
420  }
421}
422
423static inline Thread_Control *_Scheduler_SMP_Get_lowest_scheduled(
424  Scheduler_SMP_Context *self
425)
426{
427  Thread_Control *lowest_ready = NULL;
428  Chain_Control *scheduled = &self->Scheduled;
429
430  if ( !_Chain_Is_empty( scheduled ) ) {
431    lowest_ready = (Thread_Control *) _Chain_Last( scheduled );
432  }
433
434  return lowest_ready;
435}
436
437/**
438 * @brief Enqueues a thread according to the specified order function.
439 *
440 * The thread must not be in the scheduled state.
441 *
442 * @param[in] context The scheduler instance context.
443 * @param[in] thread The thread to enqueue.
444 * @param[in] order The order function.
445 * @param[in] insert_ready Function to insert a node into the set of ready
446 * nodes.
447 * @param[in] insert_scheduled Function to insert a node into the set of
448 * scheduled nodes.
449 * @param[in] move_from_scheduled_to_ready Function to move a node from the set
450 * of scheduled nodes to the set of ready nodes.
451 */
452static inline void _Scheduler_SMP_Enqueue_ordered(
453  Scheduler_Context *context,
454  Thread_Control *thread,
455  Chain_Node_order order,
456  Scheduler_SMP_Insert insert_ready,
457  Scheduler_SMP_Insert insert_scheduled,
458  Scheduler_SMP_Move move_from_scheduled_to_ready
459)
460{
461  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
462  Thread_Control *lowest_scheduled =
463    _Scheduler_SMP_Get_lowest_scheduled( self );
464
465  _Assert( lowest_scheduled != NULL );
466
467  if ( ( *order )( &thread->Object.Node, &lowest_scheduled->Object.Node ) ) {
468    Scheduler_SMP_Node *lowest_scheduled_node =
469      _Scheduler_SMP_Node_get( lowest_scheduled );
470
471    _Scheduler_SMP_Node_change_state(
472      lowest_scheduled_node,
473      SCHEDULER_SMP_NODE_READY
474    );
475    _Scheduler_SMP_Allocate_processor( self, thread, lowest_scheduled );
476    ( *insert_scheduled )( &self->Base, thread );
477    ( *move_from_scheduled_to_ready )( &self->Base, lowest_scheduled );
478  } else {
479    ( *insert_ready )( &self->Base, thread );
480  }
481}
482
483/**
484 * @brief Enqueues a scheduled thread according to the specified order
485 * function.
486 *
487 * @param[in] context The scheduler instance context.
488 * @param[in] thread The thread to enqueue.
489 * @param[in] order The order function.
490 * @param[in] get_highest_ready Function to get the highest ready node.
491 * @param[in] insert_ready Function to insert a node into the set of ready
492 * nodes.
493 * @param[in] insert_scheduled Function to insert a node into the set of
494 * scheduled nodes.
495 * @param[in] move_from_ready_to_scheduled Function to move a node from the set
496 * of ready nodes to the set of scheduled nodes.
497 */
498static inline void _Scheduler_SMP_Enqueue_scheduled_ordered(
499  Scheduler_Context *context,
500  Thread_Control *thread,
501  Chain_Node_order order,
502  Scheduler_SMP_Get_highest_ready get_highest_ready,
503  Scheduler_SMP_Insert insert_ready,
504  Scheduler_SMP_Insert insert_scheduled,
505  Scheduler_SMP_Move move_from_ready_to_scheduled
506)
507{
508  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
509  Scheduler_SMP_Node *node = _Scheduler_SMP_Node_get( thread );
510  Thread_Control *highest_ready = ( *get_highest_ready )( &self->Base );
511
512  _Assert( highest_ready != NULL );
513
514  /*
515   * The thread has been extracted from the scheduled chain.  We have to place
516   * it now on the scheduled or ready set.
517   */
518  if ( ( *order )( &thread->Object.Node, &highest_ready->Object.Node ) ) {
519    ( *insert_scheduled )( &self->Base, thread );
520  } else {
521    _Scheduler_SMP_Node_change_state( node, SCHEDULER_SMP_NODE_READY );
522    _Scheduler_SMP_Allocate_processor( self, highest_ready, thread );
523    ( *insert_ready )( &self->Base, thread );
524    ( *move_from_ready_to_scheduled )( &self->Base, highest_ready );
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.