-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree_prcu.c
584 lines (514 loc) · 13.1 KB
/
tree_prcu.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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <assert.h>
#include <stdint.h>
#include "benchmark_list.h"
typedef struct node {
int value;
struct node *child[2] __attribute__((aligned(16)));
pthread_mutex_t lock;
int marked;
int tag[2];
} node_t;
typedef struct prcu_tree {
node_t *root;
} prcu_tree_t;
typedef struct predicate_info {
int min_key;
int max_key;
int max;
} predicate_info_t;
static int max_key_in_range;
#ifdef PRCU_EER
#define PRCU_TABLE_SIZE (1)
#elif defined PRCU_D
#define PRCU_TABLE_SIZE (1023)
typedef struct prcu_table {
int which;
volatile long count[2];
pthread_mutex_t lock;
char padding[128];
} prcu_table_t;
static prcu_table_t *prcu_table;
#elif defined PRCU_DEER
#define PRCU_TABLE_SIZE (16)
typedef struct prcu_table {
volatile unsigned long time;
char p[184];
} prcu_table_t;
#endif
#define URCU_MAX_FREE_PTRS (1000)
typedef struct prcu_pthread_data {
#ifdef PRCU_EER
volatile unsigned long time;
volatile long key;
int nr_threads;
struct prcu_pthread_data **sync_data;
#elif defined PRCU_D
int which;
#elif defined PRCU_DEER
prcu_table_t *prcu_table;
int nr_threads;
struct prcu_pthread_data **sync_data;
#endif
int f_size;
void *free_ptrs[URCU_MAX_FREE_PTRS];
} prcu_pthread_data_t;
static node_t *prcu_new_node(int key)
{
node_t *ret = (node_t *)malloc(sizeof(node_t));
assert(ret != NULL);
ret->value = key;
ret->marked = 0;
ret->child[0] = NULL;
ret->child[1] = NULL;
ret->tag[0] = 0;
ret->tag[1] = 0;
if (pthread_mutex_init(&(ret->lock), NULL) != 0)
assert(0);
return ret;
}
int pred_hash(int key)
{
int num_buckets = PRCU_TABLE_SIZE;
int result, num_elements_per_bucket, overflow, threshold;
if (num_buckets > max_key_in_range)
return key;
num_elements_per_bucket = max_key_in_range / num_buckets;
overflow = max_key_in_range - (num_buckets * num_elements_per_bucket);
if (overflow == 0)
result = (key / num_elements_per_bucket);
else {
threshold = overflow * (num_elements_per_bucket + 1);
if (key < threshold)
result = (key / (num_elements_per_bucket + 1));
else
result = overflow + ((key - (threshold)) / num_elements_per_bucket);
}
return result;
}
int pred(predicate_info_t *info, int key)
{
return (info->min_key < key && info->max_key >= key);
}
int pred_next(predicate_info_t *info, int cur_bucket)
{
int max_bucket = pred_hash(info->max_key);
if (cur_bucket < max_bucket)
return cur_bucket + 1;
else
return -1;
}
static inline uint64_t read_tsc(void)
{
unsigned upper, lower;
asm volatile("rdtsc"
: "=a" (lower), "=d" (upper)
:
: "memory");
return ((uint64_t) lower) | (((uint64_t) upper) << 32);
}
static inline void set_bit(int nr, volatile unsigned long *addr)
{
asm("btsl %1,%0" : "+m" (*addr) : "Ir" (nr));
}
void prcu_enter(int key, prcu_pthread_data_t *prcu_data)
{
#ifdef PRCU_EER
prcu_data->key = key;
__sync_lock_test_and_set(&prcu_data->time, read_tsc() << 1);
#elif defined PRCU_D
int j = pred_hash(key);
prcu_data->which = prcu_table[j].which;
__sync_fetch_and_add(&prcu_table[j].count[prcu_data->which], 1);
#elif defined PRCU_DEER
int j = pred_hash(key);
__sync_lock_test_and_set(&prcu_data->prcu_table[j].time, read_tsc() << 1);
#else
assert(0);
#endif
}
void prcu_exit(int key, prcu_pthread_data_t *prcu_data)
{
#ifdef PRCU_EER
set_bit(0, &prcu_data->time);
#elif defined PRCU_D
int j = pred_hash(key);
__sync_fetch_and_add(&prcu_table[j].count[prcu_data->which], -1);
#elif defined PRCU_DEER
int j = pred_hash(key);
set_bit(0, &prcu_data->prcu_table[j].time);
#else
assert(0);
#endif
}
#ifdef PRCU_D
static inline void prcu_wait(prcu_table_t *node)
{
int i, which;
for (i = 0; i < 10000; i++)
if (node->count[0] == 0 && node->count[1] == 0)
return;
pthread_mutex_lock(&node->lock);
which = node->which;
node->which = !which;
asm volatile("":::"memory");
while (node->count[which] != 0) ;
node->which = which;
asm volatile("":::"memory");
while (node->count[!which] != 0) ;
pthread_mutex_unlock(&node->lock);
}
#elif defined PRCU_DEER
static inline void prcu_wait(int j, uint64_t now, prcu_pthread_data_t *prcu_data)
{
int i;
unsigned long t;
for (i = 0; i < prcu_data->nr_threads; i++) {
while (1) {
t = prcu_data->sync_data[i]->prcu_table[j].time;
if (t & 1 || t > now)
break;
}
}
}
#endif
void prcu_wait_for_readers(int min, predicate_info_t *pred_info, prcu_pthread_data_t *prcu_data)
{
#ifdef PRCU_EER
uint64_t now;
unsigned long t;
int i;
asm volatile("mfence" ::: "memory");
now = read_tsc() << 1;
for (i = 0; i < prcu_data->nr_threads; i++) {
if (!pred(pred_info, prcu_data->sync_data[i]->key))
continue;
while (1) {
t = prcu_data->sync_data[i]->time;
if (t & 1 || t > now)
break;
}
}
#elif defined PRCU_D
int bucket;
asm volatile("mfence" ::: "memory");
bucket = min;
while (bucket >= 0) {
prcu_wait(&(prcu_table[bucket]));
bucket = pred_next(pred_info, bucket);
}
#elif defined PRCU_DEER
uint64_t now;
int bucket;
asm volatile("mfence" ::: "memory");
now = read_tsc() << 1;
bucket = min;
while (bucket >= 0) {
prcu_wait(bucket, now, prcu_data);
bucket = pred_next(pred_info, bucket);
}
#else
assert(0);
#endif
}
inline void rcu_free(void *ptr, prcu_pthread_data_t *prcu_data)
{
int i;
predicate_info_t pred_info;
prcu_data->free_ptrs[prcu_data->f_size] = ptr;
prcu_data->f_size++;
if (prcu_data->f_size == URCU_MAX_FREE_PTRS) {
pred_info.min_key = 0;
pred_info.max_key = max_key_in_range - 1;
pred_info.max = max_key_in_range;
prcu_wait_for_readers(0, &pred_info, prcu_data);
for (i = 0; i < URCU_MAX_FREE_PTRS; i++)
free(prcu_data->free_ptrs[i]);
prcu_data->f_size = 0;
}
}
int validate(node_t *prev, int tag, node_t *cur, int direction)
{
int ret;
if (cur == NULL)
ret = (!(prev->marked) && (prev->child[direction] == cur) &&
(prev->tag[direction] == tag));
else
ret = (!(prev->marked) && !(cur->marked) &&
prev->child[direction] == cur);
return ret;
}
pthread_data_t *alloc_pthread_data(void)
{
pthread_data_t *d;
size_t pthread_size, prcu_size;
pthread_size = sizeof(pthread_data_t);
pthread_size = CACHE_ALIGN_SIZE(pthread_size);
prcu_size = sizeof(prcu_pthread_data_t);
prcu_size = CACHE_ALIGN_SIZE(prcu_size);
d = (pthread_data_t *)malloc(pthread_size + prcu_size);
if (d != NULL)
d->ds_data = ((void *)d) + pthread_size;
return d;
}
void free_pthread_data(pthread_data_t *d)
{
#if (defined PRCU_EER || defined PRCU_DEER)
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)d->ds_data;
free(prcu_data->sync_data);
#endif
free(d);
}
void *list_global_init(int init_size, int value_range)
{
prcu_tree_t *tree;
node_t *prev, *cur, *new_node;
int i, key, val, direction;
tree = (prcu_tree_t *)malloc(sizeof(prcu_tree_t));
if (tree == NULL)
return NULL;
tree->root = prcu_new_node(INT_MAX);
i = 0;
while (i < init_size) {
key = rand() % value_range;
prev = tree->root;
cur = prev->child[0];
direction = 0;
while (cur != NULL) {
prev = cur;
val = cur->value;
if (val > key) {
direction = 0;
cur = cur->child[0];
} else if (val < key) {
direction = 1;
cur = cur->child[1];
} else
break;
}
if (cur != NULL)
continue;
new_node = prcu_new_node(key);
if (new_node == NULL)
return NULL;
prev->child[direction] = new_node;
i++;
}
max_key_in_range = value_range;
#ifdef PRCU_D
prcu_table = malloc(sizeof(prcu_table_t) * PRCU_TABLE_SIZE);
if (prcu_table == NULL)
return NULL;
for (i = 0; i < PRCU_TABLE_SIZE; i++) {
prcu_table[i].count[0] = 0;
prcu_table[i].count[1] = 0;
prcu_table[i].which = 0;
pthread_mutex_init(&(prcu_table[i].lock), NULL);
}
#endif
return tree;
}
int list_thread_init(pthread_data_t *data, pthread_data_t **sync_data, int nr_threads)
{
#ifdef PRCU_EER
int i;
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
prcu_data->time = 1;
prcu_data->key = 0;
prcu_data->nr_threads = nr_threads;
if ((prcu_data->sync_data = malloc(nr_threads * sizeof(prcu_pthread_data_t *))) == NULL)
return -1;
for (i = 0; i < nr_threads; i++)
prcu_data->sync_data[i] = sync_data[i]->ds_data;
#elif defined PRCU_D
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
prcu_data->which = 0;
#elif defined PRCU_DEER
int i;
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
prcu_data->prcu_table = (prcu_table_t *)malloc(sizeof(prcu_table_t) * PRCU_TABLE_SIZE);
if (prcu_data->prcu_table == NULL)
return -1;
for (i = 0; i < PRCU_TABLE_SIZE; i++)
prcu_data->prcu_table[i].time = 1;
prcu_data->nr_threads = nr_threads;
if ((prcu_data->sync_data = malloc(nr_threads * sizeof(prcu_pthread_data_t *))) == NULL)
return -1;
for (i = 0; i < nr_threads; i++)
prcu_data->sync_data[i] = sync_data[i]->ds_data;
#else
assert(0);
#endif
prcu_data->f_size = 0;
return 0;
}
void list_global_exit(void *tree)
{
}
int list_ins(int key, pthread_data_t *data)
{
prcu_tree_t *tree = (prcu_tree_t *)data->list;
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
node_t *prev, *cur, *new_node;
int direction, tag, val;
while (1) {
prcu_enter(key, prcu_data);
prev = tree->root;
cur = prev->child[0];
direction = 0;
while (cur != NULL) {
val = cur->value;
if (val > key) {
direction = 0;
prev = cur;
cur = cur->child[0];
} else if (val < key) {
direction = 1;
prev = cur;
cur = cur->child[1];
} else
break;
}
tag = prev->tag[direction];
prcu_exit(key, prcu_data);
if (cur != NULL)
return 0;
pthread_mutex_lock(&(prev->lock));
if (validate(prev, tag, cur, direction)) {
new_node = prcu_new_node(key);
prev->child[direction] = new_node;
pthread_mutex_unlock(&(prev->lock));
return 1;
}
pthread_mutex_unlock(&(prev->lock));
}
}
int list_del(int key, pthread_data_t *data)
{
prcu_tree_t *tree = (prcu_tree_t *)data->list;
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
node_t *prev, *cur, *prev_succ, *succ, *next, *new_node;
int direction, val, direction_succ;
predicate_info_t pred_info;
int min_bucket;
while (1) {
prcu_enter(key, prcu_data);
prev = tree->root;
cur = prev->child[0];
direction = 0;
while (cur != NULL) {
val = cur->value;
if (val > key) {
direction = 0;
prev = cur;
cur = cur->child[0];
} else if (val < key) {
direction = 1;
prev = cur;
cur = cur->child[1];
} else
break;
}
prcu_exit(key, prcu_data);
if (cur == NULL)
return 0;
pthread_mutex_lock(&(prev->lock));
pthread_mutex_lock(&(cur->lock));
if (!validate(prev, 0, cur, direction)) {
pthread_mutex_unlock(&(prev->lock));
pthread_mutex_unlock(&(cur->lock));
continue;
}
if (cur->child[0] == NULL) {
cur->marked = 1;
prev->child[direction] = cur->child[1];
if (prev->child[direction] == NULL)
prev->tag[direction]++;
pthread_mutex_unlock(&(prev->lock));
pthread_mutex_unlock(&(cur->lock));
rcu_free(cur, prcu_data);
return 1;
}
if (cur->child[1] == NULL) {
cur->marked = 1;
prev->child[direction] = cur->child[0];
if (prev->child[direction] == NULL)
prev->tag[direction]++;
pthread_mutex_unlock(&(prev->lock));
pthread_mutex_unlock(&(cur->lock));
rcu_free(cur, prcu_data);
return 1;
}
prev_succ = cur;
succ = cur->child[1];
next = succ->child[0];
while (next != NULL) {
prev_succ = succ;
succ = next;
next = next->child[0];
}
if (prev_succ != cur) {
pthread_mutex_lock(&(prev_succ->lock));
direction_succ = 0;
} else
direction_succ = 1;
pthread_mutex_lock(&(succ->lock));
if (validate(prev_succ, 0, succ, direction_succ) &&
validate(succ, succ->tag[0], NULL, 0)) {
cur->marked = 1;
new_node = prcu_new_node(succ->value);
new_node->child[0] = cur->child[0];
new_node->child[1] = cur->child[1];
pthread_mutex_lock(&(new_node->lock));
prev->child[direction] = new_node;
pred_info.min_key = key;
pred_info.max_key = succ->value;
pred_info.max = max_key_in_range;
min_bucket = pred_hash(key);
prcu_wait_for_readers(min_bucket, &pred_info, prcu_data);
succ->marked = 1;
if (prev_succ == cur) {
new_node->child[1] = succ->child[1];
if (new_node->child[1] == NULL)
new_node->tag[1]++;
} else {
prev_succ->child[0] = succ->child[1];
if (prev_succ->child[0] == NULL)
prev_succ->tag[0]++;
}
pthread_mutex_unlock(&(prev->lock));
pthread_mutex_unlock(&(new_node->lock));
pthread_mutex_unlock(&(cur->lock));
if (prev_succ != cur)
pthread_mutex_unlock(&(prev_succ->lock));
pthread_mutex_unlock(&(succ->lock));
rcu_free(cur, prcu_data);
rcu_free(succ, prcu_data);
return 1;
}
pthread_mutex_unlock(&(prev->lock));
pthread_mutex_unlock(&(cur->lock));
if (prev_succ != cur)
pthread_mutex_unlock(&(prev_succ->lock));
pthread_mutex_unlock(&(succ->lock));
}
}
int list_find(int key, pthread_data_t *data)
{
prcu_tree_t *tree = (prcu_tree_t *)data->list;
prcu_pthread_data_t *prcu_data = (prcu_pthread_data_t *)data->ds_data;
node_t *cur;
int val;
prcu_enter(key, prcu_data);
cur = tree->root->child[0];
while (cur != NULL) {
val = cur->value;
if (val > key)
cur = cur->child[0];
else if (val < key)
cur = cur->child[0];
else
break;
}
prcu_exit(key, prcu_data);
return (cur != NULL);
}