ARGOBOTS  1.1
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 {
68  ABTI_key *p_newkey;
69  int abt_errno = ABTU_malloc(sizeof(ABTI_key), (void **)&p_newkey);
70  ABTI_CHECK_ERROR(abt_errno);
71 
72  p_newkey->f_destructor = destructor;
73  p_newkey->id = ABTD_atomic_fetch_add_uint32(&g_key_id, 1);
74  /* Return value */
75  *newkey = ABTI_key_get_handle(p_newkey);
76  return ABT_SUCCESS;
77 }
78 
110 {
111  ABT_key h_key = *key;
112  ABTI_key *p_key = ABTI_key_get_ptr(h_key);
114  ABTU_free(p_key);
115 
116  /* Return value */
117  *key = ABT_KEY_NULL;
118  return ABT_SUCCESS;
119 }
120 
156 int ABT_key_set(ABT_key key, void *value)
157 {
158  ABTI_key *p_key = ABTI_key_get_ptr(key);
160 
161  ABTI_global *p_global;
162  ABTI_SETUP_GLOBAL(&p_global);
163 
164  ABTI_xstream *p_local_xstream;
165  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
166 
167  /* Obtain the key-value table pointer. */
168  int abt_errno =
169  ABTI_ktable_set(p_global, ABTI_xstream_get_local(p_local_xstream),
170  &p_local_xstream->p_thread->p_keytable, p_key, value);
171  ABTI_CHECK_ERROR(abt_errno);
172  return ABT_SUCCESS;
173 }
174 
211 int ABT_key_get(ABT_key key, void **value)
212 {
213  ABTI_key *p_key = ABTI_key_get_ptr(key);
215 
216  /* We don't allow an external thread to call this routine. */
217  ABTI_xstream *p_local_xstream;
218 #ifndef ABT_CONFIG_ENABLE_VER_20_API
219  ABTI_SETUP_GLOBAL(NULL);
220 #endif
221  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
222 
223  /* Obtain the key-value table pointer */
224  *value = ABTI_ktable_get(&p_local_xstream->p_thread->p_keytable, p_key);
225  return ABT_SUCCESS;
226 }
227 
228 /*****************************************************************************/
229 /* Private APIs */
230 /*****************************************************************************/
231 
232 void ABTI_ktable_free(ABTI_global *p_global, ABTI_local *p_local,
233  ABTI_ktable *p_ktable)
234 {
235  ABTI_ktelem *p_elem;
236  int i;
237 
238  for (i = 0; i < p_ktable->size; i++) {
239  p_elem =
241  while (p_elem) {
242  /* Call the destructor if it exists and the value is not null. */
243  if (p_elem->f_destructor && p_elem->value) {
244  p_elem->f_destructor(p_elem->value);
245  }
246  p_elem =
248  }
249  }
250  ABTI_ktable_mem_header *p_header =
251  (ABTI_ktable_mem_header *)p_ktable->p_used_mem;
252  while (p_header) {
253  ABTI_ktable_mem_header *p_next = p_header->p_next;
254  if (ABTU_likely(p_header->is_from_mempool)) {
255  ABTI_mem_free_desc(p_global, p_local, (void *)p_header);
256  } else {
257  ABTU_free(p_header);
258  }
259  p_header = p_next;
260  }
261 }
ABTI_key
Definition: abti.h:413
ABT_key
struct ABT_key_opaque * ABT_key
Work-unit-specific data key handle type.
Definition: abt.h:938
ABTI_SETUP_LOCAL_XSTREAM
#define ABTI_SETUP_LOCAL_XSTREAM(pp_local_xstream)
Definition: abti_error.h:73
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:59
ABT_KEY_NULL
#define ABT_KEY_NULL
Definition: abt.h:1065
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
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:211
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:109
ABTI_xstream
Definition: abti.h:264
ABTU_likely
#define ABTU_likely(cond)
Definition: abtu.h:111
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:232
ABTU_malloc
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:262
ABTI_ktelem::value
void * value
Definition: abti.h:422
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
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:68
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:426
ABTI_ktable::size
int size
Definition: abti.h:427
ABTI_ktelem
Definition: abti.h:418
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:217
ABTI_ktelem::f_destructor
void(* f_destructor)(void *value)
Definition: abti.h:420
ABTI_ktable::p_used_mem
void * p_used_mem
Definition: abti.h:429
ABTI_ktelem::p_next
ABTD_atomic_ptr p_next
Definition: abti.h:423
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
ABTI_ktable::p_elems
ABTD_atomic_ptr p_elems[1]
Definition: abti.h:432
ABTI_CHECK_NULL_KEY_PTR
#define ABTI_CHECK_NULL_KEY_PTR(p)
Definition: abti_error.h:231
ABTI_global
Definition: abti.h:196
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:358
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:156
ABTD_atomic_relaxed_load_ptr
static void * ABTD_atomic_relaxed_load_ptr(const ABTD_atomic_ptr *ptr)
Definition: abtd_atomic.h:846