Fix urcu-defer: add missing brackets.
[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 #include <linux/futex.h>
32 #include <sys/time.h>
33 #include <syscall.h>
34 #include <unistd.h>
35
36 #include "urcu-defer-static.h"
37 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
38 #include "urcu-defer.h"
39
40 #define futex(...) syscall(__NR_futex, __VA_ARGS__)
41
42 void __attribute__((destructor)) urcu_defer_exit(void);
43
44 extern void synchronize_rcu(void);
45
46 /*
47 * urcu_defer_mutex nests inside defer_thread_mutex.
48 */
49 static pthread_mutex_t urcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
50 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
51
52 static int defer_thread_futex;
53
54 /*
55 * Written to only by each individual deferer. Read by both the deferer and
56 * the reclamation tread.
57 */
58 static struct defer_queue __thread defer_queue;
59
60 /* Thread IDs of registered deferers */
61 #define INIT_NUM_THREADS 4
62
63 struct deferer_registry {
64 pthread_t tid;
65 struct defer_queue *defer_queue;
66 unsigned long last_head;
67 };
68
69 static struct deferer_registry *registry;
70 static int num_deferers, alloc_deferers;
71
72 static pthread_t tid_defer;
73
74 /*
75 * Wake-up any waiting defer thread. Called from many concurrent threads.
76 */
77 static void wake_up_defer(void)
78 {
79 if (unlikely(atomic_read(&defer_thread_futex) == -1)) {
80 atomic_set(&defer_thread_futex, 0);
81 futex(&defer_thread_futex, FUTEX_WAKE, 0,
82 NULL, NULL, 0);
83 }
84 }
85
86 /*
87 * Defer thread waiting. Single thread.
88 */
89 static void wait_defer(void)
90 {
91 atomic_dec(&defer_thread_futex);
92 if (atomic_read(&defer_thread_futex) == -1)
93 futex(&defer_thread_futex, FUTEX_WAIT, -1,
94 NULL, NULL, 0);
95 }
96
97 static void internal_urcu_lock(pthread_mutex_t *mutex)
98 {
99 int ret;
100
101 #ifndef DISTRUST_SIGNALS_EXTREME
102 ret = pthread_mutex_lock(mutex);
103 if (ret) {
104 perror("Error in pthread mutex lock");
105 exit(-1);
106 }
107 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
108 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
109 if (ret != EBUSY && ret != EINTR) {
110 printf("ret = %d, errno = %d\n", ret, errno);
111 perror("Error in pthread mutex lock");
112 exit(-1);
113 }
114 pthread_testcancel();
115 poll(NULL,0,10);
116 }
117 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
118 }
119
120 static void internal_urcu_unlock(pthread_mutex_t *mutex)
121 {
122 int ret;
123
124 ret = pthread_mutex_unlock(mutex);
125 if (ret) {
126 perror("Error in pthread mutex unlock");
127 exit(-1);
128 }
129 }
130
131 /*
132 * Must be called after Q.S. is reached.
133 */
134 static void rcu_defer_barrier_queue(struct defer_queue *queue,
135 unsigned long head)
136 {
137 unsigned long i;
138 void (*fct)(void *p);
139 void *p;
140
141 /*
142 * Tail is only modified when lock is held.
143 * Head is only modified by owner thread.
144 */
145
146 for (i = queue->tail; i != head;) {
147 smp_rmb(); /* read head before q[]. */
148 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
149 if (unlikely(DQ_IS_FCT_BIT(p))) {
150 DQ_CLEAR_FCT_BIT(p);
151 queue->last_fct_out = p;
152 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
153 } else if (unlikely(p == DQ_FCT_MARK)) {
154 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
155 queue->last_fct_out = p;
156 p = LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
157 }
158 fct = queue->last_fct_out;
159 fct(p);
160 }
161 smp_mb(); /* push tail after having used q[] */
162 STORE_SHARED(queue->tail, i);
163 }
164
165 static void _rcu_defer_barrier_thread(void)
166 {
167 unsigned long head, num_items;
168
169 head = defer_queue.head;
170 num_items = head - defer_queue.tail;
171 if (unlikely(!num_items))
172 return;
173 synchronize_rcu();
174 rcu_defer_barrier_queue(&defer_queue, head);
175 }
176
177
178 void rcu_defer_barrier_thread(void)
179 {
180 internal_urcu_lock(&urcu_defer_mutex);
181 _rcu_defer_barrier_thread();
182 internal_urcu_unlock(&urcu_defer_mutex);
183 }
184
185 /*
186 * rcu_defer_barrier - Execute all queued rcu callbacks.
187 *
188 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
189 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
190 * are guaranteed to be executed.
191 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
192 * execution are not guaranteed to be executed in the current batch (could
193 * be left for the next batch). These callbacks queued by other threads are only
194 * guaranteed to be executed if there is explicit synchronization between
195 * the thread adding to the queue and the thread issuing the defer_barrier call.
196 */
197
198 void rcu_defer_barrier(void)
199 {
200 struct deferer_registry *index;
201 unsigned long num_items = 0;
202
203 if (!registry)
204 return;
205
206 internal_urcu_lock(&urcu_defer_mutex);
207 for (index = registry; index < registry + num_deferers; index++) {
208 index->last_head = LOAD_SHARED(index->defer_queue->head);
209 num_items += index->last_head - index->defer_queue->tail;
210 }
211 if (likely(!num_items)) {
212 /*
213 * We skip the grace period because there are no queued
214 * callbacks to execute.
215 */
216 goto end;
217 }
218 synchronize_rcu();
219 for (index = registry; index < registry + num_deferers; index++)
220 rcu_defer_barrier_queue(index->defer_queue,
221 index->last_head);
222 end:
223 internal_urcu_unlock(&urcu_defer_mutex);
224 }
225
226 /*
227 * _rcu_defer_queue - Queue a RCU callback.
228 */
229 void _rcu_defer_queue(void (*fct)(void *p), void *p)
230 {
231 unsigned long head, tail;
232
233 /*
234 * Head is only modified by ourself. Tail can be modified by reclamation
235 * thread.
236 */
237 head = defer_queue.head;
238 tail = LOAD_SHARED(defer_queue.tail);
239
240 /*
241 * If queue is full, empty it ourself.
242 * Worse-case: must allow 2 supplementary entries for fct pointer.
243 */
244 if (unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
245 assert(head - tail <= DEFER_QUEUE_SIZE);
246 rcu_defer_barrier_thread();
247 assert(head - LOAD_SHARED(defer_queue.tail) == 0);
248 }
249
250 if (unlikely(defer_queue.last_fct_in != fct)) {
251 defer_queue.last_fct_in = fct;
252 if (unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
253 /*
254 * If the function to encode is not aligned or the
255 * marker, write DQ_FCT_MARK followed by the function
256 * pointer.
257 */
258 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
259 DQ_FCT_MARK);
260 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
261 fct);
262 } else {
263 DQ_SET_FCT_BIT(fct);
264 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
265 fct);
266 }
267 } else {
268 if (unlikely(DQ_IS_FCT_BIT(p) || p == DQ_FCT_MARK)) {
269 /*
270 * If the data to encode is not aligned or the marker,
271 * write DQ_FCT_MARK followed by the function pointer.
272 */
273 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
274 DQ_FCT_MARK);
275 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
276 fct);
277 }
278 }
279 _STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], p);
280 smp_wmb(); /* Publish new pointer before head */
281 /* Write q[] before head. */
282 STORE_SHARED(defer_queue.head, head);
283 /*
284 * Wake-up any waiting defer thread.
285 */
286 wake_up_defer();
287 }
288
289 void *thr_defer(void *args)
290 {
291 for (;;) {
292 pthread_testcancel();
293 /*
294 * "Be green". Don't wake up the CPU if there is no RCU work
295 * to perform whatsoever. Aims at saving laptop battery life by
296 * leaving the processor in sleep state when idle.
297 */
298 wait_defer();
299 /* Sleeping after wait_defer to let many callbacks enqueue */
300 poll(NULL,0,100); /* wait for 100ms */
301 rcu_defer_barrier();
302 }
303
304 return NULL;
305 }
306
307 /*
308 * library wrappers to be used by non-LGPL compatible source code.
309 */
310
311 void rcu_defer_queue(void (*fct)(void *p), void *p)
312 {
313 _rcu_defer_queue(fct, p);
314 }
315
316 static void rcu_add_deferer(pthread_t id)
317 {
318 struct deferer_registry *oldarray;
319
320 if (!registry) {
321 alloc_deferers = INIT_NUM_THREADS;
322 num_deferers = 0;
323 registry =
324 malloc(sizeof(struct deferer_registry) * alloc_deferers);
325 }
326 if (alloc_deferers < num_deferers + 1) {
327 oldarray = registry;
328 registry = malloc(sizeof(struct deferer_registry)
329 * (alloc_deferers << 1));
330 memcpy(registry, oldarray,
331 sizeof(struct deferer_registry) * alloc_deferers);
332 alloc_deferers <<= 1;
333 free(oldarray);
334 }
335 registry[num_deferers].tid = id;
336 /* reference to the TLS of _this_ deferer thread. */
337 registry[num_deferers].defer_queue = &defer_queue;
338 registry[num_deferers].last_head = 0;
339 num_deferers++;
340 }
341
342 /*
343 * Never shrink (implementation limitation).
344 * This is O(nb threads). Eventually use a hash table.
345 */
346 static void rcu_remove_deferer(pthread_t id)
347 {
348 struct deferer_registry *index;
349
350 assert(registry != NULL);
351 for (index = registry; index < registry + num_deferers; index++) {
352 if (pthread_equal(index->tid, id)) {
353 memcpy(index, &registry[num_deferers - 1],
354 sizeof(struct deferer_registry));
355 registry[num_deferers - 1].tid = 0;
356 registry[num_deferers - 1].defer_queue = NULL;
357 registry[num_deferers - 1].last_head = 0;
358 num_deferers--;
359 return;
360 }
361 }
362 /* Hrm not found, forgot to register ? */
363 assert(0);
364 }
365
366 static void start_defer_thread(void)
367 {
368 int ret;
369
370 ret = pthread_create(&tid_defer, NULL, thr_defer,
371 NULL);
372 assert(!ret);
373 }
374
375 static void stop_defer_thread(void)
376 {
377 int ret;
378 void *tret;
379
380 pthread_cancel(tid_defer);
381 wake_up_defer();
382 ret = pthread_join(tid_defer, &tret);
383 assert(!ret);
384 }
385
386 void rcu_defer_register_thread(void)
387 {
388 int deferers;
389
390 internal_urcu_lock(&defer_thread_mutex);
391 internal_urcu_lock(&urcu_defer_mutex);
392 defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
393 rcu_add_deferer(pthread_self());
394 deferers = num_deferers;
395 internal_urcu_unlock(&urcu_defer_mutex);
396
397 if (deferers == 1)
398 start_defer_thread();
399 internal_urcu_unlock(&defer_thread_mutex);
400 }
401
402 void rcu_defer_unregister_thread(void)
403 {
404 int deferers;
405
406 internal_urcu_lock(&defer_thread_mutex);
407 internal_urcu_lock(&urcu_defer_mutex);
408 rcu_remove_deferer(pthread_self());
409 _rcu_defer_barrier_thread();
410 free(defer_queue.q);
411 defer_queue.q = NULL;
412 deferers = num_deferers;
413 internal_urcu_unlock(&urcu_defer_mutex);
414
415 if (deferers == 0)
416 stop_defer_thread();
417 internal_urcu_unlock(&defer_thread_mutex);
418 }
419
420 void urcu_defer_exit(void)
421 {
422 free(registry);
423 }
This page took 0.037457 seconds and 5 git commands to generate.