blob: a3b696439c9e877266924c1b4d40b91f72e864ae [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)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100300#endif
301
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200302#define HA_ATOMIC_UPDATE_MAX(val, new) \
303 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200304 typeof(*(val)) __old_max = *(val); \
305 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200306 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200307 while (__old_max < __new_max && \
308 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
309 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200310 })
311#define HA_ATOMIC_UPDATE_MIN(val, new) \
312 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200313 typeof(*(val)) __old_min = *(val); \
314 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200315 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200316 while (__old_min > __new_min && \
317 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
318 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200319 })
320
Willy Tarreaub29dc952017-10-31 18:00:20 +0100321#define HA_BARRIER() pl_barrier()
322
Olivier Houchardd0c3b882019-03-07 18:55:31 +0100323/* Variants that don't generate any memory barrier.
324 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
325 * that will always generate correct code.
326 * Usually it's fine to use those when updating data that have no dependency,
327 * ie updating a counter. Otherwise a barrier is required.
328 */
329#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
330#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
331#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
332#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
333#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
334#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
335#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
336#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Willy Tarreau60b639c2018-08-02 10:16:17 +0200337void thread_harmless_till_end();
338void thread_isolate();
339void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200340
Willy Tarreau0c026f42018-08-01 19:12:20 +0200341extern THREAD_LOCAL unsigned int tid; /* The thread id */
342extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200343extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200344extern volatile unsigned long threads_want_rdv_mask;
345extern volatile unsigned long threads_harmless_mask;
346
347/* explanation for threads_want_rdv_mask and threads_harmless_mask :
348 * - threads_want_rdv_mask is a bit field indicating all threads that have
349 * requested a rendez-vous of other threads using thread_isolate().
350 * - threads_harmless_mask is a bit field indicating all threads that are
351 * currently harmless in that they promise not to access a shared resource.
352 *
353 * For a given thread, its bits in want_rdv and harmless can be translated like
354 * this :
355 *
356 * ----------+----------+----------------------------------------------------
357 * want_rdv | harmless | description
358 * ----------+----------+----------------------------------------------------
359 * 0 | 0 | thread not interested in RDV, possibly harmful
360 * 0 | 1 | thread not interested in RDV but harmless
361 * 1 | 1 | thread interested in RDV and waiting for its turn
362 * 1 | 0 | thread currently working isolated from others
363 * ----------+----------+----------------------------------------------------
364 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200365
William Lallemand6e1796e2018-06-07 11:23:40 +0200366#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
367
Willy Tarreau0c026f42018-08-01 19:12:20 +0200368/* sets the thread ID and the TID bit for the current thread */
369static inline void ha_set_tid(unsigned int data)
370{
371 tid = data;
372 tid_bit = (1UL << tid);
373}
374
Willy Tarreau60b639c2018-08-02 10:16:17 +0200375/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
376 * not be touching any unprotected shared resource during this period. Usually
377 * this is called before poll(), but it may also be placed around very slow
378 * calls (eg: some crypto operations). Needs to be terminated using
379 * thread_harmless_end().
380 */
381static inline void thread_harmless_now()
382{
383 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
384}
385
386/* Ends the harmless period started by thread_harmless_now(). Usually this is
387 * placed after the poll() call. If it is discovered that a job was running and
388 * is relying on the thread still being harmless, the thread waits for the
389 * other one to finish.
390 */
391static inline void thread_harmless_end()
392{
393 while (1) {
394 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
395 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
396 break;
397 thread_harmless_till_end();
398 }
399}
400
401/* an isolated thread has harmless cleared and want_rdv set */
402static inline unsigned long thread_isolated()
403{
404 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
405}
406
William Lallemand6e1796e2018-06-07 11:23:40 +0200407
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200408#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
409
Christopher Fauletf51bac22018-01-30 11:04:29 +0100410/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200411enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200412 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200413 TASK_RQ_LOCK,
414 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200415 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200416 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200417 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200418 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200419 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200420 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200421 STK_TABLE_LOCK,
422 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200423 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200424 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200425 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200426 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200427 SSL_LOCK,
428 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200429 PATREF_LOCK,
430 PATEXP_LOCK,
431 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200432 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200433 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200434 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200435 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200436 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200437 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200438 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200439 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100440 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100441 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100442 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100443 AUTH_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000444 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200445 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200446};
447struct lock_stat {
448 uint64_t nsec_wait_for_write;
449 uint64_t nsec_wait_for_read;
450 uint64_t num_write_locked;
451 uint64_t num_write_unlocked;
452 uint64_t num_read_locked;
453 uint64_t num_read_unlocked;
454};
455
456extern struct lock_stat lock_stats[LOCK_LABELS];
457
458#define __HA_SPINLOCK_T unsigned long
459
460#define __SPIN_INIT(l) ({ (*l) = 0; })
461#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100462#define __SPIN_LOCK(l) pl_take_s(l)
463#define __SPIN_TRYLOCK(l) !pl_try_s(l)
464#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200465
466#define __HA_RWLOCK_T unsigned long
467
468#define __RWLOCK_INIT(l) ({ (*l) = 0; })
469#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
470#define __RWLOCK_WRLOCK(l) pl_take_w(l)
471#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
472#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
473#define __RWLOCK_RDLOCK(l) pl_take_r(l)
474#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
475#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
476
477#define HA_SPINLOCK_T struct ha_spinlock
478
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100479#define HA_SPIN_INIT(l) __spin_init(l)
480#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200481
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100482#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
483#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
484#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200485
486#define HA_RWLOCK_T struct ha_rwlock
487
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100488#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
489#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
490#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
491#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
492#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
493#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
494#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
495#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200496
497struct ha_spinlock {
498 __HA_SPINLOCK_T lock;
499 struct {
500 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
501 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
502 struct {
503 const char *function;
504 const char *file;
505 int line;
506 } last_location; /* location of the last owner */
507 } info;
508};
509
510struct ha_rwlock {
511 __HA_RWLOCK_T lock;
512 struct {
513 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
514 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
515 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
516 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
517 struct {
518 const char *function;
519 const char *file;
520 int line;
521 } last_location; /* location of the last write owner */
522 } info;
523};
524
Christopher Fauletf51bac22018-01-30 11:04:29 +0100525static inline const char *lock_label(enum lock_label label)
526{
527 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100528 case FD_LOCK: return "FD";
529 case TASK_RQ_LOCK: return "TASK_RQ";
530 case TASK_WQ_LOCK: return "TASK_WQ";
531 case POOL_LOCK: return "POOL";
532 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100533 case PROXY_LOCK: return "PROXY";
534 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100535 case LBPRM_LOCK: return "LBPRM";
536 case SIGNALS_LOCK: return "SIGNALS";
537 case STK_TABLE_LOCK: return "STK_TABLE";
538 case STK_SESS_LOCK: return "STK_SESS";
539 case APPLETS_LOCK: return "APPLETS";
540 case PEER_LOCK: return "PEER";
541 case BUF_WQ_LOCK: return "BUF_WQ";
542 case STRMS_LOCK: return "STRMS";
543 case SSL_LOCK: return "SSL";
544 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
545 case PATREF_LOCK: return "PATREF";
546 case PATEXP_LOCK: return "PATEXP";
547 case PATLRU_LOCK: return "PATLRU";
548 case VARS_LOCK: return "VARS";
549 case COMP_POOL_LOCK: return "COMP_POOL";
550 case LUA_LOCK: return "LUA";
551 case NOTIF_LOCK: return "NOTIF";
552 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
553 case DNS_LOCK: return "DNS";
554 case PID_LIST_LOCK: return "PID_LIST";
555 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
556 case PIPES_LOCK: return "PIPES";
557 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100558 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100559 case AUTH_LOCK: return "AUTH";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000560 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100561 case LOCK_LABELS: break; /* keep compiler happy */
562 };
563 /* only way to come here is consecutive to an internal bug */
564 abort();
565}
566
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200567static inline void show_lock_stats()
568{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200569 int lbl;
570
571 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
572 fprintf(stderr,
573 "Stats about Lock %s: \n"
574 "\t # write lock : %lu\n"
575 "\t # write unlock: %lu (%ld)\n"
576 "\t # wait time for write : %.3f msec\n"
577 "\t # wait time for write/lock: %.3f nsec\n"
578 "\t # read lock : %lu\n"
579 "\t # read unlock : %lu (%ld)\n"
580 "\t # wait time for read : %.3f msec\n"
581 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100582 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200583 lock_stats[lbl].num_write_locked,
584 lock_stats[lbl].num_write_unlocked,
585 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
586 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
587 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
588 lock_stats[lbl].num_read_locked,
589 lock_stats[lbl].num_read_unlocked,
590 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
591 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
592 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
593 }
594}
595
596/* Following functions are used to collect some stats about locks. We wrap
597 * pthread functions to known how much time we wait in a lock. */
598
599static uint64_t nsec_now(void) {
600 struct timespec ts;
601
602 clock_gettime(CLOCK_MONOTONIC, &ts);
603 return ((uint64_t) ts.tv_sec * 1000000000ULL +
604 (uint64_t) ts.tv_nsec);
605}
606
607static inline void __ha_rwlock_init(struct ha_rwlock *l)
608{
609 memset(l, 0, sizeof(struct ha_rwlock));
610 __RWLOCK_INIT(&l->lock);
611}
612
613static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
614{
615 __RWLOCK_DESTROY(&l->lock);
616 memset(l, 0, sizeof(struct ha_rwlock));
617}
618
619
620static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
621 const char *func, const char *file, int line)
622{
623 uint64_t start_time;
624
625 if (unlikely(l->info.cur_writer & tid_bit)) {
626 /* the thread is already owning the lock for write */
627 abort();
628 }
629
630 if (unlikely(l->info.cur_readers & tid_bit)) {
631 /* the thread is already owning the lock for read */
632 abort();
633 }
634
635 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
636
637 start_time = nsec_now();
638 __RWLOCK_WRLOCK(&l->lock);
639 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
640
641 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
642
643 l->info.cur_writer = tid_bit;
644 l->info.last_location.function = func;
645 l->info.last_location.file = file;
646 l->info.last_location.line = line;
647
648 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
649}
650
651static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
652 const char *func, const char *file, int line)
653{
654 uint64_t start_time;
655 int r;
656
657 if (unlikely(l->info.cur_writer & tid_bit)) {
658 /* the thread is already owning the lock for write */
659 abort();
660 }
661
662 if (unlikely(l->info.cur_readers & tid_bit)) {
663 /* the thread is already owning the lock for read */
664 abort();
665 }
666
667 /* We set waiting writer because trywrlock could wait for readers to quit */
668 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
669
670 start_time = nsec_now();
671 r = __RWLOCK_TRYWRLOCK(&l->lock);
672 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
673 if (unlikely(r)) {
674 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
675 return r;
676 }
677 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
678
679 l->info.cur_writer = tid_bit;
680 l->info.last_location.function = func;
681 l->info.last_location.file = file;
682 l->info.last_location.line = line;
683
684 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
685
686 return 0;
687}
688
689static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
690 const char *func, const char *file, int line)
691{
692 if (unlikely(!(l->info.cur_writer & tid_bit))) {
693 /* the thread is not owning the lock for write */
694 abort();
695 }
696
697 l->info.cur_writer = 0;
698 l->info.last_location.function = func;
699 l->info.last_location.file = file;
700 l->info.last_location.line = line;
701
702 __RWLOCK_WRUNLOCK(&l->lock);
703
704 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
705}
706
707static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
708{
709 uint64_t start_time;
710
711 if (unlikely(l->info.cur_writer & tid_bit)) {
712 /* the thread is already owning the lock for write */
713 abort();
714 }
715
716 if (unlikely(l->info.cur_readers & tid_bit)) {
717 /* the thread is already owning the lock for read */
718 abort();
719 }
720
721 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
722
723 start_time = nsec_now();
724 __RWLOCK_RDLOCK(&l->lock);
725 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
726 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
727
728 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
729
730 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
731}
732
733static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
734{
735 int r;
736
737 if (unlikely(l->info.cur_writer & tid_bit)) {
738 /* the thread is already owning the lock for write */
739 abort();
740 }
741
742 if (unlikely(l->info.cur_readers & tid_bit)) {
743 /* the thread is already owning the lock for read */
744 abort();
745 }
746
747 /* try read should never wait */
748 r = __RWLOCK_TRYRDLOCK(&l->lock);
749 if (unlikely(r))
750 return r;
751 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
752
753 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
754
755 return 0;
756}
757
758static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
759{
760 if (unlikely(!(l->info.cur_readers & tid_bit))) {
761 /* the thread is not owning the lock for read */
762 abort();
763 }
764
765 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
766
767 __RWLOCK_RDUNLOCK(&l->lock);
768
769 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
770}
771
772static inline void __spin_init(struct ha_spinlock *l)
773{
774 memset(l, 0, sizeof(struct ha_spinlock));
775 __SPIN_INIT(&l->lock);
776}
777
778static inline void __spin_destroy(struct ha_spinlock *l)
779{
780 __SPIN_DESTROY(&l->lock);
781 memset(l, 0, sizeof(struct ha_spinlock));
782}
783
784static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
785 const char *func, const char *file, int line)
786{
787 uint64_t start_time;
788
789 if (unlikely(l->info.owner & tid_bit)) {
790 /* the thread is already owning the lock */
791 abort();
792 }
793
794 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
795
796 start_time = nsec_now();
797 __SPIN_LOCK(&l->lock);
798 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
799
800 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
801
802
803 l->info.owner = tid_bit;
804 l->info.last_location.function = func;
805 l->info.last_location.file = file;
806 l->info.last_location.line = line;
807
808 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
809}
810
811static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
812 const char *func, const char *file, int line)
813{
814 int r;
815
816 if (unlikely(l->info.owner & tid_bit)) {
817 /* the thread is already owning the lock */
818 abort();
819 }
820
821 /* try read should never wait */
822 r = __SPIN_TRYLOCK(&l->lock);
823 if (unlikely(r))
824 return r;
825 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
826
827 l->info.owner = tid_bit;
828 l->info.last_location.function = func;
829 l->info.last_location.file = file;
830 l->info.last_location.line = line;
831
832 return 0;
833}
834
835static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
836 const char *func, const char *file, int line)
837{
838 if (unlikely(!(l->info.owner & tid_bit))) {
839 /* the thread is not owning the lock */
840 abort();
841 }
842
843 l->info.owner = 0;
844 l->info.last_location.function = func;
845 l->info.last_location.file = file;
846 l->info.last_location.line = line;
847
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100848 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200849 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
850}
851
852#else /* DEBUG_THREAD */
853
854#define HA_SPINLOCK_T unsigned long
855
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100856#define HA_SPIN_INIT(l) ({ (*l) = 0; })
857#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
858#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
859#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
860#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200861
862#define HA_RWLOCK_T unsigned long
863
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100864#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
865#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
866#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
867#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
868#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
869#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
870#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
871#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200872
873#endif /* DEBUG_THREAD */
874
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100875#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200876
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100877static __inline int
878__ha_cas_dw(void *target, void *compare, const void *set)
879{
880 char ret;
881
882 __asm __volatile("lock cmpxchg16b %0; setz %3"
883 : "+m" (*(void **)target),
884 "=a" (((void **)compare)[0]),
885 "=d" (((void **)compare)[1]),
886 "=q" (ret)
887 : "a" (((void **)compare)[0]),
888 "d" (((void **)compare)[1]),
889 "b" (((const void **)set)[0]),
890 "c" (((const void **)set)[1])
891 : "memory", "cc");
892 return (ret);
893}
894
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100895/* Use __ha_barrier_atomic* when you're trying to protect data that are
896 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
897 */
898static __inline void
899__ha_barrier_atomic_load(void)
900{
901 __asm __volatile("" ::: "memory");
902}
903
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100904static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100905__ha_barrier_atomic_store(void)
906{
907 __asm __volatile("" ::: "memory");
908}
909
910static __inline void
911__ha_barrier_atomic_full(void)
912{
913 __asm __volatile("" ::: "memory");
914}
915
916static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100917__ha_barrier_load(void)
918{
919 __asm __volatile("lfence" ::: "memory");
920}
921
922static __inline void
923__ha_barrier_store(void)
924{
925 __asm __volatile("sfence" ::: "memory");
926}
927
928static __inline void
929__ha_barrier_full(void)
930{
931 __asm __volatile("mfence" ::: "memory");
932}
933
934#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200935
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100936/* Use __ha_barrier_atomic* when you're trying to protect data that are
937 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
938 */
939static __inline void
940__ha_barrier_atomic_load(void)
941{
942 __asm __volatile("dmb" ::: "memory");
943}
944
945static __inline void
946__ha_barrier_atomic_store(void)
947{
948 __asm __volatile("dsb" ::: "memory");
949}
950
951static __inline void
952__ha_barrier_atomic_full(void)
953{
954 __asm __volatile("dmb" ::: "memory");
955}
956
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100957static __inline void
958__ha_barrier_load(void)
959{
960 __asm __volatile("dmb" ::: "memory");
961}
962
963static __inline void
964__ha_barrier_store(void)
965{
966 __asm __volatile("dsb" ::: "memory");
967}
968
969static __inline void
970__ha_barrier_full(void)
971{
972 __asm __volatile("dmb" ::: "memory");
973}
974
Willy Tarreau41ccb192018-02-14 14:16:28 +0100975static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100976{
977 uint64_t previous;
978 int tmp;
979
980 __asm __volatile("1:"
981 "ldrexd %0, [%4];"
982 "cmp %Q0, %Q2;"
983 "ittt eq;"
984 "cmpeq %R0, %R2;"
985 "strexdeq %1, %3, [%4];"
986 "cmpeq %1, #1;"
987 "beq 1b;"
988 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100989 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100990 : "memory", "cc");
991 tmp = (previous == *(uint64_t *)compare);
992 *(uint64_t *)compare = previous;
993 return (tmp);
994}
995
996#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100997
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100998/* Use __ha_barrier_atomic* when you're trying to protect data that are
999 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1000 */
1001static __inline void
1002__ha_barrier_atomic_load(void)
1003{
1004 __asm __volatile("dmb ishld" ::: "memory");
1005}
1006
1007static __inline void
1008__ha_barrier_atomic_store(void)
1009{
1010 __asm __volatile("dmb ishst" ::: "memory");
1011}
1012
1013static __inline void
1014__ha_barrier_atomic_full(void)
1015{
1016 __asm __volatile("dmb ish" ::: "memory");
1017}
1018
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001019static __inline void
1020__ha_barrier_load(void)
1021{
1022 __asm __volatile("dmb ishld" ::: "memory");
1023}
1024
1025static __inline void
1026__ha_barrier_store(void)
1027{
1028 __asm __volatile("dmb ishst" ::: "memory");
1029}
1030
1031static __inline void
1032__ha_barrier_full(void)
1033{
1034 __asm __volatile("dmb ish" ::: "memory");
1035}
1036
1037static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1038{
1039 void *value[2];
1040 uint64_t tmp1, tmp2;
1041
1042 __asm__ __volatile__("1:"
1043 "ldxp %0, %1, [%4];"
1044 "mov %2, %0;"
1045 "mov %3, %1;"
1046 "eor %0, %0, %5;"
1047 "eor %1, %1, %6;"
1048 "orr %1, %0, %1;"
1049 "mov %w0, #0;"
1050 "cbnz %1, 2f;"
1051 "stxp %w0, %7, %8, [%4];"
1052 "cbnz %w0, 1b;"
1053 "mov %w0, #1;"
1054 "2:"
1055 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1056 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1057 : "cc", "memory");
1058
1059 memcpy(compare, &value, sizeof(value));
1060 return (tmp1);
1061}
1062
1063#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001064#define __ha_barrier_atomic_load __sync_synchronize
1065#define __ha_barrier_atomic_store __sync_synchronize
1066#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001067#define __ha_barrier_load __sync_synchronize
1068#define __ha_barrier_store __sync_synchronize
1069#define __ha_barrier_full __sync_synchronize
1070#endif
1071
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001072void ha_spin_init(HA_SPINLOCK_T *l);
1073void ha_rwlock_init(HA_RWLOCK_T *l);
1074
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001075#endif /* USE_THREAD */
1076
Willy Tarreau149ab772019-01-26 14:27:06 +01001077extern int thread_cpus_enabled_at_boot;
1078
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001079static inline void __ha_compiler_barrier(void)
1080{
1081 __asm __volatile("" ::: "memory");
1082}
1083
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001084int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001085int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001086
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001087#ifndef _HA_ATOMIC_CAS
1088#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1089#endif /* !_HA_ATOMIC_CAS */
1090
1091#ifndef _HA_ATOMIC_ADD
1092#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1093#endif /* !_HA_ATOMIC_ADD */
1094
1095#ifndef _HA_ATOMIC_XADD
1096#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1097#endif /* !_HA_ATOMIC_SUB */
1098
1099#ifndef _HA_ATOMIC_SUB
1100#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1101#endif /* !_HA_ATOMIC_SUB */
1102
1103#ifndef _HA_ATOMIC_AND
1104#define _HA_ATOMIC_AND HA_ATOMIC_AND
1105#endif /* !_HA_ATOMIC_AND */
1106
1107#ifndef _HA_ATOMIC_OR
1108#define _HA_ATOMIC_OR HA_ATOMIC_OR
1109#endif /* !_HA_ATOMIC_OR */
1110
1111#ifndef _HA_ATOMIC_XCHG
1112#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1113#endif /* !_HA_ATOMIC_XCHG */
1114
1115#ifndef _HA_ATOMIC_STORE
1116#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1117#endif /* !_HA_ATOMIC_STORE */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001118#endif /* _COMMON_HATHREADS_H */