jlaitine commented on code in PR #16194: URL: https://github.com/apache/nuttx/pull/16194#discussion_r2074856652
########## include/nuttx/mutex.h: ########## @@ -354,407 +384,497 @@ void nxmutex_reset(FAR mutex_t *mutex); * ****************************************************************************/ -int nxmutex_breaklock(FAR mutex_t *mutex, FAR unsigned int *locked); +int nxrmutex_breaklock(FAR rmutex_t *rmutex, FAR unsigned int *count); /**************************************************************************** - * Name: nxmutex_restorelock + * Name: nxrmutex_restorelock * * Description: - * This function attempts to restore the mutex. + * This function attempts to restore the recursive mutex. * * Parameters: - * mutex - mutex descriptor. - * locked - true: it's mean that the mutex is broke success + * rmutex - Recursive 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 + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: * ****************************************************************************/ -int nxmutex_restorelock(FAR mutex_t *mutex, unsigned int locked); +int nxrmutex_restorelock(FAR rmutex_t *rmutex, unsigned int count); + +#define nxrmutex_set_protocol(rmutex, protocol) \ + nxmutex_set_protocol(&(rmutex)->mutex, protocol) +#define nxrmutex_getprioceiling(rmutex, prioceiling) \ + nxmutex_getprioceiling(&(rmutex)->mutex, prioceiling) +#define nxrmutex_setprioceiling(rmutex, prioceiling, old_ceiling) \ + nxmutex_setprioceiling(&(rmutex)->mutex, prioceiling, old_ceiling) + +/**************************************************************************** + * Inline functions + ****************************************************************************/ /**************************************************************************** - * Name: nxmutex_set_protocol + * Name: nxmutex_get_holder * * Description: - * This function attempts to set the priority protocol of a mutex. + * This function get the holder of the mutex referenced by 'mutex'. + * Note that this is inherently racy unless the calling thread is + * holding the mutex. * * Parameters: - * mutex - mutex descriptor. - * protocol - mutex protocol value to set. + * 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 * ****************************************************************************/ -int nxmutex_set_protocol(FAR mutex_t *mutex, int protocol); +static inline pid_t nxmutex_get_holder(FAR mutex_t *mutex) +{ + uint32_t mholder = mutex->sem.val.mholder & ~NXSEM_MBLOCKING_BIT; Review Comment: This is not necessary, and is not even possible. Including nuttx/atomic.h in mutex.h will lead to horrible include dependency problems for some platforms. I'd rather remove this function, together with nxmutex_is_locked in the future; there is no reason to have this kind of interfaces for mutex... ########## include/nuttx/mutex.h: ########## @@ -354,407 +384,497 @@ void nxmutex_reset(FAR mutex_t *mutex); * ****************************************************************************/ -int nxmutex_breaklock(FAR mutex_t *mutex, FAR unsigned int *locked); +int nxrmutex_breaklock(FAR rmutex_t *rmutex, FAR unsigned int *count); /**************************************************************************** - * Name: nxmutex_restorelock + * Name: nxrmutex_restorelock * * Description: - * This function attempts to restore the mutex. + * This function attempts to restore the recursive mutex. * * Parameters: - * mutex - mutex descriptor. - * locked - true: it's mean that the mutex is broke success + * rmutex - Recursive 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 + * returned on success. A negated errno value is returned on failure. + * Possible returned errors: * ****************************************************************************/ -int nxmutex_restorelock(FAR mutex_t *mutex, unsigned int locked); +int nxrmutex_restorelock(FAR rmutex_t *rmutex, unsigned int count); + +#define nxrmutex_set_protocol(rmutex, protocol) \ + nxmutex_set_protocol(&(rmutex)->mutex, protocol) +#define nxrmutex_getprioceiling(rmutex, prioceiling) \ + nxmutex_getprioceiling(&(rmutex)->mutex, prioceiling) +#define nxrmutex_setprioceiling(rmutex, prioceiling, old_ceiling) \ + nxmutex_setprioceiling(&(rmutex)->mutex, prioceiling, old_ceiling) + +/**************************************************************************** + * Inline functions + ****************************************************************************/ /**************************************************************************** - * Name: nxmutex_set_protocol + * Name: nxmutex_get_holder * * Description: - * This function attempts to set the priority protocol of a mutex. + * This function get the holder of the mutex referenced by 'mutex'. + * Note that this is inherently racy unless the calling thread is + * holding the mutex. * * Parameters: - * mutex - mutex descriptor. - * protocol - mutex protocol value to set. + * 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 * ****************************************************************************/ -int nxmutex_set_protocol(FAR mutex_t *mutex, int protocol); +static inline pid_t nxmutex_get_holder(FAR mutex_t *mutex) +{ + uint32_t mholder = mutex->sem.val.mholder & ~NXSEM_MBLOCKING_BIT; + return NXSEM_MACQUIRED(mholder) ? (pid_t)mholder : -1; +} /**************************************************************************** - * Name: nxmutex_getprioceiling + * Name: nxmutex_is_locked * * Description: - * This function attempts to get the priority ceiling of a mutex. + * This function get the lock state the mutex referenced by 'mutex'. + * Note that this is inherently racy unless the calling thread is + * holding the mutex. * * Parameters: - * mutex - mutex descriptor. - * prioceiling - location to return the mutex priority ceiling. + * 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 * ****************************************************************************/ -int nxmutex_getprioceiling(FAR const mutex_t *mutex, FAR int *prioceiling); +static inline bool nxmutex_is_locked(FAR mutex_t *mutex) +{ + return NXSEM_MACQUIRED(mutex->sem.val.mholder); Review Comment: See my reply above -- 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