From 7530ec7ac6d82d7ac13d17ab3dbef898acf8a917 Mon Sep 17 00:00:00 2001 From: Joseph Schuchart Date: Wed, 10 Jul 2024 09:17:07 -0400 Subject: [PATCH] Add feature test macro to enable pthread_mutexattr_settype() Signed-off-by: Joseph Schuchart --- .../pthreads/threads_pthreads_module.c | 40 +++++++++++++++++++ .../threads/pthreads/threads_pthreads_mutex.h | 39 +++--------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/opal/mca/threads/pthreads/threads_pthreads_module.c b/opal/mca/threads/pthreads/threads_pthreads_module.c index ac09b71d53d..bcbd1fded13 100644 --- a/opal/mca/threads/pthreads/threads_pthreads_module.c +++ b/opal/mca/threads/pthreads/threads_pthreads_module.c @@ -23,7 +23,11 @@ * $HEADER$ */ +/* needed for pthread_mutexattr_settype */ +#define _GNU_SOURCE + #include +#include #include "opal/constants.h" #include "opal/mca/threads/threads.h" @@ -38,3 +42,39 @@ int opal_tsd_key_create(opal_tsd_key_t *key, opal_tsd_destructor_t destructor) rc = pthread_key_create(key, destructor); return 0 == rc ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO; } + + +int opal_thread_internal_mutex_init_recursive(opal_thread_internal_mutex_t *p_mutex) +{ + int ret; +#if OPAL_ENABLE_DEBUG + pthread_mutexattr_t mutex_attr; + ret = pthread_mutexattr_init(&mutex_attr); + if (0 != ret) + return OPAL_ERR_IN_ERRNO; + ret = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); + if (0 != ret) { + ret = pthread_mutexattr_destroy(&mutex_attr); + assert(0 == ret); + return OPAL_ERR_IN_ERRNO; + } + ret = pthread_mutex_init(p_mutex, &mutex_attr); + if (0 != ret) { + ret = pthread_mutexattr_destroy(&mutex_attr); + assert(0 == ret); + return OPAL_ERR_IN_ERRNO; + } + ret = pthread_mutexattr_destroy(&mutex_attr); + assert(0 == ret); +#else + pthread_mutexattr_t mutex_attr; + ret = pthread_mutexattr_init(&mutex_attr); + if (0 != ret) { + return OPAL_ERR_IN_ERRNO; + } + pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); + ret = pthread_mutex_init(p_mutex, &mutex_attr); + pthread_mutexattr_destroy(&mutex_attr); +#endif + return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO; +} diff --git a/opal/mca/threads/pthreads/threads_pthreads_mutex.h b/opal/mca/threads/pthreads/threads_pthreads_mutex.h index 0207681ee9f..ee9dda81f18 100644 --- a/opal/mca/threads/pthreads/threads_pthreads_mutex.h +++ b/opal/mca/threads/pthreads/threads_pthreads_mutex.h @@ -61,48 +61,19 @@ typedef pthread_mutex_t opal_thread_internal_mutex_t; # define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER #endif + +int opal_thread_internal_mutex_init_recursive(opal_thread_internal_mutex_t *p_mutex); + static inline int opal_thread_internal_mutex_init(opal_thread_internal_mutex_t *p_mutex, bool recursive) { int ret; -#if OPAL_ENABLE_DEBUG - if (recursive) { - pthread_mutexattr_t mutex_attr; - ret = pthread_mutexattr_init(&mutex_attr); - if (0 != ret) - return OPAL_ERR_IN_ERRNO; - ret = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); - if (0 != ret) { - ret = pthread_mutexattr_destroy(&mutex_attr); - assert(0 == ret); - return OPAL_ERR_IN_ERRNO; - } - ret = pthread_mutex_init(p_mutex, &mutex_attr); - if (0 != ret) { - ret = pthread_mutexattr_destroy(&mutex_attr); - assert(0 == ret); - return OPAL_ERR_IN_ERRNO; - } - ret = pthread_mutexattr_destroy(&mutex_attr); - assert(0 == ret); - } else { - ret = pthread_mutex_init(p_mutex, NULL); - } -#else if (recursive) { - pthread_mutexattr_t mutex_attr; - ret = pthread_mutexattr_init(&mutex_attr); - if (0 != ret) { - return OPAL_ERR_IN_ERRNO; - } - pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); - ret = pthread_mutex_init(p_mutex, &mutex_attr); - pthread_mutexattr_destroy(&mutex_attr); + return opal_thread_internal_mutex_init_recursive(p_mutex); } else { ret = pthread_mutex_init(p_mutex, NULL); + return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO; } -#endif - return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO; } static inline void opal_thread_internal_mutex_lock(opal_thread_internal_mutex_t *p_mutex)