Skip to content

Commit 0bcef04

Browse files
authored
Merge pull request #8053 from shintaro-iwasaki/topic/fix_issue_8036
opal/mca/threads/qthreads: Fix #8036
2 parents 4c86172 + 84dcb23 commit 0bcef04

15 files changed

+446
-99
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,4 @@ test/util/bipartite_graph
688688
opal/test/reachable/reachable_netlink
689689
opal/test/reachable/reachable_weighted
690690
opal/mca/threads/argobots/threads_argobots.h
691+
opal/mca/threads/qthreads/threads_qthreads.h

opal/mca/threads/argobots/configure.m4

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,6 @@ AC_DEFUN([MCA_opal_threads_argobots_POST_CONFIG],[
123123
THREAD_CXXCPPFLAGS="$TPKG_CXXCPPFLAGS"
124124
THREAD_LDFLAGS="$TPKG_LDFLAGS"
125125
THREAD_LIBS="$TPKG_LIBS"
126-
AC_SUBST(THREAD_CFLAGS)
127-
AC_SUBST(THREAD_FCFLAGS)
128-
AC_SUBST(THREAD_CXXFLAGS)
129-
AC_SUBST(THREAD_CPPFLAGS)
130-
AC_SUBST(THREAD_LDFLAGS)
131-
AC_SUBST(THREAD_LIBS)
132126
LIBS="$LIBS $THREAD_LIBS"
133127
LDFLAGS="$LDFLAGS $THREAD_LDFLAGS"
134128
])

opal/mca/threads/configure.m4

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,12 @@ AS_IF([test x"$opal_thread_type_found" = x""],
5656

5757
AC_MSG_RESULT([Found thread type $opal_thread_type_found])
5858

59+
AC_SUBST(THREAD_CFLAGS)
60+
AC_SUBST(THREAD_FCFLAGS)
61+
AC_SUBST(THREAD_CXXFLAGS)
62+
AC_SUBST(THREAD_CPPFLAGS)
63+
AC_SUBST(THREAD_LDFLAGS)
64+
AC_SUBST(THREAD_LIBS)
65+
5966
OPAL_SUMMARY_ADD([[Miscellaneous]],[[Threading Package]],[], [$opal_thread_type_found])
6067
])dnl

opal/mca/threads/pthreads/configure.m4

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -824,12 +824,6 @@ AC_DEFUN([MCA_opal_threads_pthreads_POST_CONFIG],[
824824
THREAD_CXXCPPFLAGS="$TPKG_CXXCPPFLAGS"
825825
THREAD_LDFLAGS="$TPKG_LDFLAGS"
826826
THREAD_LIBS="$TPKG_LIBS"
827-
AC_SUBST(THREAD_CFLAGS)
828-
AC_SUBST(THREAD_FCFLAGS)
829-
AC_SUBST(THREAD_CXXFLAGS)
830-
AC_SUBST(THREAD_CPPFLAGS)
831-
AC_SUBST(THREAD_LDFLAGS)
832-
AC_SUBST(THREAD_LIBS)
833827
])
834828
])dnl
835829

opal/mca/threads/qthreads/Makefile.am

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,38 @@
1919
# $HEADER$
2020
#
2121

22-
noinst_LTLIBRARIES = libmca_threads_qthreads.la
22+
AM_CPPFLAGS = $(opal_qthreads_CPPFLAGS)
2323

24-
libmca_threads_qthreads_la_SOURCES = \
24+
sources = \
25+
threads_qthreads.h \
2526
threads_qthreads_component.c \
26-
threads_qthreads_mutex.c \
2727
threads_qthreads_condition.c \
28-
threads_qthreads_module.c
28+
threads_qthreads_module.c \
29+
threads_qthreads_mutex.c \
30+
threads_qthreads_wait_sync.c \
31+
threads_qthreads_mutex.h \
32+
threads_qthreads_threads.h \
33+
threads_qthreads_tsd.h \
34+
threads_qthreads_wait_sync.h
35+
36+
lib_sources = $(sources)
37+
38+
if MCA_BUILD_opal_threads_qthreads_DSO
39+
component_noinst =
40+
component_install = mca_threads_qthreads.la
41+
else
42+
component_noinst = libmca_threads_qthreads.la
43+
component_install =
44+
endif
45+
46+
mcacomponentdir = $(opallibdir)
47+
mcacomponent_LTLIBRARIES = $(component_install)
48+
mca_threads_qthreads_la_SOURCES = $(sources)
49+
mca_threads_qthreads_la_LDFLAGS = -module -avoid-version
50+
mca_threads_qthreads_la_LIBADD = $(top_builddir)/opal/lib@OPAL_LIB_PREFIX@open-pal.la \
51+
$(opal_qthreads_LIBS)
2952

