2017-03-26 23:46:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/pthread/pthread_mutex.c
|
|
|
|
*
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
* intr - false: ignore EINTR errors when locking; true treat EINTR as
|
2017-03-26 23:46:19 +02:00
|
|
|
* other errors by returning the errno value
|
|
|
|
*
|
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,
|
|
|
|
FAR const struct timespec *abs_timeout, bool intr)
|
2017-03-26 23:46:19 +02:00
|
|
|
{
|
|
|
|
int ret = EINVAL;
|
|
|
|
|
|
|
|
/* Verify input parameters */
|
|
|
|
|
|
|
|
DEBUGASSERT(mutex != NULL);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-29 15:05:06 +02:00
|
|
|
/* Take semaphore underlying the mutex. pthread_sem_take
|
2017-04-11 19:03:25 +02:00
|
|
|
* returns zero on success and a positive errno value on failure.
|
2017-04-10 17:51:03 +02:00
|
|
|
*/
|
2017-03-26 23:46:19 +02:00
|
|
|
|
2022-12-07 11:47:08 +01:00
|
|
|
ret = pthread_sem_take(&mutex->sem, 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)
|
|
|
|
{
|
|
|
|
ret = EOWNERDEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the mutex to the list of mutexes held by this task */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to take the semaphore underlying the mutex */
|
|
|
|
|
2017-10-05 15:59:06 +02:00
|
|
|
ret = nxsem_trywait(&mutex->sem);
|
|
|
|
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
|
|
|
}
|
2017-03-26 23:46:19 +02:00
|
|
|
else
|
|
|
|
{
|
2017-03-29 15:50:40 +02:00
|
|
|
/* Add the mutex to the list of mutexes held by this task */
|
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 */
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-03-26 23:46:19 +02:00
|
|
|
/* Now release the underlying semaphore */
|
|
|
|
|
2017-05-29 15:05:06 +02:00
|
|
|
ret = pthread_sem_give(&mutex->sem);
|
2017-03-26 23:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|