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