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

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

commit 5beb4ae6d5d494ccff0401fc80a9b9a04539be28
Author: wangchengdong <[email protected]>
AuthorDate: Mon Oct 27 09:17:00 2025 +0800

    sched/sched: Introduce nxsched_wakeup()
    
        Add a new function nxsched_wakeup() to the scheduler,
        which allows waking up a sleeping task before its timeout.
    
    Signed-off-by: Chengdong Wang [email protected]
---
 include/nuttx/sched.h     | 23 +++++++++++++++++++++-
 sched/sched/sched_sleep.c | 49 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 69 insertions(+), 3 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 977e294730f..34308781ce1 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -264,7 +264,8 @@ enum tstate_e
   TSTATE_TASK_STOPPED,        /* BLOCKED      - Waiting for SIGCONT */
 #endif
 
-  NUM_TASK_STATES             /* Must be last */
+  NUM_TASK_STATES,
+  TSTATE_SLEEPING = TSTATE_WAIT_SIG /* Map TSTATE_SLEEPING to TSTATE_WAIT_SIG 
*/
 };
 
 typedef enum tstate_e tstate_t;
@@ -1733,6 +1734,26 @@ int nxsched_smp_call_async(cpu_set_t cpuset,
 
 void nxsched_ticksleep(unsigned int ticks);
 
+/****************************************************************************
+ * Name: nxsched_wakeup
+ *
+ * Description:
+ *   The nxsched_wakeup() function is used to wake up a task that is
+ *   currently in the sleeping state before its timeout expires.
+ *
+ *   This function can be used by internal scheduler logic or by
+ *   system-level components that need to resume a sleeping task early.
+ *
+ * Input Parameters:
+ *   tcb - Pointer to the TCB of the task to be awakened.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void nxsched_wakeup(FAR struct tcb_s *tcb);
+
 /****************************************************************************
  * Name: nxsched_usleep
  *
diff --git a/sched/sched/sched_sleep.c b/sched/sched/sched_sleep.c
index de4a922d1cc..754e5e987ed 100644
--- a/sched/sched/sched_sleep.c
+++ b/sched/sched/sched_sleep.c
@@ -130,7 +130,7 @@ void nxsched_ticksleep(unsigned int ticks)
 
   /* Add the task to the specified blocked task list */
 
-  rtcb->task_state = TSTATE_WAIT_SIG;
+  rtcb->task_state = TSTATE_SLEEPING;
   dq_addlast((FAR dq_entry_t *)rtcb, list_waitingforsignal());
 
   /* Now, perform the context switch if one is needed */
@@ -143,9 +143,54 @@ void nxsched_ticksleep(unsigned int ticks)
 }
 
 /****************************************************************************
- * Public Functions
+ * Name: nxsched_wakeup
+ *
+ * Description:
+ *   The nxsched_wakeup() function is used to wake up a task that is
+ *   currently in the sleeping state before its timeout expires.
+ *
+ *   This function can be used by internal scheduler logic or by
+ *   system-level components that need to resume a sleeping task early.
+ *
+ * Input Parameters:
+ *   tcb - Pointer to the TCB of the task to be awakened.
+ *
+ * Returned Value:
+ *   None
+ *
  ****************************************************************************/
 
+void nxsched_wakeup(FAR struct tcb_s *tcb)
+{
+  irqstate_t flags;
+
+  DEBUGASSERT(tcb != NULL);
+
+  flags = enter_critical_section();
+
+  if (tcb->task_state == TSTATE_SLEEPING)
+    {
+      FAR struct tcb_s *rtcb = this_task();
+
+      /* Remove the task from sleeping list */
+
+      dq_rem((FAR dq_entry_t *)tcb, list_waitingforsignal());
+
+      wd_cancel(&tcb->waitdog);
+
+      /* Add the task to ready-to-run task list, and
+       * perform the context switch if one is needed
+       */
+
+      if (nxsched_add_readytorun(tcb))
+        {
+          up_switch_context(this_task(), rtcb);
+        }
+    }
+
+  leave_critical_section(flags);
+}
+
 /****************************************************************************
  * Name: nxsched_usleep
  *

Reply via email to