blob: 8e96e37e837d45a4dca0cfd93b669497ef518ecb [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 :
29 * - with threads support disabled, this symbol is defined as zero (0UL).
30 * - with threads enabled, this variable is never zero, it contains the mask
31 * of enabled threads. Thus if only one thread is enabled, it equals 1.
32 */
33
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020034#ifndef USE_THREAD
35
Willy Tarreau421f02e2018-01-20 18:19:22 +010036#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020037#define MAX_THREADS_MASK 1
38
39/* Only way found to replace variables with constants that are optimized away
40 * at build time.
41 */
42enum { all_threads_mask = 1UL };
43enum { tid_bit = 1UL };
44enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010045
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010046#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010047#define __decl_spinlock(lock)
48#define __decl_aligned_spinlock(lock)
49#define __decl_rwlock(lock)
50#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010051
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020052#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
53#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
54#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020055#define HA_ATOMIC_XADD(val, i) \
56 ({ \
57 typeof((val)) __p_xadd = (val); \
58 typeof(*(val)) __old_xadd = *__p_xadd; \
59 *__p_xadd += i; \
60 __old_xadd; \
61 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020062#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
63#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
64#define HA_ATOMIC_XCHG(val, new) \
65 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020066 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020067 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020068 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020069 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010070#define HA_ATOMIC_BTS(val, bit) \
71 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020072 typeof((val)) __p_bts = (val); \
73 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
74 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
75 if (!__t_bts) \
76 *__p_bts |= __b_bts; \
77 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010078 })
79#define HA_ATOMIC_BTR(val, bit) \
80 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020081 typeof((val)) __p_btr = (val); \
82 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
83 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
84 if (__t_btr) \
85 *__p_btr &= ~__b_btr; \
86 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010087 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020088#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
89#define HA_ATOMIC_UPDATE_MAX(val, new) \
90 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020091 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020092 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020093 if (*(val) < __new_max) \
94 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020095 *(val); \
96 })
97
98#define HA_ATOMIC_UPDATE_MIN(val, new) \
99 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200100 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200101 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200102 if (*(val) > __new_min) \
103 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200104 *(val); \
105 })
106
Willy Tarreaub29dc952017-10-31 18:00:20 +0100107#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200108
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100109#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
110#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
111#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
112#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
113#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200114
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100115#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
116#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
117#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
118#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
119#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
120#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
121#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
122#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200123
William Lallemand6e1796e2018-06-07 11:23:40 +0200124#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
125
Willy Tarreau0c026f42018-08-01 19:12:20 +0200126static inline void ha_set_tid(unsigned int tid)
127{
128}
William Lallemand6e1796e2018-06-07 11:23:40 +0200129
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100130static inline void __ha_barrier_load(void)
131{
132}
133
134static inline void __ha_barrier_store(void)
135{
136}
137
138static inline void __ha_barrier_full(void)
139{
140}
141
Willy Tarreau60b639c2018-08-02 10:16:17 +0200142static inline void thread_harmless_now()
143{
144}
145
146static inline void thread_harmless_end()
147{
148}
149
150static inline void thread_isolate()
151{
152}
153
154static inline void thread_release()
155{
156}
157
158static inline unsigned long thread_isolated()
159{
160 return 1;
161}
162
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200163#else /* USE_THREAD */
164
165#include <stdio.h>
166#include <stdlib.h>
167#include <string.h>
168#include <pthread.h>
169#include <import/plock.h>
170
Willy Tarreau421f02e2018-01-20 18:19:22 +0100171#define MAX_THREADS LONGBITS
Willy Tarreau0c026f42018-08-01 19:12:20 +0200172#define MAX_THREADS_MASK ((unsigned long)-1)
Willy Tarreau421f02e2018-01-20 18:19:22 +0100173
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100174#define __decl_hathreads(decl) decl
175
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100176/* declare a self-initializing spinlock */
177#define __decl_spinlock(lock) \
178 HA_SPINLOCK_T (lock); \
179 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
180
181/* declare a self-initializing spinlock, aligned on a cache line */
182#define __decl_aligned_spinlock(lock) \
183 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
184 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
185
186/* declare a self-initializing rwlock */
187#define __decl_rwlock(lock) \
188 HA_RWLOCK_T (lock); \
189 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
190
191/* declare a self-initializing rwlock, aligned on a cache line */
192#define __decl_aligned_rwlock(lock) \
193 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
194 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
195
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200196/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
197 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100198
David Carlierec5e8452018-01-11 14:20:43 +0000199#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100200/* gcc < 4.7 */
201
202#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
203#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200204#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100205#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
206#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
207
208/* the CAS is a bit complicated. The older API doesn't support returning the
209 * value and the swap's result at the same time. So here we take what looks
210 * like the safest route, consisting in using the boolean version guaranteeing
211 * that the operation was performed or not, and we snoop a previous value. If
212 * the compare succeeds, we return. If it fails, we return the previous value,
213 * but only if it differs from the expected one. If it's the same it's a race
214 * thus we try again to avoid confusing a possibly sensitive caller.
215 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200216#define HA_ATOMIC_CAS(val, old, new) \
217 ({ \
218 typeof((val)) __val_cas = (val); \
219 typeof((old)) __oldp_cas = (old); \
220 typeof(*(old)) __oldv_cas; \
221 typeof((new)) __new_cas = (new); \
222 int __ret_cas; \
223 do { \
224 __oldv_cas = *__val_cas; \
225 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
226 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
227 if (!__ret_cas) \
228 *__oldp_cas = __oldv_cas; \
229 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100230 })
231
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200232#define HA_ATOMIC_XCHG(val, new) \
233 ({ \
234 typeof((val)) __val_xchg = (val); \
235 typeof(*(val)) __old_xchg; \
236 typeof((new)) __new_xchg = (new); \
237 do { __old_xchg = *__val_xchg; \
238 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
239 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100240 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100241
242#define HA_ATOMIC_BTS(val, bit) \
243 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200244 typeof(*(val)) __b_bts = (1UL << (bit)); \
245 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100246 })
247
248#define HA_ATOMIC_BTR(val, bit) \
249 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200250 typeof(*(val)) __b_btr = (1UL << (bit)); \
251 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100252 })
253
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200254#define HA_ATOMIC_STORE(val, new) \
255 ({ \
256 typeof((val)) __val_store = (val); \
257 typeof(*(val)) __old_store; \
258 typeof((new)) __new_store = (new); \
259 do { __old_store = *__val_store; \
260 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100261 })
262#else
263/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200264#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
265#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
Willy Tarreau9378df82018-09-05 16:11:03 +0200266#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, 0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200267#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
268#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
269#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100270#define HA_ATOMIC_BTS(val, bit) \
271 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200272 typeof(*(val)) __b_bts = (1UL << (bit)); \
273 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100274 })
275
276#define HA_ATOMIC_BTR(val, bit) \
277 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200278 typeof(*(val)) __b_btr = (1UL << (bit)); \
279 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100280 })
281
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200282#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
283#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100284#endif
285
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200286#define HA_ATOMIC_UPDATE_MAX(val, new) \
287 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200288 typeof(*(val)) __old_max = *(val); \
289 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200290 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200291 while (__old_max < __new_max && \
292 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
293 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200294 })
295#define HA_ATOMIC_UPDATE_MIN(val, new) \
296 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200297 typeof(*(val)) __old_min = *(val); \
298 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200299 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200300 while (__old_min > __new_min && \
301 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
302 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200303 })
304
Willy Tarreaub29dc952017-10-31 18:00:20 +0100305#define HA_BARRIER() pl_barrier()
306
Willy Tarreau60b639c2018-08-02 10:16:17 +0200307void thread_harmless_till_end();
308void thread_isolate();
309void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200310
Willy Tarreau0c026f42018-08-01 19:12:20 +0200311extern THREAD_LOCAL unsigned int tid; /* The thread id */
312extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200313extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200314extern volatile unsigned long threads_want_rdv_mask;
315extern volatile unsigned long threads_harmless_mask;
316
317/* explanation for threads_want_rdv_mask and threads_harmless_mask :
318 * - threads_want_rdv_mask is a bit field indicating all threads that have
319 * requested a rendez-vous of other threads using thread_isolate().
320 * - threads_harmless_mask is a bit field indicating all threads that are
321 * currently harmless in that they promise not to access a shared resource.
322 *
323 * For a given thread, its bits in want_rdv and harmless can be translated like
324 * this :
325 *
326 * ----------+----------+----------------------------------------------------
327 * want_rdv | harmless | description
328 * ----------+----------+----------------------------------------------------
329 * 0 | 0 | thread not interested in RDV, possibly harmful
330 * 0 | 1 | thread not interested in RDV but harmless
331 * 1 | 1 | thread interested in RDV and waiting for its turn
332 * 1 | 0 | thread currently working isolated from others
333 * ----------+----------+----------------------------------------------------
334 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200335
William Lallemand6e1796e2018-06-07 11:23:40 +0200336#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
337
Willy Tarreau0c026f42018-08-01 19:12:20 +0200338/* sets the thread ID and the TID bit for the current thread */
339static inline void ha_set_tid(unsigned int data)
340{
341 tid = data;
342 tid_bit = (1UL << tid);
343}
344
Willy Tarreau60b639c2018-08-02 10:16:17 +0200345/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
346 * not be touching any unprotected shared resource during this period. Usually
347 * this is called before poll(), but it may also be placed around very slow
348 * calls (eg: some crypto operations). Needs to be terminated using
349 * thread_harmless_end().
350 */
351static inline void thread_harmless_now()
352{
353 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
354}
355
356/* Ends the harmless period started by thread_harmless_now(). Usually this is
357 * placed after the poll() call. If it is discovered that a job was running and
358 * is relying on the thread still being harmless, the thread waits for the
359 * other one to finish.
360 */
361static inline void thread_harmless_end()
362{
363 while (1) {
364 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
365 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
366 break;
367 thread_harmless_till_end();
368 }
369}
370
371/* an isolated thread has harmless cleared and want_rdv set */
372static inline unsigned long thread_isolated()
373{
374 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
375}
376
William Lallemand6e1796e2018-06-07 11:23:40 +0200377
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200378#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
379
Christopher Fauletf51bac22018-01-30 11:04:29 +0100380/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200381enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200382 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200383 TASK_RQ_LOCK,
384 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200385 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200386 LISTENER_LOCK,
387 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200388 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200389 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200390 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200391 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200392 STK_TABLE_LOCK,
393 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200394 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200395 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200396 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200397 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200398 SSL_LOCK,
399 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200400 PATREF_LOCK,
401 PATEXP_LOCK,
402 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200403 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200404 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200405 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200406 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200407 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200408 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200409 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200410 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100411 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100412 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100413 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100414 AUTH_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200415 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200416};
417struct lock_stat {
418 uint64_t nsec_wait_for_write;
419 uint64_t nsec_wait_for_read;
420 uint64_t num_write_locked;
421 uint64_t num_write_unlocked;
422 uint64_t num_read_locked;
423 uint64_t num_read_unlocked;
424};
425
426extern struct lock_stat lock_stats[LOCK_LABELS];
427
428#define __HA_SPINLOCK_T unsigned long
429
430#define __SPIN_INIT(l) ({ (*l) = 0; })
431#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100432#define __SPIN_LOCK(l) pl_take_s(l)
433#define __SPIN_TRYLOCK(l) !pl_try_s(l)
434#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200435
436#define __HA_RWLOCK_T unsigned long
437
438#define __RWLOCK_INIT(l) ({ (*l) = 0; })
439#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
440#define __RWLOCK_WRLOCK(l) pl_take_w(l)
441#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
442#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
443#define __RWLOCK_RDLOCK(l) pl_take_r(l)
444#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
445#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
446
447#define HA_SPINLOCK_T struct ha_spinlock
448
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100449#define HA_SPIN_INIT(l) __spin_init(l)
450#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200451
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100452#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
453#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
454#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200455
456#define HA_RWLOCK_T struct ha_rwlock
457
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100458#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
459#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
460#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
461#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
462#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
463#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
464#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
465#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200466
467struct ha_spinlock {
468 __HA_SPINLOCK_T lock;
469 struct {
470 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
471 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
472 struct {
473 const char *function;
474 const char *file;
475 int line;
476 } last_location; /* location of the last owner */
477 } info;
478};
479
480struct ha_rwlock {
481 __HA_RWLOCK_T lock;
482 struct {
483 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
484 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
485 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
486 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
487 struct {
488 const char *function;
489 const char *file;
490 int line;
491 } last_location; /* location of the last write owner */
492 } info;
493};
494
Christopher Fauletf51bac22018-01-30 11:04:29 +0100495static inline const char *lock_label(enum lock_label label)
496{
497 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100498 case FD_LOCK: return "FD";
499 case TASK_RQ_LOCK: return "TASK_RQ";
500 case TASK_WQ_LOCK: return "TASK_WQ";
501 case POOL_LOCK: return "POOL";
502 case LISTENER_LOCK: return "LISTENER";
503 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
504 case PROXY_LOCK: return "PROXY";
505 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100506 case LBPRM_LOCK: return "LBPRM";
507 case SIGNALS_LOCK: return "SIGNALS";
508 case STK_TABLE_LOCK: return "STK_TABLE";
509 case STK_SESS_LOCK: return "STK_SESS";
510 case APPLETS_LOCK: return "APPLETS";
511 case PEER_LOCK: return "PEER";
512 case BUF_WQ_LOCK: return "BUF_WQ";
513 case STRMS_LOCK: return "STRMS";
514 case SSL_LOCK: return "SSL";
515 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
516 case PATREF_LOCK: return "PATREF";
517 case PATEXP_LOCK: return "PATEXP";
518 case PATLRU_LOCK: return "PATLRU";
519 case VARS_LOCK: return "VARS";
520 case COMP_POOL_LOCK: return "COMP_POOL";
521 case LUA_LOCK: return "LUA";
522 case NOTIF_LOCK: return "NOTIF";
523 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
524 case DNS_LOCK: return "DNS";
525 case PID_LIST_LOCK: return "PID_LIST";
526 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
527 case PIPES_LOCK: return "PIPES";
528 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100529 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100530 case AUTH_LOCK: return "AUTH";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100531 case LOCK_LABELS: break; /* keep compiler happy */
532 };
533 /* only way to come here is consecutive to an internal bug */
534 abort();
535}
536
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200537static inline void show_lock_stats()
538{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200539 int lbl;
540
541 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
542 fprintf(stderr,
543 "Stats about Lock %s: \n"
544 "\t # write lock : %lu\n"
545 "\t # write unlock: %lu (%ld)\n"
546 "\t # wait time for write : %.3f msec\n"
547 "\t # wait time for write/lock: %.3f nsec\n"
548 "\t # read lock : %lu\n"
549 "\t # read unlock : %lu (%ld)\n"
550 "\t # wait time for read : %.3f msec\n"
551 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100552 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200553 lock_stats[lbl].num_write_locked,
554 lock_stats[lbl].num_write_unlocked,
555 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
556 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
557 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
558 lock_stats[lbl].num_read_locked,
559 lock_stats[lbl].num_read_unlocked,
560 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
561 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
562 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
563 }
564}
565
566/* Following functions are used to collect some stats about locks. We wrap
567 * pthread functions to known how much time we wait in a lock. */
568
569static uint64_t nsec_now(void) {
570 struct timespec ts;
571
572 clock_gettime(CLOCK_MONOTONIC, &ts);
573 return ((uint64_t) ts.tv_sec * 1000000000ULL +
574 (uint64_t) ts.tv_nsec);
575}
576
577static inline void __ha_rwlock_init(struct ha_rwlock *l)
578{
579 memset(l, 0, sizeof(struct ha_rwlock));
580 __RWLOCK_INIT(&l->lock);
581}
582
583static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
584{
585 __RWLOCK_DESTROY(&l->lock);
586 memset(l, 0, sizeof(struct ha_rwlock));
587}
588
589
590static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
591 const char *func, const char *file, int line)
592{
593 uint64_t start_time;
594
595 if (unlikely(l->info.cur_writer & tid_bit)) {
596 /* the thread is already owning the lock for write */
597 abort();
598 }
599
600 if (unlikely(l->info.cur_readers & tid_bit)) {
601 /* the thread is already owning the lock for read */
602 abort();
603 }
604
605 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
606
607 start_time = nsec_now();
608 __RWLOCK_WRLOCK(&l->lock);
609 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
610
611 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
612
613 l->info.cur_writer = tid_bit;
614 l->info.last_location.function = func;
615 l->info.last_location.file = file;
616 l->info.last_location.line = line;
617
618 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
619}
620
621static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
622 const char *func, const char *file, int line)
623{
624 uint64_t start_time;
625 int r;
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 /* We set waiting writer because trywrlock could wait for readers to quit */
638 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
639
640 start_time = nsec_now();
641 r = __RWLOCK_TRYWRLOCK(&l->lock);
642 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
643 if (unlikely(r)) {
644 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
645 return r;
646 }
647 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
648
649 l->info.cur_writer = tid_bit;
650 l->info.last_location.function = func;
651 l->info.last_location.file = file;
652 l->info.last_location.line = line;
653
654 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
655
656 return 0;
657}
658
659static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
660 const char *func, const char *file, int line)
661{
662 if (unlikely(!(l->info.cur_writer & tid_bit))) {
663 /* the thread is not owning the lock for write */
664 abort();
665 }
666
667 l->info.cur_writer = 0;
668 l->info.last_location.function = func;
669 l->info.last_location.file = file;
670 l->info.last_location.line = line;
671
672 __RWLOCK_WRUNLOCK(&l->lock);
673
674 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
675}
676
677static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
678{
679 uint64_t start_time;
680
681 if (unlikely(l->info.cur_writer & tid_bit)) {
682 /* the thread is already owning the lock for write */
683 abort();
684 }
685
686 if (unlikely(l->info.cur_readers & tid_bit)) {
687 /* the thread is already owning the lock for read */
688 abort();
689 }
690
691 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
692
693 start_time = nsec_now();
694 __RWLOCK_RDLOCK(&l->lock);
695 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
696 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
697
698 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
699
700 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
701}
702
703static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
704{
705 int r;
706
707 if (unlikely(l->info.cur_writer & tid_bit)) {
708 /* the thread is already owning the lock for write */
709 abort();
710 }
711
712 if (unlikely(l->info.cur_readers & tid_bit)) {
713 /* the thread is already owning the lock for read */
714 abort();
715 }
716
717 /* try read should never wait */
718 r = __RWLOCK_TRYRDLOCK(&l->lock);
719 if (unlikely(r))
720 return r;
721 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
722
723 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
724
725 return 0;
726}
727
728static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
729{
730 if (unlikely(!(l->info.cur_readers & tid_bit))) {
731 /* the thread is not owning the lock for read */
732 abort();
733 }
734
735 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
736
737 __RWLOCK_RDUNLOCK(&l->lock);
738
739 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
740}
741
742static inline void __spin_init(struct ha_spinlock *l)
743{
744 memset(l, 0, sizeof(struct ha_spinlock));
745 __SPIN_INIT(&l->lock);
746}
747
748static inline void __spin_destroy(struct ha_spinlock *l)
749{
750 __SPIN_DESTROY(&l->lock);
751 memset(l, 0, sizeof(struct ha_spinlock));
752}
753
754static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
755 const char *func, const char *file, int line)
756{
757 uint64_t start_time;
758
759 if (unlikely(l->info.owner & tid_bit)) {
760 /* the thread is already owning the lock */
761 abort();
762 }
763
764 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
765
766 start_time = nsec_now();
767 __SPIN_LOCK(&l->lock);
768 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
769
770 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
771
772
773 l->info.owner = tid_bit;
774 l->info.last_location.function = func;
775 l->info.last_location.file = file;
776 l->info.last_location.line = line;
777
778 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
779}
780
781static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
782 const char *func, const char *file, int line)
783{
784 int r;
785
786 if (unlikely(l->info.owner & tid_bit)) {
787 /* the thread is already owning the lock */
788 abort();
789 }
790
791 /* try read should never wait */
792 r = __SPIN_TRYLOCK(&l->lock);
793 if (unlikely(r))
794 return r;
795 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
796
797 l->info.owner = tid_bit;
798 l->info.last_location.function = func;
799 l->info.last_location.file = file;
800 l->info.last_location.line = line;
801
802 return 0;
803}
804
805static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
806 const char *func, const char *file, int line)
807{
808 if (unlikely(!(l->info.owner & tid_bit))) {
809 /* the thread is not owning the lock */
810 abort();
811 }
812
813 l->info.owner = 0;
814 l->info.last_location.function = func;
815 l->info.last_location.file = file;
816 l->info.last_location.line = line;
817
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100818 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200819 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
820}
821
822#else /* DEBUG_THREAD */
823
824#define HA_SPINLOCK_T unsigned long
825
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100826#define HA_SPIN_INIT(l) ({ (*l) = 0; })
827#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
828#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
829#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
830#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200831
832#define HA_RWLOCK_T unsigned long
833
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100834#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
835#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
836#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
837#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
838#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
839#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
840#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
841#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200842
843#endif /* DEBUG_THREAD */
844
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100845#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200846
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100847static __inline int
848__ha_cas_dw(void *target, void *compare, const void *set)
849{
850 char ret;
851
852 __asm __volatile("lock cmpxchg16b %0; setz %3"
853 : "+m" (*(void **)target),
854 "=a" (((void **)compare)[0]),
855 "=d" (((void **)compare)[1]),
856 "=q" (ret)
857 : "a" (((void **)compare)[0]),
858 "d" (((void **)compare)[1]),
859 "b" (((const void **)set)[0]),
860 "c" (((const void **)set)[1])
861 : "memory", "cc");
862 return (ret);
863}
864
865static __inline void
866__ha_barrier_load(void)
867{
868 __asm __volatile("lfence" ::: "memory");
869}
870
871static __inline void
872__ha_barrier_store(void)
873{
874 __asm __volatile("sfence" ::: "memory");
875}
876
877static __inline void
878__ha_barrier_full(void)
879{
880 __asm __volatile("mfence" ::: "memory");
881}
882
883#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200884
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100885static __inline void
886__ha_barrier_load(void)
887{
888 __asm __volatile("dmb" ::: "memory");
889}
890
891static __inline void
892__ha_barrier_store(void)
893{
894 __asm __volatile("dsb" ::: "memory");
895}
896
897static __inline void
898__ha_barrier_full(void)
899{
900 __asm __volatile("dmb" ::: "memory");
901}
902
Willy Tarreau41ccb192018-02-14 14:16:28 +0100903static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100904{
905 uint64_t previous;
906 int tmp;
907
908 __asm __volatile("1:"
909 "ldrexd %0, [%4];"
910 "cmp %Q0, %Q2;"
911 "ittt eq;"
912 "cmpeq %R0, %R2;"
913 "strexdeq %1, %3, [%4];"
914 "cmpeq %1, #1;"
915 "beq 1b;"
916 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100917 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100918 : "memory", "cc");
919 tmp = (previous == *(uint64_t *)compare);
920 *(uint64_t *)compare = previous;
921 return (tmp);
922}
923
924#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100925
926static __inline void
927__ha_barrier_load(void)
928{
929 __asm __volatile("dmb ishld" ::: "memory");
930}
931
932static __inline void
933__ha_barrier_store(void)
934{
935 __asm __volatile("dmb ishst" ::: "memory");
936}
937
938static __inline void
939__ha_barrier_full(void)
940{
941 __asm __volatile("dmb ish" ::: "memory");
942}
943
944static __inline int __ha_cas_dw(void *target, void *compare, void *set)
945{
946 void *value[2];
947 uint64_t tmp1, tmp2;
948
949 __asm__ __volatile__("1:"
950 "ldxp %0, %1, [%4];"
951 "mov %2, %0;"
952 "mov %3, %1;"
953 "eor %0, %0, %5;"
954 "eor %1, %1, %6;"
955 "orr %1, %0, %1;"
956 "mov %w0, #0;"
957 "cbnz %1, 2f;"
958 "stxp %w0, %7, %8, [%4];"
959 "cbnz %w0, 1b;"
960 "mov %w0, #1;"
961 "2:"
962 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
963 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
964 : "cc", "memory");
965
966 memcpy(compare, &value, sizeof(value));
967 return (tmp1);
968}
969
970#else
971#define __ha_barrier_load __sync_synchronize
972#define __ha_barrier_store __sync_synchronize
973#define __ha_barrier_full __sync_synchronize
974#endif
975
Willy Tarreaua8ae77d2018-11-25 19:28:23 +0100976void ha_spin_init(HA_SPINLOCK_T *l);
977void ha_rwlock_init(HA_RWLOCK_T *l);
978
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200979#endif /* USE_THREAD */
980
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100981static inline void __ha_compiler_barrier(void)
982{
983 __asm __volatile("" ::: "memory");
984}
985
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200986int parse_nbthread(const char *arg, char **err);
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200987
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200988#endif /* _COMMON_HATHREADS_H */