pussuw commented on code in PR #16194: URL: https://github.com/apache/nuttx/pull/16194#discussion_r2058865683
########## include/nuttx/mutex.h: ########## @@ -754,6 +571,273 @@ int nxrmutex_restorelock(FAR rmutex_t *rmutex, unsigned int count); #define nxrmutex_setprioceiling(rmutex, prioceiling, old_ceiling) \ nxmutex_setprioceiling(&(rmutex)->mutex, prioceiling, old_ceiling) +/**************************************************************************** + * Inline functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxmutex_destroy + * + * Description: + * This function initializes the UNNAMED mutex. Following a + * successful call to nxmutex_init(), the mutex may be used in subsequent + * calls to nxmutex_lock(), nxmutex_unlock(), and nxmutex_trylock(). The + * mutex remains usable until it is destroyed. + * + * Parameters: + * mutex - Semaphore to be destroyed + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * + ****************************************************************************/ + +static inline int nxmutex_destroy(FAR mutex_t *mutex) +{ + return nxsem_destroy(&mutex->sem); +} + +/**************************************************************************** + * Name: nxmutex_lock + * + * Description: + * This function attempts to lock the mutex referenced by 'mutex'. The + * mutex is implemented with a semaphore, so if the semaphore value is + * (<=) zero, then the calling task will not return until it successfully + * acquires the lock. + * + * Parameters: + * mutex - mutex descriptor. + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + ****************************************************************************/ + +static inline int nxmutex_lock(FAR mutex_t *mutex) +{ + int ret; + + ret = nxsem_wait(&mutex->sem); + if (ret >= 0) + { + nxmutex_add_backtrace(mutex); + } + + return ret; +} + +/**************************************************************************** + * Name: nxmutex_trylock + * + * Description: + * This function locks the mutex only if the mutex is currently not locked. + * If the mutex has been locked already, the call returns without blocking. + * + * Parameters: + * mutex - mutex descriptor. + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + * -EINVAL - Invalid attempt to lock the mutex + * -EAGAIN - The mutex is not available. + * + ****************************************************************************/ + +static inline int nxmutex_trylock(FAR mutex_t *mutex) +{ + int ret; + + ret = nxsem_trywait(&mutex->sem); + if (ret >= 0) + { + nxmutex_add_backtrace(mutex); + } + + return ret; +} + +/**************************************************************************** + * Name: nxmutex_unlock + * + * Description: + * This function attempts to unlock the mutex referenced by 'mutex'. + * + * Parameters: + * mutex - mutex descriptor. + * + * Return Value: + * This is an internal OS interface and should not be used by applications. + * It follows the NuttX internal error return policy: Zero (OK) is + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: + * + * Assumptions: + * This function may be called from an interrupt handler. + * + ****************************************************************************/ + +static inline int nxmutex_unlock(FAR mutex_t *mutex) +{ + return nxsem_post(&mutex->sem); +} + +/**************************************************************************** + * Name: nxmutex_reset + * + * Description: + * This function reset lock state. + * + * Parameters: + * mutex - mutex descriptor. + * + ****************************************************************************/ + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) +static inline void nxmutex_reset(FAR mutex_t *mutex) +{ + nxsem_reset(&mutex->sem, 1); Review Comment: A mutex does not have a count. This logic and the logic in nxsem_reset is redundant. Setting the holder to 0 should suffice ? -- 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