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