Skip to content

Master: new thread-specific-data (tsd) api #7936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions ompi/runtime/ompi_rte.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static int _setup_proc_session_dir(char **sdir);
#define OPAL_PRINT_NAME_ARG_NUM_BUFS 16

static bool fns_init=false;
static opal_tsd_key_t print_args_tsd_key;
static opal_tsd_tracked_key_t print_args_tsd_key;
static char* opal_print_args_null = "NULL";
typedef struct {
char *buffers[OPAL_PRINT_NAME_ARG_NUM_BUFS];
Expand Down Expand Up @@ -108,14 +108,12 @@ get_print_name_buffer(void)

if (!fns_init) {
/* setup the print_args function */
if (OPAL_SUCCESS != (ret = opal_tsd_key_create(&print_args_tsd_key, buffer_cleanup))) {
OPAL_ERROR_LOG(ret);
return NULL;
}
OBJ_CONSTRUCT(&print_args_tsd_key, opal_tsd_tracked_key_t);
opal_tsd_tracked_key_set_destructor(&print_args_tsd_key, buffer_cleanup);
fns_init = true;
}

ret = opal_tsd_getspecific(print_args_tsd_key, (void**)&ptr);
ret = opal_tsd_tracked_key_get(&print_args_tsd_key, (void**)&ptr);
if (OPAL_SUCCESS != ret) return NULL;

if (NULL == ptr) {
Expand All @@ -124,7 +122,7 @@ get_print_name_buffer(void)
ptr->buffers[i] = (char *) malloc((OPAL_PRINT_NAME_ARGS_MAX_SIZE+1) * sizeof(char));
}
ptr->cntr = 0;
ret = opal_tsd_setspecific(print_args_tsd_key, (void*)ptr);
ret = opal_tsd_tracked_key_set(&print_args_tsd_key, (void*)ptr);
}

return (opal_print_args_buffers_t*) ptr;
Expand Down Expand Up @@ -940,6 +938,10 @@ int ompi_rte_finalize(void)
opal_process_info.initial_errhandler = NULL;
}

if (fns_init) {
OBJ_DESTRUCT(&print_args_tsd_key);
}

/* cleanup our internal nspace hack */
opal_pmix_finalize_nspace_tracker();

Expand Down
18 changes: 9 additions & 9 deletions opal/mca/common/ucx/common_ucx_wpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ opal_common_ucx_wpool_init(opal_common_ucx_wpool_t *wpool,
goto err_wpool_add;
}

opal_tsd_key_create(&wpool->tls_key, _tlocal_cleanup);

OBJ_CONSTRUCT(&wpool->tls_key, opal_tsd_tracked_key_t);
opal_tsd_tracked_key_set_destructor(&wpool->tls_key, _tlocal_cleanup);
return rc;

err_wpool_add:
Expand All @@ -235,7 +235,7 @@ void opal_common_ucx_wpool_finalize(opal_common_ucx_wpool_t *wpool)

/* After this have been called no thread cleanup callback
* will be called */
opal_tsd_key_delete(wpool->tls_key);
OBJ_DESTRUCT(&wpool->tls_key);

