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 | |
| 25 | #include <common/config.h> |
| 26 | |
| 27 | #define MAX_THREADS_MASK ((unsigned long)-1) |
| 28 | extern THREAD_LOCAL unsigned int tid; /* The thread id */ |
Christopher Faulet | e9a896e | 2017-11-14 10:16:04 +0100 | [diff] [blame] | 29 | extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 30 | |
| 31 | #ifndef USE_THREAD |
| 32 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 33 | #define MAX_THREADS 1 |
Willy Tarreau | 0cd82e8 | 2018-05-23 19:54:43 +0200 | [diff] [blame] | 34 | #define all_threads_mask 0UL |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 36 | #define __decl_hathreads(decl) |
| 37 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 38 | #define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);}) |
| 39 | #define HA_ATOMIC_ADD(val, i) ({*(val) += (i);}) |
| 40 | #define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);}) |
| 41 | #define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);}) |
| 42 | #define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);}) |
| 43 | #define HA_ATOMIC_XCHG(val, new) \ |
| 44 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 45 | typeof(*(val)) __old_xchg = *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 46 | *(val) = new; \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 47 | __old_xchg; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 48 | }) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 49 | #define HA_ATOMIC_BTS(val, bit) \ |
| 50 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 51 | typeof((val)) __p_bts = (val); \ |
| 52 | typeof(*__p_bts) __b_bts = (1UL << (bit)); \ |
| 53 | typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \ |
| 54 | if (!__t_bts) \ |
| 55 | *__p_bts |= __b_bts; \ |
| 56 | __t_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 57 | }) |
| 58 | #define HA_ATOMIC_BTR(val, bit) \ |
| 59 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 60 | typeof((val)) __p_btr = (val); \ |
| 61 | typeof(*__p_btr) __b_btr = (1UL << (bit)); \ |
| 62 | typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \ |
| 63 | if (__t_btr) \ |
| 64 | *__p_btr &= ~__b_btr; \ |
| 65 | __t_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 66 | }) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 67 | #define HA_ATOMIC_STORE(val, new) ({*(val) = new;}) |
| 68 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 69 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 70 | typeof(*(val)) __new_max = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 71 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 72 | if (*(val) < __new_max) \ |
| 73 | *(val) = __new_max; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 74 | *(val); \ |
| 75 | }) |
| 76 | |
| 77 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 78 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 79 | typeof(*(val)) __new_min = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 80 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 81 | if (*(val) > __new_min) \ |
| 82 | *(val) = __new_min; \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 83 | *(val); \ |
| 84 | }) |
| 85 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 86 | #define HA_BARRIER() do { } while (0) |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 87 | |
| 88 | #define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0) |
| 89 | #define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0) |
| 90 | #define THREAD_WANT_SYNC() do { /* do nothing */ } while(0) |
| 91 | #define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0) |
| 92 | #define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0) |
| 93 | #define THREAD_NO_SYNC() ({ 0; }) |
| 94 | #define THREAD_NEED_SYNC() ({ 1; }) |
| 95 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 96 | #define HA_SPIN_INIT(l) do { /* do nothing */ } while(0) |
| 97 | #define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0) |
| 98 | #define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0) |
| 99 | #define HA_SPIN_TRYLOCK(lbl, l) ({ 0; }) |
| 100 | #define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 101 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 102 | #define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0) |
| 103 | #define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0) |
| 104 | #define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 105 | #define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; }) |
| 106 | #define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 107 | #define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0) |
| 108 | #define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; }) |
| 109 | #define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 110 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 111 | #define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset) |
| 112 | |
| 113 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 114 | static inline void __ha_barrier_load(void) |
| 115 | { |
| 116 | } |
| 117 | |
| 118 | static inline void __ha_barrier_store(void) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | static inline void __ha_barrier_full(void) |
| 123 | { |
| 124 | } |
| 125 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 126 | #else /* USE_THREAD */ |
| 127 | |
| 128 | #include <stdio.h> |
| 129 | #include <stdlib.h> |
| 130 | #include <string.h> |
| 131 | #include <pthread.h> |
| 132 | #include <import/plock.h> |
| 133 | |
Willy Tarreau | 421f02e | 2018-01-20 18:19:22 +0100 | [diff] [blame] | 134 | #define MAX_THREADS LONGBITS |
| 135 | |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 136 | #define __decl_hathreads(decl) decl |
| 137 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 138 | /* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to |
| 139 | * have a header file regrouping all functions dealing with threads. */ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 140 | |
David Carlier | ec5e845 | 2018-01-11 14:20:43 +0000 | [diff] [blame] | 141 | #if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 142 | /* gcc < 4.7 */ |
| 143 | |
| 144 | #define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i) |
| 145 | #define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i) |
| 146 | #define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags) |
| 147 | #define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags) |
| 148 | |
| 149 | /* the CAS is a bit complicated. The older API doesn't support returning the |
| 150 | * value and the swap's result at the same time. So here we take what looks |
| 151 | * like the safest route, consisting in using the boolean version guaranteeing |
| 152 | * that the operation was performed or not, and we snoop a previous value. If |
| 153 | * the compare succeeds, we return. If it fails, we return the previous value, |
| 154 | * but only if it differs from the expected one. If it's the same it's a race |
| 155 | * thus we try again to avoid confusing a possibly sensitive caller. |
| 156 | */ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 157 | #define HA_ATOMIC_CAS(val, old, new) \ |
| 158 | ({ \ |
| 159 | typeof((val)) __val_cas = (val); \ |
| 160 | typeof((old)) __oldp_cas = (old); \ |
| 161 | typeof(*(old)) __oldv_cas; \ |
| 162 | typeof((new)) __new_cas = (new); \ |
| 163 | int __ret_cas; \ |
| 164 | do { \ |
| 165 | __oldv_cas = *__val_cas; \ |
| 166 | __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \ |
| 167 | } while (!__ret_cas && *__oldp_cas == __oldv_cas); \ |
| 168 | if (!__ret_cas) \ |
| 169 | *__oldp_cas = __oldv_cas; \ |
| 170 | __ret_cas; \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 171 | }) |
| 172 | |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 173 | #define HA_ATOMIC_XCHG(val, new) \ |
| 174 | ({ \ |
| 175 | typeof((val)) __val_xchg = (val); \ |
| 176 | typeof(*(val)) __old_xchg; \ |
| 177 | typeof((new)) __new_xchg = (new); \ |
| 178 | do { __old_xchg = *__val_xchg; \ |
| 179 | } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \ |
| 180 | __old_xchg; \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 181 | }) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 182 | |
| 183 | #define HA_ATOMIC_BTS(val, bit) \ |
| 184 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 185 | typeof(*(val)) __b_bts = (1UL << (bit)); \ |
| 186 | __sync_fetch_and_or((val), __b_bts) & __b_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 187 | }) |
| 188 | |
| 189 | #define HA_ATOMIC_BTR(val, bit) \ |
| 190 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 191 | typeof(*(val)) __b_btr = (1UL << (bit)); \ |
| 192 | __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 193 | }) |
| 194 | |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 195 | #define HA_ATOMIC_STORE(val, new) \ |
| 196 | ({ \ |
| 197 | typeof((val)) __val_store = (val); \ |
| 198 | typeof(*(val)) __old_store; \ |
| 199 | typeof((new)) __new_store = (new); \ |
| 200 | do { __old_store = *__val_store; \ |
| 201 | } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \ |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 202 | }) |
| 203 | #else |
| 204 | /* gcc >= 4.7 */ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 205 | #define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0) |
| 206 | #define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0) |
| 207 | #define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0) |
| 208 | #define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0) |
| 209 | #define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0) |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 210 | #define HA_ATOMIC_BTS(val, bit) \ |
| 211 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 212 | typeof(*(val)) __b_bts = (1UL << (bit)); \ |
| 213 | __sync_fetch_and_or((val), __b_bts) & __b_bts; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 214 | }) |
| 215 | |
| 216 | #define HA_ATOMIC_BTR(val, bit) \ |
| 217 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 218 | typeof(*(val)) __b_btr = (1UL << (bit)); \ |
| 219 | __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \ |
Willy Tarreau | 5266b3e | 2018-01-25 17:43:58 +0100 | [diff] [blame] | 220 | }) |
| 221 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 222 | #define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0) |
| 223 | #define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0) |
Willy Tarreau | 1a69af6 | 2018-01-04 18:49:31 +0100 | [diff] [blame] | 224 | #endif |
| 225 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 226 | #define HA_ATOMIC_UPDATE_MAX(val, new) \ |
| 227 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 228 | typeof(*(val)) __old_max = *(val); \ |
| 229 | typeof(*(val)) __new_max = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 230 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 231 | while (__old_max < __new_max && \ |
| 232 | !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \ |
| 233 | *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 234 | }) |
| 235 | #define HA_ATOMIC_UPDATE_MIN(val, new) \ |
| 236 | ({ \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 237 | typeof(*(val)) __old_min = *(val); \ |
| 238 | typeof(*(val)) __new_min = (new); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 239 | \ |
Christopher Faulet | 48aa13f | 2018-04-09 08:45:43 +0200 | [diff] [blame] | 240 | while (__old_min > __new_min && \ |
| 241 | !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \ |
| 242 | *(val); \ |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 243 | }) |
| 244 | |
Willy Tarreau | b29dc95 | 2017-10-31 18:00:20 +0100 | [diff] [blame] | 245 | #define HA_BARRIER() pl_barrier() |
| 246 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 247 | #define THREAD_SYNC_INIT(m) thread_sync_init(m) |
| 248 | #define THREAD_SYNC_ENABLE() thread_sync_enable() |
| 249 | #define THREAD_WANT_SYNC() thread_want_sync() |
| 250 | #define THREAD_ENTER_SYNC() thread_enter_sync() |
| 251 | #define THREAD_EXIT_SYNC() thread_exit_sync() |
| 252 | #define THREAD_NO_SYNC() thread_no_sync() |
| 253 | #define THREAD_NEED_SYNC() thread_need_sync() |
| 254 | |
| 255 | int thread_sync_init(unsigned long mask); |
| 256 | void thread_sync_enable(void); |
| 257 | void thread_want_sync(void); |
| 258 | void thread_enter_sync(void); |
| 259 | void thread_exit_sync(void); |
| 260 | int thread_no_sync(void); |
| 261 | int thread_need_sync(void); |
| 262 | |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 263 | extern unsigned long all_threads_mask; |
| 264 | |
William Lallemand | 6e1796e | 2018-06-07 11:23:40 +0200 | [diff] [blame] | 265 | #define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset) |
| 266 | |
| 267 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 268 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 269 | |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 270 | /* 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] | 271 | enum lock_label { |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 272 | THREAD_SYNC_LOCK = 0, |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 273 | FD_LOCK, |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 274 | TASK_RQ_LOCK, |
| 275 | TASK_WQ_LOCK, |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 276 | POOL_LOCK, |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 277 | LISTENER_LOCK, |
| 278 | LISTENER_QUEUE_LOCK, |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 279 | PROXY_LOCK, |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 280 | SERVER_LOCK, |
Christopher Faulet | 5d42e09 | 2017-10-16 12:00:40 +0200 | [diff] [blame] | 281 | UPDATED_SERVERS_LOCK, |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 282 | LBPRM_LOCK, |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 283 | SIGNALS_LOCK, |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 284 | STK_TABLE_LOCK, |
| 285 | STK_SESS_LOCK, |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 286 | APPLETS_LOCK, |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 287 | PEER_LOCK, |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 288 | BUF_WQ_LOCK, |
Emeric Brun | 6b35e9b | 2017-06-30 16:23:45 +0200 | [diff] [blame] | 289 | STRMS_LOCK, |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 290 | SSL_LOCK, |
| 291 | SSL_GEN_CERTS_LOCK, |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 292 | PATREF_LOCK, |
| 293 | PATEXP_LOCK, |
| 294 | PATLRU_LOCK, |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 295 | VARS_LOCK, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 296 | COMP_POOL_LOCK, |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 297 | LUA_LOCK, |
Thierry FOURNIER | 738a6d7 | 2017-07-17 00:14:07 +0200 | [diff] [blame] | 298 | NOTIF_LOCK, |
Christopher Faulet | 24289f2 | 2017-09-25 14:48:02 +0200 | [diff] [blame] | 299 | SPOE_APPLET_LOCK, |
Christopher Faulet | b2812a6 | 2017-10-04 16:17:58 +0200 | [diff] [blame] | 300 | DNS_LOCK, |
Christopher Faulet | cfda847 | 2017-10-20 15:40:23 +0200 | [diff] [blame] | 301 | PID_LIST_LOCK, |
Christopher Faulet | c2a89a6 | 2017-10-23 15:54:24 +0200 | [diff] [blame] | 302 | EMAIL_ALERTS_LOCK, |
Emeric Brun | d8b3b65 | 2017-11-07 11:19:48 +0100 | [diff] [blame] | 303 | PIPES_LOCK, |
Willy Tarreau | 1605c7a | 2018-01-23 19:01:49 +0100 | [diff] [blame] | 304 | START_LOCK, |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 305 | TLSKEYS_REF_LOCK, |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 306 | PENDCONN_LOCK, |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 307 | LOCK_LABELS |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 308 | }; |
| 309 | struct lock_stat { |
| 310 | uint64_t nsec_wait_for_write; |
| 311 | uint64_t nsec_wait_for_read; |
| 312 | uint64_t num_write_locked; |
| 313 | uint64_t num_write_unlocked; |
| 314 | uint64_t num_read_locked; |
| 315 | uint64_t num_read_unlocked; |
| 316 | }; |
| 317 | |
| 318 | extern struct lock_stat lock_stats[LOCK_LABELS]; |
| 319 | |
| 320 | #define __HA_SPINLOCK_T unsigned long |
| 321 | |
| 322 | #define __SPIN_INIT(l) ({ (*l) = 0; }) |
| 323 | #define __SPIN_DESTROY(l) ({ (*l) = 0; }) |
Willy Tarreau | 88ac59b | 2017-11-06 01:03:26 +0100 | [diff] [blame] | 324 | #define __SPIN_LOCK(l) pl_take_s(l) |
| 325 | #define __SPIN_TRYLOCK(l) !pl_try_s(l) |
| 326 | #define __SPIN_UNLOCK(l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 327 | |
| 328 | #define __HA_RWLOCK_T unsigned long |
| 329 | |
| 330 | #define __RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 331 | #define __RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 332 | #define __RWLOCK_WRLOCK(l) pl_take_w(l) |
| 333 | #define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l) |
| 334 | #define __RWLOCK_WRUNLOCK(l) pl_drop_w(l) |
| 335 | #define __RWLOCK_RDLOCK(l) pl_take_r(l) |
| 336 | #define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l) |
| 337 | #define __RWLOCK_RDUNLOCK(l) pl_drop_r(l) |
| 338 | |
| 339 | #define HA_SPINLOCK_T struct ha_spinlock |
| 340 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 341 | #define HA_SPIN_INIT(l) __spin_init(l) |
| 342 | #define HA_SPIN_DESTROY(l) __spin_destroy(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 343 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 344 | #define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__) |
| 345 | #define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__) |
| 346 | #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] | 347 | |
| 348 | #define HA_RWLOCK_T struct ha_rwlock |
| 349 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 350 | #define HA_RWLOCK_INIT(l) __ha_rwlock_init((l)) |
| 351 | #define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l)) |
| 352 | #define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 353 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 354 | #define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__) |
| 355 | #define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l) |
| 356 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l) |
| 357 | #define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 358 | |
| 359 | struct ha_spinlock { |
| 360 | __HA_SPINLOCK_T lock; |
| 361 | struct { |
| 362 | unsigned long owner; /* a bit is set to 1 << tid for the lock owner */ |
| 363 | unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */ |
| 364 | struct { |
| 365 | const char *function; |
| 366 | const char *file; |
| 367 | int line; |
| 368 | } last_location; /* location of the last owner */ |
| 369 | } info; |
| 370 | }; |
| 371 | |
| 372 | struct ha_rwlock { |
| 373 | __HA_RWLOCK_T lock; |
| 374 | struct { |
| 375 | unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */ |
| 376 | unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */ |
| 377 | unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */ |
| 378 | unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */ |
| 379 | struct { |
| 380 | const char *function; |
| 381 | const char *file; |
| 382 | int line; |
| 383 | } last_location; /* location of the last write owner */ |
| 384 | } info; |
| 385 | }; |
| 386 | |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 387 | static inline const char *lock_label(enum lock_label label) |
| 388 | { |
| 389 | switch (label) { |
| 390 | case THREAD_SYNC_LOCK: return "THREAD_SYNC"; |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 391 | case FD_LOCK: return "FD"; |
| 392 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 393 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 394 | case POOL_LOCK: return "POOL"; |
| 395 | case LISTENER_LOCK: return "LISTENER"; |
| 396 | case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE"; |
| 397 | case PROXY_LOCK: return "PROXY"; |
| 398 | case SERVER_LOCK: return "SERVER"; |
| 399 | case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS"; |
| 400 | case LBPRM_LOCK: return "LBPRM"; |
| 401 | case SIGNALS_LOCK: return "SIGNALS"; |
| 402 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 403 | case STK_SESS_LOCK: return "STK_SESS"; |
| 404 | case APPLETS_LOCK: return "APPLETS"; |
| 405 | case PEER_LOCK: return "PEER"; |
| 406 | case BUF_WQ_LOCK: return "BUF_WQ"; |
| 407 | case STRMS_LOCK: return "STRMS"; |
| 408 | case SSL_LOCK: return "SSL"; |
| 409 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 410 | case PATREF_LOCK: return "PATREF"; |
| 411 | case PATEXP_LOCK: return "PATEXP"; |
| 412 | case PATLRU_LOCK: return "PATLRU"; |
| 413 | case VARS_LOCK: return "VARS"; |
| 414 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 415 | case LUA_LOCK: return "LUA"; |
| 416 | case NOTIF_LOCK: return "NOTIF"; |
| 417 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 418 | case DNS_LOCK: return "DNS"; |
| 419 | case PID_LIST_LOCK: return "PID_LIST"; |
| 420 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 421 | case PIPES_LOCK: return "PIPES"; |
| 422 | case START_LOCK: return "START"; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 423 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
Christopher Faulet | 5cd4bbd | 2018-03-14 16:18:06 +0100 | [diff] [blame] | 424 | case PENDCONN_LOCK: return "PENDCONN"; |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 425 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 426 | }; |
| 427 | /* only way to come here is consecutive to an internal bug */ |
| 428 | abort(); |
| 429 | } |
| 430 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 431 | static inline void show_lock_stats() |
| 432 | { |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 433 | int lbl; |
| 434 | |
| 435 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 436 | fprintf(stderr, |
| 437 | "Stats about Lock %s: \n" |
| 438 | "\t # write lock : %lu\n" |
| 439 | "\t # write unlock: %lu (%ld)\n" |
| 440 | "\t # wait time for write : %.3f msec\n" |
| 441 | "\t # wait time for write/lock: %.3f nsec\n" |
| 442 | "\t # read lock : %lu\n" |
| 443 | "\t # read unlock : %lu (%ld)\n" |
| 444 | "\t # wait time for read : %.3f msec\n" |
| 445 | "\t # wait time for read/lock : %.3f nsec\n", |
Christopher Faulet | f51bac2 | 2018-01-30 11:04:29 +0100 | [diff] [blame] | 446 | lock_label(lbl), |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 447 | lock_stats[lbl].num_write_locked, |
| 448 | lock_stats[lbl].num_write_unlocked, |
| 449 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 450 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 451 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0, |
| 452 | lock_stats[lbl].num_read_locked, |
| 453 | lock_stats[lbl].num_read_unlocked, |
| 454 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 455 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 456 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* Following functions are used to collect some stats about locks. We wrap |
| 461 | * pthread functions to known how much time we wait in a lock. */ |
| 462 | |
| 463 | static uint64_t nsec_now(void) { |
| 464 | struct timespec ts; |
| 465 | |
| 466 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 467 | return ((uint64_t) ts.tv_sec * 1000000000ULL + |
| 468 | (uint64_t) ts.tv_nsec); |
| 469 | } |
| 470 | |
| 471 | static inline void __ha_rwlock_init(struct ha_rwlock *l) |
| 472 | { |
| 473 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 474 | __RWLOCK_INIT(&l->lock); |
| 475 | } |
| 476 | |
| 477 | static inline void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 478 | { |
| 479 | __RWLOCK_DESTROY(&l->lock); |
| 480 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 481 | } |
| 482 | |
| 483 | |
| 484 | static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 485 | const char *func, const char *file, int line) |
| 486 | { |
| 487 | uint64_t start_time; |
| 488 | |
| 489 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 490 | /* the thread is already owning the lock for write */ |
| 491 | abort(); |
| 492 | } |
| 493 | |
| 494 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 495 | /* the thread is already owning the lock for read */ |
| 496 | abort(); |
| 497 | } |
| 498 | |
| 499 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 500 | |
| 501 | start_time = nsec_now(); |
| 502 | __RWLOCK_WRLOCK(&l->lock); |
| 503 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 504 | |
| 505 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 506 | |
| 507 | l->info.cur_writer = tid_bit; |
| 508 | l->info.last_location.function = func; |
| 509 | l->info.last_location.file = file; |
| 510 | l->info.last_location.line = line; |
| 511 | |
| 512 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 513 | } |
| 514 | |
| 515 | static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 516 | const char *func, const char *file, int line) |
| 517 | { |
| 518 | uint64_t start_time; |
| 519 | int r; |
| 520 | |
| 521 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 522 | /* the thread is already owning the lock for write */ |
| 523 | abort(); |
| 524 | } |
| 525 | |
| 526 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 527 | /* the thread is already owning the lock for read */ |
| 528 | abort(); |
| 529 | } |
| 530 | |
| 531 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 532 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 533 | |
| 534 | start_time = nsec_now(); |
| 535 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
| 536 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 537 | if (unlikely(r)) { |
| 538 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 539 | return r; |
| 540 | } |
| 541 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 542 | |
| 543 | l->info.cur_writer = tid_bit; |
| 544 | l->info.last_location.function = func; |
| 545 | l->info.last_location.file = file; |
| 546 | l->info.last_location.line = line; |
| 547 | |
| 548 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 554 | const char *func, const char *file, int line) |
| 555 | { |
| 556 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 557 | /* the thread is not owning the lock for write */ |
| 558 | abort(); |
| 559 | } |
| 560 | |
| 561 | l->info.cur_writer = 0; |
| 562 | l->info.last_location.function = func; |
| 563 | l->info.last_location.file = file; |
| 564 | l->info.last_location.line = line; |
| 565 | |
| 566 | __RWLOCK_WRUNLOCK(&l->lock); |
| 567 | |
| 568 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 569 | } |
| 570 | |
| 571 | static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 572 | { |
| 573 | uint64_t start_time; |
| 574 | |
| 575 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 576 | /* the thread is already owning the lock for write */ |
| 577 | abort(); |
| 578 | } |
| 579 | |
| 580 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 581 | /* the thread is already owning the lock for read */ |
| 582 | abort(); |
| 583 | } |
| 584 | |
| 585 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 586 | |
| 587 | start_time = nsec_now(); |
| 588 | __RWLOCK_RDLOCK(&l->lock); |
| 589 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time)); |
| 590 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 591 | |
| 592 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 593 | |
| 594 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 595 | } |
| 596 | |
| 597 | static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 598 | { |
| 599 | int r; |
| 600 | |
| 601 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 602 | /* the thread is already owning the lock for write */ |
| 603 | abort(); |
| 604 | } |
| 605 | |
| 606 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 607 | /* the thread is already owning the lock for read */ |
| 608 | abort(); |
| 609 | } |
| 610 | |
| 611 | /* try read should never wait */ |
| 612 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 613 | if (unlikely(r)) |
| 614 | return r; |
| 615 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 616 | |
| 617 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 623 | { |
| 624 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 625 | /* the thread is not owning the lock for read */ |
| 626 | abort(); |
| 627 | } |
| 628 | |
| 629 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 630 | |
| 631 | __RWLOCK_RDUNLOCK(&l->lock); |
| 632 | |
| 633 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1); |
| 634 | } |
| 635 | |
| 636 | static inline void __spin_init(struct ha_spinlock *l) |
| 637 | { |
| 638 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 639 | __SPIN_INIT(&l->lock); |
| 640 | } |
| 641 | |
| 642 | static inline void __spin_destroy(struct ha_spinlock *l) |
| 643 | { |
| 644 | __SPIN_DESTROY(&l->lock); |
| 645 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 646 | } |
| 647 | |
| 648 | static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 649 | const char *func, const char *file, int line) |
| 650 | { |
| 651 | uint64_t start_time; |
| 652 | |
| 653 | if (unlikely(l->info.owner & tid_bit)) { |
| 654 | /* the thread is already owning the lock */ |
| 655 | abort(); |
| 656 | } |
| 657 | |
| 658 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 659 | |
| 660 | start_time = nsec_now(); |
| 661 | __SPIN_LOCK(&l->lock); |
| 662 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 663 | |
| 664 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 665 | |
| 666 | |
| 667 | l->info.owner = tid_bit; |
| 668 | l->info.last_location.function = func; |
| 669 | l->info.last_location.file = file; |
| 670 | l->info.last_location.line = line; |
| 671 | |
| 672 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 673 | } |
| 674 | |
| 675 | static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 676 | const char *func, const char *file, int line) |
| 677 | { |
| 678 | int r; |
| 679 | |
| 680 | if (unlikely(l->info.owner & tid_bit)) { |
| 681 | /* the thread is already owning the lock */ |
| 682 | abort(); |
| 683 | } |
| 684 | |
| 685 | /* try read should never wait */ |
| 686 | r = __SPIN_TRYLOCK(&l->lock); |
| 687 | if (unlikely(r)) |
| 688 | return r; |
| 689 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 690 | |
| 691 | l->info.owner = tid_bit; |
| 692 | l->info.last_location.function = func; |
| 693 | l->info.last_location.file = file; |
| 694 | l->info.last_location.line = line; |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 700 | const char *func, const char *file, int line) |
| 701 | { |
| 702 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 703 | /* the thread is not owning the lock */ |
| 704 | abort(); |
| 705 | } |
| 706 | |
| 707 | l->info.owner = 0; |
| 708 | l->info.last_location.function = func; |
| 709 | l->info.last_location.file = file; |
| 710 | l->info.last_location.line = line; |
| 711 | |
Willy Tarreau | 7c2a2ad | 2017-11-02 16:26:02 +0100 | [diff] [blame] | 712 | __SPIN_UNLOCK(&l->lock); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 713 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 714 | } |
| 715 | |
| 716 | #else /* DEBUG_THREAD */ |
| 717 | |
| 718 | #define HA_SPINLOCK_T unsigned long |
| 719 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 720 | #define HA_SPIN_INIT(l) ({ (*l) = 0; }) |
| 721 | #define HA_SPIN_DESTROY(l) ({ (*l) = 0; }) |
| 722 | #define HA_SPIN_LOCK(lbl, l) pl_take_s(l) |
| 723 | #define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l) |
| 724 | #define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 725 | |
| 726 | #define HA_RWLOCK_T unsigned long |
| 727 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 728 | #define HA_RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 729 | #define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 730 | #define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l) |
| 731 | #define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l) |
| 732 | #define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l) |
| 733 | #define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l) |
| 734 | #define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l) |
| 735 | #define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 736 | |
| 737 | #endif /* DEBUG_THREAD */ |
| 738 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 739 | #ifdef __x86_64__ |
| 740 | #define HA_HAVE_CAS_DW 1 |
| 741 | #define HA_CAS_IS_8B |
| 742 | static __inline int |
| 743 | __ha_cas_dw(void *target, void *compare, const void *set) |
| 744 | { |
| 745 | char ret; |
| 746 | |
| 747 | __asm __volatile("lock cmpxchg16b %0; setz %3" |
| 748 | : "+m" (*(void **)target), |
| 749 | "=a" (((void **)compare)[0]), |
| 750 | "=d" (((void **)compare)[1]), |
| 751 | "=q" (ret) |
| 752 | : "a" (((void **)compare)[0]), |
| 753 | "d" (((void **)compare)[1]), |
| 754 | "b" (((const void **)set)[0]), |
| 755 | "c" (((const void **)set)[1]) |
| 756 | : "memory", "cc"); |
| 757 | return (ret); |
| 758 | } |
| 759 | |
| 760 | static __inline void |
| 761 | __ha_barrier_load(void) |
| 762 | { |
| 763 | __asm __volatile("lfence" ::: "memory"); |
| 764 | } |
| 765 | |
| 766 | static __inline void |
| 767 | __ha_barrier_store(void) |
| 768 | { |
| 769 | __asm __volatile("sfence" ::: "memory"); |
| 770 | } |
| 771 | |
| 772 | static __inline void |
| 773 | __ha_barrier_full(void) |
| 774 | { |
| 775 | __asm __volatile("mfence" ::: "memory"); |
| 776 | } |
| 777 | |
| 778 | #elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__)) |
| 779 | #define HA_HAVE_CAS_DW 1 |
| 780 | static __inline void |
| 781 | __ha_barrier_load(void) |
| 782 | { |
| 783 | __asm __volatile("dmb" ::: "memory"); |
| 784 | } |
| 785 | |
| 786 | static __inline void |
| 787 | __ha_barrier_store(void) |
| 788 | { |
| 789 | __asm __volatile("dsb" ::: "memory"); |
| 790 | } |
| 791 | |
| 792 | static __inline void |
| 793 | __ha_barrier_full(void) |
| 794 | { |
| 795 | __asm __volatile("dmb" ::: "memory"); |
| 796 | } |
| 797 | |
Willy Tarreau | 41ccb19 | 2018-02-14 14:16:28 +0100 | [diff] [blame] | 798 | 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] | 799 | { |
| 800 | uint64_t previous; |
| 801 | int tmp; |
| 802 | |
| 803 | __asm __volatile("1:" |
| 804 | "ldrexd %0, [%4];" |
| 805 | "cmp %Q0, %Q2;" |
| 806 | "ittt eq;" |
| 807 | "cmpeq %R0, %R2;" |
| 808 | "strexdeq %1, %3, [%4];" |
| 809 | "cmpeq %1, #1;" |
| 810 | "beq 1b;" |
| 811 | : "=&r" (previous), "=&r" (tmp) |
Willy Tarreau | 41ccb19 | 2018-02-14 14:16:28 +0100 | [diff] [blame] | 812 | : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target) |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 813 | : "memory", "cc"); |
| 814 | tmp = (previous == *(uint64_t *)compare); |
| 815 | *(uint64_t *)compare = previous; |
| 816 | return (tmp); |
| 817 | } |
| 818 | |
| 819 | #elif defined (__aarch64__) |
| 820 | #define HA_HAVE_CAS_DW 1 |
| 821 | #define HA_CAS_IS_8B |
| 822 | |
| 823 | static __inline void |
| 824 | __ha_barrier_load(void) |
| 825 | { |
| 826 | __asm __volatile("dmb ishld" ::: "memory"); |
| 827 | } |
| 828 | |
| 829 | static __inline void |
| 830 | __ha_barrier_store(void) |
| 831 | { |
| 832 | __asm __volatile("dmb ishst" ::: "memory"); |
| 833 | } |
| 834 | |
| 835 | static __inline void |
| 836 | __ha_barrier_full(void) |
| 837 | { |
| 838 | __asm __volatile("dmb ish" ::: "memory"); |
| 839 | } |
| 840 | |
| 841 | static __inline int __ha_cas_dw(void *target, void *compare, void *set) |
| 842 | { |
| 843 | void *value[2]; |
| 844 | uint64_t tmp1, tmp2; |
| 845 | |
| 846 | __asm__ __volatile__("1:" |
| 847 | "ldxp %0, %1, [%4];" |
| 848 | "mov %2, %0;" |
| 849 | "mov %3, %1;" |
| 850 | "eor %0, %0, %5;" |
| 851 | "eor %1, %1, %6;" |
| 852 | "orr %1, %0, %1;" |
| 853 | "mov %w0, #0;" |
| 854 | "cbnz %1, 2f;" |
| 855 | "stxp %w0, %7, %8, [%4];" |
| 856 | "cbnz %w0, 1b;" |
| 857 | "mov %w0, #1;" |
| 858 | "2:" |
| 859 | : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1]) |
| 860 | : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1]) |
| 861 | : "cc", "memory"); |
| 862 | |
| 863 | memcpy(compare, &value, sizeof(value)); |
| 864 | return (tmp1); |
| 865 | } |
| 866 | |
| 867 | #else |
| 868 | #define __ha_barrier_load __sync_synchronize |
| 869 | #define __ha_barrier_store __sync_synchronize |
| 870 | #define __ha_barrier_full __sync_synchronize |
| 871 | #endif |
| 872 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 873 | #endif /* USE_THREAD */ |
| 874 | |
Olivier Houchard | f61f0cb | 2017-12-21 17:13:05 +0100 | [diff] [blame] | 875 | static inline void __ha_compiler_barrier(void) |
| 876 | { |
| 877 | __asm __volatile("" ::: "memory"); |
| 878 | } |
| 879 | |
Willy Tarreau | 4037a3f | 2018-03-28 18:06:47 +0200 | [diff] [blame] | 880 | /* Dummy I/O handler used by the sync pipe.*/ |
| 881 | void thread_sync_io_handler(int fd); |
| 882 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 883 | #endif /* _COMMON_HATHREADS_H */ |