From 83aae8844a7bf229828ee6cf671e0b1251bfe1d4 Mon Sep 17 00:00:00 2001 From: George Bosilca Date: Mon, 30 Oct 2023 14:51:13 -0400 Subject: [PATCH 1/2] Avoid static initialization of recursive mutexes There is no portable way to statically initialize recursive mutexes. Where possible avoid doing so, and where not possible add a specialized __constructor__ (thanks @eschnett for the patch). Fixes #12029. Signed-off-by: George Bosilca (cherry picked from commit ad4c825e95ecd084b146947079216875e44d674c) --- ompi/instance/instance.c | 7 +++++++ opal/mca/btl/usnic/btl_usnic_component.c | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ompi/instance/instance.c b/ompi/instance/instance.c index 8b67db9ccd5..1427db7b982 100644 --- a/ompi/instance/instance.c +++ b/ompi/instance/instance.c @@ -59,7 +59,14 @@ ompi_predefined_instance_t ompi_mpi_instance_null = {{{{0}}}}; +#if defined(OPAL_RECURSIVE_MUTEX_STATIC_INIT) static opal_recursive_mutex_t instance_lock = OPAL_RECURSIVE_MUTEX_STATIC_INIT; +#else +static opal_recursive_mutex_t instance_lock; +__attribute__((__constructor__)) static void instance_lock_init(void) { + OBJ_CONSTRUCT(&instance_lock, opal_recursive_mutex_t); +} +#endif /* defined(OPAL_RECURSIVE_MUTEX_STATIC_INIT) */ /** MPI_Init instance */ ompi_instance_t *ompi_mpi_instance_default = NULL; diff --git a/opal/mca/btl/usnic/btl_usnic_component.c b/opal/mca/btl/usnic/btl_usnic_component.c index 4c771a16e12..fe0b00f312f 100644 --- a/opal/mca/btl/usnic/btl_usnic_component.c +++ b/opal/mca/btl/usnic/btl_usnic_component.c @@ -83,7 +83,7 @@ #define OPAL_BTL_USNIC_NUM_COMPLETIONS 500 /* MPI_THREAD_MULTIPLE_SUPPORT */ -opal_recursive_mutex_t btl_usnic_lock = OPAL_RECURSIVE_MUTEX_STATIC_INIT; +opal_recursive_mutex_t btl_usnic_lock; /* recursive mutexes must be dynamically initialized */ /* RNG buffer definition */ opal_rng_buff_t opal_btl_usnic_rand_buff = {{0}}; From 6fbf8a3b0fddea5141e28b70d0f5fc3793d9b324 Mon Sep 17 00:00:00 2001 From: George Bosilca Date: Mon, 30 Oct 2023 15:54:05 -0400 Subject: [PATCH 2/2] Add support for __constructor__ The only remaining statically initialized recursive mutex has now support for the constructor attribute. Signed-off-by: George Bosilca (cherry picked from commit 787dd7bf65b3e8f5ebb0a64b455415e549364abe) --- config/opal_check_attributes.m4 | 10 ++++++++++ ompi/instance/instance.c | 6 ++++-- opal/include/opal_config_bottom.h | 6 ++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config/opal_check_attributes.m4 b/config/opal_check_attributes.m4 index 9b2d5488f38..2d2e1ba3d7f 100644 --- a/config/opal_check_attributes.m4 +++ b/config/opal_check_attributes.m4 @@ -559,6 +559,14 @@ AC_DEFUN([OPAL_CHECK_ATTRIBUTES], [ [], []) + _OPAL_CHECK_SPECIFIC_ATTRIBUTE([constructor], + [ + void foo(void) __attribute__ ((__constructor__)); + void foo(void) { return ; } + ], + [], + []) + _OPAL_CHECK_SPECIFIC_ATTRIBUTE([destructor], [ void foo(void) __attribute__ ((__destructor__)); @@ -631,6 +639,8 @@ AC_DEFUN([OPAL_CHECK_ATTRIBUTES], [ [Whether your compiler has __attribute__ warn unused result or not]) AC_DEFINE_UNQUOTED(OPAL_HAVE_ATTRIBUTE_WEAK_ALIAS, [$opal_cv___attribute__weak_alias], [Whether your compiler has __attribute__ weak alias or not]) + AC_DEFINE_UNQUOTED(OPAL_HAVE_ATTRIBUTE_CONSTRUCTOR, [$opal_cv___attribute__constructor], + [Whether your compiler has __attribute__ constructor or not]) AC_DEFINE_UNQUOTED(OPAL_HAVE_ATTRIBUTE_DESTRUCTOR, [$opal_cv___attribute__destructor], [Whether your compiler has __attribute__ destructor or not]) AC_DEFINE_UNQUOTED(OPAL_HAVE_ATTRIBUTE_OPTNONE, [$opal_cv___attribute__optnone], diff --git a/ompi/instance/instance.c b/ompi/instance/instance.c index 1427db7b982..6d85bdec0b4 100644 --- a/ompi/instance/instance.c +++ b/ompi/instance/instance.c @@ -61,11 +61,13 @@ ompi_predefined_instance_t ompi_mpi_instance_null = {{{{0}}}}; #if defined(OPAL_RECURSIVE_MUTEX_STATIC_INIT) static opal_recursive_mutex_t instance_lock = OPAL_RECURSIVE_MUTEX_STATIC_INIT; -#else +#elif defined(OPAL_HAVE_ATTRIBUTE_CONSTRUCTOR) static opal_recursive_mutex_t instance_lock; -__attribute__((__constructor__)) static void instance_lock_init(void) { +__opal_attribute_constructor__ static void instance_lock_init(void) { OBJ_CONSTRUCT(&instance_lock, opal_recursive_mutex_t); } +#else +#error "No support for recursive mutexes available on this platform. #endif /* defined(OPAL_RECURSIVE_MUTEX_STATIC_INIT) */ /** MPI_Init instance */ diff --git a/opal/include/opal_config_bottom.h b/opal/include/opal_config_bottom.h index 2c2392a5429..a7337ab6cbd 100644 --- a/opal/include/opal_config_bottom.h +++ b/opal/include/opal_config_bottom.h @@ -229,6 +229,12 @@ # define __opal_attribute_weak_alias__(a) #endif +#if OPAL_HAVE_ATTRIBUTE_CONSTRUCTOR +# define __opal_attribute_constructor__ __attribute__((__constructor__)) +#else +# define __opal_attribute_constructor__ +#endif + #if OPAL_HAVE_ATTRIBUTE_DESTRUCTOR # define __opal_attribute_destructor__ __attribute__((__destructor__)) #else