From 948332ca343f596acf7af0b434812a2bb7f90079 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Apr 2017 09:51:03 -0600 Subject: [PATCH] pthreads: Backed most of last pthread changes. Found the 'real' root poblem. A one like error in pthread_mutex.c. --- libc/pthread/pthread_rwlock_rdlock.c | 13 +------------ libc/pthread/pthread_rwlock_wrlock.c | 14 +------------- sched/pthread/pthread_cancel.c | 12 ++++++------ sched/pthread/pthread_exit.c | 12 ++++++------ sched/pthread/pthread_mutex.c | 6 ++++-- 5 files changed, 18 insertions(+), 39 deletions(-) diff --git a/libc/pthread/pthread_rwlock_rdlock.c b/libc/pthread/pthread_rwlock_rdlock.c index 29dcf7daae1..4eadfedccb5 100644 --- a/libc/pthread/pthread_rwlock_rdlock.c +++ b/libc/pthread/pthread_rwlock_rdlock.c @@ -55,18 +55,7 @@ static void rdlock_cleanup(FAR void *arg) { FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg; -#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE - /* Check if this is a robust mutex in an inconsistent state */ - - if ((rw_lock->lock.flags & _PTHREAD_MFLAGS_INCONSISTENT) != 0) - { - (void)pthread_mutex_consistent(&rw_lock->lock); - } - else -#endif - { - (void)pthread_mutex_unlock(&rw_lock->lock); - } + (void)pthread_mutex_unlock(&rw_lock->lock); } #endif diff --git a/libc/pthread/pthread_rwlock_wrlock.c b/libc/pthread/pthread_rwlock_wrlock.c index 93a6a238582..c9f4f3a7c56 100644 --- a/libc/pthread/pthread_rwlock_wrlock.c +++ b/libc/pthread/pthread_rwlock_wrlock.c @@ -56,19 +56,7 @@ static void wrlock_cleanup(FAR void *arg) FAR pthread_rwlock_t *rw_lock = (FAR pthread_rwlock_t *)arg; rw_lock->num_writers--; - -#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE - /* Check if this is a robust mutex in an inconsistent state */ - - if ((rw_lock->lock.flags & _PTHREAD_MFLAGS_INCONSISTENT) != 0) - { - (void)pthread_mutex_consistent(&rw_lock->lock); - } - else -#endif - { - (void)pthread_mutex_unlock(&rw_lock->lock); - } + (void)pthread_mutex_unlock(&rw_lock->lock); } #endif diff --git a/sched/pthread/pthread_cancel.c b/sched/pthread/pthread_cancel.c index f68d272e03a..e2cad60ebbf 100644 --- a/sched/pthread/pthread_cancel.c +++ b/sched/pthread/pthread_cancel.c @@ -145,12 +145,6 @@ int pthread_cancel(pthread_t thread) pthread_exit(PTHREAD_CANCELED); } -#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE - /* Recover any mutexes still held by the canceled thread */ - - pthread_mutex_inconsistent(tcb); -#endif - #ifdef CONFIG_PTHREAD_CLEANUP /* Perform any stack pthread clean-up callbacks. * @@ -168,6 +162,12 @@ int pthread_cancel(pthread_t thread) (void)pthread_completejoin((pid_t)thread, PTHREAD_CANCELED); +#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE + /* Recover any mutexes still held by the canceled thread */ + + pthread_mutex_inconsistent(tcb); +#endif + /* Then let task_terminate do the real work */ return task_terminate((pid_t)thread, false); diff --git a/sched/pthread/pthread_exit.c b/sched/pthread/pthread_exit.c index 4929db94cb5..bcc8e79c855 100644 --- a/sched/pthread/pthread_exit.c +++ b/sched/pthread/pthread_exit.c @@ -105,12 +105,6 @@ void pthread_exit(FAR void *exit_value) tcb->cpcount = 0; #endif -#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE - /* Recover any mutexes still held by the canceled thread */ - - pthread_mutex_inconsistent((FAR struct pthread_tcb_s *)tcb); -#endif - #ifdef CONFIG_PTHREAD_CLEANUP /* Perform any stack pthread clean-up callbacks */ @@ -129,6 +123,12 @@ void pthread_exit(FAR void *exit_value) exit(EXIT_FAILURE); } +#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE + /* Recover any mutexes still held by the canceled thread */ + + pthread_mutex_inconsistent((FAR struct pthread_tcb_s *)tcb); +#endif + /* Perform common task termination logic. This will get called again later * through logic kicked off by _exit(). However, we need to call it before * calling _exit() in order certain operations if this is the last thread diff --git a/sched/pthread/pthread_mutex.c b/sched/pthread/pthread_mutex.c index f11a5b01f6f..afd296407ef 100644 --- a/sched/pthread/pthread_mutex.c +++ b/sched/pthread/pthread_mutex.c @@ -125,10 +125,12 @@ int pthread_mutex_take(FAR struct pthread_mutex_s *mutex, bool intr) } else { - /* Take semaphore underlying the mutex */ + /* Take semaphore underlying the mutex. pthread_takesemaphore + * returns zero on success and a positive errno value on failue. + */ ret = pthread_takesemaphore(&mutex->sem, intr); - if (ret < OK) + if (ret != OK) { ret = get_errno(); }