pkarashchenko commented on code in PR #8645:
URL: https://github.com/apache/nuttx/pull/8645#discussion_r1133730229
##########
include/nuttx/mutex.h:
##########
@@ -126,11 +130,28 @@ int nxmutex_destroy(FAR mutex_t *mutex);
* mutex - mutex descriptor.
*
* Return Value:
+ * Return true if mutex is hold by current thread.
*
****************************************************************************/
bool nxmutex_is_hold(FAR mutex_t *mutex);
+/****************************************************************************
+ * Name: nxmutex_is_reset
+ *
+ * Description:
+ * This function check whether the mutex is be reset
+ *
+ * Parameters:
+ * mutex - mutex descriptor.
+ *
+ * Return Value:
+ * Return true if mutex is be reset.
+ *
+ ****************************************************************************/
+
+#define nxmutex_is_reset(mutex) ((mutex)->holder == NXMUTEX_RESET)
Review Comment:
Optional
```suggestion
#define nxmutex_is_reset(mutex) (NXMUTEX_HOLDER(mutex) == NXMUTEX_RESET)
```
##########
include/nuttx/mutex.h:
##########
@@ -438,6 +529,34 @@ int nxrmutex_trylock(FAR rmutex_t *rmutex);
int nxrmutex_timedlock(FAR rmutex_t *rmutex, unsigned int timeout);
+/****************************************************************************
+ * Name: nxrmutex_clocklock
+ *
+ * Description:
+ * This function attempts to lock the mutex . If the mutex value
Review Comment:
```suggestion
* This function attempts to lock the mutex. If the mutex value
```
##########
sched/mutex/mutex_timedlock.c:
##########
@@ -0,0 +1,75 @@
+/****************************************************************************
+ * sched/mutex/mutex_timedlock.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/sched.h>
+#include <nuttx/clock.h>
+#include <nuttx/mutex.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxmutex_timedlock
+ *
+ * Description:
+ * This function attempts to lock the mutex . If the mutex value
+ * is (<=) zero,then the calling task will not return until it
+ * successfully acquires the lock or timed out
+ *
+ * Input Parameters:
+ * mutex - Mutex object
+ * timeout - The time when mutex lock timed out
+ *
+ * Returned Value:
+ * OK The mutex successfully acquires
+ * EINVAL The mutex argument does not refer to a valid mutex. Or the
+ * thread would have blocked, and the abstime parameter specified
+ * a nanoseconds field value less than zero or greater than or
+ * equal to 1000 million.
+ * ETIMEDOUT The mutex could not be locked before the specified timeout
+ * expired.
+ * EDEADLK A deadlock condition was detected.
+ *
+ ****************************************************************************/
+
+int nxmutex_timedlock(FAR mutex_t *mutex, unsigned int timeout)
+{
+ int ret;
+
+ /* Wait until we get the lock or until the timeout expires */
+
+ do
+ {
+ ret = nxsem_tickwait(&mutex->sem, MSEC2TICK(timeout));
+ }
+ while (ret == -EINTR || ret == -ECANCELED);
Review Comment:
ditto for `-ECANCELED`
##########
sched/mutex/mutex_lock.c:
##########
@@ -0,0 +1,77 @@
+/****************************************************************************
+ * sched/mutex/mutex_lock.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/sched.h>
+#include <nuttx/mutex.h>
+
+#include <assert.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * 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:
+ *
+ ****************************************************************************/
+
+int nxmutex_lock(FAR mutex_t *mutex)
+{
+ int ret;
+
+ DEBUGASSERT(!nxmutex_is_hold(mutex));
+ for (; ; )
+ {
+ /* Take the semaphore (perhaps waiting) */
+
+ ret = nxsem_wait(&mutex->sem);
+ if (ret >= 0)
+ {
+ mutex->holder = nxsched_gettid();
+ break;
+ }
+
+ if (ret != -EINTR && ret != -ECANCELED)
Review Comment:
I'm not sure how the caller should deal with `-ECANCELED`. Un libc
implementation the mutex was remapped either to `nxsem_` ro to `sem_` API, so
in user space the `sem_` API was used so `enter_cancellation_point()` and
`leave_cancellation_point()` was applied. Now the path of `ECANCELED` is not
clear to me
##########
sched/mutex/mutex_trylock.c:
##########
@@ -0,0 +1,69 @@
+/****************************************************************************
+ * sched/mutex/mutex_trylock.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/sched.h>
+#include <nuttx/mutex.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+int nxmutex_trylock(FAR mutex_t *mutex)
+{
+ int ret;
+
+ if (nxmutex_is_hold(mutex))
+ {
+ return -EAGAIN;
Review Comment:
Shouldn't `-EDEADLK` be a better candidate?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]