ARGOBOTS  1059a7c2eb7e3f99f736a9c3a4f6ea476ac1b804
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 
42 int ABT_key_create(void (*destructor)(void *value), ABT_key *newkey)
43 {
44  ABTI_key *p_newkey;
45  int abt_errno = ABTU_malloc(sizeof(ABTI_key), (void **)&p_newkey);
46  ABTI_CHECK_ERROR(abt_errno);
47 
48  p_newkey->f_destructor = destructor;
49  p_newkey->id = ABTD_atomic_fetch_add_uint32(&g_key_id, 1);
50  /* Return value */
51  *newkey = ABTI_key_get_handle(p_newkey);
52  return ABT_SUCCESS;
53 }
54 
69 {
70  ABT_key h_key = *key;
71  ABTI_key *p_key = ABTI_key_get_ptr(h_key);
73  ABTU_free(p_key);
74 
75  /* Return value */
76  *key = ABT_KEY_NULL;
77  return ABT_SUCCESS;
78 }
79 
93 int ABT_key_set(ABT_key key, void *value)
94 {
95  ABTI_key *p_key = ABTI_key_get_ptr(key);
97 
98  ABTI_xstream *p_local_xstream;
99  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
100 
101  /* Obtain the key-value table pointer. */
102  int abt_errno =
103  ABTI_ktable_set(ABTI_xstream_get_local(p_local_xstream),
104  &p_local_xstream->p_thread->p_keytable, p_key, value);
105  ABTI_CHECK_ERROR(abt_errno);
106  return ABT_SUCCESS;
107 }
108 
125 int ABT_key_get(ABT_key key, void **value)
126 {
127  ABTI_key *p_key = ABTI_key_get_ptr(key);
129 
130  /* We don't allow an external thread to call this routine. */
131  ABTI_xstream *p_local_xstream;
132  ABTI_SETUP_LOCAL_XSTREAM(&p_local_xstream);
133 
134  /* Obtain the key-value table pointer */
135  *value = ABTI_ktable_get(&p_local_xstream->p_thread->p_keytable, p_key);
136  return ABT_SUCCESS;
137 }
138 
139 void ABTI_ktable_free(ABTI_local *p_local, ABTI_ktable *p_ktable)
140 {
141  ABTI_ktelem *p_elem;
142  int i;
143 
144  for (i = 0; i < p_ktable->size; i++) {
145  p_elem =
147  while (p_elem) {
148  /* Call the destructor if it exists and the value is not null. */
149  if (p_elem->f_destructor && p_elem->value) {
150  p_elem->f_destructor(p_elem->value);
151  }
152  p_elem =
154  }
155  }
156  ABTI_ktable_mem_header *p_header =
157  (ABTI_ktable_mem_header *)p_ktable->p_used_mem;
158  while (p_header) {
159  ABTI_ktable_mem_header *p_next = p_header->p_next;
160  if (ABTU_likely(p_header->is_from_mempool)) {
161  ABTI_mem_free_desc(p_local, (void *)p_header);
162  } else {
163  ABTU_free(p_header);
164  }
165  p_header = p_next;
166  }
167 }
static void ABTI_mem_free_desc(ABTI_local *p_local, void *p_desc)
Definition: abti_mem.h:326
static void * ABTI_ktable_get(ABTD_atomic_ptr *pp_ktable, ABTI_key *p_key)
Definition: abti_key.h:283
void * value
Definition: abti.h:363
#define ABTU_likely(cond)
Definition: abtu.h:17
int size
Definition: abti.h:368
struct ABTI_local ABTI_local
Definition: abti.h:101
ABTI_thread * p_thread
Definition: abti.h:251
#define ABTI_KEY_ID_END_
Definition: abti_key.h:49
static ABTI_key * ABTI_key_get_ptr(ABT_key key)
Definition: abti_key.h:11
static ABTD_atomic_uint32 g_key_id
Definition: key.c:13
static void * ABTD_atomic_relaxed_load_ptr(const ABTD_atomic_ptr *ptr)
Definition: abtd_atomic.h:731
#define ABT_KEY_NULL
Definition: abt.h:418
Definition: abti.h:354
void ABTI_ktable_free(ABTI_local *p_local, ABTI_ktable *p_ktable)
Definition: key.c:139
static ABT_key ABTI_key_get_handle(ABTI_key *p_key)
Definition: abti_key.h:26
int ABT_key_get(ABT_key key, void **value) ABT_API_PUBLIC
Get the value associated with the key.
Definition: key.c:125
uint32_t id
Definition: abti.h:356
#define ABTI_SETUP_LOCAL_XSTREAM(pp_local_xstream)
Definition: abti_error.h:68
void * p_used_mem
Definition: abti.h:370
int ABT_key_free(ABT_key *key) ABT_API_PUBLIC
Free an WU-specific data key.
Definition: key.c:68
struct ABT_key_opaque * ABT_key
Definition: abt.h:355
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:129
static uint32_t ABTD_atomic_fetch_add_uint32(ABTD_atomic_uint32 *ptr, uint32_t v)
Definition: abtd_atomic.h:391
void(* f_destructor)(void *value)
Definition: abti.h:361
struct ABTI_ktable_mem_header * p_next
Definition: abti_key.h:52
ABTD_atomic_ptr p_next
Definition: abti.h:364
#define ABT_SUCCESS
Definition: abt.h:64
static ABTU_ret_err int ABTI_ktable_set(ABTI_local *p_local, ABTD_atomic_ptr *pp_ktable, ABTI_key *p_key, void *value)
Definition: abti_key.h:224
int ABT_key_create(void(*destructor)(void *value), ABT_key *newkey) ABT_API_PUBLIC
Create an WU-specific data key.
Definition: key.c:42
ABTD_atomic_ptr p_elems[1]
Definition: abti.h:373
void(* f_destructor)(void *value)
Definition: abti.h:355
ABT_bool is_from_mempool
Definition: abti_key.h:53
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:127
ABTD_atomic_ptr p_keytable
Definition: abti.h:325
static ABTI_local * ABTI_xstream_get_local(ABTI_xstream *p_xstream)
Definition: abti_stream.h:67
static void ABTU_free(void *ptr)
Definition: abtu.h:122
#define ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(val)
Definition: abtd_atomic.h:51
int ABT_key_set(ABT_key key, void *value) ABT_API_PUBLIC
Associate a value with the key.
Definition: key.c:93
#define ABTI_CHECK_NULL_KEY_PTR(p)
Definition: abti_error.h:229