-
Notifications
You must be signed in to change notification settings - Fork 1
/
qsbr.c
103 lines (79 loc) · 2.1 KB
/
qsbr.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
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include "qsbr.h"
#include "util.h"
#define CACHE_ALIGN 64
#define QSBR_MAX_THREADS 100
static long g_nr_threads = 0;
static qsbr_pthread_data_t *g_threads[QSBR_MAX_THREADS] = {0,};
static volatile pthread_spinlock_t g_update_lock __attribute__ ((__aligned__ (CACHE_ALIGN)));
static volatile int g_epoch __attribute__ ((__aligned__ (CACHE_ALIGN)));
void qsbr_init(void)
{
g_epoch = 1;
pthread_spin_init(&g_update_lock, PTHREAD_PROCESS_SHARED);
}
void qsbr_pthread_init(qsbr_pthread_data_t *qsbr_data)
{
int i;
qsbr_data->epoch = 0;
qsbr_data->in_critical = 1;
for (i = 0; i < QSBR_N_EPOCHS; i++)
qsbr_data->freelist_count[i] = 0;
g_threads[FETCH_AND_ADD(&g_nr_threads, 1)] = qsbr_data;
}
static inline void qsbr_free(qsbr_pthread_data_t *qsbr_data, int epoch)
{
int i;
MEMBARSTLD();
for (i = 0; i < qsbr_data->freelist_count[epoch]; i++)
free(qsbr_data->freelist[epoch][i]);
qsbr_data->freelist_count[epoch] = 0;
}
static int qsbr_update_epoch()
{
int i, cur_epoch;
if (!pthread_spin_trylock(&g_update_lock))
return 0;
cur_epoch = g_epoch;
for (i = 0; i < g_nr_threads; i++) {
if (g_threads[i]->in_critical == 1 &&
g_threads[i]->epoch != cur_epoch) {
pthread_spin_unlock(&g_update_lock);
return 0;
}
}
g_epoch = (cur_epoch + 1) % QSBR_N_EPOCHS;
pthread_spin_unlock(&g_update_lock);
return 1;
}
void qsbr_quiescent_state(qsbr_pthread_data_t *qsbr_data)
{
int epoch;
epoch = g_epoch;
if (qsbr_data->epoch != epoch) {
qsbr_free(qsbr_data, epoch);
qsbr_data->epoch = epoch;
} else {
qsbr_data->in_critical = 0;
if (qsbr_update_epoch()) {
qsbr_data->in_critical = 1;
MEMBARSTLD();
epoch = g_epoch;
if (qsbr_data->epoch != epoch) {
qsbr_free(qsbr_data, epoch);
qsbr_data->epoch = epoch;
}
return;
}
qsbr_data->in_critical = 1;
MEMBARSTLD();
}
return;
}
void qsbr_free_ptr(void *ptr, qsbr_pthread_data_t *qsbr_data)
{
assert(qsbr_data->freelist_count[qsbr_data->epoch] < QSBR_FREELIST_SIZE);
qsbr_data->freelist[qsbr_data->epoch][(qsbr_data->freelist_count[qsbr_data->epoch])++] = ptr;
}