mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 08:36:24 +08:00
pthreads: Add more robustness characteristics: pthread_mutex_lock() and trylock() will now return EOWNERDEAD if the mutex is locked by a thread that no longer exists. Add pthread_mutex_consistent() to recover from this situation.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
* sched/pthread/pthread_mutextrylock.c
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007-2009, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -43,6 +43,7 @@
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sched.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
@@ -83,15 +84,13 @@
|
||||
|
||||
int pthread_mutex_trylock(FAR pthread_mutex_t *mutex)
|
||||
{
|
||||
int ret = OK;
|
||||
int status;
|
||||
int ret = EINVAL;
|
||||
|
||||
sinfo("mutex=0x%p\n", mutex);
|
||||
DEBUGASSERT(mutex != NULL);
|
||||
|
||||
if (!mutex)
|
||||
{
|
||||
ret = EINVAL;
|
||||
}
|
||||
else
|
||||
if (mutex != NULL)
|
||||
{
|
||||
int mypid = (int)getpid();
|
||||
|
||||
@@ -103,7 +102,8 @@ int pthread_mutex_trylock(FAR pthread_mutex_t *mutex)
|
||||
|
||||
/* Try to get the semaphore. */
|
||||
|
||||
if (sem_trywait((FAR sem_t *)&mutex->sem) == OK)
|
||||
status = sem_trywait((FAR sem_t *)&mutex->sem);
|
||||
if (status == OK)
|
||||
{
|
||||
/* If we successfully obtained the semaphore, then indicate
|
||||
* that we own it.
|
||||
@@ -117,33 +117,63 @@ int pthread_mutex_trylock(FAR pthread_mutex_t *mutex)
|
||||
mutex->nlocks = 1;
|
||||
}
|
||||
#endif
|
||||
ret = OK;
|
||||
}
|
||||
|
||||
/* Was it not available? */
|
||||
/* sem_trywait failed */
|
||||
|
||||
else if (get_errno() == EAGAIN)
|
||||
{
|
||||
#ifdef CONFIG_MUTEX_TYPES
|
||||
|
||||
/* Check if recursive mutex was locked by ourself. */
|
||||
|
||||
if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->pid == mypid)
|
||||
{
|
||||
/* Increment the number of locks held and return successfully. */
|
||||
|
||||
mutex->nlocks++;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = EBUSY;
|
||||
}
|
||||
#else
|
||||
ret = EBUSY;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = EINVAL;
|
||||
/* Did it fail because the semaphore was not avaialabl? */
|
||||
|
||||
int errcode = get_errno();
|
||||
if (errcode == EAGAIN)
|
||||
{
|
||||
#ifdef CONFIG_MUTEX_TYPES
|
||||
/* Check if recursive mutex was locked by the calling thread. */
|
||||
|
||||
if (mutex->type == PTHREAD_MUTEX_RECURSIVE && mutex->pid == mypid)
|
||||
{
|
||||
/* Increment the number of locks held and return successfully. */
|
||||
|
||||
mutex->nlocks++;
|
||||
ret = OK;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
/* The calling thread does not hold the semaphore. The correct
|
||||
* behavior for the 'robust' mutex is to verify that the holder of
|
||||
* the mutex is still valid. This is protection from the case
|
||||
* where the holder of the mutex has exitted without unlocking it.
|
||||
*/
|
||||
|
||||
if (mutex->pid > 0 && sched_gettcb(mutex->pid) == NULL)
|
||||
{
|
||||
DEBUGASSERT(mutex->pid != 0); /* < 0: available, >0 owned, ==0 error */
|
||||
|
||||
/* A thread holds the mutex, but there is no such thread.
|
||||
* POSIX requires that the 'robust' mutex return EOWNERDEAD
|
||||
* in this case. It is then the caller's responsibility to
|
||||
* call pthread_mutx_consistent() fo fix the mutex.
|
||||
*/
|
||||
|
||||
ret = EOWNERDEAD;
|
||||
}
|
||||
|
||||
/* The mutex is locked by another, active thread */
|
||||
|
||||
else
|
||||
{
|
||||
ret = EBUSY;
|
||||
}
|
||||
}
|
||||
|
||||
/* Some other, unhandled error occurred */
|
||||
|
||||
else
|
||||
{
|
||||
ret = errcode;
|
||||
}
|
||||
}
|
||||
|
||||
sched_unlock();
|
||||
|
||||
Reference in New Issue
Block a user