blob: 7cbb3495b8b8170b4998bee285d16d54925849d0 [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 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020089#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
90#define HA_ATOMIC_UPDATE_MAX(val, new) \
91 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020092 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020093 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020094 if (*(val) < __new_max) \
95 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020096 *(val); \
97 })
98
99#define HA_ATOMIC_UPDATE_MIN(val, new) \
100 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200101 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200102 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200103 if (*(val) > __new_min) \
104 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200105 *(val); \
106 })
107
Willy Tarreaub29dc952017-10-31 18:00:20 +0100108#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200109
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100110#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
111#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
112#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
113#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
114#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200115
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100116#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
117#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
118#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
119#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
120#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
121#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
122#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
123#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200124
William Lallemand6e1796e2018-06-07 11:23:40 +0200125#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
126
Willy Tarreau0c026f42018-08-01 19:12:20 +0200127static inline void ha_set_tid(unsigned int tid)
128{
129}
William Lallemand6e1796e2018-06-07 11:23:40 +0200130
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100131static inline void __ha_barrier_atomic_load(void)
132{
133}
134
135static inline void __ha_barrier_atomic_store(void)
136{
137}
138
139static inline void __ha_barrier_atomic_full(void)
140{
141}
142
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100143static inline void __ha_barrier_load(void)
144{
145}
146
147static inline void __ha_barrier_store(void)
148{
149}
150
151static inline void __ha_barrier_full(void)
152{
153}
154
Willy Tarreau60b639c2018-08-02 10:16:17 +0200155static inline void thread_harmless_now()
156{
157}
158
159static inline void thread_harmless_end()
160{
161}
162
163static inline void thread_isolate()
164{
165}
166
167static inline void thread_release()
168{
169}
170
171static inline unsigned long thread_isolated()
172{
173 return 1;
174}
175
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200176#else /* USE_THREAD */
177
178#include <stdio.h>
179#include <stdlib.h>
180#include <string.h>
181#include <pthread.h>
182#include <import/plock.h>
183
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100184#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100185#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100186#endif
187
188#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100189
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100190#define __decl_hathreads(decl) decl
191
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100192/* declare a self-initializing spinlock */
193#define __decl_spinlock(lock) \
194 HA_SPINLOCK_T (lock); \
195 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
196
197/* declare a self-initializing spinlock, aligned on a cache line */
198#define __decl_aligned_spinlock(lock) \
199 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
200 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
201
202/* declare a self-initializing rwlock */
203#define __decl_rwlock(lock) \
204 HA_RWLOCK_T (lock); \
205 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
206
207/* declare a self-initializing rwlock, aligned on a cache line */
208#define __decl_aligned_rwlock(lock) \
209 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
210 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
211
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200212/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
213 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100214
David Carlierec5e8452018-01-11 14:20:43 +0000215#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100216/* gcc < 4.7 */
217
218#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
219#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200220#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100221#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
222#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
223
224/* the CAS is a bit complicated. The older API doesn't support returning the
225 * value and the swap's result at the same time. So here we take what looks
226 * like the safest route, consisting in using the boolean version guaranteeing
227 * that the operation was performed or not, and we snoop a previous value. If
228 * the compare succeeds, we return. If it fails, we return the previous value,
229 * but only if it differs from the expected one. If it's the same it's a race
230 * thus we try again to avoid confusing a possibly sensitive caller.
231 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200232#define HA_ATOMIC_CAS(val, old, new) \
233 ({ \
234 typeof((val)) __val_cas = (val); \
235 typeof((old)) __oldp_cas = (old); \
236 typeof(*(old)) __oldv_cas; \
237 typeof((new)) __new_cas = (new); \
238 int __ret_cas; \
239 do { \
240 __oldv_cas = *__val_cas; \
241 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
242 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
243 if (!__ret_cas) \
244 *__oldp_cas = __oldv_cas; \
245 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100246 })
247
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200248#define HA_ATOMIC_XCHG(val, new) \
249 ({ \
250 typeof((val)) __val_xchg = (val); \
251 typeof(*(val)) __old_xchg; \
252 typeof((new)) __new_xchg = (new); \
253 do { __old_xchg = *__val_xchg; \
254 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
255 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100256 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100257
258#define HA_ATOMIC_BTS(val, bit) \
259 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200260 typeof(*(val)) __b_bts = (1UL << (bit)); \
261 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100262 })
263
264#define HA_ATOMIC_BTR(val, bit) \
265 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200266 typeof(*(val)) __b_btr = (1UL << (bit)); \
267 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100268 })
269
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200270#define HA_ATOMIC_STORE(val, new) \
271 ({ \
272 typeof((val)) __val_store = (val); \
273 typeof(*(val)) __old_store; \
274 typeof((new)) __new_store = (new); \
275 do { __old_store = *__val_store; \
276 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100277 })
278#else
279/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100280#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
281#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
282#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
283#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
284#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
285#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100286#define HA_ATOMIC_BTS(val, bit) \
287 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200288 typeof(*(val)) __b_bts = (1UL << (bit)); \
289 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100290 })
291
292#define HA_ATOMIC_BTR(val, bit) \
293 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200294 typeof(*(val)) __b_btr = (1UL << (bit)); \
295 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100296 })
297
Olivier Houchard11353792019-03-07 18:48:22 +0100298#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
299#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200300
301/* Variants that don't generate any memory barrier.
302 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
303 * that will always generate correct code.
304 * Usually it's fine to use those when updating data that have no dependency,
305 * ie updating a counter. Otherwise a barrier is required.
306 */
307#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
308#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
309#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
310#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
311#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
312#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
313#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
314#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
315
316#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100317
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200318#define HA_ATOMIC_UPDATE_MAX(val, new) \
319 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200320 typeof(*(val)) __old_max = *(val); \
321 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200322 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200323 while (__old_max < __new_max && \
324 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
325 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200326 })
327#define HA_ATOMIC_UPDATE_MIN(val, new) \
328 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200329 typeof(*(val)) __old_min = *(val); \
330 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200331 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200332 while (__old_min > __new_min && \
333 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
334 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200335 })
336
Willy Tarreaub29dc952017-10-31 18:00:20 +0100337#define HA_BARRIER() pl_barrier()
338
Willy Tarreau60b639c2018-08-02 10:16:17 +0200339void thread_harmless_till_end();
340void thread_isolate();
341void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200342
Willy Tarreau0c026f42018-08-01 19:12:20 +0200343extern THREAD_LOCAL unsigned int tid; /* The thread id */
344extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200345extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200346extern volatile unsigned long threads_want_rdv_mask;
347extern volatile unsigned long threads_harmless_mask;
348
349/* explanation for threads_want_rdv_mask and threads_harmless_mask :
350 * - threads_want_rdv_mask is a bit field indicating all threads that have
351 * requested a rendez-vous of other threads using thread_isolate().
352 * - threads_harmless_mask is a bit field indicating all threads that are
353 * currently harmless in that they promise not to access a shared resource.
354 *
355 * For a given thread, its bits in want_rdv and harmless can be translated like
356 * this :
357 *
358 * ----------+----------+----------------------------------------------------
359 * want_rdv | harmless | description
360 * ----------+----------+----------------------------------------------------
361 * 0 | 0 | thread not interested in RDV, possibly harmful
362 * 0 | 1 | thread not interested in RDV but harmless
363 * 1 | 1 | thread interested in RDV and waiting for its turn
364 * 1 | 0 | thread currently working isolated from others
365 * ----------+----------+----------------------------------------------------
366 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200367
William Lallemand6e1796e2018-06-07 11:23:40 +0200368#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
369
Willy Tarreau0c026f42018-08-01 19:12:20 +0200370/* sets the thread ID and the TID bit for the current thread */
371static inline void ha_set_tid(unsigned int data)
372{
373 tid = data;
374 tid_bit = (1UL << tid);
375}
376
Willy Tarreau60b639c2018-08-02 10:16:17 +0200377/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
378 * not be touching any unprotected shared resource during this period. Usually
379 * this is called before poll(), but it may also be placed around very slow
380 * calls (eg: some crypto operations). Needs to be terminated using
381 * thread_harmless_end().
382 */
383static inline void thread_harmless_now()
384{
385 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
386}
387
388/* Ends the harmless period started by thread_harmless_now(). Usually this is
389 * placed after the poll() call. If it is discovered that a job was running and
390 * is relying on the thread still being harmless, the thread waits for the
391 * other one to finish.
392 */
393static inline void thread_harmless_end()
394{
395 while (1) {
396 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
397 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
398 break;
399 thread_harmless_till_end();
400 }
401}
402
403/* an isolated thread has harmless cleared and want_rdv set */
404static inline unsigned long thread_isolated()
405{
406 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
407}
408
William Lallemand6e1796e2018-06-07 11:23:40 +0200409
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200410#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
411
Christopher Fauletf51bac22018-01-30 11:04:29 +0100412/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200413enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200414 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200415 TASK_RQ_LOCK,
416 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200417 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200418 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200419 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200420 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200421 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200422 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200423 STK_TABLE_LOCK,
424 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200425 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200426 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200427 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200428 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200429 SSL_LOCK,
430 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200431 PATREF_LOCK,
432 PATEXP_LOCK,
433 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200434 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200435 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200436 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200437 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200438 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200439 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200440 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200441 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100442 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100443 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100444 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100445 AUTH_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000446 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200447 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200448};
449struct lock_stat {
450 uint64_t nsec_wait_for_write;
451 uint64_t nsec_wait_for_read;
452 uint64_t num_write_locked;
453 uint64_t num_write_unlocked;
454 uint64_t num_read_locked;
455 uint64_t num_read_unlocked;
456};
457
458extern struct lock_stat lock_stats[LOCK_LABELS];
459
460#define __HA_SPINLOCK_T unsigned long
461
462#define __SPIN_INIT(l) ({ (*l) = 0; })
463#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100464#define __SPIN_LOCK(l) pl_take_s(l)
465#define __SPIN_TRYLOCK(l) !pl_try_s(l)
466#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200467
468#define __HA_RWLOCK_T unsigned long
469
470#define __RWLOCK_INIT(l) ({ (*l) = 0; })
471#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
472#define __RWLOCK_WRLOCK(l) pl_take_w(l)
473#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
474#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
475#define __RWLOCK_RDLOCK(l) pl_take_r(l)
476#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
477#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
478
479#define HA_SPINLOCK_T struct ha_spinlock
480
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100481#define HA_SPIN_INIT(l) __spin_init(l)
482#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200483
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100484#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
485#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
486#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200487
488#define HA_RWLOCK_T struct ha_rwlock
489
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100490#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
491#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
492#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
493#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
494#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
495#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
496#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
497#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200498
499struct ha_spinlock {
500 __HA_SPINLOCK_T lock;
501 struct {
502 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
503 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
504 struct {
505 const char *function;
506 const char *file;
507 int line;
508 } last_location; /* location of the last owner */
509 } info;
510};
511
512struct ha_rwlock {
513 __HA_RWLOCK_T lock;
514 struct {
515 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
516 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
517 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
518 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
519 struct {
520 const char *function;
521 const char *file;
522 int line;
523 } last_location; /* location of the last write owner */
524 } info;
525};
526
Christopher Fauletf51bac22018-01-30 11:04:29 +0100527static inline const char *lock_label(enum lock_label label)
528{
529 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100530 case FD_LOCK: return "FD";
531 case TASK_RQ_LOCK: return "TASK_RQ";
532 case TASK_WQ_LOCK: return "TASK_WQ";
533 case POOL_LOCK: return "POOL";
534 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100535 case PROXY_LOCK: return "PROXY";
536 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100537 case LBPRM_LOCK: return "LBPRM";
538 case SIGNALS_LOCK: return "SIGNALS";
539 case STK_TABLE_LOCK: return "STK_TABLE";
540 case STK_SESS_LOCK: return "STK_SESS";
541 case APPLETS_LOCK: return "APPLETS";
542 case PEER_LOCK: return "PEER";
543 case BUF_WQ_LOCK: return "BUF_WQ";
544 case STRMS_LOCK: return "STRMS";
545 case SSL_LOCK: return "SSL";
546 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
547 case PATREF_LOCK: return "PATREF";
548 case PATEXP_LOCK: return "PATEXP";
549 case PATLRU_LOCK: return "PATLRU";
550 case VARS_LOCK: return "VARS";
551 case COMP_POOL_LOCK: return "COMP_POOL";
552 case LUA_LOCK: return "LUA";
553 case NOTIF_LOCK: return "NOTIF";
554 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
555 case DNS_LOCK: return "DNS";
556 case PID_LIST_LOCK: return "PID_LIST";
557 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
558 case PIPES_LOCK: return "PIPES";
559 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100560 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100561 case AUTH_LOCK: return "AUTH";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000562 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100563 case LOCK_LABELS: break; /* keep compiler happy */
564 };
565 /* only way to come here is consecutive to an internal bug */
566 abort();
567}
568
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200569static inline void show_lock_stats()
570{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200571 int lbl;
572
573 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
574 fprintf(stderr,
575 "Stats about Lock %s: \n"
576 "\t # write lock : %lu\n"
577 "\t # write unlock: %lu (%ld)\n"
578 "\t # wait time for write : %.3f msec\n"
579 "\t # wait time for write/lock: %.3f nsec\n"
580 "\t # read lock : %lu\n"
581 "\t # read unlock : %lu (%ld)\n"
582 "\t # wait time for read : %.3f msec\n"
583 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100584 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200585 lock_stats[lbl].num_write_locked,
586 lock_stats[lbl].num_write_unlocked,
587 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
588 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
589 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
590 lock_stats[lbl].num_read_locked,
591 lock_stats[lbl].num_read_unlocked,
592 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
593 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
594 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
595 }
596}
597
598/* Following functions are used to collect some stats about locks. We wrap
599 * pthread functions to known how much time we wait in a lock. */
600
601static uint64_t nsec_now(void) {
602 struct timespec ts;
603
604 clock_gettime(CLOCK_MONOTONIC, &ts);
605 return ((uint64_t) ts.tv_sec * 1000000000ULL +
606 (uint64_t) ts.tv_nsec);
607}
608
609static inline void __ha_rwlock_init(struct ha_rwlock *l)
610{
611 memset(l, 0, sizeof(struct ha_rwlock));
612 __RWLOCK_INIT(&l->lock);
613}
614
615static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
616{
617 __RWLOCK_DESTROY(&l->lock);
618 memset(l, 0, sizeof(struct ha_rwlock));
619}
620
621
622static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
623 const char *func, const char *file, int line)
624{
625 uint64_t start_time;
626
627 if (unlikely(l->info.cur_writer & tid_bit)) {
628 /* the thread is already owning the lock for write */
629 abort();
630 }
631
632 if (unlikely(l->info.cur_readers & tid_bit)) {
633 /* the thread is already owning the lock for read */
634 abort();
635 }
636
637 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
638
639 start_time = nsec_now();
640 __RWLOCK_WRLOCK(&l->lock);
641 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
642
643 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
644
645 l->info.cur_writer = tid_bit;
646 l->info.last_location.function = func;
647 l->info.last_location.file = file;
648 l->info.last_location.line = line;
649
650 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
651}
652
653static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
654 const char *func, const char *file, int line)
655{
656 uint64_t start_time;
657 int r;
658
659 if (unlikely(l->info.cur_writer & tid_bit)) {
660 /* the thread is already owning the lock for write */
661 abort();
662 }
663
664 if (unlikely(l->info.cur_readers & tid_bit)) {
665 /* the thread is already owning the lock for read */
666 abort();
667 }
668
669 /* We set waiting writer because trywrlock could wait for readers to quit */
670 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
671
672 start_time = nsec_now();
673 r = __RWLOCK_TRYWRLOCK(&l->lock);
674 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
675 if (unlikely(r)) {
676 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
677 return r;
678 }
679 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
680
681 l->info.cur_writer = tid_bit;
682 l->info.last_location.function = func;
683 l->info.last_location.file = file;
684 l->info.last_location.line = line;
685
686 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
687
688 return 0;
689}
690
691static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
692 const char *func, const char *file, int line)
693{
694 if (unlikely(!(l->info.cur_writer & tid_bit))) {
695 /* the thread is not owning the lock for write */
696 abort();
697 }
698
699 l->info.cur_writer = 0;
700 l->info.last_location.function = func;
701 l->info.last_location.file = file;
702 l->info.last_location.line = line;
703
704 __RWLOCK_WRUNLOCK(&l->lock);
705
706 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
707}
708
709static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
710{
711 uint64_t start_time;
712
713 if (unlikely(l->info.cur_writer & tid_bit)) {
714 /* the thread is already owning the lock for write */
715 abort();
716 }
717
718 if (unlikely(l->info.cur_readers & tid_bit)) {
719 /* the thread is already owning the lock for read */
720 abort();
721 }
722
723 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
724
725 start_time = nsec_now();
726 __RWLOCK_RDLOCK(&l->lock);
727 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
728 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
729
730 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
731
732 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
733}
734
735static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
736{
737 int r;
738
739 if (unlikely(l->info.cur_writer & tid_bit)) {
740 /* the thread is already owning the lock for write */
741 abort();
742 }
743
744 if (unlikely(l->info.cur_readers & tid_bit)) {
745 /* the thread is already owning the lock for read */
746 abort();
747 }
748
749 /* try read should never wait */
750 r = __RWLOCK_TRYRDLOCK(&l->lock);
751 if (unlikely(r))
752 return r;
753 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
754
755 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
756
757 return 0;
758}
759
760static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
761{
762 if (unlikely(!(l->info.cur_readers & tid_bit))) {
763 /* the thread is not owning the lock for read */
764 abort();
765 }
766
767 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
768
769 __RWLOCK_RDUNLOCK(&l->lock);
770
771 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
772}
773
774static inline void __spin_init(struct ha_spinlock *l)
775{
776 memset(l, 0, sizeof(struct ha_spinlock));
777 __SPIN_INIT(&l->lock);
778}
779
780static inline void __spin_destroy(struct ha_spinlock *l)
781{
782 __SPIN_DESTROY(&l->lock);
783 memset(l, 0, sizeof(struct ha_spinlock));
784}
785
786static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
787 const char *func, const char *file, int line)
788{
789 uint64_t start_time;
790
791 if (unlikely(l->info.owner & tid_bit)) {
792 /* the thread is already owning the lock */
793 abort();
794 }
795
796 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
797
798 start_time = nsec_now();
799 __SPIN_LOCK(&l->lock);
800 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
801
802 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
803
804
805 l->info.owner = tid_bit;
806 l->info.last_location.function = func;
807 l->info.last_location.file = file;
808 l->info.last_location.line = line;
809
810 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
811}
812
813static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
814 const char *func, const char *file, int line)
815{
816 int r;
817
818 if (unlikely(l->info.owner & tid_bit)) {
819 /* the thread is already owning the lock */
820 abort();
821 }
822
823 /* try read should never wait */
824 r = __SPIN_TRYLOCK(&l->lock);
825 if (unlikely(r))
826 return r;
827 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
828
829 l->info.owner = tid_bit;
830 l->info.last_location.function = func;
831 l->info.last_location.file = file;
832 l->info.last_location.line = line;
833
834 return 0;
835}
836
837static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
838 const char *func, const char *file, int line)
839{
840 if (unlikely(!(l->info.owner & tid_bit))) {
841 /* the thread is not owning the lock */
842 abort();
843 }
844
845 l->info.owner = 0;
846 l->info.last_location.function = func;
847 l->info.last_location.file = file;
848 l->info.last_location.line = line;
849
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100850 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200851 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
852}
853
854#else /* DEBUG_THREAD */
855
856#define HA_SPINLOCK_T unsigned long
857
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100858#define HA_SPIN_INIT(l) ({ (*l) = 0; })
859#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
860#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
861#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
862#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200863
864#define HA_RWLOCK_T unsigned long
865
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100866#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
867#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
868#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
869#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
870#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
871#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
872#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
873#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200874
875#endif /* DEBUG_THREAD */
876
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100877#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200878
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100879static __inline int
880__ha_cas_dw(void *target, void *compare, const void *set)
881{
882 char ret;
883
884 __asm __volatile("lock cmpxchg16b %0; setz %3"
885 : "+m" (*(void **)target),
886 "=a" (((void **)compare)[0]),
887 "=d" (((void **)compare)[1]),
888 "=q" (ret)
889 : "a" (((void **)compare)[0]),
890 "d" (((void **)compare)[1]),
891 "b" (((const void **)set)[0]),
892 "c" (((const void **)set)[1])
893 : "memory", "cc");
894 return (ret);
895}
896
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100897/* Use __ha_barrier_atomic* when you're trying to protect data that are
898 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
899 */
900static __inline void
901__ha_barrier_atomic_load(void)
902{
903 __asm __volatile("" ::: "memory");
904}
905
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100906static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100907__ha_barrier_atomic_store(void)
908{
909 __asm __volatile("" ::: "memory");
910}
911
912static __inline void
913__ha_barrier_atomic_full(void)
914{
915 __asm __volatile("" ::: "memory");
916}
917
918static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100919__ha_barrier_load(void)
920{
921 __asm __volatile("lfence" ::: "memory");
922}
923
924static __inline void
925__ha_barrier_store(void)
926{
927 __asm __volatile("sfence" ::: "memory");
928}
929
930static __inline void
931__ha_barrier_full(void)
932{
933 __asm __volatile("mfence" ::: "memory");
934}
935
936#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200937
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100938/* Use __ha_barrier_atomic* when you're trying to protect data that are
939 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
940 */
941static __inline void
942__ha_barrier_atomic_load(void)
943{
944 __asm __volatile("dmb" ::: "memory");
945}
946
947static __inline void
948__ha_barrier_atomic_store(void)
949{
950 __asm __volatile("dsb" ::: "memory");
951}
952
953static __inline void
954__ha_barrier_atomic_full(void)
955{
956 __asm __volatile("dmb" ::: "memory");
957}
958
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100959static __inline void
960__ha_barrier_load(void)
961{
962 __asm __volatile("dmb" ::: "memory");
963}
964
965static __inline void
966__ha_barrier_store(void)
967{
968 __asm __volatile("dsb" ::: "memory");
969}
970
971static __inline void
972__ha_barrier_full(void)
973{
974 __asm __volatile("dmb" ::: "memory");
975}
976
Willy Tarreau41ccb192018-02-14 14:16:28 +0100977static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100978{
979 uint64_t previous;
980 int tmp;
981
982 __asm __volatile("1:"
983 "ldrexd %0, [%4];"
984 "cmp %Q0, %Q2;"
985 "ittt eq;"
986 "cmpeq %R0, %R2;"
987 "strexdeq %1, %3, [%4];"
988 "cmpeq %1, #1;"
989 "beq 1b;"
990 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100991 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100992 : "memory", "cc");
993 tmp = (previous == *(uint64_t *)compare);
994 *(uint64_t *)compare = previous;
995 return (tmp);
996}
997
998#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100999
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001000/* Use __ha_barrier_atomic* when you're trying to protect data that are
1001 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1002 */
1003static __inline void
1004__ha_barrier_atomic_load(void)
1005{
1006 __asm __volatile("dmb ishld" ::: "memory");
1007}
1008
1009static __inline void
1010__ha_barrier_atomic_store(void)
1011{
1012 __asm __volatile("dmb ishst" ::: "memory");
1013}
1014
1015static __inline void
1016__ha_barrier_atomic_full(void)
1017{
1018 __asm __volatile("dmb ish" ::: "memory");
1019}
1020
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001021static __inline void
1022__ha_barrier_load(void)
1023{
1024 __asm __volatile("dmb ishld" ::: "memory");
1025}
1026
1027static __inline void
1028__ha_barrier_store(void)
1029{
1030 __asm __volatile("dmb ishst" ::: "memory");
1031}
1032
1033static __inline void
1034__ha_barrier_full(void)
1035{
1036 __asm __volatile("dmb ish" ::: "memory");
1037}
1038
1039static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1040{
1041 void *value[2];
1042 uint64_t tmp1, tmp2;
1043
1044 __asm__ __volatile__("1:"
1045 "ldxp %0, %1, [%4];"
1046 "mov %2, %0;"
1047 "mov %3, %1;"
1048 "eor %0, %0, %5;"
1049 "eor %1, %1, %6;"
1050 "orr %1, %0, %1;"
1051 "mov %w0, #0;"
1052 "cbnz %1, 2f;"
1053 "stxp %w0, %7, %8, [%4];"
1054 "cbnz %w0, 1b;"
1055 "mov %w0, #1;"
1056 "2:"
1057 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1058 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1059 : "cc", "memory");
1060
1061 memcpy(compare, &value, sizeof(value));
1062 return (tmp1);
1063}
1064
1065#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001066#define __ha_barrier_atomic_load __sync_synchronize
1067#define __ha_barrier_atomic_store __sync_synchronize
1068#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001069#define __ha_barrier_load __sync_synchronize
1070#define __ha_barrier_store __sync_synchronize
1071#define __ha_barrier_full __sync_synchronize
1072#endif
1073
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001074void ha_spin_init(HA_SPINLOCK_T *l);
1075void ha_rwlock_init(HA_RWLOCK_T *l);
1076
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001077#endif /* USE_THREAD */
1078
Willy Tarreau149ab772019-01-26 14:27:06 +01001079extern int thread_cpus_enabled_at_boot;
1080
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001081static inline void __ha_compiler_barrier(void)
1082{
1083 __asm __volatile("" ::: "memory");
1084}
1085
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001086int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001087int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001088
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001089#ifndef _HA_ATOMIC_CAS
1090#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1091#endif /* !_HA_ATOMIC_CAS */
1092
1093#ifndef _HA_ATOMIC_ADD
1094#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1095#endif /* !_HA_ATOMIC_ADD */
1096
1097#ifndef _HA_ATOMIC_XADD
1098#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1099#endif /* !_HA_ATOMIC_SUB */
1100
1101#ifndef _HA_ATOMIC_SUB
1102#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1103#endif /* !_HA_ATOMIC_SUB */
1104
1105#ifndef _HA_ATOMIC_AND
1106#define _HA_ATOMIC_AND HA_ATOMIC_AND
1107#endif /* !_HA_ATOMIC_AND */
1108
1109#ifndef _HA_ATOMIC_OR
1110#define _HA_ATOMIC_OR HA_ATOMIC_OR
1111#endif /* !_HA_ATOMIC_OR */
1112
1113#ifndef _HA_ATOMIC_XCHG
1114#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1115#endif /* !_HA_ATOMIC_XCHG */
1116
1117#ifndef _HA_ATOMIC_STORE
1118#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1119#endif /* !_HA_ATOMIC_STORE */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001120#endif /* _COMMON_HATHREADS_H */