-
Notifications
You must be signed in to change notification settings - Fork 42
/
concurrent_hash_map.c
394 lines (336 loc) · 8.65 KB
/
concurrent_hash_map.c
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* File: concurrent_hash_map2.c
* Author: Vasileios Trigonakis <vasileios.trigonakis@epfl.ch>
* Description: Similar to Java's ConcurrentHashMap.
* Doug Lea. 1.3.4. http://gee.cs.oswego.edu/dl/classes/EDU/oswego/
* cs/dl/util/concurrent/intro.html, 2003.
* concurrent_hash_map2.c is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <vasileios.trigonakis@epfl.ch>,
* Tudor David <tudor.david@epfl.ch>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "concurrent_hash_map.h"
RETRY_STATS_VARS;
__thread ssmem_allocator_t* alloc = NULL;
/* ********************************************************************************
* help functions
******************************************************************************** */
static int
floor_log_2(unsigned int n)
{
int pos = 0;
if (n >= 1<<16) { n >>= 16; pos += 16; }
if (n >= 1<< 8) { n >>= 8; pos += 8; }
if (n >= 1<< 4) { n >>= 4; pos += 4; }
if (n >= 1<< 2) { n >>= 2; pos += 2; }
if (n >= 1<< 1) { pos += 1; }
return ((n == 0) ? (-1) : pos);
}
static inline int
hash(skey_t key, int hash_seed)
{
return (key >> hash_seed);
}
/* ********************************************************************************
* create functions
******************************************************************************** */
static chm_seg_t*
chm_seg_new(size_t capacity, float load_factor)
{
chm_seg_t* seg = memalign(CACHE_LINE_SIZE, sizeof(chm_seg_t));
assert(seg != NULL);
seg->table = memalign(CACHE_LINE_SIZE, capacity * sizeof(chm_node_t*));
assert(seg->table != NULL);
seg->num_buckets = capacity;
seg->hash = capacity - 1;
seg->modifications = 0;
seg->size = 0;
seg->load_factor = load_factor;
seg->size_limit = seg->load_factor * capacity;
if (seg->size_limit == 0)
{
seg->size_limit = 1;
}
int i;
for (i = 0; i < seg->num_buckets; i++)
{
seg->table[i] = NULL;
}
optik_init(&seg->lock);
return seg;
}
chm_t*
chm_new(size_t capacity, size_t num_segments)
{
chm_t* chm = memalign(CACHE_LINE_SIZE, sizeof(chm_t));
assert(chm != NULL);
if (capacity < num_segments)
{
capacity = num_segments;
}
chm->num_segments = num_segments;
chm->segments = memalign(CACHE_LINE_SIZE, chm->num_segments * sizeof(chm_seg_t*));
assert(chm->segments != NULL);
chm->hash = chm->num_segments - 1;
int hs = floor_log_2(chm->num_segments);
chm->hash_seed = hs;
assert(capacity % chm->num_segments == 0);
size_t capacity_seg = capacity / chm->num_segments;
#if defined(DEBUG)
printf("#seg: %5zu, #bu/seg: %zu\n", chm->num_segments, capacity_seg);
#endif
int s;
for (s = 0; s < chm->num_segments; s++)
{
chm->segments[s] = chm_seg_new(capacity_seg, CHM_LOAD_FACTOR);
}
return chm;
}
static chm_node_t*
chm_node_new(skey_t key, sval_t val, chm_node_t* next)
{
volatile chm_node_t* node;
#if GC == 1
node = (volatile chm_node_t*) ssmem_alloc(alloc, sizeof(chm_node_t));
#else
node = (volatile chm_node_t*) ssalloc(sizeof(chm_node_t));
#endif
if (node == NULL)
{
perror("malloc @ new_node");
exit(1);
}
node->key = key;
node->val = val;
node->next = next;
#if defined(__tile__)
/* on tilera you may have store reordering causing the pointer to a new node
to become visible, before the contents of the node are visible */
MEM_BARRIER;
#endif /* __tile__ */
return (chm_node_t*) node;
}
void
chm_seg_rehash(chm_t* set, int seg_num, chm_node_t* new)
{
chm_seg_t** seg_oldp = set->segments + seg_num;
chm_seg_t* seg_old = *seg_oldp;
chm_seg_t* seg_new = chm_seg_new(seg_old->num_buckets << 1, seg_old->load_factor);
int mask_new = seg_new->hash;
int b;
for (b = 0; b < seg_old->num_buckets; b++)
{
chm_node_t* curr = seg_old->table[b];
if (curr != NULL)
{
chm_node_t* next = curr->next;
int idx = hash(curr->key, set->hash_seed) & mask_new;
if (next == NULL) /* single node on list */
{
seg_new->table[idx] = curr;
}
else
{ /* reuse consecutive sequence at same slot */
chm_node_t* last_run = curr;
int last_idx = idx;
chm_node_t* last;
for (last = next; last != NULL; last = last->next)
{
int k = hash(last->key, set->hash_seed) & mask_new;
if (k != last_idx)
{
last_idx = k;
last_run = last;
}
}
seg_new->table[last_idx] = last_run;
/* clone remaining */
chm_node_t* p;
for (p = curr; p != last_run; p = p->next)
{
int k = hash(p->key, set->hash_seed) & mask_new;
chm_node_t* n = chm_node_new(p->key, p->val, seg_new->table[k]);
seg_new->table[k] = n;
#if GC == 1
ssmem_free(alloc, (void*) p);
#endif
}
}
}
}
int new_idx = hash(new->key, set->hash_seed) & mask_new; /* add the new node */
new->next = seg_new->table[new_idx];
seg_new->table[new_idx] = new;
seg_new->size = seg_old->size + 1;
set->segments[seg_num] = seg_new;
#if GC == 1
ssmem_release(alloc, (void*) seg_old);
ssmem_release(alloc, seg_old->table);
#endif
}
/* ********************************************************************************
* hash table interface
******************************************************************************** */
sval_t
chm_get(chm_t* set, skey_t key)
{
PARSE_TRY();
chm_seg_t* seg = set->segments[key & set->hash];
chm_node_t** bucket = &seg->table[hash(key, set->hash_seed) & seg->hash];
chm_node_t* curr = *bucket;
while (curr != NULL)
{
if (curr->key == key)
{
return curr->val;
}
curr = curr->next;
}
return 0;
}
static inline int
chm_contains(chm_t* set, chm_seg_t* seg, skey_t key)
{
PARSE_TRY();
chm_node_t** bucket = &seg->table[hash(key, set->hash_seed) & seg->hash];
chm_node_t* curr = *bucket;
while (curr != NULL)
{
if (curr->key == key)
{
return 1;
}
curr = curr->next;
}
return 0;
}
int
chm_put(chm_t* set, skey_t key, sval_t val)
{
PARSE_TRY();
UPDATE_TRY();
while (1)
{
int seg_num = key & set->hash;
volatile chm_seg_t* seg = set->segments[seg_num];
optik_t* seg_lock = &seg->lock;
COMPILER_NO_REORDER(optik_t version = *seg_lock;);
chm_node_t** bucket = &seg->table[hash(key, set->hash_seed) & seg->hash];
chm_node_t* curr = *bucket;
chm_node_t* pred = NULL;
while (curr != NULL)
{
if (curr->key == key)
{
return 0;
}
pred = curr;
curr = curr->next;
}
if (unlikely(!optik_trylock_version(seg_lock, version)))
{
continue;
}
chm_node_t* n = chm_node_new(key, val, NULL);
uint32_t sizepp = seg->size + 1;
if (unlikely(sizepp >= seg->size_limit))
{
#if defined(DEBUG)
printf("-[%3d]- seg size limit %u :: resize\n", seg_num, seg->size_limit);
#endif
chm_seg_rehash(set, seg_num, n);
}
else
{
if (pred != NULL)
{
pred->next = n;
}
else
{
*bucket = n;
}
seg->size = sizepp;
optik_unlock(seg_lock);
}
return 1;
}
}
sval_t
chm_rem(chm_t* set, skey_t key)
{
PARSE_TRY();
UPDATE_TRY();
restart:
{
volatile int seg_num = key & set->hash;
volatile chm_seg_t* seg = set->segments[seg_num];
optik_t* seg_lock = &seg->lock;
COMPILER_NO_REORDER(optik_t version = *seg_lock;);
chm_node_t** bucket = &seg->table[hash(key, set->hash_seed) & seg->hash];
chm_node_t* curr = *bucket;
chm_node_t* pred = NULL;
while (curr != NULL)
{
if (curr->key == key)
{
if (unlikely(!optik_trylock_version(seg_lock, version)))
{
goto restart;
}
/* do the remove */
if (pred != NULL)
{
pred->next = curr->next;
}
else
{
*bucket = curr->next;
}
#if GC == 1
ssmem_free(alloc, (void*) curr);
#endif
seg->size--;
optik_unlock(seg_lock);
return curr->val;
}
pred = curr;
curr = curr->next;
}
}
return 0;
}
/* not linearizable */
size_t
chm_size(chm_t* set)
{
size_t size = 0;
int s;
for (s = 0; s < set->num_segments; s++)
{
chm_seg_t* seg = set->segments[s];
int i;
for (i = 0; i < seg->num_buckets; i++)
{
chm_node_t* curr = seg->table[i];
while (curr != NULL)
{
size++;
curr = curr->next;
}
}
}
return size;
}