Skip to content

Commit e5ef80f

Browse files
authored
Merge pull request #7936 from janjust/master-new-tsd-thread-api
Master: new thread-specific-data (tsd) api
2 parents 863a058 + d809f6b commit e5ef80f

File tree

15 files changed

+172
-147
lines changed

15 files changed

+172
-147
lines changed

ompi/runtime/ompi_rte.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int _setup_proc_session_dir(char **sdir);
7878
#define OPAL_PRINT_NAME_ARG_NUM_BUFS 16
7979

8080
static bool fns_init=false;
81-
static opal_tsd_key_t print_args_tsd_key;
81+
static opal_tsd_tracked_key_t print_args_tsd_key;
8282
static char* opal_print_args_null = "NULL";
8383
typedef struct {
8484
char *buffers[OPAL_PRINT_NAME_ARG_NUM_BUFS];
@@ -108,14 +108,12 @@ get_print_name_buffer(void)
108108

109109
if (!fns_init) {
110110
/* setup the print_args function */
111-
if (OPAL_SUCCESS != (ret = opal_tsd_key_create(&print_args_tsd_key, buffer_cleanup))) {
112-
OPAL_ERROR_LOG(ret);
113-
return NULL;
114-
}
111+
OBJ_CONSTRUCT(&print_args_tsd_key, opal_tsd_tracked_key_t);
112+
opal_tsd_tracked_key_set_destructor(&print_args_tsd_key, buffer_cleanup);
115113
fns_init = true;
116114
}
117115

118-
ret = opal_tsd_getspecific(print_args_tsd_key, (void**)&ptr);
116+
ret = opal_tsd_tracked_key_get(&print_args_tsd_key, (void**)&ptr);
119117
if (OPAL_SUCCESS != ret) return NULL;
120118

121119
if (NULL == ptr) {
@@ -124,7 +122,7 @@ get_print_name_buffer(void)
124122
ptr->buffers[i] = (char *) malloc((OPAL_PRINT_NAME_ARGS_MAX_SIZE+1) * sizeof(char));
125123
}
126124
ptr->cntr = 0;
127-
ret = opal_tsd_setspecific(print_args_tsd_key, (void*)ptr);
125+
ret = opal_tsd_tracked_key_set(&print_args_tsd_key, (void*)ptr);
128126
}
129127

130128
return (opal_print_args_buffers_t*) ptr;
@@ -940,6 +938,10 @@ int ompi_rte_finalize(void)
940938
opal_process_info.initial_errhandler = NULL;
941939
}
942940

941+
if (fns_init) {
942+
OBJ_DESTRUCT(&print_args_tsd_key);
943+
}
944+
943945
/* cleanup our internal nspace hack */
944946
opal_pmix_finalize_nspace_tracker();
945947

opal/mca/common/ucx/common_ucx_wpool.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ opal_common_ucx_wpool_init(opal_common_ucx_wpool_t *wpool,
207207
goto err_wpool_add;
208208
}
209209

210-
opal_tsd_key_create(&wpool->tls_key, _tlocal_cleanup);
211-
210+
OBJ_CONSTRUCT(&wpool->tls_key, opal_tsd_tracked_key_t);
211+
opal_tsd_tracked_key_set_destructor(&wpool->tls_key, _tlocal_cleanup);
212212
return rc;
213213

214214
err_wpool_add:
@@ -235,7 +235,7 @@ void opal_common_ucx_wpool_finalize(opal_common_ucx_wpool_t *wpool)
235235

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

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

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

570570
(*mem_ptr) = mem;
571571
(*my_mem_addr) = rkey_addr;
@@ -653,7 +653,7 @@ static int _comm_ucx_wpmem_map(opal_common_ucx_wpool_t *wpool,
653653

654654
static void _common_ucx_wpmem_free(opal_common_ucx_wpmem_t *mem)
655655
{
656-
opal_tsd_key_delete(mem->mem_tls_key);
656+
OBJ_DESTRUCT(&mem->mem_tls_key);
657657
free(mem->mem_addrs);
658658
free(mem->mem_displs);
659659
ucp_mem_unmap(mem->ctx->wpool->ucp_ctx, mem->memh);
@@ -726,15 +726,15 @@ static _tlocal_table_t* _common_ucx_tls_init(opal_common_ucx_wpool_t *wpool)
726726
return NULL;
727727
}
728728

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

731731
return tls;
732732
}
733733

