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