Skip to content

Commit

Permalink
sched: replace sync pause with async pause for setpriority
Browse files Browse the repository at this point in the history
reason:
In the kernel, we are planning to remove all occurrences of up_cpu_pause as one of the steps to
simplify the implementation of critical sections. The goal is to enable spin_lock_irqsave to encapsulate critical sections,
thereby facilitating the replacement of critical sections(big lock) with smaller spin_lock_irqsave(small lock)

Configuring NuttX and compile:
$ ./tools/configure.sh -l qemu-armv8a:nsh_smp
$ make
Running with qemu
$ qemu-system-aarch64 -cpu cortex-a53 -smp 4 -nographic
-machine virt,virtualization=on,gic-version=3
-net none -chardev stdio,id=con,mux=on -serial chardev:con
-mon chardev=con,mode=readline -kernel ./nuttx

Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 authored and xiaoxiang781216 committed Oct 5, 2024
1 parent 8e89984 commit 0b0f535
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions sched/sched/sched_setpriority.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,58 @@
#include "irq/irq.h"
#include "sched/sched.h"

/****************************************************************************
* Private Type Declarations
****************************************************************************/

#ifdef CONFIG_SMP
struct reprioritize_arg_s
{
pid_t pid;
cpu_set_t saved_affinity;
uint16_t saved_flags;
int sched_priority;
bool need_restore;
};

/****************************************************************************
* Private Functions
****************************************************************************/

static int reprioritize_handler(FAR void *cookie)
{
FAR struct reprioritize_arg_s *arg = cookie;
FAR struct tcb_s *rtcb = this_task();
FAR struct tcb_s *tcb;
irqstate_t flags;

flags = enter_critical_section();

tcb = nxsched_get_tcb(arg->pid);

if (!tcb || tcb->task_state == TSTATE_TASK_INVALID ||
(tcb->flags & TCB_FLAG_EXIT_PROCESSING) != 0)
{
leave_critical_section(flags);
return OK;
}

if (arg->need_restore)
{
tcb->affinity = arg->saved_affinity;
tcb->flags = arg->saved_flags;
}

if (nxsched_reprioritize_rtr(tcb, arg->sched_priority))
{
up_switch_context(this_task(), rtcb);
}

leave_critical_section(flags);
return OK;
}
#endif

/****************************************************************************
* Name: nxsched_nexttcb
*
Expand Down Expand Up @@ -175,6 +223,34 @@ static inline void nxsched_running_setpriority(FAR struct tcb_s *tcb,
{
/* A context switch will occur. */

#ifdef CONFIG_SMP
if (tcb->cpu != this_cpu() &&
tcb->task_state == TSTATE_TASK_RUNNING)
{
struct reprioritize_arg_s arg;

if ((tcb->flags & TCB_FLAG_CPU_LOCKED) != 0)
{
arg.pid = tcb->pid;
arg.need_restore = false;
}
else
{
arg.pid = tcb->pid;
arg.saved_flags = tcb->flags;
arg.saved_affinity = tcb->affinity;
arg.need_restore = true;

tcb->flags |= TCB_FLAG_CPU_LOCKED;
CPU_SET(tcb->cpu, &tcb->affinity);
}

arg.sched_priority = sched_priority;
nxsched_smp_call_single(tcb->cpu, reprioritize_handler,
&arg, true);
}
else
#endif
if (nxsched_reprioritize_rtr(tcb, sched_priority))
{
up_switch_context(this_task(), rtcb);
Expand Down

0 comments on commit 0b0f535

Please sign in to comment.