Skip to content

Commit 224c719

Browse files
committed
Add feature test macro to enable pthread_mutexattr_settype()
Signed-off-by: Joseph Schuchart <[email protected]>
1 parent 571c1e7 commit 224c719

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

opal/mca/threads/pthreads/threads_pthreads_module.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
* $HEADER$
2424
*/
2525

26+
/* needed for pthread_mutexattr_settype */
27+
#define _GNU_SOURCE
28+
2629
#include <unistd.h>
30+
#include <pthread.h>
2731

2832
#include "opal/constants.h"
2933
#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)
3842
rc = pthread_key_create(key, destructor);
3943
return 0 == rc ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
4044
}
45+
46+
47+
int opal_thread_internal_mutex_init_recursive(opal_thread_internal_mutex_t *p_mutex)
48+
{
49+
int ret;
50+
#if OPAL_ENABLE_DEBUG
51+
pthread_mutexattr_t mutex_attr;
52+
ret = pthread_mutexattr_init(&mutex_attr);
53+
if (0 != ret)
54+
return OPAL_ERR_IN_ERRNO;
55+
ret = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
56+
if (0 != ret) {
57+
ret = pthread_mutexattr_destroy(&mutex_attr);
58+
assert(0 == ret);
59+
return OPAL_ERR_IN_ERRNO;
60+
}
61+
ret = pthread_mutex_init(p_mutex, &mutex_attr);
62+
if (0 != ret) {
63+
ret = pthread_mutexattr_destroy(&mutex_attr);
64+
assert(0 == ret);
65+
return OPAL_ERR_IN_ERRNO;
66+
}
67+
ret = pthread_mutexattr_destroy(&mutex_attr);
68+
assert(0 == ret);
69+
#else
70+
pthread_mutexattr_t mutex_attr;
71+
ret = pthread_mutexattr_init(&mutex_attr);
72+
if (0 != ret) {
73+
return OPAL_ERR_IN_ERRNO;
74+
}
75+
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
76+
ret = pthread_mutex_init(p_mutex, &mutex_attr);
77+
pthread_mutexattr_destroy(&mutex_attr);
78+
#endif
79+
return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
80+
}

opal/mca/threads/pthreads/threads_pthreads_mutex.h

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,48 +61,19 @@ typedef pthread_mutex_t opal_thread_internal_mutex_t;
6161
# define OPAL_THREAD_INTERNAL_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER
6262
#endif
6363

64+
65+
int opal_thread_internal_mutex_init_recursive(opal_thread_internal_mutex_t *p_mutex);
66+
6467
static inline int opal_thread_internal_mutex_init(opal_thread_internal_mutex_t *p_mutex,
6568
bool recursive)
6669
{
6770
int ret;
68-
#if OPAL_ENABLE_DEBUG
69-
if (recursive) {
70-
pthread_mutexattr_t mutex_attr;
71-
ret = pthread_mutexattr_init(&mutex_attr);
72-
if (0 != ret)
73-
return OPAL_ERR_IN_ERRNO;
74-
ret = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
75-
if (0 != ret) {
76-
ret = pthread_mutexattr_destroy(&mutex_attr);
77-
assert(0 == ret);
78-
return OPAL_ERR_IN_ERRNO;
79-
}
80-
ret = pthread_mutex_init(p_mutex, &mutex_attr);
81-
if (0 != ret) {
82-
ret = pthread_mutexattr_destroy(&mutex_attr);
83-
assert(0 == ret);
84-
return OPAL_ERR_IN_ERRNO;
85-
}
86-
ret = pthread_mutexattr_destroy(&mutex_attr);
87-
assert(0 == ret);
88-
} else {
89-
ret = pthread_mutex_init(p_mutex, NULL);
90-
}
91-
#else
9271
if (recursive) {
93-
pthread_mutexattr_t mutex_attr;
94-
ret = pthread_mutexattr_init(&mutex_attr);
95-
if (0 != ret) {
96-
return OPAL_ERR_IN_ERRNO;
97-
}
98-
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
99-
ret = pthread_mutex_init(p_mutex, &mutex_attr);
100-
pthread_mutexattr_destroy(&mutex_attr);
72+
return opal_thread_internal_mutex_init_recursive(p_mutex);
10173
} else {
10274
ret = pthread_mutex_init(p_mutex, NULL);
75+
return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
10376
}
104-
#endif
105-
return 0 == ret ? OPAL_SUCCESS : OPAL_ERR_IN_ERRNO;
10677
}
10778

10879
static inline void opal_thread_internal_mutex_lock(opal_thread_internal_mutex_t *p_mutex)

0 commit comments

Comments
 (0)