This is an automated email from the ASF dual-hosted git repository.

ligd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit fc15b8da8a1e2839f1b980673ffd41d845485024
Author: guoshengyuan1 <[email protected]>
AuthorDate: Tue Sep 23 10:08:20 2025 +0800

    arch: unify the scheduling information of task_exit
    
    Replace all nxsched_suspend/resume_*** with nxsched_switch_context
    
    Delete nxsched_resume_scheduler in up_exit and call it uniformly in 
task_exit
    
    Co-authored-by: yinshengkai <[email protected]>
    Signed-off-by: guoshengyuan1 <[email protected]>
---
 arch/avr/src/common/avr_exit.c         |  1 -
 arch/hc/src/common/hc_exit.c           |  1 -
 arch/or1k/src/common/or1k_exit.c       |  1 -
 arch/renesas/src/common/renesas_exit.c |  1 -
 arch/sim/src/sim/sim_exit.c            |  1 -
 arch/sparc/src/common/sparc_exit.c     |  4 ----
 arch/x86/src/common/x86_exit.c         |  1 -
 arch/x86_64/src/common/x86_64_exit.c   |  1 -
 arch/z16/src/common/z16_exit.c         |  1 -
 arch/z80/src/common/z80_exit.c         |  1 -
 include/nuttx/sched.h                  | 21 +++++++++++++++++++++
 sched/task/task_exit.c                 | 16 ++++++++--------
 12 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/arch/avr/src/common/avr_exit.c b/arch/avr/src/common/avr_exit.c
index 1d304e3170b..4d19bb03745 100644
--- a/arch/avr/src/common/avr_exit.c
+++ b/arch/avr/src/common/avr_exit.c
@@ -69,7 +69,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/arch/hc/src/common/hc_exit.c b/arch/hc/src/common/hc_exit.c
index fd413d4fb95..3732c4b1d3c 100644
--- a/arch/hc/src/common/hc_exit.c
+++ b/arch/hc/src/common/hc_exit.c
@@ -68,7 +68,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/arch/or1k/src/common/or1k_exit.c b/arch/or1k/src/common/or1k_exit.c
index 022be0df25b..3923f58d36b 100644
--- a/arch/or1k/src/common/or1k_exit.c
+++ b/arch/or1k/src/common/or1k_exit.c
@@ -70,7 +70,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/arch/renesas/src/common/renesas_exit.c 
b/arch/renesas/src/common/renesas_exit.c
index 2d93a97d547..feed938f70b 100644
--- a/arch/renesas/src/common/renesas_exit.c
+++ b/arch/renesas/src/common/renesas_exit.c
@@ -68,7 +68,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/arch/sim/src/sim/sim_exit.c b/arch/sim/src/sim/sim_exit.c
index 9e2e62631ab..7088b8edcb9 100644
--- a/arch/sim/src/sim/sim_exit.c
+++ b/arch/sim/src/sim/sim_exit.c
@@ -69,7 +69,6 @@ void up_exit(int status)
    * NOTE: the API also adjusts the global IRQ control for SMP
    */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
   /* Restore the cpu lock */
