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