Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/common/hathreads.h |
| 3 | * definitions, macros and inline functions about threads. |
| 4 | * |
| 5 | * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _COMMON_HATHREADS_H |
| 23 | #define _COMMON_HATHREADS_H |
| 24 | |
| 25 | #include <common/config.h> |
Willy Tarreau | 90fa97b | 2018-11-25 19:46:08 +0100 | [diff] [blame] | 26 | #include <common/initcall.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 28 | /* Note about all_threads_mask : |
| 29 | * - with threads support disabled, this symbol is defined as zero (0UL). |
| 30 | * - with threads enabled, this variable is never zero, it contains the mask |
| 31 | * of enabled threads. Thus if only one thread is enabled, it equals 1. |
| 32 | */ |
| 33 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 34 | #ifndef USE_THREAD |
| 35 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 36 | #define MAX_THREADS 1 |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 37 | #define MAX_THREADS_MASK 1 |
| 38 | |
| 39 | /* Only way found to replace variables with constants that are optimized away |
| 40 | * at build time. |
| 41 | */ |
| 42 | enum { all_threads_mask = 1UL }; |
| 43 | enum { tid_bit = 1UL }; |
| 44 | enum { tid = 0 }; |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 45 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 46 | #define __decl_hathreads(decl) |
Willy Tarreau | 90fa97b | 2018-11-25 19:46:08 +0100 | [diff] [blame] | 47 | #define __decl_spinlock(lock) |
| 48 | #define __decl_aligned_spinlock(lock) |
| 49 | #define __decl_rwlock(lock) |
| 50 | #define __decl_aligned_rwlock(lock) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 51 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 52 | #define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);}) |
| 53 | #define HA_ATOMIC_ADD(val, i) ({*(val) += (i);}) |
| 54 | #define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);}) |
Willy Tarreau | 9378df8 | 2018-09-05 16:11:03 +0200 | [diff] [blame] | 55 | #define HA_ATOMIC_XADD(val, i) \ |
| 56 | ({ \ |
| 57 | typeof((val)) __p_xadd = (val); \ |
| 58 | typeof(*(val)) __old_xadd = *__p_xadd; \ |
| 59 | *__p_xadd += i; \ |
| 60 | __old_xadd; \ |
| 61 | }) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 62 | #define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);}) |
| 63 | #define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);}) |
| 64 | #define HA_ATOMIC_XCHG(val, new) \ |
| 65 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 66 | typeof(*(val)) __old_xchg = *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 67 | *(val) = new; \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 68 | __old_xchg; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 69 | }) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 70 | #define HA_ATOMIC_BTS(val, bit) \ |
| 71 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 72 | typeof((val)) __p_bts = (val); \ |
| 73 | typeof(*__p_bts) __b_bts = (1UL << (bit)); \ |
| 74 | typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \ |
| 75 | if (!__t_bts) \ |
| 76 | *__p_bts |= __b_bts; \ |
| 77 | __t_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 78 | }) |
| 79 | #define HA_ATOMIC_BTR(val, bit) \ |
| 80 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 81 | typeof((val)) __p_btr = (val); \ |
| 82 | typeof(*__p_btr) __b_btr = (1UL << (bit)); \ |
| 83 | typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \ |
| 84 | if (__t_btr) \ |
| 85 | *__p_btr &= ~__b_btr; \ |
| 86 | __t_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 87 | }) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 88 | #define HA_ATOMIC_STORE(val, new) ({*(val) = new;}) |
| 89 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 90 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 91 | typeof(*(val)) __new_max = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 92 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 93 | if (*(val) < __new_max) \ |
| 94 | *(val) = __new_max; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 95 | *(val); \ |
| 96 | }) |
| 97 | |
| 98 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 99 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 100 | typeof(*(val)) __new_min = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 101 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 102 | if (*(val) > __new_min) \ |
| 103 | *(val) = __new_min; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 104 | *(val); \ |
| 105 | }) |
| 106 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 107 | #define HA_BARRIER() do { } while (0) |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 108 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 109 | #define HA_SPIN_INIT(l) do { /* do nothing */ } while(0) |
| 110 | #define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0) |
| 111 | #define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0) |
| 112 | #define HA_SPIN_TRYLOCK(lbl, l) ({ 0; }) |
| 113 | #define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 114 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 115 | #define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0) |
| 116 | #define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0) |
| 117 | #define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 118 | #define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; }) |
| 119 | #define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 120 | #define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 121 | #define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; }) |
| 122 | #define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 123 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 124 | #define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset) |
| 125 | |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 126 | static inline void ha_set_tid(unsigned int tid) |
| 127 | { |
| 128 | } |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 129 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 130 | static inline void __ha_barrier_load(void) |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | static inline void __ha_barrier_store(void) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | static inline void __ha_barrier_full(void) |
| 139 | { |
| 140 | } |
| 141 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 142 | static inline void thread_harmless_now() |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | static inline void thread_harmless_end() |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | static inline void thread_isolate() |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | static inline void thread_release() |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | static inline unsigned long thread_isolated() |
| 159 | { |
| 160 | return 1; |
| 161 | } |
| 162 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 163 | #else /* USE_THREAD */ |
| 164 | |
| 165 | #include <stdio.h> |
| 166 | #include <stdlib.h> |
| 167 | #include <string.h> |
| 168 | #include <pthread.h> |
| 169 | #include <import/plock.h> |
| 170 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 171 | #define MAX_THREADS LONGBITS |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 172 | #define MAX_THREADS_MASK ((unsigned long)-1) |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 173 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 174 | #define __decl_hathreads(decl) decl |
| 175 | |
Willy Tarreau | 90fa97b | 2018-11-25 19:46:08 +0100 | [diff] [blame] | 176 | /* declare a self-initializing spinlock */ |
| 177 | #define __decl_spinlock(lock) \ |
| 178 | HA_SPINLOCK_T (lock); \ |
| 179 | INITCALL1(STG_LOCK, ha_spin_init, &(lock)) |
| 180 | |
| 181 | /* declare a self-initializing spinlock, aligned on a cache line */ |
| 182 | #define __decl_aligned_spinlock(lock) \ |
| 183 | HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \ |
| 184 | INITCALL1(STG_LOCK, ha_spin_init, &(lock)) |
| 185 | |
| 186 | /* declare a self-initializing rwlock */ |
| 187 | #define __decl_rwlock(lock) \ |
| 188 | HA_RWLOCK_T (lock); \ |
| 189 | INITCALL1(STG_LOCK, ha_rwlock_init, &(lock)) |
| 190 | |
| 191 | /* declare a self-initializing rwlock, aligned on a cache line */ |
| 192 | #define __decl_aligned_rwlock(lock) \ |
| 193 | HA_RWLOCK_T (lock) __attribute__((aligned(64))); \ |
| 194 | INITCALL1(STG_LOCK, ha_rwlock_init, &(lock)) |
| 195 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 196 | /* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to |
| 197 | * have a header file regrouping all functions dealing with threads. */ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 198 | |
David Carlier | ec5e845 | 2018-01-11 14:20:43 +0000 | [diff] [blame] | 199 | #if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 200 | /* gcc < 4.7 */ |
| 201 | |
| 202 | #define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i) |
| 203 | #define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i) |
Willy Tarreau | 9378df8 | 2018-09-05 16:11:03 +0200 | [diff] [blame] | 204 | #define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 205 | #define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags) |
| 206 | #define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags) |
| 207 | |
| 208 | /* the CAS is a bit complicated. The older API doesn't support returning the |
| 209 | * value and the swap's result at the same time. So here we take what looks |
| 210 | * like the safest route, consisting in using the boolean version guaranteeing |
| 211 | * that the operation was performed or not, and we snoop a previous value. If |
| 212 | * the compare succeeds, we return. If it fails, we return the previous value, |
| 213 | * but only if it differs from the expected one. If it's the same it's a race |
| 214 | * thus we try again to avoid confusing a possibly sensitive caller. |
| 215 | */ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 216 | #define HA_ATOMIC_CAS(val, old, new) \ |
| 217 | ({ \ |
| 218 | typeof((val)) __val_cas = (val); \ |
| 219 | typeof((old)) __oldp_cas = (old); \ |
| 220 | typeof(*(old)) __oldv_cas; \ |
| 221 | typeof((new)) __new_cas = (new); \ |
| 222 | int __ret_cas; \ |
| 223 | do { \ |
| 224 | __oldv_cas = *__val_cas; \ |
| 225 | __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \ |
| 226 | } while (!__ret_cas && *__oldp_cas == __oldv_cas); \ |
| 227 | if (!__ret_cas) \ |
| 228 | *__oldp_cas = __oldv_cas; \ |
| 229 | __ret_cas; \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 230 | }) |
| 231 | |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 232 | #define HA_ATOMIC_XCHG(val, new) \ |
| 233 | ({ \ |
| 234 | typeof((val)) __val_xchg = (val); \ |
| 235 | typeof(*(val)) __old_xchg; \ |
| 236 | typeof((new)) __new_xchg = (new); \ |
| 237 | do { __old_xchg = *__val_xchg; \ |
| 238 | } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \ |
| 239 | __old_xchg; \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 240 | }) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 241 | |
| 242 | #define HA_ATOMIC_BTS(val, bit) \ |
| 243 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 244 | typeof(*(val)) __b_bts = (1UL << (bit)); \ |
| 245 | __sync_fetch_and_or((val), __b_bts) & __b_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 246 | }) |
| 247 | |
| 248 | #define HA_ATOMIC_BTR(val, bit) \ |
| 249 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 250 | typeof(*(val)) __b_btr = (1UL << (bit)); \ |
| 251 | __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 252 | }) |
| 253 | |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 254 | #define HA_ATOMIC_STORE(val, new) \ |
| 255 | ({ \ |
| 256 | typeof((val)) __val_store = (val); \ |
| 257 | typeof(*(val)) __old_store; \ |
| 258 | typeof((new)) __new_store = (new); \ |
| 259 | do { __old_store = *__val_store; \ |
| 260 | } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 261 | }) |
| 262 | #else |
| 263 | /* gcc >= 4.7 */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 264 | #define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0) |
| 265 | #define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0) |
Willy Tarreau | 9378df8 | 2018-09-05 16:11:03 +0200 | [diff] [blame] | 266 | #define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, 0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 267 | #define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0) |
| 268 | #define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0) |
| 269 | #define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 270 | #define HA_ATOMIC_BTS(val, bit) \ |
| 271 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 272 | typeof(*(val)) __b_bts = (1UL << (bit)); \ |
| 273 | __sync_fetch_and_or((val), __b_bts) & __b_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 274 | }) |
| 275 | |
| 276 | #define HA_ATOMIC_BTR(val, bit) \ |
| 277 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 278 | typeof(*(val)) __b_btr = (1UL << (bit)); \ |
| 279 | __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 280 | }) |
| 281 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 282 | #define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0) |
| 283 | #define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 284 | #endif |
| 285 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 286 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 287 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 288 | typeof(*(val)) __old_max = *(val); \ |
| 289 | typeof(*(val)) __new_max = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 290 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 291 | while (__old_max < __new_max && \ |
| 292 | !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \ |
| 293 | *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 294 | }) |
| 295 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 296 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 297 | typeof(*(val)) __old_min = *(val); \ |
| 298 | typeof(*(val)) __new_min = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 299 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 300 | while (__old_min > __new_min && \ |
| 301 | !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \ |
| 302 | *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 303 | }) |
| 304 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 305 | #define HA_BARRIER() pl_barrier() |
| 306 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 307 | void thread_harmless_till_end(); |
| 308 | void thread_isolate(); |
| 309 | void thread_release(); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 310 | |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 311 | extern THREAD_LOCAL unsigned int tid; /* The thread id */ |
| 312 | extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */ |
Christopher Faulet | ddb6c16 | 2018-07-20 09:31:53 +0200 | [diff] [blame] | 313 | extern volatile unsigned long all_threads_mask; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 314 | extern volatile unsigned long threads_want_rdv_mask; |
| 315 | extern volatile unsigned long threads_harmless_mask; |
| 316 | |
| 317 | /* explanation for threads_want_rdv_mask and threads_harmless_mask : |
| 318 | * - threads_want_rdv_mask is a bit field indicating all threads that have |
| 319 | * requested a rendez-vous of other threads using thread_isolate(). |
| 320 | * - threads_harmless_mask is a bit field indicating all threads that are |
| 321 | * currently harmless in that they promise not to access a shared resource. |
| 322 | * |
| 323 | * For a given thread, its bits in want_rdv and harmless can be translated like |
| 324 | * this : |
| 325 | * |
| 326 | * ----------+----------+---------------------------------------------------- |
| 327 | * want_rdv | harmless | description |
| 328 | * ----------+----------+---------------------------------------------------- |
| 329 | * 0 | 0 | thread not interested in RDV, possibly harmful |
| 330 | * 0 | 1 | thread not interested in RDV but harmless |
| 331 | * 1 | 1 | thread interested in RDV and waiting for its turn |
| 332 | * 1 | 0 | thread currently working isolated from others |
| 333 | * ----------+----------+---------------------------------------------------- |
| 334 | */ |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 335 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 336 | #define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset) |
| 337 | |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 338 | /* sets the thread ID and the TID bit for the current thread */ |
| 339 | static inline void ha_set_tid(unsigned int data) |
| 340 | { |
| 341 | tid = data; |
| 342 | tid_bit = (1UL << tid); |
| 343 | } |
| 344 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 345 | /* Marks the thread as harmless. Note: this must be true, i.e. the thread must |
| 346 | * not be touching any unprotected shared resource during this period. Usually |
| 347 | * this is called before poll(), but it may also be placed around very slow |
| 348 | * calls (eg: some crypto operations). Needs to be terminated using |
| 349 | * thread_harmless_end(). |
| 350 | */ |
| 351 | static inline void thread_harmless_now() |
| 352 | { |
| 353 | HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 354 | } |
| 355 | |
| 356 | /* Ends the harmless period started by thread_harmless_now(). Usually this is |
| 357 | * placed after the poll() call. If it is discovered that a job was running and |
| 358 | * is relying on the thread still being harmless, the thread waits for the |
| 359 | * other one to finish. |
| 360 | */ |
| 361 | static inline void thread_harmless_end() |
| 362 | { |
| 363 | while (1) { |
| 364 | HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit); |
| 365 | if (likely((threads_want_rdv_mask & all_threads_mask) == 0)) |
| 366 | break; |
| 367 | thread_harmless_till_end(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* an isolated thread has harmless cleared and want_rdv set */ |
| 372 | static inline unsigned long thread_isolated() |
| 373 | { |
| 374 | return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit; |
| 375 | } |
| 376 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 377 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 378 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 379 | |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 380 | /* WARNING!!! if you update this enum, please also keep lock_label() up to date below */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 381 | enum lock_label { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 382 | FD_LOCK, |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 383 | TASK_RQ_LOCK, |
| 384 | TASK_WQ_LOCK, |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 385 | POOL_LOCK, |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 386 | LISTENER_LOCK, |
| 387 | LISTENER_QUEUE_LOCK, |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 388 | PROXY_LOCK, |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 389 | SERVER_LOCK, |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 390 | LBPRM_LOCK, |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 391 | SIGNALS_LOCK, |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 392 | STK_TABLE_LOCK, |
| 393 | STK_SESS_LOCK, |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 394 | APPLETS_LOCK, |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 395 | PEER_LOCK, |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 396 | BUF_WQ_LOCK, |
Emeric Brun | 6b35e9b | 2017-06-30 16:23:45 +0200 | [diff] [blame] | 397 | STRMS_LOCK, |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 398 | SSL_LOCK, |
| 399 | SSL_GEN_CERTS_LOCK, |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 400 | PATREF_LOCK, |
| 401 | PATEXP_LOCK, |
| 402 | PATLRU_LOCK, |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 403 | VARS_LOCK, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 404 | COMP_POOL_LOCK, |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 405 | LUA_LOCK, |
Thierry FOURNIER | 738a6d7 | 2017-07-17 00:14:07 +0200 | [diff] [blame] | 406 | NOTIF_LOCK, |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 407 | SPOE_APPLET_LOCK, |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 408 | DNS_LOCK, |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 409 | PID_LIST_LOCK, |
Christopher Faulet | c2a89a6 | 2017-10-23 15:54:24 +0200 | [diff] [blame] | 410 | EMAIL_ALERTS_LOCK, |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 411 | PIPES_LOCK, |
Willy Tarreau | 1605c7a | 2018-01-23 19:01:49 +0100 | [diff] [blame] | 412 | START_LOCK, |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 413 | TLSKEYS_REF_LOCK, |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 414 | AUTH_LOCK, |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 415 | LOCK_LABELS |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 416 | }; |
| 417 | struct lock_stat { |
| 418 | uint64_t nsec_wait_for_write; |
| 419 | uint64_t nsec_wait_for_read; |
| 420 | uint64_t num_write_locked; |
| 421 | uint64_t num_write_unlocked; |
| 422 | uint64_t num_read_locked; |
| 423 | uint64_t num_read_unlocked; |
| 424 | }; |
| 425 | |
| 426 | extern struct lock_stat lock_stats[LOCK_LABELS]; |
| 427 | |
| 428 | #define __HA_SPINLOCK_T unsigned long |
| 429 | |
| 430 | #define __SPIN_INIT(l) ({ (*l) = 0; }) |
| 431 | #define __SPIN_DESTROY(l) ({ (*l) = 0; }) |
Willy Tarreau | 88ac59b | 2017-11-06 01:03:26 +0100 | [diff] [blame] | 432 | #define __SPIN_LOCK(l) pl_take_s(l) |
| 433 | #define __SPIN_TRYLOCK(l) !pl_try_s(l) |
| 434 | #define __SPIN_UNLOCK(l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 435 | |
| 436 | #define __HA_RWLOCK_T unsigned long |
| 437 | |
| 438 | #define __RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 439 | #define __RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 440 | #define __RWLOCK_WRLOCK(l) pl_take_w(l) |
| 441 | #define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l) |
| 442 | #define __RWLOCK_WRUNLOCK(l) pl_drop_w(l) |
| 443 | #define __RWLOCK_RDLOCK(l) pl_take_r(l) |
| 444 | #define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l) |
| 445 | #define __RWLOCK_RDUNLOCK(l) pl_drop_r(l) |
| 446 | |
| 447 | #define HA_SPINLOCK_T struct ha_spinlock |
| 448 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 449 | #define HA_SPIN_INIT(l) __spin_init(l) |
| 450 | #define HA_SPIN_DESTROY(l) __spin_destroy(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 451 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 452 | #define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__) |
| 453 | #define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__) |
| 454 | #define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 455 | |
| 456 | #define HA_RWLOCK_T struct ha_rwlock |
| 457 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 458 | #define HA_RWLOCK_INIT(l) __ha_rwlock_init((l)) |
| 459 | #define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l)) |
| 460 | #define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 461 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 462 | #define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__) |
| 463 | #define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l) |
| 464 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l) |
| 465 | #define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 466 | |
| 467 | struct ha_spinlock { |
| 468 | __HA_SPINLOCK_T lock; |
| 469 | struct { |
| 470 | unsigned long owner; /* a bit is set to 1 << tid for the lock owner */ |
| 471 | unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */ |
| 472 | struct { |
| 473 | const char *function; |
| 474 | const char *file; |
| 475 | int line; |
| 476 | } last_location; /* location of the last owner */ |
| 477 | } info; |
| 478 | }; |
| 479 | |
| 480 | struct ha_rwlock { |
| 481 | __HA_RWLOCK_T lock; |
| 482 | struct { |
| 483 | unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */ |
| 484 | unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */ |
| 485 | unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */ |
| 486 | unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */ |
| 487 | struct { |
| 488 | const char *function; |
| 489 | const char *file; |
| 490 | int line; |
| 491 | } last_location; /* location of the last write owner */ |
| 492 | } info; |
| 493 | }; |
| 494 | |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 495 | static inline const char *lock_label(enum lock_label label) |
| 496 | { |
| 497 | switch (label) { |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 498 | case FD_LOCK: return "FD"; |
| 499 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 500 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 501 | case POOL_LOCK: return "POOL"; |
| 502 | case LISTENER_LOCK: return "LISTENER"; |
| 503 | case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE"; |
| 504 | case PROXY_LOCK: return "PROXY"; |
| 505 | case SERVER_LOCK: return "SERVER"; |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 506 | case LBPRM_LOCK: return "LBPRM"; |
| 507 | case SIGNALS_LOCK: return "SIGNALS"; |
| 508 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 509 | case STK_SESS_LOCK: return "STK_SESS"; |
| 510 | case APPLETS_LOCK: return "APPLETS"; |
| 511 | case PEER_LOCK: return "PEER"; |
| 512 | case BUF_WQ_LOCK: return "BUF_WQ"; |
| 513 | case STRMS_LOCK: return "STRMS"; |
| 514 | case SSL_LOCK: return "SSL"; |
| 515 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 516 | case PATREF_LOCK: return "PATREF"; |
| 517 | case PATEXP_LOCK: return "PATEXP"; |
| 518 | case PATLRU_LOCK: return "PATLRU"; |
| 519 | case VARS_LOCK: return "VARS"; |
| 520 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 521 | case LUA_LOCK: return "LUA"; |
| 522 | case NOTIF_LOCK: return "NOTIF"; |
| 523 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 524 | case DNS_LOCK: return "DNS"; |
| 525 | case PID_LIST_LOCK: return "PID_LIST"; |
| 526 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 527 | case PIPES_LOCK: return "PIPES"; |
| 528 | case START_LOCK: return "START"; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 529 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
Willy Tarreau | 34d4b52 | 2018-10-29 18:02:54 +0100 | [diff] [blame] | 530 | case AUTH_LOCK: return "AUTH"; |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 531 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 532 | }; |
| 533 | /* only way to come here is consecutive to an internal bug */ |
| 534 | abort(); |
| 535 | } |
| 536 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 537 | static inline void show_lock_stats() |
| 538 | { |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 539 | int lbl; |
| 540 | |
| 541 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 542 | fprintf(stderr, |
| 543 | "Stats about Lock %s: \n" |
| 544 | "\t # write lock : %lu\n" |
| 545 | "\t # write unlock: %lu (%ld)\n" |
| 546 | "\t # wait time for write : %.3f msec\n" |
| 547 | "\t # wait time for write/lock: %.3f nsec\n" |
| 548 | "\t # read lock : %lu\n" |
| 549 | "\t # read unlock : %lu (%ld)\n" |
| 550 | "\t # wait time for read : %.3f msec\n" |
| 551 | "\t # wait time for read/lock : %.3f nsec\n", |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 552 | lock_label(lbl), |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 553 | lock_stats[lbl].num_write_locked, |
| 554 | lock_stats[lbl].num_write_unlocked, |
| 555 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 556 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 557 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0, |
| 558 | lock_stats[lbl].num_read_locked, |
| 559 | lock_stats[lbl].num_read_unlocked, |
| 560 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 561 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 562 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /* Following functions are used to collect some stats about locks. We wrap |
| 567 | * pthread functions to known how much time we wait in a lock. */ |
| 568 | |
| 569 | static uint64_t nsec_now(void) { |
| 570 | struct timespec ts; |
| 571 | |
| 572 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 573 | return ((uint64_t) ts.tv_sec * 1000000000ULL + |
| 574 | (uint64_t) ts.tv_nsec); |
| 575 | } |
| 576 | |
| 577 | static inline void __ha_rwlock_init(struct ha_rwlock *l) |
| 578 | { |
| 579 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 580 | __RWLOCK_INIT(&l->lock); |
| 581 | } |
| 582 | |
| 583 | static inline void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 584 | { |
| 585 | __RWLOCK_DESTROY(&l->lock); |
| 586 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 587 | } |
| 588 | |
| 589 | |
| 590 | static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 591 | const char *func, const char *file, int line) |
| 592 | { |
| 593 | uint64_t start_time; |
| 594 | |
| 595 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 596 | /* the thread is already owning the lock for write */ |
| 597 | abort(); |
| 598 | } |
| 599 | |
| 600 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 601 | /* the thread is already owning the lock for read */ |
| 602 | abort(); |
| 603 | } |
| 604 | |
| 605 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 606 | |
| 607 | start_time = nsec_now(); |
| 608 | __RWLOCK_WRLOCK(&l->lock); |
| 609 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 610 | |
| 611 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 612 | |
| 613 | l->info.cur_writer = tid_bit; |
| 614 | l->info.last_location.function = func; |
| 615 | l->info.last_location.file = file; |
| 616 | l->info.last_location.line = line; |
| 617 | |
| 618 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 619 | } |
| 620 | |
| 621 | static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 622 | const char *func, const char *file, int line) |
| 623 | { |
| 624 | uint64_t start_time; |
| 625 | int r; |
| 626 | |
| 627 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 628 | /* the thread is already owning the lock for write */ |
| 629 | abort(); |
| 630 | } |
| 631 | |
| 632 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 633 | /* the thread is already owning the lock for read */ |
| 634 | abort(); |
| 635 | } |
| 636 | |
| 637 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 638 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 639 | |
| 640 | start_time = nsec_now(); |
| 641 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
| 642 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 643 | if (unlikely(r)) { |
| 644 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 645 | return r; |
| 646 | } |
| 647 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 648 | |
| 649 | l->info.cur_writer = tid_bit; |
| 650 | l->info.last_location.function = func; |
| 651 | l->info.last_location.file = file; |
| 652 | l->info.last_location.line = line; |
| 653 | |
| 654 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 660 | const char *func, const char *file, int line) |
| 661 | { |
| 662 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 663 | /* the thread is not owning the lock for write */ |
| 664 | abort(); |
| 665 | } |
| 666 | |
| 667 | l->info.cur_writer = 0; |
| 668 | l->info.last_location.function = func; |
| 669 | l->info.last_location.file = file; |
| 670 | l->info.last_location.line = line; |
| 671 | |
| 672 | __RWLOCK_WRUNLOCK(&l->lock); |
| 673 | |
| 674 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 675 | } |
| 676 | |
| 677 | static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 678 | { |
| 679 | uint64_t start_time; |
| 680 | |
| 681 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 682 | /* the thread is already owning the lock for write */ |
| 683 | abort(); |
| 684 | } |
| 685 | |
| 686 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 687 | /* the thread is already owning the lock for read */ |
| 688 | abort(); |
| 689 | } |
| 690 | |
| 691 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 692 | |
| 693 | start_time = nsec_now(); |
| 694 | __RWLOCK_RDLOCK(&l->lock); |
| 695 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time)); |
| 696 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 697 | |
| 698 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 699 | |
| 700 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 701 | } |
| 702 | |
| 703 | static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 704 | { |
| 705 | int r; |
| 706 | |
| 707 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 708 | /* the thread is already owning the lock for write */ |
| 709 | abort(); |
| 710 | } |
| 711 | |
| 712 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 713 | /* the thread is already owning the lock for read */ |
| 714 | abort(); |
| 715 | } |
| 716 | |
| 717 | /* try read should never wait */ |
| 718 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 719 | if (unlikely(r)) |
| 720 | return r; |
| 721 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 722 | |
| 723 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 729 | { |
| 730 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 731 | /* the thread is not owning the lock for read */ |
| 732 | abort(); |
| 733 | } |
| 734 | |
| 735 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 736 | |
| 737 | __RWLOCK_RDUNLOCK(&l->lock); |
| 738 | |
| 739 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1); |
| 740 | } |
| 741 | |
| 742 | static inline void __spin_init(struct ha_spinlock *l) |
| 743 | { |
| 744 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 745 | __SPIN_INIT(&l->lock); |
| 746 | } |
| 747 | |
| 748 | static inline void __spin_destroy(struct ha_spinlock *l) |
| 749 | { |
| 750 | __SPIN_DESTROY(&l->lock); |
| 751 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 752 | } |
| 753 | |
| 754 | static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 755 | const char *func, const char *file, int line) |
| 756 | { |
| 757 | uint64_t start_time; |
| 758 | |
| 759 | if (unlikely(l->info.owner & tid_bit)) { |
| 760 | /* the thread is already owning the lock */ |
| 761 | abort(); |
| 762 | } |
| 763 | |
| 764 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 765 | |
| 766 | start_time = nsec_now(); |
| 767 | __SPIN_LOCK(&l->lock); |
| 768 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 769 | |
| 770 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 771 | |
| 772 | |
| 773 | l->info.owner = tid_bit; |
| 774 | l->info.last_location.function = func; |
| 775 | l->info.last_location.file = file; |
| 776 | l->info.last_location.line = line; |
| 777 | |
| 778 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 779 | } |
| 780 | |
| 781 | static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 782 | const char *func, const char *file, int line) |
| 783 | { |
| 784 | int r; |
| 785 | |
| 786 | if (unlikely(l->info.owner & tid_bit)) { |
| 787 | /* the thread is already owning the lock */ |
| 788 | abort(); |
| 789 | } |
| 790 | |
| 791 | /* try read should never wait */ |
| 792 | r = __SPIN_TRYLOCK(&l->lock); |
| 793 | if (unlikely(r)) |
| 794 | return r; |
| 795 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 796 | |
| 797 | l->info.owner = tid_bit; |
| 798 | l->info.last_location.function = func; |
| 799 | l->info.last_location.file = file; |
| 800 | l->info.last_location.line = line; |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 806 | const char *func, const char *file, int line) |
| 807 | { |
| 808 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 809 | /* the thread is not owning the lock */ |
| 810 | abort(); |
| 811 | } |
| 812 | |
| 813 | l->info.owner = 0; |
| 814 | l->info.last_location.function = func; |
| 815 | l->info.last_location.file = file; |
| 816 | l->info.last_location.line = line; |
| 817 | |
Willy Tarreau | 7c2a2ad | 2017-11-02 16:26:02 +0100 | [diff] [blame] | 818 | __SPIN_UNLOCK(&l->lock); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 819 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 820 | } |
| 821 | |
| 822 | #else /* DEBUG_THREAD */ |
| 823 | |
| 824 | #define HA_SPINLOCK_T unsigned long |
| 825 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 826 | #define HA_SPIN_INIT(l) ({ (*l) = 0; }) |
| 827 | #define HA_SPIN_DESTROY(l) ({ (*l) = 0; }) |
| 828 | #define HA_SPIN_LOCK(lbl, l) pl_take_s(l) |
| 829 | #define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l) |
| 830 | #define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 831 | |
| 832 | #define HA_RWLOCK_T unsigned long |
| 833 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 834 | #define HA_RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 835 | #define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 836 | #define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l) |
| 837 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l) |
| 838 | #define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l) |
| 839 | #define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l) |
| 840 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l) |
| 841 | #define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 842 | |
| 843 | #endif /* DEBUG_THREAD */ |
| 844 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 845 | #ifdef __x86_64__ |
Willy Tarreau | 2325d8a | 2018-10-10 18:29:23 +0200 | [diff] [blame] | 846 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 847 | static __inline int |
| 848 | __ha_cas_dw(void *target, void *compare, const void *set) |
| 849 | { |
| 850 | char ret; |
| 851 | |
| 852 | __asm __volatile("lock cmpxchg16b %0; setz %3" |
| 853 | : "+m" (*(void **)target), |
| 854 | "=a" (((void **)compare)[0]), |
| 855 | "=d" (((void **)compare)[1]), |
| 856 | "=q" (ret) |
| 857 | : "a" (((void **)compare)[0]), |
| 858 | "d" (((void **)compare)[1]), |
| 859 | "b" (((const void **)set)[0]), |
| 860 | "c" (((const void **)set)[1]) |
| 861 | : "memory", "cc"); |
| 862 | return (ret); |
| 863 | } |
| 864 | |
| 865 | static __inline void |
| 866 | __ha_barrier_load(void) |
| 867 | { |
| 868 | __asm __volatile("lfence" ::: "memory"); |
| 869 | } |
| 870 | |
| 871 | static __inline void |
| 872 | __ha_barrier_store(void) |
| 873 | { |
| 874 | __asm __volatile("sfence" ::: "memory"); |
| 875 | } |
| 876 | |
| 877 | static __inline void |
| 878 | __ha_barrier_full(void) |
| 879 | { |
| 880 | __asm __volatile("mfence" ::: "memory"); |
| 881 | } |
| 882 | |
| 883 | #elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__)) |
Willy Tarreau | 2325d8a | 2018-10-10 18:29:23 +0200 | [diff] [blame] | 884 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 885 | static __inline void |
| 886 | __ha_barrier_load(void) |
| 887 | { |
| 888 | __asm __volatile("dmb" ::: "memory"); |
| 889 | } |
| 890 | |
| 891 | static __inline void |
| 892 | __ha_barrier_store(void) |
| 893 | { |
| 894 | __asm __volatile("dsb" ::: "memory"); |
| 895 | } |
| 896 | |
| 897 | static __inline void |
| 898 | __ha_barrier_full(void) |
| 899 | { |
| 900 | __asm __volatile("dmb" ::: "memory"); |
| 901 | } |
| 902 | |
Willy Tarreau | 41ccb19 | 2018-02-14 14:16:28 +0100 | [diff] [blame] | 903 | static __inline int __ha_cas_dw(void *target, void *compare, const void *set) |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 904 | { |
| 905 | uint64_t previous; |
| 906 | int tmp; |
| 907 | |
| 908 | __asm __volatile("1:" |
| 909 | "ldrexd %0, [%4];" |
| 910 | "cmp %Q0, %Q2;" |
| 911 | "ittt eq;" |
| 912 | "cmpeq %R0, %R2;" |
| 913 | "strexdeq %1, %3, [%4];" |
| 914 | "cmpeq %1, #1;" |
| 915 | "beq 1b;" |
| 916 | : "=&r" (previous), "=&r" (tmp) |
Willy Tarreau | 41ccb19 | 2018-02-14 14:16:28 +0100 | [diff] [blame] | 917 | : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target) |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 918 | : "memory", "cc"); |
| 919 | tmp = (previous == *(uint64_t *)compare); |
| 920 | *(uint64_t *)compare = previous; |
| 921 | return (tmp); |
| 922 | } |
| 923 | |
| 924 | #elif defined (__aarch64__) |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 925 | |
| 926 | static __inline void |
| 927 | __ha_barrier_load(void) |
| 928 | { |
| 929 | __asm __volatile("dmb ishld" ::: "memory"); |
| 930 | } |
| 931 | |
| 932 | static __inline void |
| 933 | __ha_barrier_store(void) |
| 934 | { |
| 935 | __asm __volatile("dmb ishst" ::: "memory"); |
| 936 | } |
| 937 | |
| 938 | static __inline void |
| 939 | __ha_barrier_full(void) |
| 940 | { |
| 941 | __asm __volatile("dmb ish" ::: "memory"); |
| 942 | } |
| 943 | |
| 944 | static __inline int __ha_cas_dw(void *target, void *compare, void *set) |
| 945 | { |
| 946 | void *value[2]; |
| 947 | uint64_t tmp1, tmp2; |
| 948 | |
| 949 | __asm__ __volatile__("1:" |
| 950 | "ldxp %0, %1, [%4];" |
| 951 | "mov %2, %0;" |
| 952 | "mov %3, %1;" |
| 953 | "eor %0, %0, %5;" |
| 954 | "eor %1, %1, %6;" |
| 955 | "orr %1, %0, %1;" |
| 956 | "mov %w0, #0;" |
| 957 | "cbnz %1, 2f;" |
| 958 | "stxp %w0, %7, %8, [%4];" |
| 959 | "cbnz %w0, 1b;" |
| 960 | "mov %w0, #1;" |
| 961 | "2:" |
| 962 | : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1]) |
| 963 | : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1]) |
| 964 | : "cc", "memory"); |
| 965 | |
| 966 | memcpy(compare, &value, sizeof(value)); |
| 967 | return (tmp1); |
| 968 | } |
| 969 | |
| 970 | #else |
| 971 | #define __ha_barrier_load __sync_synchronize |
| 972 | #define __ha_barrier_store __sync_synchronize |
| 973 | #define __ha_barrier_full __sync_synchronize |
| 974 | #endif |
| 975 | |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 976 | void ha_spin_init(HA_SPINLOCK_T *l); |
| 977 | void ha_rwlock_init(HA_RWLOCK_T *l); |
| 978 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 979 | #endif /* USE_THREAD */ |
| 980 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 981 | static inline void __ha_compiler_barrier(void) |
| 982 | { |
| 983 | __asm __volatile("" ::: "memory"); |
| 984 | } |
| 985 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 986 | int parse_nbthread(const char *arg, char **err); |
Willy Tarreau | 4037a3f | 2018-03-28 18:06:47 +0200 | [diff] [blame] | 987 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 988 | #endif /* _COMMON_HATHREADS_H */ |