| 1 | #ifndef _URCU_MEMB_STATIC_H |
| 2 | #define _URCU_MEMB_STATIC_H |
| 3 | |
| 4 | /* |
| 5 | * urcu-memb-static.h |
| 6 | * |
| 7 | * Userspace RCU header. |
| 8 | * |
| 9 | * TO BE INCLUDED ONLY IN CODE THAT IS TO BE RECOMPILED ON EACH LIBURCU |
| 10 | * RELEASE. See urcu.h for linking dynamically with the userspace rcu 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 <unistd.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | #include <urcu/debug.h> |
| 38 | #include <urcu/config.h> |
| 39 | #include <urcu/compiler.h> |
| 40 | #include <urcu/arch.h> |
| 41 | #include <urcu/system.h> |
| 42 | #include <urcu/uatomic.h> |
| 43 | #include <urcu/list.h> |
| 44 | #include <urcu/futex.h> |
| 45 | #include <urcu/tls-compat.h> |
| 46 | #include <urcu/static/urcu-common.h> |
| 47 | |
| 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | /* |
| 53 | * This code section can only be included in LGPL 2.1 compatible source code. |
| 54 | * See below for the function call wrappers which can be used in code meant to |
| 55 | * be only linked with the Userspace RCU library. This comes with a small |
| 56 | * performance degradation on the read-side due to the added function calls. |
| 57 | * This is required to permit relinking with newer versions of the library. |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * Slave barriers are only guaranteed to be ordered wrt master barriers. |
| 62 | * |
| 63 | * The pair ordering is detailed as (O: ordered, X: not ordered) : |
| 64 | * slave master |
| 65 | * slave X O |
| 66 | * master O O |
| 67 | */ |
| 68 | |
| 69 | #ifdef CONFIG_RCU_FORCE_SYS_MEMBARRIER |
| 70 | #define urcu_memb_has_sys_membarrier 1 |
| 71 | #else |
| 72 | extern int urcu_memb_has_sys_membarrier; |
| 73 | #endif |
| 74 | |
| 75 | static inline void urcu_memb_smp_mb_slave(void) |
| 76 | { |
| 77 | if (caa_likely(urcu_memb_has_sys_membarrier)) |
| 78 | cmm_barrier(); |
| 79 | else |
| 80 | cmm_smp_mb(); |
| 81 | } |
| 82 | |
| 83 | extern struct urcu_gp urcu_memb_gp; |
| 84 | |
| 85 | extern DECLARE_URCU_TLS(struct urcu_reader, urcu_memb_reader); |
| 86 | |
| 87 | /* |
| 88 | * Helper for _rcu_read_lock(). The format of urcu_memb_gp.ctr (as well as |
| 89 | * the per-thread rcu_reader.ctr) has the lower-order bits containing a count of |
| 90 | * _rcu_read_lock() nesting, and a single high-order URCU_BP_GP_CTR_PHASE bit |
| 91 | * that contains either zero or one. The smp_mb_slave() ensures that the accesses in |
| 92 | * _rcu_read_lock() happen before the subsequent read-side critical section. |
| 93 | */ |
| 94 | static inline void _urcu_memb_read_lock_update(unsigned long tmp) |
| 95 | { |
| 96 | if (caa_likely(!(tmp & URCU_GP_CTR_NEST_MASK))) { |
| 97 | _CMM_STORE_SHARED(URCU_TLS(urcu_memb_reader).ctr, _CMM_LOAD_SHARED(urcu_memb_gp.ctr)); |
| 98 | urcu_memb_smp_mb_slave(); |
| 99 | } else |
| 100 | _CMM_STORE_SHARED(URCU_TLS(urcu_memb_reader).ctr, tmp + URCU_GP_COUNT); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Enter an RCU read-side critical section. |
| 105 | * |
| 106 | * The first cmm_barrier() call ensures that the compiler does not reorder |
| 107 | * the body of _rcu_read_lock() with a mutex. |
| 108 | * |
| 109 | * This function and its helper are both less than 10 lines long. The |
| 110 | * intent is that this function meets the 10-line criterion in LGPL, |
| 111 | * allowing this function to be invoked directly from non-LGPL code. |
| 112 | */ |
| 113 | static inline void _urcu_memb_read_lock(void) |
| 114 | { |
| 115 | unsigned long tmp; |
| 116 | |
| 117 | urcu_assert_debug(URCU_TLS(urcu_memb_reader).registered); |
| 118 | cmm_barrier(); |
| 119 | tmp = URCU_TLS(urcu_memb_reader).ctr; |
| 120 | urcu_assert_debug((tmp & URCU_GP_CTR_NEST_MASK) != URCU_GP_CTR_NEST_MASK); |
| 121 | _urcu_memb_read_lock_update(tmp); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * This is a helper function for _rcu_read_unlock(). |
| 126 | * |
| 127 | * The first smp_mb_slave() call ensures that the critical section is |
| 128 | * seen to precede the store to rcu_reader.ctr. |
| 129 | * The second smp_mb_slave() call ensures that we write to rcu_reader.ctr |
| 130 | * before reading the update-side futex. |
| 131 | */ |
| 132 | static inline void _urcu_memb_read_unlock_update_and_wakeup(unsigned long tmp) |
| 133 | { |
| 134 | if (caa_likely((tmp & URCU_GP_CTR_NEST_MASK) == URCU_GP_COUNT)) { |
| 135 | urcu_memb_smp_mb_slave(); |
| 136 | _CMM_STORE_SHARED(URCU_TLS(urcu_memb_reader).ctr, tmp - URCU_GP_COUNT); |
| 137 | urcu_memb_smp_mb_slave(); |
| 138 | urcu_common_wake_up_gp(&urcu_memb_gp); |
| 139 | } else |
| 140 | _CMM_STORE_SHARED(URCU_TLS(urcu_memb_reader).ctr, tmp - URCU_GP_COUNT); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Exit an RCU read-side critical section. Both this function and its |
| 145 | * helper are smaller than 10 lines of code, and are intended to be |
| 146 | * usable by non-LGPL code, as called out in LGPL. |
| 147 | */ |
| 148 | static inline void _urcu_memb_read_unlock(void) |
| 149 | { |
| 150 | unsigned long tmp; |
| 151 | |
| 152 | urcu_assert_debug(URCU_TLS(urcu_memb_reader).registered); |
| 153 | tmp = URCU_TLS(urcu_memb_reader).ctr; |
| 154 | urcu_assert_debug(tmp & URCU_GP_CTR_NEST_MASK); |
| 155 | _urcu_memb_read_unlock_update_and_wakeup(tmp); |
| 156 | cmm_barrier(); /* Ensure the compiler does not reorder us with mutex */ |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Returns whether within a RCU read-side critical section. |
| 161 | * |
| 162 | * This function is less than 10 lines long. The intent is that this |
| 163 | * function meets the 10-line criterion for LGPL, allowing this function |
| 164 | * to be invoked directly from non-LGPL code. |
| 165 | */ |
| 166 | static inline int _urcu_memb_read_ongoing(void) |
| 167 | { |
| 168 | return URCU_TLS(urcu_memb_reader).ctr & URCU_GP_CTR_NEST_MASK; |
| 169 | } |
| 170 | |
| 171 | #ifdef __cplusplus |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | #endif /* _URCU_MEMB_STATIC_H */ |