30-
AM_LDFLAGS = -lqthread
53+
noinst_LTLIBRARIES = $(component_noinst)
54+
libmca_threads_qthreads_la_SOURCES = $(sources)
55+
libmca_threads_qthreads_la_LDFLAGS = -module -avoid-version $(opal_qthreads_LDFLAGS)
56+
libmca_threads_qthreads_la_LIBADD = $(opal_qthreads_LIBS)

opal/mca/threads/qthreads/configure.m4

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,68 @@
2525

2626
AC_DEFUN([OPAL_CONFIG_QTHREADS],[
2727

28-
AC_CHECK_HEADERS([qthread/qthread.h],
29-
[AC_CHECK_LIB([qthread],[qthread_initialize],
30-
[threads_qthreads_happy="yes"],
31-
[threads_qthreads_happy="no"])],
32-
[threads_qthreads_happy="no"])
28+
AC_ARG_WITH([qthreads],
29+
[AC_HELP_STRING([--with-qthreads=DIR],
30+
[Specify location of qthreads installation. Error if qthreads support cannot be found.])])
3331

34-
AS_IF([test "$threads_qthreads_happy" = "yes"],
32+
AC_ARG_WITH([qthreads-libdir],
33+
[AC_HELP_STRING([--with-qthreads-libdir=DIR],
34+
[Search for qthreads libraries in DIR])])
35+
36+
opal_check_qthreads_save_CPPFLAGS=$CPPFLAGS
37+
opal_check_qthreads_save_LDFLAGS=$LDFLAGS
38+
opal_check_qthreads_save_LIBS=$LIBS
39+
40+
opal_qthreads_happy=yes
41+
AS_IF([test "$with_qthreads" = "no"],
42+
[opal_qthreads_happy=no])
43+
44+
AS_IF([test $opal_qthreads_happy = yes],
45+
[AC_MSG_CHECKING([looking for qthreads in])
46+
AS_IF([test "$with_qthreads" != "yes"],
47+
[opal_qthreads_dir=$with_qthreads
48+
AC_MSG_RESULT([($opal_qthreads_dir)])],
49+
[AC_MSG_RESULT([(default search paths)])])
50+
AS_IF([test ! -z "$with_qthreads_libdir" && \
51+
test "$with_qthreads_libdir" != "yes"],
52+
[opal_qthreads_libdir=$with_qthreads_libdir])
53+
])
54+
55+
AS_IF([test $opal_qthreads_happy = yes],
56+
[OPAL_CHECK_PACKAGE([opal_qthreads],
57+
[qthread.h],
58+
[qthread],
59+
[qthread_initialize],
60+
[],
61+
[$opal_qthreads_dir],
62+
[$opal_qthreads_libdir],
63+
[],
64+
[opal_qthreads_happy=no])])
65+
66+
AS_IF([test $opal_qthreads_happy = yes && test -n "$opal_qthreads_dir"],
67+
[OPAL_QTHREADS_INCLUDE_PATH="$opal_qthreads_dir/include/"],
68+
[OPAL_QTHREADS_INCLUDE_PATH=""])
69+
70+
AS_IF([test $opal_qthreads_happy = yes],
71+
[TPKG_CFLAGS="$opal_qthreads_CPPFLAGS"
72+
TPKG_FCFLAGS="$opal_qthreads_CPPFLAGS"
73+
TPKG_CXXFLAGS="$opal_qthreads_CPPFLAGS"
74+
TPKG_CPPFLAGS="$opal_qthreads_CPPFLAGS"
75+
TPKG_CXXCPPFLAGS="$opal_qthreads_CPPFLAGS"
76+
TPKG_LDFLAGS="$opal_qthreads_LDFLAGS"
77+
TPKG_LIBS="$opal_qthreads_LIBS"])
78+
79+
AC_CONFIG_FILES([opal/mca/threads/qthreads/threads_qthreads.h])
80+
AC_SUBST([OPAL_QTHREADS_INCLUDE_PATH])
81+
AC_SUBST([opal_qthreads_CPPFLAGS])
82+
AC_SUBST([opal_qthreads_LDFLAGS])
83+
AC_SUBST([opal_qthreads_LIBS])
84+
85+
CPPFLAGS=$opal_check_qthreads_save_CPPFLAGS
86+
LDFLAGS=$opal_check_qthreads_save_LDFLAGS
87+
LIBS=$opal_check_qthreads_save_LIBS
88+
89+
AS_IF([test "$opal_qthreads_happy" = "yes"],
3590
[$1],
3691
[$2])
3792
])dnl
@@ -60,6 +115,15 @@ AC_DEFUN([MCA_opal_threads_qthreads_POST_CONFIG],[
60115
AC_DEFINE_UNQUOTED([MCA_threads_wait_sync_base_include_HEADER],
61116
["opal/mca/threads/qthreads/threads_qthreads_wait_sync.h"],
62117
[Header to include for wait_sync implementation])
118+
THREAD_CFLAGS="$TPKG_CFLAGS"
119+
THREAD_FCFLAGS="$TPKG_FCFLAGS"
120+
THREAD_CXXFLAGS="$TPKG_CXXFLAGS"
121+
THREAD_CPPFLAGS="$TPKG_CPPFLAGS"
122+
THREAD_CXXCPPFLAGS="$TPKG_CXXCPPFLAGS"
123+
THREAD_LDFLAGS="$TPKG_LDFLAGS"
124+
THREAD_LIBS="$TPKG_LIBS"
125+
LIBS="$LIBS $THREAD_LIBS"
126+
LDFLAGS="$LDFLAGS $THREAD_LDFLAGS"
63127
])
64128
])dnl
65129

