2017-03-26 23:46:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/pthread/pthread_mutex.c
|
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-04-11 22:23:39 +02:00
|
|
|
* 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
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
2020-04-11 22:23:39 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
2020-04-11 22:23:39 +02:00
|
|
|
* 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.
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/sched.h>
|
2017-10-05 15:59:06 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-03-26 23:46:19 +02:00
|
|
|
|
|
|
|
#include "sched/sched.h"
|
|
|
|
#include "pthread/pthread.h"
|
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_add
|
|
|
|
*
|
|
|
|
* Description:
|
2017-05-31 18:55:37 +02:00
|
|
|
* Add the mutex to the list of mutexes held by this pthread.
|
2017-03-29 15:50:40 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-04 23:22:27 +02:00
|
|
|
* mutex - The mutex to be locked
|
2017-03-29 15:50:40 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-03-29 15:50:40 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pthread_mutex_add(FAR struct pthread_mutex_s *mutex)
|
|
|
|
{
|
2017-05-31 18:55:37 +02:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2020-06-03 20:38:39 +02:00
|
|
|
irqstate_t flags;
|
2017-03-29 15:50:40 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(mutex->flink == NULL);
|
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
/* Add the mutex to the list of mutexes held by this pthread */
|
2017-05-31 18:55:37 +02:00
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
flags = enter_critical_section();
|
|
|
|
mutex->flink = rtcb->mhead;
|
|
|
|
rtcb->mhead = mutex;
|
|
|
|
leave_critical_section(flags);
|
2017-05-31 18:55:37 +02:00
|
|
|
}
|
|
|
|
|
2024-03-12 07:52:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_check
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Verify that the mutex is not in the list of mutexes held by
|
|
|
|
* this pthread.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mutex - The mutex to be locked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
|
|
|
static void pthread_mutex_check(FAR struct pthread_mutex_s *mutex)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *tcb = this_task();
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
FAR struct pthread_mutex_s *cur;
|
|
|
|
|
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
for (cur = tcb->mhead; cur != NULL; cur = cur->flink)
|
|
|
|
{
|
|
|
|
/* The mutex should not be in the list of mutexes held by this task */
|
|
|
|
|
|
|
|
DEBUGASSERT(cur != mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-05-31 18:55:37 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_remove
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove the mutex to the list of mutexes held by this pthread.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-04 23:22:27 +02:00
|
|
|
* mutex - The mutex to be locked
|
2017-05-31 18:55:37 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-05-31 18:55:37 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void pthread_mutex_remove(FAR struct pthread_mutex_s *mutex)
|
|
|
|
{
|
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2020-06-03 20:38:39 +02:00
|
|
|
FAR struct pthread_mutex_s *curr;
|
|
|
|
FAR struct pthread_mutex_s *prev;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Remove the mutex from the list of mutexes held by this task */
|
2017-05-31 18:55:37 +02:00
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
for (prev = NULL, curr = rtcb->mhead;
|
|
|
|
curr != NULL && curr != mutex;
|
|
|
|
prev = curr, curr = curr->flink)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT(curr == mutex);
|
|
|
|
|
|
|
|
/* Remove the mutex from the list. prev == NULL means that the mutex
|
|
|
|
* to be removed is at the head of the list.
|
2017-05-31 18:55:37 +02:00
|
|
|
*/
|
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
if (prev == NULL)
|
2017-05-31 18:55:37 +02:00
|
|
|
{
|
2020-06-03 20:38:39 +02:00
|
|
|
rtcb->mhead = mutex->flink;
|
2017-05-31 18:55:37 +02:00
|
|
|
}
|
2020-06-03 20:38:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->flink = mutex->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex->flink = NULL;
|
|
|
|
leave_critical_section(flags);
|
2017-03-29 15:50:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-26 23:46:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_take
|
|
|
|
*
|
|
|
|
* Description:
|
2017-03-29 15:50:40 +02:00
|
|
|
* Take the pthread_mutex, waiting if necessary. If successful, add the
|
|
|
|
* mutex to the list of mutexes held by this thread.
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-04-11 19:03:25 +02:00
|
|
|
* mutex - The mutex to be locked
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-03-26 23:46:19 +02:00
|
|
|
* 0 on success or an errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-24 21:40:11 +01:00
|
|
|
int pthread_mutex_take(FAR struct pthread_mutex_s *mutex,
|
2022-12-07 11:47:24 +01:00
|
|
|
FAR const struct timespec *abs_timeout)
|
2017-03-26 23:46:19 +02:00
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
if (mutex != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that no unexpected context switches occur */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
/* Error out if the mutex is already in an inconsistent state. */
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
if ((mutex->flags & _PTHREAD_MFLAGS_INCONSISTENT) != 0)
|
2017-03-26 23:46:19 +02:00
|
|
|
{
|
2017-03-29 15:50:40 +02:00
|
|
|
ret = EOWNERDEAD;
|
|
|
|
}
|
2024-07-04 10:59:57 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
|
|
|
|
else if (mutex_is_hold(&mutex->mutex) &&
|
|
|
|
mutex->type != PTHREAD_MUTEX_RECURSIVE)
|
|
|
|
{
|
|
|
|
ret = EDEADLK;
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-29 15:50:40 +02:00
|
|
|
else
|
|
|
|
{
|
2024-07-04 10:59:57 +02:00
|
|
|
/* mutex_clocklock returns zero when successful, and the negative
|
|
|
|
* errno value is returned when failed.
|
2017-04-10 17:51:03 +02:00
|
|
|
*/
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
ret = -mutex_clocklock(&mutex->mutex, abs_timeout);
|
2017-04-11 19:03:25 +02:00
|
|
|
if (ret == OK)
|
2017-03-29 15:50:40 +02:00
|
|
|
{
|
|
|
|
/* Check if the holder of the mutex has terminated without
|
|
|
|
* releasing. In that case, the state of the mutex is
|
|
|
|
* inconsistent and we return EOWNERDEAD.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((mutex->flags & _PTHREAD_MFLAGS_INCONSISTENT) != 0)
|
|
|
|
{
|
2024-07-04 10:59:57 +02:00
|
|
|
/* If the holder thread has terminated, we need to reset
|
|
|
|
* the mutex and return an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mutex_reset(&mutex->mutex);
|
2017-03-29 15:50:40 +02:00
|
|
|
ret = EOWNERDEAD;
|
|
|
|
}
|
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
/* If mutex is recursion, it is already in the linked list,
|
|
|
|
* and we should not add it to the link list again.
|
|
|
|
*/
|
2017-03-29 15:50:40 +02:00
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
else if (!mutex_is_recursive(&mutex->mutex))
|
2017-03-29 15:50:40 +02:00
|
|
|
{
|
2024-03-12 07:52:53 +01:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
|
|
|
pthread_mutex_check(mutex);
|
|
|
|
#endif
|
2017-03-29 15:50:40 +02:00
|
|
|
pthread_mutex_add(mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_trytake
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Try to take the pthread_mutex without waiting. If successful, add the
|
|
|
|
* mutex to the list of mutexes held by this thread.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-04-11 19:03:25 +02:00
|
|
|
* mutex - The mutex to be locked
|
|
|
|
* intr - false: ignore EINTR errors when locking; true treat EINTR as
|
2017-03-29 15:50:40 +02:00
|
|
|
* other errors by returning the errno value
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-03-29 15:50:40 +02:00
|
|
|
* 0 on success or an errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pthread_mutex_trytake(FAR struct pthread_mutex_s *mutex)
|
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
if (mutex != NULL)
|
|
|
|
{
|
|
|
|
/* Make sure that no unexpected context switches occur */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
|
|
|
/* Error out if the mutex is already in an inconsistent state. */
|
|
|
|
|
|
|
|
if ((mutex->flags & _PTHREAD_MFLAGS_INCONSISTENT) != 0)
|
|
|
|
{
|
|
|
|
ret = EOWNERDEAD;
|
|
|
|
}
|
2024-07-04 10:59:57 +02:00
|
|
|
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
|
|
|
|
else if (mutex_is_hold(&mutex->mutex) &&
|
|
|
|
mutex->type != PTHREAD_MUTEX_RECURSIVE)
|
|
|
|
{
|
|
|
|
ret = EBUSY;
|
|
|
|
}
|
|
|
|
#endif
|
2017-03-29 15:50:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to take the semaphore underlying the mutex */
|
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
ret = mutex_trylock(&mutex->mutex);
|
2017-10-05 15:59:06 +02:00
|
|
|
if (ret < 0)
|
2017-03-29 15:50:40 +02:00
|
|
|
{
|
2017-10-05 15:59:06 +02:00
|
|
|
ret = -ret;
|
2017-03-29 15:50:40 +02:00
|
|
|
}
|
2024-07-04 10:59:57 +02:00
|
|
|
else if (!mutex_is_recursive(&mutex->mutex))
|
2017-03-26 23:46:19 +02:00
|
|
|
{
|
2024-07-04 10:59:57 +02:00
|
|
|
/* If we successfully acquire the mutex, and we didn't get
|
|
|
|
* it before, add the mutex to the linked list.
|
|
|
|
*/
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2017-03-29 15:50:40 +02:00
|
|
|
pthread_mutex_add(mutex);
|
2017-03-26 23:46:19 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-29 15:50:40 +02:00
|
|
|
|
|
|
|
sched_unlock();
|
2017-03-26 23:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: pthread_mutex_give
|
|
|
|
*
|
|
|
|
* Description:
|
2020-02-23 09:50:23 +01:00
|
|
|
* Take the pthread_mutex and, if successful, add the mutex to the list of
|
2017-03-26 23:46:19 +02:00
|
|
|
* mutexes held by this thread.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-04 23:22:27 +02:00
|
|
|
* mutex - The mutex to be unlocked
|
2017-03-26 23:46:19 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-03-26 23:46:19 +02:00
|
|
|
* 0 on success or an errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int pthread_mutex_give(FAR struct pthread_mutex_s *mutex)
|
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
if (mutex != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the mutex from the list of mutexes held by this task */
|
|
|
|
|
|
|
|
if (!mutex_is_recursive(&mutex->mutex))
|
|
|
|
{
|
|
|
|
pthread_mutex_remove(mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now release the underlying mutex */
|
|
|
|
|
|
|
|
ret = -mutex_unlock(&mutex->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_breaklock(FAR struct pthread_mutex_s *mutex,
|
|
|
|
FAR unsigned int *breakval)
|
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
2017-03-26 23:46:19 +02:00
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
if (mutex != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the mutex from the list of mutexes held by this task */
|
|
|
|
|
2017-05-31 18:55:37 +02:00
|
|
|
pthread_mutex_remove(mutex);
|
2017-03-29 15:50:40 +02:00
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
/* Now release the underlying mutex */
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2024-07-04 10:59:57 +02:00
|
|
|
ret = -mutex_breaklock(&mutex->mutex, breakval);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_restorelock(FAR struct pthread_mutex_s *mutex,
|
|
|
|
unsigned int breakval)
|
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
if (mutex != NULL)
|
|
|
|
{
|
|
|
|
ret = -mutex_restorelock(&mutex->mutex, breakval);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Add the mutex to the list of mutexes held by this task */
|
|
|
|
|
|
|
|
pthread_mutex_add(mutex);
|
|
|
|
}
|
2017-03-26 23:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|