generic urcu deferral (call_rcu())
[urcu.git] / urcu-defer.c
1 /*
2 * urcu-defer.c
3 *
4 * Userspace RCU library - batch memory reclamation
5 *
6 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <poll.h>
31
32 #include "urcu-defer-static.h"
33 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
34 #include "urcu-defer.h"
35
36 void __attribute__((destructor)) urcu_defer_exit(void);
37
38 extern void synchronize_rcu(void);
39
40 /*
41 * urcu_defer_mutex nests inside defer_thread_mutex.
42 */
43 static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46 /*
47 * Written to only by each individual deferer. Read by both the deferer and
48 * the reclamation tread.
49 */
50 struct defer_queue __thread defer_queue;
51
52 /* Thread IDs of registered deferers */
53 #define INIT_NUM_THREADS 4
54
55 struct deferer_registry {
56 pthread_t tid;
57 struct defer_queue *defer_queue;
58 unsigned long last_head;
59 };
60
61 static struct deferer_registry *registry;
62 static int num_deferers, alloc_deferers;
63
64 static pthread_t tid_defer;
65 static int exit_defer;
66
67 static void internal_urcu_lock(pthread_mutex_t *mutex)
68 {
69 int ret;
70
71 #ifndef DISTRUST_SIGNALS_EXTREME
72 ret = pthread_mutex_lock(mutex);
73 if (ret) {
74 perror("Error in pthread mutex lock");
75 exit(-1);
76 }
77 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
78 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
79 if (ret != EBUSY && ret != EINTR) {
80 printf("ret = %d, errno = %d\n", ret, errno);
81 perror("Error in pthread mutex lock");
82 exit(-1);
83 }
84 poll(NULL,0,10);
85 }
86 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
87 }
88
89 static void internal_urcu_unlock(pthread_mutex_t *mutex)
90 {
91 int ret;
92
93 ret = pthread_mutex_unlock(mutex);
94 if (ret) {
95 perror("Error in pthread mutex unlock");
96 exit(-1);
97 }
98 }
99
100 /*
101 * Must be called after Q.S. is reached.
102 */
103 static void rcu_defer_barrier_queue(struct defer_queue *queue,
104 unsigned long head)
105 {
106 unsigned long i;
107 void (*fct)(void *p);
108 void *p;
109
110 /*
111 * Tail is only modified when lock is held.
112 * Head is only modified by owner thread.
113 */
114
115 for (i = queue->tail; i != head;) {
116 smp_rmb(); /* read head before q[]. */
117 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
118 if (unlikely(DQ_IS_FCT_BIT(p))) {
119 //printf("%lu fct bit %p\n", i-1, p);
120 DQ_CLEAR_FCT_BIT(p);
121 queue->last_fct_out = p;
122 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
123 } else if (unlikely(p == DQ_FCT_MARK)) {
124 //printf("%lu fct mark %p\n", i-1, p);
125 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
126 queue->last_fct_out = p;
127 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
128 }// else
129 //printf("%lu data %p\n", i-1, p);
130 fct = queue->last_fct_out;
131 //printf("tid %lu %lu last_fct %p data %p\n", pthread_self(), i-1, fct, p);
132 fct(p);
133 }
134 smp_mb(); /* push tail after having used q[] */
135 STORE_SHARED(queue->tail, i);
136 }
137
138 static void _rcu_defer_barrier_thread(void)
139 {
140 unsigned long head;
141
142 head = defer_queue.head;
143 synchronize_rcu();
144 rcu_defer_barrier_queue(&defer_queue, head);
145 }
146
147
148 void rcu_defer_barrier_thread(void)
149 {
150 internal_urcu_lock(&urcu_defer_mutex);
151 _rcu_defer_barrier_thread();
152 internal_urcu_unlock(&urcu_defer_mutex);
153 }
154
155 void rcu_defer_barrier(void)
156 {
157 struct deferer_registry *index;
158
159 if (!registry)
160 return;
161
162 internal_urcu_lock(&urcu_defer_mutex);
163 for (index = registry; index < registry + num_deferers; index++)
164 index->last_head = LOAD_SHARED(index->defer_queue->head);
165 synchronize_rcu();
166 for (index = registry; index < registry + num_deferers; index++)
167 rcu_defer_barrier_queue(index->defer_queue,
168 index->last_head);
169 internal_urcu_unlock(&urcu_defer_mutex);
170 }
171
172 void *thr_defer(void *args)
173 {
174 for (;;) {
175 if (LOAD_SHARED(exit_defer))
176 break;
177 poll(NULL,0,100); /* wait for 100ms */
178 rcu_defer_barrier();
179 }
180
181 return NULL;
182 }
183
184 /*
185 * library wrappers to be used by non-LGPL compatible source code.
186 */
187
188 void rcu_defer_queue(void (*fct)(void *p), void *p)
189 {
190 _rcu_defer_queue(fct, p);
191 }
192
193 static void rcu_add_deferer(pthread_t id)
194 {
195 struct deferer_registry *oldarray;
196
197 if (!registry) {
198 alloc_deferers = INIT_NUM_THREADS;
199 num_deferers = 0;
200 registry =
201 malloc(sizeof(struct deferer_registry) * alloc_deferers);
202 }
203 if (alloc_deferers < num_deferers + 1) {
204 oldarray = registry;
205 registry = malloc(sizeof(struct deferer_registry)
206 * (alloc_deferers << 1));
207 memcpy(registry, oldarray,
208 sizeof(struct deferer_registry) * alloc_deferers);
209 alloc_deferers <<= 1;
210 free(oldarray);
211 }
212 registry[num_deferers].tid = id;
213 /* reference to the TLS of _this_ deferer thread. */
214 registry[num_deferers].defer_queue = &defer_queue;
215 registry[num_deferers].last_head = 0;
216 num_deferers++;
217 }
218
219 /*
220 * Never shrink (implementation limitation).
221 * This is O(nb threads). Eventually use a hash table.
222 */
223 static void rcu_remove_deferer(pthread_t id)
224 {
225 struct deferer_registry *index;
226
227 assert(registry != NULL);
228 for (index = registry; index < registry + num_deferers; index++) {
229 if (pthread_equal(index->tid, id)) {
230 memcpy(index, &registry[num_deferers - 1],
231 sizeof(struct deferer_registry));
232 registry[num_deferers - 1].tid = 0;
233 registry[num_deferers - 1].defer_queue = NULL;
234 registry[num_deferers - 1].last_head = 0;
235 num_deferers--;
236 return;
237 }
238 }
239 /* Hrm not found, forgot to register ? */
240 assert(0);
241 }
242
243 static void start_defer_thread(void)
244 {
245 int ret;
246
247 ret = pthread_create(&tid_defer, NULL, thr_defer,
248 NULL);
249 assert(!ret);
250 }
251
252 static void stop_defer_thread(void)
253 {
254 int ret;
255 void *tret;
256
257 STORE_SHARED(exit_defer, 1);
258 ret = pthread_join(tid_defer, &tret);
259 assert(!ret);
260 }
261
262 void rcu_defer_register_thread(void)
263 {
264 int deferers;
265
266 internal_urcu_lock(&defer_thread_mutex);
267 internal_urcu_lock(&urcu_defer_mutex);
268 defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
269 rcu_add_deferer(pthread_self());
270 deferers = num_deferers;
271 internal_urcu_unlock(&urcu_defer_mutex);
272
273 if (deferers == 1)
274 start_defer_thread();
275 internal_urcu_unlock(&defer_thread_mutex);
276 }
277
278 void rcu_defer_unregister_thread(void)
279 {
280 int deferers;
281
282 internal_urcu_lock(&defer_thread_mutex);
283 internal_urcu_lock(&urcu_defer_mutex);
284 rcu_remove_deferer(pthread_self());
285 _rcu_defer_barrier_thread();
286 free(defer_queue.q);
287 defer_queue.q = NULL;
288 deferers = num_deferers;
289 internal_urcu_unlock(&urcu_defer_mutex);
290
291 if (deferers == 0)
292 stop_defer_thread();
293 internal_urcu_unlock(&defer_thread_mutex);
294 }
295
296 void urcu_defer_exit(void)
297 {
298 free(registry);
299 }
This page took 0.03524 seconds and 5 git commands to generate.