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