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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
/* Copyright (C) 2000-2019 Red Hat, Inc.
This file is part of elfutils.
Written by Srdan Milakovic <sm108@rice.edu>, 2019.
Derived from Ulrich Drepper <drepper@redhat.com>, 2000.
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
elfutils 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.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>. */
#include <assert.h>
#include <stdlib.h>
#include <system.h>
#include <pthread.h>
/* Before including this file the following macros must be defined:
NAME name of the hash table structure.
TYPE data type of the hash table entries
*/
static size_t
lookup (NAME *htab, HASHTYPE hval)
{
/* First hash function: simply take the modul but prevent zero. Small values
can skip the division, which helps performance when this is common. */
size_t idx = 1 + (hval < htab->size ? hval : hval % htab->size);
HASHTYPE hash;
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
if (hash == hval)
return idx;
else if (hash == 0)
return 0;
/* Second hash function as suggested in [Knuth]. */
HASHTYPE second_hash = 1 + hval % (htab->size - 2);
for(;;)
{
if (idx <= second_hash)
idx = htab->size + idx - second_hash;
else
idx -= second_hash;
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
if (hash == hval)
return idx;
else if (hash == 0)
return 0;
}
}
static int
insert_helper (NAME *htab, HASHTYPE hval, TYPE val)
{
/* First hash function: simply take the modul but prevent zero. Small values
can skip the division, which helps performance when this is common. */
size_t idx = 1 + (hval < htab->size ? hval : hval % htab->size);
TYPE val_ptr;
HASHTYPE hash;
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
if (hash == hval)
return -1;
else if (hash == 0)
{
val_ptr = NULL;
atomic_compare_exchange_strong_explicit(&htab->table[idx].val_ptr,
(uintptr_t *) &val_ptr,
(uintptr_t) val,
memory_order_acquire,
memory_order_acquire);
if (val_ptr == NULL)
{
atomic_store_explicit(&htab->table[idx].hashval, hval,
memory_order_release);
return 0;
}
else
{
do
{
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
}
while (hash == 0);
if (hash == hval)
return -1;
}
}
/* Second hash function as suggested in [Knuth]. */
HASHTYPE second_hash = 1 + hval % (htab->size - 2);
for(;;)
{
if (idx <= second_hash)
idx = htab->size + idx - second_hash;
else
idx -= second_hash;
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
if (hash == hval)
return -1;
else if (hash == 0)
{
val_ptr = NULL;
atomic_compare_exchange_strong_explicit(&htab->table[idx].val_ptr,
(uintptr_t *) &val_ptr,
(uintptr_t) val,
memory_order_acquire,
memory_order_acquire);
if (val_ptr == NULL)
{
atomic_store_explicit(&htab->table[idx].hashval, hval,
memory_order_release);
return 0;
}
else
{
do
{
hash = atomic_load_explicit(&htab->table[idx].hashval,
memory_order_acquire);
}
while (hash == 0);
if (hash == hval)
return -1;
}
}
}
}
#define NO_RESIZING 0u
#define ALLOCATING_MEMORY 1u
#define MOVING_DATA 3u
#define CLEANING 2u
#define STATE_BITS 2u
#define STATE_INCREMENT (1u << STATE_BITS)
#define STATE_MASK (STATE_INCREMENT - 1)
#define GET_STATE(A) ((A) & STATE_MASK)
#define IS_NO_RESIZE_OR_CLEANING(A) (((A) & 0x1u) == 0)
#define GET_ACTIVE_WORKERS(A) ((A) >> STATE_BITS)
#define INITIALIZATION_BLOCK_SIZE 256
#define MOVE_BLOCK_SIZE 256
#define CEIL(A, B) (((A) + (B) - 1) / (B))
/* Initializes records and copies the data from the old table.
It can share work with other threads */
static void resize_helper(NAME *htab, int blocking)
{
size_t num_old_blocks = CEIL(htab->old_size, MOVE_BLOCK_SIZE);
size_t num_new_blocks = CEIL(htab->size, INITIALIZATION_BLOCK_SIZE);
size_t my_block;
size_t num_finished_blocks = 0;
while ((my_block = atomic_fetch_add_explicit(&htab->next_init_block, 1,
memory_order_acquire))
< num_new_blocks)
{
size_t record_it = my_block * INITIALIZATION_BLOCK_SIZE;
size_t record_end = (my_block + 1) * INITIALIZATION_BLOCK_SIZE;
if (record_end > htab->size)
record_end = htab->size;
while (record_it++ != record_end)
{
atomic_init(&htab->table[record_it].hashval, (uintptr_t) NULL);
atomic_init(&htab->table[record_it].val_ptr, (uintptr_t) NULL);
}
num_finished_blocks++;
}
atomic_fetch_add_explicit(&htab->num_initialized_blocks,
num_finished_blocks, memory_order_release);
while (atomic_load_explicit(&htab->num_initialized_blocks,
memory_order_acquire) != num_new_blocks);
/* All block are initialized, start moving */
num_finished_blocks = 0;
while ((my_block = atomic_fetch_add_explicit(&htab->next_move_block, 1,
memory_order_acquire))
< num_old_blocks)
{
size_t record_it = my_block * MOVE_BLOCK_SIZE;
size_t record_end = (my_block + 1) * MOVE_BLOCK_SIZE;
if (record_end > htab->old_size)
record_end = htab->old_size;
while (record_it++ != record_end)
{
TYPE val_ptr = (TYPE) atomic_load_explicit(
&htab->old_table[record_it].val_ptr,
memory_order_acquire);
if (val_ptr == NULL)
continue;
HASHTYPE hashval = atomic_load_explicit(
&htab->old_table[record_it].hashval,
memory_order_acquire);
assert(hashval);
insert_helper(htab, hashval, val_ptr);
}
num_finished_blocks++;
}
atomic_fetch_add_explicit(&htab->num_moved_blocks, num_finished_blocks,
memory_order_release);
if (blocking)
while (atomic_load_explicit(&htab->num_moved_blocks,
memory_order_acquire) != num_old_blocks);
}
static void
resize_master(NAME *htab)
{
htab->old_size = htab->size;
htab->old_table = htab->table;
htab->size = next_prime(htab->size * 2);
htab->table = malloc((1 + htab->size) * sizeof(htab->table[0]));
assert(htab->table);
/* Change state from ALLOCATING_MEMORY to MOVING_DATA */
atomic_fetch_xor_explicit(&htab->resizing_state,
ALLOCATING_MEMORY ^ MOVING_DATA,
memory_order_release);
resize_helper(htab, 1);
/* Change state from MOVING_DATA to CLEANING */
size_t resize_state = atomic_fetch_xor_explicit(&htab->resizing_state,
MOVING_DATA ^ CLEANING,
memory_order_acq_rel);
while (GET_ACTIVE_WORKERS(resize_state) != 0)
resize_state = atomic_load_explicit(&htab->resizing_state,
memory_order_acquire);
/* There are no more active workers */
atomic_store_explicit(&htab->next_init_block, 0, memory_order_relaxed);
atomic_store_explicit(&htab->num_initialized_blocks, 0,
memory_order_relaxed);
atomic_store_explicit(&htab->next_move_block, 0, memory_order_relaxed);
atomic_store_explicit(&htab->num_moved_blocks, 0, memory_order_relaxed);
free(htab->old_table);
/* Change state to NO_RESIZING */
atomic_fetch_xor_explicit(&htab->resizing_state, CLEANING ^ NO_RESIZING,
memory_order_relaxed);
}
static void
resize_worker(NAME *htab)
{
size_t resize_state = atomic_load_explicit(&htab->resizing_state,
memory_order_acquire);
/* If the resize has finished */
if (IS_NO_RESIZE_OR_CLEANING(resize_state))
return;
/* Register as worker and check if the resize has finished in the meantime*/
resize_state = atomic_fetch_add_explicit(&htab->resizing_state,
STATE_INCREMENT,
memory_order_acquire);
if (IS_NO_RESIZE_OR_CLEANING(resize_state))
{
atomic_fetch_sub_explicit(&htab->resizing_state, STATE_INCREMENT,
memory_order_relaxed);
return;
}
/* Wait while the new table is being allocated. */
while (GET_STATE(resize_state) == ALLOCATING_MEMORY)
resize_state = atomic_load_explicit(&htab->resizing_state,
memory_order_acquire);
/* Check if the resize is done */
assert(GET_STATE(resize_state) != NO_RESIZING);
if (GET_STATE(resize_state) == CLEANING)
{
atomic_fetch_sub_explicit(&htab->resizing_state, STATE_INCREMENT,
memory_order_relaxed);
return;
}
resize_helper(htab, 0);
/* Deregister worker */
atomic_fetch_sub_explicit(&htab->resizing_state, STATE_INCREMENT,
memory_order_release);
}
int
#define INIT(name) _INIT (name)
#define _INIT(name) \
name##_init
INIT(NAME) (NAME *htab, size_t init_size)
{
/* We need the size to be a prime. */
init_size = next_prime (init_size);
/* Initialize the data structure. */
htab->size = init_size;
atomic_init(&htab->filled, 0);
atomic_init(&htab->resizing_state, 0);
atomic_init(&htab->next_init_block, 0);
atomic_init(&htab->num_initialized_blocks, 0);
atomic_init(&htab->next_move_block, 0);
atomic_init(&htab->num_moved_blocks, 0);
pthread_rwlock_init(&htab->resize_rwl, NULL);
htab->table = (void *) malloc ((init_size + 1) * sizeof (htab->table[0]));
if (htab->table == NULL)
return -1;
for (size_t i = 0; i <= init_size; i++)
{
atomic_init(&htab->table[i].hashval, (uintptr_t) NULL);
atomic_init(&htab->table[i].val_ptr, (uintptr_t) NULL);
}
return 0;
}
int
#define FREE(name) _FREE (name)
#define _FREE(name) \
name##_free
FREE(NAME) (NAME *htab)
{
pthread_rwlock_destroy(&htab->resize_rwl);
free (htab->table);
return 0;
}
int
#define INSERT(name) _INSERT (name)
#define _INSERT(name) \
name##_insert
INSERT(NAME) (NAME *htab, HASHTYPE hval, TYPE data)
{
int incremented = 0;
for(;;)
{
while (pthread_rwlock_tryrdlock(&htab->resize_rwl) != 0)
resize_worker(htab);
size_t filled;
if (!incremented)
{
filled = atomic_fetch_add_explicit(&htab->filled, 1,
memory_order_acquire);
incremented = 1;
}
else
{
filled = atomic_load_explicit(&htab->filled,
memory_order_acquire);
}
if (100 * filled > 90 * htab->size)
{
/* Table is filled more than 90%. Resize the table. */
size_t resizing_state = atomic_load_explicit(&htab->resizing_state,
memory_order_acquire);
if (resizing_state == 0 &&
atomic_compare_exchange_strong_explicit(&htab->resizing_state,
&resizing_state,
ALLOCATING_MEMORY,
memory_order_acquire,
memory_order_acquire))
{
/* Master thread */
pthread_rwlock_unlock(&htab->resize_rwl);
pthread_rwlock_wrlock(&htab->resize_rwl);
resize_master(htab);
pthread_rwlock_unlock(&htab->resize_rwl);
}
else
{
/* Worker thread */
pthread_rwlock_unlock(&htab->resize_rwl);
resize_worker(htab);
}
}
else
{
/* Lock acquired, no need for resize*/
break;
}
}
int ret_val = insert_helper(htab, hval, data);
if (ret_val == -1)
atomic_fetch_sub_explicit(&htab->filled, 1, memory_order_relaxed);
pthread_rwlock_unlock(&htab->resize_rwl);
return ret_val;
}
TYPE
#define FIND(name) _FIND (name)
#define _FIND(name) \
name##_find
FIND(NAME) (NAME *htab, HASHTYPE hval)
{
while (pthread_rwlock_tryrdlock(&htab->resize_rwl) != 0)
resize_worker(htab);
size_t idx;
/* Make the hash data nonzero. */
hval = hval ?: 1;
idx = lookup(htab, hval);
if (idx == 0)
{
pthread_rwlock_unlock(&htab->resize_rwl);
return NULL;
}
/* get a copy before unlocking the lock */
TYPE ret_val = (TYPE) atomic_load_explicit(&htab->table[idx].val_ptr,
memory_order_relaxed);
pthread_rwlock_unlock(&htab->resize_rwl);
return ret_val;
}
|