<div dir="ltr">Hi,<div><br></div><div>I've been learning how SMP scheduler word in RTEMS and I came across the following function definition:</div><div>--------------------------------------------------------------------------------------------</div><div><br>/**<br> * @brief Allocates the cpu for the scheduled thread.<br> *<br> * Attempts to prevent migrations but does not take into account affinity.<br> *<br> * @param context The scheduler context instance.<br> * @param scheduled The scheduled node that should be executed next.<br> * @param victim If the heir is this node's thread, no processor is allocated.<br> * @param[in, out] victim_cpu The cpu to allocate.<br> */<br></div><div><br></div><div> static inline void _Scheduler_SMP_Allocate_processor_lazy(<br>  Scheduler_Context *context,<br>  Scheduler_Node    *scheduled,<br>  Scheduler_Node    *victim,<br>  Per_CPU_Control   *victim_cpu<br>)<br>{<br>  Thread_Control *scheduled_thread = _Scheduler_Node_get_user( scheduled );<br>  Thread_Control *victim_thread = _Scheduler_Node_get_user( victim );<br>  Per_CPU_Control *scheduled_cpu = _Thread_Get_CPU( scheduled_thread );<br>  Per_CPU_Control *cpu_self = _Per_CPU_Get();<br>  Thread_Control *heir;<br><br>  _Assert( _ISR_Get_level() != 0 );<br><br>  if ( _Thread_Is_executing_on_a_processor( scheduled_thread ) ) {<br>    if ( _Scheduler_SMP_Is_processor_owned_by_us( context, scheduled_cpu ) ) {<br>      heir = scheduled_cpu->heir;<br>      _Thread_Dispatch_update_heir(<br>        cpu_self,<br>        scheduled_cpu,<br>        scheduled_thread<br>      );<br>    } else {<br>      /* We have to force a migration to our processor set */<br>      heir = scheduled_thread;<br>    }<br>  } else { <br>    heir = scheduled_thread;<br>  }<br><br>  if ( heir != victim_thread ) {<br>    _Thread_Set_CPU( heir, victim_cpu );<br>    _Thread_Dispatch_update_heir( cpu_self, victim_cpu, heir );<br>  }<br>}<br></div><div><br></div><div><br></div><div>--------------------------------------------------------------------------------</div><div>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?</div><div><br></div><div>Thanks,</div><div>Richi.</div><br></div>