| 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 | |
| 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 | */ |
| 100 | struct 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 | |
| 111 | /* Do not #define _LGPL_SOURCE to ensure we can emit the wrapper symbols */ |
| 112 | #include "urcu-defer.h" |
| 113 | |
| 114 | void __attribute__((destructor)) rcu_defer_exit(void); |
| 115 | |
| 116 | extern void synchronize_rcu(void); |
| 117 | |
| 118 | /* |
| 119 | * rcu_defer_mutex nests inside defer_thread_mutex. |
| 120 | */ |
| 121 | static pthread_mutex_t rcu_defer_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 122 | static pthread_mutex_t defer_thread_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 123 | |
| 124 | static int32_t defer_thread_futex; |
| 125 | |
| 126 | /* |
| 127 | * Written to only by each individual deferer. Read by both the deferer and |
| 128 | * the reclamation tread. |
| 129 | */ |
| 130 | static struct defer_queue __thread defer_queue; |
| 131 | static CDS_LIST_HEAD(registry_defer); |
| 132 | static pthread_t tid_defer; |
| 133 | |
| 134 | static void mutex_lock_defer(pthread_mutex_t *mutex) |
| 135 | { |
| 136 | int ret; |
| 137 | |
| 138 | #ifndef DISTRUST_SIGNALS_EXTREME |
| 139 | ret = pthread_mutex_lock(mutex); |
| 140 | if (ret) { |
| 141 | perror("Error in pthread mutex lock"); |
| 142 | exit(-1); |
| 143 | } |
| 144 | #else /* #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 145 | while ((ret = pthread_mutex_trylock(mutex)) != 0) { |
| 146 | if (ret != EBUSY && ret != EINTR) { |
| 147 | printf("ret = %d, errno = %d\n", ret, errno); |
| 148 | perror("Error in pthread mutex lock"); |
| 149 | exit(-1); |
| 150 | } |
| 151 | pthread_testcancel(); |
| 152 | poll(NULL,0,10); |
| 153 | } |
| 154 | #endif /* #else #ifndef DISTRUST_SIGNALS_EXTREME */ |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Wake-up any waiting defer thread. Called from many concurrent threads. |
| 159 | */ |
| 160 | static void wake_up_defer(void) |
| 161 | { |
| 162 | if (unlikely(uatomic_read(&defer_thread_futex) == -1)) { |
| 163 | uatomic_set(&defer_thread_futex, 0); |
| 164 | futex_noasync(&defer_thread_futex, FUTEX_WAKE, 1, |
| 165 | NULL, NULL, 0); |
| 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, ®istry_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 | cmm_smp_mb(); /* Write futex before read queue */ |
| 190 | if (rcu_defer_num_callbacks()) { |
| 191 | cmm_smp_mb(); /* Read queue before write futex */ |
| 192 | /* Callbacks are queued, don't wait. */ |
| 193 | uatomic_set(&defer_thread_futex, 0); |
| 194 | } else { |
| 195 | cmm_smp_rmb(); /* Read queue before read futex */ |
| 196 | if (uatomic_read(&defer_thread_futex) == -1) |
| 197 | futex_noasync(&defer_thread_futex, FUTEX_WAIT, -1, |
| 198 | NULL, NULL, 0); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Must be called after Q.S. is reached. |
| 204 | */ |
| 205 | static void rcu_defer_barrier_queue(struct defer_queue *queue, |
| 206 | unsigned long head) |
| 207 | { |
| 208 | unsigned long i; |
| 209 | void (*fct)(void *p); |
| 210 | void *p; |
| 211 | |
| 212 | /* |
| 213 | * Tail is only modified when lock is held. |
| 214 | * Head is only modified by owner thread. |
| 215 | */ |
| 216 | |
| 217 | for (i = queue->tail; i != head;) { |
| 218 | cmm_smp_rmb(); /* read head before q[]. */ |
| 219 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 220 | if (unlikely(DQ_IS_FCT_BIT(p))) { |
| 221 | DQ_CLEAR_FCT_BIT(p); |
| 222 | queue->last_fct_out = p; |
| 223 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 224 | } else if (unlikely(p == DQ_FCT_MARK)) { |
| 225 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 226 | queue->last_fct_out = p; |
| 227 | p = CMM_LOAD_SHARED(queue->q[i++ & DEFER_QUEUE_MASK]); |
| 228 | } |
| 229 | fct = queue->last_fct_out; |
| 230 | fct(p); |
| 231 | } |
| 232 | cmm_smp_mb(); /* push tail after having used q[] */ |
| 233 | CMM_STORE_SHARED(queue->tail, i); |
| 234 | } |
| 235 | |
| 236 | static void _rcu_defer_barrier_thread(void) |
| 237 | { |
| 238 | unsigned long head, num_items; |
| 239 | |
| 240 | head = defer_queue.head; |
| 241 | num_items = head - defer_queue.tail; |
| 242 | if (unlikely(!num_items)) |
| 243 | return; |
| 244 | synchronize_rcu(); |
| 245 | rcu_defer_barrier_queue(&defer_queue, head); |
| 246 | } |
| 247 | |
| 248 | void rcu_defer_barrier_thread(void) |
| 249 | { |
| 250 | mutex_lock_defer(&rcu_defer_mutex); |
| 251 | _rcu_defer_barrier_thread(); |
| 252 | mutex_unlock(&rcu_defer_mutex); |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * rcu_defer_barrier - Execute all queued rcu callbacks. |
| 257 | * |
| 258 | * Execute all RCU callbacks queued before rcu_defer_barrier() execution. |
| 259 | * All callbacks queued on the local thread prior to a rcu_defer_barrier() call |
| 260 | * are guaranteed to be executed. |
| 261 | * Callbacks queued by other threads concurrently with rcu_defer_barrier() |
| 262 | * execution are not guaranteed to be executed in the current batch (could |
| 263 | * be left for the next batch). These callbacks queued by other threads are only |
| 264 | * guaranteed to be executed if there is explicit synchronization between |
| 265 | * the thread adding to the queue and the thread issuing the defer_barrier call. |
| 266 | */ |
| 267 | |
| 268 | void rcu_defer_barrier(void) |
| 269 | { |
| 270 | struct defer_queue *index; |
| 271 | unsigned long num_items = 0; |
| 272 | |
| 273 | if (cds_list_empty(®istry_defer)) |
| 274 | return; |
| 275 | |
| 276 | mutex_lock_defer(&rcu_defer_mutex); |
| 277 | cds_list_for_each_entry(index, ®istry_defer, list) { |
| 278 | index->last_head = CMM_LOAD_SHARED(index->head); |
| 279 | num_items += index->last_head - index->tail; |
| 280 | } |
| 281 | if (likely(!num_items)) { |
| 282 | /* |
| 283 | * We skip the grace period because there are no queued |
| 284 | * callbacks to execute. |
| 285 | */ |
| 286 | goto end; |
| 287 | } |
| 288 | synchronize_rcu(); |
| 289 | cds_list_for_each_entry(index, ®istry_defer, list) |
| 290 | rcu_defer_barrier_queue(index, index->last_head); |
| 291 | end: |
| 292 | mutex_unlock(&rcu_defer_mutex); |
| 293 | } |
| 294 | |
| 295 | /* |
| 296 | * _defer_rcu - Queue a RCU callback. |
| 297 | */ |
| 298 | void _defer_rcu(void (*fct)(void *p), void *p) |
| 299 | { |
| 300 | unsigned long head, tail; |
| 301 | |
| 302 | /* |
| 303 | * Head is only modified by ourself. Tail can be modified by reclamation |
| 304 | * thread. |
| 305 | */ |
| 306 | head = defer_queue.head; |
| 307 | tail = CMM_LOAD_SHARED(defer_queue.tail); |
| 308 | |
| 309 | /* |
| 310 | * If queue is full, or reached threshold. Empty queue ourself. |
| 311 | * Worse-case: must allow 2 supplementary entries for fct pointer. |
| 312 | */ |
| 313 | if (unlikely(head - tail >= DEFER_QUEUE_SIZE - 2)) { |
| 314 | assert(head - tail <= DEFER_QUEUE_SIZE); |
| 315 | rcu_defer_barrier_thread(); |
| 316 | assert(head - CMM_LOAD_SHARED(defer_queue.tail) == 0); |
| 317 | } |
| 318 | |
| 319 | if (unlikely(defer_queue.last_fct_in != fct)) { |
| 320 | defer_queue.last_fct_in = fct; |
| 321 | if (unlikely(DQ_IS_FCT_BIT(fct) || fct == DQ_FCT_MARK)) { |
| 322 | /* |
| 323 | * If the function to encode is not aligned or the |
| 324 | * marker, write DQ_FCT_MARK followed by the function |
| 325 | * pointer. |
| 326 | */ |
| 327 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], |
| 328 | DQ_FCT_MARK); |
| 329 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], |
| 330 | fct); |
| 331 | } else { |
| 332 | DQ_SET_FCT_BIT(fct); |
| 333 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], |
| 334 | fct); |
| 335 | } |
| 336 | } else { |
| 337 | if (unlikely(DQ_IS_FCT_BIT(p) || p == DQ_FCT_MARK)) { |
| 338 | /* |
| 339 | * If the data to encode is not aligned or the marker, |
| 340 | * write DQ_FCT_MARK followed by the function pointer. |
| 341 | */ |
| 342 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], |
| 343 | DQ_FCT_MARK); |
| 344 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], |
| 345 | fct); |
| 346 | } |
| 347 | } |
| 348 | _CMM_STORE_SHARED(defer_queue.q[head++ & DEFER_QUEUE_MASK], p); |
| 349 | cmm_smp_wmb(); /* Publish new pointer before head */ |
| 350 | /* Write q[] before head. */ |
| 351 | CMM_STORE_SHARED(defer_queue.head, head); |
| 352 | cmm_smp_mb(); /* Write queue head before read futex */ |
| 353 | /* |
| 354 | * Wake-up any waiting defer thread. |
| 355 | */ |
| 356 | wake_up_defer(); |
| 357 | } |
| 358 | |
| 359 | void *thr_defer(void *args) |
| 360 | { |
| 361 | for (;;) { |
| 362 | pthread_testcancel(); |
| 363 | /* |
| 364 | * "Be green". Don't wake up the CPU if there is no RCU work |
| 365 | * to perform whatsoever. Aims at saving laptop battery life by |
| 366 | * leaving the processor in sleep state when idle. |
| 367 | */ |
| 368 | wait_defer(); |
| 369 | /* Sleeping after wait_defer to let many callbacks enqueue */ |
| 370 | poll(NULL,0,100); /* wait for 100ms */ |
| 371 | rcu_defer_barrier(); |
| 372 | } |
| 373 | |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * library wrappers to be used by non-LGPL compatible source code. |
| 379 | */ |
| 380 | |
| 381 | void defer_rcu(void (*fct)(void *p), void *p) |
| 382 | { |
| 383 | _defer_rcu(fct, p); |
| 384 | } |
| 385 | |
| 386 | static void start_defer_thread(void) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | ret = pthread_create(&tid_defer, NULL, thr_defer, NULL); |
| 391 | assert(!ret); |
| 392 | } |
| 393 | |
| 394 | static void stop_defer_thread(void) |
| 395 | { |
| 396 | int ret; |
| 397 | void *tret; |
| 398 | |
| 399 | pthread_cancel(tid_defer); |
| 400 | wake_up_defer(); |
| 401 | ret = pthread_join(tid_defer, &tret); |
| 402 | assert(!ret); |
| 403 | } |
| 404 | |
| 405 | int rcu_defer_register_thread(void) |
| 406 | { |
| 407 | int was_empty; |
| 408 | |
| 409 | assert(defer_queue.last_head == 0); |
| 410 | assert(defer_queue.q == NULL); |
| 411 | defer_queue.q = malloc(sizeof(void *) * DEFER_QUEUE_SIZE); |
| 412 | if (!defer_queue.q) |
| 413 | return -ENOMEM; |
| 414 | |
| 415 | mutex_lock_defer(&defer_thread_mutex); |
| 416 | mutex_lock_defer(&rcu_defer_mutex); |
| 417 | was_empty = cds_list_empty(®istry_defer); |
| 418 | cds_list_add(&defer_queue.list, ®istry_defer); |
| 419 | mutex_unlock(&rcu_defer_mutex); |
| 420 | |
| 421 | if (was_empty) |
| 422 | start_defer_thread(); |
| 423 | mutex_unlock(&defer_thread_mutex); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | void rcu_defer_unregister_thread(void) |
| 428 | { |
| 429 | int is_empty; |
| 430 | |
| 431 | mutex_lock_defer(&defer_thread_mutex); |
| 432 | mutex_lock_defer(&rcu_defer_mutex); |
| 433 | cds_list_del(&defer_queue.list); |
| 434 | _rcu_defer_barrier_thread(); |
| 435 | free(defer_queue.q); |
| 436 | defer_queue.q = NULL; |
| 437 | is_empty = cds_list_empty(®istry_defer); |
| 438 | mutex_unlock(&rcu_defer_mutex); |
| 439 | |
| 440 | if (is_empty) |
| 441 | stop_defer_thread(); |
| 442 | mutex_unlock(&defer_thread_mutex); |
| 443 | } |
| 444 | |
| 445 | void rcu_defer_exit(void) |
| 446 | { |
| 447 | assert(cds_list_empty(®istry_defer)); |
| 448 | } |
| 449 | |
| 450 | #endif /* _URCU_DEFER_IMPL_H */ |