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