Need help understanding the function Scheduler_SMP_Allocate_processor_lazy

Richi Dubey richidubey at gmail.com
Sat Jun 20 12:11:41 UTC 2020


Hi,

I've been learning how SMP scheduler word in RTEMS and I came across the
following function definition:
--------------------------------------------------------------------------------------------

/**
 * @brief Allocates the cpu for the scheduled thread.
 *
 * Attempts to prevent migrations but does not take into account affinity.
 *
 * @param context The scheduler context instance.
 * @param scheduled The scheduled node that should be executed next.
 * @param victim If the heir is this node's thread, no processor is
allocated.
 * @param[in, out] victim_cpu The cpu to allocate.
 */

 static inline void _Scheduler_SMP_Allocate_processor_lazy(
  Scheduler_Context *context,
  Scheduler_Node    *scheduled,
  Scheduler_Node    *victim,
  Per_CPU_Control   *victim_cpu
)
{
  Thread_Control *scheduled_thread = _Scheduler_Node_get_user( scheduled );
  Thread_Control *victim_thread = _Scheduler_Node_get_user( victim );
  Per_CPU_Control *scheduled_cpu = _Thread_Get_CPU( scheduled_thread );
  Per_CPU_Control *cpu_self = _Per_CPU_Get();
  Thread_Control *heir;

  _Assert( _ISR_Get_level() != 0 );

  if ( _Thread_Is_executing_on_a_processor( scheduled_thread ) ) {
    if ( _Scheduler_SMP_Is_processor_owned_by_us( context, scheduled_cpu )
) {
      heir = scheduled_cpu->heir;
      _Thread_Dispatch_update_heir(
        cpu_self,
        scheduled_cpu,
        scheduled_thread
      );
    } else {
      /* We have to force a migration to our processor set */
      heir = scheduled_thread;
    }
  } else {
    heir = scheduled_thread;
  }

  if ( heir != victim_thread ) {
    _Thread_Set_CPU( heir, victim_cpu );
    _Thread_Dispatch_update_heir( cpu_self, victim_cpu, heir );
  }
}


--------------------------------------------------------------------------------
Can someone please help me understand what this function is trying to do
and how exactly it is achieving that?  I am finding it hard to understand
the meaning and use of the terms like cpu_self (why do we need cpu_self ?
Is it the CPU for scheduled or for
victim?),  _Thread_Is_executing_on_a_processor( scheduled_thread ): Why
would we have the thread already executing when we need to allocate a
processor to it? Why are we allocating a processor then?

Thanks,
Richi.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20200620/99cbda39/attachment.html>


More information about the devel mailing list