/* Go over remaining TLS structures and release it */
OPAL_LIST_FOREACH_SAFE(tls_item, tls_next, &wpool->tls_list,
Expand Down Expand Up @@ -565,7 +565,7 @@ int opal_common_ucx_wpmem_create(opal_common_ucx_ctx_t *ctx,

/* Dont need the destructor here, will use
* wpool-level destructor */
opal_tsd_key_create(&mem->mem_tls_key, NULL);
OBJ_CONSTRUCT(&mem->mem_tls_key, opal_tsd_tracked_key_t);

(*mem_ptr) = mem;
(*my_mem_addr) = rkey_addr;
Expand Down Expand Up @@ -653,7 +653,7 @@ static int _comm_ucx_wpmem_map(opal_common_ucx_wpool_t *wpool,

static void _common_ucx_wpmem_free(opal_common_ucx_wpmem_t *mem)
{
opal_tsd_key_delete(mem->mem_tls_key);
OBJ_DESTRUCT(&mem->mem_tls_key);
free(mem->mem_addrs);
free(mem->mem_displs);
ucp_mem_unmap(mem->ctx->wpool->ucp_ctx, mem->memh);
Expand Down Expand Up @@ -726,15 +726,15 @@ static _tlocal_table_t* _common_ucx_tls_init(opal_common_ucx_wpool_t *wpool)
return NULL;
}

opal_tsd_setspecific(wpool->tls_key, tls);
opal_tsd_tracked_key_set(&wpool->tls_key, tls);

return tls;
}

static inline _tlocal_table_t *
_tlocal_get_tls(opal_common_ucx_wpool_t *wpool){
_tlocal_table_t *tls;
int rc = opal_tsd_getspecific(wpool->tls_key, (void**)&tls);
int rc = opal_tsd_tracked_key_get(&wpool->tls_key, (void**)&tls);

if (OPAL_SUCCESS != rc) {
return NULL;
Expand Down Expand Up @@ -795,7 +795,7 @@ static void _common_ucx_tls_cleanup(_tlocal_table_t *tls)
free(tls->ctx_tbl[i]);
}

opal_tsd_setspecific(tls->wpool->tls_key, NULL);
opal_tsd_tracked_key_set(&tls->wpool->tls_key, NULL);

OBJ_RELEASE(tls);
return;
Expand Down Expand Up @@ -1051,7 +1051,7 @@ static _tlocal_mem_t *_tlocal_add_mem(_tlocal_table_t *tls,
calloc(1, sizeof(*tls->mem_tbl[free_idx]->mem_tls_ptr));
tls->mem_tbl[free_idx]->mem_tls_ptr->winfo = ctx_rec->winfo;
tls->mem_tbl[free_idx]->mem_tls_ptr->rkeys = tls->mem_tbl[free_idx]->mem->rkeys;
opal_tsd_setspecific(mem->mem_tls_key, tls->mem_tbl[free_idx]->mem_tls_ptr);
opal_tsd_tracked_key_set(&mem->mem_tls_key, tls->mem_tbl[free_idx]->mem_tls_ptr);

/* Make sure that we completed all the data structures before
* placing the item to the list
Expand Down
8 changes: 4 additions & 4 deletions opal/mca/common/ucx/common_ucx_wpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct {

/* Thread-local key to allow each thread to have
* local information assisiated with this wpool */
opal_tsd_key_t tls_key;
opal_tsd_tracked_key_t tls_key;

/* Bookkeeping information */
opal_list_t idle_workers;
Expand Down Expand Up @@ -107,7 +107,7 @@ typedef struct {
/* TLS item that allows each thread to
* store endpoints and rkey arrays
* for faster access */
opal_tsd_key_t mem_tls_key;
opal_tsd_tracked_key_t mem_tls_key;
} opal_common_ucx_wpmem_t;

/* The structure that wraps UCP worker and holds the state that is required
Expand Down Expand Up @@ -201,7 +201,7 @@ opal_common_ucx_tlocal_fetch(opal_common_ucx_wpmem_t *mem, int target,
int rc = OPAL_SUCCESS;

/* First check the fast-path */
rc = opal_tsd_getspecific(mem->mem_tls_key, (void**)&fp);
rc = opal_tsd_tracked_key_get(&mem->mem_tls_key, (void**)&fp);
if (OPAL_SUCCESS != rc) {
return rc;
}
Expand All @@ -212,7 +212,7 @@ opal_common_ucx_tlocal_fetch(opal_common_ucx_wpmem_t *mem, int target,
if (OPAL_SUCCESS != rc) {
return rc;
}
rc = opal_tsd_getspecific(mem->mem_tls_key, (void**)&fp);
rc = opal_tsd_tracked_key_get(&mem->mem_tls_key, (void**)&fp);
if (OPAL_SUCCESS != rc) {
return rc;
}
Expand Down
17 changes: 11 additions & 6 deletions opal/mca/hwloc/base/hwloc_base_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,20 @@ static int opal_hwloc_base_open(mca_base_open_flag_t flags)
return OPAL_SUCCESS;
}

static opal_tsd_tracked_key_t *print_tsd_key = NULL;

static int opal_hwloc_base_close(void)
{
int ret;
if (!opal_hwloc_base_inited) {
return OPAL_SUCCESS;
}

if (NULL != print_tsd_key) {
OBJ_RELEASE(print_tsd_key);
print_tsd_key = NULL;
}

/* no need to close the component as it was statically opened */

/* for support of tools such as ompi_info */
Expand Down Expand Up @@ -277,7 +284,6 @@ static int opal_hwloc_base_close(void)
}

static bool fns_init=false;
static opal_tsd_key_t print_tsd_key;
char* opal_hwloc_print_null = "NULL";

static void buffer_cleanup(void *value)
Expand All @@ -301,13 +307,12 @@ opal_hwloc_print_buffers_t *opal_hwloc_get_print_buffer(void)

if (!fns_init) {
/* setup the print_args function */
if (OPAL_SUCCESS != (ret = opal_tsd_key_create(&print_tsd_key, buffer_cleanup))) {
return NULL;
}
print_tsd_key = OBJ_NEW(opal_tsd_tracked_key_t);
opal_tsd_tracked_key_set_destructor(print_tsd_key, buffer_cleanup);
fns_init = true;
}

ret = opal_tsd_getspecific(print_tsd_key, (void**)&ptr);
ret = opal_tsd_tracked_key_get(print_tsd_key, (void**)&ptr);
if (OPAL_SUCCESS != ret) return NULL;

if (NULL == ptr) {
Expand All @@ -316,7 +321,7 @@ opal_hwloc_print_buffers_t *opal_hwloc_get_print_buffer(void)
ptr->buffers[i] = (char *) malloc((OPAL_HWLOC_PRINT_MAX_SIZE+1) * sizeof(char));
}
ptr->cntr = 0;
ret = opal_tsd_setspecific(print_tsd_key, (void*)ptr);
ret = opal_tsd_tracked_key_set(print_tsd_key, (void*)ptr);
}

return (opal_hwloc_print_buffers_t*) ptr;
Expand Down
38 changes: 0 additions & 38 deletions opal/mca/threads/argobots/threads_argobots_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,43 +122,5 @@ int opal_tsd_key_create(opal_tsd_key_t *key, opal_tsd_destructor_t destructor)
opal_threads_argobots_ensure_init();
int rc;
rc = ABT_key_create(destructor, key);
if (ABT_SUCCESS == rc) {
opal_mutex_lock(&opal_tsd_lock);
if (opal_tsd_key_values_size <= opal_tsd_key_values_count) {
opal_tsd_key_values_size = opal_tsd_key_values_size == 0
? 1 : opal_tsd_key_values_size * 2;
opal_tsd_key_values = (struct opal_tsd_key_value *)
realloc(opal_tsd_key_values, opal_tsd_key_values_size *
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++;
opal_mutex_unlock(&opal_tsd_lock);
}
return (ABT_SUCCESS == rc) ? OPAL_SUCCESS : OPAL_ERROR;
}

int opal_tsd_keys_destruct(void)
{
int i;
void *ptr;
opal_mutex_lock(&opal_tsd_lock);
for (i = 0; i < opal_tsd_key_values_count; i++) {
if (OPAL_SUCCESS ==
opal_tsd_getspecific(opal_tsd_key_values[i].key, &ptr)) {
if (NULL != opal_tsd_key_values[i].destructor) {
opal_tsd_key_values[i].destructor(ptr);
opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
}
}
}
if (0 < opal_tsd_key_values_count) {
free(opal_tsd_key_values);
opal_tsd_key_values = NULL;
opal_tsd_key_values_count = 0;
opal_tsd_key_values_size = 0;
}
opal_mutex_unlock(&opal_tsd_lock);
return OPAL_SUCCESS;
}
4 changes: 2 additions & 2 deletions opal/mca/threads/argobots/threads_argobots_tsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ static inline int opal_tsd_key_delete(opal_tsd_key_t key)
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;
}

static inline int opal_tsd_setspecific(opal_tsd_key_t key, void *value)
static inline int opal_tsd_set(opal_tsd_key_t key, void *value)
{
opal_threads_argobots_ensure_init();
int ret = ABT_key_set(key, value);
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;
}

static inline int opal_tsd_getspecific(opal_tsd_key_t key, void **valuep)
static inline int opal_tsd_get(opal_tsd_key_t key, void **valuep)
{
int ret = ABT_key_get(key, valuep);
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;
Expand Down
3 changes: 2 additions & 1 deletion opal/mca/threads/base/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ headers += \
base/base.h

libmca_threads_la_SOURCES += \
base/threads_base.c
base/threads_base.c \
base/tsd.c
78 changes: 78 additions & 0 deletions opal/mca/threads/base/tsd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "opal/mca/threads/tsd.h"

static void _tracked_destructor(void *arg) {
opal_tsd_list_item_t *tsd = NULL;
opal_tsd_tracked_key_t *key = NULL;
if (NULL == arg) {
return;
}

tsd = (opal_tsd_list_item_t *)arg;
key = tsd->tracked_key;

opal_mutex_lock(&key->mutex);
opal_list_remove_item(&key->tsd_list, &tsd->super);
opal_mutex_unlock(&key->mutex);

if (NULL != key->user_destructor) {
key->user_destructor(tsd->data);
}
OBJ_RELEASE(tsd);
}

void opal_tsd_tracked_key_constructor(opal_tsd_tracked_key_t *key) {
OBJ_CONSTRUCT(&key->mutex, opal_mutex_t);
OBJ_CONSTRUCT(&key->tsd_list, opal_list_t);
key->user_destructor = NULL;
opal_tsd_key_create(&key->key, _tracked_destructor);
}

void opal_tsd_tracked_key_destructor(opal_tsd_tracked_key_t *key)
{
opal_tsd_list_item_t *tsd, *next;

opal_tsd_key_delete(key->key);
OPAL_LIST_FOREACH_SAFE(tsd, next, &key->tsd_list, opal_tsd_list_item_t) {
opal_list_remove_item(&key->tsd_list, &tsd->super);
if (NULL != key->user_destructor) {
key->user_destructor(tsd->data);
}
OBJ_RELEASE(tsd);
}
OBJ_DESTRUCT(&key->mutex);
OBJ_DESTRUCT(&key->tsd_list);
}

int opal_tsd_tracked_key_set(opal_tsd_tracked_key_t *key, void *p)
{
assert( NULL != key);

opal_tsd_list_item_t *tsd = NULL;
opal_tsd_get(key->key, (void **)&tsd);

if (NULL == tsd) {
tsd = OBJ_NEW(opal_tsd_list_item_t);
if (NULL == tsd) {
return OPAL_ERR_OUT_OF_RESOURCE;
}

opal_mutex_lock(&key->mutex);
opal_list_append(&key->tsd_list, &tsd->super);
opal_mutex_unlock(&key->mutex);
}

tsd->data = p;
tsd->tracked_key = key;

return opal_tsd_set(key->key, (void *)tsd);
}

void opal_tsd_tracked_key_set_destructor(opal_tsd_tracked_key_t *key, opal_tsd_destructor_t destructor)
{
assert (NULL != key);
key->user_destructor = destructor;
}

OBJ_CLASS_INSTANCE(opal_tsd_list_item_t, opal_list_item_t, NULL, NULL);
OBJ_CLASS_INSTANCE(opal_tsd_tracked_key_t, opal_object_t,
opal_tsd_tracked_key_constructor, opal_tsd_tracked_key_destructor);
Loading