Skip to content

Commit 8e99c68

Browse files
shintaro-iwasakihppritcha
authored andcommitted
mca/threads: fix Pthreads mutex implementation.
Signed-off-by: Shintaro Iwasaki <siwasaki@anl.gov>
1 parent ca2f98e commit 8e99c68

17 files changed

+228
-241
lines changed

opal/mca/pmix/base/base.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ OPAL_DECLSPEC int opal_pmix_base_exchange(opal_value_t *info,
5555

5656
OPAL_DECLSPEC void opal_pmix_base_set_evbase(opal_event_base_t *evbase);
5757

58-
#define opal_pmix_condition_wait(a,b) pthread_cond_wait(a, &(b)->m_lock_pthread)
59-
typedef pthread_cond_t opal_pmix_condition_t;
60-
#define opal_pmix_condition_broadcast(a) pthread_cond_broadcast(a)
61-
#define opal_pmix_condition_signal(a) pthread_cond_signal(a)
62-
#define OPAL_PMIX_CONDITION_STATIC_INIT PTHREAD_COND_INITIALIZER
58+
#define opal_pmix_condition_wait(a,b) opal_cond_wait(a, b)
59+
typedef opal_cond_t opal_pmix_condition_t;
60+
#define opal_pmix_condition_broadcast(a) opal_cond_broadcast(a)
61+
#define opal_pmix_condition_signal(a) opal_cond_signal(a)
62+
#define OPAL_PMIX_CONDITION_STATIC_INIT OPAL_CONDITION_STATIC_INIT
6363

6464
typedef struct {
6565
opal_mutex_t mutex;
@@ -81,7 +81,7 @@ extern opal_pmix_base_t opal_pmix_base;
8181
#define OPAL_PMIX_CONSTRUCT_LOCK(l) \
8282
do { \
8383
OBJ_CONSTRUCT(&(l)->mutex, opal_mutex_t); \
84-
pthread_cond_init(&(l)->cond, NULL); \
84+
opal_cond_init(&(l)->cond); \
8585
(l)->active = true; \
8686
OPAL_POST_OBJECT((l)); \
8787
} while(0)
@@ -90,7 +90,7 @@ extern opal_pmix_base_t opal_pmix_base;
9090
do { \
9191
OPAL_ACQUIRE_OBJECT((l)); \
9292
OBJ_DESTRUCT(&(l)->mutex); \
93-
pthread_cond_destroy(&(l)->cond); \
93+
opal_cond_destroy(&(l)->cond); \
9494
} while(0)
9595

9696

opal/mca/threads/configure.m4

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,36 @@ m4_define(MCA_opal_threads_CONFIGURE_MODE, STOP_AT_FIRST)
2525
AC_DEFINE_UNQUOTED([OPAL_ENABLE_MULTI_THREADS], [1],
2626
[Whether we should enable thread support within the OPAL code base])
2727
AC_DEFUN([MCA_opal_threads_CONFIG],[
28-
threads_base_include=
29-
3028
# All components look at this value
3129
AC_ARG_WITH([threads],
3230
[AC_HELP_STRING([--with-threads=TYPE],
33-
[Build high resolution threads component TYPE])])
31+
[Build high resolution threads component TYPE])],
32+
[],
33+
[with_threads=pthreads])
34+
35+
thread_type=$with_threads
3436

3537
# first, compile all the components
3638
MCA_CONFIGURE_FRAMEWORK($1, $2, 1)
3739

38-
if test "$mutex_base_include" = "" ; then
39-
mutex_base_include="pthreads/mutex_unix.h"
40-
fi
40+
threads_base_include="${thread_type}/threads_${thread_type}_threads.h"
41+
mutex_base_include="${thread_type}/threads_${thread_type}_mutex.h"
42+
tsd_base_include="${thread_type}/threads_${thread_type}_tsd.h"
43+
wait_sync_base_include="${thread_type}/threads_${thread_type}_wait_sync.h"
44+
4145
AC_DEFINE_UNQUOTED([MCA_threads_IMPLEMENTATION_HEADER],
4246
["opal/mca/threads/$threads_base_include"],
4347
[Header to include for threads implementation])
4448

4549
AC_DEFINE_UNQUOTED([MCA_mutex_IMPLEMENTATION_HEADER],
4650
["opal/mca/threads/$mutex_base_include"],
4751
[Header to include for mutex implementation])
48-
])
4952

53+
AC_DEFINE_UNQUOTED([MCA_tsd_IMPLEMENTATION_HEADER],
54+
["opal/mca/threads/$tsd_base_include"],
55+
[Header to include for tsd implementation])
5056

