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