blob: a19327e1059f5265e8817b4a925c467b4978dfb6 [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
25#include <common/config.h>
Willy Tarreau90fa97b2018-11-25 19:46:08 +010026#include <common/initcall.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020027
Willy Tarreau0ccd3222018-07-30 10:34:35 +020028/* Note about all_threads_mask :
Willy Tarreauda9e9392019-02-02 17:03:41 +010029 * - this variable is comprised between 1 and LONGBITS.
30 * - with threads support disabled, this symbol is defined as constant 1UL.
31 * - with threads enabled, it contains the mask of enabled threads. Thus if
32 * only one thread is enabled, it equals 1.
Willy Tarreau0ccd3222018-07-30 10:34:35 +020033 */
34
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020035#ifndef USE_THREAD
36
Willy Tarreau421f02e2018-01-20 18:19:22 +010037#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020038#define MAX_THREADS_MASK 1
39
40/* Only way found to replace variables with constants that are optimized away
41 * at build time.
42 */
43enum { all_threads_mask = 1UL };
44enum { tid_bit = 1UL };
45enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010046
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010047#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010048#define __decl_spinlock(lock)
49#define __decl_aligned_spinlock(lock)
50#define __decl_rwlock(lock)
51#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010052
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020053#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
54#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
55#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020056#define HA_ATOMIC_XADD(val, i) \
57 ({ \
58 typeof((val)) __p_xadd = (val); \
59 typeof(*(val)) __old_xadd = *__p_xadd; \
60 *__p_xadd += i; \
61 __old_xadd; \
62 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020063#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
64#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
65#define HA_ATOMIC_XCHG(val, new) \
66 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020067 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020068 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020069 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020070 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010071#define HA_ATOMIC_BTS(val, bit) \
72 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020073 typeof((val)) __p_bts = (val); \
74 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
75 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
76 if (!__t_bts) \
77 *__p_bts |= __b_bts; \
78 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010079 })
80#define HA_ATOMIC_BTR(val, bit) \
81 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020082 typeof((val)) __p_btr = (val); \
83 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
84 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
85 if (__t_btr) \
86 *__p_btr &= ~__b_btr; \
87 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010088 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +020089#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020090#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
91#define HA_ATOMIC_UPDATE_MAX(val, new) \
92 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020093 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020094 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020095 if (*(val) < __new_max) \
96 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020097 *(val); \
98 })
99
100#define HA_ATOMIC_UPDATE_MIN(val, new) \
101 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200102 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200103 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200104 if (*(val) > __new_min) \
105 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200106 *(val); \
107 })
108
Willy Tarreaub29dc952017-10-31 18:00:20 +0100109#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200110
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100111#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
112#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
113#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
114#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
115#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100117#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
118#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
119#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
120#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
121#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
122#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
123#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
124#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200125
William Lallemand6e1796e2018-06-07 11:23:40 +0200126#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
127
Willy Tarreau0c026f42018-08-01 19:12:20 +0200128static inline void ha_set_tid(unsigned int tid)
129{
130}
William Lallemand6e1796e2018-06-07 11:23:40 +0200131
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100132static inline void __ha_barrier_atomic_load(void)
133{
134}
135
136static inline void __ha_barrier_atomic_store(void)
137{
138}
139
140static inline void __ha_barrier_atomic_full(void)
141{
142}
143
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100144static inline void __ha_barrier_load(void)
145{
146}
147
148static inline void __ha_barrier_store(void)
149{
150}
151
152static inline void __ha_barrier_full(void)
153{
154}
155
Willy Tarreau60b639c2018-08-02 10:16:17 +0200156static inline void thread_harmless_now()
157{
158}
159
160static inline void thread_harmless_end()
161{
162}
163
164static inline void thread_isolate()
165{
166}
167
168static inline void thread_release()
169{
170}
171
172static inline unsigned long thread_isolated()
173{
174 return 1;
175}
176
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200177#else /* USE_THREAD */
178
179#include <stdio.h>
180#include <stdlib.h>
181#include <string.h>
182#include <pthread.h>
183#include <import/plock.h>
184
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100185#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100186#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100187#endif
188
189#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100190
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100191#define __decl_hathreads(decl) decl
192
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100193/* declare a self-initializing spinlock */
194#define __decl_spinlock(lock) \
195 HA_SPINLOCK_T (lock); \
196 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
197
198/* declare a self-initializing spinlock, aligned on a cache line */
199#define __decl_aligned_spinlock(lock) \
200 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
201 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
202
203/* declare a self-initializing rwlock */
204#define __decl_rwlock(lock) \
205 HA_RWLOCK_T (lock); \
206 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
207
208/* declare a self-initializing rwlock, aligned on a cache line */
209#define __decl_aligned_rwlock(lock) \
210 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
211 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
212
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200213/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
214 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100215
David Carlierec5e8452018-01-11 14:20:43 +0000216#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100217/* gcc < 4.7 */
218
219#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
220#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200221#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100222#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
223#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
224
225/* the CAS is a bit complicated. The older API doesn't support returning the
226 * value and the swap's result at the same time. So here we take what looks
227 * like the safest route, consisting in using the boolean version guaranteeing
228 * that the operation was performed or not, and we snoop a previous value. If
229 * the compare succeeds, we return. If it fails, we return the previous value,
230 * but only if it differs from the expected one. If it's the same it's a race
231 * thus we try again to avoid confusing a possibly sensitive caller.
232 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200233#define HA_ATOMIC_CAS(val, old, new) \
234 ({ \
235 typeof((val)) __val_cas = (val); \
236 typeof((old)) __oldp_cas = (old); \
237 typeof(*(old)) __oldv_cas; \
238 typeof((new)) __new_cas = (new); \
239 int __ret_cas; \
240 do { \
241 __oldv_cas = *__val_cas; \
242 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
243 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
244 if (!__ret_cas) \
245 *__oldp_cas = __oldv_cas; \
246 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100247 })
248
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200249#define HA_ATOMIC_XCHG(val, new) \
250 ({ \
251 typeof((val)) __val_xchg = (val); \
252 typeof(*(val)) __old_xchg; \
253 typeof((new)) __new_xchg = (new); \
254 do { __old_xchg = *__val_xchg; \
255 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
256 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100257 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100258
259#define HA_ATOMIC_BTS(val, bit) \
260 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200261 typeof(*(val)) __b_bts = (1UL << (bit)); \
262 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100263 })
264
265#define HA_ATOMIC_BTR(val, bit) \
266 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200267 typeof(*(val)) __b_btr = (1UL << (bit)); \
268 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100269 })
270
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200271#define HA_ATOMIC_LOAD(val) \
272 ({ \
273 typeof(*(val)) ret; \
274 __sync_synchronize(); \
275 ret = *(volatile typeof(val))val; \
276 __sync_synchronize(); \
277 ret; \
278 })
279
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200280#define HA_ATOMIC_STORE(val, new) \
281 ({ \
282 typeof((val)) __val_store = (val); \
283 typeof(*(val)) __old_store; \
284 typeof((new)) __new_store = (new); \
285 do { __old_store = *__val_store; \
286 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100287 })
288#else
289/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100290#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
291#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
292#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
293#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
294#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
295#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100296#define HA_ATOMIC_BTS(val, bit) \
297 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200298 typeof(*(val)) __b_bts = (1UL << (bit)); \
299 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100300 })
301
302#define HA_ATOMIC_BTR(val, bit) \
303 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200304 typeof(*(val)) __b_btr = (1UL << (bit)); \
305 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100306 })
307
Olivier Houchard11353792019-03-07 18:48:22 +0100308#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
309#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200310#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200311
312/* Variants that don't generate any memory barrier.
313 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
314 * that will always generate correct code.
315 * Usually it's fine to use those when updating data that have no dependency,
316 * ie updating a counter. Otherwise a barrier is required.
317 */
318#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
319#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
320#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
321#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
322#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
323#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
324#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
325#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200326#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200327
328#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100329
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200330#define HA_ATOMIC_UPDATE_MAX(val, new) \
331 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200332 typeof(*(val)) __old_max = *(val); \
333 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200334 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200335 while (__old_max < __new_max && \
336 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
337 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200338 })
339#define HA_ATOMIC_UPDATE_MIN(val, new) \
340 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200341 typeof(*(val)) __old_min = *(val); \
342 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200343 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200344 while (__old_min > __new_min && \
345 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
346 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200347 })
348
Willy Tarreaub29dc952017-10-31 18:00:20 +0100349#define HA_BARRIER() pl_barrier()
350
Willy Tarreau60b639c2018-08-02 10:16:17 +0200351void thread_harmless_till_end();
352void thread_isolate();
353void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200354
Willy Tarreau0c026f42018-08-01 19:12:20 +0200355extern THREAD_LOCAL unsigned int tid; /* The thread id */
356extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200357extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200358extern volatile unsigned long threads_want_rdv_mask;
359extern volatile unsigned long threads_harmless_mask;
360
361/* explanation for threads_want_rdv_mask and threads_harmless_mask :
362 * - threads_want_rdv_mask is a bit field indicating all threads that have
363 * requested a rendez-vous of other threads using thread_isolate().
364 * - threads_harmless_mask is a bit field indicating all threads that are
365 * currently harmless in that they promise not to access a shared resource.
366 *
367 * For a given thread, its bits in want_rdv and harmless can be translated like
368 * this :
369 *
370 * ----------+----------+----------------------------------------------------
371 * want_rdv | harmless | description
372 * ----------+----------+----------------------------------------------------
373 * 0 | 0 | thread not interested in RDV, possibly harmful
374 * 0 | 1 | thread not interested in RDV but harmless
375 * 1 | 1 | thread interested in RDV and waiting for its turn
376 * 1 | 0 | thread currently working isolated from others
377 * ----------+----------+----------------------------------------------------
378 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200379
William Lallemand6e1796e2018-06-07 11:23:40 +0200380#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
381
Willy Tarreau0c026f42018-08-01 19:12:20 +0200382/* sets the thread ID and the TID bit for the current thread */
383static inline void ha_set_tid(unsigned int data)
384{
385 tid = data;
386 tid_bit = (1UL << tid);
387}
388
Willy Tarreau60b639c2018-08-02 10:16:17 +0200389/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
390 * not be touching any unprotected shared resource during this period. Usually
391 * this is called before poll(), but it may also be placed around very slow
392 * calls (eg: some crypto operations). Needs to be terminated using
393 * thread_harmless_end().
394 */
395static inline void thread_harmless_now()
396{
397 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
398}
399
400/* Ends the harmless period started by thread_harmless_now(). Usually this is
401 * placed after the poll() call. If it is discovered that a job was running and
402 * is relying on the thread still being harmless, the thread waits for the
403 * other one to finish.
404 */
405static inline void thread_harmless_end()
406{
407 while (1) {
408 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
409 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
410 break;
411 thread_harmless_till_end();
412 }
413}
414
415/* an isolated thread has harmless cleared and want_rdv set */
416static inline unsigned long thread_isolated()
417{
418 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
419}
420
William Lallemand6e1796e2018-06-07 11:23:40 +0200421
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200422#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
423
Christopher Fauletf51bac22018-01-30 11:04:29 +0100424/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200425enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200426 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200427 TASK_RQ_LOCK,
428 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200429 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200430 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200431 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200432 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200433 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200434 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200435 STK_TABLE_LOCK,
436 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200437 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200438 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200439 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200440 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200441 SSL_LOCK,
442 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200443 PATREF_LOCK,
444 PATEXP_LOCK,
445 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200446 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200447 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200448 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200449 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200450 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200451 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200452 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200453 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100454 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100455 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100456 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100457 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200458 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000459 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200460 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200461};
462struct lock_stat {
463 uint64_t nsec_wait_for_write;
464 uint64_t nsec_wait_for_read;
465 uint64_t num_write_locked;
466 uint64_t num_write_unlocked;
467 uint64_t num_read_locked;
468 uint64_t num_read_unlocked;
469};
470
471extern struct lock_stat lock_stats[LOCK_LABELS];
472
473#define __HA_SPINLOCK_T unsigned long
474
475#define __SPIN_INIT(l) ({ (*l) = 0; })
476#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100477#define __SPIN_LOCK(l) pl_take_s(l)
478#define __SPIN_TRYLOCK(l) !pl_try_s(l)
479#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200480
481#define __HA_RWLOCK_T unsigned long
482
483#define __RWLOCK_INIT(l) ({ (*l) = 0; })
484#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
485#define __RWLOCK_WRLOCK(l) pl_take_w(l)
486#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
487#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
488#define __RWLOCK_RDLOCK(l) pl_take_r(l)
489#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
490#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
491
492#define HA_SPINLOCK_T struct ha_spinlock
493
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100494#define HA_SPIN_INIT(l) __spin_init(l)
495#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200496
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100497#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
498#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
499#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200500
501#define HA_RWLOCK_T struct ha_rwlock
502
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100503#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
504#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
505#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
506#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
507#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
508#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
509#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
510#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200511
512struct ha_spinlock {
513 __HA_SPINLOCK_T lock;
514 struct {
515 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
516 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
517 struct {
518 const char *function;
519 const char *file;
520 int line;
521 } last_location; /* location of the last owner */
522 } info;
523};
524
525struct ha_rwlock {
526 __HA_RWLOCK_T lock;
527 struct {
528 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
529 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
530 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
531 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
532 struct {
533 const char *function;
534 const char *file;
535 int line;
536 } last_location; /* location of the last write owner */
537 } info;
538};
539
Christopher Fauletf51bac22018-01-30 11:04:29 +0100540static inline const char *lock_label(enum lock_label label)
541{
542 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100543 case FD_LOCK: return "FD";
544 case TASK_RQ_LOCK: return "TASK_RQ";
545 case TASK_WQ_LOCK: return "TASK_WQ";
546 case POOL_LOCK: return "POOL";
547 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100548 case PROXY_LOCK: return "PROXY";
549 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100550 case LBPRM_LOCK: return "LBPRM";
551 case SIGNALS_LOCK: return "SIGNALS";
552 case STK_TABLE_LOCK: return "STK_TABLE";
553 case STK_SESS_LOCK: return "STK_SESS";
554 case APPLETS_LOCK: return "APPLETS";
555 case PEER_LOCK: return "PEER";
556 case BUF_WQ_LOCK: return "BUF_WQ";
557 case STRMS_LOCK: return "STRMS";
558 case SSL_LOCK: return "SSL";
559 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
560 case PATREF_LOCK: return "PATREF";
561 case PATEXP_LOCK: return "PATEXP";
562 case PATLRU_LOCK: return "PATLRU";
563 case VARS_LOCK: return "VARS";
564 case COMP_POOL_LOCK: return "COMP_POOL";
565 case LUA_LOCK: return "LUA";
566 case NOTIF_LOCK: return "NOTIF";
567 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
568 case DNS_LOCK: return "DNS";
569 case PID_LIST_LOCK: return "PID_LIST";
570 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
571 case PIPES_LOCK: return "PIPES";
572 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100573 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100574 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200575 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000576 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100577 case LOCK_LABELS: break; /* keep compiler happy */
578 };
579 /* only way to come here is consecutive to an internal bug */
580 abort();
581}
582
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200583static inline void show_lock_stats()
584{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200585 int lbl;
586
587 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
588 fprintf(stderr,
589 "Stats about Lock %s: \n"
590 "\t # write lock : %lu\n"
591 "\t # write unlock: %lu (%ld)\n"
592 "\t # wait time for write : %.3f msec\n"
593 "\t # wait time for write/lock: %.3f nsec\n"
594 "\t # read lock : %lu\n"
595 "\t # read unlock : %lu (%ld)\n"
596 "\t # wait time for read : %.3f msec\n"
597 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100598 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200599 lock_stats[lbl].num_write_locked,
600 lock_stats[lbl].num_write_unlocked,
601 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
602 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
603 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
604 lock_stats[lbl].num_read_locked,
605 lock_stats[lbl].num_read_unlocked,
606 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
607 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
608 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
609 }
610}
611
612/* Following functions are used to collect some stats about locks. We wrap
613 * pthread functions to known how much time we wait in a lock. */
614
615static uint64_t nsec_now(void) {
616 struct timespec ts;
617
618 clock_gettime(CLOCK_MONOTONIC, &ts);
619 return ((uint64_t) ts.tv_sec * 1000000000ULL +
620 (uint64_t) ts.tv_nsec);
621}
622
623static inline void __ha_rwlock_init(struct ha_rwlock *l)
624{
625 memset(l, 0, sizeof(struct ha_rwlock));
626 __RWLOCK_INIT(&l->lock);
627}
628
629static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
630{
631 __RWLOCK_DESTROY(&l->lock);
632 memset(l, 0, sizeof(struct ha_rwlock));
633}
634
635
636static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
637 const char *func, const char *file, int line)
638{
639 uint64_t start_time;
640
641 if (unlikely(l->info.cur_writer & tid_bit)) {
642 /* the thread is already owning the lock for write */
643 abort();
644 }
645
646 if (unlikely(l->info.cur_readers & tid_bit)) {
647 /* the thread is already owning the lock for read */
648 abort();
649 }
650
651 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
652
653 start_time = nsec_now();
654 __RWLOCK_WRLOCK(&l->lock);
655 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
656
657 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
658
659 l->info.cur_writer = tid_bit;
660 l->info.last_location.function = func;
661 l->info.last_location.file = file;
662 l->info.last_location.line = line;
663
664 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
665}
666
667static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
668 const char *func, const char *file, int line)
669{
670 uint64_t start_time;
671 int r;
672
673 if (unlikely(l->info.cur_writer & tid_bit)) {
674 /* the thread is already owning the lock for write */
675 abort();
676 }
677
678 if (unlikely(l->info.cur_readers & tid_bit)) {
679 /* the thread is already owning the lock for read */
680 abort();
681 }
682
683 /* We set waiting writer because trywrlock could wait for readers to quit */
684 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
685
686 start_time = nsec_now();
687 r = __RWLOCK_TRYWRLOCK(&l->lock);
688 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
689 if (unlikely(r)) {
690 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
691 return r;
692 }
693 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
694
695 l->info.cur_writer = tid_bit;
696 l->info.last_location.function = func;
697 l->info.last_location.file = file;
698 l->info.last_location.line = line;
699
700 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
701
702 return 0;
703}
704
705static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
706 const char *func, const char *file, int line)
707{
708 if (unlikely(!(l->info.cur_writer & tid_bit))) {
709 /* the thread is not owning the lock for write */
710 abort();
711 }
712
713 l->info.cur_writer = 0;
714 l->info.last_location.function = func;
715 l->info.last_location.file = file;
716 l->info.last_location.line = line;
717
718 __RWLOCK_WRUNLOCK(&l->lock);
719
720 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
721}
722
723static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
724{
725 uint64_t start_time;
726
727 if (unlikely(l->info.cur_writer & tid_bit)) {
728 /* the thread is already owning the lock for write */
729 abort();
730 }
731
732 if (unlikely(l->info.cur_readers & tid_bit)) {
733 /* the thread is already owning the lock for read */
734 abort();
735 }
736
737 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
738
739 start_time = nsec_now();
740 __RWLOCK_RDLOCK(&l->lock);
741 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
742 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
743
744 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
745
746 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
747}
748
749static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
750{
751 int r;
752
753 if (unlikely(l->info.cur_writer & tid_bit)) {
754 /* the thread is already owning the lock for write */
755 abort();
756 }
757
758 if (unlikely(l->info.cur_readers & tid_bit)) {
759 /* the thread is already owning the lock for read */
760 abort();
761 }
762
763 /* try read should never wait */
764 r = __RWLOCK_TRYRDLOCK(&l->lock);
765 if (unlikely(r))
766 return r;
767 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
768
769 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
770
771 return 0;
772}
773
774static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
775{
776 if (unlikely(!(l->info.cur_readers & tid_bit))) {
777 /* the thread is not owning the lock for read */
778 abort();
779 }
780
781 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
782
783 __RWLOCK_RDUNLOCK(&l->lock);
784
785 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
786}
787
788static inline void __spin_init(struct ha_spinlock *l)
789{
790 memset(l, 0, sizeof(struct ha_spinlock));
791 __SPIN_INIT(&l->lock);
792}
793
794static inline void __spin_destroy(struct ha_spinlock *l)
795{
796 __SPIN_DESTROY(&l->lock);
797 memset(l, 0, sizeof(struct ha_spinlock));
798}
799
800static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
801 const char *func, const char *file, int line)
802{
803 uint64_t start_time;
804
805 if (unlikely(l->info.owner & tid_bit)) {
806 /* the thread is already owning the lock */
807 abort();
808 }
809
810 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
811
812 start_time = nsec_now();
813 __SPIN_LOCK(&l->lock);
814 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
815
816 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
817
818
819 l->info.owner = tid_bit;
820 l->info.last_location.function = func;
821 l->info.last_location.file = file;
822 l->info.last_location.line = line;
823
824 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
825}
826
827static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
828 const char *func, const char *file, int line)
829{
830 int r;
831
832 if (unlikely(l->info.owner & tid_bit)) {
833 /* the thread is already owning the lock */
834 abort();
835 }
836
837 /* try read should never wait */
838 r = __SPIN_TRYLOCK(&l->lock);
839 if (unlikely(r))
840 return r;
841 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
842
843 l->info.owner = tid_bit;
844 l->info.last_location.function = func;
845 l->info.last_location.file = file;
846 l->info.last_location.line = line;
847
848 return 0;
849}
850
851static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
852 const char *func, const char *file, int line)
853{
854 if (unlikely(!(l->info.owner & tid_bit))) {
855 /* the thread is not owning the lock */
856 abort();
857 }
858
859 l->info.owner = 0;
860 l->info.last_location.function = func;
861 l->info.last_location.file = file;
862 l->info.last_location.line = line;
863
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100864 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200865 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
866}
867
868#else /* DEBUG_THREAD */
869
870#define HA_SPINLOCK_T unsigned long
871
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100872#define HA_SPIN_INIT(l) ({ (*l) = 0; })
873#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
874#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
875#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
876#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200877
878#define HA_RWLOCK_T unsigned long
879
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100880#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
881#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
882#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
883#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
884#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
885#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
886#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
887#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200888
889#endif /* DEBUG_THREAD */
890
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100891#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200892
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100893static __inline int
894__ha_cas_dw(void *target, void *compare, const void *set)
895{
896 char ret;
897
898 __asm __volatile("lock cmpxchg16b %0; setz %3"
899 : "+m" (*(void **)target),
900 "=a" (((void **)compare)[0]),
901 "=d" (((void **)compare)[1]),
902 "=q" (ret)
903 : "a" (((void **)compare)[0]),
904 "d" (((void **)compare)[1]),
905 "b" (((const void **)set)[0]),
906 "c" (((const void **)set)[1])
907 : "memory", "cc");
908 return (ret);
909}
910
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100911/* Use __ha_barrier_atomic* when you're trying to protect data that are
912 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
913 */
914static __inline void
915__ha_barrier_atomic_load(void)
916{
917 __asm __volatile("" ::: "memory");
918}
919
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100920static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100921__ha_barrier_atomic_store(void)
922{
923 __asm __volatile("" ::: "memory");
924}
925
926static __inline void
927__ha_barrier_atomic_full(void)
928{
929 __asm __volatile("" ::: "memory");
930}
931
932static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100933__ha_barrier_load(void)
934{
935 __asm __volatile("lfence" ::: "memory");
936}
937
938static __inline void
939__ha_barrier_store(void)
940{
941 __asm __volatile("sfence" ::: "memory");
942}
943
944static __inline void
945__ha_barrier_full(void)
946{
947 __asm __volatile("mfence" ::: "memory");
948}
949
950#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200951
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100952/* Use __ha_barrier_atomic* when you're trying to protect data that are
953 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
954 */
955static __inline void
956__ha_barrier_atomic_load(void)
957{
958 __asm __volatile("dmb" ::: "memory");
959}
960
961static __inline void
962__ha_barrier_atomic_store(void)
963{
964 __asm __volatile("dsb" ::: "memory");
965}
966
967static __inline void
968__ha_barrier_atomic_full(void)
969{
970 __asm __volatile("dmb" ::: "memory");
971}
972
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100973static __inline void
974__ha_barrier_load(void)
975{
976 __asm __volatile("dmb" ::: "memory");
977}
978
979static __inline void
980__ha_barrier_store(void)
981{
982 __asm __volatile("dsb" ::: "memory");
983}
984
985static __inline void
986__ha_barrier_full(void)
987{
988 __asm __volatile("dmb" ::: "memory");
989}
990
Willy Tarreau41ccb192018-02-14 14:16:28 +0100991static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100992{
993 uint64_t previous;
994 int tmp;
995
996 __asm __volatile("1:"
997 "ldrexd %0, [%4];"
998 "cmp %Q0, %Q2;"
999 "ittt eq;"
1000 "cmpeq %R0, %R2;"
1001 "strexdeq %1, %3, [%4];"
1002 "cmpeq %1, #1;"
1003 "beq 1b;"
1004 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001005 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001006 : "memory", "cc");
1007 tmp = (previous == *(uint64_t *)compare);
1008 *(uint64_t *)compare = previous;
1009 return (tmp);
1010}
1011
1012#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001013
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001014/* Use __ha_barrier_atomic* when you're trying to protect data that are
1015 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1016 */
1017static __inline void
1018__ha_barrier_atomic_load(void)
1019{
1020 __asm __volatile("dmb ishld" ::: "memory");
1021}
1022
1023static __inline void
1024__ha_barrier_atomic_store(void)
1025{
1026 __asm __volatile("dmb ishst" ::: "memory");
1027}
1028
1029static __inline void
1030__ha_barrier_atomic_full(void)
1031{
1032 __asm __volatile("dmb ish" ::: "memory");
1033}
1034
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001035static __inline void
1036__ha_barrier_load(void)
1037{
1038 __asm __volatile("dmb ishld" ::: "memory");
1039}
1040
1041static __inline void
1042__ha_barrier_store(void)
1043{
1044 __asm __volatile("dmb ishst" ::: "memory");
1045}
1046
1047static __inline void
1048__ha_barrier_full(void)
1049{
1050 __asm __volatile("dmb ish" ::: "memory");
1051}
1052
1053static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1054{
1055 void *value[2];
1056 uint64_t tmp1, tmp2;
1057
1058 __asm__ __volatile__("1:"
1059 "ldxp %0, %1, [%4];"
1060 "mov %2, %0;"
1061 "mov %3, %1;"
1062 "eor %0, %0, %5;"
1063 "eor %1, %1, %6;"
1064 "orr %1, %0, %1;"
1065 "mov %w0, #0;"
1066 "cbnz %1, 2f;"
1067 "stxp %w0, %7, %8, [%4];"
1068 "cbnz %w0, 1b;"
1069 "mov %w0, #1;"
1070 "2:"
1071 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1072 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1073 : "cc", "memory");
1074
1075 memcpy(compare, &value, sizeof(value));
1076 return (tmp1);
1077}
1078
1079#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001080#define __ha_barrier_atomic_load __sync_synchronize
1081#define __ha_barrier_atomic_store __sync_synchronize
1082#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001083#define __ha_barrier_load __sync_synchronize
1084#define __ha_barrier_store __sync_synchronize
1085#define __ha_barrier_full __sync_synchronize
1086#endif
1087
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001088void ha_spin_init(HA_SPINLOCK_T *l);
1089void ha_rwlock_init(HA_RWLOCK_T *l);
1090
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001091#endif /* USE_THREAD */
1092
Willy Tarreau149ab772019-01-26 14:27:06 +01001093extern int thread_cpus_enabled_at_boot;
1094
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001095static inline void __ha_compiler_barrier(void)
1096{
1097 __asm __volatile("" ::: "memory");
1098}
1099
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001100int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001101int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001102
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001103#ifndef _HA_ATOMIC_CAS
1104#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1105#endif /* !_HA_ATOMIC_CAS */
1106
1107#ifndef _HA_ATOMIC_ADD
1108#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1109#endif /* !_HA_ATOMIC_ADD */
1110
1111#ifndef _HA_ATOMIC_XADD
1112#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1113#endif /* !_HA_ATOMIC_SUB */
1114
1115#ifndef _HA_ATOMIC_SUB
1116#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1117#endif /* !_HA_ATOMIC_SUB */
1118
1119#ifndef _HA_ATOMIC_AND
1120#define _HA_ATOMIC_AND HA_ATOMIC_AND
1121#endif /* !_HA_ATOMIC_AND */
1122
1123#ifndef _HA_ATOMIC_OR
1124#define _HA_ATOMIC_OR HA_ATOMIC_OR
1125#endif /* !_HA_ATOMIC_OR */
1126
1127#ifndef _HA_ATOMIC_XCHG
1128#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1129#endif /* !_HA_ATOMIC_XCHG */
1130
1131#ifndef _HA_ATOMIC_STORE
1132#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1133#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001134
1135#ifndef _HA_ATOMIC_LOAD
1136#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1137#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001138#endif /* _COMMON_HATHREADS_H */