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