From 5e8037390c8cb6b2d592db3ba457c083249352f9 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 22 Jun 2017 06:26:00 -0600 Subject: [PATCH] Fix problems in last commit noted by Jeongchan Kim: last change returned -EINVAL vs EINVAL; Treat the case where the mutex is already unlocked just like the case where the mutex is held by another thread -- return EPERM. --- sched/pthread/pthread_mutexunlock.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sched/pthread/pthread_mutexunlock.c b/sched/pthread/pthread_mutexunlock.c index be73134883..3dd7d09683 100644 --- a/sched/pthread/pthread_mutexunlock.c +++ b/sched/pthread/pthread_mutexunlock.c @@ -53,7 +53,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: pthread_mutex_unlock + * Name: pthread_mutex_islocked * * Description: * Return true is the mutex is locked. @@ -114,13 +114,13 @@ static inline bool pthread_mutex_islocked(FAR struct pthread_mutex_s *mutex) int pthread_mutex_unlock(FAR pthread_mutex_t *mutex) { - int ret = OK; + int ret = EPERM; sinfo("mutex=0x%p\n", mutex); DEBUGASSERT(mutex != NULL); if (mutex == NULL) { - return -EINVAL; + return EINVAL; } /* Make sure the semaphore is stable while we make the following checks. @@ -130,8 +130,10 @@ int pthread_mutex_unlock(FAR pthread_mutex_t *mutex) sched_lock(); /* The unlock operation is only performed if the mutex is actually locked. - * If the mutex is not locked, then SUCCESS will be returned (there is - * no error return value specified for this case). + * EPERM *must* be returned if the mutex type is PTHREAD_MUTEX_ERRORCHECK + * or PTHREAD_MUTEX_RECURSIVE, or the mutex is a robust mutex, and the + * current thread does not own the mutex. Behavior is undefined for the + * remaining case. */ if (pthread_mutex_islocked(mutex))