xiaoxiang781216 commented on code in PR #16194:
URL: https://github.com/apache/nuttx/pull/16194#discussion_r2075344025


##########
sched/semaphore/sem_waitirq.c:
##########
@@ -72,31 +72,54 @@ void nxsem_wait_irq(FAR struct tcb_s *wtcb, int errcode)
 {
   FAR struct tcb_s *rtcb = this_task();
   FAR sem_t *sem = wtcb->waitobj;
+  bool mutex = NXSEM_IS_MUTEX(sem);
 
   /* It is possible that an interrupt/context switch beat us to the punch
    * and already changed the task's state.
    */
 
-  DEBUGASSERT(sem != NULL && atomic_read(NXSEM_COUNT(sem)) < 0);
+  DEBUGASSERT(sem != NULL);
+  DEBUGASSERT((mutex || atomic_read(NXSEM_COUNT(sem)) < 0) &&
+              (!mutex || NXSEM_MBLOCKING(atomic_read(NXSEM_MHOLDER(sem)))));

Review Comment:
   let's spit to two assert like other similar places, which also make the 
assertion trigger condition more clear.



##########
sched/semaphore/sem_post.c:
##########
@@ -85,19 +87,54 @@ int nxsem_post_slow(FAR sem_t *sem)
 
   flags = enter_critical_section();
 
-  /* Check the maximum allowable value */
+  if (mutex)
+    {
+      /* Mutex post from interrupt context is not allowed */
+
+      DEBUGASSERT(!up_interrupt_context());
+
+      /* Lock the mutex for us by setting the blocking bit */
+
+      mholder = atomic_fetch_or(NXSEM_MHOLDER(sem), NXSEM_MBLOCKING_BIT);
+
+      /* Mutex post from another thread is not allowed, unless
+       * called from nxsem_reset
+       */
+
+      DEBUGASSERT(mholder == (NXSEM_MBLOCKING_BIT | NXSEM_MRESET) ||
+                  (mholder & (~NXSEM_MBLOCKING_BIT)) == nxsched_gettid());
+
+      blocking = NXSEM_MBLOCKING(mholder);
 
-  sem_count = atomic_read(NXSEM_COUNT(sem));
-  do
+      if (!blocking)

Review Comment:
   ```
   if (!blocking && mholder != NXSEM_MRESET)
     {
       atomic_set(NXSEM_MHOLDER(sem), NXSEM_NO_MHOLDER);
     }
   ```



##########
include/nuttx/mutex.h:
##########
@@ -354,407 +384,499 @@ 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_function 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_function bool nxmutex_is_locked(FAR mutex_t *mutex)
+{
+  return NXSEM_MACQUIRED(mutex->sem.val.mholder);
+}
 
 /****************************************************************************
- * Name: nxmutex_setprioceiling
+ * Name: nxmutex_destroy
  *
  * Description:
- *   This function attempts to set the priority ceiling of a mutex.
+ *   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        - mutex descriptor.
- *   prioceiling  - mutex priority ceiling value to set.
- *   old_ceiling  - location to return the mutex ceiling priority set before.
+ *   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
+ *   returned on success.  A negated errno value is returned on failure.
  *
  ****************************************************************************/
 
-int nxmutex_setprioceiling(FAR mutex_t *mutex, int prioceiling,
-                           FAR int *old_ceiling);
+static inline_function int nxmutex_destroy(FAR mutex_t *mutex)
+{
+  return nxsem_destroy(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_init
+ * Name: nxmutex_lock
  *
  * Description:
- *   This function initializes the UNNAMED recursive mutex. Following a
- *   successful call to nxrmutex_init(), the recursive mutex may be used in
- *   subsequent calls to nxrmutex_lock(), nxrmutex_unlock(),
- *   and nxrmutex_trylock(). The recursive mutex remains usable
- *   until it is destroyed.
+ *   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:
- *   rmutex - Recursive mutex to be initialized
+ *   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:
  *
  ****************************************************************************/
 
-int nxrmutex_init(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_lock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_wait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_destroy
+ * Name: nxmutex_trylock
  *
  * Description:
- *   This function destroy the UNNAMED recursive mutex.
+ *   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:
- *   rmutex - Recursive mutex to be destroyed
+ *   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.
  *
  ****************************************************************************/
 
-int nxrmutex_destroy(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_trylock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_trywait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_hold
+ * Name: nxmutex_unlock
  *
  * Description:
- *   This function check whether the calling thread hold the recursive mutex
- *   referenced by 'rmutex'.
+ *   This function attempts to unlock the mutex referenced by 'mutex'.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   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.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_hold(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_unlock(FAR mutex_t *mutex)
+{
+  return nxsem_post(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_recursive
+ * Name: nxmutex_reset
  *
  * Description:
- *   This function check whether the recursive mutex is currently held
- *   recursively. That is, whether it's locked more than once by the
- *   current holder.
- *   Note that this is inherently racy unless the calling thread is
- *   holding the mutex.
+ *   This function reset lock state.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
- *
- * Return Value:
- *  If rmutex has returned to True recursively, otherwise returns false.
+ *   mutex - mutex descriptor.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_recursive(FAR rmutex_t *rmutex);
+#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+static inline_function void nxmutex_reset(FAR mutex_t *mutex)
+{
+  nxsem_reset(&mutex->sem, 1);
+}
+#endif
 
 /****************************************************************************
- * Name: nxrmutex_get_holder
+ * Name: nxmutex_breaklock
  *
  * Description:
- *   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.
+ *   This function attempts to break the mutex
  *
  * Parameters:
- *   rmutex - Rmutex descriptor.
+ *   mutex   - Mutex descriptor.
+ *   locked  - Is the mutex break success
  *
  * 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:
  *
  ****************************************************************************/
 
-int nxrmutex_get_holder(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_breaklock(FAR mutex_t *mutex,
+                                    FAR unsigned int *locked)

Review Comment:
   align



##########
include/nuttx/mutex.h:
##########
@@ -354,407 +384,499 @@ 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_function 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_function bool nxmutex_is_locked(FAR mutex_t *mutex)
+{
+  return NXSEM_MACQUIRED(mutex->sem.val.mholder);
+}
 
 /****************************************************************************
- * Name: nxmutex_setprioceiling
+ * Name: nxmutex_destroy
  *
  * Description:
- *   This function attempts to set the priority ceiling of a mutex.
+ *   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        - mutex descriptor.
- *   prioceiling  - mutex priority ceiling value to set.
- *   old_ceiling  - location to return the mutex ceiling priority set before.
+ *   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
+ *   returned on success.  A negated errno value is returned on failure.
  *
  ****************************************************************************/
 
-int nxmutex_setprioceiling(FAR mutex_t *mutex, int prioceiling,
-                           FAR int *old_ceiling);
+static inline_function int nxmutex_destroy(FAR mutex_t *mutex)
+{
+  return nxsem_destroy(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_init
+ * Name: nxmutex_lock
  *
  * Description:
- *   This function initializes the UNNAMED recursive mutex. Following a
- *   successful call to nxrmutex_init(), the recursive mutex may be used in
- *   subsequent calls to nxrmutex_lock(), nxrmutex_unlock(),
- *   and nxrmutex_trylock(). The recursive mutex remains usable
- *   until it is destroyed.
+ *   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:
- *   rmutex - Recursive mutex to be initialized
+ *   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:
  *
  ****************************************************************************/
 
-int nxrmutex_init(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_lock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_wait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_destroy
+ * Name: nxmutex_trylock
  *
  * Description:
- *   This function destroy the UNNAMED recursive mutex.
+ *   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:
- *   rmutex - Recursive mutex to be destroyed
+ *   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.
  *
  ****************************************************************************/
 
-int nxrmutex_destroy(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_trylock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_trywait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_hold
+ * Name: nxmutex_unlock
  *
  * Description:
- *   This function check whether the calling thread hold the recursive mutex
- *   referenced by 'rmutex'.
+ *   This function attempts to unlock the mutex referenced by 'mutex'.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   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.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_hold(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_unlock(FAR mutex_t *mutex)
+{
+  return nxsem_post(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_recursive
+ * Name: nxmutex_reset
  *
  * Description:
- *   This function check whether the recursive mutex is currently held
- *   recursively. That is, whether it's locked more than once by the
- *   current holder.
- *   Note that this is inherently racy unless the calling thread is
- *   holding the mutex.
+ *   This function reset lock state.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
- *
- * Return Value:
- *  If rmutex has returned to True recursively, otherwise returns false.
+ *   mutex - mutex descriptor.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_recursive(FAR rmutex_t *rmutex);
+#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+static inline_function void nxmutex_reset(FAR mutex_t *mutex)
+{
+  nxsem_reset(&mutex->sem, 1);
+}
+#endif
 
 /****************************************************************************
- * Name: nxrmutex_get_holder
+ * Name: nxmutex_breaklock
  *
  * Description:
- *   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.
+ *   This function attempts to break the mutex
  *
  * Parameters:
- *   rmutex - Rmutex descriptor.
+ *   mutex   - Mutex descriptor.
+ *   locked  - Is the mutex break success
  *
  * 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:
  *
  ****************************************************************************/
 
-int nxrmutex_get_holder(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_breaklock(FAR mutex_t *mutex,
+                                    FAR unsigned int *locked)
+{
+  int ret = OK;
+
+  *locked = false;
+  ret = nxmutex_unlock(mutex);
+  if (ret >= 0)
+    {
+      *locked = true;
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_locked
+ * Name: nxmutex_restorelock
  *
  * Description:
- *   This function get the lock state the recursive mutex
- *   referenced by 'rmutex'.
- *   Note that this is inherently racy unless the calling thread is
- *   holding the mutex.
+ *   This function attempts to restore the mutex.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   mutex   - mutex descriptor.
+ *   locked  - true: it's mean that the mutex is broke success
  *
  * 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
  *
  ****************************************************************************/
 
-bool nxrmutex_is_locked(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_restorelock(FAR mutex_t *mutex,
+                                      unsigned int locked)

Review Comment:
   align



##########
include/nuttx/mutex.h:
##########
@@ -354,407 +384,499 @@ 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_function 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_function bool nxmutex_is_locked(FAR mutex_t *mutex)
+{
+  return NXSEM_MACQUIRED(mutex->sem.val.mholder);
+}
 
 /****************************************************************************
- * Name: nxmutex_setprioceiling
+ * Name: nxmutex_destroy
  *
  * Description:
- *   This function attempts to set the priority ceiling of a mutex.
+ *   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        - mutex descriptor.
- *   prioceiling  - mutex priority ceiling value to set.
- *   old_ceiling  - location to return the mutex ceiling priority set before.
+ *   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
+ *   returned on success.  A negated errno value is returned on failure.
  *
  ****************************************************************************/
 
-int nxmutex_setprioceiling(FAR mutex_t *mutex, int prioceiling,
-                           FAR int *old_ceiling);
+static inline_function int nxmutex_destroy(FAR mutex_t *mutex)
+{
+  return nxsem_destroy(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_init
+ * Name: nxmutex_lock
  *
  * Description:
- *   This function initializes the UNNAMED recursive mutex. Following a
- *   successful call to nxrmutex_init(), the recursive mutex may be used in
- *   subsequent calls to nxrmutex_lock(), nxrmutex_unlock(),
- *   and nxrmutex_trylock(). The recursive mutex remains usable
- *   until it is destroyed.
+ *   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:
- *   rmutex - Recursive mutex to be initialized
+ *   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:
  *
  ****************************************************************************/
 
-int nxrmutex_init(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_lock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_wait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_destroy
+ * Name: nxmutex_trylock
  *
  * Description:
- *   This function destroy the UNNAMED recursive mutex.
+ *   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:
- *   rmutex - Recursive mutex to be destroyed
+ *   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.
  *
  ****************************************************************************/
 
-int nxrmutex_destroy(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_trylock(FAR mutex_t *mutex)
+{
+  int ret;
+
+  ret = nxsem_trywait(&mutex->sem);
+  if (ret >= 0)
+    {
+      nxmutex_add_backtrace(mutex);
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_hold
+ * Name: nxmutex_unlock
  *
  * Description:
- *   This function check whether the calling thread hold the recursive mutex
- *   referenced by 'rmutex'.
+ *   This function attempts to unlock the mutex referenced by 'mutex'.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   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.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_hold(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_unlock(FAR mutex_t *mutex)
+{
+  return nxsem_post(&mutex->sem);
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_recursive
+ * Name: nxmutex_reset
  *
  * Description:
- *   This function check whether the recursive mutex is currently held
- *   recursively. That is, whether it's locked more than once by the
- *   current holder.
- *   Note that this is inherently racy unless the calling thread is
- *   holding the mutex.
+ *   This function reset lock state.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
- *
- * Return Value:
- *  If rmutex has returned to True recursively, otherwise returns false.
+ *   mutex - mutex descriptor.
  *
  ****************************************************************************/
 
-bool nxrmutex_is_recursive(FAR rmutex_t *rmutex);
+#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+static inline_function void nxmutex_reset(FAR mutex_t *mutex)
+{
+  nxsem_reset(&mutex->sem, 1);
+}
+#endif
 
 /****************************************************************************
- * Name: nxrmutex_get_holder
+ * Name: nxmutex_breaklock
  *
  * Description:
- *   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.
+ *   This function attempts to break the mutex
  *
  * Parameters:
- *   rmutex - Rmutex descriptor.
+ *   mutex   - Mutex descriptor.
+ *   locked  - Is the mutex break success
  *
  * 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:
  *
  ****************************************************************************/
 
-int nxrmutex_get_holder(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_breaklock(FAR mutex_t *mutex,
+                                    FAR unsigned int *locked)
+{
+  int ret = OK;
+
+  *locked = false;
+  ret = nxmutex_unlock(mutex);
+  if (ret >= 0)
+    {
+      *locked = true;
+    }
+
+  return ret;
+}
 
 /****************************************************************************
- * Name: nxrmutex_is_locked
+ * Name: nxmutex_restorelock
  *
  * Description:
- *   This function get the lock state the recursive mutex
- *   referenced by 'rmutex'.
- *   Note that this is inherently racy unless the calling thread is
- *   holding the mutex.
+ *   This function attempts to restore the mutex.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   mutex   - mutex descriptor.
+ *   locked  - true: it's mean that the mutex is broke success
  *
  * 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
  *
  ****************************************************************************/
 
-bool nxrmutex_is_locked(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_restorelock(FAR mutex_t *mutex,
+                                      unsigned int locked)
+{
+  return locked ? nxmutex_lock(mutex) : OK;
+}
 
 /****************************************************************************
- * Name: nrxmutex_lock
+ * Name: nxmutex_set_protocol
  *
  * Description:
- *   This function attempts to lock the recursive mutex referenced by
- *   'rmutex'.The recursive mutex can be locked multiple times in the same
- *   thread.
+ *   This function attempts to set the priority protocol of a mutex.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   mutex        - mutex descriptor.
+ *   protocol     - mutex protocol value to set.
  *
  * 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:
+ *   returned on success.  A negated errno value is returned on failure
  *
  ****************************************************************************/
 
-int nxrmutex_lock(FAR rmutex_t *rmutex);
+static inline_function int nxmutex_set_protocol(FAR mutex_t *mutex,
+                                                int protocol)
+{
+  return nxsem_set_protocol(&mutex->sem, protocol);
+}
 
 /****************************************************************************
- * Name: nxrmutex_trylock
+ * Name: nxmutex_getprioceiling
  *
  * Description:
- *   This function locks the recursive mutex if the recursive mutex is
- *   currently not locked or the same thread call.
- *   If the recursive mutex is locked and other thread call it,
- *   the call returns without blocking.
+ *   This function attempts to get the priority ceiling of a mutex.
  *
  * Parameters:
- *   rmutex - Recursive mutex descriptor.
+ *   mutex        - mutex descriptor.
+ *   prioceiling  - location to return the mutex priority ceiling.
  *
  * 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 recursive mutex
- *     -EAGAIN - The recursive mutex is not available.
+ *   returned on success.  A negated errno value is returned on failure
  *
  ****************************************************************************/
 
-int nxrmutex_trylock(FAR rmutex_t *rmutex);
+#ifdef CONFIG_PRIORITY_PROTECT
+static inline_function int nxmutex_getprioceiling(FAR const mutex_t *mutex,
+                                         FAR int *prioceiling)

Review Comment:
   align



-- 
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


Reply via email to