1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
**********************************************************************************************
* File: maat_rhash.cpp
* Description:
* Authors: Liu WenTan <[email protected]>
* Date: 2022-10-31
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
***********************************************************************************************
*/
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/queue.h>
#include "rcu_hash.h"
#include "Maat_utils.h"
struct rcu_hash_garbage_bag {
void *garbage;
void (* garbage_free)(void *garbage);
TAILQ_ENTRY(rcu_hash_garbage_bag) entries;
};
TAILQ_HEAD(rcu_hash_garbage_q, rcu_hash_garbage_bag);
struct rcu_hash_table {
int is_updating;
char effective_hash; // 'a' or 'b'
/* two hash map for rcu */
struct rcu_hash_node *hashmap_a;
struct rcu_hash_node *hashmap_b;
void (*data_free_fn)(void *user_ctx, void *data);
void *user_ctx;
struct rcu_hash_garbage_q garbage_q;
size_t garbage_q_len;
pthread_mutex_t update_mutex;
};
struct rcu_hash_node {
char *key;
size_t key_len;
void *data; //table_runtime解析成两个成员
/* htable the node belongs to */
struct rcu_hash_table *htable;
UT_hash_handle hh_a;
UT_hash_handle hh_b;
};
void rcu_hash_garbage_queue_free(struct rcu_hash_garbage_q* garbage_q)
{
struct rcu_hash_garbage_bag *p = NULL;
while ((p = TAILQ_FIRST(garbage_q)) != NULL) {
p->garbage_free(p->garbage);
TAILQ_REMOVE(garbage_q, p, entries);
free(p);
p = NULL;
}
}
size_t rcu_hash_garbage_queue_len(struct rcu_hash_table *htable)
{
return htable->garbage_q_len;
}
void rcu_hash_garbage_bagging(struct rcu_hash_garbage_q* garbage_q, void* garbage, void (* func)(void *))
{
struct rcu_hash_garbage_bag *bag = ALLOC(struct rcu_hash_garbage_bag, 1);
bag->garbage = garbage;
bag->garbage_free = func;
TAILQ_INSERT_TAIL(garbage_q, bag, entries);
}
void rcu_hash_node_free(struct rcu_hash_node *node)
{
if (NULL == node) {
return;
}
if (node->key != NULL) {
free(node->key);
node->key = NULL;
}
if (node->data != NULL) {
node->htable->data_free_fn(node->htable->user_ctx, node->data);
}
free(node);
node = NULL;
}
struct rcu_hash_table *rcu_hash_new(rcu_hash_data_free_fn *free_fn)
{
if (NULL == free_fn) {
return NULL;
}
struct rcu_hash_table *htable = ALLOC(struct rcu_hash_table, 1);
htable->is_updating = 0;
htable->effective_hash = 'a';
TAILQ_INIT(&htable->garbage_q);
htable->garbage_q_len = 0;
htable->data_free_fn = free_fn;
pthread_mutex_init(&htable->update_mutex, NULL);
return htable;
}
void rcu_hash_free(struct rcu_hash_table *htable)
{
if (NULL == htable) {
return;
}
struct rcu_hash_node *tmp = NULL;
struct rcu_hash_node *item = NULL;
if (htable->effective_hash == 'a') {
HASH_ITER(hh_a, htable->hashmap_a, item, tmp) {
HASH_DELETE(hh_a, htable->hashmap_a, item);
rcu_hash_node_free(item);
}
} else {
HASH_ITER(hh_b, htable->hashmap_b, item, tmp) {
HASH_DELETE(hh_b, htable->hashmap_b, item);
rcu_hash_node_free(item);
}
}
rcu_hash_garbage_queue_free(&(htable->garbage_q));
if (htable->user_ctx != NULL) {
free(htable->user_ctx);
htable->user_ctx = NULL;
}
pthread_mutex_destroy(&htable->update_mutex);
free(htable);
htable = NULL;
}
void rcu_hash_set_user_ctx(struct rcu_hash_table *htable, void *user_ctx)
{
htable->user_ctx = user_ctx;
}
void *rcu_hash_get_user_ctx(struct rcu_hash_table *htable)
{
return htable->user_ctx;
}
void rcu_hash_commit_prepare(struct rcu_hash_table *htable)
{
struct rcu_hash_node *node = NULL;
struct rcu_hash_node *tmp = NULL;
if (htable->effective_hash == 'a') {
assert(htable->hashmap_b == NULL);
HASH_ITER(hh_a, htable->hashmap_a, node, tmp) {
HASH_ADD_KEYPTR(hh_b, htable->hashmap_b, node->key, node->key_len, node);
}
} else {
assert(htable->hashmap_a == NULL);
HASH_ITER(hh_b, htable->hashmap_b, node, tmp) {
HASH_ADD_KEYPTR(hh_a, htable->hashmap_a, node->key, node->key_len, node);
}
}
htable->is_updating = 1;
}
void rcu_hash_add(struct rcu_hash_table *htable, const char *key, size_t key_len, void *data)
{
if (NULL == htable || NULL == key || 0 == key_len) {
return;
}
struct rcu_hash_node *tmp = NULL;
struct rcu_hash_node *node = ALLOC(struct rcu_hash_node, 1);
node->key = (char *)malloc(sizeof(char) * key_len);
memcpy(node->key, key, key_len);
node->key_len = key_len;
node->data = data;
node->htable = htable;
if (!htable->is_updating) {
rcu_hash_commit_prepare(htable);
}
if (htable->effective_hash == 'a') {
HASH_FIND(hh_b, htable->hashmap_b, key, key_len, tmp);
if (NULL == tmp) {
HASH_ADD_KEYPTR(hh_b, htable->hashmap_b, node->key, node->key_len, node);
}
} else {
HASH_FIND(hh_a, htable->hashmap_a, key, key_len, tmp);
if (NULL == tmp) {
HASH_ADD_KEYPTR(hh_a, htable->hashmap_a, node->key, node->key_len, node);
}
}
}
void rcu_hash_del(struct rcu_hash_table *htable, const char *key, size_t key_len)
{
if (NULL == htable || NULL == key || 0 == key_len) {
return;
}
if (!htable->is_updating) {
rcu_hash_commit_prepare(htable);
}
struct rcu_hash_node *node = NULL;
if (htable->effective_hash == 'a') {
HASH_FIND(hh_b, htable->hashmap_b, key, key_len, node);
if (node != NULL) {
HASH_DELETE(hh_b, htable->hashmap_b, node);
}
} else {
HASH_FIND(hh_a, htable->hashmap_a, key, key_len, node);
if (node != NULL) {
HASH_DELETE(hh_a, htable->hashmap_a, node);
}
}
if (node != NULL) {
rcu_hash_garbage_bagging(&(htable->garbage_q), node, (void (*)(void*))rcu_hash_node_free);
htable->garbage_q_len++;
}
}
void *rcu_hash_find(struct rcu_hash_table *htable, const char *key, size_t key_len)
{
if (NULL == htable || NULL == key || 0 == key_len) {
return NULL;
}
struct rcu_hash_node *node = NULL;
if (htable->effective_hash == 'a') {
HASH_FIND(hh_a, htable->hashmap_a, key, key_len, node);
if (node != NULL) {
return node->data;
}
} else {
HASH_FIND(hh_b, htable->hashmap_b, key, key_len, node);
if (node != NULL) {
return node->data;
}
}
return NULL;
}
size_t rcu_hash_count(struct rcu_hash_table *htable)
{
if (NULL == htable) {
return 0;
}
if (htable->effective_hash == 'a') {
return HASH_CNT(hh_a, htable->hashmap_a);
} else {
return HASH_CNT(hh_b, htable->hashmap_b);
}
}
int rcu_hash_is_updating(struct rcu_hash_table *htable)
{
if (NULL == htable) {
return 0;
}
return htable->is_updating;
}
void rcu_hash_commit(struct rcu_hash_table *htable)
{
if (NULL == htable) {
return;
}
if (!htable->is_updating) {
return;
}
struct rcu_hash_node *node = NULL;
struct rcu_hash_node *tmp = NULL;
pthread_mutex_lock(&htable->update_mutex);
if (!htable->is_updating) {
pthread_mutex_unlock(&htable->update_mutex);
return;
}
/* updating hash_map is ready, so change effective hash_map */
if (htable->effective_hash == 'a') {
htable->effective_hash = 'b';
usleep(100);
HASH_ITER(hh_a, htable->hashmap_a, node, tmp) {
HASH_DELETE(hh_a, htable->hashmap_a, node);
}
} else {
htable->effective_hash = 'a';
usleep(100);
HASH_ITER(hh_b, htable->hashmap_b, node, tmp) {
HASH_DELETE(hh_b, htable->hashmap_b, node);
}
}
htable->is_updating = 0;
rcu_hash_garbage_queue_free(&(htable->garbage_q));
htable->garbage_q_len = 0;
pthread_mutex_unlock(&htable->update_mutex);
}
size_t rcu_hash_list(struct rcu_hash_table *htable, void ***data_array)
{
size_t i = 0;
size_t node_cnt = 0;
struct rcu_hash_node *node = NULL, *tmp = NULL;
if (htable->effective_hash == 'a') {
node_cnt = HASH_CNT(hh_a, htable->hashmap_a);
*data_array = ALLOC(void *, node_cnt);
HASH_ITER(hh_a, htable->hashmap_a, node, tmp) {
(*data_array)[i] = node->data;
i++;
}
} else {
node_cnt = HASH_CNT(hh_b, htable->hashmap_b);
*data_array = ALLOC(void *, node_cnt);
HASH_ITER(hh_b, htable->hashmap_b, node, tmp) {
(*data_array)[i] = node->data;
i++;
}
}
return node_cnt;
}
|