| 1 | #ifndef _URCU_RECLAIM_STATIC_H |
| 2 | #define _URCU_RECLAIM_STATIC_H |
| 3 | |
| 4 | /* |
| 5 | * urcu-reclaim-static.h |
| 6 | * |
| 7 | * Userspace RCU header - memory reclamation. |
| 8 | * |
| 9 | * TO BE INCLUDED ONLY IN LGPL-COMPATIBLE CODE. See urcu-reclaim.h for linking |
| 10 | * dynamically with the userspace rcu reclamation library. |
| 11 | * |
| 12 | * Copyright (c) 2009 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 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 | |
| 35 | #include <compiler.h> |
| 36 | #include <arch.h> |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * Number of entries in the per-thread reclaim queue. Must be power of 2. |
| 41 | */ |
| 42 | #define RECLAIM_QUEUE_SIZE (1 << 12) |
| 43 | #define RECLAIM_QUEUE_MASK (RECLAIM_QUEUE_SIZE - 1) |
| 44 | |
| 45 | /* |
| 46 | * Identify a shared load. A smp_rmc() or smp_mc() should come before the load. |
| 47 | */ |
| 48 | #define _LOAD_SHARED(p) ACCESS_ONCE(p) |
| 49 | |
| 50 | /* |
| 51 | * Load a data from shared memory, doing a cache flush if required. |
| 52 | */ |
| 53 | #define LOAD_SHARED(p) \ |
| 54 | ({ \ |
| 55 | smp_rmc(); \ |
| 56 | _LOAD_SHARED(p); \ |
| 57 | }) |
| 58 | |
| 59 | /* |
| 60 | * Identify a shared store. A smp_wmc() or smp_mc() should follow the store. |
| 61 | */ |
| 62 | #define _STORE_SHARED(x, v) ({ ACCESS_ONCE(x) = (v); }) |
| 63 | |
| 64 | /* |
| 65 | * Store v into x, where x is located in shared memory. Performs the required |
| 66 | * cache flush after writing. Returns v. |
| 67 | */ |
| 68 | #define STORE_SHARED(x, v) \ |
| 69 | ({ \ |
| 70 | _STORE_SHARED(x, v); \ |
| 71 | smp_wmc(); \ |
| 72 | (v); \ |
| 73 | }) |
| 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 | struct reclaim_queue { |
| 90 | unsigned long head; /* add element at head */ |
| 91 | unsigned long tail; /* next element to remove at tail */ |
| 92 | void **q; |
| 93 | }; |
| 94 | |
| 95 | extern struct reclaim_queue __thread reclaim_queue; |
| 96 | |
| 97 | extern void rcu_reclaim_barrier_thread(void); |
| 98 | |
| 99 | /* |
| 100 | * not signal-safe. |
| 101 | */ |
| 102 | static inline void _rcu_reclaim_queue(void *p) |
| 103 | { |
| 104 | unsigned long head, tail; |
| 105 | |
| 106 | /* |
| 107 | * Head is only modified by ourself. Tail can be modified by reclamation |
| 108 | * thread. |
| 109 | */ |
| 110 | head = reclaim_queue.head; |
| 111 | tail = LOAD_SHARED(reclaim_queue.tail); |
| 112 | |
| 113 | /* |
| 114 | * If queue is full, empty it ourself. |
| 115 | */ |
| 116 | if (unlikely(head - tail >= RECLAIM_QUEUE_SIZE)) { |
| 117 | assert(head - tail == RECLAIM_QUEUE_SIZE); |
| 118 | rcu_reclaim_barrier_thread(); |
| 119 | assert(head - LOAD_SHARED(reclaim_queue.tail) == 0); |
| 120 | } |
| 121 | |
| 122 | smp_wmb(); /* Publish new pointer before write q[] */ |
| 123 | _STORE_SHARED(reclaim_queue.q[head & RECLAIM_QUEUE_MASK], p); |
| 124 | smp_wmb(); /* Write q[] before head. */ |
| 125 | STORE_SHARED(reclaim_queue.head, head + 1); |
| 126 | } |
| 127 | |
| 128 | #endif /* _URCU_RECLAIM_STATIC_H */ |