13 static inline void ABTI_ktable_set(ABTI_ktable *p_ktable, ABTI_key *p_key,
15 static inline void *ABTI_ktable_get(ABTI_ktable *p_ktable, ABTI_key *p_key);
16 void ABTI_ktable_delete(ABTI_ktable *p_ktable, ABTI_key *p_key);
18 static ABTD_atomic_uint32
g_key_id = ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(0);
51 p_newkey = (ABTI_key *)
ABTU_malloc(
sizeof(ABTI_key));
52 p_newkey->f_destructor = destructor;
53 p_newkey->id = ABTD_atomic_fetch_add_uint32(&
g_key_id, 1);
54 ABTD_atomic_relaxed_store_uint32(&p_newkey->refcount, 1);
58 *newkey = ABTI_key_get_handle(p_newkey);
80 ABTI_key *p_key = ABTI_key_get_ptr(h_key);
81 ABTI_CHECK_NULL_KEY_PTR(p_key);
86 refcount = ABTD_atomic_fetch_sub_uint32(&p_key->refcount, 1);
117 ABTI_local *p_local = ABTI_local_get_local();
118 ABTI_thread *p_thread;
120 ABTI_ktable *p_ktable;
122 ABTI_key *p_key = ABTI_key_get_ptr(key);
123 ABTI_CHECK_NULL_KEY_PTR(p_key);
126 ABTI_CHECK_INITIALIZED();
130 p_thread = p_local->p_thread;
132 if (p_thread->p_keytable == NULL) {
134 p_thread->p_keytable = ABTI_ktable_alloc(key_table_size);
136 p_ktable = p_thread->p_keytable;
138 p_task = p_local->p_task;
140 if (p_task->p_keytable == NULL) {
142 p_task->p_keytable = ABTI_ktable_alloc(key_table_size);
144 p_ktable = p_task->p_keytable;
148 ABTI_ktable_set(p_ktable, p_key, value);
176 ABTI_local *p_local = ABTI_local_get_local();
177 ABTI_thread *p_thread;
179 ABTI_ktable *p_ktable = NULL;
182 ABTI_key *p_key = ABTI_key_get_ptr(key);
183 ABTI_CHECK_NULL_KEY_PTR(p_key);
186 ABTI_CHECK_INITIALIZED();
190 p_thread = p_local->p_thread;
192 p_ktable = p_thread->p_keytable;
195 keyval = ABTI_ktable_get(p_ktable, p_key);
198 p_task = p_local->p_task;
200 p_ktable = p_task->p_keytable;
203 keyval = ABTI_ktable_get(p_ktable, p_key);
217 ABTI_ktable *ABTI_ktable_alloc(
int size)
219 ABTI_ktable *p_ktable;
221 p_ktable = (ABTI_ktable *)
ABTU_malloc(
sizeof(ABTI_ktable));
222 p_ktable->size = size;
225 (ABTI_ktelem **)
ABTU_calloc(size,
sizeof(ABTI_ktelem *));
230 void ABTI_ktable_free(ABTI_ktable *p_ktable)
232 ABTI_ktelem *p_elem, *p_next;
237 for (i = 0; i < p_ktable->size; i++) {
238 p_elem = p_ktable->p_elems[i];
241 p_key = p_elem->p_key;
242 if (p_key->f_destructor && p_elem->value) {
243 p_key->f_destructor(p_elem->value);
245 refcount = ABTD_atomic_fetch_sub_uint32(&p_key->refcount, 1);
246 if (refcount == 1 && p_key->freed ==
ABT_TRUE) {
250 p_next = p_elem->p_next;
260 static inline uint32_t ABTI_ktable_get_idx(ABTI_key *p_key,
int size)
262 return p_key->id % size;
265 static inline void ABTI_ktable_set(ABTI_ktable *p_ktable, ABTI_key *p_key,
272 idx = ABTI_ktable_get_idx(p_key, p_ktable->size);
273 p_elem = p_ktable->p_elems[idx];
275 if (p_elem->p_key == p_key) {
276 p_elem->value = value;
279 p_elem = p_elem->p_next;
283 p_elem = (ABTI_ktelem *)
ABTU_malloc(
sizeof(ABTI_ktelem));
284 p_elem->p_key = p_key;
285 p_elem->value = value;
286 p_elem->p_next = p_ktable->p_elems[idx];
287 ABTD_atomic_fetch_add_uint32(&p_key->refcount, 1);
288 p_ktable->p_elems[idx] = p_elem;
293 static inline void *ABTI_ktable_get(ABTI_ktable *p_ktable, ABTI_key *p_key)
298 idx = ABTI_ktable_get_idx(p_key, p_ktable->size);
299 p_elem = p_ktable->p_elems[idx];
301 if (p_elem->p_key == p_key) {
302 return p_elem->value;
304 p_elem = p_elem->p_next;
310 void ABTI_ktable_delete(ABTI_ktable *p_ktable, ABTI_key *p_key)
313 ABTI_ktelem *p_prev = NULL;
316 idx = ABTI_ktable_get_idx(p_key, p_ktable->size);
317 p_elem = p_ktable->p_elems[idx];
319 if (p_elem->p_key == p_key) {
321 p_prev->p_next = p_elem->p_next;
323 p_ktable->p_elems[idx] = p_elem->p_next;
332 p_elem = p_elem->p_next;
static ABTD_atomic_uint32 g_key_id
static void * ABTU_malloc(size_t size)
int ABT_key_get(ABT_key key, void **value)
Get the value associated with the key.
int ABT_key_free(ABT_key *key)
Free an WU-specific data key.
struct ABT_key_opaque * ABT_key
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
ABTI_global * gp_ABTI_global
int ABT_key_create(void(*destructor)(void *value), ABT_key *newkey)
Create an WU-specific data key.
#define ABT_ERR_INV_XSTREAM
static void ABTU_free(void *ptr)
int ABT_key_set(ABT_key key, void *value)
Associate a value with the key.
static void * ABTU_calloc(size_t num, size_t size)