From 2b33168cc0fe497f56f6086a88764701eb2f0b6a Mon Sep 17 00:00:00 2001 From: jiangtao16 Date: Thu, 14 Aug 2025 15:45:59 +0800 Subject: [PATCH] sched/semaphore: Fix illegal abstime. Remove CONFIG_DEBUG_FEATURES to check illegal abstime. Signed-off-by: jiangtao16 --- sched/semaphore/sem_clockwait.c | 83 +++++++++++++++------------------ 1 file changed, 37 insertions(+), 46 deletions(-) diff --git a/sched/semaphore/sem_clockwait.c b/sched/semaphore/sem_clockwait.c index db4b1b6b708..f94ac81e809 100644 --- a/sched/semaphore/sem_clockwait.c +++ b/sched/semaphore/sem_clockwait.c @@ -94,7 +94,7 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid, { FAR struct tcb_s *rtcb = this_task(); irqstate_t flags; - int ret = ERROR; + int ret = -EINVAL; DEBUGASSERT(sem != NULL && abstime != NULL); DEBUGASSERT(up_interrupt_context() == false); @@ -107,55 +107,46 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid, * enabled while we are blocked waiting for the semaphore. */ - flags = enter_critical_section(); - - /* Try to take the semaphore without waiting. */ - - ret = nxsem_trywait(sem); - if (ret == OK) + if (abstime->tv_nsec >= 0 && abstime->tv_nsec < 1000000000) { - /* We got it! */ + flags = enter_critical_section(); - goto out; + /* Try to take the semaphore without waiting. */ + + ret = nxsem_trywait(sem); + if (ret != OK) + { + /* We will have to wait for the semaphore. Make sure that + * we were provided with a valid timeout. + */ + + if (clockid == CLOCK_REALTIME) + { + wd_start_realtime(&rtcb->waitdog, abstime, + nxsem_timeout, (uintptr_t)rtcb); + } + else + { + wd_start_abstime(&rtcb->waitdog, abstime, + nxsem_timeout, (uintptr_t)rtcb); + } + + /* Now perform the blocking wait. If nxsem_wait() fails, the + * negated errno value will be returned below. + */ + + ret = nxsem_wait(sem); + + /* Stop the watchdog timer */ + + wd_cancel(&rtcb->waitdog); + } + + /* We can now restore interrupts and delete the watchdog */ + + leave_critical_section(flags); } - /* We will have to wait for the semaphore. Make sure that we were provided - * with a valid timeout. - */ - -#ifdef CONFIG_DEBUG_FEATURES - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) - { - ret = -EINVAL; - goto out; - } -#endif - - if (clockid == CLOCK_REALTIME) - { - wd_start_realtime(&rtcb->waitdog, abstime, - nxsem_timeout, (uintptr_t)rtcb); - } - else - { - wd_start_abstime(&rtcb->waitdog, abstime, - nxsem_timeout, (uintptr_t)rtcb); - } - - /* Now perform the blocking wait. If nxsem_wait() fails, the - * negated errno value will be returned below. - */ - - ret = nxsem_wait(sem); - - /* Stop the watchdog timer */ - - wd_cancel(&rtcb->waitdog); - - /* We can now restore interrupts and delete the watchdog */ - -out: - leave_critical_section(flags); return ret; }