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