<div dir="ltr">Please review this version of my patch. This still gives a data-exception error because of the earlier discussed issue while trying to scheduled Init task.<div><br></div><div>This patch resolves issues from <a href="https://github.com/richidubey/rtems/pull/7/files">https://github.com/richidubey/rtems/pull/7/files</a> and patch v3.</div><div><br></div><div>Thanks,</div><div>Richi.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Aug 27, 2020 at 2:32 AM Richi Dubey <<a href="mailto:richidubey@gmail.com">richidubey@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">---<br>
 cpukit/include/rtems/scheduler.h       |  6 +-<br>
 .../include/rtems/score/schedulerstrongapa.h | 152 ++-<br>
 cpukit/score/src/schedulerstrongapa.c     | 886 ++++++++++++++----<br>
 3 files changed, 813 insertions(+), 231 deletions(-)<br>
<br>
diff --git a/cpukit/include/rtems/scheduler.h b/cpukit/include/rtems/scheduler.h<br>
index 955a83cfb4..6a05c2798a 100644<br>
--- a/cpukit/include/rtems/scheduler.h<br>
+++ b/cpukit/include/rtems/scheduler.h<br>
@@ -257,16 +257,14 @@<br>
  #define RTEMS_SCHEDULER_STRONG_APA( name, prio_count ) \<br>
   static struct { \<br>
    Scheduler_strong_APA_Context Base; \<br>
-   Chain_Control        Ready[ ( prio_count ) ]; \<br>
+Â Â Â Scheduler_strong_APA_CPU CPU[ CONFIGURE_MAXIMUM_PROCESSORS ]; \<br>
   } SCHEDULER_STRONG_APA_CONTEXT_NAME( name )<br>
<br>
  #define RTEMS_SCHEDULER_TABLE_STRONG_APA( name, obj_name ) \<br>
   { \<br>
    &SCHEDULER_STRONG_APA_CONTEXT_NAME( name ).Base.Base.Base, \<br>
    SCHEDULER_STRONG_APA_ENTRY_POINTS, \<br>
-Â Â Â RTEMS_ARRAY_SIZE( \<br>
-Â Â Â Â SCHEDULER_STRONG_APA_CONTEXT_NAME( name ).Ready \<br>
-Â Â Â ) - 1, \<br>
+Â Â Â SCHEDULER_STRONG_APA_MAXIMUM_PRIORITY, \<br>
    ( obj_name ) \<br>
    SCHEDULER_CONTROL_IS_NON_PREEMPT_MODE_SUPPORTED( false ) \<br>
   }<br>
diff --git a/cpukit/include/rtems/score/schedulerstrongapa.h b/cpukit/include/rtems/score/schedulerstrongapa.h<br>
index 0ac28cb439..cfd79e932a 100644<br>
--- a/cpukit/include/rtems/score/schedulerstrongapa.h<br>
+++ b/cpukit/include/rtems/score/schedulerstrongapa.h<br>
@@ -6,31 +6,47 @@<br>
 * @brief Strong APA Scheduler API<br>
 */<br>
<br>
-/*<br>
- * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved.<br>
+/* SPDX-License-Identifier: BSD-2-Clause<br>
 *<br>
- *Â embedded brains GmbH<br>
- *Â Dornierstr. 4<br>
- *Â 82178 Puchheim<br>
- *Â Germany<br>
- *Â <<a href="mailto:rtems@embedded-brains.de" target="_blank">rtems@embedded-brains.de</a>><br>
+ * Copyright (C) 2020 Richi Dubey<br>
+ * Copyright (c) 2013, 2018 embedded brains GmbH<br>
 *<br>
- * The license and distribution terms for this file may be<br>
- * found in the file LICENSE in this distribution or at<br>
- * <a href="http://www.rtems.org/license/LICENSE" rel="noreferrer" target="_blank">http://www.rtems.org/license/LICENSE</a>.<br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions<br>
+ * are met:<br>
+ * 1. Redistributions of source code must retain the above copyright<br>
+ *Â Â notice, this list of conditions and the following disclaimer.<br>
+ * 2. Redistributions in binary form must reproduce the above copyright<br>
+ *Â Â notice, this list of conditions and the following disclaimer in the<br>
+ *Â Â documentation and/or other materials provided with the distribution.<br>
+ *<br>
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE<br>
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
+ * POSSIBILITY OF SUCH DAMAGE.<br>
 */<br>
<br>
 #ifndef _RTEMS_SCORE_SCHEDULERSTRONGAPA_H<br>
 #define _RTEMS_SCORE_SCHEDULERSTRONGAPA_H<br>
<br>
 #include <rtems/score/scheduler.h><br>
-#include <rtems/score/schedulerpriority.h><br>
 #include <rtems/score/schedulersmp.h><br>
