ARGOBOTS  4dc37e16e1b227a480715ab67dae1dcfb4d2d4e0
key.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
13 static ABTD_atomic_uint32 g_key_id =
14  ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(ABTI_KEY_ID_END_);
15 
66 int ABT_key_create(void (*destructor)(void *value), ABT_key *newkey)
67 {
68  ABTI_UB_ASSERT(ABTI_initialized());
69  ABTI_UB_ASSERT(newkey);
70 
71  ABTI_key *p_newkey;
72  int abt_errno = ABTU_malloc(sizeof(ABTI_key), (void **)&p_newkey);
73  ABTI_CHECK_ERROR(abt_errno);
74 
75  p_newkey->f_destructor = destructor;
76  p_newkey->id = ABTD_atomic_fetch_add_uint32(&g_key_id, 1);
77  /* Return value */
78  *newkey = ABTI_key_get_handle(p_newkey);
79  return ABT_SUCCESS;
80 }
81 
113 {
114  ABTI_UB_ASSERT(ABTI_initialized());
115  ABTI_UB_ASSERT(key);
116 
117  ABT_key h_key = *key;
118  ABTI_key *p_key = ABTI_key_get_ptr(h_key);
119  ABTI_CHECK_NULL_KEY_PTR(p_key);
120  ABTU_free(p_key);
121 
122  /* Return value */
123  *key = ABT_KEY_NULL;
124  return ABT_SUCCESS;
125 }
126 
162 int ABT_key_set(ABT_key key, void *value)
163 {
164 #ifdef ABT_CONFIG_ENABLE_VER_20_API
165  ABTI_UB_ASSERT(ABTI_initialized());
166 #endif
167 
168  ABTI_key *p_key = ABTI_key_get_ptr(key);
169  ABTI_CHECK_NULL_KEY_PTR(p_key);
170 
171  ABTI_global *p_global;
172  ABTI_SETUP_GLOBAL(&p_global);
173 
174  ABTI_xstream *p_local_xstream;
175  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
176 
177  /* Obtain the key-value table pointer. */
178  int abt_errno =
179  ABTI_ktable_set(p_global, ABTI_xstream_get_local(p_local_xstream),
180  &p_local_xstream->p_thread->p_keytable, p_key, value);
181  ABTI_CHECK_ERROR(abt_errno);
182  return ABT_SUCCESS;
183 }
184 
221 int ABT_key_get(ABT_key key, void **value)
222 {
223  ABTI_UB_ASSERT(value);
224 #ifdef ABT_CONFIG_ENABLE_VER_20_API
225  ABTI_UB_ASSERT(ABTI_initialized());
226 #endif
227 
228  ABTI_key *p_key = ABTI_key_get_ptr(key);
229  ABTI_CHECK_NULL_KEY_PTR(p_key);
230 
231  /* We don't allow an external thread to call this routine. */
232  ABTI_xstream *p_local_xstream;
233 #ifndef ABT_CONFIG_ENABLE_VER_20_API
234  ABTI_SETUP_GLOBAL(NULL);
235 #endif
236  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
237 
238  /* Obtain the key-value table pointer */
239  *value = ABTI_ktable_get(&p_local_xstream->p_thread->p_keytable, p_key);
240  return ABT_SUCCESS;
241 }
242 
243 /*****************************************************************************/
244 /* Private APIs */
245 /*****************************************************************************/
246 
247 void ABTI_ktable_free(ABTI_global *p_global, ABTI_local *p_local,
248  ABTI_ktable *p_ktable)
249 {
250  ABTI_ktelem *p_elem;
251  int i;
252 
253  for (i = 0; i < p_ktable->size; i++) {
254  p_elem =
255  (ABTI_ktelem *)ABTD_atomic_relaxed_load_ptr(&p_ktable->p_elems[i]);
256  while (p_elem) {
257  /* Call the destructor if it exists and the value is not null. */
258  if (p_elem->f_destructor && p_elem->value) {
259  p_elem->f_destructor(p_elem->value);
260  }
261  p_elem =
262  (ABTI_ktelem *)ABTD_atomic_relaxed_load_ptr(&p_elem->p_next);
263  }
264  }
265  ABTI_ktable_mem_header *p_header =
266  (ABTI_ktable_mem_header *)p_ktable->p_used_mem;
267  while (p_header) {
268  ABTI_ktable_mem_header *p_next = p_header->p_next;
269  if (ABTU_likely(p_header->is_from_mempool)) {
270  ABTI_mem_free_desc(p_global, p_local, (void *)p_header);
271  } else {
272  ABTU_free(p_header);
273  }
274  p_header = p_next;
275  }
276 }
ABT_key
struct ABT_key_opaque * ABT_key
Work-unit-specific data key handle type.
Definition: abt.h:980
ABT_KEY_NULL
#define ABT_KEY_NULL
Definition: abt.h:1108
ABT_key_get
int ABT_key_get(ABT_key key, void **value)
Get a value associated with a work-unit-specific data key in the calling work unit.
Definition: key.c:221
ABT_key_free
int ABT_key_free(ABT_key *key)
Free a work-unit-specific data key.
Definition: key.c:112
ABTU_likely
#define ABTU_likely(cond)
Definition: abtu.h:119
g_key_id
static ABTD_atomic_uint32 g_key_id
Definition: key.c:13
abti.h
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
ABT_key_create
int ABT_key_create(void(*destructor)(void *value), ABT_key *newkey)
Create a new work-unit-specific data key.
Definition: key.c:66
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
ABT_key_set
int ABT_key_set(ABT_key key, void *value)
Associate a value with a work-unit-specific data key in the calling work unit.
Definition: key.c:162