ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
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 
15 
66 int ABT_key_create(void (*destructor)(void *value), ABT_key *newkey)
67 {
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 {
115  ABTI_UB_ASSERT(key);
116 
117  ABT_key h_key = *key;
118  ABTI_key *p_key = ABTI_key_get_ptr(h_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
166 #endif
167 
168  ABTI_key *p_key = ABTI_key_get_ptr(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
226 #endif
227 
228  ABTI_key *p_key = ABTI_key_get_ptr(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 =
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 =
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 }
ABTI_key
Definition: abti.h:461
ABT_key
struct ABT_key_opaque * ABT_key
Work-unit-specific data key handle type.
Definition: abt.h:980
ABTI_SETUP_LOCAL_XSTREAM
#define ABTI_SETUP_LOCAL_XSTREAM(pp_local_xstream)
Definition: abti_error.h:89
ABTI_ktable_get
static void * ABTI_ktable_get(ABTD_atomic_ptr *pp_ktable, ABTI_key *p_key)
Definition: abti_key.h:287
ABTI_key_get_ptr
static ABTI_key * ABTI_key_get_ptr(ABT_key key)
Definition: abti_key.h:11
ABTI_SETUP_GLOBAL
#define ABTI_SETUP_GLOBAL(pp_global)
Definition: abti_error.h:75
ABT_KEY_NULL
#define ABT_KEY_NULL
Definition: abt.h:1108
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:136
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
ABTI_KEY_ID_END_
#define ABTI_KEY_ID_END_
Definition: abti_key.h:49
ABTI_ktable_mem_header
Definition: abti_key.h:51
ABT_key_free
int ABT_key_free(ABT_key *key)
Free a work-unit-specific data key.
Definition: key.c:112
ABTI_xstream
Definition: abti.h:294
ABTU_likely
#define ABTU_likely(cond)
Definition: abtu.h:119
ABTD_ATOMIC_UINT32_STATIC_INITIALIZER
#define ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(val)
Definition: abtd_atomic.h:59
g_key_id
static ABTD_atomic_uint32 g_key_id
Definition: key.c:13
ABTI_ktable_set
static ABTU_ret_err int ABTI_ktable_set(ABTI_global *p_global, ABTI_local *p_local, ABTD_atomic_ptr *pp_ktable, ABTI_key *p_key, void *value)
Definition: abti_key.h:225
ABTI_ktable_mem_header::p_next
struct ABTI_ktable_mem_header * p_next
Definition: abti_key.h:52
abti.h
ABTI_ktable_free
void ABTI_ktable_free(ABTI_global *p_global, ABTI_local *p_local, ABTI_ktable *p_ktable)
Definition: key.c:247
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:235
ABTI_ktelem::value
void * value
Definition: abti.h:470
ABTD_atomic_uint32
Definition: abtd_atomic.h:27
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
ABTI_ktable_mem_header::is_from_mempool
ABT_bool is_from_mempool
Definition: abti_key.h:53
ABTI_initialized
ABT_bool ABTI_initialized(void)
Definition: global.c:187
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTI_key_get_handle
static ABT_key ABTI_key_get_handle(ABTI_key *p_key)
Definition: abti_key.h:26
ABTI_xstream_get_local
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:48
ABTD_atomic_fetch_add_uint32
static uint32_t ABTD_atomic_fetch_add_uint32(ABTD_atomic_uint32 *ptr, uint32_t v)
Definition: abtd_atomic.h:457
ABTI_ktable
Definition: abti.h:474
ABTI_ktable::size
int size
Definition: abti.h:475
ABTI_UB_ASSERT
#define ABTI_UB_ASSERT(cond)
Definition: abti_error.h:19
ABTI_ktelem
Definition: abti.h:466
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
ABTI_ktelem::f_destructor
void(* f_destructor)(void *value)
Definition: abti.h:468
ABTI_ktable::p_used_mem
void * p_used_mem
Definition: abti.h:477
ABTI_ktelem::p_next
ABTD_atomic_ptr p_next
Definition: abti.h:471
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:132
ABTI_ktable::p_elems
ABTD_atomic_ptr p_elems[1]
Definition: abti.h:480
ABTI_CHECK_NULL_KEY_PTR
#define ABTI_CHECK_NULL_KEY_PTR(p)
Definition: abti_error.h:265
ABTI_global
Definition: abti.h:223
ABTI_mem_free_desc
static void ABTI_mem_free_desc(ABTI_global *p_global, ABTI_local *p_local, void *p_desc)
Definition: abti_mem.h:531
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
ABTD_atomic_relaxed_load_ptr
static void * ABTD_atomic_relaxed_load_ptr(const ABTD_atomic_ptr *ptr)
Definition: abtd_atomic.h:846