+#include <rtems/score/percpu.h><br>
<br>
 #ifdef __cplusplus<br>
 extern "C" {<br>
 #endif /* __cplusplus */<br>
<br>
+#define STRONG_SCHEDULER_NODE_OF_CHAIN( node ) \<br>
+Â RTEMS_CONTAINER_OF( node, Scheduler_strong_APA_Node, Ready_node )<br>
+<br>
 /**<br>
 * @defgroup RTEMSScoreSchedulerStrongAPA Strong APA Scheduler<br>
 *<br>
@@ -38,42 +54,96 @@ extern "C" {<br>
 *<br>
 * @brief Strong APA Scheduler<br>
 *<br>
- * This is an implementation of the global fixed priority scheduler (G-FP). It<br>
- * uses one ready chain per priority to ensure constant time insert operations.<br>
- * The scheduled chain uses linear insert operations and has at most processor<br>
- * count entries. Since the processor and priority count are constants all<br>
- * scheduler operations complete in a bounded execution time.<br>
- *<br>
- * The the_thread preempt mode will be ignored.<br>
+ * This is an implementation of the Strong APA scheduler defined by<br>
+ * Cerqueira et al. in Linux's Processor Affinity API, Refined:<br>
+ * Shifting Real-Time Tasks Towards Higher Schedulability.<br>
 *<br>
+ * The scheduled and ready nodes are accessed via the<br>
+ * Scheduler_strong_APA_Context::Ready which helps in backtracking when a<br>
+ * node which is executing on a CPU gets blocked. New node is allocated to<br>
+ * the cpu by checking all the executing nodes in the affinity set of the<br>
+ * node and the subsequent nodes executing on the processors in its<br>
+ * affinity set.<br>
 * @{<br>
 */<br>
<br>
 /**<br>
- * @brief Scheduler context specialization for Strong APA<br>
- * schedulers.<br>
+ * @brief Scheduler node specialization for Strong APA schedulers.<br>
 */<br>
 typedef struct {<br>
- Scheduler_SMP_Context  Base;<br>
-Â Priority_bit_map_Control Bit_map;<br>
- Chain_Control      Ready[ RTEMS_ZERO_LENGTH_ARRAY ];<br>
-} Scheduler_strong_APA_Context;<br>
+Â /**<br>
+Â Â * @brief SMP scheduler node.<br>
+Â Â */<br>
+Â Scheduler_SMP_Node Base;<br>
+<br>
+ /**<br>
+Â Â * @brief Chain node for Scheduler_strong_APA_Context::Ready.<br>
+Â Â */<br>
+Â Chain_Node Ready_node;<br>
+<br>
+Â /**<br>
+Â Â * @brief CPU that this node would preempt in the backtracking part of<br>
+Â Â * _Scheduler_strong_APA_Get_highest_ready and<br>
+Â Â * _Scheduler_strong_APA_Do_Enqueue.<br>
+Â Â */<br>
+Â Per_CPU_Control *cpu_to_preempt;<br>
+<br>
+Â /**<br>
+Â Â * @brief The associated affinity set of this node.<br>
+Â Â */<br>
+Â Processor_mask Affinity;<br>
+} Scheduler_strong_APA_Node;<br>
+<br>
<br>
 /**<br>
- * @brief Scheduler node specialization for Strong APA<br>
- * schedulers.<br>
+ * @brief CPU related variables and a CPU_Control to implement BFS.<br>
+ */<br>
+typedef struct<br>
+{<br>
+Â Â /**<br>
+Â Â * @brief CPU in a queue.<br>
+Â Â */<br>
+Â Per_CPU_Control *cpu;<br>
+<br>
+Â /**<br>
+Â Â * @brief The node that would preempt this CPU.<br>
+Â Â */<br>
+Â Scheduler_Node *preempting_node;<br>
+<br>
+Â Â /**<br>
+Â Â * @brief Whether or not this cpu has been added to the queue<br>
+Â Â * (visited in BFS).<br>
+Â Â */<br>
+Â bool visited;<br>
+<br>
+Â /**<br>
+Â Â * @brief The node currently executing on this cpu.<br>
+Â Â */<br>
+Â Â Scheduler_Node *executing;<br>
+} Scheduler_strong_APA_CPU;<br>
+<br>
+ /**<br>
+ * @brief Scheduler context and node definition for Strong APA scheduler.<br>
 */<br>
 typedef struct {<br>
+ /**<br>
+Â Â * @brief @see Scheduler_SMP_Context.<br>
+Â Â */<br>
+Â Scheduler_SMP_Context Base;<br>
+<br>
  /**<br>
-Â Â * @brief SMP scheduler node.<br>
+Â Â * @brief Chain of all the ready and scheduled nodes present in<br>
+Â Â * the Strong APA scheduler.<br>
  */<br>
-Â Scheduler_SMP_Node Base;<br>
+Â Chain_Control Ready;<br>
<br>
  /**<br>
-Â Â * @brief The associated ready queue of this node.<br>
+Â Â * @brief Struct with important variables for each cpu.<br>
  */<br>
-Â Scheduler_priority_Ready_queue Ready_queue;<br>
-} Scheduler_strong_APA_Node;<br>
+Â Scheduler_strong_APA_CPU CPU[ RTEMS_ZERO_LENGTH_ARRAY ];<br>
+} Scheduler_strong_APA_Context;<br>
+<br>
+#define SCHEDULER_STRONG_APA_MAXIMUM_PRIORITY 255<br>
<br>
 /**<br>
 * @brief Entry points for the Strong APA Scheduler.<br>
@@ -100,8 +170,8 @@ typedef struct {<br>
   _Scheduler_default_Release_job, \<br>
   _Scheduler_default_Cancel_job, \<br>
   _Scheduler_default_Tick, \<br>
-Â Â _Scheduler_SMP_Start_idle \<br>
-Â Â SCHEDULER_OPERATION_DEFAULT_GET_SET_AFFINITY \<br>
+Â Â _Scheduler_SMP_Start_idle, \<br>
+Â Â _Scheduler_strong_APA_Set_affinity \<br>
  }<br>
<br>
 /**<br>
@@ -168,7 +238,7 @@ void _Scheduler_strong_APA_Update_priority(<br>
 /**<br>
 * @brief Asks for help.<br>
 *<br>
- * @param scheduler The scheduler control instance.<br>
+ * @param scheduler The scheduler control instance.<br>
 * @param the_thread The thread that asks for help.<br>
 * @param node The node of @a the_thread.<br>
 *<br>
@@ -246,6 +316,20 @@ void _Scheduler_strong_APA_Yield(<br>
  Scheduler_Node     *node<br>
 );<br>
<br>
+/**<br>
+ * @brief Sets the affinity .<br>
+ *<br>
+ * @param scheduler The scheduler control instance.<br>
+ * @param the_thread The thread to yield.<br>
+ * @param[in, out] node The node of @a the_thread.<br>
+ */<br>
+bool _Scheduler_strong_APA_Set_affinity(<br>
+Â const Scheduler_Control *scheduler,<br>
+ Thread_Control     *thread,<br>
+ Scheduler_Node     *node_base,<br>
+ const Processor_mask  *affinity<br>
+);<br>
+<br>
 /** @} */<br>
<br>
 #ifdef __cplusplus<br>
diff --git a/cpukit/score/src/schedulerstrongapa.c b/cpukit/score/src/schedulerstrongapa.c<br>
index 924cd86412..2c623f3cb3 100644<br>
--- a/cpukit/score/src/schedulerstrongapa.c<br>
+++ b/cpukit/score/src/schedulerstrongapa.c<br>
@@ -6,18 +6,31 @@<br>
 * @brief Strong APA Scheduler Implementation<br>
 */<br>
<br>
-/*<br>
- * Copyright (c) 2013, 2016 embedded brains GmbH. All rights reserved.<br>
+/* SPDX-License-Identifier: BSD-2-Clause<br>
+ *<br>
+ * Copyright (C) 2020 Richi Dubey<br>
+ * Copyright (c) 2013, 2018 embedded brains GmbH<br>
 *<br>
- *Â embedded brains GmbH<br>
- *Â Dornierstr. 4<br>
- *Â 82178 Puchheim<br>
- *Â Germany<br>
- *Â <<a href="mailto:rtems@embedded-brains.de" target="_blank">rtems@embedded-brains.de</a>><br>
+ * Redistribution and use in source and binary forms, with or without<br>
+ * modification, are permitted provided that the following conditions<br>
+ * are met:<br>
+ * 1. Redistributions of source code must retain the above copyright<br>
+ *Â Â notice, this list of conditions and the following disclaimer.<br>
+ * 2. Redistributions in binary form must reproduce the above copyright<br>
+ *Â Â notice, this list of conditions and the following disclaimer in the<br>
+ *Â Â documentation and/or other materials provided with the distribution.<br>
 *<br>
- * The license and distribution terms for this file may be<br>
- * found in the file LICENSE in this distribution or at<br>
- * <a href="http://www.rtems.org/license/LICENSE" rel="noreferrer" target="_blank">http://www.rtems.org/license/LICENSE</a>.<br>
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE<br>
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
+ * POSSIBILITY OF SUCH DAMAGE.<br>
 */<br>
<br>
 #ifdef HAVE_CONFIG_H<br>
@@ -25,301 +38,739 @@<br>
 #endif<br>
<br>
 #include <rtems/score/schedulerstrongapa.h><br>
-#include <rtems/score/schedulerpriorityimpl.h><br>
 #include <rtems/score/schedulersmpimpl.h><br>
+#include <rtems/score/assert.h><br>
<br>
-static Scheduler_strong_APA_Context *_Scheduler_strong_APA_Get_self(<br>
-Â Scheduler_Context *context<br>
-)<br>
+static inline Scheduler_strong_APA_Context *<br>
+_Scheduler_strong_APA_Get_context( const Scheduler_Control *scheduler )<br>
+{<br>
+Â return (Scheduler_strong_APA_Context *) _Scheduler_Get_context( scheduler );<br>
+}<br>
+<br>
+static inline Scheduler_strong_APA_Context *<br>
+_Scheduler_strong_APA_Get_self( Scheduler_Context *context )<br>
 {<br>
  return (Scheduler_strong_APA_Context *) context;<br>
 }<br>
<br>
-static Scheduler_strong_APA_Node *<br>
+static inline Scheduler_strong_APA_Node *<br>
 _Scheduler_strong_APA_Node_downcast( Scheduler_Node *node )<br>
 {<br>
  return (Scheduler_strong_APA_Node *) node;<br>
 }<br>
<br>
-static void _Scheduler_strong_APA_Move_from_scheduled_to_ready(<br>
+static inline void _Scheduler_strong_APA_Do_update(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *scheduled_to_ready<br>
+ Scheduler_Node  *node,<br>
+ Priority_Control  new_priority<br>
 )<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_self( context );<br>
-Â Scheduler_strong_APA_Node *node =<br>
-Â Â _Scheduler_strong_APA_Node_downcast( scheduled_to_ready );<br>
-<br>
-Â _Chain_Extract_unprotected( &node->Base.Base.Node.Chain );<br>
-Â _Scheduler_priority_Ready_queue_enqueue_first(<br>
-Â Â &node->Base.Base.Node.Chain,<br>
-Â Â &node->Ready_queue,<br>
-Â Â &self->Bit_map<br>
-Â );<br>
+Â Scheduler_SMP_Node *smp_node;<br>
+Â (void) context;<br>
+<br>
+Â smp_node = _Scheduler_SMP_Node_downcast( node );<br>
+Â _Scheduler_SMP_Node_update_priority( smp_node, new_priority );<br>
 }<br>
