diff --git a/opal/runtime/opal_finalize.c b/opal/runtime/opal_finalize.c index 1229bb85e0..4719e1a011 100644 --- a/opal/runtime/opal_finalize.c +++ b/opal/runtime/opal_finalize.c @@ -13,7 +13,7 @@ * Copyright (c) 2010-2015 Los Alamos National Security, LLC. * All rights reserved. * Copyright (c) 2013-2015 Intel, Inc. All rights reserved - * Copyright (c) 2016 Research Organization for Information Science + * Copyright (c) 2016-2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. * $COPYRIGHT$ * @@ -55,6 +55,7 @@ #include "opal/runtime/opal_cr.h" #include "opal/mca/crs/base/base.h" +#include "opal/threads/tsd.h" extern int opal_initialized; extern int opal_util_initialized; @@ -160,6 +161,9 @@ opal_finalize(void) /* close the sec framework */ (void) mca_base_framework_close(&opal_sec_base_framework); + /* cleanup the main thread specific stuff */ + opal_tsd_keys_destruct(); + /* finalize util code */ opal_finalize_util(); diff --git a/opal/runtime/opal_init.c b/opal/runtime/opal_init.c index dd844c0106..28e0ef92a9 100644 --- a/opal/runtime/opal_init.c +++ b/opal/runtime/opal_init.c @@ -16,7 +16,7 @@ * Copyright (c) 2010-2015 Los Alamos National Security, LLC. * All rights reserved. * Copyright (c) 2013-2016 Intel, Inc. All rights reserved - * Copyright (c) 2015-2016 Research Organization for Information Science + * Copyright (c) 2015-2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. * $COPYRIGHT$ * @@ -344,6 +344,8 @@ opal_init_util(int* pargc, char*** pargv) return OPAL_SUCCESS; } + opal_thread_set_main(); + opal_init_called = true; /* set the nodename right away so anyone who needs it has it. Note diff --git a/opal/threads/thread.c b/opal/threads/thread.c index f74efaa975..e3405d8503 100644 --- a/opal/threads/thread.c +++ b/opal/threads/thread.c @@ -10,7 +10,7 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2010 Cisco Systems, Inc. All rights reserved. - * Copyright (c) 2015 Research Organization for Information Science + * Copyright (c) 2015-2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. * $COPYRIGHT$ * @@ -22,12 +22,23 @@ #include "opal_config.h" #include "opal/threads/threads.h" +#include "opal/threads/tsd.h" #include "opal/constants.h" bool opal_debug_threads = false; static void opal_thread_construct(opal_thread_t *t); +static pthread_t opal_main_thread; + +struct opal_tsd_key_value { + opal_tsd_key_t key; + opal_tsd_destructor_t destructor; +}; + +static struct opal_tsd_key_value *opal_tsd_key_values = NULL; +static int opal_tsd_key_values_count = 0; + OBJ_CLASS_INSTANCE(opal_thread_t, opal_object_t, opal_thread_construct, NULL); @@ -83,3 +94,39 @@ void opal_thread_kill(opal_thread_t *t, int sig) { pthread_kill(t->t_handle, sig); } + +int opal_tsd_key_create(opal_tsd_key_t *key, + opal_tsd_destructor_t destructor) +{ + int rc; + rc = pthread_key_create(key, destructor); + if ((0 == rc) && (pthread_self() == opal_main_thread)) { + opal_tsd_key_values = (struct opal_tsd_key_value *)realloc(opal_tsd_key_values, (opal_tsd_key_values_count+1) * sizeof(struct opal_tsd_key_value)); + opal_tsd_key_values[opal_tsd_key_values_count].key = *key; + opal_tsd_key_values[opal_tsd_key_values_count].destructor = destructor; + opal_tsd_key_values_count ++; + } + return rc; +} + +int opal_tsd_keys_destruct() +{ + int i; + void * ptr; + for (i=0; i