]> pd.if.org Git - nbds/blob - runtime/rcu.c
Some refactoring. WARNING: tests not passing!
[nbds] / runtime / rcu.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  *
5  * safe memory reclemation using a simple technique from rcu
6  */
7 #include <string.h>
8 #include "common.h"
9 #include "lwt.h"
10 #include "mem.h"
11 #include "tls.h"
12
13 #define RCU_POST_THRESHOLD 10
14 #define RCU_QUEUE_SCALE 20
15
16 typedef struct fifo {
17     uint32_t head;
18     uint32_t tail;
19     uint32_t scale;
20     void *x[0];
21 } fifo_t;
22
23 static uint64_t rcu_[MAX_NUM_THREADS][MAX_NUM_THREADS] = {};
24 static uint64_t rcu_last_posted_[MAX_NUM_THREADS][MAX_NUM_THREADS] = {};
25 static fifo_t *pending_[MAX_NUM_THREADS] = {};
26 static int num_threads_ = 0;
27
28 static fifo_t *fifo_alloc(int scale) {
29     fifo_t *q = (fifo_t *)nbd_malloc(sizeof(fifo_t) + (1 << scale) * sizeof(void *)); 
30     memset(q, 0, sizeof(fifo_t));
31     q->scale = scale;
32     q->head = 0;
33     q->tail = 0;
34     return q;
35 }
36
37 static uint32_t fifo_index (fifo_t *q, uint32_t i) {
38     return i & MASK(q->scale);
39 }
40
41 static void fifo_enqueue (fifo_t *q, void *x) {
42     assert(fifo_index(q, q->head + 1) != fifo_index(q, q->tail));
43     uint32_t i = fifo_index(q, q->head++);
44     q->x[i] = x;
45 }
46
47 static void *fifo_dequeue (fifo_t *q) {
48     uint32_t i = fifo_index(q, q->tail++);
49     return q->x[i];
50 }
51
52 void rcu_thread_init (int id) {
53     assert(id < MAX_NUM_THREADS);
54     if (pending_[id] == NULL) {
55         pending_[id] = fifo_alloc(RCU_QUEUE_SCALE);
56         SYNC_ADD(&num_threads_, 1);
57     }
58 }
59
60 static void rcu_post (uint64_t x) {
61     LOCALIZE_THREAD_LOCAL(tid_, int);
62     if (x - rcu_last_posted_[tid_][tid_] < RCU_POST_THRESHOLD)
63         return;
64
65     int next_thread_id = (tid_ + 1) % num_threads_;
66
67     TRACE("r0", "rcu_post: %llu", x, 0);
68     rcu_[next_thread_id][tid_] = rcu_last_posted_[tid_][tid_] = x;
69 }
70
71 void rcu_update (void) {
72     LOCALIZE_THREAD_LOCAL(tid_, int);
73     assert(tid_ < num_threads_);
74     int next_thread_id = (tid_ + 1) % num_threads_;
75     int i;
76     for (i = 0; i < num_threads_; ++i) {
77         if (i == tid_)
78             continue;
79
80         // No need to post an update if the value hasn't changed
81         if (rcu_[tid_][i] == rcu_last_posted_[tid_][i])
82             continue;
83
84         uint64_t x = rcu_[tid_][i];
85         rcu_[next_thread_id][i] = rcu_last_posted_[tid_][i] = x;
86     }
87
88     // free
89     while (pending_[tid_]->tail != rcu_[tid_][tid_]) {
90         nbd_free(fifo_dequeue(pending_[tid_]));
91     }
92 }
93
94 void nbd_defer_free (void *x) {
95     LOCALIZE_THREAD_LOCAL(tid_, int);
96     fifo_enqueue(pending_[tid_], x);
97     TRACE("r0", "nbd_defer_free: put %p on queue at position %llu", x, pending_[tid_]->head);
98     rcu_post(pending_[tid_]->head);
99 }
100
101 #ifdef MAKE_rcu_test
102 #include <errno.h>
103 #include <stdio.h>
104 #include "runtime.h"
105
106 #define NUM_ITERATIONS 10000000
107
108 typedef struct node {
109     struct node *next;
110 } node_t;
111
112 typedef struct lifo {
113     node_t *head;
114 } lifo_t;
115
116 static volatile int wait_;
117 static lifo_t *stk_;
118
119 static lifo_t *lifo_alloc (void) {
120     lifo_t *stk = (lifo_t *)nbd_malloc(sizeof(lifo_t)); 
121     memset(stk, 0, sizeof(lifo_t));
122     return stk;
123 }
124
125 static void lifo_aba_push (lifo_t *stk, node_t *x) {
126     node_t *head;
127     do {
128         head = ((volatile lifo_t *)stk)->head;
129         ((volatile node_t *)x)->next = head;
130     } while (__sync_val_compare_and_swap(&stk->head, head, x) != head);
131 }
132
133 node_t *lifo_aba_pop (lifo_t *stk) {
134     node_t *head;
135     do {
136         head = ((volatile lifo_t *)stk)->head;
137         if (head == NULL)
138             return NULL;
139     } while (__sync_val_compare_and_swap(&stk->head, head, head->next) != head);
140     head->next = NULL;
141     return head;
142 }
143
144 node_t *node_alloc (void) {
145     node_t *node = (node_t *)nbd_malloc(sizeof(node_t));
146     memset(node, 0, sizeof(node_t));
147     return node;
148 }
149
150 void *worker (void *arg) {
151     int id = (int)(size_t)arg;
152     unsigned int rand_seed = (unsigned int)id + 1;
153
154     // Wait for all the worker threads to be ready.
155     __sync_fetch_and_add(&wait_, -1);
156     do {} while (wait_); 
157
158     int i;
159     for (i = 0; i < NUM_ITERATIONS; ++ i) {
160         int n = rand_r(&rand_seed);
161         if (n & 0x1) {
162             lifo_aba_push(stk_, node_alloc());
163         } else {
164             node_t *x = lifo_aba_pop(stk_);
165             if (x) {
166                 nbd_defer_free(x);
167             }
168         }
169         rcu_update();
170     }
171
172     return NULL;
173 }
174
175 int main (int argc, char **argv) {
176     nbd_init();
177     //lwt_set_trace_level("m0r0");
178
179     int num_threads = 2;
180     if (argc == 2)
181     {
182         errno = 0;
183         num_threads = strtol(argv[1], NULL, 10);
184         if (errno) {
185             fprintf(stderr, "%s: Invalid argument for number of threads\n", argv[0]);
186             return -1;
187         }
188         if (num_threads <= 0) {
189             fprintf(stderr, "%s: Number of threads must be at least 1\n", argv[0]);
190             return -1;
191         }
192     }
193
194     stk_ = lifo_alloc();
195     wait_ = num_threads;
196
197     pthread_t thread[num_threads];
198     for (int i = 0; i < num_threads; ++i) {
199         int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
200         if (rc != 0) { perror("pthread_create"); return rc; }
201     }
202     for (int i = 0; i < num_threads; ++i) {
203         pthread_join(thread[i], NULL);
204     }
205
206     return 0;
207 }
208 #endif//rcu_test