<br>
-static void _Scheduler_strong_APA_Move_from_ready_to_scheduled(<br>
+/*<br>
+ * Returns true if the Strong APA scheduler has ready nodes<br>
+ * available for scheduling.<br>
+ */<br>
+static inline bool<br>
+Â Â _Scheduler_strong_APA_Has_ready( Scheduler_Context *context )<br>
+{<br>
+Â Scheduler_strong_APA_Context *self;<br>
+ const Chain_Node       *tail;<br>
+ Chain_Node          *next;<br>
+ Scheduler_strong_APA_Node  *node;<br>
+<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â tail = _Chain_Immutable_tail( &self->Ready );<br>
+Â next = _Chain_First( &self->Ready );<br>
+<br>
+Â while ( next != tail ) {<br>
+Â Â node = (Scheduler_strong_APA_Node *) STRONG_SCHEDULER_NODE_OF_CHAIN( next );<br>
+<br>
+Â Â if (<br>
+Â Â Â _Scheduler_SMP_Node_state( &node->Base.Base ) ==<br>
+Â Â Â SCHEDULER_SMP_NODE_READY<br>
+Â Â ) {<br>
+Â Â Â return true;<br>
+Â Â }<br>
+<br>
+Â Â next = _Chain_Next( next );<br>
+Â }<br>
+<br>
+Â return false;<br>
+}<br>
+<br>
+static inline void _Scheduler_strong_APA_Allocate_processor(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *ready_to_scheduled<br>
+ Scheduler_Node  *scheduled_base,<br>
+ Scheduler_Node  *victim_base,<br>
+ Per_CPU_Control  *victim_cpu<br>
 )<br>
 {<br>
+ Scheduler_strong_APA_Node  *scheduled;<br>
  Scheduler_strong_APA_Context *self;<br>
- Scheduler_strong_APA_Node  *node;<br>
- Priority_Control       insert_priority;<br>
<br>
+Â (void) victim_base;<br>
+<br>
+Â scheduled = _Scheduler_strong_APA_Node_downcast( scheduled_base );<br>
  self = _Scheduler_strong_APA_Get_self( context );<br>
-Â node = _Scheduler_strong_APA_Node_downcast( ready_to_scheduled );<br>
<br>
-Â _Scheduler_priority_Ready_queue_extract(<br>
-Â Â &node->Base.Base.Node.Chain,<br>
-Â Â &node->Ready_queue,<br>
-Â Â &self->Bit_map<br>
+Â self->CPU[ _Per_CPU_Get_index( victim_cpu ) ].executing = scheduled_base;<br>
+<br>
+Â _Scheduler_SMP_Allocate_processor_exact(<br>
+Â Â context,<br>
+Â Â &( scheduled->Base.Base ),<br>
+Â Â NULL,<br>
+Â Â victim_cpu<br>
  );<br>
-Â insert_priority = _Scheduler_SMP_Node_priority( &node->Base.Base );<br>
+}<br>
+<br>
+/*<br>
+ * Finds and returns the highest ready node present by accessing the<br>
+ * _Strong_APA_Context->CPU with front and rear values.<br>
+ */<br>
+static inline Scheduler_Node * _Scheduler_strong_APA_Find_highest_ready(<br>
+Â Scheduler_strong_APA_Context *self,<br>
+ uint32_t           front,<br>
+ uint32_t           rear<br>
+)<br>
+{<br>
+ Scheduler_Node       *highest_ready;<br>
+Â Scheduler_strong_APA_CPUÂ Â *CPU;<br>
+ const Chain_Node      *tail;<br>
+ Chain_Node         *next;<br>
+ uint32_t           index_assigned_cpu;<br>
+ uint32_t           index_curr_cpu;<br>
+ Scheduler_strong_APA_Node  *node;<br>
+ Priority_Control       min_priority_num;<br>
+ Priority_Control       curr_priority;<br>
+ Per_CPU_Control       *assigned_cpu;<br>
+ Scheduler_SMP_Node_state   curr_state;<br>
+ Per_CPU_Control       *curr_CPU;<br>
+ bool             first_task;<br>
+<br>
+Â CPU = self->CPU;<br>
+Â Â /*<br>
+Â Â * When the first task accessed has nothing to compare its priority against<br>
+Â Â * So, it is the task with the highest priority witnessed so far.<br>
+Â Â */<br>
+Â first_task = true;<br>
+<br>
+Â _Assert( rear < CONFIGURE_MAXIMUM_PROCESSOR );<br>
+<br>
+Â while( front <= rear ) {<br>
+Â Â curr_CPU = CPU[ front ].cpu;<br>
+Â Â front = front + 1;<br>
+<br>
+Â Â tail = _Chain_Immutable_tail( &self->Ready );<br>
+Â Â next = _Chain_First( &self->Ready );<br>
+<br>
+Â Â while ( next != tail ) {<br>
+Â Â Â node = (Scheduler_strong_APA_Node*) STRONG_SCHEDULER_NODE_OF_CHAIN( next );<br>
+Â Â Â //Check if the curr_CPU is in the affinity set of the node.<br>
+Â Â Â index_curr_cpu = _Per_CPU_Get_index( curr_CPU );<br>
+Â Â Â if (<br>
+Â Â Â Â _Processor_mask_Is_set( &node->Affinity, index_curr_cpu )<br>
+Â Â Â ) {<br>
+Â Â Â Â curr_state = _Scheduler_SMP_Node_state( &node->Base.Base );<br>
+<br>
+Â Â Â Â if ( curr_state == SCHEDULER_SMP_NODE_SCHEDULED ) {<br>
+Â Â Â Â Â assigned_cpu = _Thread_Get_CPU( node->Base.Base.user );<br>
+Â Â Â Â Â index_assigned_cpu =Â _Per_CPU_Get_index( assigned_cpu );<br>
+<br>
+Â Â Â Â Â if ( CPU[ index_assigned_cpu ].visited == false ) {<br>
+Â Â Â Â Â Â rear = rear + 1;<br>
+Â Â Â Â Â Â CPU[ rear ].cpu = assigned_cpu;<br>
+Â Â Â Â Â Â CPU[ index_assigned_cpu ].visited = true;<br>
+Â Â Â Â Â Â /*<br>
+Â Â Â Â Â Â Â * The curr CPU of the queue invoked this node to add its CPU<br>
+Â Â Â Â Â Â Â * that it is executing on to the queue. So this node might get<br>
+Â Â Â Â Â Â Â * preempted because of the invoker curr_CPU and this curr_CPU<br>
+Â Â Â Â Â Â Â * is the CPU that node should preempt in case this node<br>
+Â Â Â Â Â Â Â * gets preempted.<br>
+Â Â Â Â Â Â Â */<br>
+Â Â Â Â Â Â node->cpu_to_preempt = curr_CPU;<br>
+Â Â Â Â Â }<br>
+Â Â Â Â }<br>
+Â Â Â Â else if ( curr_state == SCHEDULER_SMP_NODE_READY ) {<br>
+Â Â Â Â Â curr_priority = _Scheduler_Node_get_priority( &node->Base.Base );<br>
+Â Â Â Â Â curr_priority = SCHEDULER_PRIORITY_PURIFY( curr_priority );<br>
+<br>
+Â Â Â Â Â if ( first_task == true || curr_priority < min_priority_num ) {<br>
+Â Â Â Â Â Â min_priority_num = curr_priority;<br>
+Â Â Â Â Â Â highest_ready = &node->Base.Base;<br>
+Â Â Â Â Â Â first_task = false;<br>
+Â Â Â Â Â Â /*<br>
+Â Â Â Â Â Â * In case curr_CPU is filter_CPU, we need to store the<br>
+Â Â Â Â Â Â * cpu_to_preempt value so that we go back to SMP_*<br>
+Â Â Â Â Â Â * function, rather than preempting the node ourselves.<br>
+Â Â Â Â Â Â */<br>
+Â Â Â Â Â Â node->cpu_to_preempt = curr_CPU;<br>
+Â Â Â Â Â }<br>
+Â Â Â Â }<br>
+Â Â Â }<br>
+Â Â next = _Chain_Next( next );<br>
+Â Â }<br>
+Â }<br>
+<br>
+Â return highest_ready;<br>
+}<br>
+<br>
+static inline void _Scheduler_strong_APA_Move_from_ready_to_scheduled(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *ready_to_scheduled<br>
+)<br>
+{<br>
+Â Priority_Control insert_priority;<br>
+<br>
+Â insert_priority = _Scheduler_SMP_Node_priority( ready_to_scheduled );<br>
  insert_priority = SCHEDULER_PRIORITY_APPEND( insert_priority );<br>
-Â _Chain_Insert_ordered_unprotected(<br>
-Â Â &self->Base.Scheduled,<br>
-Â Â &node->Base.Base.Node.Chain,<br>
-Â Â &insert_priority,<br>
-Â Â _Scheduler_SMP_Priority_less_equal<br>
+Â _Scheduler_SMP_Insert_scheduled(<br>
+Â Â context,<br>
+Â Â ready_to_scheduled,<br>
+Â Â insert_priority<br>
  );<br>
 }<br>
<br>
-static void _Scheduler_strong_APA_Insert_ready(<br>
+/*<br>
+ * Implement the BFS Algorithm for task departure to get the highest ready task<br>
+ * for a particular CPU, returns the highest ready Scheduler_Node<br>
+ * Scheduler_Node filter here pointst to the victim node that is blocked<br>
+ * resulting which this function is called.<br>
+ */<br>
+static inline Scheduler_Node *_Scheduler_strong_APA_Get_highest_ready(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *node_base,<br>
- Priority_Control  insert_priority<br>
+ Scheduler_Node  *filter<br>
 )<br>
 {<br>
  Scheduler_strong_APA_Context *self;<br>
+ Per_CPU_Control       *filter_cpu;<br>
  Scheduler_strong_APA_Node  *node;<br>
+ Scheduler_Node        *highest_ready;<br>
+ Scheduler_Node        *curr_node;<br>
+ Scheduler_Node        *next_node;<br>
+Â Scheduler_strong_APA_CPUÂ Â Â *CPU;<br>
+ uint32_t           front;<br>
+ uint32_t           rear;<br>
+ uint32_t           cpu_max;<br>
+ uint32_t               cpu_index;<br>
<br>
  self = _Scheduler_strong_APA_Get_self( context );<br>
-Â node = _Scheduler_strong_APA_Node_downcast( node_base );<br>
+Â //Denotes front and rear of the queue.<br>
+Â front = 0;<br>
+Â rear = -1;<br>
<br>
-Â if ( SCHEDULER_PRIORITY_IS_APPEND( insert_priority ) ) {<br>
-Â Â _Scheduler_priority_Ready_queue_enqueue(<br>
-Â Â Â &node->Base.Base.Node.Chain,<br>
-Â Â Â &node->Ready_queue,<br>
-Â Â Â &self->Bit_map<br>
-Â Â );<br>
-Â } else {<br>
-Â Â _Scheduler_priority_Ready_queue_enqueue_first(<br>
-Â Â Â &node->Base.Base.Node.Chain,<br>
-Â Â Â &node->Ready_queue,<br>
-Â Â Â &self->Bit_map<br>
-Â Â );<br>
+Â filter_cpu = _Thread_Get_CPU( filter->user );<br>
+Â CPU = self->CPU;<br>
+Â cpu_max = _SMP_Get_processor_maximum();<br>
+<br>
+Â for ( cpu_index = 0 ; cpu_index < cpu_max ; ++cpu_index ) {<br>
+Â Â CPU[ cpu_index ].visited = false;<br>
  }<br>
+<br>
+Â rear = rear + 1;<br>
+Â CPU[ rear ].cpu = filter_cpu;<br>
+Â CPU[ _Per_CPU_Get_index( filter_cpu ) ].visited = true;<br>
+<br>
+Â highest_ready = _Scheduler_strong_APA_Find_highest_ready(<br>
+Â Â Â Â Â Â Â Â Â Â self,<br>
+Â Â Â Â Â Â Â Â Â Â front,<br>
+Â Â Â Â Â Â Â Â Â Â rear<br>
+Â Â Â Â Â Â Â Â Â );<br>
+<br>
+Â if ( highest_ready != filter ) {<br>
+Â Â /*<br>
+Â Â Â * Backtrack on the path from<br>
+Â Â Â * filter_cpu to highest_ready, shifting along every task.<br>
+Â Â Â */<br>
+<br>
+Â Â node = _Scheduler_strong_APA_Node_downcast( highest_ready );<br>
+Â Â /*<br>
+Â Â Â * Highest ready is not just directly reachable from the victim cpu<br>
+Â Â Â * So there is need for task shifting .<br>
+Â Â Â */<br>
+Â Â while ( node->cpu_to_preempt !=Â filter_cpu ) {<br>
+Â Â Â curr_node = &node->Base.Base;<br>
+Â Â Â next_node = CPU[ _Per_CPU_Get_index( node->cpu_to_preempt ) ].executing;<br>
+<br>
+Â Â Â _Scheduler_SMP_Preempt(<br>
+Â Â Â Â context,<br>
+Â Â Â Â curr_node,<br>
+Â Â Â Â next_node,<br>
+Â Â Â Â _Scheduler_strong_APA_Allocate_processor<br>
+Â Â Â );<br>
+<br>
+Â Â Â if( curr_node == highest_ready ) {<br>
+Â Â Â Â _Scheduler_strong_APA_Move_from_ready_to_scheduled( context, curr_node );<br>
+Â Â Â }<br>
+<br>
+Â Â Â node = _Scheduler_strong_APA_Node_downcast( next_node );<br>
+Â Â }<br>
+Â Â /*<br>
+Â Â Â * To save the last node so that the caller SMP_* function<br>
+Â Â Â * can do the allocation<br>
+Â Â Â */<br>
+Â Â Â curr_node = &node->Base.Base;<br>
+Â Â Â highest_ready = curr_node;<br>
+Â Â }<br>
+<br>
+Â return highest_ready;<br>
 }<br>
<br>
-static void _Scheduler_strong_APA_Extract_from_ready(<br>
+/*<br>
+ * Checks the lowest scheduled directly reachable task<br>
+ */<br>
+static inline Scheduler_Node *_Scheduler_strong_APA_Get_lowest_scheduled(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *the_thread<br>
+ Scheduler_Node  *filter_base<br>
 )<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_self( context );<br>
-Â Scheduler_strong_APA_Node *node =<br>
-Â Â _Scheduler_strong_APA_Node_downcast( the_thread );<br>
-<br>
-Â _Scheduler_priority_Ready_queue_extract(<br>
-Â Â &node->Base.Base.Node.Chain,<br>
-Â Â &node->Ready_queue,<br>
-Â Â &self->Bit_map<br>
-Â );<br>
+ uint32_t           cpu_max;<br>
+ uint32_t           cpu_index;<br>
+ Scheduler_Node        *curr_node;<br>
+ Scheduler_Node        *lowest_scheduled;<br>
+ Priority_Control       max_priority_num;<br>
+ Priority_Control       curr_priority;<br>
+ Scheduler_strong_APA_Node  *filter_strong_node;<br>
+Â Scheduler_strong_APA_Context *self;<br>
+<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â lowest_scheduled = NULL; // To remove compiler warning.<br>
+Â max_priority_num = 0;Â Â // Max (Lowest) priority encountered so far.<br>
+Â filter_strong_node = _Scheduler_strong_APA_Node_downcast( filter_base );<br>
+<br>
+Â //lowest_scheduled is NULL if affinty of a node is 0<br>
+Â _Assert( !_Processor_mask_Zero( &filter_strong_node->Affinity ) );<br>
+Â cpu_max = _SMP_Get_processor_maximum();<br>
+<br>
+Â for ( cpu_index = 0 ; cpu_index < cpu_max ; ++cpu_index ) {<br>
+Â Â //Checks if the CPU is in the affinity set of filter_strong_node<br>
+Â Â if ( _Processor_mask_Is_set( &filter_strong_node->Affinity, cpu_index ) ) {<br>
+Â Â Â Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );<br>
+<br>
+Â Â Â if ( _Per_CPU_Is_processor_online( cpu ) ) {<br>
+Â Â Â Â curr_node = self->CPU[ _Per_CPU_Get_index( cpu ) ].executing;<br>
+Â Â Â Â curr_priority = _Scheduler_Node_get_priority( curr_node );<br>
+Â Â Â Â curr_priority = SCHEDULER_PRIORITY_PURIFY( curr_priority );<br>
+<br>
+Â Â Â Â if ( curr_priority > max_priority_num ) {<br>
+Â Â Â Â Â lowest_scheduled = curr_node;<br>
+Â Â Â Â Â max_priority_num = curr_priority;<br>
+Â Â Â Â }<br>
+Â Â Â }<br>
+Â Â }<br>
+Â }<br>
+<br>
+Â _Assert( lowest_scheduled != NULL );<br>
+Â return lowest_scheduled;<br>
 }<br>
<br>
-static void _Scheduler_strong_APA_Do_update(<br>
+static inline void _Scheduler_strong_APA_Extract_from_scheduled(<br>
  Scheduler_Context *context,<br>
-Â Scheduler_Node *node_to_update,<br>
-Â Priority_Control new_priority<br>
+ Scheduler_Node  *node_to_extract<br>
 )<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_self( context );<br>
-Â Scheduler_strong_APA_Node *node =<br>
-Â Â _Scheduler_strong_APA_Node_downcast( node_to_update );<br>
-<br>
-Â _Scheduler_SMP_Node_update_priority( &node->Base, new_priority );<br>
-Â _Scheduler_priority_Ready_queue_update(<br>
-Â Â &node->Ready_queue,<br>
-Â Â SCHEDULER_PRIORITY_UNMAP( new_priority ),<br>
-Â Â &self->Bit_map,<br>
-Â Â &self->Ready[ 0 ]<br>
-Â );<br>
+Â Scheduler_strong_APA_Context *self;<br>
+ Scheduler_strong_APA_Node  *node;<br>
+<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â node = _Scheduler_strong_APA_Node_downcast( node_to_extract );<br>
+<br>
+Â _Scheduler_SMP_Extract_from_scheduled( &self->Base.Base, &node->Base.Base );<br>
+Â //Not removing it from Ready since the node could go in the READY state.<br>
 }<br>
<br>
-static Scheduler_strong_APA_Context *<br>
-_Scheduler_strong_APA_Get_context( const Scheduler_Control *scheduler )<br>
+static inline void _Scheduler_strong_APA_Extract_from_ready(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *node_to_extract<br>
+)<br>
 {<br>
-Â return (Scheduler_strong_APA_Context *) _Scheduler_Get_context( scheduler );<br>
+Â Scheduler_strong_APA_Context *self;<br>
+ Scheduler_strong_APA_Node  *node;<br>
+<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â node = _Scheduler_strong_APA_Node_downcast( node_to_extract );<br>
+<br>
+Â _Assert( !_Chain_Is_empty( self->Ready ) );<br>
+Â _Assert( !_Chain_Is_node_off_chain( &node->Ready_node ) );<br>
+<br>
+Â Â _Chain_Extract_unprotected( &node->Ready_node );<br>
+Â Â _Chain_Set_off_chain( &node->Ready_node );<br>
 }<br>
<br>
-void _Scheduler_strong_APA_Initialize( const Scheduler_Control *scheduler )<br>
+static inline void _Scheduler_strong_APA_Insert_ready(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *node_base,<br>
+ Priority_Control  insert_priority<br>
+)<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_context( scheduler );<br>
+Â Scheduler_strong_APA_Context *self;<br>
+ Scheduler_strong_APA_Node  *node;<br>
<br>
-Â _Scheduler_SMP_Initialize( &self->Base );<br>
-Â _Priority_bit_map_Initialize( &self->Bit_map );<br>
-Â _Scheduler_priority_Ready_queue_initialize(<br>
-Â Â &self->Ready[ 0 ],<br>
-Â Â scheduler->maximum_priority<br>
-Â );<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â node = _Scheduler_strong_APA_Node_downcast( node_base );<br>
+<br>
+Â if( _Chain_Is_node_off_chain( &node->Ready_node ) ) {<br>
+Â Â _Chain_Append_unprotected( &self->Ready, &node->Ready_node );<br>
+Â }<br>
 }<br>
<br>
-void _Scheduler_strong_APA_Node_initialize(<br>
-Â const Scheduler_Control *scheduler,<br>
- Scheduler_Node     *node,<br>
- Thread_Control     *the_thread,<br>
- Priority_Control     priority<br>
+static inline void _Scheduler_strong_APA_Move_from_scheduled_to_ready(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *scheduled_to_ready<br>
 )<br>
 {<br>
- Scheduler_Context      *context;<br>
-Â Scheduler_strong_APA_Context *self;<br>
- Scheduler_strong_APA_Node  *the_node;<br>
+Â Priority_Control insert_priority;<br>
<br>
-Â the_node = _Scheduler_strong_APA_Node_downcast( node );<br>
-Â _Scheduler_SMP_Node_initialize(<br>
-Â Â scheduler,<br>
-Â Â &the_node->Base,<br>
-Â Â the_thread,<br>
-Â Â priority<br>
-Â );<br>
+Â _Scheduler_SMP_Extract_from_scheduled( context, scheduled_to_ready );<br>
+Â insert_priority = _Scheduler_SMP_Node_priority( scheduled_to_ready );<br>
<br>
-Â context = _Scheduler_Get_context( scheduler );<br>
-Â self = _Scheduler_strong_APA_Get_self( context );<br>
-Â _Scheduler_priority_Ready_queue_update(<br>
-Â Â &the_node->Ready_queue,<br>
-Â Â SCHEDULER_PRIORITY_UNMAP( priority ),<br>
-Â Â &self->Bit_map,<br>
-Â Â &self->Ready[ 0 ]<br>
+Â _Scheduler_strong_APA_Insert_ready(<br>
+Â Â context,<br>
+Â Â scheduled_to_ready,<br>
+Â Â insert_priority<br>
  );<br>
 }<br>
<br>
-static bool _Scheduler_strong_APA_Has_ready( Scheduler_Context *context )<br>
+static inline Scheduler_Node* _Scheduler_strong_APA_Get_lowest_reachable(<br>
+Â Scheduler_strong_APA_Context *self,<br>
+ uint32_t           front,<br>
+ uint32_t           rear,<br>
+ Per_CPU_Control       **cpu_to_preempt<br>
+)<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_self( context );<br>
+ Scheduler_Node       *lowest_reachable;<br>
+ Priority_Control       max_priority_num;<br>
+ uint32_t          cpu_max;<br>
+ uint32_t          cpu_index;<br>
+ Thread_Control       *curr_thread;<br>
+ Per_CPU_Control       *curr_CPU;<br>
+ Priority_Control       curr_priority;<br>
+ Scheduler_Node       *curr_node;<br>
+ Scheduler_strong_APA_Node  *curr_strong_node; //Current Strong_APA_Node<br>
+Â Scheduler_strong_APA_CPUÂ Â *CPU;<br>
+<br>
+Â max_priority_num = 0; //Max (Lowest) priority encountered so far.<br>
+Â CPU = self->CPU;<br>
+Â cpu_max = _SMP_Get_processor_maximum();<br>
+<br>
+Â while( front <= rear ) {<br>
+Â Â curr_CPU = CPU[ front ].cpu;<br>
+Â Â front = front + 1;<br>
+<br>
+Â Â curr_node = CPU[ _Per_CPU_Get_index( curr_CPU ) ].executing;<br>
+Â Â curr_thread = curr_node->user;<br>
+<br>
+Â Â curr_priority = _Scheduler_Node_get_priority( curr_node );<br>
+Â Â curr_priority = SCHEDULER_PRIORITY_PURIFY( curr_priority );<br>
+<br>
+Â Â curr_strong_node = _Scheduler_strong_APA_Node_downcast( curr_node );<br>
+<br>
+Â Â if ( curr_priority > max_priority_num ) {<br>
+Â Â Â lowest_reachable = curr_node;<br>
+Â Â Â max_priority_num = curr_priority;<br>
+Â Â Â *cpu_to_preempt = curr_CPU;<br>
+Â Â }<br>
+<br>
+Â Â if ( !curr_thread->is_idle ) {<br>
+Â Â Â for ( cpu_index = 0 ; cpu_index < cpu_max ; ++cpu_index ) {<br>
+Â Â Â Â if ( _Processor_mask_Is_set( &curr_strong_node->Affinity, cpu_index ) ) {<br>
+Â Â Â Â Â //Checks if the thread_CPU is in the affinity set of the node<br>
+Â Â Â Â Â Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );<br>
+Â Â Â Â Â if ( _Per_CPU_Is_processor_online( cpu ) && CPU[ cpu_index ].visited == false ) {<br>
+Â Â Â Â Â Â rear = rear + 1;<br>
+Â Â Â Â Â Â CPU[ rear ].cpu = cpu;<br>
+Â Â Â Â Â Â CPU[ cpu_index ].visited = true;<br>
+Â Â Â Â Â Â CPU[ cpu_index ].preempting_node = curr_node;<br>
+Â Â Â Â Â }<br>
+Â Â Â Â }<br>
+Â Â Â }<br>
+Â Â }<br>
+Â }<br>
<br>
-Â return !_Priority_bit_map_Is_empty( &self->Bit_map );<br>
+Â return lowest_reachable;<br>
 }<br>
<br>
-static Scheduler_Node *_Scheduler_strong_APA_Get_highest_ready(<br>
+static inline bool _Scheduler_strong_APA_Do_enqueue(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *node<br>
+ Scheduler_Node  *lowest_reachable,<br>
+ Scheduler_Node  *node,<br>
+ Priority_Control insert_priority,<br>
+ Per_CPU_Control *cpu_to_preempt<br>
 )<br>
 {<br>
-Â Scheduler_strong_APA_Context *self =<br>
-Â Â _Scheduler_strong_APA_Get_self( context );<br>
+ bool             needs_help;<br>
+ Priority_Control       node_priority;<br>
+ Priority_Control       lowest_priority;<br>
+Â Scheduler_strong_APA_CPUÂ Â Â *CPU;<br>
+ Scheduler_Node        *curr_node;<br>
+ Scheduler_strong_APA_Node  *curr_strong_node; //Current Strong_APA_Node<br>
+ Per_CPU_Control       *curr_CPU;<br>
+Â Scheduler_strong_APA_Context *self;<br>
+ Scheduler_Node        *next_node;<br>
<br>
-Â (void) node;<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â CPU = self->CPU;<br>
+<br>
+Â node_priority = _Scheduler_Node_get_priority( node );<br>
+Â node_priority = SCHEDULER_PRIORITY_PURIFY( node_priority );<br>
+<br>
+Â lowest_priority =Â _Scheduler_Node_get_priority( lowest_reachable );<br>
+Â lowest_priority = SCHEDULER_PRIORITY_PURIFY( lowest_priority );<br>
+<br>
+Â if( lowest_priority > node_priority ) {<br>
+Â Â //Backtrack on the path from<br>
+Â Â //_Thread_Get_CPU(lowest_reachable->user) to lowest_reachable, shifting<br>
+Â Â //along every task<br>
+<br>
+Â Â curr_node = CPU[ _Per_CPU_Get_index( cpu_to_preempt ) ].preempting_node;<br>
+Â Â curr_strong_node = _Scheduler_strong_APA_Node_downcast( curr_node );<br>
+Â Â curr_strong_node->cpu_to_preempt = cpu_to_preempt;<br>
+<br>
+Â Â //Save which cpu to preempt in cpu_to_preempt value of the node<br>
+Â Â while ( curr_node != node ) {<br>
+Â Â Â curr_CPU = _Thread_Get_CPU( curr_node->user );<br>
+Â Â Â curr_node = CPU[ _Per_CPU_Get_index( curr_CPU ) ].preempting_node;<br>
+Â Â Â curr_strong_node = _Scheduler_strong_APA_Node_downcast( curr_node );<br>
+Â Â Â curr_strong_node->cpu_to_preempt =Â curr_CPU;<br>
+Â Â Â }<br>
+<br>
+Â Â curr_CPU = curr_strong_node->cpu_to_preempt;<br>
+Â Â next_node = CPU[ _Per_CPU_Get_index( curr_CPU ) ].executing;<br>
+<br>
+Â Â node_priority = _Scheduler_Node_get_priority( curr_node );<br>
+Â Â node_priority = SCHEDULER_PRIORITY_PURIFY( node_priority );<br>
+<br>
+Â Â _Scheduler_SMP_Enqueue_to_scheduled(<br>
+Â Â Â context,<br>
+Â Â Â curr_node,<br>
+Â Â Â node_priority,<br>
+Â Â Â next_node,<br>
+Â Â Â _Scheduler_SMP_Insert_scheduled,<br>
+Â Â Â _Scheduler_strong_APA_Move_from_scheduled_to_ready,<br>
+Â Â Â _Scheduler_strong_APA_Allocate_processor<br>
+Â Â );<br>
<br>
-Â return (Scheduler_Node *) _Scheduler_priority_Ready_queue_first(<br>
-Â Â &self->Bit_map,<br>
-Â Â &self->Ready[ 0 ]<br>
-Â );<br>
+Â Â curr_node = next_node;<br>
+Â Â curr_strong_node = _Scheduler_strong_APA_Node_downcast( curr_node );<br>
+<br>
+Â Â while ( curr_node !=Â lowest_reachable ) {<br>
+Â Â Â curr_CPU = curr_strong_node->cpu_to_preempt;<br>
+Â Â Â next_node = CPU[ _Per_CPU_Get_index( curr_CPU ) ].executing;<br>
+Â Â Â //curr_node preempts the next_node;<br>
+Â Â Â _Scheduler_SMP_Preempt(<br>
+Â Â Â Â context,<br>
+Â Â Â Â curr_node,<br>
+Â Â Â Â next_node,<br>
+Â Â Â Â _Scheduler_strong_APA_Allocate_processor<br>
+Â Â Â );<br>
+<br>
+Â Â Â curr_node = next_node;<br>
+Â Â Â curr_strong_node = _Scheduler_strong_APA_Node_downcast( curr_node );<br>
+Â Â }<br>
+<br>
+Â Â _Scheduler_strong_APA_Move_from_scheduled_to_ready( context, lowest_reachable );<br>
+<br>
+Â Â needs_help = false;<br>
+Â } else {<br>
+Â Â needs_help = true;<br>
+Â }<br>
+<br>
+Â //Add it to Ready chain since it is now either scheduled or just ready.<br>
+Â _Scheduler_strong_APA_Insert_ready( context,node, insert_priority );<br>
+<br>
+Â return needs_help;<br>
 }<br>
<br>
-void _Scheduler_strong_APA_Block(<br>
-Â const Scheduler_Control *scheduler,<br>
- Thread_Control     *the_thread,<br>
- Scheduler_Node     *node<br>
+/*<br>
+ * BFS Algorithm for task arrival<br>
+ * Enqueue node either in the scheduled chain or in the ready chain.<br>
+ * node is the newly arrived node and is currently not scheduled.<br>
+ */<br>
+static inline bool _Scheduler_strong_APA_Enqueue(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *node,<br>
+ Priority_Control  insert_priority<br>
 )<br>
 {<br>
-Â Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
+Â Scheduler_strong_APA_Context *self;<br>
+Â Scheduler_strong_APA_CPUÂ Â Â *CPU;<br>
+ uint32_t           cpu_max;<br>
+ uint32_t           cpu_index;<br>
+ Per_CPU_Control       *cpu_to_preempt;<br>
+ Scheduler_Node        *lowest_reachable;<br>
+ Scheduler_strong_APA_Node  *strong_node;<br>
<br>
-Â _Scheduler_SMP_Block(<br>
+Â //Denotes front and rear of the queue<br>
+ uint32_t   front;<br>
+ uint32_t   rear;<br>
+<br>
+Â front = 0;<br>
+Â rear = -1;<br>
+<br>
+Â self = _Scheduler_strong_APA_Get_self( context );<br>
+Â strong_node = _Scheduler_strong_APA_Node_downcast( node );<br>
+Â cpu_max = _SMP_Get_processor_maximum();<br>
+Â CPU = self->CPU;<br>
+<br>
+Â for ( cpu_index = 0 ; cpu_index < cpu_max ; ++cpu_index ) {<br>
+Â Â CPU[ cpu_index ].visited = false;<br>
+<br>
+Â Â //Checks if the thread_CPU is in the affinity set of the node<br>
+Â Â if ( _Processor_mask_Is_set( &strong_node->Affinity, cpu_index ) ) {<br>
+Â Â Â Per_CPU_Control *cpu = _Per_CPU_Get_by_index( cpu_index );<br>
+<br>
+Â Â Â if ( _Per_CPU_Is_processor_online( cpu ) ) {<br>
+Â Â Â Â rear = rear + 1;<br>
+Â Â Â Â CPU[ rear ].cpu = cpu;<br>
+Â Â Â Â CPU[ cpu_index ].visited = true;<br>
+Â Â Â Â CPU[ cpu_index ].preempting_node = node;<br>
+Â Â Â }<br>
+Â Â }<br>
+Â }<br>
+<br>
+Â //This assert makes sure that there always exist an element in the<br>
+Â // Queue when we start the queue traversal.<br>
+Â _Assert( !_Processor_mask_Zero( &strong_node->Affinity ) );<br>
+<br>
+Â lowest_reachable = _Scheduler_strong_APA_Get_lowest_reachable(<br>
+Â Â Â Â Â Â Â Â Â Â Â Â self,<br>
+Â Â Â Â Â Â Â Â Â Â Â Â front,<br>
+Â Â Â Â Â Â Â Â Â Â Â Â rear,<br>
+Â Â Â Â Â Â Â Â Â Â Â Â &cpu_to_preempt<br>
+Â Â Â Â Â Â Â Â Â Â Â );<br>
+<br>
+Â return _Scheduler_strong_APA_Do_enqueue(<br>
+Â Â Â Â Â Â context,<br>
+Â Â Â Â Â Â lowest_reachable,<br>
+Â Â Â Â Â Â node,<br>
+Â Â Â Â Â Â insert_priority,<br>
+Â Â Â Â Â Â cpu_to_preempt<br>
+Â Â Â Â Â );<br>
+}<br>
+<br>
+static inline bool _Scheduler_strong_APA_Enqueue_scheduled(<br>
+Â Scheduler_Context *context,<br>
+ Scheduler_Node  *node,<br>
+ Priority_Control  insert_priority<br>
+)<br>
+{<br>
+Â return _Scheduler_SMP_Enqueue_scheduled(<br>
   context,<br>
-Â Â the_thread,<br>
   node,<br>
-Â Â _Scheduler_SMP_Extract_from_scheduled,<br>
+Â Â insert_priority,<br>
+Â Â _Scheduler_SMP_Priority_less_equal,<br>
   _Scheduler_strong_APA_Extract_from_ready,<br>
   _Scheduler_strong_APA_Get_highest_ready,<br>
+Â Â _Scheduler_strong_APA_Insert_ready,<br>
+Â Â _Scheduler_SMP_Insert_scheduled,<br>
   _Scheduler_strong_APA_Move_from_ready_to_scheduled,<br>
-Â Â _Scheduler_SMP_Allocate_processor_exact<br>
+Â Â _Scheduler_strong_APA_Allocate_processor<br>
  );<br>
 }<br>
<br>
-static bool _Scheduler_strong_APA_Enqueue(<br>
+static inline bool _Scheduler_strong_APA_Do_ask_for_help(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *node,<br>
- Priority_Control  insert_priority<br>
+ Thread_Control  *the_thread,<br>
+ Scheduler_Node  *node<br>
 )<br>
 {<br>
-Â return _Scheduler_SMP_Enqueue(<br>
+Â return _Scheduler_SMP_Ask_for_help(<br>
   context,<br>
+Â Â the_thread,<br>
   node,<br>
-Â Â insert_priority,<br>
   _Scheduler_SMP_Priority_less_equal,<br>
   _Scheduler_strong_APA_Insert_ready,<br>
   _Scheduler_SMP_Insert_scheduled,<br>
   _Scheduler_strong_APA_Move_from_scheduled_to_ready,<br>
-Â Â _Scheduler_SMP_Get_lowest_scheduled,<br>
-Â Â _Scheduler_SMP_Allocate_processor_exact<br>
+Â Â _Scheduler_strong_APA_Get_lowest_scheduled,<br>
+Â Â _Scheduler_strong_APA_Allocate_processor<br>
  );<br>
 }<br>
<br>
-static bool _Scheduler_strong_APA_Enqueue_scheduled(<br>
+static inline void _Scheduler_strong_APA_Do_set_affinity(<br>
  Scheduler_Context *context,<br>
- Scheduler_Node  *node,<br>
- Priority_Control insert_priority<br>
+ Scheduler_Node  *node_base,<br>
+ void       *arg<br>
 )<br>
 {<br>
-Â return _Scheduler_SMP_Enqueue_scheduled(<br>
+Â Scheduler_strong_APA_Node *node;<br>
+<br>
+Â node = _Scheduler_strong_APA_Node_downcast( node_base );<br>
+Â node->Affinity = *( (const Processor_mask *) arg );<br>
+}<br>
+<br>
+void _Scheduler_strong_APA_Initialize( const Scheduler_Control *scheduler )<br>
+{<br>
+Â Scheduler_strong_APA_Context *self =<br>
+Â Â Â _Scheduler_strong_APA_Get_context( scheduler );<br>
+<br>
+Â _Scheduler_SMP_Initialize( &self->Base );<br>
+Â _Chain_Initialize_empty( &self->Ready );<br>
+}<br>
+<br>
+void _Scheduler_strong_APA_Yield(<br>
+Â const Scheduler_Control *scheduler,<br>
+ Thread_Control     *thread,<br>
+ Scheduler_Node     *node<br>
+)<br>
+{<br>
+Â Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
+<br>
+Â _Scheduler_SMP_Yield(<br>
   context,<br>
+Â Â thread,<br>
   node,<br>
-Â Â insert_priority,<br>
-Â Â _Scheduler_SMP_Priority_less_equal,<br>
   _Scheduler_strong_APA_Extract_from_ready,<br>
-Â Â _Scheduler_strong_APA_Get_highest_ready,<br>
-Â Â _Scheduler_strong_APA_Insert_ready,<br>
-Â Â _Scheduler_SMP_Insert_scheduled,<br>
-Â Â _Scheduler_strong_APA_Move_from_ready_to_scheduled,<br>
-Â Â _Scheduler_SMP_Allocate_processor_exact<br>
+Â Â _Scheduler_strong_APA_Enqueue,<br>
+Â Â _Scheduler_strong_APA_Enqueue_scheduled<br>
  );<br>
 }<br>
<br>
-void _Scheduler_strong_APA_Unblock(<br>
+void _Scheduler_strong_APA_Block(<br>
  const Scheduler_Control *scheduler,<br>
- Thread_Control     *the_thread,<br>
+ Thread_Control     *thread,<br>
  Scheduler_Node     *node<br>
 )<br>
 {<br>
  Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
-<br>
-Â _Scheduler_SMP_Unblock(<br>
+Â //The extract from ready automatically removes the node from Ready chain.<br>
+Â _Scheduler_SMP_Block(<br>
   context,<br>
-Â Â the_thread,<br>
+Â Â thread,<br>
   node,<br>
-Â Â _Scheduler_strong_APA_Do_update,<br>
-Â Â _Scheduler_strong_APA_Enqueue<br>
+Â Â _Scheduler_strong_APA_Extract_from_scheduled,<br>
+Â Â _Scheduler_strong_APA_Extract_from_ready,<br>
+Â Â _Scheduler_strong_APA_Get_highest_ready,<br>
+Â Â _Scheduler_strong_APA_Move_from_ready_to_scheduled,<br>
+Â Â _Scheduler_strong_APA_Allocate_processor<br>
  );<br>
 }<br>
<br>
-static bool _Scheduler_strong_APA_Do_ask_for_help(<br>
-Â Scheduler_Context *context,<br>
- Thread_Control  *the_thread,<br>
- Scheduler_Node  *node<br>
+void _Scheduler_strong_APA_Unblock(<br>
+Â const Scheduler_Control *scheduler,<br>
+ Thread_Control     *thread,<br>
+ Scheduler_Node     *node<br>
 )<br>
 {<br>
-Â return _Scheduler_SMP_Ask_for_help(<br>
+Â Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
+<br>
+Â _Scheduler_SMP_Unblock(<br>
   context,<br>
-Â Â the_thread,<br>
+Â Â thread,<br>
   node,<br>
-Â Â _Scheduler_SMP_Priority_less_equal,<br>
-Â Â _Scheduler_strong_APA_Insert_ready,<br>
-Â Â _Scheduler_SMP_Insert_scheduled,<br>
-Â Â _Scheduler_strong_APA_Move_from_scheduled_to_ready,<br>
-Â Â _Scheduler_SMP_Get_lowest_scheduled,<br>
-Â Â _Scheduler_SMP_Allocate_processor_lazy<br>
+Â Â _Scheduler_strong_APA_Do_update,<br>
+Â Â _Scheduler_strong_APA_Enqueue<br>
  );<br>
 }<br>
<br>
 void _Scheduler_strong_APA_Update_priority(<br>
  const Scheduler_Control *scheduler,<br>
- Thread_Control     *the_thread,<br>
+ Thread_Control     *thread,<br>
  Scheduler_Node     *node<br>
 )<br>
 {<br>
@@ -327,7 +778,7 @@ void _Scheduler_strong_APA_Update_priority(<br>
<br>
  _Scheduler_SMP_Update_priority(<br>
   context,<br>
-Â Â the_thread,<br>
+Â Â thread,<br>
   node,<br>
   _Scheduler_strong_APA_Extract_from_ready,<br>
   _Scheduler_strong_APA_Do_update,<br>
@@ -345,7 +796,11 @@ bool _Scheduler_strong_APA_Ask_for_help(<br>
 {<br>
  Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
<br>
-Â return _Scheduler_strong_APA_Do_ask_for_help( context, the_thread, node );<br>
+Â return _Scheduler_strong_APA_Do_ask_for_help(<br>
+Â Â context,<br>
+Â Â the_thread,<br>
+Â Â node<br>
+Â );<br>
 }<br>
<br>
 void _Scheduler_strong_APA_Reconsider_help_request(<br>
@@ -381,7 +836,7 @@ void _Scheduler_strong_APA_Withdraw_node(<br>
   _Scheduler_strong_APA_Extract_from_ready,<br>
   _Scheduler_strong_APA_Get_highest_ready,<br>
   _Scheduler_strong_APA_Move_from_ready_to_scheduled,<br>
-Â Â _Scheduler_SMP_Allocate_processor_lazy<br>
+Â Â _Scheduler_strong_APA_Allocate_processor<br>
  );<br>
 }<br>
<br>
@@ -416,20 +871,65 @@ Thread_Control *_Scheduler_strong_APA_Remove_processor(<br>
  );<br>
 }<br>
<br>
-void _Scheduler_strong_APA_Yield(<br>
+void _Scheduler_strong_APA_Node_initialize(<br>
  const Scheduler_Control *scheduler,<br>
+ Scheduler_Node     *node,<br>
  Thread_Control     *the_thread,<br>
- Scheduler_Node     *node<br>
+ Priority_Control     priority<br>
 )<br>
 {<br>
-Â Scheduler_Context *context = _Scheduler_Get_context( scheduler );<br>
+Â Scheduler_SMP_Node *smp_node;<br>
+Â Scheduler_strong_APA_Node *strong_node;<br>
<br>
-Â _Scheduler_SMP_Yield(<br>
-Â Â context,<br>
-Â Â the_thread,<br>
-Â Â node,<br>
-Â Â _Scheduler_strong_APA_Extract_from_ready,<br>
-Â Â _Scheduler_strong_APA_Enqueue,<br>
-Â Â _Scheduler_strong_APA_Enqueue_scheduled<br>
+Â smp_node = _Scheduler_SMP_Node_downcast( node );<br>
+Â strong_node = _Scheduler_strong_APA_Node_downcast( node );<br>
+<br>
+Â _Scheduler_SMP_Node_initialize( scheduler, smp_node, the_thread, priority );<br>
+<br>
+Â _Processor_mask_Assign(<br>
+Â Â &strong_node->Affinity,<br>
+Â Â _SMP_Get_online_processors()<br>
  );<br>
 }<br>
+<br>
+bool _Scheduler_strong_APA_Set_affinity(<br>
+Â const Scheduler_Control *scheduler,<br>
+ Thread_Control     *thread,<br>
+ Scheduler_Node     *node_base,<br>
+ const Processor_mask  *affinity<br>
+)<br>
+{<br>
+ Scheduler_Context     *context;<br>
+Â Scheduler_strong_APA_Node *node;<br>
+ Processor_mask       local_affinity;<br>
+<br>
+Â context = _Scheduler_Get_context( scheduler );<br>
+Â _Processor_mask_And( &local_affinity, &context->Processors, affinity );<br>
+<br>
+Â if ( _Processor_mask_Is_zero( &local_affinity ) ) {<br>
+Â Â return false;<br>
+Â }<br>
+<br>
+Â node = _Scheduler_strong_APA_Node_downcast( node_base );<br>
+<br>
+Â if ( _Processor_mask_Is_equal( &node->Affinity, affinity ) )<br>
+Â Â return true;Â Â Â Â //Nothing to do. Return true.<br>
+<br>
+ _Processor_mask_Assign( &node->Affinity, &local_affinity );<br>
+<br>
+ _Scheduler_SMP_Set_affinity(<br>
+Â Â context,<br>
+Â Â thread,<br>
+Â Â node_base,<br>
+Â Â &local_affinity,<br>
+Â Â _Scheduler_strong_APA_Do_set_affinity,<br>
+Â Â _Scheduler_strong_APA_Extract_from_ready,<br>
+Â Â _Scheduler_strong_APA_Get_highest_ready,<br>
+Â Â _Scheduler_strong_APA_Move_from_ready_to_scheduled,<br>
+Â Â _Scheduler_strong_APA_Enqueue,<br>
+Â Â _Scheduler_strong_APA_Allocate_processor<br>
+ );<br>
+<br>
+Â return true;<br>
+}<br>
+<br>
-- <br>
2.17.1<br>
<br>
</blockquote></div>