diff --git a/arch/sparc/src/common/sparc_exit.c 
b/arch/sparc/src/common/sparc_exit.c
index 1a1d2310c61..34c185a4f0a 100644
--- a/arch/sparc/src/common/sparc_exit.c
+++ b/arch/sparc/src/common/sparc_exit.c
@@ -58,10 +58,6 @@ void up_exit(int status)
 {
   struct tcb_s *tcb = this_task();
 
-  /* Update scheduler parameters */
-
-  nxsched_suspend_scheduler(tcb);
-
   /* Destroy the task at the head of the ready to run list. */
 
   (void)nxtask_exit();
diff --git a/arch/x86/src/common/x86_exit.c b/arch/x86/src/common/x86_exit.c
index add3ac5d467..1e3045bb607 100644
--- a/arch/x86/src/common/x86_exit.c
+++ b/arch/x86/src/common/x86_exit.c
@@ -71,7 +71,6 @@ void up_exit(int status)
    * NOTE: the API also adjusts the global IRQ control for SMP
    */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/arch/x86_64/src/common/x86_64_exit.c 
b/arch/x86_64/src/common/x86_64_exit.c
index 9482b0af054..8e74d349efa 100644
--- a/arch/x86_64/src/common/x86_64_exit.c
+++ b/arch/x86_64/src/common/x86_64_exit.c
@@ -70,7 +70,6 @@ void up_exit(int status)
    * NOTE: the API also adjusts the global IRQ control for SMP
    */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
   /* Context switch, rearrange MMU */
diff --git a/arch/z16/src/common/z16_exit.c b/arch/z16/src/common/z16_exit.c
index c254eb2f778..370c0007e42 100644
--- a/arch/z16/src/common/z16_exit.c
+++ b/arch/z16/src/common/z16_exit.c
@@ -69,7 +69,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
   /* Then switch contexts */
diff --git a/arch/z80/src/common/z80_exit.c b/arch/z80/src/common/z80_exit.c
index 6212af3594b..e6db9d5aa38 100644
--- a/arch/z80/src/common/z80_exit.c
+++ b/arch/z80/src/common/z80_exit.c
@@ -71,7 +71,6 @@ void up_exit(int status)
 
   /* Adjusts time slice for SCHED_RR & SCHED_SPORADIC cases */
 
-  nxsched_resume_scheduler(tcb);
   g_running_tasks[this_cpu()] = tcb;
 
 #ifdef CONFIG_ARCH_ADDRENV
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 0ae6d2c7260..218ac30d4a6 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -1260,6 +1260,27 @@ void nxsched_suspend_scheduler(FAR struct tcb_s *tcb);
 #  define nxsched_suspend_scheduler(tcb)
 #endif
 
+/****************************************************************************
+ * Name: nxsched_switch_context
+ *
+ * Description:
+ *   This function is used to switch context between two tasks.
+ *
+ * Input Parameters:
+ *   from - The TCB of the task to be suspended.
+ *   to   - The TCB of the task to be resumed.
+ *
+ * Returned Value:
+ *   None
+ ****************************************************************************/
+
+static inline void nxsched_switch_context(FAR struct tcb_s *from,
+                                          FAR struct tcb_s *to)
+{
+  nxsched_suspend_scheduler(from);
+  nxsched_resume_scheduler(to);
+}
+
 /****************************************************************************
  * Name: nxsched_get_param
  *
diff --git a/sched/task/task_exit.c b/sched/task/task_exit.c
index 9a021849eba..f2df5d6c4be 100644
--- a/sched/task/task_exit.c
+++ b/sched/task/task_exit.c
@@ -90,10 +90,6 @@ int nxtask_exit(void)
   sinfo("%s pid=%d,TCB=%p\n", get_task_name(dtcb),
         dtcb->pid, dtcb);
 
-  /* Update scheduler parameters */
-
-  nxsched_suspend_scheduler(dtcb);
-
   /* Remove the TCB of the current task from the ready-to-run list.  A
    * context switch will definitely be necessary -- that must be done
    * by the architecture-specific logic.
@@ -112,12 +108,16 @@ int nxtask_exit(void)
   rtcb = this_task();
 #endif
 
-  /* NOTE: nxsched_resume_scheduler() was moved to up_exit()
-   * because the global IRQ control for SMP should be deferred until
-   * context switching, otherwise, the context switching would be done
-   * without a critical section
+  /* Update scheduler parameters.
+   *
+   * When the thread exits, SYS_restore_context is called to
+   * restore the context, which does not update the scheduling
+   * information.
+   * We need to update the scheduling information before tcb is released.
    */
 
+  nxsched_switch_context(dtcb, rtcb);
+
   /* We are now in a bad state -- the head of the ready to run task list
    * does not correspond to the thread that is running.  Disabling pre-
    * emption on this TCB and marking the new ready-to-run task as not

Reply via email to