ARGOBOTS  c6511494322293e01714f56f341b8d2b22c1e3c1
 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 
13 static ABTD_atomic_uint32 g_key_id =
14  ABTD_ATOMIC_UINT32_STATIC_INITIALIZER(ABTI_KEY_ID_END_);
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);
72  ABTI_CHECK_NULL_KEY_PTR(p_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);
96  ABTI_CHECK_NULL_KEY_PTR(p_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);
128  ABTI_CHECK_NULL_KEY_PTR(p_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 =
146  (ABTI_ktelem *)ABTD_atomic_relaxed_load_ptr(&p_ktable->p_elems[i]);
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 =
153  (ABTI_ktelem *)ABTD_atomic_relaxed_load_ptr(&p_elem->p_next);
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 }
#define ABTU_likely(cond)
Definition: abtu.h:17
static ABTD_atomic_uint32 g_key_id
Definition: key.c:13
#define ABT_KEY_NULL
Definition: abt.h:418
int ABT_key_get(ABT_key key, void **value) ABT_API_PUBLIC
Get the value associated with the key.
Definition: key.c:125
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
#define ABT_SUCCESS
Definition: abt.h:64
int ABT_key_create(void(*destructor)(void *value), ABT_key *newkey) ABT_API_PUBLIC
Create an WU-specific data key.
Definition: key.c:42
static void ABTU_free(void *ptr)
Definition: abtu.h:122
int ABT_key_set(ABT_key key, void *value) ABT_API_PUBLIC
Associate a value with the key.
Definition: key.c:93