57+
AC_DEFINE_UNQUOTED([MCA_wait_sync_IMPLEMENTATION_HEADER],
58+
["opal/mca/threads/$wait_sync_base_include"],
59+
[Header to include for wait_sync implementation])
60+
])

opal/mca/threads/mutex.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "opal_config.h"
3131

32+
BEGIN_C_DECLS
33+
3234
/**
3335
* @file:
3436
*
@@ -37,17 +39,14 @@
3739
* Functions for locking of critical sections.
3840
*/
3941

40-
4142
/**
4243
* Opaque mutex object
4344
*/
4445

4546
typedef struct opal_mutex_t opal_mutex_t;
4647
typedef struct opal_mutex_t opal_recursive_mutex_t;
4748

48-
BEGIN_C_DECLS
4949
#include MCA_mutex_IMPLEMENTATION_HEADER
50-
END_C_DECLS
5150

5251
OBJ_CLASS_DECLARATION(opal_mutex_t);
5352
OBJ_CLASS_DECLARATION(opal_recursive_mutex_t);

opal/mca/threads/pthreads/Makefile.am

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
noinst_LTLIBRARIES = libmca_threads_pthreads.la
2222

2323
libmca_threads_pthreads_la_SOURCES = \
24-
threads_pthreads.h \
2524
threads_pthreads_component.c \
26-
threads_pthreads_mutex.c \
2725
threads_pthreads_condition.c \
26+
threads_pthreads_module.c \
27+
threads_pthreads_mutex.c \
28+
threads_pthreads_mutex.h \
29+
threads_pthreads_threads.h \
30+
threads_pthreads_tsd.h \
2831
threads_pthreads_wait_sync.c \
29-
threads_pthreads_module.c
32+
threads_pthreads_wait_sync.h

opal/mca/threads/pthreads/configure.m4

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ AC_DEFUN([MCA_opal_threads_pthreads_COMPILE_MODE], [
3030
])
3131

3232
AC_DEFUN([MCA_opal_threads_pthreads_POST_CONFIG],[
33-
AS_IF([test "$1" = "1"], [threads_base_include="pthreads/threads_pthreads.h"])
33+
AS_IF([test "$1" = "1"], [threads_base_include="pthreads/threads_pthreads_threads.h"])
3434
])dnl
3535

3636
AC_DEFUN([MCA_opal_mutex_pthreads_POST_CONFIG],[
37-
AS_IF([test "$1" = "1"], [mutex_base_include="pthreads/mutex_unix.h"])
37+
AS_IF([test "$1" = "1"], [mutex_base_include="pthreads/threads_pthreads_mutex.h"])
38+
])dnl
39+
40+
AC_DEFUN([MCA_opal_tsd_pthreads_POST_CONFIG],[
41+
AS_IF([test "$1" = "1"], [threads_base_include="pthreads/threads_pthreads_tsd.h"])
42+
])dnl
43+
44+
AC_DEFUN([MCA_opal_wait_sync_pthreads_POST_CONFIG],[
45+
AS_IF([test "$1" = "1"], [mutex_base_include="pthreads/threads_pthreads_wait_sync.h"])
3846
])dnl
3947

4048
# MCA_threads_pthreads_CONFIG(action-if-can-compile,

opal/mca/threads/pthreads/threads_pthreads_component.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
#include "opal/mca/threads/thread.h"
2727
#include "opal/mca/threads/threads.h"
28-
#include "opal/mca/threads/pthreads/threads_pthreads.h"
2928
#include "opal/constants.h"
3029

3130
static int opal_threads_pthreads_open(void);

opal/mca/threads/pthreads/threads_pthreads_mutex.c

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@
3131
#include "opal/mca/threads/mutex.h"
3232
//#include "opal/mca/threads/pthreads/mutex_unix.h"
3333

