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

4.115
Last change on this file since b8a5abf was b8a5abf, checked in by Sebastian Huber <sebastian.huber@…>, on 02/26/15 at 09:33:36

score: Update _Thread_Heir only if necessary

Previously, the _Thread_Heir was updated unconditionally in case a new
heir was determined. The _Thread_Dispatch_necessary was only updated in
case the executing thread was preemptible or an internal thread was
unblocked. Change this to update the _Thread_Heir and
_Thread_Dispatch_necessary only in case the currently selected heir
thread is preemptible or a dispatch is forced. Move the schedule
decision into the change priority operation and use the schedule
operation only in rtems_task_mode() in case preemption is enabled or an
ASR dispatch is necessary. This is a behaviour change. Previously, the
RTEMS_NO_PREEMPT also prevented signal delivery in certain cases (not
always). Now, signal delivery is no longer influenced by
RTEMS_NO_PREEMPT. Since the currently selected heir thread is used to
determine if a new heir is chosen, non-preemptible heir threads
currently not executing now prevent a new heir. This may have an
application impact, see change test tm04. Document this change in sp04.

Update #2273.

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