blob: 6c420dd3fa6971a6a2c0cf076afbf9523ed89c1f [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
2 * include/common/hathreads.h
3 * definitions, macros and inline functions about threads.
4 *
5 * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _COMMON_HATHREADS_H
23#define _COMMON_HATHREADS_H
24
Willy Tarreau38171da2019-05-17 16:33:13 +020025#include <unistd.h>
26#ifdef _POSIX_PRIORITY_SCHEDULING
27#include <sched.h>
28#endif
29
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020030#include <common/config.h>
Willy Tarreau90fa97b2018-11-25 19:46:08 +010031#include <common/initcall.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020032
Willy Tarreau0ccd3222018-07-30 10:34:35 +020033/* Note about all_threads_mask :
Willy Tarreauda9e9392019-02-02 17:03:41 +010034 * - this variable is comprised between 1 and LONGBITS.
35 * - with threads support disabled, this symbol is defined as constant 1UL.
36 * - with threads enabled, it contains the mask of enabled threads. Thus if
37 * only one thread is enabled, it equals 1.
Willy Tarreau0ccd3222018-07-30 10:34:35 +020038 */
39
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020040#ifndef USE_THREAD
41
Willy Tarreau421f02e2018-01-20 18:19:22 +010042#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020043#define MAX_THREADS_MASK 1
44
45/* Only way found to replace variables with constants that are optimized away
46 * at build time.
47 */
48enum { all_threads_mask = 1UL };
49enum { tid_bit = 1UL };
50enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010051
Willy Tarreau5a6e2242019-05-20 18:56:48 +020052extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020053 clockid_t clock_id;
Willy Tarreau5a6e2242019-05-20 18:56:48 +020054 /* pad to cache line (64B) */
55 char __pad[0]; /* unused except to check remaining room */
56 char __end[0] __attribute__((aligned(64)));
57} thread_info[MAX_THREADS];
58
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010059#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010060#define __decl_spinlock(lock)
61#define __decl_aligned_spinlock(lock)
62#define __decl_rwlock(lock)
63#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010064
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020065#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020066#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020067#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
68#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020069#define HA_ATOMIC_XADD(val, i) \
70 ({ \
71 typeof((val)) __p_xadd = (val); \
72 typeof(*(val)) __old_xadd = *__p_xadd; \
73 *__p_xadd += i; \
74 __old_xadd; \
75 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020076#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
77#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
78#define HA_ATOMIC_XCHG(val, new) \
79 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020080 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020081 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020082 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020083 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010084#define HA_ATOMIC_BTS(val, bit) \
85 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020086 typeof((val)) __p_bts = (val); \
87 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
88 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
89 if (!__t_bts) \
90 *__p_bts |= __b_bts; \
91 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010092 })
93#define HA_ATOMIC_BTR(val, bit) \
94 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020095 typeof((val)) __p_btr = (val); \
96 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
97 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
98 if (__t_btr) \
99 *__p_btr &= ~__b_btr; \
100 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100101 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200102#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200103#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
104#define HA_ATOMIC_UPDATE_MAX(val, new) \
105 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200106 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200107 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200108 if (*(val) < __new_max) \
109 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110 *(val); \
111 })
112
113#define HA_ATOMIC_UPDATE_MIN(val, new) \
114 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200115 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200117 if (*(val) > __new_min) \
118 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200119 *(val); \
120 })
121
Willy Tarreaub29dc952017-10-31 18:00:20 +0100122#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200123
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100124#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
125#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
126#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
127#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
128#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200129
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100130#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
131#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
132#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
133#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
134#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
135#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
136#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
137#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200138
William Lallemand6e1796e2018-06-07 11:23:40 +0200139#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
140
Willy Tarreau0c026f42018-08-01 19:12:20 +0200141static inline void ha_set_tid(unsigned int tid)
142{
Willy Tarreau38171da2019-05-17 16:33:13 +0200143}
144
145static inline void ha_thread_relax(void)
146{
147#if _POSIX_PRIORITY_SCHEDULING
148 sched_yield();
149#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200150}
William Lallemand6e1796e2018-06-07 11:23:40 +0200151
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100152static inline void __ha_barrier_atomic_load(void)
153{
154}
155
156static inline void __ha_barrier_atomic_store(void)
157{
158}
159
160static inline void __ha_barrier_atomic_full(void)
161{
162}
163
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100164static inline void __ha_barrier_load(void)
165{
166}
167
168static inline void __ha_barrier_store(void)
169{
170}
171
172static inline void __ha_barrier_full(void)
173{
174}
175
Willy Tarreau60b639c2018-08-02 10:16:17 +0200176static inline void thread_harmless_now()
177{
178}
179
180static inline void thread_harmless_end()
181{
182}
183
184static inline void thread_isolate()
185{
186}
187
188static inline void thread_release()
189{
190}
191
192static inline unsigned long thread_isolated()
193{
194 return 1;
195}
196
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200197#else /* USE_THREAD */
198
199#include <stdio.h>
200#include <stdlib.h>
201#include <string.h>
202#include <pthread.h>
203#include <import/plock.h>
204
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100205#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100206#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100207#endif
208
209#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100210
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100211#define __decl_hathreads(decl) decl
212
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100213/* declare a self-initializing spinlock */
214#define __decl_spinlock(lock) \
215 HA_SPINLOCK_T (lock); \
216 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
217
218/* declare a self-initializing spinlock, aligned on a cache line */
219#define __decl_aligned_spinlock(lock) \
220 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
221 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
222
223/* declare a self-initializing rwlock */
224#define __decl_rwlock(lock) \
225 HA_RWLOCK_T (lock); \
226 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
227
228/* declare a self-initializing rwlock, aligned on a cache line */
229#define __decl_aligned_rwlock(lock) \
230 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
231 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
232
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200233/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
234 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100235
David Carlierec5e8452018-01-11 14:20:43 +0000236#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100237/* gcc < 4.7 */
238
239#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
240#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200241#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100242#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
243#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
244
245/* the CAS is a bit complicated. The older API doesn't support returning the
246 * value and the swap's result at the same time. So here we take what looks
247 * like the safest route, consisting in using the boolean version guaranteeing
248 * that the operation was performed or not, and we snoop a previous value. If
249 * the compare succeeds, we return. If it fails, we return the previous value,
250 * but only if it differs from the expected one. If it's the same it's a race
251 * thus we try again to avoid confusing a possibly sensitive caller.
252 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200253#define HA_ATOMIC_CAS(val, old, new) \
254 ({ \
255 typeof((val)) __val_cas = (val); \
256 typeof((old)) __oldp_cas = (old); \
257 typeof(*(old)) __oldv_cas; \
258 typeof((new)) __new_cas = (new); \
259 int __ret_cas; \
260 do { \
261 __oldv_cas = *__val_cas; \
262 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
263 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
264 if (!__ret_cas) \
265 *__oldp_cas = __oldv_cas; \
266 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100267 })
268
Willy Tarreau6a38b322019-05-11 18:04:24 +0200269#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
270
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200271#define HA_ATOMIC_XCHG(val, new) \
272 ({ \
273 typeof((val)) __val_xchg = (val); \
274 typeof(*(val)) __old_xchg; \
275 typeof((new)) __new_xchg = (new); \
276 do { __old_xchg = *__val_xchg; \
277 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
278 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100279 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100280
281#define HA_ATOMIC_BTS(val, bit) \
282 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200283 typeof(*(val)) __b_bts = (1UL << (bit)); \
284 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100285 })
286
287#define HA_ATOMIC_BTR(val, bit) \
288 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200289 typeof(*(val)) __b_btr = (1UL << (bit)); \
290 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100291 })
292
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200293#define HA_ATOMIC_LOAD(val) \
294 ({ \
295 typeof(*(val)) ret; \
296 __sync_synchronize(); \
297 ret = *(volatile typeof(val))val; \
298 __sync_synchronize(); \
299 ret; \
300 })
301
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200302#define HA_ATOMIC_STORE(val, new) \
303 ({ \
304 typeof((val)) __val_store = (val); \
305 typeof(*(val)) __old_store; \
306 typeof((new)) __new_store = (new); \
307 do { __old_store = *__val_store; \
308 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100309 })
310#else
311/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100312#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
Willy Tarreau6a38b322019-05-11 18:04:24 +0200313#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100314#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
315#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
316#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
317#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
318#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100319#define HA_ATOMIC_BTS(val, bit) \
320 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200321 typeof(*(val)) __b_bts = (1UL << (bit)); \
322 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100323 })
324
325#define HA_ATOMIC_BTR(val, bit) \
326 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200327 typeof(*(val)) __b_btr = (1UL << (bit)); \
328 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100329 })
330
Olivier Houchard11353792019-03-07 18:48:22 +0100331#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
332#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200333#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200334
335/* Variants that don't generate any memory barrier.
336 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
337 * that will always generate correct code.
338 * Usually it's fine to use those when updating data that have no dependency,
339 * ie updating a counter. Otherwise a barrier is required.
340 */
341#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
Willy Tarreau6a38b322019-05-11 18:04:24 +0200342#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200343#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
344#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
345#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
346#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
347#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
348#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
349#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200350#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200351
352#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100353
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200354#define HA_ATOMIC_UPDATE_MAX(val, new) \
355 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200356 typeof(*(val)) __old_max = *(val); \
357 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200358 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200359 while (__old_max < __new_max && \
360 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
361 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200362 })
363#define HA_ATOMIC_UPDATE_MIN(val, new) \
364 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200365 typeof(*(val)) __old_min = *(val); \
366 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200367 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200368 while (__old_min > __new_min && \
369 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
370 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200371 })
372
Willy Tarreaub29dc952017-10-31 18:00:20 +0100373#define HA_BARRIER() pl_barrier()
374
Willy Tarreau60b639c2018-08-02 10:16:17 +0200375void thread_harmless_till_end();
376void thread_isolate();
377void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200378
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200379extern struct thread_info {
380 pthread_t pthread;
381 clockid_t clock_id;
382 /* pad to cache line (64B) */
383 char __pad[0]; /* unused except to check remaining room */
384 char __end[0] __attribute__((aligned(64)));
385} thread_info[MAX_THREADS];
386
Willy Tarreau0c026f42018-08-01 19:12:20 +0200387extern THREAD_LOCAL unsigned int tid; /* The thread id */
388extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200389extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200390extern volatile unsigned long threads_want_rdv_mask;
391extern volatile unsigned long threads_harmless_mask;
392
393/* explanation for threads_want_rdv_mask and threads_harmless_mask :
394 * - threads_want_rdv_mask is a bit field indicating all threads that have
395 * requested a rendez-vous of other threads using thread_isolate().
396 * - threads_harmless_mask is a bit field indicating all threads that are
397 * currently harmless in that they promise not to access a shared resource.
398 *
399 * For a given thread, its bits in want_rdv and harmless can be translated like
400 * this :
401 *
402 * ----------+----------+----------------------------------------------------
403 * want_rdv | harmless | description
404 * ----------+----------+----------------------------------------------------
405 * 0 | 0 | thread not interested in RDV, possibly harmful
406 * 0 | 1 | thread not interested in RDV but harmless
407 * 1 | 1 | thread interested in RDV and waiting for its turn
408 * 1 | 0 | thread currently working isolated from others
409 * ----------+----------+----------------------------------------------------
410 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200411
William Lallemand6e1796e2018-06-07 11:23:40 +0200412#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
413
Willy Tarreau0c026f42018-08-01 19:12:20 +0200414/* sets the thread ID and the TID bit for the current thread */
415static inline void ha_set_tid(unsigned int data)
416{
417 tid = data;
418 tid_bit = (1UL << tid);
419}
420
Willy Tarreau38171da2019-05-17 16:33:13 +0200421static inline void ha_thread_relax(void)
422{
423#if _POSIX_PRIORITY_SCHEDULING
424 sched_yield();
425#else
426 pl_cpu_relax();
427#endif
428}
429
Willy Tarreau60b639c2018-08-02 10:16:17 +0200430/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
431 * not be touching any unprotected shared resource during this period. Usually
432 * this is called before poll(), but it may also be placed around very slow
433 * calls (eg: some crypto operations). Needs to be terminated using
434 * thread_harmless_end().
435 */
436static inline void thread_harmless_now()
437{
438 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
439}
440
441/* Ends the harmless period started by thread_harmless_now(). Usually this is
442 * placed after the poll() call. If it is discovered that a job was running and
443 * is relying on the thread still being harmless, the thread waits for the
444 * other one to finish.
445 */
446static inline void thread_harmless_end()
447{
448 while (1) {
449 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
450 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
451 break;
452 thread_harmless_till_end();
453 }
454}
455
456/* an isolated thread has harmless cleared and want_rdv set */
457static inline unsigned long thread_isolated()
458{
459 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
460}
461
William Lallemand6e1796e2018-06-07 11:23:40 +0200462
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200463#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
464
Christopher Fauletf51bac22018-01-30 11:04:29 +0100465/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200466enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200467 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200468 TASK_RQ_LOCK,
469 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200470 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200471 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200472 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200473 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200474 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200475 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200476 STK_TABLE_LOCK,
477 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200478 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200479 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200480 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200481 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200482 SSL_LOCK,
483 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200484 PATREF_LOCK,
485 PATEXP_LOCK,
486 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200487 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200488 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200489 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200490 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200491 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200492 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200493 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200494 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100495 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100496 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100497 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200498 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000499 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200500 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200501};
502struct lock_stat {
503 uint64_t nsec_wait_for_write;
504 uint64_t nsec_wait_for_read;
505 uint64_t num_write_locked;
506 uint64_t num_write_unlocked;
507 uint64_t num_read_locked;
508 uint64_t num_read_unlocked;
509};
510
511extern struct lock_stat lock_stats[LOCK_LABELS];
512
513#define __HA_SPINLOCK_T unsigned long
514
515#define __SPIN_INIT(l) ({ (*l) = 0; })
516#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100517#define __SPIN_LOCK(l) pl_take_s(l)
518#define __SPIN_TRYLOCK(l) !pl_try_s(l)
519#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200520
521#define __HA_RWLOCK_T unsigned long
522
523#define __RWLOCK_INIT(l) ({ (*l) = 0; })
524#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
525#define __RWLOCK_WRLOCK(l) pl_take_w(l)
526#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
527#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
528#define __RWLOCK_RDLOCK(l) pl_take_r(l)
529#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
530#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
531
532#define HA_SPINLOCK_T struct ha_spinlock
533
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100534#define HA_SPIN_INIT(l) __spin_init(l)
535#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200536
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100537#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
538#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
539#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200540
541#define HA_RWLOCK_T struct ha_rwlock
542
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100543#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
544#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
545#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
546#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
547#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
548#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
549#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
550#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200551
552struct ha_spinlock {
553 __HA_SPINLOCK_T lock;
554 struct {
555 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
556 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
557 struct {
558 const char *function;
559 const char *file;
560 int line;
561 } last_location; /* location of the last owner */
562 } info;
563};
564
565struct ha_rwlock {
566 __HA_RWLOCK_T lock;
567 struct {
568 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
569 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
570 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
571 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
572 struct {
573 const char *function;
574 const char *file;
575 int line;
576 } last_location; /* location of the last write owner */
577 } info;
578};
579
Christopher Fauletf51bac22018-01-30 11:04:29 +0100580static inline const char *lock_label(enum lock_label label)
581{
582 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100583 case FD_LOCK: return "FD";
584 case TASK_RQ_LOCK: return "TASK_RQ";
585 case TASK_WQ_LOCK: return "TASK_WQ";
586 case POOL_LOCK: return "POOL";
587 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100588 case PROXY_LOCK: return "PROXY";
589 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100590 case LBPRM_LOCK: return "LBPRM";
591 case SIGNALS_LOCK: return "SIGNALS";
592 case STK_TABLE_LOCK: return "STK_TABLE";
593 case STK_SESS_LOCK: return "STK_SESS";
594 case APPLETS_LOCK: return "APPLETS";
595 case PEER_LOCK: return "PEER";
596 case BUF_WQ_LOCK: return "BUF_WQ";
597 case STRMS_LOCK: return "STRMS";
598 case SSL_LOCK: return "SSL";
599 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
600 case PATREF_LOCK: return "PATREF";
601 case PATEXP_LOCK: return "PATEXP";
602 case PATLRU_LOCK: return "PATLRU";
603 case VARS_LOCK: return "VARS";
604 case COMP_POOL_LOCK: return "COMP_POOL";
605 case LUA_LOCK: return "LUA";
606 case NOTIF_LOCK: return "NOTIF";
607 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
608 case DNS_LOCK: return "DNS";
609 case PID_LIST_LOCK: return "PID_LIST";
610 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
611 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100612 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100613 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200614 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000615 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100616 case LOCK_LABELS: break; /* keep compiler happy */
617 };
618 /* only way to come here is consecutive to an internal bug */
619 abort();
620}
621
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200622static inline void show_lock_stats()
623{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200624 int lbl;
625
626 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
627 fprintf(stderr,
628 "Stats about Lock %s: \n"
629 "\t # write lock : %lu\n"
630 "\t # write unlock: %lu (%ld)\n"
631 "\t # wait time for write : %.3f msec\n"
632 "\t # wait time for write/lock: %.3f nsec\n"
633 "\t # read lock : %lu\n"
634 "\t # read unlock : %lu (%ld)\n"
635 "\t # wait time for read : %.3f msec\n"
636 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100637 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200638 lock_stats[lbl].num_write_locked,
639 lock_stats[lbl].num_write_unlocked,
640 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
641 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
642 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
643 lock_stats[lbl].num_read_locked,
644 lock_stats[lbl].num_read_unlocked,
645 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
646 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
647 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
648 }
649}
650
651/* Following functions are used to collect some stats about locks. We wrap
652 * pthread functions to known how much time we wait in a lock. */
653
654static uint64_t nsec_now(void) {
655 struct timespec ts;
656
657 clock_gettime(CLOCK_MONOTONIC, &ts);
658 return ((uint64_t) ts.tv_sec * 1000000000ULL +
659 (uint64_t) ts.tv_nsec);
660}
661
662static inline void __ha_rwlock_init(struct ha_rwlock *l)
663{
664 memset(l, 0, sizeof(struct ha_rwlock));
665 __RWLOCK_INIT(&l->lock);
666}
667
668static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
669{
670 __RWLOCK_DESTROY(&l->lock);
671 memset(l, 0, sizeof(struct ha_rwlock));
672}
673
674
675static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
676 const char *func, const char *file, int line)
677{
678 uint64_t start_time;
679
680 if (unlikely(l->info.cur_writer & tid_bit)) {
681 /* the thread is already owning the lock for write */
682 abort();
683 }
684
685 if (unlikely(l->info.cur_readers & tid_bit)) {
686 /* the thread is already owning the lock for read */
687 abort();
688 }
689
690 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
691
692 start_time = nsec_now();
693 __RWLOCK_WRLOCK(&l->lock);
694 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
695
696 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
697
698 l->info.cur_writer = tid_bit;
699 l->info.last_location.function = func;
700 l->info.last_location.file = file;
701 l->info.last_location.line = line;
702
703 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
704}
705
706static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
707 const char *func, const char *file, int line)
708{
709 uint64_t start_time;
710 int r;
711
712 if (unlikely(l->info.cur_writer & tid_bit)) {
713 /* the thread is already owning the lock for write */
714 abort();
715 }
716
717 if (unlikely(l->info.cur_readers & tid_bit)) {
718 /* the thread is already owning the lock for read */
719 abort();
720 }
721
722 /* We set waiting writer because trywrlock could wait for readers to quit */
723 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
724
725 start_time = nsec_now();
726 r = __RWLOCK_TRYWRLOCK(&l->lock);
727 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
728 if (unlikely(r)) {
729 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
730 return r;
731 }
732 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
733
734 l->info.cur_writer = tid_bit;
735 l->info.last_location.function = func;
736 l->info.last_location.file = file;
737 l->info.last_location.line = line;
738
739 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
740
741 return 0;
742}
743
744static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
745 const char *func, const char *file, int line)
746{
747 if (unlikely(!(l->info.cur_writer & tid_bit))) {
748 /* the thread is not owning the lock for write */
749 abort();
750 }
751
752 l->info.cur_writer = 0;
753 l->info.last_location.function = func;
754 l->info.last_location.file = file;
755 l->info.last_location.line = line;
756
757 __RWLOCK_WRUNLOCK(&l->lock);
758
759 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
760}
761
762static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
763{
764 uint64_t start_time;
765
766 if (unlikely(l->info.cur_writer & tid_bit)) {
767 /* the thread is already owning the lock for write */
768 abort();
769 }
770
771 if (unlikely(l->info.cur_readers & tid_bit)) {
772 /* the thread is already owning the lock for read */
773 abort();
774 }
775
776 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
777
778 start_time = nsec_now();
779 __RWLOCK_RDLOCK(&l->lock);
780 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
781 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
782
783 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
784
785 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
786}
787
788static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
789{
790 int r;
791
792 if (unlikely(l->info.cur_writer & tid_bit)) {
793 /* the thread is already owning the lock for write */
794 abort();
795 }
796
797 if (unlikely(l->info.cur_readers & tid_bit)) {
798 /* the thread is already owning the lock for read */
799 abort();
800 }
801
802 /* try read should never wait */
803 r = __RWLOCK_TRYRDLOCK(&l->lock);
804 if (unlikely(r))
805 return r;
806 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
807
808 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
809
810 return 0;
811}
812
813static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
814{
815 if (unlikely(!(l->info.cur_readers & tid_bit))) {
816 /* the thread is not owning the lock for read */
817 abort();
818 }
819
820 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
821
822 __RWLOCK_RDUNLOCK(&l->lock);
823
824 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
825}
826
827static inline void __spin_init(struct ha_spinlock *l)
828{
829 memset(l, 0, sizeof(struct ha_spinlock));
830 __SPIN_INIT(&l->lock);
831}
832
833static inline void __spin_destroy(struct ha_spinlock *l)
834{
835 __SPIN_DESTROY(&l->lock);
836 memset(l, 0, sizeof(struct ha_spinlock));
837}
838
839static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
840 const char *func, const char *file, int line)
841{
842 uint64_t start_time;
843
844 if (unlikely(l->info.owner & tid_bit)) {
845 /* the thread is already owning the lock */
846 abort();
847 }
848
849 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
850
851 start_time = nsec_now();
852 __SPIN_LOCK(&l->lock);
853 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
854
855 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
856
857
858 l->info.owner = tid_bit;
859 l->info.last_location.function = func;
860 l->info.last_location.file = file;
861 l->info.last_location.line = line;
862
863 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
864}
865
866static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
867 const char *func, const char *file, int line)
868{
869 int r;
870
871 if (unlikely(l->info.owner & tid_bit)) {
872 /* the thread is already owning the lock */
873 abort();
874 }
875
876 /* try read should never wait */
877 r = __SPIN_TRYLOCK(&l->lock);
878 if (unlikely(r))
879 return r;
880 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
881
882 l->info.owner = tid_bit;
883 l->info.last_location.function = func;
884 l->info.last_location.file = file;
885 l->info.last_location.line = line;
886
887 return 0;
888}
889
890static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
891 const char *func, const char *file, int line)
892{
893 if (unlikely(!(l->info.owner & tid_bit))) {
894 /* the thread is not owning the lock */
895 abort();
896 }
897
898 l->info.owner = 0;
899 l->info.last_location.function = func;
900 l->info.last_location.file = file;
901 l->info.last_location.line = line;
902
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100903 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200904 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
905}
906
907#else /* DEBUG_THREAD */
908
909#define HA_SPINLOCK_T unsigned long
910
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100911#define HA_SPIN_INIT(l) ({ (*l) = 0; })
912#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
913#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
914#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
915#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200916
917#define HA_RWLOCK_T unsigned long
918
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100919#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
920#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
921#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
922#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
923#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
924#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
925#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
926#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200927
928#endif /* DEBUG_THREAD */
929
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100930#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200931
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100932static __inline int
933__ha_cas_dw(void *target, void *compare, const void *set)
934{
935 char ret;
936
937 __asm __volatile("lock cmpxchg16b %0; setz %3"
938 : "+m" (*(void **)target),
939 "=a" (((void **)compare)[0]),
940 "=d" (((void **)compare)[1]),
941 "=q" (ret)
942 : "a" (((void **)compare)[0]),
943 "d" (((void **)compare)[1]),
944 "b" (((const void **)set)[0]),
945 "c" (((const void **)set)[1])
946 : "memory", "cc");
947 return (ret);
948}
949
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100950/* Use __ha_barrier_atomic* when you're trying to protect data that are
951 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
952 */
953static __inline void
954__ha_barrier_atomic_load(void)
955{
956 __asm __volatile("" ::: "memory");
957}
958
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100959static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100960__ha_barrier_atomic_store(void)
961{
962 __asm __volatile("" ::: "memory");
963}
964
965static __inline void
966__ha_barrier_atomic_full(void)
967{
968 __asm __volatile("" ::: "memory");
969}
970
971static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100972__ha_barrier_load(void)
973{
974 __asm __volatile("lfence" ::: "memory");
975}
976
977static __inline void
978__ha_barrier_store(void)
979{
980 __asm __volatile("sfence" ::: "memory");
981}
982
983static __inline void
984__ha_barrier_full(void)
985{
986 __asm __volatile("mfence" ::: "memory");
987}
988
989#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200990
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100991/* Use __ha_barrier_atomic* when you're trying to protect data that are
992 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
993 */
994static __inline void
995__ha_barrier_atomic_load(void)
996{
997 __asm __volatile("dmb" ::: "memory");
998}
999
1000static __inline void
1001__ha_barrier_atomic_store(void)
1002{
1003 __asm __volatile("dsb" ::: "memory");
1004}
1005
1006static __inline void
1007__ha_barrier_atomic_full(void)
1008{
1009 __asm __volatile("dmb" ::: "memory");
1010}
1011
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001012static __inline void
1013__ha_barrier_load(void)
1014{
1015 __asm __volatile("dmb" ::: "memory");
1016}
1017
1018static __inline void
1019__ha_barrier_store(void)
1020{
1021 __asm __volatile("dsb" ::: "memory");
1022}
1023
1024static __inline void
1025__ha_barrier_full(void)
1026{
1027 __asm __volatile("dmb" ::: "memory");
1028}
1029
Willy Tarreau41ccb192018-02-14 14:16:28 +01001030static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001031{
1032 uint64_t previous;
1033 int tmp;
1034
1035 __asm __volatile("1:"
1036 "ldrexd %0, [%4];"
1037 "cmp %Q0, %Q2;"
1038 "ittt eq;"
1039 "cmpeq %R0, %R2;"
1040 "strexdeq %1, %3, [%4];"
1041 "cmpeq %1, #1;"
1042 "beq 1b;"
1043 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001044 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001045 : "memory", "cc");
1046 tmp = (previous == *(uint64_t *)compare);
1047 *(uint64_t *)compare = previous;
1048 return (tmp);
1049}
1050
1051#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001052
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001053/* Use __ha_barrier_atomic* when you're trying to protect data that are
1054 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1055 */
1056static __inline void
1057__ha_barrier_atomic_load(void)
1058{
1059 __asm __volatile("dmb ishld" ::: "memory");
1060}
1061
1062static __inline void
1063__ha_barrier_atomic_store(void)
1064{
1065 __asm __volatile("dmb ishst" ::: "memory");
1066}
1067
1068static __inline void
1069__ha_barrier_atomic_full(void)
1070{
1071 __asm __volatile("dmb ish" ::: "memory");
1072}
1073
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001074static __inline void
1075__ha_barrier_load(void)
1076{
1077 __asm __volatile("dmb ishld" ::: "memory");
1078}
1079
1080static __inline void
1081__ha_barrier_store(void)
1082{
1083 __asm __volatile("dmb ishst" ::: "memory");
1084}
1085
1086static __inline void
1087__ha_barrier_full(void)
1088{
1089 __asm __volatile("dmb ish" ::: "memory");
1090}
1091
1092static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1093{
1094 void *value[2];
1095 uint64_t tmp1, tmp2;
1096
1097 __asm__ __volatile__("1:"
1098 "ldxp %0, %1, [%4];"
1099 "mov %2, %0;"
1100 "mov %3, %1;"
1101 "eor %0, %0, %5;"
1102 "eor %1, %1, %6;"
1103 "orr %1, %0, %1;"
1104 "mov %w0, #0;"
1105 "cbnz %1, 2f;"
1106 "stxp %w0, %7, %8, [%4];"
1107 "cbnz %w0, 1b;"
1108 "mov %w0, #1;"
1109 "2:"
1110 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1111 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1112 : "cc", "memory");
1113
1114 memcpy(compare, &value, sizeof(value));
1115 return (tmp1);
1116}
1117
1118#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001119#define __ha_barrier_atomic_load __sync_synchronize
1120#define __ha_barrier_atomic_store __sync_synchronize
1121#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001122#define __ha_barrier_load __sync_synchronize
1123#define __ha_barrier_store __sync_synchronize
1124#define __ha_barrier_full __sync_synchronize
1125#endif
1126
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001127void ha_spin_init(HA_SPINLOCK_T *l);
1128void ha_rwlock_init(HA_RWLOCK_T *l);
1129
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001130#endif /* USE_THREAD */
1131
Willy Tarreau149ab772019-01-26 14:27:06 +01001132extern int thread_cpus_enabled_at_boot;
1133
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001134static inline void __ha_compiler_barrier(void)
1135{
1136 __asm __volatile("" ::: "memory");
1137}
1138
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001139int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001140int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001141
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001142#ifndef _HA_ATOMIC_CAS
1143#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1144#endif /* !_HA_ATOMIC_CAS */
1145
Willy Tarreau6a38b322019-05-11 18:04:24 +02001146#ifndef _HA_ATOMIC_DWCAS
1147#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1148#endif /* !_HA_ATOMIC_CAS */
1149
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001150#ifndef _HA_ATOMIC_ADD
1151#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1152#endif /* !_HA_ATOMIC_ADD */
1153
1154#ifndef _HA_ATOMIC_XADD
1155#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1156#endif /* !_HA_ATOMIC_SUB */
1157
1158#ifndef _HA_ATOMIC_SUB
1159#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1160#endif /* !_HA_ATOMIC_SUB */
1161
1162#ifndef _HA_ATOMIC_AND
1163#define _HA_ATOMIC_AND HA_ATOMIC_AND
1164#endif /* !_HA_ATOMIC_AND */
1165
1166#ifndef _HA_ATOMIC_OR
1167#define _HA_ATOMIC_OR HA_ATOMIC_OR
1168#endif /* !_HA_ATOMIC_OR */
1169
1170#ifndef _HA_ATOMIC_XCHG
1171#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1172#endif /* !_HA_ATOMIC_XCHG */
1173
1174#ifndef _HA_ATOMIC_STORE
1175#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1176#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001177
1178#ifndef _HA_ATOMIC_LOAD
1179#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1180#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001181#endif /* _COMMON_HATHREADS_H */