34-
35-
OBJ_CLASS_INSTANCE(opal_mutex_t,
36-
opal_object_t,
37-
NULL,
38-
NULL);
39-
OBJ_CLASS_INSTANCE(opal_recursive_mutex_t,
40-
opal_object_t,
41-
NULL,
42-
NULL);
43-
4434
/*
4535
* Wait and see if some upper layer wants to use threads, if support
4636
* exists.
@@ -63,3 +53,46 @@ struct opal_pthread_mutex_t {
6353
typedef struct opal_pthread_mutex_t opal_pthread_mutex_t;
6454
typedef struct opal_pthread_mutex_t opal_pthread_recursive_mutex_t;
6555

56+
static void mca_threads_pthreads_mutex_constructor(opal_mutex_t *p_mutex) {
57+
pthread_mutex_init(&p_mutex->m_lock_pthread, NULL);
58+
#if OPAL_ENABLE_DEBUG
59+
p_mutex->m_lock_debug = 0;
60+
p_mutex->m_lock_file = NULL;
61+
p_mutex->m_lock_line = 0;
62+
#endif
63+
opal_atomic_lock_init(&p_mutex->m_lock_atomic, 0);
64+
}
65+
66+
static void mca_threads_pthreads_mutex_desctructor(opal_mutex_t *p_mutex) {
67+
pthread_mutex_destroy(&p_mutex->m_lock_pthread);
68+
}
69+
70+
static void mca_threads_pthreads_recursive_mutex_constructor
71+
(opal_recursive_mutex_t *p_mutex) {
72+
pthread_mutexattr_t mutex_attr;
73+
pthread_mutexattr_init(&mutex_attr);
74+
pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
75+
pthread_mutex_init(&p_mutex->m_lock_pthread, &mutex_attr);
76+
pthread_mutexattr_destroy(&mutex_attr);
77+
#if OPAL_ENABLE_DEBUG
78+
p_mutex->m_lock_debug = 0;
79+
p_mutex->m_lock_file = NULL;
80+
p_mutex->m_lock_line = 0;
81+
#endif
82+
opal_atomic_lock_init(&p_mutex->m_lock_atomic, 0);
83+
}
84+
85+
static void mca_threads_pthreads_recursive_mutex_desctructor
86+
(opal_recursive_mutex_t *p_mutex) {
87+
pthread_mutex_destroy(&p_mutex->m_lock_pthread);
88+
}
89+
90+
OBJ_CLASS_INSTANCE(opal_mutex_t,
91+
opal_object_t,
92+
mca_threads_pthreads_mutex_constructor,
93+
mca_threads_pthreads_mutex_desctructor);
94+
95+
OBJ_CLASS_INSTANCE(opal_recursive_mutex_t,
96+
opal_object_t,
97+
mca_threads_pthreads_recursive_mutex_constructor,
98+
mca_threads_pthreads_recursive_mutex_desctructor);

opal/mca/threads/pthreads/mutex_unix.h renamed to opal/mca/threads/pthreads/threads_pthreads_mutex.h

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

26-
#ifndef OPAL_MUTEX_UNIX_H
27-
#define OPAL_MUTEX_UNIX_H 1
26+
#ifndef OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_MUTEX_H
27+
#define OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_MUTEX_H 1
2828

2929
/**
3030
* @file:
@@ -211,6 +211,14 @@ static inline void opal_mutex_atomic_unlock(opal_mutex_t *m)
211211

212212
#endif
213213

214+
typedef pthread_cond_t opal_cond_t;
215+
#define OPAL_CONDITION_STATIC_INIT PTHREAD_COND_INITIALIZER
216+
#define opal_cond_init(a) pthread_cond_init(a, NULL)
217+
#define opal_cond_wait(a,b) pthread_cond_wait(a, &(b)->m_lock_pthread)
218+
#define opal_cond_broadcast(a) pthread_cond_broadcast(a)
219+
#define opal_cond_signal(a) pthread_cond_signal(a)
220+
#define opal_cond_destroy(a) pthread_cond_destroy(a)
221+
214222
END_C_DECLS
215223

216-
#endif /* OPAL_MUTEX_UNIX_H */
224+
#endif /* OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_MUTEX_H */
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
#ifndef OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_THREADS_H
3+
#define OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_THREADS_H 1
4+
5+
#include <pthread.h>
6+
#include <signal.h>
7+
8+
struct opal_thread_t {
9+
opal_object_t super;
10+
opal_thread_fn_t t_run;
11+
void* t_arg;
12+
pthread_t t_handle;
13+
};
14+
15+
#endif /* OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_THREADS_H */
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#ifndef OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_TSD_H
3+
#define OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_TSD_H 1
4+
5+
#include <pthread.h>
6+
#include <signal.h>
7+
8+
typedef pthread_key_t opal_tsd_key_t;
9+
10+
static inline int
11+
opal_tsd_key_delete(opal_tsd_key_t key)
12+
{
13+
return pthread_key_delete(key);
14+
}
15+
16+
static inline int
17+
opal_tsd_setspecific(opal_tsd_key_t key, void *value)
18+
{
19+
return pthread_setspecific(key, value);
20+
}
21+
22+
static inline int
23+
opal_tsd_getspecific(opal_tsd_key_t key, void **valuep)
24+
{
25+
*valuep = pthread_getspecific(key);
26+
return OPAL_SUCCESS;
27+
}
28+
29+
#endif /* OPAL_MCA_THREADS_PTHREADS_THREADS_PTHREADS_TSD_H */

0 commit comments

Comments
 (0)