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

5
Last change on this file since d37adfe5 was d37adfe5, checked in by Sebastian Huber <sebastian.huber@…>, on 03/03/16 at 06:02:03

score: Fix CPU time used by executing threads

The CPU time used of a thread was previously maintained per-processor
mostly during _Thread_Dispatch(). However, on SMP configurations the
actual processor of a thread is difficult to figure out since thread
dispatching is a highly asynchronous process (e.g. via inter-processor
interrupts). Only the intended processor of a thread is known to the
scheduler easily. Do the CPU usage accounting during thread heir
updates in the context of the scheduler operations. Provide the
function _Thread_Get_CPU_time_used() to get the CPU usage of a thread
using proper locks to get a consistent value.

Close #2627.

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