opal/mca/threads/qthreads/threads_qthreads.h renamed to opal/mca/threads/qthreads/threads_qthreads.h.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
#ifndef OPAL_MCA_THREADS_QTHREADS_THREADS_QTHREADS_H
2727
#define OPAL_MCA_THREADS_QTHREADS_THREADS_QTHREADS_H 1
2828

29-
#include <qthread/qthread.h>
29+
#include "@OPAL_QTHREADS_INCLUDE_PATH@qthread.h"
30+
#include "@OPAL_QTHREADS_INCLUDE_PATH@qthread/tls.h"
3031

31-
static inline void ensure_init_qthreads(void)
32+
static inline void opal_threads_ensure_init_qthreads(void)
3233
{
3334
qthread_initialize();
3435
}

opal/mca/threads/qthreads/threads_qthreads_component.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "opal_config.h"
2525

26+
#include "opal/mca/threads/qthreads/threads_qthreads.h"
2627
#include "opal/mca/threads/thread.h"
2728
#include "opal/mca/threads/threads.h"
2829
#include "opal/constants.h"

opal/mca/threads/qthreads/threads_qthreads_module.c

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@
1919
* $HEADER$
2020
*/
2121

22-
#include <unistd.h>
23-
2422
#include "opal/mca/threads/qthreads/threads_qthreads.h"
2523
#include "opal/constants.h"
26-
#include "opal/util/sys_limits.h"
27-
#include "opal/util/output.h"
28-
#include "opal/prefetch.h"
2924
#include "opal/mca/threads/threads.h"
3025
#include "opal/mca/threads/tsd.h"
3126

@@ -34,40 +29,82 @@ struct opal_tsd_key_value {
3429
opal_tsd_destructor_t destructor;
3530
};
3631

