source: rtems/cpukit/score/include/rtems/score/schedulerimpl.h @ 5bd822a7

4.115
Last change on this file since 5bd822a7 was 5bd822a7, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 26, 2014 at 10:51:34 AM

smp: Fix scheduler helping protocol

Ensure that scheduler nodes in the SCHEDULER_HELP_ACTIVE_OWNER or
SCHEDULER_HELP_ACTIVE_RIVAL helping state are always
SCHEDULER_SMP_NODE_READY or SCHEDULER_SMP_NODE_SCHEDULED to ensure the
MrsP protocol properties.

  • Property mode set to 100644
File size: 37.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief Inlined Routines Associated with the Manipulation of the Scheduler
5 *
6 * This inline file contains all of the inlined routines associated with
7 * the manipulation of the scheduler.
8 */
9
10/*
11 *  Copyright (C) 2010 Gedare Bloom.
12 *  Copyright (C) 2011 On-Line Applications Research Corporation (OAR).
13 *  Copyright (c) 2014 embedded brains GmbH
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#ifndef _RTEMS_SCORE_SCHEDULERIMPL_H
21#define _RTEMS_SCORE_SCHEDULERIMPL_H
22
23#include <rtems/score/scheduler.h>
24#include <rtems/score/cpusetimpl.h>
25#include <rtems/score/smpimpl.h>
26#include <rtems/score/threadimpl.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * @addtogroup ScoreScheduler
34 */
35/**@{**/
36
37/**
38 *  @brief Initializes the scheduler to the policy chosen by the user.
39 *
40 *  This routine initializes the scheduler to the policy chosen by the user
41 *  through confdefs, or to the priority scheduler with ready chains by
42 *  default.
43 */
44void _Scheduler_Handler_initialization( void );
45
46RTEMS_INLINE_ROUTINE Scheduler_Context *_Scheduler_Get_context(
47  const Scheduler_Control *scheduler
48)
49{
50  return scheduler->context;
51}
52
53RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get(
54  const Thread_Control *the_thread
55)
56{
57#if defined(RTEMS_SMP)
58  return the_thread->Scheduler.control;
59#else
60  (void) the_thread;
61
62  return &_Scheduler_Table[ 0 ];
63#endif
64}
65
66RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_own(
67  const Thread_Control *the_thread
68)
69{
70#if defined(RTEMS_SMP)
71  return the_thread->Scheduler.own_control;
72#else
73  (void) the_thread;
74
75  return &_Scheduler_Table[ 0 ];
76#endif
77}
78
79RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU_index(
80  uint32_t cpu_index
81)
82{
83#if defined(RTEMS_SMP)
84  return _Scheduler_Assignments[ cpu_index ].scheduler;
85#else
86  (void) cpu_index;
87
88  return &_Scheduler_Table[ 0 ];
89#endif
90}
91
92RTEMS_INLINE_ROUTINE const Scheduler_Control *_Scheduler_Get_by_CPU(
93  const Per_CPU_Control *cpu
94)
95{
96  uint32_t cpu_index = _Per_CPU_Get_index( cpu );
97
98  return _Scheduler_Get_by_CPU_index( cpu_index );
99}
100
101RTEMS_INLINE_ROUTINE Scheduler_Node *_Scheduler_Thread_get_own_node(
102  const Thread_Control *the_thread
103)
104{
105#if defined(RTEMS_SMP)
106  return the_thread->Scheduler.own_node;
107#else
108  return the_thread->Scheduler.node;
109#endif
110}
111
112#if defined(RTEMS_SMP)
113RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Node_get_user(
114  const Scheduler_Node *node
115)
116{
117  return node->user;
118}
119#endif
120
121/**
122 * The preferred method to add a new scheduler is to define the jump table
123 * entries and add a case to the _Scheduler_Initialize routine.
124 *
125 * Generic scheduling implementations that rely on the ready queue only can
126 * be found in the _Scheduler_queue_XXX functions.
127 */
128
129/*
130 * Passing the Scheduler_Control* to these functions allows for multiple
131 * scheduler's to exist simultaneously, which could be useful on an SMP
132 * system.  Then remote Schedulers may be accessible.  How to protect such
133 * accesses remains an open problem.
134 */
135
136/**
137 * @brief General scheduling decision.
138 *
139 * This kernel routine implements the scheduling decision logic for
140 * the scheduler. It does NOT dispatch.
141 *
142 * @param[in] the_thread The thread which state changed previously.
143 */
144RTEMS_INLINE_ROUTINE void _Scheduler_Schedule( Thread_Control *the_thread )
145{
146  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
147
148  ( *scheduler->Operations.schedule )( scheduler, the_thread );
149}
150
151#if defined(RTEMS_SMP)
152typedef struct {
153  Thread_Control *needs_help;
154  Thread_Control *next_needs_help;
155} Scheduler_Ask_for_help_context ;
156
157RTEMS_INLINE_ROUTINE bool _Scheduler_Ask_for_help_visitor(
158  Resource_Node *resource_node,
159  void          *arg
160)
161{
162  bool done;
163  Scheduler_Ask_for_help_context *help_context = arg;
164  Thread_Control *previous_needs_help = help_context->needs_help;
165  Thread_Control *next_needs_help;
166  Thread_Control *offers_help =
167    THREAD_RESOURCE_NODE_TO_THREAD( resource_node );
168  const Scheduler_Control *scheduler = _Scheduler_Get_own( offers_help );
169
170  next_needs_help = ( *scheduler->Operations.ask_for_help )(
171    scheduler,
172    offers_help,
173    previous_needs_help
174  );
175
176  done = next_needs_help != previous_needs_help;
177
178  if ( done ) {
179    help_context->next_needs_help = next_needs_help;
180  }
181
182  return done;
183}
184
185/**
186 * @brief Ask threads depending on resources owned by the thread for help.
187 *
188 * A thread is in need for help if it lost its assigned processor due to
189 * pre-emption by a higher priority thread or it was not possible to assign it
190 * a processor since its priority is to low on its current scheduler instance.
191 *
192 * The run-time of this function depends on the size of the resource tree of
193 * the thread needing help and other resource trees in case threads in need for
194 * help are produced during this operation.
195 *
196 * @param[in] needs_help The thread needing help.
197 */
198RTEMS_INLINE_ROUTINE void _Scheduler_Ask_for_help(
199  Thread_Control *needs_help
200)
201{
202  do {
203    const Scheduler_Control *scheduler = _Scheduler_Get_own( needs_help );
204
205    needs_help = ( *scheduler->Operations.ask_for_help )(
206      scheduler,
207      needs_help,
208      needs_help
209    );
210
211    if ( needs_help != NULL ) {
212      Scheduler_Ask_for_help_context help_context = { needs_help, NULL };
213
214      _Resource_Iterate(
215        &needs_help->Resource_node,
216        _Scheduler_Ask_for_help_visitor,
217        &help_context
218      );
219
220      needs_help = help_context.next_needs_help;
221    }
222  } while ( needs_help != NULL );
223}
224
225RTEMS_INLINE_ROUTINE void _Scheduler_Ask_for_help_if_necessary(
226  Thread_Control *needs_help
227)
228{
229  if (
230    needs_help != NULL
231      && _Resource_Node_owns_resources( &needs_help->Resource_node )
232  ) {
233    Scheduler_Node *node = _Scheduler_Thread_get_own_node( needs_help );
234
235    if (
236      node->help_state != SCHEDULER_HELP_ACTIVE_RIVAL
237        || _Scheduler_Node_get_user( node ) != needs_help
238    ) {
239      _Scheduler_Ask_for_help( needs_help );
240    }
241  }
242}
243#endif
244
245/**
246 * @brief Scheduler yield with a particular thread.
247 *
248 * This routine is invoked when a thread wishes to voluntarily transfer control
249 * of the processor to another thread.
250 *
251 * @param[in] the_thread The yielding thread.
252 */
253RTEMS_INLINE_ROUTINE void _Scheduler_Yield( Thread_Control *the_thread )
254{
255  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
256#if defined(RTEMS_SMP)
257  Thread_Control *needs_help;
258
259  needs_help =
260#endif
261  ( *scheduler->Operations.yield )( scheduler, the_thread );
262
263#if defined(RTEMS_SMP)
264  _Scheduler_Ask_for_help_if_necessary( needs_help );
265#endif
266}
267
268/**
269 * @brief Blocks a thread with respect to the scheduler.
270 *
271 * This routine removes @a the_thread from the scheduling decision for
272 * the scheduler. The primary task is to remove the thread from the
273 * ready queue.  It performs any necessary schedulering operations
274 * including the selection of a new heir thread.
275 *
276 * @param[in] the_thread The thread.
277 */
278RTEMS_INLINE_ROUTINE void _Scheduler_Block( Thread_Control *the_thread )
279{
280  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
281
282  ( *scheduler->Operations.block )( scheduler, the_thread );
283}
284
285/**
286 * @brief Unblocks a thread with respect to the scheduler.
287 *
288 * This routine adds @a the_thread to the scheduling decision for
289 * the scheduler.  The primary task is to add the thread to the
290 * ready queue per the schedulering policy and update any appropriate
291 * scheduling variables, for example the heir thread.
292 *
293 * @param[in] the_thread The thread.
294 */
295RTEMS_INLINE_ROUTINE void _Scheduler_Unblock( Thread_Control *the_thread )
296{
297  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
298#if defined(RTEMS_SMP)
299  Thread_Control *needs_help;
300
301  needs_help =
302#endif
303  ( *scheduler->Operations.unblock )( scheduler, the_thread );
304
305#if defined(RTEMS_SMP)
306  _Scheduler_Ask_for_help_if_necessary( needs_help );
307#endif
308}
309
310/**
311 * @brief Propagates a priority change of a thread to the scheduler.
312 *
313 * The caller must ensure that the thread is in the ready state.  The caller
314 * must ensure that the priority value actually changed and is not equal to the
315 * current priority value.
316 *
317 * @param[in] the_thread The thread changing its priority.
318 * @param[in] new_priority The new thread priority.
319 * @param[in] prepend_it In case this is true, then enqueue the thread as the
320 * first of its priority group, otherwise enqueue the thread as the last of its
321 * priority group.
322 */
323RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority(
324  Thread_Control          *the_thread,
325  Priority_Control         new_priority,
326  bool                     prepend_it
327)
328{
329  const Scheduler_Control *scheduler = _Scheduler_Get_own( the_thread );
330#if defined(RTEMS_SMP)
331  Thread_Control *needs_help;
332
333  needs_help =
334#endif
335  ( *scheduler->Operations.change_priority )(
336    scheduler,
337    the_thread,
338    new_priority,
339    prepend_it
340  );
341
342#if defined(RTEMS_SMP)
343  _Scheduler_Ask_for_help_if_necessary( needs_help );
344#endif
345}
346
347/**
348 * @brief Initializes a scheduler node.
349 *
350 * The scheduler node contains arbitrary data on function entry.  The caller
351 * must ensure that _Scheduler_Node_destroy() will be called after a
352 * _Scheduler_Node_initialize() before the memory of the scheduler node is
353 * destroyed.
354 *
355 * @param[in] scheduler The scheduler instance.
356 * @param[in] the_thread The thread containing the scheduler node.
357 */
358RTEMS_INLINE_ROUTINE void _Scheduler_Node_initialize(
359  const Scheduler_Control *scheduler,
360  Thread_Control          *the_thread
361)
362{
363  return ( *scheduler->Operations.node_initialize )( scheduler, the_thread );
364}
365
366/**
367 * @brief Destroys a scheduler node.
368 *
369 * The caller must ensure that _Scheduler_Node_destroy() will be called only
370 * after a corresponding _Scheduler_Node_initialize().
371 *
372 * @param[in] scheduler The scheduler instance.
373 * @param[in] the_thread The thread containing the scheduler node.
374 */
375RTEMS_INLINE_ROUTINE void _Scheduler_Node_destroy(
376  const Scheduler_Control *scheduler,
377  Thread_Control          *the_thread
378)
379{
380  ( *scheduler->Operations.node_destroy )( scheduler, the_thread );
381}
382
383/**
384 * @brief Updates the scheduler about a priority change of a not ready thread.
385 *
386 * @param[in] the_thread The thread.
387 * @param[in] new_priority The new priority of the thread.
388 */
389RTEMS_INLINE_ROUTINE void _Scheduler_Update_priority(
390  Thread_Control   *the_thread,
391  Priority_Control  new_priority
392)
393{
394  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
395
396  ( *scheduler->Operations.update_priority )(
397    scheduler,
398    the_thread,
399    new_priority
400  );
401}
402
403/**
404 * @brief Compares two priority values.
405 *
406 * @param[in] scheduler The scheduler instance.
407 * @param[in] p1 The first priority value.
408 * @param[in] p2 The second priority value.
409 *
410 * @retval negative The value @a p1 encodes a lower priority than @a p2 in the
411 * intuitive sense of priority.
412 * @retval 0 The priorities @a p1 and @a p2 are equal.
413 * @retval positive The value @a p1 encodes a higher priority than @a p2 in the
414 * intuitive sense of priority.
415 *
416 * @see _Scheduler_Is_priority_lower_than() and
417 * _Scheduler_Is_priority_higher_than().
418 */
419RTEMS_INLINE_ROUTINE int _Scheduler_Priority_compare(
420  const Scheduler_Control *scheduler,
421  Priority_Control         p1,
422  Priority_Control         p2
423)
424{
425  return ( *scheduler->Operations.priority_compare )( p1, p2 );
426}
427
428/**
429 * @brief Releases a job of a thread with respect to the scheduler.
430 *
431 * @param[in] the_thread The thread.
432 * @param[in] length The period length.
433 */
434RTEMS_INLINE_ROUTINE void _Scheduler_Release_job(
435  Thread_Control *the_thread,
436  uint32_t        length
437)
438{
439  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
440
441  ( *scheduler->Operations.release_job )( scheduler, the_thread, length );
442}
443
444/**
445 * @brief Scheduler method invoked at each clock tick.
446 *
447 * This method is invoked at each clock tick to allow the scheduler
448 * implementation to perform any activities required.  For the
449 * scheduler which support standard RTEMS features, this includes
450 * time-slicing management.
451 */
452RTEMS_INLINE_ROUTINE void _Scheduler_Tick( void )
453{
454  uint32_t cpu_count = _SMP_Get_processor_count();
455  uint32_t cpu_index;
456
457  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
458    const Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
459    const Scheduler_Control *scheduler = _Scheduler_Get_by_CPU( cpu );
460    Thread_Control *executing = cpu->executing;
461
462    if ( scheduler != NULL && executing != NULL ) {
463      ( *scheduler->Operations.tick )( scheduler, executing );
464    }
465  }
466}
467
468/**
469 * @brief Starts the idle thread for a particular processor.
470 *
471 * @param[in,out] the_thread The idle thread for the processor.
472 * @parma[in,out] processor The processor for the idle thread.
473 *
474 * @see _Thread_Create_idle().
475 */
476RTEMS_INLINE_ROUTINE void _Scheduler_Start_idle(
477  const Scheduler_Control *scheduler,
478  Thread_Control          *the_thread,
479  Per_CPU_Control         *cpu
480)
481{
482  ( *scheduler->Operations.start_idle )( scheduler, the_thread, cpu );
483}
484
485#if defined(RTEMS_SMP)
486RTEMS_INLINE_ROUTINE const Scheduler_Assignment *_Scheduler_Get_assignment(
487  uint32_t cpu_index
488)
489{
490  return &_Scheduler_Assignments[ cpu_index ];
491}
492
493RTEMS_INLINE_ROUTINE bool _Scheduler_Is_mandatory_processor(
494  const Scheduler_Assignment *assignment
495)
496{
497  return (assignment->attributes & SCHEDULER_ASSIGN_PROCESSOR_MANDATORY) != 0;
498}
499
500RTEMS_INLINE_ROUTINE bool _Scheduler_Should_start_processor(
501  const Scheduler_Assignment *assignment
502)
503{
504  return assignment->scheduler != NULL;
505}
506#endif /* defined(RTEMS_SMP) */
507
508RTEMS_INLINE_ROUTINE bool _Scheduler_Has_processor_ownership(
509  const Scheduler_Control *scheduler,
510  uint32_t cpu_index
511)
512{
513#if defined(RTEMS_SMP)
514  const Scheduler_Assignment *assignment =
515    _Scheduler_Get_assignment( cpu_index );
516
517  return assignment->scheduler == scheduler;
518#else
519  (void) scheduler;
520  (void) cpu_index;
521
522  return true;
523#endif
524}
525
526RTEMS_INLINE_ROUTINE void _Scheduler_Set(
527  const Scheduler_Control *scheduler,
528  Thread_Control          *the_thread
529)
530{
531#if defined(RTEMS_SMP)
532  const Scheduler_Control *current_scheduler = _Scheduler_Get( the_thread );
533
534  if ( current_scheduler != scheduler ) {
535    _Thread_Set_state( the_thread, STATES_MIGRATING );
536    _Scheduler_Node_destroy( current_scheduler, the_thread );
537    the_thread->Scheduler.own_control = scheduler;
538    the_thread->Scheduler.control = scheduler;
539    _Scheduler_Node_initialize( scheduler, the_thread );
540    _Scheduler_Update_priority( the_thread, the_thread->current_priority );
541    _Thread_Clear_state( the_thread, STATES_MIGRATING );
542  }
543#else
544  (void) scheduler;
545#endif
546}
547
548#if defined(__RTEMS_HAVE_SYS_CPUSET_H__)
549
550RTEMS_INLINE_ROUTINE void _Scheduler_Get_processor_set(
551  const Scheduler_Control *scheduler,
552  size_t                   cpusetsize,
553  cpu_set_t               *cpuset
554)
555{
556  uint32_t cpu_count = _SMP_Get_processor_count();
557  uint32_t cpu_index;
558
559  CPU_ZERO_S( cpusetsize, cpuset );
560
561  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
562#if defined(RTEMS_SMP)
563    if ( _Scheduler_Has_processor_ownership( scheduler, cpu_index ) ) {
564      CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
565    }
566#else
567    (void) scheduler;
568
569    CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
570#endif
571  }
572}
573
574RTEMS_INLINE_ROUTINE bool _Scheduler_default_Get_affinity_body(
575  const Scheduler_Control *scheduler,
576  Thread_Control          *the_thread,
577  size_t                   cpusetsize,
578  cpu_set_t               *cpuset
579)
580{
581  (void) the_thread;
582
583  _Scheduler_Get_processor_set( scheduler, cpusetsize, cpuset );
584
585  return true;
586}
587
588bool _Scheduler_Get_affinity(
589  Thread_Control *the_thread,
590  size_t          cpusetsize,
591  cpu_set_t      *cpuset
592);
593
594RTEMS_INLINE_ROUTINE bool _Scheduler_default_Set_affinity_body(
595  const Scheduler_Control *scheduler,
596  Thread_Control          *the_thread,
597  size_t                   cpusetsize,
598  const cpu_set_t         *cpuset
599)
600{
601  uint32_t cpu_count = _SMP_Get_processor_count();
602  uint32_t cpu_index;
603  bool     ok = true;
604
605  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
606#if defined(RTEMS_SMP)
607    const Scheduler_Control *scheduler_of_cpu =
608      _Scheduler_Get_by_CPU_index( cpu_index );
609
610    ok = ok
611      && ( CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
612        || ( !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
613          && scheduler != scheduler_of_cpu ) );
614#else
615    (void) scheduler;
616
617    ok = ok && CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset );
618#endif
619  }
620
621  return ok;
622}
623
624bool _Scheduler_Set_affinity(
625  Thread_Control          *the_thread,
626  size_t                   cpusetsize,
627  const cpu_set_t         *cpuset
628);
629
630#endif /* defined(__RTEMS_HAVE_SYS_CPUSET_H__) */
631
632RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir(
633  Thread_Control *heir,
634  bool force_dispatch
635)
636{
637  Thread_Control *executing = _Thread_Executing;
638
639  _Thread_Heir = heir;
640
641  if ( executing != heir && ( force_dispatch || executing->is_preemptible ) )
642    _Thread_Dispatch_necessary = true;
643}
644
645RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block(
646  const Scheduler_Control *scheduler,
647  Thread_Control          *the_thread,
648  void                  ( *extract )(
649                             const Scheduler_Control *,
650                             Thread_Control * ),
651  void                  ( *schedule )(
652                             const Scheduler_Control *,
653                             Thread_Control *,
654                             bool )
655)
656{
657  ( *extract )( scheduler, the_thread );
658
659  /* TODO: flash critical section? */
660
661  if ( _Thread_Is_executing( the_thread ) || _Thread_Is_heir( the_thread ) ) {
662    ( *schedule )( scheduler, the_thread, true );
663  }
664}
665
666/**
667 * @brief Returns true if @a p1 encodes a lower priority than @a p2 in the
668 * intuitive sense of priority.
669 */
670RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_lower_than(
671  const Scheduler_Control *scheduler,
672  Priority_Control         p1,
673  Priority_Control         p2
674)
675{
676  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) < 0;
677}
678
679/**
680 * @brief Returns true if @a p1 encodes a higher priority than @a p2 in the
681 * intuitive sense of priority.
682 */
683RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_higher_than(
684  const Scheduler_Control *scheduler,
685  Priority_Control         p1,
686  Priority_Control         p2
687)
688{
689  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) > 0;
690}
691
692/**
693 * @brief Returns the priority encoding @a p1 or @a p2 with the higher priority
694 * in the intuitive sense of priority.
695 */
696RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Highest_priority_of_two(
697  const Scheduler_Control *scheduler,
698  Priority_Control         p1,
699  Priority_Control         p2
700)
701{
702  return _Scheduler_Is_priority_higher_than( scheduler, p1, p2 ) ? p1 : p2;
703}
704
705/**
706 * @brief Sets the thread priority to @a priority if it is higher than the
707 * current priority of the thread in the intuitive sense of priority.
708 */
709RTEMS_INLINE_ROUTINE void _Scheduler_Set_priority_if_higher(
710  const Scheduler_Control *scheduler,
711  Thread_Control          *the_thread,
712  Priority_Control         priority
713)
714{
715  Priority_Control current = the_thread->current_priority;
716
717  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
718    _Thread_Set_priority( the_thread, priority );
719  }
720}
721
722/**
723 * @brief Changes the thread priority to @a priority if it is higher than the
724 * current priority of the thread in the intuitive sense of priority.
725 */
726RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority_if_higher(
727  const Scheduler_Control *scheduler,
728  Thread_Control          *the_thread,
729  Priority_Control         priority,
730  bool                     prepend_it
731)
732{
733  Priority_Control current = the_thread->current_priority;
734
735  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
736    _Thread_Change_priority( the_thread, priority, prepend_it );
737  }
738}
739
740RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_processor_count(
741  const Scheduler_Control *scheduler
742)
743{
744#if defined(RTEMS_SMP)
745  return _Scheduler_Get_context( scheduler )->processor_count;
746#else
747  (void) scheduler;
748
749  return 1;
750#endif
751}
752
753RTEMS_INLINE_ROUTINE Objects_Id _Scheduler_Build_id( uint32_t scheduler_index )
754{
755  return _Objects_Build_id(
756    OBJECTS_FAKE_OBJECTS_API,
757    OBJECTS_FAKE_OBJECTS_SCHEDULERS,
758    _Objects_Local_node,
759    scheduler_index + 1
760  );
761}
762
763RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index_by_id( Objects_Id id )
764{
765  uint32_t minimum_id = _Scheduler_Build_id( 0 );
766
767  return id - minimum_id;
768}
769
770RTEMS_INLINE_ROUTINE bool _Scheduler_Get_by_id(
771  Objects_Id                id,
772  const Scheduler_Control **scheduler_p
773)
774{
775  uint32_t index = _Scheduler_Get_index_by_id( id );
776  const Scheduler_Control *scheduler = &_Scheduler_Table[ index ];
777
778  *scheduler_p = scheduler;
779
780  return index < _Scheduler_Count
781    && _Scheduler_Get_processor_count( scheduler ) > 0;
782}
783
784RTEMS_INLINE_ROUTINE bool _Scheduler_Is_id_valid( Objects_Id id )
785{
786  const Scheduler_Control *scheduler;
787  bool ok = _Scheduler_Get_by_id( id, &scheduler );
788
789  (void) scheduler;
790
791  return ok;
792}
793
794RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index(
795  const Scheduler_Control *scheduler
796)
797{
798  return (uint32_t) (scheduler - &_Scheduler_Table[ 0 ]);
799}
800
801RTEMS_INLINE_ROUTINE Scheduler_Node *_Scheduler_Thread_get_node(
802  const Thread_Control *the_thread
803)
804{
805  return the_thread->Scheduler.node;
806}
807
808RTEMS_INLINE_ROUTINE void _Scheduler_Node_do_initialize(
809  Scheduler_Node *node,
810  Thread_Control *the_thread
811)
812{
813#if defined(RTEMS_SMP)
814  node->user = the_thread;
815  node->help_state = SCHEDULER_HELP_YOURSELF;
816  node->owner = the_thread;
817  node->idle = NULL;
818  node->accepts_help = the_thread;
819#else
820  (void) node;
821  (void) the_thread;
822#endif
823}
824
825#if defined(RTEMS_SMP)
826/**
827 * @brief Gets an idle thread from the scheduler instance.
828 *
829 * @param[in] context The scheduler instance context.
830 *
831 * @retval idle An idle thread for use.  This function must always return an
832 * idle thread.  If none is available, then this is a fatal error.
833 */
834typedef Thread_Control *( *Scheduler_Get_idle_thread )(
835  Scheduler_Context *context
836);
837
838/**
839 * @brief Releases an idle thread to the scheduler instance for reuse.
840 *
841 * @param[in] context The scheduler instance context.
842 * @param[in] idle The idle thread to release
843 */
844typedef void ( *Scheduler_Release_idle_thread )(
845  Scheduler_Context *context,
846  Thread_Control    *idle
847);
848
849RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Node_get_owner(
850  const Scheduler_Node *node
851)
852{
853  return node->owner;
854}
855
856RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Node_get_idle(
857  const Scheduler_Node *node
858)
859{
860  return node->idle;
861}
862
863RTEMS_INLINE_ROUTINE void _Scheduler_Node_set_user(
864  Scheduler_Node *node,
865  Thread_Control *user
866)
867{
868  node->user = user;
869}
870
871RTEMS_INLINE_ROUTINE void _Scheduler_Thread_set_node(
872  Thread_Control *the_thread,
873  Scheduler_Node *node
874)
875{
876  the_thread->Scheduler.node = node;
877}
878
879RTEMS_INLINE_ROUTINE void _Scheduler_Thread_set_scheduler_and_node(
880  Thread_Control       *the_thread,
881  Scheduler_Node       *node,
882  const Thread_Control *previous_user_of_node
883)
884{
885  const Scheduler_Control *scheduler =
886    _Scheduler_Get_own( previous_user_of_node );
887
888  the_thread->Scheduler.control = scheduler;
889  _Scheduler_Thread_set_node( the_thread, node );
890}
891
892extern const bool _Scheduler_Thread_state_valid_state_changes[ 3 ][ 3 ];
893
894RTEMS_INLINE_ROUTINE void _Scheduler_Thread_change_state(
895  Thread_Control         *the_thread,
896  Thread_Scheduler_state  new_state
897)
898{
899  _Assert(
900    _Scheduler_Thread_state_valid_state_changes
901      [ the_thread->Scheduler.state ][ new_state ]
902  );
903
904  the_thread->Scheduler.state = new_state;
905}
906
907/**
908 * @brief Changes the scheduler help state of a thread.
909 *
910 * @param[in] the_thread The thread.
911 * @param[in] new_help_state The new help state.
912 *
913 * @return The previous help state.
914 */
915RTEMS_INLINE_ROUTINE Scheduler_Help_state _Scheduler_Thread_change_help_state(
916  Thread_Control       *the_thread,
917  Scheduler_Help_state  new_help_state
918)
919{
920  Scheduler_Node *node = _Scheduler_Thread_get_own_node( the_thread );
921  Scheduler_Help_state previous_help_state = node->help_state;
922
923  node->help_state = new_help_state;
924
925  return previous_help_state;
926}
927
928/**
929 * @brief Changes the resource tree root of a thread.
930 *
931 * For each node of the resource sub-tree specified by the top thread the
932 * scheduler asks for help.  So the root thread gains access to all scheduler
933 * nodes corresponding to the resource sub-tree.  In case a thread previously
934 * granted help is displaced by this operation, then the scheduler asks for
935 * help using its remaining resource tree.
936 *
937 * The run-time of this function depends on the size of the resource sub-tree
938 * and other resource trees in case threads in need for help are produced
939 * during this operation.
940 *
941 * @param[in] top The thread specifying the resource sub-tree top.
942 * @param[in] root The thread specifying the new resource sub-tree root.
943 */
944void _Scheduler_Thread_change_resource_root(
945  Thread_Control *top,
946  Thread_Control *root
947);
948
949/**
950 * @brief Use an idle thread for this scheduler node.
951 *
952 * A thread in the SCHEDULER_HELP_ACTIVE_OWNER or SCHEDULER_HELP_ACTIVE_RIVAL
953 * helping state may use an idle thread for the scheduler node owned by itself
954 * in case it executes currently using another scheduler node or in case it is
955 * in a blocking state.
956 *
957 * @param[in] context The scheduler instance context.
958 * @param[in] node The node which wants to use the idle thread.
959 * @param[in] get_idle_thread Function to get an idle thread.
960 */
961RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Use_idle_thread(
962  Scheduler_Context         *context,
963  Scheduler_Node            *node,
964  Scheduler_Get_idle_thread  get_idle_thread
965)
966{
967  Thread_Control *idle = ( *get_idle_thread )( context );
968
969  _Assert(
970    node->help_state == SCHEDULER_HELP_ACTIVE_OWNER
971      || node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL
972  );
973  _Assert( _Scheduler_Node_get_idle( node ) == NULL );
974  _Assert(
975    _Scheduler_Node_get_owner( node ) == _Scheduler_Node_get_user( node )
976  );
977
978  _Scheduler_Thread_set_node( idle, node );
979
980  _Scheduler_Node_set_user( node, idle );
981  node->idle = idle;
982
983  return idle;
984}
985
986/**
987 * @brief Try to schedule this scheduler node.
988 *
989 * @param[in] context The scheduler instance context.
990 * @param[in] node The node which wants to get scheduled.
991 * @param[in] get_idle_thread Function to get an idle thread.
992 *
993 * @retval true This node can be scheduled.
994 * @retval false Otherwise.
995 */
996RTEMS_INLINE_ROUTINE bool _Scheduler_Try_to_schedule_node(
997  Scheduler_Context         *context,
998  Scheduler_Node            *node,
999  Scheduler_Get_idle_thread  get_idle_thread
1000)
1001{
1002  bool schedule;
1003  Thread_Control *owner;
1004  Thread_Control *user;
1005
1006  if ( node->help_state == SCHEDULER_HELP_YOURSELF ) {
1007    return true;
1008  }
1009
1010  owner = _Scheduler_Node_get_owner( node );
1011  user = _Scheduler_Node_get_user( node );
1012
1013  if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL) {
1014    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1015      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1016    } else if ( owner->Scheduler.state == THREAD_SCHEDULER_BLOCKED ) {
1017      _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1018    } else {
1019      _Scheduler_Node_set_user( node, owner );
1020    }
1021
1022    schedule = true;
1023  } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1024    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1025      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1026    } else {
1027      _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1028    }
1029
1030    schedule = true;
1031  } else {
1032    _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1033
1034    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1035      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1036      schedule = true;
1037    } else {
1038      schedule = false;
1039    }
1040  }
1041
1042  return schedule;
1043}
1044
1045/**
1046 * @brief Release an idle thread using this scheduler node.
1047 *
1048 * @param[in] context The scheduler instance context.
1049 * @param[in] node The node which may have an idle thread as user.
1050 * @param[in] release_idle_thread Function to release an idle thread.
1051 *
1052 * @retval idle The idle thread which used this node.
1053 * @retval NULL This node had no idle thread as an user.
1054 */
1055RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Release_idle_thread(
1056  Scheduler_Context             *context,
1057  Scheduler_Node                *node,
1058  Scheduler_Release_idle_thread  release_idle_thread
1059)
1060{
1061  Thread_Control *idle = _Scheduler_Node_get_idle( node );
1062
1063  if ( idle != NULL ) {
1064    Thread_Control *owner = _Scheduler_Node_get_owner( node );
1065
1066    node->idle = NULL;
1067    _Scheduler_Node_set_user( node, owner );
1068    _Scheduler_Thread_change_state( idle, THREAD_SCHEDULER_READY );
1069    _Scheduler_Thread_set_node( idle, idle->Scheduler.own_node );
1070
1071    ( *release_idle_thread )( context, idle );
1072  }
1073
1074  return idle;
1075}
1076
1077/**
1078 * @brief Block this scheduler node.
1079 *
1080 * @param[in] context The scheduler instance context.
1081 * @param[in] thread The thread which wants to get blocked referencing this
1082 *   node.  This is not necessarily the user of this node in case the node
1083 *   participates in the scheduler helping protocol.
1084 * @param[in] node The node which wants to get blocked.
1085 * @param[in] is_scheduled This node is scheduled.
1086 * @param[in] get_idle_thread Function to get an idle thread.
1087 *
1088 * @retval true Continue with the blocking operation.
1089 * @retval false Otherwise.
1090 */
1091RTEMS_INLINE_ROUTINE bool _Scheduler_Block_node(
1092  Scheduler_Context         *context,
1093  Thread_Control            *thread,
1094  Scheduler_Node            *node,
1095  bool                       is_scheduled,
1096  Scheduler_Get_idle_thread  get_idle_thread
1097)
1098{
1099  Thread_Control *old_user;
1100  Thread_Control *new_user;
1101
1102  _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_BLOCKED );
1103
1104  if ( node->help_state == SCHEDULER_HELP_YOURSELF ) {
1105    _Assert( thread == _Scheduler_Node_get_user( node ) );
1106
1107    return true;
1108  }
1109
1110  new_user = NULL;
1111
1112  if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1113    if ( is_scheduled ) {
1114      _Assert( thread == _Scheduler_Node_get_user( node ) );
1115      old_user = thread;
1116      new_user = _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1117    }
1118  } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL ) {
1119    if ( is_scheduled ) {
1120      old_user = _Scheduler_Node_get_user( node );
1121
1122      if ( thread == old_user ) {
1123        Thread_Control *owner = _Scheduler_Node_get_owner( node );
1124
1125        if (
1126          thread != owner
1127            && owner->Scheduler.state == THREAD_SCHEDULER_READY
1128        ) {
1129          new_user = owner;
1130          _Scheduler_Node_set_user( node, new_user );
1131        } else {
1132          new_user = _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1133        }
1134      }
1135    }
1136  } else {
1137    /* Not implemented, this is part of the OMIP support path. */
1138    _Assert(0);
1139  }
1140
1141  if ( new_user != NULL ) {
1142    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1143
1144    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1145    _Thread_Set_CPU( new_user, cpu );
1146    _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, new_user );
1147  }
1148
1149  return false;
1150}
1151
1152/**
1153 * @brief Unblock this scheduler node.
1154 *
1155 * @param[in] context The scheduler instance context.
1156 * @param[in] the_thread The thread which wants to get unblocked.
1157 * @param[in] node The node which wants to get unblocked.
1158 * @param[in] is_scheduled This node is scheduled.
1159 * @param[in] release_idle_thread Function to release an idle thread.
1160 *
1161 * @retval true Continue with the unblocking operation.
1162 * @retval false Otherwise.
1163 */
1164RTEMS_INLINE_ROUTINE bool _Scheduler_Unblock_node(
1165  Scheduler_Context             *context,
1166  Thread_Control                *the_thread,
1167  Scheduler_Node                *node,
1168  bool                           is_scheduled,
1169  Scheduler_Release_idle_thread  release_idle_thread
1170)
1171{
1172  bool unblock;
1173
1174  if ( is_scheduled ) {
1175    Thread_Control *old_user = _Scheduler_Node_get_user( node );
1176    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1177    Thread_Control *idle = _Scheduler_Release_idle_thread(
1178      context,
1179      node,
1180      release_idle_thread
1181    );
1182    Thread_Control *owner = _Scheduler_Node_get_owner( node );
1183    Thread_Control *new_user;
1184
1185    if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1186      _Assert( idle != NULL );
1187      new_user = the_thread;
1188    } else if ( idle != NULL ) {
1189      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1190      new_user = the_thread;
1191    } else if ( the_thread != owner ) {
1192      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1193      _Assert( old_user != the_thread );
1194      _Scheduler_Thread_change_state( owner, THREAD_SCHEDULER_READY );
1195      new_user = the_thread;
1196      _Scheduler_Node_set_user( node, new_user );
1197    } else {
1198      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1199      _Assert( old_user != the_thread );
1200      _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1201      new_user = NULL;
1202    }
1203
1204    if ( new_user != NULL ) {
1205      _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1206      _Thread_Set_CPU( new_user, cpu );
1207      _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, new_user );
1208    }
1209
1210    unblock = false;
1211  } else {
1212    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1213
1214    unblock = true;
1215  }
1216
1217  return unblock;
1218}
1219
1220/**
1221 * @brief Asks a ready scheduler node for help.
1222 *
1223 * @param[in] node The ready node offering help.
1224 * @param[in] needs_help The thread needing help.
1225 *
1226 * @retval needs_help The thread needing help.
1227 */
1228RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_ready_node_for_help(
1229  Scheduler_Node *node,
1230  Thread_Control *needs_help
1231)
1232{
1233  _Scheduler_Node_set_user( node, needs_help );
1234
1235  return needs_help;
1236}
1237
1238/**
1239 * @brief Asks a scheduled scheduler node for help.
1240 *
1241 * @param[in] context The scheduler instance context.
1242 * @param[in] node The scheduled node offering help.
1243 * @param[in] offers_help The thread offering help.
1244 * @param[in] needs_help The thread needing help.
1245 * @param[in] previous_accepts_help The previous thread accepting help by this
1246 *   scheduler node.
1247 * @param[in] release_idle_thread Function to release an idle thread.
1248 *
1249 * @retval needs_help The previous thread accepting help by this scheduler node
1250 *   which was displaced by the thread needing help.
1251 * @retval NULL There are no more threads needing help.
1252 */
1253RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_scheduled_node_for_help(
1254  Scheduler_Context             *context,
1255  Scheduler_Node                *node,
1256  Thread_Control                *offers_help,
1257  Thread_Control                *needs_help,
1258  Thread_Control                *previous_accepts_help,
1259  Scheduler_Release_idle_thread  release_idle_thread
1260)
1261{
1262  Thread_Control *next_needs_help = NULL;
1263  Thread_Control *old_user = NULL;
1264  Thread_Control *new_user = NULL;
1265
1266  if (
1267    previous_accepts_help != needs_help
1268      && _Scheduler_Thread_get_node( previous_accepts_help ) == node
1269  ) {
1270    Thread_Control *idle = _Scheduler_Release_idle_thread(
1271      context,
1272      node,
1273      release_idle_thread
1274    );
1275
1276    if ( idle != NULL ) {
1277      old_user = idle;
1278    } else {
1279      _Assert( _Scheduler_Node_get_user( node ) == previous_accepts_help );
1280      old_user = previous_accepts_help;
1281    }
1282
1283    if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1284      new_user = needs_help;
1285    } else {
1286      _Assert(
1287        node->help_state == SCHEDULER_HELP_ACTIVE_OWNER
1288          || node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL
1289      );
1290      _Assert( offers_help->Scheduler.node == offers_help->Scheduler.own_node );
1291
1292      new_user = offers_help;
1293    }
1294
1295    if ( previous_accepts_help != offers_help ) {
1296      next_needs_help = previous_accepts_help;
1297    }
1298  } else if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1299    Thread_Control *idle = _Scheduler_Release_idle_thread(
1300      context,
1301      node,
1302      release_idle_thread
1303    );
1304
1305    if ( idle != NULL ) {
1306      old_user = idle;
1307    } else {
1308      old_user = _Scheduler_Node_get_user( node );
1309    }
1310
1311    new_user = needs_help;
1312  } else {
1313    _Assert( needs_help->Scheduler.state == THREAD_SCHEDULER_SCHEDULED );
1314  }
1315
1316  if ( new_user != old_user ) {
1317    Per_CPU_Control *cpu_self = _Per_CPU_Get();
1318    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1319
1320    _Scheduler_Thread_change_state( old_user, THREAD_SCHEDULER_READY );
1321    _Scheduler_Thread_set_scheduler_and_node(
1322      old_user,
1323      _Scheduler_Thread_get_own_node( old_user ),
1324      old_user
1325    );
1326
1327    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1328    _Scheduler_Thread_set_scheduler_and_node( new_user, node, offers_help );
1329
1330    _Scheduler_Node_set_user( node, new_user );
1331    _Thread_Set_CPU( new_user, cpu );
1332    _Thread_Dispatch_update_heir( cpu_self, cpu, new_user );
1333  }
1334
1335  return next_needs_help;
1336}
1337
1338/**
1339 * @brief Asks a blocked scheduler node for help.
1340 *
1341 * @param[in] context The scheduler instance context.
1342 * @param[in] node The scheduled node offering help.
1343 * @param[in] offers_help The thread offering help.
1344 * @param[in] needs_help The thread needing help.
1345 *
1346 * @retval true Enqueue this scheduler node.
1347 * @retval false Otherwise.
1348 */
1349RTEMS_INLINE_ROUTINE bool _Scheduler_Ask_blocked_node_for_help(
1350  Scheduler_Context *context,
1351  Scheduler_Node    *node,
1352  Thread_Control    *offers_help,
1353  Thread_Control    *needs_help
1354)
1355{
1356  bool enqueue;
1357
1358  _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1359
1360  if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1361    _Scheduler_Node_set_user( node, needs_help );
1362    _Scheduler_Thread_set_scheduler_and_node( needs_help, node, offers_help );
1363
1364    enqueue = true;
1365  } else {
1366    enqueue = false;
1367  }
1368
1369  return enqueue;
1370}
1371#endif
1372
1373/** @} */
1374
1375#ifdef __cplusplus
1376}
1377#endif
1378
1379#endif
1380/* end of include file */
Note: See TracBrowser for help on using the repository browser.