diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 977e294730fe8..34308781ce15d 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 de4a922d1cccd..9cfa03bcb094a 100644 --- a/sched/sched/sched_sleep.c +++ b/sched/sched/sched_sleep.c @@ -52,42 +52,7 @@ static void nxsched_timeout(wdparm_t arg) { - FAR struct tcb_s *wtcb; - irqstate_t flags; - - /* Get waiting tcb from parameter */ - - wtcb = (FAR struct tcb_s *)(uintptr_t)arg; - - /* We must be in a critical section in order to call up_switch_context() - * below. - */ - - flags = enter_critical_section(); - - /* There may be a race condition -- make sure the task is - * still waiting for a signal - */ - - if (wtcb->task_state == TSTATE_WAIT_SIG) - { - FAR struct tcb_s *rtcb = this_task(); - - /* Remove the task from waiting list */ - - dq_rem((FAR dq_entry_t *)wtcb, list_waitingforsignal()); - - /* Add the task to ready-to-run task list, and - * perform the context switch if one is needed - */ - - if (nxsched_add_readytorun(wtcb)) - { - up_switch_context(this_task(), rtcb); - } - } - - leave_critical_section(flags); + nxsched_wakeup((FAR struct tcb_s *)(uintptr_t)arg); } /**************************************************************************** @@ -130,7 +95,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 +108,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 *