xiaoxiang781216 commented on code in PR #15075: URL: https://github.com/apache/nuttx/pull/15075#discussion_r1877801792
########## sched/semaphore/sem_trywait.c: ########## @@ -38,6 +38,76 @@ #include "sched/sched.h" #include "semaphore/semaphore.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsem_trywait_slow + * + * Description: + * This function locks the specified semaphore in slow mode. + * + * Input Parameters: + * sem - the semaphore descriptor + * + * Returned Value: + * + * EINVAL - Invalid attempt to get the semaphore + * EAGAIN - The semaphore is not available. + * + * Assumptions: + * + ****************************************************************************/ + +static int nxsem_trywait_slow(FAR sem_t *sem) +{ + FAR struct tcb_s *rtcb; + irqstate_t flags; + int32_t semcount; + int ret; + + /* The following operations must be performed with interrupts disabled + * because sem_post() may be called from an interrupt handler. + */ + + flags = enter_critical_section(); + rtcb = this_task(); + + /* If the semaphore is available, give it to the requesting task */ + + semcount = atomic_read(NXSEM_COUNT(sem)); + do + { + if (semcount <= 0) + { + leave_critical_section(flags); + return -EAGAIN; + } + } + while (!atomic_try_cmpxchg_acquire(NXSEM_COUNT(sem), + &semcount, semcount - 1)); + + /* It is, let the task take the semaphore */ + + ret = nxsem_protect_wait(sem); + if (ret < 0) + { + atomic_fetch_add(NXSEM_COUNT(sem), 1); + leave_critical_section(flags); + return ret; + } + + nxsem_add_holder(sem); + rtcb->waitobj = NULL; + ret = OK; Review Comment: remove -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org