37-
static int opal_main_thread;
38-
struct opal_tsd_key_value *opal_tsd_key_values = NULL;
39-
static int opal_tsd_key_values_count = 0;
32+
/* false: uninitialized, true: initialized. */
33+
static opal_atomic_lock_t opal_thread_self_key_lock = OPAL_ATOMIC_LOCK_INIT;
34+
static bool opal_thread_self_key_init = false;
35+
static opal_tsd_key_t opal_thread_self_key;
36+
37+
static inline void self_key_ensure_init(void)
38+
{
39+
if (false == opal_thread_self_key_init) {
40+
/* not initialized yet. */
41+
opal_atomic_lock(&opal_thread_self_key_lock);
42+
/* check again. */
43+
if (false == opal_thread_self_key_init) {
44+
/* This thread is responsible for initializing this key. */
45+
qthread_key_create(&opal_thread_self_key, NULL);
46+
opal_atomic_mb();
47+
opal_thread_self_key_init = true;
48+
}
49+
opal_atomic_unlock(&opal_thread_self_key_lock);
50+
}
51+
/* opal_thread_self_key has been already initialized. */
52+
}
4053

4154
/*
4255
* Constructor
4356
*/
4457
static void opal_thread_construct(opal_thread_t *t)
4558
{
59+
t->t_run = 0;
60+
t->t_thread_ret = 0;
4661
}
4762

4863
OBJ_CLASS_INSTANCE(opal_thread_t,
4964
opal_object_t,
5065
opal_thread_construct, NULL);
5166

67+
static inline aligned_t *opal_thread_get_qthreads_self(void)
68+
{
69+
self_key_ensure_init();
70+
void *ptr = qthread_getspecific(opal_thread_self_key);
71+
return (aligned_t *)ptr;
72+
}
5273

53-
opal_thread_t *opal_thread_get_self(void)
74+
static aligned_t opal_thread_qthreads_wrapper(void *arg)
5475
{
55-
return NULL;
76+
opal_thread_t *t = (opal_thread_t *)arg;
77+
78+
/* Register itself. */
79+
self_key_ensure_init();
80+
qthread_setspecific(opal_thread_self_key, t->t_thread_ret_ptr);
81+
82+
t->t_ret = ((void *(*)(void *))t->t_run)(t);
83+
return 0;
5684
}
5785

58-
bool opal_thread_self_compare(opal_thread_t *t)
86+
opal_thread_t *opal_thread_get_self(void)
5987
{
60-
return OPAL_ERR_NOT_IMPLEMENTED;
88+
opal_threads_ensure_init_qthreads();
89+
opal_thread_t *t = OBJ_NEW(opal_thread_t);
90+
t->t_thread_ret_ptr = opal_thread_get_qthreads_self();
91+
return t;
6192
}
6293

63-
int sync_wait_mt(void *p)
94+
bool opal_thread_self_compare(opal_thread_t *t)
6495
{
65-
return OPAL_ERR_NOT_IMPLEMENTED;
96+
opal_threads_ensure_init_qthreads();
97+
return opal_thread_get_qthreads_self() == &t->t_thread_ret;
6698
}
6799

68100
int opal_thread_join(opal_thread_t *t, void **thr_return)
69101
{
70-
return OPAL_ERR_NOT_IMPLEMENTED;
102+
qthread_readFF(NULL, t->t_thread_ret_ptr);
103+
if (thr_return) {
104+
*thr_return = t->t_ret;
105+
}
106+
t->t_thread_ret = 0;
107+
return OPAL_SUCCESS;
71108
}
72109

73110
void opal_thread_set_main(void)
@@ -76,12 +113,17 @@ void opal_thread_set_main(void)
76113

77114
int opal_thread_start(opal_thread_t *t)
78115
{
79-
return OPAL_ERR_NOT_IMPLEMENTED;
116+
opal_threads_ensure_init_qthreads();
117+
t->t_thread_ret_ptr = &t->t_thread_ret;
118+
qthread_fork(opal_thread_qthreads_wrapper, t, &t->t_thread_ret);
119+
return OPAL_SUCCESS;
80120
}
81121

82122
OBJ_CLASS_DECLARATION(opal_thread_t);
83123

84124
int opal_tsd_key_create(opal_tsd_key_t *key, opal_tsd_destructor_t destructor)
85125
{
86-
return OPAL_ERR_NOT_IMPLEMENTED;
126+
opal_threads_ensure_init_qthreads();
127+
qthread_key_create(key, destructor);
128+
return OPAL_SUCCESS;
87129
}

0 commit comments

Comments
 (0)