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

4.115
Last change on this file since a7e4de2 was a7e4de2, checked in by Joel Sherrill <joel.sherrill@…>, on 03/06/15 at 20:33:08

Fix even more Doxygen issues

  • Property mode set to 100644
File size: 37.4 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 * The operation must update the heir and thread dispatch necessary variables
318 * in case the set of scheduled threads changes.
319 *
320 * @param[in] the_thread The thread changing its priority.
321 * @param[in] new_priority The new thread priority.
322 * @param[in] prepend_it In case this is true, then enqueue the thread as the
323 * first of its priority group, otherwise enqueue the thread as the last of its
324 * priority group.
325 */
326RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority(
327  Thread_Control          *the_thread,
328  Priority_Control         new_priority,
329  bool                     prepend_it
330)
331{
332  const Scheduler_Control *scheduler = _Scheduler_Get_own( the_thread );
333#if defined(RTEMS_SMP)
334  Thread_Control *needs_help;
335
336  needs_help =
337#endif
338  ( *scheduler->Operations.change_priority )(
339    scheduler,
340    the_thread,
341    new_priority,
342    prepend_it
343  );
344
345#if defined(RTEMS_SMP)
346  _Scheduler_Ask_for_help_if_necessary( needs_help );
347#endif
348}
349
350/**
351 * @brief Initializes a scheduler node.
352 *
353 * The scheduler node contains arbitrary data on function entry.  The caller
354 * must ensure that _Scheduler_Node_destroy() will be called after a
355 * _Scheduler_Node_initialize() before the memory of the scheduler node is
356 * destroyed.
357 *
358 * @param[in] scheduler The scheduler instance.
359 * @param[in] the_thread The thread containing the scheduler node.
360 */
361RTEMS_INLINE_ROUTINE void _Scheduler_Node_initialize(
362  const Scheduler_Control *scheduler,
363  Thread_Control          *the_thread
364)
365{
366  return ( *scheduler->Operations.node_initialize )( scheduler, the_thread );
367}
368
369/**
370 * @brief Destroys a scheduler node.
371 *
372 * The caller must ensure that _Scheduler_Node_destroy() will be called only
373 * after a corresponding _Scheduler_Node_initialize().
374 *
375 * @param[in] scheduler The scheduler instance.
376 * @param[in] the_thread The thread containing the scheduler node.
377 */
378RTEMS_INLINE_ROUTINE void _Scheduler_Node_destroy(
379  const Scheduler_Control *scheduler,
380  Thread_Control          *the_thread
381)
382{
383  ( *scheduler->Operations.node_destroy )( scheduler, the_thread );
384}
385
386/**
387 * @brief Updates the scheduler about a priority change of a not ready thread.
388 *
389 * @param[in] the_thread The thread.
390 * @param[in] new_priority The new priority of the thread.
391 */
392RTEMS_INLINE_ROUTINE void _Scheduler_Update_priority(
393  Thread_Control   *the_thread,
394  Priority_Control  new_priority
395)
396{
397  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
398
399  ( *scheduler->Operations.update_priority )(
400    scheduler,
401    the_thread,
402    new_priority
403  );
404}
405
406/**
407 * @brief Compares two priority values.
408 *
409 * @param[in] scheduler The scheduler instance.
410 * @param[in] p1 The first priority value.
411 * @param[in] p2 The second priority value.
412 *
413 * @retval negative The value @a p1 encodes a lower priority than @a p2 in the
414 * intuitive sense of priority.
415 * @retval 0 The priorities @a p1 and @a p2 are equal.
416 * @retval positive The value @a p1 encodes a higher priority than @a p2 in the
417 * intuitive sense of priority.
418 *
419 * @see _Scheduler_Is_priority_lower_than() and
420 * _Scheduler_Is_priority_higher_than().
421 */
422RTEMS_INLINE_ROUTINE int _Scheduler_Priority_compare(
423  const Scheduler_Control *scheduler,
424  Priority_Control         p1,
425  Priority_Control         p2
426)
427{
428  return ( *scheduler->Operations.priority_compare )( p1, p2 );
429}
430
431/**
432 * @brief Releases a job of a thread with respect to the scheduler.
433 *
434 * @param[in] the_thread The thread.
435 * @param[in] length The period length.
436 */
437RTEMS_INLINE_ROUTINE void _Scheduler_Release_job(
438  Thread_Control *the_thread,
439  uint32_t        length
440)
441{
442  const Scheduler_Control *scheduler = _Scheduler_Get( the_thread );
443
444  ( *scheduler->Operations.release_job )( scheduler, the_thread, length );
445}
446
447/**
448 * @brief Scheduler method invoked at each clock tick.
449 *
450 * This method is invoked at each clock tick to allow the scheduler
451 * implementation to perform any activities required.  For the
452 * scheduler which support standard RTEMS features, this includes
453 * time-slicing management.
454 */
455RTEMS_INLINE_ROUTINE void _Scheduler_Tick( void )
456{
457  uint32_t cpu_count = _SMP_Get_processor_count();
458  uint32_t cpu_index;
459
460  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
461    const Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );
462    const Scheduler_Control *scheduler = _Scheduler_Get_by_CPU( cpu );
463    Thread_Control *executing = cpu->executing;
464
465    if ( scheduler != NULL && executing != NULL ) {
466      ( *scheduler->Operations.tick )( scheduler, executing );
467    }
468  }
469}
470
471/**
472 * @brief Starts the idle thread for a particular processor.
473 *
474 * @param[in] scheduler The scheduler instance.
475 * @param[in,out] the_thread The idle thread for the processor.
476 * @param[in,out] cpu The processor for the idle thread.
477 *
478 * @see _Thread_Create_idle().
479 */
480RTEMS_INLINE_ROUTINE void _Scheduler_Start_idle(
481  const Scheduler_Control *scheduler,
482  Thread_Control          *the_thread,
483  Per_CPU_Control         *cpu
484)
485{
486  ( *scheduler->Operations.start_idle )( scheduler, the_thread, cpu );
487}
488
489#if defined(RTEMS_SMP)
490RTEMS_INLINE_ROUTINE const Scheduler_Assignment *_Scheduler_Get_assignment(
491  uint32_t cpu_index
492)
493{
494  return &_Scheduler_Assignments[ cpu_index ];
495}
496
497RTEMS_INLINE_ROUTINE bool _Scheduler_Is_mandatory_processor(
498  const Scheduler_Assignment *assignment
499)
500{
501  return (assignment->attributes & SCHEDULER_ASSIGN_PROCESSOR_MANDATORY) != 0;
502}
503
504RTEMS_INLINE_ROUTINE bool _Scheduler_Should_start_processor(
505  const Scheduler_Assignment *assignment
506)
507{
508  return assignment->scheduler != NULL;
509}
510#endif /* defined(RTEMS_SMP) */
511
512RTEMS_INLINE_ROUTINE bool _Scheduler_Has_processor_ownership(
513  const Scheduler_Control *scheduler,
514  uint32_t cpu_index
515)
516{
517#if defined(RTEMS_SMP)
518  const Scheduler_Assignment *assignment =
519    _Scheduler_Get_assignment( cpu_index );
520
521  return assignment->scheduler == scheduler;
522#else
523  (void) scheduler;
524  (void) cpu_index;
525
526  return true;
527#endif
528}
529
530RTEMS_INLINE_ROUTINE void _Scheduler_Set(
531  const Scheduler_Control *scheduler,
532  Thread_Control          *the_thread
533)
534{
535#if defined(RTEMS_SMP)
536  const Scheduler_Control *current_scheduler = _Scheduler_Get( the_thread );
537
538  if ( current_scheduler != scheduler ) {
539    _Thread_Set_state( the_thread, STATES_MIGRATING );
540    _Scheduler_Node_destroy( current_scheduler, the_thread );
541    the_thread->Scheduler.own_control = scheduler;
542    the_thread->Scheduler.control = scheduler;
543    _Scheduler_Node_initialize( scheduler, the_thread );
544    _Scheduler_Update_priority( the_thread, the_thread->current_priority );
545    _Thread_Clear_state( the_thread, STATES_MIGRATING );
546  }
547#else
548  (void) scheduler;
549#endif
550}
551
552#if defined(__RTEMS_HAVE_SYS_CPUSET_H__)
553
554RTEMS_INLINE_ROUTINE void _Scheduler_Get_processor_set(
555  const Scheduler_Control *scheduler,
556  size_t                   cpusetsize,
557  cpu_set_t               *cpuset
558)
559{
560  uint32_t cpu_count = _SMP_Get_processor_count();
561  uint32_t cpu_index;
562
563  CPU_ZERO_S( cpusetsize, cpuset );
564
565  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
566#if defined(RTEMS_SMP)
567    if ( _Scheduler_Has_processor_ownership( scheduler, cpu_index ) ) {
568      CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
569    }
570#else
571    (void) scheduler;
572
573    CPU_SET_S( (int) cpu_index, cpusetsize, cpuset );
574#endif
575  }
576}
577
578RTEMS_INLINE_ROUTINE bool _Scheduler_default_Get_affinity_body(
579  const Scheduler_Control *scheduler,
580  Thread_Control          *the_thread,
581  size_t                   cpusetsize,
582  cpu_set_t               *cpuset
583)
584{
585  (void) the_thread;
586
587  _Scheduler_Get_processor_set( scheduler, cpusetsize, cpuset );
588
589  return true;
590}
591
592bool _Scheduler_Get_affinity(
593  Thread_Control *the_thread,
594  size_t          cpusetsize,
595  cpu_set_t      *cpuset
596);
597
598RTEMS_INLINE_ROUTINE bool _Scheduler_default_Set_affinity_body(
599  const Scheduler_Control *scheduler,
600  Thread_Control          *the_thread,
601  size_t                   cpusetsize,
602  const cpu_set_t         *cpuset
603)
604{
605  uint32_t cpu_count = _SMP_Get_processor_count();
606  uint32_t cpu_index;
607  bool     ok = true;
608
609  for ( cpu_index = 0 ; cpu_index < cpu_count ; ++cpu_index ) {
610#if defined(RTEMS_SMP)
611    const Scheduler_Control *scheduler_of_cpu =
612      _Scheduler_Get_by_CPU_index( cpu_index );
613
614    ok = ok
615      && ( CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
616        || ( !CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset )
617          && scheduler != scheduler_of_cpu ) );
618#else
619    (void) scheduler;
620
621    ok = ok && CPU_ISSET_S( (int) cpu_index, cpusetsize, cpuset );
622#endif
623  }
624
625  return ok;
626}
627
628bool _Scheduler_Set_affinity(
629  Thread_Control          *the_thread,
630  size_t                   cpusetsize,
631  const cpu_set_t         *cpuset
632);
633
634#endif /* defined(__RTEMS_HAVE_SYS_CPUSET_H__) */
635
636RTEMS_INLINE_ROUTINE void _Scheduler_Update_heir(
637  Thread_Control *new_heir,
638  bool            force_dispatch
639)
640{
641  Thread_Control *heir = _Thread_Heir;
642
643  if ( heir != new_heir && ( heir->is_preemptible || force_dispatch ) ) {
644    _Thread_Heir = new_heir;
645    _Thread_Dispatch_necessary = true;
646  }
647}
648
649RTEMS_INLINE_ROUTINE void _Scheduler_Generic_block(
650  const Scheduler_Control *scheduler,
651  Thread_Control          *the_thread,
652  void                  ( *extract )(
653                             const Scheduler_Control *,
654                             Thread_Control * ),
655  void                  ( *schedule )(
656                             const Scheduler_Control *,
657                             Thread_Control *,
658                             bool )
659)
660{
661  ( *extract )( scheduler, the_thread );
662
663  /* TODO: flash critical section? */
664
665  if ( _Thread_Is_executing( the_thread ) || _Thread_Is_heir( the_thread ) ) {
666    ( *schedule )( scheduler, the_thread, true );
667  }
668}
669
670/**
671 * @brief Returns true if @a p1 encodes a lower priority than @a p2 in the
672 * intuitive sense of priority.
673 */
674RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_lower_than(
675  const Scheduler_Control *scheduler,
676  Priority_Control         p1,
677  Priority_Control         p2
678)
679{
680  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) < 0;
681}
682
683/**
684 * @brief Returns true if @a p1 encodes a higher priority than @a p2 in the
685 * intuitive sense of priority.
686 */
687RTEMS_INLINE_ROUTINE bool _Scheduler_Is_priority_higher_than(
688  const Scheduler_Control *scheduler,
689  Priority_Control         p1,
690  Priority_Control         p2
691)
692{
693  return _Scheduler_Priority_compare( scheduler, p1,  p2 ) > 0;
694}
695
696/**
697 * @brief Returns the priority encoding @a p1 or @a p2 with the higher priority
698 * in the intuitive sense of priority.
699 */
700RTEMS_INLINE_ROUTINE Priority_Control _Scheduler_Highest_priority_of_two(
701  const Scheduler_Control *scheduler,
702  Priority_Control         p1,
703  Priority_Control         p2
704)
705{
706  return _Scheduler_Is_priority_higher_than( scheduler, p1, p2 ) ? p1 : p2;
707}
708
709/**
710 * @brief Sets the thread priority to @a priority if it is higher than the
711 * current priority of the thread in the intuitive sense of priority.
712 */
713RTEMS_INLINE_ROUTINE void _Scheduler_Set_priority_if_higher(
714  const Scheduler_Control *scheduler,
715  Thread_Control          *the_thread,
716  Priority_Control         priority
717)
718{
719  Priority_Control current = the_thread->current_priority;
720
721  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
722    _Thread_Set_priority( the_thread, priority );
723  }
724}
725
726/**
727 * @brief Changes the thread priority to @a priority if it is higher than the
728 * current priority of the thread in the intuitive sense of priority.
729 */
730RTEMS_INLINE_ROUTINE void _Scheduler_Change_priority_if_higher(
731  const Scheduler_Control *scheduler,
732  Thread_Control          *the_thread,
733  Priority_Control         priority,
734  bool                     prepend_it
735)
736{
737  Priority_Control current = the_thread->current_priority;
738
739  if ( _Scheduler_Is_priority_higher_than( scheduler, priority, current ) ) {
740    _Thread_Change_priority( the_thread, priority, prepend_it );
741  }
742}
743
744RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_processor_count(
745  const Scheduler_Control *scheduler
746)
747{
748#if defined(RTEMS_SMP)
749  return _Scheduler_Get_context( scheduler )->processor_count;
750#else
751  (void) scheduler;
752
753  return 1;
754#endif
755}
756
757RTEMS_INLINE_ROUTINE Objects_Id _Scheduler_Build_id( uint32_t scheduler_index )
758{
759  return _Objects_Build_id(
760    OBJECTS_FAKE_OBJECTS_API,
761    OBJECTS_FAKE_OBJECTS_SCHEDULERS,
762    _Objects_Local_node,
763    scheduler_index + 1
764  );
765}
766
767RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index_by_id( Objects_Id id )
768{
769  uint32_t minimum_id = _Scheduler_Build_id( 0 );
770
771  return id - minimum_id;
772}
773
774RTEMS_INLINE_ROUTINE bool _Scheduler_Get_by_id(
775  Objects_Id                id,
776  const Scheduler_Control **scheduler_p
777)
778{
779  uint32_t index = _Scheduler_Get_index_by_id( id );
780  const Scheduler_Control *scheduler = &_Scheduler_Table[ index ];
781
782  *scheduler_p = scheduler;
783
784  return index < _Scheduler_Count
785    && _Scheduler_Get_processor_count( scheduler ) > 0;
786}
787
788RTEMS_INLINE_ROUTINE bool _Scheduler_Is_id_valid( Objects_Id id )
789{
790  const Scheduler_Control *scheduler;
791  bool ok = _Scheduler_Get_by_id( id, &scheduler );
792
793  (void) scheduler;
794
795  return ok;
796}
797
798RTEMS_INLINE_ROUTINE uint32_t _Scheduler_Get_index(
799  const Scheduler_Control *scheduler
800)
801{
802  return (uint32_t) (scheduler - &_Scheduler_Table[ 0 ]);
803}
804
805RTEMS_INLINE_ROUTINE Scheduler_Node *_Scheduler_Thread_get_node(
806  const Thread_Control *the_thread
807)
808{
809  return the_thread->Scheduler.node;
810}
811
812RTEMS_INLINE_ROUTINE void _Scheduler_Node_do_initialize(
813  Scheduler_Node *node,
814  Thread_Control *the_thread
815)
816{
817#if defined(RTEMS_SMP)
818  node->user = the_thread;
819  node->help_state = SCHEDULER_HELP_YOURSELF;
820  node->owner = the_thread;
821  node->idle = NULL;
822  node->accepts_help = the_thread;
823#else
824  (void) node;
825  (void) the_thread;
826#endif
827}
828
829#if defined(RTEMS_SMP)
830/**
831 * @brief Gets an idle thread from the scheduler instance.
832 *
833 * @param[in] context The scheduler instance context.
834 *
835 * @retval idle An idle thread for use.  This function must always return an
836 * idle thread.  If none is available, then this is a fatal error.
837 */
838typedef Thread_Control *( *Scheduler_Get_idle_thread )(
839  Scheduler_Context *context
840);
841
842/**
843 * @brief Releases an idle thread to the scheduler instance for reuse.
844 *
845 * @param[in] context The scheduler instance context.
846 * @param[in] idle The idle thread to release
847 */
848typedef void ( *Scheduler_Release_idle_thread )(
849  Scheduler_Context *context,
850  Thread_Control    *idle
851);
852
853RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Node_get_owner(
854  const Scheduler_Node *node
855)
856{
857  return node->owner;
858}
859
860RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Node_get_idle(
861  const Scheduler_Node *node
862)
863{
864  return node->idle;
865}
866
867RTEMS_INLINE_ROUTINE void _Scheduler_Node_set_user(
868  Scheduler_Node *node,
869  Thread_Control *user
870)
871{
872  node->user = user;
873}
874
875RTEMS_INLINE_ROUTINE void _Scheduler_Thread_set_node(
876  Thread_Control *the_thread,
877  Scheduler_Node *node
878)
879{
880  the_thread->Scheduler.node = node;
881}
882
883RTEMS_INLINE_ROUTINE void _Scheduler_Thread_set_scheduler_and_node(
884  Thread_Control       *the_thread,
885  Scheduler_Node       *node,
886  const Thread_Control *previous_user_of_node
887)
888{
889  const Scheduler_Control *scheduler =
890    _Scheduler_Get_own( previous_user_of_node );
891
892  the_thread->Scheduler.control = scheduler;
893  _Scheduler_Thread_set_node( the_thread, node );
894}
895
896extern const bool _Scheduler_Thread_state_valid_state_changes[ 3 ][ 3 ];
897
898RTEMS_INLINE_ROUTINE void _Scheduler_Thread_change_state(
899  Thread_Control         *the_thread,
900  Thread_Scheduler_state  new_state
901)
902{
903  _Assert(
904    _Scheduler_Thread_state_valid_state_changes
905      [ the_thread->Scheduler.state ][ new_state ]
906  );
907
908  the_thread->Scheduler.state = new_state;
909}
910
911/**
912 * @brief Changes the scheduler help state of a thread.
913 *
914 * @param[in] the_thread The thread.
915 * @param[in] new_help_state The new help state.
916 *
917 * @return The previous help state.
918 */
919RTEMS_INLINE_ROUTINE Scheduler_Help_state _Scheduler_Thread_change_help_state(
920  Thread_Control       *the_thread,
921  Scheduler_Help_state  new_help_state
922)
923{
924  Scheduler_Node *node = _Scheduler_Thread_get_own_node( the_thread );
925  Scheduler_Help_state previous_help_state = node->help_state;
926
927  node->help_state = new_help_state;
928
929  return previous_help_state;
930}
931
932/**
933 * @brief Changes the resource tree root of a thread.
934 *
935 * For each node of the resource sub-tree specified by the top thread the
936 * scheduler asks for help.  So the root thread gains access to all scheduler
937 * nodes corresponding to the resource sub-tree.  In case a thread previously
938 * granted help is displaced by this operation, then the scheduler asks for
939 * help using its remaining resource tree.
940 *
941 * The run-time of this function depends on the size of the resource sub-tree
942 * and other resource trees in case threads in need for help are produced
943 * during this operation.
944 *
945 * @param[in] top The thread specifying the resource sub-tree top.
946 * @param[in] root The thread specifying the new resource sub-tree root.
947 */
948void _Scheduler_Thread_change_resource_root(
949  Thread_Control *top,
950  Thread_Control *root
951);
952
953/**
954 * @brief Use an idle thread for this scheduler node.
955 *
956 * A thread in the SCHEDULER_HELP_ACTIVE_OWNER or SCHEDULER_HELP_ACTIVE_RIVAL
957 * helping state may use an idle thread for the scheduler node owned by itself
958 * in case it executes currently using another scheduler node or in case it is
959 * in a blocking state.
960 *
961 * @param[in] context The scheduler instance context.
962 * @param[in] node The node which wants to use the idle thread.
963 * @param[in] get_idle_thread Function to get an idle thread.
964 */
965RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Use_idle_thread(
966  Scheduler_Context         *context,
967  Scheduler_Node            *node,
968  Scheduler_Get_idle_thread  get_idle_thread
969)
970{
971  Thread_Control *idle = ( *get_idle_thread )( context );
972
973  _Assert(
974    node->help_state == SCHEDULER_HELP_ACTIVE_OWNER
975      || node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL
976  );
977  _Assert( _Scheduler_Node_get_idle( node ) == NULL );
978  _Assert(
979    _Scheduler_Node_get_owner( node ) == _Scheduler_Node_get_user( node )
980  );
981
982  _Scheduler_Thread_set_node( idle, node );
983
984  _Scheduler_Node_set_user( node, idle );
985  node->idle = idle;
986
987  return idle;
988}
989
990/**
991 * @brief Try to schedule this scheduler node.
992 *
993 * @param[in] context The scheduler instance context.
994 * @param[in] node The node which wants to get scheduled.
995 * @param[in] get_idle_thread Function to get an idle thread.
996 *
997 * @retval true This node can be scheduled.
998 * @retval false Otherwise.
999 */
1000RTEMS_INLINE_ROUTINE bool _Scheduler_Try_to_schedule_node(
1001  Scheduler_Context         *context,
1002  Scheduler_Node            *node,
1003  Scheduler_Get_idle_thread  get_idle_thread
1004)
1005{
1006  bool schedule;
1007  Thread_Control *owner;
1008  Thread_Control *user;
1009
1010  if ( node->help_state == SCHEDULER_HELP_YOURSELF ) {
1011    return true;
1012  }
1013
1014  owner = _Scheduler_Node_get_owner( node );
1015  user = _Scheduler_Node_get_user( node );
1016
1017  if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL) {
1018    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1019      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1020    } else if ( owner->Scheduler.state == THREAD_SCHEDULER_BLOCKED ) {
1021      _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1022    } else {
1023      _Scheduler_Node_set_user( node, owner );
1024    }
1025
1026    schedule = true;
1027  } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1028    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1029      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1030    } else {
1031      _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1032    }
1033
1034    schedule = true;
1035  } else {
1036    _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1037
1038    if ( user->Scheduler.state == THREAD_SCHEDULER_READY ) {
1039      _Scheduler_Thread_set_scheduler_and_node( user, node, owner );
1040      schedule = true;
1041    } else {
1042      schedule = false;
1043    }
1044  }
1045
1046  return schedule;
1047}
1048
1049/**
1050 * @brief Release an idle thread using this scheduler node.
1051 *
1052 * @param[in] context The scheduler instance context.
1053 * @param[in] node The node which may have an idle thread as user.
1054 * @param[in] release_idle_thread Function to release an idle thread.
1055 *
1056 * @retval idle The idle thread which used this node.
1057 * @retval NULL This node had no idle thread as an user.
1058 */
1059RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Release_idle_thread(
1060  Scheduler_Context             *context,
1061  Scheduler_Node                *node,
1062  Scheduler_Release_idle_thread  release_idle_thread
1063)
1064{
1065  Thread_Control *idle = _Scheduler_Node_get_idle( node );
1066
1067  if ( idle != NULL ) {
1068    Thread_Control *owner = _Scheduler_Node_get_owner( node );
1069
1070    node->idle = NULL;
1071    _Scheduler_Node_set_user( node, owner );
1072    _Scheduler_Thread_change_state( idle, THREAD_SCHEDULER_READY );
1073    _Scheduler_Thread_set_node( idle, idle->Scheduler.own_node );
1074
1075    ( *release_idle_thread )( context, idle );
1076  }
1077
1078  return idle;
1079}
1080
1081/**
1082 * @brief Block this scheduler node.
1083 *
1084 * @param[in] context The scheduler instance context.
1085 * @param[in] thread The thread which wants to get blocked referencing this
1086 *   node.  This is not necessarily the user of this node in case the node
1087 *   participates in the scheduler helping protocol.
1088 * @param[in] node The node which wants to get blocked.
1089 * @param[in] is_scheduled This node is scheduled.
1090 * @param[in] get_idle_thread Function to get an idle thread.
1091 *
1092 * @retval true Continue with the blocking operation.
1093 * @retval false Otherwise.
1094 */
1095RTEMS_INLINE_ROUTINE bool _Scheduler_Block_node(
1096  Scheduler_Context         *context,
1097  Thread_Control            *thread,
1098  Scheduler_Node            *node,
1099  bool                       is_scheduled,
1100  Scheduler_Get_idle_thread  get_idle_thread
1101)
1102{
1103  Thread_Control *old_user;
1104  Thread_Control *new_user;
1105
1106  _Scheduler_Thread_change_state( thread, THREAD_SCHEDULER_BLOCKED );
1107
1108  if ( node->help_state == SCHEDULER_HELP_YOURSELF ) {
1109    _Assert( thread == _Scheduler_Node_get_user( node ) );
1110
1111    return true;
1112  }
1113
1114  new_user = NULL;
1115
1116  if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1117    if ( is_scheduled ) {
1118      _Assert( thread == _Scheduler_Node_get_user( node ) );
1119      old_user = thread;
1120      new_user = _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1121    }
1122  } else if ( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL ) {
1123    if ( is_scheduled ) {
1124      old_user = _Scheduler_Node_get_user( node );
1125
1126      if ( thread == old_user ) {
1127        Thread_Control *owner = _Scheduler_Node_get_owner( node );
1128
1129        if (
1130          thread != owner
1131            && owner->Scheduler.state == THREAD_SCHEDULER_READY
1132        ) {
1133          new_user = owner;
1134          _Scheduler_Node_set_user( node, new_user );
1135        } else {
1136          new_user = _Scheduler_Use_idle_thread( context, node, get_idle_thread );
1137        }
1138      }
1139    }
1140  } else {
1141    /* Not implemented, this is part of the OMIP support path. */
1142    _Assert(0);
1143  }
1144
1145  if ( new_user != NULL ) {
1146    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1147
1148    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1149    _Thread_Set_CPU( new_user, cpu );
1150    _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, new_user );
1151  }
1152
1153  return false;
1154}
1155
1156/**
1157 * @brief Unblock this scheduler node.
1158 *
1159 * @param[in] context The scheduler instance context.
1160 * @param[in] the_thread The thread which wants to get unblocked.
1161 * @param[in] node The node which wants to get unblocked.
1162 * @param[in] is_scheduled This node is scheduled.
1163 * @param[in] release_idle_thread Function to release an idle thread.
1164 *
1165 * @retval true Continue with the unblocking operation.
1166 * @retval false Otherwise.
1167 */
1168RTEMS_INLINE_ROUTINE bool _Scheduler_Unblock_node(
1169  Scheduler_Context             *context,
1170  Thread_Control                *the_thread,
1171  Scheduler_Node                *node,
1172  bool                           is_scheduled,
1173  Scheduler_Release_idle_thread  release_idle_thread
1174)
1175{
1176  bool unblock;
1177
1178  if ( is_scheduled ) {
1179    Thread_Control *old_user = _Scheduler_Node_get_user( node );
1180    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1181    Thread_Control *idle = _Scheduler_Release_idle_thread(
1182      context,
1183      node,
1184      release_idle_thread
1185    );
1186    Thread_Control *owner = _Scheduler_Node_get_owner( node );
1187    Thread_Control *new_user;
1188
1189    if ( node->help_state == SCHEDULER_HELP_ACTIVE_OWNER ) {
1190      _Assert( idle != NULL );
1191      new_user = the_thread;
1192    } else if ( idle != NULL ) {
1193      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1194      new_user = the_thread;
1195    } else if ( the_thread != owner ) {
1196      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1197      _Assert( old_user != the_thread );
1198      _Scheduler_Thread_change_state( owner, THREAD_SCHEDULER_READY );
1199      new_user = the_thread;
1200      _Scheduler_Node_set_user( node, new_user );
1201    } else {
1202      _Assert( node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL );
1203      _Assert( old_user != the_thread );
1204      _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1205      new_user = NULL;
1206    }
1207
1208    if ( new_user != NULL ) {
1209      _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1210      _Thread_Set_CPU( new_user, cpu );
1211      _Thread_Dispatch_update_heir( _Per_CPU_Get(), cpu, new_user );
1212    }
1213
1214    unblock = false;
1215  } else {
1216    _Scheduler_Thread_change_state( the_thread, THREAD_SCHEDULER_READY );
1217
1218    unblock = true;
1219  }
1220
1221  return unblock;
1222}
1223
1224/**
1225 * @brief Asks a ready scheduler node for help.
1226 *
1227 * @param[in] node The ready node offering help.
1228 * @param[in] needs_help The thread needing help.
1229 *
1230 * @retval needs_help The thread needing help.
1231 */
1232RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_ready_node_for_help(
1233  Scheduler_Node *node,
1234  Thread_Control *needs_help
1235)
1236{
1237  _Scheduler_Node_set_user( node, needs_help );
1238
1239  return needs_help;
1240}
1241
1242/**
1243 * @brief Asks a scheduled scheduler node for help.
1244 *
1245 * @param[in] context The scheduler instance context.
1246 * @param[in] node The scheduled node offering help.
1247 * @param[in] offers_help The thread offering help.
1248 * @param[in] needs_help The thread needing help.
1249 * @param[in] previous_accepts_help The previous thread accepting help by this
1250 *   scheduler node.
1251 * @param[in] release_idle_thread Function to release an idle thread.
1252 *
1253 * @retval needs_help The previous thread accepting help by this scheduler node
1254 *   which was displaced by the thread needing help.
1255 * @retval NULL There are no more threads needing help.
1256 */
1257RTEMS_INLINE_ROUTINE Thread_Control *_Scheduler_Ask_scheduled_node_for_help(
1258  Scheduler_Context             *context,
1259  Scheduler_Node                *node,
1260  Thread_Control                *offers_help,
1261  Thread_Control                *needs_help,
1262  Thread_Control                *previous_accepts_help,
1263  Scheduler_Release_idle_thread  release_idle_thread
1264)
1265{
1266  Thread_Control *next_needs_help = NULL;
1267  Thread_Control *old_user = NULL;
1268  Thread_Control *new_user = NULL;
1269
1270  if (
1271    previous_accepts_help != needs_help
1272      && _Scheduler_Thread_get_node( previous_accepts_help ) == node
1273  ) {
1274    Thread_Control *idle = _Scheduler_Release_idle_thread(
1275      context,
1276      node,
1277      release_idle_thread
1278    );
1279
1280    if ( idle != NULL ) {
1281      old_user = idle;
1282    } else {
1283      _Assert( _Scheduler_Node_get_user( node ) == previous_accepts_help );
1284      old_user = previous_accepts_help;
1285    }
1286
1287    if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1288      new_user = needs_help;
1289    } else {
1290      _Assert(
1291        node->help_state == SCHEDULER_HELP_ACTIVE_OWNER
1292          || node->help_state == SCHEDULER_HELP_ACTIVE_RIVAL
1293      );
1294      _Assert( offers_help->Scheduler.node == offers_help->Scheduler.own_node );
1295
1296      new_user = offers_help;
1297    }
1298
1299    if ( previous_accepts_help != offers_help ) {
1300      next_needs_help = previous_accepts_help;
1301    }
1302  } else if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1303    Thread_Control *idle = _Scheduler_Release_idle_thread(
1304      context,
1305      node,
1306      release_idle_thread
1307    );
1308
1309    if ( idle != NULL ) {
1310      old_user = idle;
1311    } else {
1312      old_user = _Scheduler_Node_get_user( node );
1313    }
1314
1315    new_user = needs_help;
1316  } else {
1317    _Assert( needs_help->Scheduler.state == THREAD_SCHEDULER_SCHEDULED );
1318  }
1319
1320  if ( new_user != old_user ) {
1321    Per_CPU_Control *cpu_self = _Per_CPU_Get();
1322    Per_CPU_Control *cpu = _Thread_Get_CPU( old_user );
1323
1324    _Scheduler_Thread_change_state( old_user, THREAD_SCHEDULER_READY );
1325    _Scheduler_Thread_set_scheduler_and_node(
1326      old_user,
1327      _Scheduler_Thread_get_own_node( old_user ),
1328      old_user
1329    );
1330
1331    _Scheduler_Thread_change_state( new_user, THREAD_SCHEDULER_SCHEDULED );
1332    _Scheduler_Thread_set_scheduler_and_node( new_user, node, offers_help );
1333
1334    _Scheduler_Node_set_user( node, new_user );
1335    _Thread_Set_CPU( new_user, cpu );
1336    _Thread_Dispatch_update_heir( cpu_self, cpu, new_user );
1337  }
1338
1339  return next_needs_help;
1340}
1341
1342/**
1343 * @brief Asks a blocked scheduler node for help.
1344 *
1345 * @param[in] context The scheduler instance context.
1346 * @param[in] node The scheduled node offering help.
1347 * @param[in] offers_help The thread offering help.
1348 * @param[in] needs_help The thread needing help.
1349 *
1350 * @retval true Enqueue this scheduler node.
1351 * @retval false Otherwise.
1352 */
1353RTEMS_INLINE_ROUTINE bool _Scheduler_Ask_blocked_node_for_help(
1354  Scheduler_Context *context,
1355  Scheduler_Node    *node,
1356  Thread_Control    *offers_help,
1357  Thread_Control    *needs_help
1358)
1359{
1360  bool enqueue;
1361
1362  _Assert( node->help_state == SCHEDULER_HELP_PASSIVE );
1363
1364  if ( needs_help->Scheduler.state == THREAD_SCHEDULER_READY ) {
1365    _Scheduler_Node_set_user( node, needs_help );
1366    _Scheduler_Thread_set_scheduler_and_node( needs_help, node, offers_help );
1367
1368    enqueue = true;
1369  } else {
1370    enqueue = false;
1371  }
1372
1373  return enqueue;
1374}
1375#endif
1376
1377/** @} */
1378
1379#ifdef __cplusplus
1380}
1381#endif
1382
1383#endif
1384/* end of include file */
Note: See TracBrowser for help on using the repository browser.