From 039c7dbcd6bd9d2915097cdb2dcb747bc382c356 Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Wed, 14 Oct 2015 15:43:13 -0600 Subject: [PATCH 1/2] opal/mutex: add static mutex initializers Signed-off-by: Nathan Hjelm --- opal/class/opal_object.h | 15 ++++++++++-- opal/threads/mutex_unix.h | 51 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/opal/class/opal_object.h b/opal/class/opal_object.h index 7de1c9f5d0..f5811ff4e4 100644 --- a/opal/class/opal_object.h +++ b/opal/class/opal_object.h @@ -168,9 +168,20 @@ extern int opal_class_init_epoch; * @param NAME Name of the class to initialize */ #if OPAL_ENABLE_DEBUG -#define OPAL_OBJ_STATIC_INIT(BASE_CLASS) { OPAL_OBJ_MAGIC_ID, OBJ_CLASS(BASE_CLASS), 1, __FILE__, __LINE__ } +#define OPAL_OBJ_STATIC_INIT(BASE_CLASS) \ + { \ + .obj_magic_id = OPAL_OBJ_MAGIC_ID, \ + .obj_class = OBJ_CLASS(BASE_CLASS), \ + .obj_reference_count = 1, \ + .cls_init_file_name = __FILE__, \ + .cls_init_lineno = __LINE__, \ + } #else -#define OPAL_OBJ_STATIC_INIT(BASE_CLASS) { OBJ_CLASS(BASE_CLASS), 1 } +#define OPAL_OBJ_STATIC_INIT(BASE_CLASS) \ + { \ + .obj_class = OBJ_CLASS(BASE_CLASS), \ + .obj_reference_count = 1, \ + } #endif /** diff --git a/opal/threads/mutex_unix.h b/opal/threads/mutex_unix.h index d159285d8e..a2de825928 100644 --- a/opal/threads/mutex_unix.h +++ b/opal/threads/mutex_unix.h @@ -1,3 +1,4 @@ +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ /* * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana * University Research and Technology @@ -9,7 +10,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2007 Los Alamos National Security, LLC. All rights + * Copyright (c) 2007-2015 Los Alamos National Security, LLC. All rights * reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. @@ -63,6 +64,54 @@ struct opal_mutex_t { OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_mutex_t); OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_recursive_mutex_t); +#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) +#define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER) +#define OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER +#endif + +#if OPAL_ENABLE_DEBUG +#define OPAL_MUTEX_STATIC_INIT \ + { \ + .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \ + .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \ + .m_lock_debug = 0, \ + .m_lock_file = NULL, \ + .m_lock_line = 0, \ + .m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \ + } +#else +#define OPAL_MUTEX_STATIC_INIT \ + { \ + .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \ + .m_lock_pthread = PTHREAD_MUTEX_INITIALIZER, \ + .m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \ + } +#endif + +#if defined(OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER) + +#if OPAL_ENABLE_DEBUG +#define OPAL_RECURSIVE_MUTEX_STATIC_INIT \ + { \ + .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \ + .m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \ + .m_lock_debug = 0, \ + .m_lock_file = NULL, \ + .m_lock_line = 0, \ + .m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \ + } +#else +#define OPAL_RECURSIVE_MUTEX_STATIC_INIT \ + { \ + .super = OPAL_OBJ_STATIC_INIT(opal_mutex_t), \ + .m_lock_pthread = OPAL_PTHREAD_RECURSIVE_MUTEX_INITIALIZER, \ + .m_lock_atomic = { .u = { .lock = OPAL_ATOMIC_UNLOCKED } }, \ + } +#endif + +#endif + /************************************************************************ * * mutex operations (non-atomic versions) From 7f7ff8d851dca7463d3c8ea6e1525096c0073c40 Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Wed, 14 Oct 2015 15:43:32 -0600 Subject: [PATCH 2/2] mpit: use opal static mutex initializer Signed-off-by: Nathan Hjelm --- ompi/mpi/tool/init_thread.c | 10 ---------- ompi/mpi/tool/mpit_common.c | 11 +++-------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/ompi/mpi/tool/init_thread.c b/ompi/mpi/tool/init_thread.c index 478d3e9b16..8f0fb6b3c6 100644 --- a/ompi/mpi/tool/init_thread.c +++ b/ompi/mpi/tool/init_thread.c @@ -32,18 +32,8 @@ extern volatile int32_t initted; int MPI_T_init_thread (int required, int *provided) { - static volatile int32_t first_init = 1; int rc = MPI_SUCCESS; - if (opal_atomic_cmpset (&first_init, 1, 0) == 1) { - OBJ_CONSTRUCT(&mpit_big_lock, opal_mutex_t); - initted = 1; - } - - while (!initted) { - usleep (10); - } - mpit_lock (); do { diff --git a/ompi/mpi/tool/mpit_common.c b/ompi/mpi/tool/mpit_common.c index 158735cd70..eed2036839 100644 --- a/ompi/mpi/tool/mpit_common.c +++ b/ompi/mpi/tool/mpit_common.c @@ -13,23 +13,18 @@ #include "ompi/mpi/tool/mpit-internal.h" -opal_mutex_t mpit_big_lock = {{0}}; +opal_mutex_t mpit_big_lock = OPAL_MUTEX_STATIC_INIT; volatile uint32_t mpit_init_count = 0; -volatile int32_t initted = 0; void mpit_lock (void) { - if (initted) { - opal_mutex_lock (&mpit_big_lock); - } + opal_mutex_lock (&mpit_big_lock); } void mpit_unlock (void) { - if (initted) { - opal_mutex_unlock (&mpit_big_lock); - } + opal_mutex_unlock (&mpit_big_lock); } int ompit_var_type_to_datatype (mca_base_var_type_t type, MPI_Datatype *datatype)