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