734734
static inline _tlocal_table_t *
735735
_tlocal_get_tls(opal_common_ucx_wpool_t *wpool){
736736
_tlocal_table_t *tls;
737-
int rc = opal_tsd_getspecific(wpool->tls_key, (void**)&tls);
737+
int rc = opal_tsd_tracked_key_get(&wpool->tls_key, (void**)&tls);
738738

739739
if (OPAL_SUCCESS != rc) {
740740
return NULL;
@@ -795,7 +795,7 @@ static void _common_ucx_tls_cleanup(_tlocal_table_t *tls)
795795
free(tls->ctx_tbl[i]);
796796
}
797797

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

800800
OBJ_RELEASE(tls);
801801
return;
@@ -1051,7 +1051,7 @@ static _tlocal_mem_t *_tlocal_add_mem(_tlocal_table_t *tls,
10511051
calloc(1, sizeof(*tls->mem_tbl[free_idx]->mem_tls_ptr));
10521052
tls->mem_tbl[free_idx]->mem_tls_ptr->winfo = ctx_rec->winfo;
10531053
tls->mem_tbl[free_idx]->mem_tls_ptr->rkeys = tls->mem_tbl[free_idx]->mem->rkeys;
1054-
opal_tsd_setspecific(mem->mem_tls_key, tls->mem_tbl[free_idx]->mem_tls_ptr);
1054+
opal_tsd_tracked_key_set(&mem->mem_tls_key, tls->mem_tbl[free_idx]->mem_tls_ptr);
10551055

10561056
/* Make sure that we completed all the data structures before
10571057
* placing the item to the list

opal/mca/common/ucx/common_ucx_wpool.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ typedef struct {
4848

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

5353
/* Bookkeeping information */
5454
opal_list_t idle_workers;
@@ -107,7 +107,7 @@ typedef struct {
107107
/* TLS item that allows each thread to
108108
* store endpoints and rkey arrays
109109
* for faster access */
110-
opal_tsd_key_t mem_tls_key;
110+
opal_tsd_tracked_key_t mem_tls_key;
111111
} opal_common_ucx_wpmem_t;
112112

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

203203
/* First check the fast-path */
204-
rc = opal_tsd_getspecific(mem->mem_tls_key, (void**)&fp);
204+
rc = opal_tsd_tracked_key_get(&mem->mem_tls_key, (void**)&fp);
205205
if (OPAL_SUCCESS != rc) {
206206
return rc;
207207
}
@@ -212,7 +212,7 @@ opal_common_ucx_tlocal_fetch(opal_common_ucx_wpmem_t *mem, int target,
212212
if (OPAL_SUCCESS != rc) {
213213
return rc;
214214
}
215-
rc = opal_tsd_getspecific(mem->mem_tls_key, (void**)&fp);
215+
rc = opal_tsd_tracked_key_get(&mem->mem_tls_key, (void**)&fp);
216216
if (OPAL_SUCCESS != rc) {
217217
return rc;
218218
}

opal/mca/hwloc/base/hwloc_base_frame.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,20 @@ static int opal_hwloc_base_open(mca_base_open_flag_t flags)
243243
return OPAL_SUCCESS;
244244
}
245245

246+
static opal_tsd_tracked_key_t *print_tsd_key = NULL;
247+
246248
static int opal_hwloc_base_close(void)
247249
{
248250
int ret;
249251
if (!opal_hwloc_base_inited) {
250252
return OPAL_SUCCESS;
251253
}
252254

255+
if (NULL != print_tsd_key) {
256+
OBJ_RELEASE(print_tsd_key);
257+
print_tsd_key = NULL;
258+
}
259+
253260
/* no need to close the component as it was statically opened */
254261

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

279286
static bool fns_init=false;
280-
static opal_tsd_key_t print_tsd_key;
281287
char* opal_hwloc_print_null = "NULL";
282288

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

302308
if (!fns_init) {
303309
/* setup the print_args function */
304-
if (OPAL_SUCCESS != (ret = opal_tsd_key_create(&print_tsd_key, buffer_cleanup))) {
305-
return NULL;
306-
}
310+
print_tsd_key = OBJ_NEW(opal_tsd_tracked_key_t);
311+
opal_tsd_tracked_key_set_destructor(print_tsd_key, buffer_cleanup);
307312
fns_init = true;
308313
}
309314

310-
ret = opal_tsd_getspecific(print_tsd_key, (void**)&ptr);
315+
ret = opal_tsd_tracked_key_get(print_tsd_key, (void**)&ptr);
311316
if (OPAL_SUCCESS != ret) return NULL;
312317

313318
if (NULL == ptr) {
@@ -316,7 +321,7 @@ opal_hwloc_print_buffers_t *opal_hwloc_get_print_buffer(void)
316321
ptr->buffers[i] = (char *) malloc((OPAL_HWLOC_PRINT_MAX_SIZE+1) * sizeof(char));
317322
}
318323
ptr->cntr = 0;
319-
ret = opal_tsd_setspecific(print_tsd_key, (void*)ptr);
324+
ret = opal_tsd_tracked_key_set(print_tsd_key, (void*)ptr);
320325
}
321326

322327
return (opal_hwloc_print_buffers_t*) ptr;

opal/mca/threads/argobots/threads_argobots_module.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,43 +122,5 @@ int opal_tsd_key_create(opal_tsd_key_t *key, opal_tsd_destructor_t destructor)
122122
opal_threads_argobots_ensure_init();
123123
int rc;
124124
rc = ABT_key_create(destructor, key);
125-
if (ABT_SUCCESS == rc) {
126-
opal_mutex_lock(&opal_tsd_lock);
127-
if (opal_tsd_key_values_size <= opal_tsd_key_values_count) {
128-
opal_tsd_key_values_size = opal_tsd_key_values_size == 0
129-
? 1 : opal_tsd_key_values_size * 2;
130-
opal_tsd_key_values = (struct opal_tsd_key_value *)
131-
realloc(opal_tsd_key_values, opal_tsd_key_values_size *
132-
sizeof(struct opal_tsd_key_value));
133-
}
134-
opal_tsd_key_values[opal_tsd_key_values_count].key = *key;
135-
opal_tsd_key_values[opal_tsd_key_values_count].destructor = destructor;
136-
opal_tsd_key_values_count++;
137-
opal_mutex_unlock(&opal_tsd_lock);
138-
}
139125
return (ABT_SUCCESS == rc) ? OPAL_SUCCESS : OPAL_ERROR;
140126
}
141-
142-
int opal_tsd_keys_destruct(void)
143-
{
144-
int i;
145-
void *ptr;
146-
opal_mutex_lock(&opal_tsd_lock);
147-
for (i = 0; i < opal_tsd_key_values_count; i++) {
148-
if (OPAL_SUCCESS ==
149-
opal_tsd_getspecific(opal_tsd_key_values[i].key, &ptr)) {
150-
if (NULL != opal_tsd_key_values[i].destructor) {
151-
opal_tsd_key_values[i].destructor(ptr);
152-
opal_tsd_setspecific(opal_tsd_key_values[i].key, NULL);
153-
}
154-
}
155-
}
156-
if (0 < opal_tsd_key_values_count) {
157-
free(opal_tsd_key_values);
158-
opal_tsd_key_values = NULL;
159-
opal_tsd_key_values_count = 0;
160-
opal_tsd_key_values_size = 0;
161-
}
162-
opal_mutex_unlock(&opal_tsd_lock);
163-
return OPAL_SUCCESS;
164-
}

opal/mca/threads/argobots/threads_argobots_tsd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ static inline int opal_tsd_key_delete(opal_tsd_key_t key)
3737
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;
3838
}
3939

