Cleanup: remove unused return value warning from tests
[urcu.git] / urcu-defer-impl.h
1 #ifndef _URCU_DEFER_IMPL_H
2 #define _URCU_DEFER_IMPL_H
3
4 /*
5 * urcu-defer-impl.h
6 *
7 * Userspace RCU header - memory reclamation.
8 *
9 * TO BE INCLUDED ONLY FROM URCU LIBRARY CODE. See urcu-defer.h for linking
10 * dynamically with the userspace rcu reclamation library.
11 *
12 * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 * Copyright (c) 2009 Paul E. McKenney, IBM Corporation.
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 *
29 * IBM's contributions to this file may be relicensed under LGPLv2 or later.
30 */
31
32 #include <stdlib.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <signal.h>
36 #include <assert.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <poll.h>
40 #include <sys/time.h>
41 #include <unistd.h>
42 #include <stdint.h>
43
44 #include "urcu/futex.h"
45
46 #include <urcu/compiler.h>
47 #include <urcu/arch.h>
48 #include <urcu/uatomic.h>
49 #include <urcu/list.h>
50 #include <urcu/system.h>
51 #include <urcu/tls-compat.h>
52 #include "urcu-die.h"
53
54 /*
55 * Number of entries in the per-thread defer queue. Must be power of 2.
56 */
57 #define DEFER_QUEUE_SIZE (1 << 12)
58 #define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1)
59
60 /*
61 * Typically, data is aligned at least on the architecture size.
62 * Use lowest bit to indicate that the current callback is changing.
63 * Assumes that (void *)-2L is not used often. Used to encode non-aligned
64 * functions and non-aligned data using extra space.
65 * We encode the (void *)-2L fct as: -2L, fct, data.
66 * We encode the (void *)-2L data as either:
67 * fct | DQ_FCT_BIT, data (if fct is aligned), or
68 * -2L, fct, data (if fct is not aligned).
69 * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
70 */
71 #define DQ_FCT_BIT (1 << 0)
72 #define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT)
73 #define DQ_SET_FCT_BIT(x) \
74 (x = (void *)((unsigned long)(x) | DQ_FCT_BIT))
75 #define DQ_CLEAR_FCT_BIT(x) \
76 (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT))
77 #define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT))
78
79 /*
80 * This code section can only be included in LGPL 2.1 compatible source code.
81 * See below for the function call wrappers which can be used in code meant to
82 * be only linked with the Userspace RCU library. This comes with a small
83 * performance degradation on the read-side due to the added function calls.
84 * This is required to permit relinking with newer versions of the library.
85 */
86
87 #ifdef DEBUG_RCU
88 #define rcu_assert(args...) assert(args)
89 #else
90 #define rcu_assert(args...)
91 #endif
92
93 /*
94 * defer queue.
95 * Contains pointers. Encoded to save space when same callback is often used.
96 * When looking up the next item:
97 * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr)
98 * - next element contains pointer to data.
99 * - else if item == DQ_FCT_MARK
100 * - set the current callback to next element ptr
101 * - following next element contains pointer to data.
102 * - else current element contains data
103 */
104 struct defer_queue {
105 unsigned long head; /* add element at head */
106 void *last_fct_in; /* last fct pointer encoded */
107 unsigned long tail; /* next element to remove at tail */
108 void *last_fct_out; /* last fct pointer encoded */
109 void **q;
110 /* registry information */
111 unsigned long last_head;
112 struct cds_list_head list; /* list of thread queues */
113 };
114
115 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
116 #include "urcu-defer.h"
117
118 void __attribute__((destructor)) rcu_defer_exit(void);
119
120 extern void synchronize_rcu(void);
121
122 /*
123 * rcu_defer_mutex nests inside defer_thread_mutex.
124 */
125 static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
126 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
127
128 static int32_t defer_thread_futex;
129 static int32_t defer_thread_stop;
130
131 /*
132 * Written to only by each individual deferer. Read by both the deferer and
133 * the reclamation tread.
134 */
135 static DEFINE_URCU_TLS(struct defer_queue, defer_queue);
136 static CDS_LIST_HEAD(registry_defer);
137 static pthread_t tid_defer;
138
139 static void mutex_lock_defer(pthread_mutex_t *mutex)
140 {
141 int ret;
142
143 #ifndef DISTRUST_SIGNALS_EXTREME
144 ret = pthread_mutex_lock(mutex);
145 if (ret)
146 urcu_die(ret);
147 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
148 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
149 if (ret != EBUSY && ret != EINTR)
150 urcu_die(ret);
151 (void) poll(NULL,0,10);
152 }
153 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
154 }
155
156 /*
157 * Wake-up any waiting defer thread. Called from many concurrent threads.
158 */
159 static void wake_up_defer(void)
160 {
161 if (caa_unlikely(uatomic_read(&defer_thread_futex) == -1)) {
162 uatomic_set(&defer_thread_futex, 0);
163 if (futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1,
164 NULL, NULL, 0) < 0)
165 urcu_die(errno);
166 }
167 }
168
169 static unsigned long rcu_defer_num_callbacks(void)
170 {
171 unsigned long num_items = 0, head;
172 struct defer_queue *index;
173
174 mutex_lock_defer(&rcu_defer_mutex);
175 cds_list_for_each_entry(index, &registry_defer, list) {
176 head = CMM_LOAD_SHARED(index->head);
177 num_items += head - index->tail;
178 }
179 mutex_unlock(&rcu_defer_mutex);
180 return num_items;
181 }
182
183 /*
184 * Defer thread waiting. Single thread.
185 */
186 static void wait_defer(void)
187 {
188 uatomic_dec(&defer_thread_futex);
189 /* Write futex before read queue */
190 /* Write futex before read defer_thread_stop */
191 cmm_smp_mb();
192 if (_CMM_LOAD_SHARED(defer_thread_stop)) {
193 uatomic_set(&defer_thread_futex, 0);
194 pthread_exit(0);
195 }
196 if (rcu_defer_num_callbacks()) {
197 cmm_smp_mb(); /* Read queue before write futex */
198 /* Callbacks are queued, don't wait. */
199 uatomic_set(&defer_thread_futex, 0);
200 } else {
201 cmm_smp_rmb(); /* Read queue before read futex */
202 if (uatomic_read(&defer_thread_futex) != -1)
203 return;
204 while (futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1,
205 NULL, NULL, 0)) {
206 switch (errno) {
207 case EWOULDBLOCK:
208 /* Value already changed. */
209 return;
210 case EINTR:
211 /* Retry if interrupted by signal. */
212 break; /* Get out of switch. */
213 default:
214 /* Unexpected error. */
215 urcu_die(errno);
216 }
217 }
218 }
219 }
220
221 /*
222 * Must be called after Q.S. is reached.
223 */
224 static void rcu_defer_barrier_queue(struct defer_queue *queue,
225 unsigned long head)
226 {
227 unsigned long i;
228 void (*fct)(void *p);
229 void *p;
230
231 /*
232 * Tail is only modified when lock is held.
233 * Head is only modified by owner thread.
234 */
235
236 for (i = queue->tail; i != head;) {
237 cmm_smp_rmb(); /* read head before q[]. */
238 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
239 if (caa_unlikely(DQ_IS_FCT_BIT(p))) {
240 DQ_CLEAR_FCT_BIT(p);
241 queue->last_fct_out = p;
242 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
243 } else if (caa_unlikely(p == DQ_FCT_MARK)) {
244 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
245 queue->last_fct_out = p;
246 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
247 }
248 fct = queue->last_fct_out;
249 fct(p);
250 }
251 cmm_smp_mb(); /* push tail after having used q[] */
252 CMM_STORE_SHARED(queue->tail, i);
253 }
254
255 static void _rcu_defer_barrier_thread(void)
256 {
257 unsigned long head, num_items;
258
259 head = URCU_TLS(defer_queue).head;
260 num_items = head - URCU_TLS(defer_queue).tail;
261 if (caa_unlikely(!num_items))
262 return;
263 synchronize_rcu();
264 rcu_defer_barrier_queue(&URCU_TLS(defer_queue), head);
265 }
266
267 void rcu_defer_barrier_thread(void)
268 {
269 mutex_lock_defer(&rcu_defer_mutex);
270 _rcu_defer_barrier_thread();
271 mutex_unlock(&rcu_defer_mutex);
272 }
273
274 /*
275 * rcu_defer_barrier - Execute all queued rcu callbacks.
276 *
277 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
278 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
279 * are guaranteed to be executed.
280 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
281 * execution are not guaranteed to be executed in the current batch (could
282 * be left for the next batch). These callbacks queued by other threads are only
283 * guaranteed to be executed if there is explicit synchronization between
284 * the thread adding to the queue and the thread issuing the defer_barrier call.
285 */
286
287 void rcu_defer_barrier(void)
288 {
289 struct defer_queue *index;
290 unsigned long num_items = 0;
291
292 if (cds_list_empty(&registry_defer))
293 return;
294
295 mutex_lock_defer(&rcu_defer_mutex);
296 cds_list_for_each_entry(index, &registry_defer, list) {
297 index->last_head = CMM_LOAD_SHARED(index->head);
298 num_items += index->last_head - index->tail;
299 }
300 if (caa_likely(!num_items)) {
301 /*
302 * We skip the grace period because there are no queued
303 * callbacks to execute.
304 */
305 goto end;
306 }
307 synchronize_rcu();
308 cds_list_for_each_entry(index, &registry_defer, list)
309 rcu_defer_barrier_queue(index, index->last_head);
310 end:
311 mutex_unlock(&rcu_defer_mutex);
312 }
313
314 /*
315 * _defer_rcu - Queue a RCU callback.
316 */
317 static void _defer_rcu(void (*fct)(void *p), void *p)
318 {
319 unsigned long head, tail;
320
321 /*
322 * Head is only modified by ourself. Tail can be modified by reclamation
323 * thread.
324 */
325 head = URCU_TLS(defer_queue).head;
326 tail = CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail);
327
328 /*
329 * If queue is full, or reached threshold. Empty queue ourself.
330 * Worse-case: must allow 2 supplementary entries for fct pointer.
331 */
332 if (caa_unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
333 assert(head - tail <= DEFER_QUEUE_SIZE);
334 rcu_defer_barrier_thread();
335 assert(head - CMM_LOAD_SHARED(URCU_TLS(defer_queue).tail) == 0);
336 }
337
338 /*
339 * Encode:
340 * if the function is not changed and the data is aligned and it is
341 * not the marker:
342 * store the data
343 * otherwise if the function is aligned and its not the marker:
344 * store the function with DQ_FCT_BIT
345 * store the data
346 * otherwise:
347 * store the marker (DQ_FCT_MARK)
348 * store the function
349 * store the data
350 *
351 * Decode: see the comments before 'struct defer_queue'
352 * or the code in rcu_defer_barrier_queue().
353 */
354 if (caa_unlikely(URCU_TLS(defer_queue).last_fct_in != fct
355 || DQ_IS_FCT_BIT(p)
356 || p == DQ_FCT_MARK)) {
357 URCU_TLS(defer_queue).last_fct_in = fct;
358 if (caa_unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
359 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
360 DQ_FCT_MARK);
361 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
362 fct);
363 } else {
364 DQ_SET_FCT_BIT(fct);
365 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK],
366 fct);
367 }
368 }
369 _CMM_STORE_SHARED(URCU_TLS(defer_queue).q[head++ & DEFER_QUEUE_MASK], p);
370 cmm_smp_wmb(); /* Publish new pointer before head */
371 /* Write q[] before head. */
372 CMM_STORE_SHARED(URCU_TLS(defer_queue).head, head);
373 cmm_smp_mb(); /* Write queue head before read futex */
374 /*
375 * Wake-up any waiting defer thread.
376 */
377 wake_up_defer();
378 }
379
380 static void *thr_defer(void *args)
381 {
382 for (;;) {
383 /*
384 * "Be green". Don't wake up the CPU if there is no RCU work
385 * to perform whatsoever. Aims at saving laptop battery life by
386 * leaving the processor in sleep state when idle.
387 */
388 wait_defer();
389 /* Sleeping after wait_defer to let many callbacks enqueue */
390 (void) poll(NULL,0,100); /* wait for 100ms */
391 rcu_defer_barrier();
392 }
393
394 return NULL;
395 }
396
397 /*
398 * library wrappers to be used by non-LGPL compatible source code.
399 */
400
401 void defer_rcu(void (*fct)(void *p), void *p)
402 {
403 _defer_rcu(fct, p);
404 }
405
406 static void start_defer_thread(void)
407 {
408 int ret;
409
410 ret = pthread_create(&tid_defer, NULL, thr_defer, NULL);
411 assert(!ret);
412 }
413
414 static void stop_defer_thread(void)
415 {
416 int ret;
417 void *tret;
418
419 _CMM_STORE_SHARED(defer_thread_stop, 1);
420 /* Store defer_thread_stop before testing futex */
421 cmm_smp_mb();
422 wake_up_defer();
423
424 ret = pthread_join(tid_defer, &tret);
425 assert(!ret);
426
427 CMM_STORE_SHARED(defer_thread_stop, 0);
428 /* defer thread should always exit when futex value is 0 */
429 assert(uatomic_read(&defer_thread_futex) == 0);
430 }
431
432 int rcu_defer_register_thread(void)
433 {
434 int was_empty;
435
436 assert(URCU_TLS(defer_queue).last_head == 0);
437 assert(URCU_TLS(defer_queue).q == NULL);
438 URCU_TLS(defer_queue).q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
439 if (!URCU_TLS(defer_queue).q)
440 return -ENOMEM;
441
442 mutex_lock_defer(&defer_thread_mutex);
443 mutex_lock_defer(&rcu_defer_mutex);
444 was_empty = cds_list_empty(&registry_defer);
445 cds_list_add(&URCU_TLS(defer_queue).list, &registry_defer);
446 mutex_unlock(&rcu_defer_mutex);
447
448 if (was_empty)
449 start_defer_thread();
450 mutex_unlock(&defer_thread_mutex);
451 return 0;
452 }
453
454 void rcu_defer_unregister_thread(void)
455 {
456 int is_empty;
457
458 mutex_lock_defer(&defer_thread_mutex);
459 mutex_lock_defer(&rcu_defer_mutex);
460 cds_list_del(&URCU_TLS(defer_queue).list);
461 _rcu_defer_barrier_thread();
462 free(URCU_TLS(defer_queue).q);
463 URCU_TLS(defer_queue).q = NULL;
464 is_empty = cds_list_empty(&registry_defer);
465 mutex_unlock(&rcu_defer_mutex);
466
467 if (is_empty)
468 stop_defer_thread();
469 mutex_unlock(&defer_thread_mutex);
470 }
471
472 void rcu_defer_exit(void)
473 {
474 assert(cds_list_empty(&registry_defer));
475 }
476
477 #endif /* _URCU_DEFER_IMPL_H */
This page took 0.037667 seconds and 4 git commands to generate.