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