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