40-
static inline int opal_tsd_setspecific(opal_tsd_key_t key, void *value)
40+
static inline int opal_tsd_set(opal_tsd_key_t key, void *value)
4141
{
4242
opal_threads_argobots_ensure_init();
4343
int ret = ABT_key_set(key, value);
4444
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;
4545
}
4646

47-
static inline int opal_tsd_getspecific(opal_tsd_key_t key, void **valuep)
47+
static inline int opal_tsd_get(opal_tsd_key_t key, void **valuep)
4848
{
4949
int ret = ABT_key_get(key, valuep);
5050
return ABT_SUCCESS == ret ? OPAL_SUCCESS : OPAL_ERROR;

opal/mca/threads/base/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ headers += \
2222
base/base.h
2323

2424
libmca_threads_la_SOURCES += \
25-
base/threads_base.c
25+
base/threads_base.c \
26+
base/tsd.c

opal/mca/threads/base/tsd.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "opal/mca/threads/tsd.h"
2+
3+
static void _tracked_destructor(void *arg) {
4+
opal_tsd_list_item_t *tsd = NULL;
5+
opal_tsd_tracked_key_t *key = NULL;
6+
if (NULL == arg) {
7+
return;
8+
}
9+
10+
tsd = (opal_tsd_list_item_t *)arg;
11+
key = tsd->tracked_key;
12+
13+
opal_mutex_lock(&key->mutex);
14+
opal_list_remove_item(&key->tsd_list, &tsd->super);
15+
opal_mutex_unlock(&key->mutex);
16+
17+
if (NULL != key->user_destructor) {
18+
key->user_destructor(tsd->data);
19+
}
20+
OBJ_RELEASE(tsd);
21+
}
22+
23+
void opal_tsd_tracked_key_constructor(opal_tsd_tracked_key_t *key) {
24+
OBJ_CONSTRUCT(&key->mutex, opal_mutex_t);
25+
OBJ_CONSTRUCT(&key->tsd_list, opal_list_t);
26+
key->user_destructor = NULL;
27+
opal_tsd_key_create(&key->key, _tracked_destructor);
28+
}
29+
30+
void opal_tsd_tracked_key_destructor(opal_tsd_tracked_key_t *key)
31+
{
32+
opal_tsd_list_item_t *tsd, *next;
33+
34+
opal_tsd_key_delete(key->key);
35+
OPAL_LIST_FOREACH_SAFE(tsd, next, &key->tsd_list, opal_tsd_list_item_t) {
36+
opal_list_remove_item(&key->tsd_list, &tsd->super);
37+
if (NULL != key->user_destructor) {
38+
key->user_destructor(tsd->data);
39+
}
40+
OBJ_RELEASE(tsd);
41+
}
42+
OBJ_DESTRUCT(&key->mutex);
43+
OBJ_DESTRUCT(&key->tsd_list);
44+
}
45+
46+
int opal_tsd_tracked_key_set(opal_tsd_tracked_key_t *key, void *p)
47+
{
48+
assert( NULL != key);
49+
50+
opal_tsd_list_item_t *tsd = NULL;
51+
opal_tsd_get(key->key, (void **)&tsd);
52+
53+
if (NULL == tsd) {
54+
tsd = OBJ_NEW(opal_tsd_list_item_t);
55+
if (NULL == tsd) {
56+
return OPAL_ERR_OUT_OF_RESOURCE;
57+
}
58+
59+
opal_mutex_lock(&key->mutex);
60+
opal_list_append(&key->tsd_list, &tsd->super);
61+
opal_mutex_unlock(&key->mutex);
62+
}
63+
64+
tsd->data = p;
65+
tsd->tracked_key = key;
66+
67+
return opal_tsd_set(key->key, (void *)tsd);
68+
}
69+
70+
void opal_tsd_tracked_key_set_destructor(opal_tsd_tracked_key_t *key, opal_tsd_destructor_t destructor)
71+
{
72+
assert (NULL != key);
73+
key->user_destructor = destructor;
74+
}
75+
76+
OBJ_CLASS_INSTANCE(opal_tsd_list_item_t, opal_list_item_t, NULL, NULL);
77+
OBJ_CLASS_INSTANCE(opal_tsd_tracked_key_t, opal_object_t,
78+
opal_tsd_tracked_key_constructor, opal_tsd_tracked_key_destructor);

0 commit comments

Comments
 (0)