Check for __linux__ before including syscall.h
[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 <syscall.h>
42 #include <unistd.h>
43 #include <stdint.h>
44
45 #include "urcu/futex.h"
46
47 #include <urcu/compiler.h>
48 #include <urcu/arch.h>
49 #include <urcu/uatomic.h>
50 #include <urcu/list.h>
51 #include <urcu/system.h>
52
53 /*
54 * Number of entries in the per-thread defer queue. Must be power of 2.
55 */
56 #define DEFER_QUEUE_SIZE (1 << 12)
57 #define DEFER_QUEUE_MASK (DEFER_QUEUE_SIZE - 1)
58
59 /*
60 * Typically, data is aligned at least on the architecture size.
61 * Use lowest bit to indicate that the current callback is changing.
62 * Assumes that (void *)-2L is not used often. Used to encode non-aligned
63 * functions and non-aligned data using extra space.
64 * We encode the (void *)-2L fct as: -2L, fct, data.
65 * We encode the (void *)-2L data as: -2L, fct, data.
66 * Here, DQ_FCT_MARK == ~DQ_FCT_BIT. Required for the test order.
67 */
68 #define DQ_FCT_BIT (1 << 0)
69 #define DQ_IS_FCT_BIT(x) ((unsigned long)(x) & DQ_FCT_BIT)
70 #define DQ_SET_FCT_BIT(x) \
71 (x = (void *)((unsigned long)(x) | DQ_FCT_BIT))
72 #define DQ_CLEAR_FCT_BIT(x) \
73 (x = (void *)((unsigned long)(x) & ~DQ_FCT_BIT))
74 #define DQ_FCT_MARK ((void *)(~DQ_FCT_BIT))
75
76 /*
77 * This code section can only be included in LGPL 2.1 compatible source code.
78 * See below for the function call wrappers which can be used in code meant to
79 * be only linked with the Userspace RCU library. This comes with a small
80 * performance degradation on the read-side due to the added function calls.
81 * This is required to permit relinking with newer versions of the library.
82 */
83
84 #ifdef DEBUG_RCU
85 #define rcu_assert(args...) assert(args)
86 #else
87 #define rcu_assert(args...)
88 #endif
89
90 /*
91 * defer queue.
92 * Contains pointers. Encoded to save space when same callback is often used.
93 * When looking up the next item:
94 * - if DQ_FCT_BIT is set, set the current callback to DQ_CLEAR_FCT_BIT(ptr)
95 * - next element contains pointer to data.
96 * - else if item == DQ_FCT_MARK
97 * - set the current callback to next element ptr
98 * - following next element contains pointer to data.
99 * - else current element contains data
100 */
101 struct defer_queue {
102 unsigned long head; /* add element at head */
103 void *last_fct_in; /* last fct pointer encoded */
104 unsigned long tail; /* next element to remove at tail */
105 void *last_fct_out; /* last fct pointer encoded */
106 void **q;
107 /* registry information */
108 unsigned long last_head;
109 struct cds_list_head list; /* list of thread queues */
110 };
111
112 /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */
113 #include "urcu-defer.h"
114
115 void __attribute__((destructor)) rcu_defer_exit(void);
116
117 extern void synchronize_rcu(void);
118
119 /*
120 * rcu_defer_mutex nests inside defer_thread_mutex.
121 */
122 static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
123 static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
124
125 static int32_t defer_thread_futex;
126
127 /*
128 * Written to only by each individual deferer. Read by both the deferer and
129 * the reclamation tread.
130 */
131 static struct defer_queue __thread defer_queue;
132 static CDS_LIST_HEAD(registry_defer);
133 static pthread_t tid_defer;
134
135 static void mutex_lock_defer(pthread_mutex_t *mutex)
136 {
137 int ret;
138
139 #ifndef DISTRUST_SIGNALS_EXTREME
140 ret = pthread_mutex_lock(mutex);
141 if (ret) {
142 perror("Error in pthread mutex lock");
143 exit(-1);
144 }
145 #else /* #ifndef DISTRUST_SIGNALS_EXTREME */
146 while ((ret = pthread_mutex_trylock(mutex)) != 0) {
147 if (ret != EBUSY && ret != EINTR) {
148 printf("ret = %d, errno = %d\n", ret, errno);
149 perror("Error in pthread mutex lock");
150 exit(-1);
151 }
152 pthread_testcancel();
153 poll(NULL,0,10);
154 }
155 #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */
156 }
157
158 /*
159 * Wake-up any waiting defer thread. Called from many concurrent threads.
160 */
161 static void wake_up_defer(void)
162 {
163 if (unlikely(uatomic_read(&defer_thread_futex) == -1)) {
164 uatomic_set(&defer_thread_futex, 0);
165 futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1,
166 NULL, NULL, 0);
167 }
168 }
169
170 static unsigned long rcu_defer_num_callbacks(void)
171 {
172 unsigned long num_items = 0, head;
173 struct defer_queue *index;
174
175 mutex_lock_defer(&rcu_defer_mutex);
176 cds_list_for_each_entry(index, &registry_defer, list) {
177 head = CMM_LOAD_SHARED(index->head);
178 num_items += head - index->tail;
179 }
180 mutex_unlock(&rcu_defer_mutex);
181 return num_items;
182 }
183
184 /*
185 * Defer thread waiting. Single thread.
186 */
187 static void wait_defer(void)
188 {
189 uatomic_dec(&defer_thread_futex);
190 cmm_smp_mb(); /* Write futex before read queue */
191 if (rcu_defer_num_callbacks()) {
192 cmm_smp_mb(); /* Read queue before write futex */
193 /* Callbacks are queued, don't wait. */
194 uatomic_set(&defer_thread_futex, 0);
195 } else {
196 cmm_smp_rmb(); /* Read queue before read futex */
197 if (uatomic_read(&defer_thread_futex) == -1)
198 futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1,
199 NULL, NULL, 0);
200 }
201 }
202
203 /*
204 * Must be called after Q.S. is reached.
205 */
206 static void rcu_defer_barrier_queue(struct defer_queue *queue,
207 unsigned long head)
208 {
209 unsigned long i;
210 void (*fct)(void *p);
211 void *p;
212
213 /*
214 * Tail is only modified when lock is held.
215 * Head is only modified by owner thread.
216 */
217
218 for (i = queue->tail; i != head;) {
219 cmm_smp_rmb(); /* read head before q[]. */
220 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
221 if (unlikely(DQ_IS_FCT_BIT(p))) {
222 DQ_CLEAR_FCT_BIT(p);
223 queue->last_fct_out = p;
224 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
225 } else if (unlikely(p == DQ_FCT_MARK)) {
226 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
227 queue->last_fct_out = p;
228 p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]);
229 }
230 fct = queue->last_fct_out;
231 fct(p);
232 }
233 cmm_smp_mb(); /* push tail after having used q[] */
234 CMM_STORE_SHARED(queue->tail, i);
235 }
236
237 static void _rcu_defer_barrier_thread(void)
238 {
239 unsigned long head, num_items;
240
241 head = defer_queue.head;
242 num_items = head - defer_queue.tail;
243 if (unlikely(!num_items))
244 return;
245 synchronize_rcu();
246 rcu_defer_barrier_queue(&defer_queue, head);
247 }
248
249 void rcu_defer_barrier_thread(void)
250 {
251 mutex_lock_defer(&rcu_defer_mutex);
252 _rcu_defer_barrier_thread();
253 mutex_unlock(&rcu_defer_mutex);
254 }
255
256 /*
257 * rcu_defer_barrier - Execute all queued rcu callbacks.
258 *
259 * Execute all RCU callbacks queued before rcu_defer_barrier() execution.
260 * All callbacks queued on the local thread prior to a rcu_defer_barrier() call
261 * are guaranteed to be executed.
262 * Callbacks queued by other threads concurrently with rcu_defer_barrier()
263 * execution are not guaranteed to be executed in the current batch (could
264 * be left for the next batch). These callbacks queued by other threads are only
265 * guaranteed to be executed if there is explicit synchronization between
266 * the thread adding to the queue and the thread issuing the defer_barrier call.
267 */
268
269 void rcu_defer_barrier(void)
270 {
271 struct defer_queue *index;
272 unsigned long num_items = 0;
273
274 if (cds_list_empty(&registry_defer))
275 return;
276
277 mutex_lock_defer(&rcu_defer_mutex);
278 cds_list_for_each_entry(index, &registry_defer, list) {
279 index->last_head = CMM_LOAD_SHARED(index->head);
280 num_items += index->last_head - index->tail;
281 }
282 if (likely(!num_items)) {
283 /*
284 * We skip the grace period because there are no queued
285 * callbacks to execute.
286 */
287 goto end;
288 }
289 synchronize_rcu();
290 cds_list_for_each_entry(index, &registry_defer, list)
291 rcu_defer_barrier_queue(index, index->last_head);
292 end:
293 mutex_unlock(&rcu_defer_mutex);
294 }
295
296 /*
297 * _defer_rcu - Queue a RCU callback.
298 */
299 void _defer_rcu(void (*fct)(void *p), void *p)
300 {
301 unsigned long head, tail;
302
303 /*
304 * Head is only modified by ourself. Tail can be modified by reclamation
305 * thread.
306 */
307 head = defer_queue.head;
308 tail = CMM_LOAD_SHARED(defer_queue.tail);
309
310 /*
311 * If queue is full, or reached threshold. Empty queue ourself.
312 * Worse-case: must allow 2 supplementary entries for fct pointer.
313 */
314 if (unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) {
315 assert(head - tail <= DEFER_QUEUE_SIZE);
316 rcu_defer_barrier_thread();
317 assert(head - CMM_LOAD_SHARED(defer_queue.tail) == 0);
318 }
319
320 if (unlikely(defer_queue.last_fct_in != fct)) {
321 defer_queue.last_fct_in = fct;
322 if (unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) {
323 /*
324 * If the function to encode is not aligned or the
325 * marker, write DQ_FCT_MARK followed by the function
326 * pointer.
327 */
328 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
329 DQ_FCT_MARK);
330 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
331 fct);
332 } else {
333 DQ_SET_FCT_BIT(fct);
334 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
335 fct);
336 }
337 } else {
338 if (unlikely(DQ_IS_FCT_BIT(p) || p == DQ_FCT_MARK)) {
339 /*
340 * If the data to encode is not aligned or the marker,
341 * write DQ_FCT_MARK followed by the function pointer.
342 */
343 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
344 DQ_FCT_MARK);
345 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK],
346 fct);
347 }
348 }
349 _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], p);
350 cmm_smp_wmb(); /* Publish new pointer before head */
351 /* Write q[] before head. */
352 CMM_STORE_SHARED(defer_queue.head, head);
353 cmm_smp_mb(); /* Write queue head before read futex */
354 /*
355 * Wake-up any waiting defer thread.
356 */
357 wake_up_defer();
358 }
359
360 void *thr_defer(void *args)
361 {
362 for (;;) {
363 pthread_testcancel();
364 /*
365 * "Be green". Don't wake up the CPU if there is no RCU work
366 * to perform whatsoever. Aims at saving laptop battery life by
367 * leaving the processor in sleep state when idle.
368 */
369 wait_defer();
370 /* Sleeping after wait_defer to let many callbacks enqueue */
371 poll(NULL,0,100); /* wait for 100ms */
372 rcu_defer_barrier();
373 }
374
375 return NULL;
376 }
377
378 /*
379 * library wrappers to be used by non-LGPL compatible source code.
380 */
381
382 void defer_rcu(void (*fct)(void *p), void *p)
383 {
384 _defer_rcu(fct, p);
385 }
386
387 static void start_defer_thread(void)
388 {
389 int ret;
390
391 ret = pthread_create(&tid_defer, NULL, thr_defer, NULL);
392 assert(!ret);
393 }
394
395 static void stop_defer_thread(void)
396 {
397 int ret;
398 void *tret;
399
400 pthread_cancel(tid_defer);
401 wake_up_defer();
402 ret = pthread_join(tid_defer, &tret);
403 assert(!ret);
404 }
405
406 int rcu_defer_register_thread(void)
407 {
408 int was_empty;
409
410 assert(defer_queue.last_head == 0);
411 assert(defer_queue.q == NULL);
412 defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE);
413 if (!defer_queue.q)
414 return -ENOMEM;
415
416 mutex_lock_defer(&defer_thread_mutex);
417 mutex_lock_defer(&rcu_defer_mutex);
418 was_empty = cds_list_empty(&registry_defer);
419 cds_list_add(&defer_queue.list, &registry_defer);
420 mutex_unlock(&rcu_defer_mutex);
421
422 if (was_empty)
423 start_defer_thread();
424 mutex_unlock(&defer_thread_mutex);
425 return 0;
426 }
427
428 void rcu_defer_unregister_thread(void)
429 {
430 int is_empty;
431
432 mutex_lock_defer(&defer_thread_mutex);
433 mutex_lock_defer(&rcu_defer_mutex);
434 cds_list_del(&defer_queue.list);
435 _rcu_defer_barrier_thread();
436 free(defer_queue.q);
437 defer_queue.q = NULL;
438 is_empty = cds_list_empty(&registry_defer);
439 mutex_unlock(&rcu_defer_mutex);
440
441 if (is_empty)
442 stop_defer_thread();
443 mutex_unlock(&defer_thread_mutex);
444 }
445
446 void rcu_defer_exit(void)
447 {
448 assert(cds_list_empty(&registry_defer));
449 }
450
451 #endif /* _URCU_DEFER_IMPL_H */
This page took 0.042847 seconds and 4 git commands to generate.