source: rtems/cpukit/score/include/rtems/score/schedulerimpl.h @ cceb19f4

4.115
Last change on this file since cceb19f4 was cceb19f4, checked in by Luca Bonato <lohathe@…>, on Nov 21, 2014 at 10:01:34 AM

smp: Fix scheduler helping protocol

New test case for smptests/smpmrsp01.

Fix _Scheduler_Block_node() in case the node is in the
SCHEDULER_HELP_ACTIVE_RIVAL helping state. For example a
rtems_task_suspend() on a task waiting for a MrsP semaphore.

Fix _Scheduler_Unblock_node() in case the node is in the
SCHEDULER_SMP_NODE_READY state. For example a rtems_task_resume() on a
task owning or waiting for a MrsP semaphore.

  • Property mode set to 100644
File size: 35.6 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 owner state may use an idle
953 * thread for the scheduler node owned by itself in case it executes currently
954 * using another scheduler node or in case it is in a blocking state.
955 *
956 * @param[in] context The scheduler instance context.
957 * @param[in] node The node which wants to use the idle thread.
958 * @param[in] get_idle_thread Function to get an idle thread.
959 */
960RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Use_idle_thread(
961  Scheduler_Context         *context,
962  Scheduler_Node            *node,
963  Scheduler_Get_idle_thread  get_idle_thread
964)
965{
966  Thread_Control *idle = ( *get_idle_thread )( context );
967
968  _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER );
969  _Assert( _Scheduler_Node_get_idle( node ) == NULL );
970  _Assert(
971    _Scheduler_Node_get_owner( node ) == _Scheduler_Node_get_user( node )
972  );
973
974  _Scheduler_Thread_set_node( idle, node );
975
976  _Scheduler_Node_set_user( node, idle );
977  node->idle = idle;
978
979  return idle;
980}
981
982/**
983 * @brief Try to schedule this scheduler node.
984 *
985 * @param[in] context The scheduler instance context.
986 * @param[in] node The node which wants to get scheduled.
987 * @param[in] get_idle_thread Function to get an idle thread.
988 *
989 * @retval true This node can be scheduled.
990 * @retval false Otherwise.
991 */
992RTEMS_INLINE_ROUTINE bool _Scheduler_Try_to_schedule_node(
993  Scheduler_Context         *context,
994  Scheduler_Node            *node,
995  Scheduler_Get_idle_thread  get_idle_thread
996)
997{
998  bool schedule;
999  Thread_Control *owner;
1000  Thread_Control *user;
1001
1002  if ( node->help_state == SCHEDULER_HELP_YOURSELF ) {
1003    return true;
1004  }
1005
1006  owner = _Scheduler_Node_get_owner( node );
1007  user = _Scheduler_Node_get_user( node );
1008
1009  if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL) {
1010    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1011      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1012    } else {
1013      _Scheduler_Node_set_user( node, owner );
1014    }
1015
1016    schedule = true;
1017  } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1018    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1019      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1020    } else {
1021      _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1022    }
1023
1024    schedule = true;
1025  } else {
1026    _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1027
1028    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1029      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1030      schedule = true;
1031    } else {
1032      schedule = false;
1033    }
1034  }
1035
1036  return schedule;
1037}
1038
1039/**
1040 * @brief Release an idle thread using this scheduler node.
1041 *
1042 * @param[in] context The scheduler instance context.
1043 * @param[in] node The node which may have an idle thread as user.
1044 * @param[in] release_idle_thread Function to release an idle thread.
1045 *
1046 * @retval idle The idle thread which used this node.
1047 * @retval NULL This node had no idle thread as an user.
1048 */
1049RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Release_idle_thread(
1050  Scheduler_Context             *context,
1051  Scheduler_Node                *node,
1052  Scheduler_Release_idle_thread  release_idle_thread
1053)
1054{
1055  Thread_Control *idle = _Scheduler_Node_get_idle( node );
1056
1057  if ( idle != NULL ) {
1058    Thread_Control *owner = _Scheduler_Node_get_owner( node );
1059
1060    node->idle = NULL;
1061    _Scheduler_Node_set_user( node, owner );
1062    _Scheduler_Thread_change_state( idle, THREAD_SCHEDULER_READY );
1063    _Scheduler_Thread_set_node( idle, idle->Scheduler.own_node );
1064
1065    ( *release_idle_thread )( context, idle );
1066  }
1067
1068  return idle;
1069}
1070
1071/**
1072 * @brief Block this scheduler node.
1073 *
1074 * @param[in] context The scheduler instance context.
1075 * @param[in] node The node which wants to get blocked.
1076 * @param[in] is_scheduled This node is scheduled.
1077 * @param[in] get_idle_thread Function to get an idle thread.
1078 *
1079 * @retval true Continue with the blocking operation.
1080 * @retval false Otherwise.
1081 */
1082RTEMS_INLINE_ROUTINE bool _Scheduler_Block_node(
1083  Scheduler_Context         *context,
1084  Thread_Control            *thread,
1085  Scheduler_Node            *node,
1086  bool                       is_scheduled,
1087  Scheduler_Get_idle_thread  get_idle_thread
1088)
1089{
1090  bool block;
1091  Thread_Control *old_user = _Scheduler_Node_get_user( node );
1092  Thread_Control *new_user = NULL;
1093
1094  _Scheduler_Thread_change_state( old_user, THREAD_SCHEDULER_BLOCKED );
1095
1096  if ( is_scheduled ) {
1097    if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1098      new_user = _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1099    } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL ) {
1100      Thread_Control *owner = _Scheduler_Node_get_owner( node );
1101
1102      if ( thread == old_user && owner != old_user ) {
1103        new_user = owner;
1104        _Scheduler_Node_set_user( node, new_user );
1105      }
1106    }
1107  }
1108
1109  if ( new_user != NULL ) {
1110    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1111
1112    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1113    _Thread_Set_CPU( new_user, cpu );
1114    _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, new_user );
1115
1116    block = false;
1117  } else {
1118    block = true;
1119  }
1120
1121  return block;
1122}
1123
1124/**
1125 * @brief Unblock this scheduler node.
1126 *
1127 * @param[in] context The scheduler instance context.
1128 * @param[in] the_thread The thread which wants to get unblocked.
1129 * @param[in] node The node which wants to get unblocked.
1130 * @param[in] is_scheduled This node is scheduled.
1131 * @param[in] release_idle_thread Function to release an idle thread.
1132 *
1133 * @retval true Continue with the unblocking operation.
1134 * @retval false Otherwise.
1135 */
1136RTEMS_INLINE_ROUTINE bool _Scheduler_Unblock_node(
1137  Scheduler_Context             *context,
1138  Thread_Control                *the_thread,
1139  Scheduler_Node                *node,
1140  bool                           is_scheduled,
1141  Scheduler_Release_idle_thread  release_idle_thread
1142)
1143{
1144  bool unblock;
1145
1146  if ( is_scheduled ) {
1147    Thread_Control *old_user = _Scheduler_Node_get_user( node );
1148    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1149
1150    if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1151      Thread_Control *idle = _Scheduler_Release_idle_thread(
1152        context,
1153        node,
1154        release_idle_thread
1155      );
1156
1157      _Assert( idle != NULL );
1158      (void) idle;
1159    } else {
1160      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1161
1162      _Scheduler_Thread_change_state( old_user, THREAD_SCHEDULER_READY );
1163      _Scheduler_Node_set_user( node, the_thread );
1164    }
1165
1166    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_SCHEDULED );
1167    _Thread_Set_CPU( the_thread, cpu );
1168    _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, the_thread );
1169
1170    unblock = false;
1171  } else {
1172    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1173
1174    unblock = true;
1175  }
1176
1177  return unblock;
1178}
1179
1180/**
1181 * @brief Asks a ready scheduler node for help.
1182 *
1183 * @param[in] node The ready node offering help.
1184 * @param[in] needs_help The thread needing help.
1185 *
1186 * @retval needs_help The thread needing help.
1187 */
1188RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_ready_node_for_help(
1189  Scheduler_Node *node,
1190  Thread_Control *needs_help
1191)
1192{
1193  _Scheduler_Node_set_user( node, needs_help );
1194
1195  return needs_help;
1196}
1197
1198/**
1199 * @brief Asks a scheduled scheduler node for help.
1200 *
1201 * @param[in] context The scheduler instance context.
1202 * @param[in] node The scheduled node offering help.
1203 * @param[in] offers_help The thread offering help.
1204 * @param[in] needs_help The thread needing help.
1205 * @param[in] previous_accepts_help The previous thread accepting help by this
1206 *   scheduler node.
1207 * @param[in] release_idle_thread Function to release an idle thread.
1208 *
1209 * @retval needs_help The previous thread accepting help by this scheduler node
1210 *   which was displaced by the thread needing help.
1211 * @retval NULL There are no more threads needing help.
1212 */
1213RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_scheduled_node_for_help(
1214  Scheduler_Context             *context,
1215  Scheduler_Node                *node,
1216  Thread_Control                *offers_help,
1217  Thread_Control                *needs_help,
1218  Thread_Control                *previous_accepts_help,
1219  Scheduler_Release_idle_thread  release_idle_thread
1220)
1221{
1222  Thread_Control *next_needs_help = NULL;
1223  Thread_Control *old_user = NULL;
1224  Thread_Control *new_user = NULL;
1225
1226  if (
1227    previous_accepts_help != needs_help
1228      && _Scheduler_Thread_get_node( previous_accepts_help ) == node
1229  ) {
1230    Thread_Control *idle = _Scheduler_Release_idle_thread(
1231      context,
1232      node,
1233      release_idle_thread
1234    );
1235
1236    if ( idle != NULL ) {
1237      old_user = idle;
1238    } else {
1239      _Assert( _Scheduler_Node_get_user( node ) == previous_accepts_help );
1240      old_user = previous_accepts_help;
1241    }
1242
1243    if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1244      new_user = needs_help;
1245    } else {
1246      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1247      _Assert( offers_help->Scheduler.node == offers_help->Scheduler.own_node );
1248
1249      new_user = offers_help;
1250    }
1251
1252    if ( previous_accepts_help != offers_help ) {
1253      next_needs_help = previous_accepts_help;
1254    }
1255  } else if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1256    Thread_Control *idle = _Scheduler_Release_idle_thread(
1257      context,
1258      node,
1259      release_idle_thread
1260    );
1261
1262    if ( idle != NULL ) {
1263      old_user = idle;
1264    } else {
1265      old_user = _Scheduler_Node_get_user( node );
1266    }
1267
1268    new_user = needs_help;
1269  } else {
1270    _Assert( needs_help->Scheduler.state == THREAD_SCHEDULER_SCHEDULED );
1271  }
1272
1273  if ( new_user != old_user ) {
1274    Per_CPU_Control *cpu_self = _Per_CPU_Get();
1275    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1276
1277    _Scheduler_Thread_change_state( old_user, THREAD_SCHEDULER_READY );
1278    _Scheduler_Thread_set_scheduler_and_node(
1279      old_user,
1280      _Scheduler_Thread_get_own_node( old_user ),
1281      old_user
1282    );
1283
1284    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1285    _Scheduler_Thread_set_scheduler_and_node( new_user, node, offers_help );
1286
1287    _Scheduler_Node_set_user( node, new_user );
1288    _Thread_Set_CPU( new_user, cpu );
1289    _Thread_Dispatch_update_heir( cpu_self, cpu, new_user );
1290  }
1291
1292  return next_needs_help;
1293}
1294
1295/**
1296 * @brief Asks a blocked scheduler node for help.
1297 *
1298 * @param[in] context The scheduler instance context.
1299 * @param[in] node The scheduled node offering help.
1300 * @param[in] offers_help The thread offering help.
1301 * @param[in] needs_help The thread needing help.
1302 *
1303 * @retval true Enqueue this scheduler node.
1304 * @retval false Otherwise.
1305 */
1306RTEMS_INLINE_ROUTINE bool _Scheduler_Ask_blocked_node_for_help(
1307  Scheduler_Context *context,
1308  Scheduler_Node    *node,
1309  Thread_Control    *offers_help,
1310  Thread_Control    *needs_help
1311)
1312{
1313  bool enqueue;
1314
1315  _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1316
1317  if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1318    _Scheduler_Node_set_user( node, needs_help );
1319    _Scheduler_Thread_set_scheduler_and_node( needs_help, node, offers_help );
1320
1321    enqueue = true;
1322  } else {
1323    enqueue = false;
1324  }
1325
1326  return enqueue;
1327}
1328#endif
1329
1330/** @} */
1331
1332#ifdef __cplusplus
1333}
1334#endif
1335
1336#endif
1337/* end of include file */
Note: See TracBrowser for help on using the repository browser.