sched/sched: Fix bug of sched_setaffinity()

TESTCASE: A thread running on CPU0 calls sched_setaffinity() to run on CPU1, but the execution result shows that only the value of tcb->affinity is modified, and it does not actually switch to CPU1.

REASON: The underlying function nxsched_running_setpriority() called by sched_setaffinity does not take into account that when changing CPU affinity, each CPU's task queue needs to be adjusted and context switching needs to be performed. Therefore, it is necessary to add a check to see if the new CPU affinity also includes the previously running CPU. If not, the task needs to be put into the new CPU queue and context switching needs to be performed.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16
2024-12-23 18:52:51 +08:00
committed by GUIDINGLI
parent 635f5bf9ef
commit 01f7ff2bb7
+10 -3
View File
@@ -76,12 +76,19 @@ static inline void nxsched_running_setpriority(FAR struct tcb_s *tcb,
nxttcb = tcb->flink;
#endif
/* A context switch will occur if the new priority of the running
* task becomes less than OR EQUAL TO the next highest priority
* ready to run task.
/* A context switch will occur:
* CASE 1. The new priority of the running task becomes less than or
* equal to the next highest priority ready to run task.
* CASE 2. In SMP, the affinity of tcb has changed and no longer includes
* the current cpu.
*/
#ifdef CONFIG_SMP
if (nxttcb && (sched_priority <= nxttcb->sched_priority ||
(tcb->affinity & (1 << tcb->cpu)) == 0))
#else
if (nxttcb && sched_priority <= nxttcb->sched_priority)
#endif
{
#ifdef CONFIG_SMP
tcb->sched_priority = (uint8_t)sched_priority;