blob: 77d5c6f380d8f9ca6d1f84aeb8a68885810c3be8 [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 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200280#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
281#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
Willy Tarreau9378df82018-09-05 16:11:03 +0200282#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, 0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200283#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
284#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
285#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
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
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200298#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
299#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
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
Willy Tarreau60b639c2018-08-02 10:16:17 +0200323void thread_harmless_till_end();
324void thread_isolate();
325void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200326
Willy Tarreau0c026f42018-08-01 19:12:20 +0200327extern THREAD_LOCAL unsigned int tid; /* The thread id */
328extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200329extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200330extern volatile unsigned long threads_want_rdv_mask;
331extern volatile unsigned long threads_harmless_mask;
332
333/* explanation for threads_want_rdv_mask and threads_harmless_mask :
334 * - threads_want_rdv_mask is a bit field indicating all threads that have
335 * requested a rendez-vous of other threads using thread_isolate().
336 * - threads_harmless_mask is a bit field indicating all threads that are
337 * currently harmless in that they promise not to access a shared resource.
338 *
339 * For a given thread, its bits in want_rdv and harmless can be translated like
340 * this :
341 *
342 * ----------+----------+----------------------------------------------------
343 * want_rdv | harmless | description
344 * ----------+----------+----------------------------------------------------
345 * 0 | 0 | thread not interested in RDV, possibly harmful
346 * 0 | 1 | thread not interested in RDV but harmless
347 * 1 | 1 | thread interested in RDV and waiting for its turn
348 * 1 | 0 | thread currently working isolated from others
349 * ----------+----------+----------------------------------------------------
350 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200351
William Lallemand6e1796e2018-06-07 11:23:40 +0200352#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
353
Willy Tarreau0c026f42018-08-01 19:12:20 +0200354/* sets the thread ID and the TID bit for the current thread */
355static inline void ha_set_tid(unsigned int data)
356{
357 tid = data;
358 tid_bit = (1UL << tid);
359}
360
Willy Tarreau60b639c2018-08-02 10:16:17 +0200361/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
362 * not be touching any unprotected shared resource during this period. Usually
363 * this is called before poll(), but it may also be placed around very slow
364 * calls (eg: some crypto operations). Needs to be terminated using
365 * thread_harmless_end().
366 */
367static inline void thread_harmless_now()
368{
369 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
370}
371
372/* Ends the harmless period started by thread_harmless_now(). Usually this is
373 * placed after the poll() call. If it is discovered that a job was running and
374 * is relying on the thread still being harmless, the thread waits for the
375 * other one to finish.
376 */
377static inline void thread_harmless_end()
378{
379 while (1) {
380 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
381 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
382 break;
383 thread_harmless_till_end();
384 }
385}
386
387/* an isolated thread has harmless cleared and want_rdv set */
388static inline unsigned long thread_isolated()
389{
390 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
391}
392
William Lallemand6e1796e2018-06-07 11:23:40 +0200393
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200394#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
395
Christopher Fauletf51bac22018-01-30 11:04:29 +0100396/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200397enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200398 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200399 TASK_RQ_LOCK,
400 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200401 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200402 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200403 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200404 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200405 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200406 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200407 STK_TABLE_LOCK,
408 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200409 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200410 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200411 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200412 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200413 SSL_LOCK,
414 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200415 PATREF_LOCK,
416 PATEXP_LOCK,
417 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200418 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200419 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200420 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200421 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200422 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200423 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200424 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200425 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100426 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100427 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100428 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100429 AUTH_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000430 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200431 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200432};
433struct lock_stat {
434 uint64_t nsec_wait_for_write;
435 uint64_t nsec_wait_for_read;
436 uint64_t num_write_locked;
437 uint64_t num_write_unlocked;
438 uint64_t num_read_locked;
439 uint64_t num_read_unlocked;
440};
441
442extern struct lock_stat lock_stats[LOCK_LABELS];
443
444#define __HA_SPINLOCK_T unsigned long
445
446#define __SPIN_INIT(l) ({ (*l) = 0; })
447#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100448#define __SPIN_LOCK(l) pl_take_s(l)
449#define __SPIN_TRYLOCK(l) !pl_try_s(l)
450#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200451
452#define __HA_RWLOCK_T unsigned long
453
454#define __RWLOCK_INIT(l) ({ (*l) = 0; })
455#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
456#define __RWLOCK_WRLOCK(l) pl_take_w(l)
457#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
458#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
459#define __RWLOCK_RDLOCK(l) pl_take_r(l)
460#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
461#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
462
463#define HA_SPINLOCK_T struct ha_spinlock
464
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100465#define HA_SPIN_INIT(l) __spin_init(l)
466#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200467
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100468#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
469#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
470#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200471
472#define HA_RWLOCK_T struct ha_rwlock
473
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
475#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
476#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
477#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
478#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
479#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
480#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
481#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200482
483struct ha_spinlock {
484 __HA_SPINLOCK_T lock;
485 struct {
486 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
487 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
488 struct {
489 const char *function;
490 const char *file;
491 int line;
492 } last_location; /* location of the last owner */
493 } info;
494};
495
496struct ha_rwlock {
497 __HA_RWLOCK_T lock;
498 struct {
499 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
500 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
501 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
502 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
503 struct {
504 const char *function;
505 const char *file;
506 int line;
507 } last_location; /* location of the last write owner */
508 } info;
509};
510
Christopher Fauletf51bac22018-01-30 11:04:29 +0100511static inline const char *lock_label(enum lock_label label)
512{
513 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100514 case FD_LOCK: return "FD";
515 case TASK_RQ_LOCK: return "TASK_RQ";
516 case TASK_WQ_LOCK: return "TASK_WQ";
517 case POOL_LOCK: return "POOL";
518 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100519 case PROXY_LOCK: return "PROXY";
520 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100521 case LBPRM_LOCK: return "LBPRM";
522 case SIGNALS_LOCK: return "SIGNALS";
523 case STK_TABLE_LOCK: return "STK_TABLE";
524 case STK_SESS_LOCK: return "STK_SESS";
525 case APPLETS_LOCK: return "APPLETS";
526 case PEER_LOCK: return "PEER";
527 case BUF_WQ_LOCK: return "BUF_WQ";
528 case STRMS_LOCK: return "STRMS";
529 case SSL_LOCK: return "SSL";
530 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
531 case PATREF_LOCK: return "PATREF";
532 case PATEXP_LOCK: return "PATEXP";
533 case PATLRU_LOCK: return "PATLRU";
534 case VARS_LOCK: return "VARS";
535 case COMP_POOL_LOCK: return "COMP_POOL";
536 case LUA_LOCK: return "LUA";
537 case NOTIF_LOCK: return "NOTIF";
538 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
539 case DNS_LOCK: return "DNS";
540 case PID_LIST_LOCK: return "PID_LIST";
541 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
542 case PIPES_LOCK: return "PIPES";
543 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100544 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100545 case AUTH_LOCK: return "AUTH";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000546 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100547 case LOCK_LABELS: break; /* keep compiler happy */
548 };
549 /* only way to come here is consecutive to an internal bug */
550 abort();
551}
552
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200553static inline void show_lock_stats()
554{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200555 int lbl;
556
557 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
558 fprintf(stderr,
559 "Stats about Lock %s: \n"
560 "\t # write lock : %lu\n"
561 "\t # write unlock: %lu (%ld)\n"
562 "\t # wait time for write : %.3f msec\n"
563 "\t # wait time for write/lock: %.3f nsec\n"
564 "\t # read lock : %lu\n"
565 "\t # read unlock : %lu (%ld)\n"
566 "\t # wait time for read : %.3f msec\n"
567 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100568 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200569 lock_stats[lbl].num_write_locked,
570 lock_stats[lbl].num_write_unlocked,
571 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
572 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
573 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
574 lock_stats[lbl].num_read_locked,
575 lock_stats[lbl].num_read_unlocked,
576 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
577 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
578 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
579 }
580}
581
582/* Following functions are used to collect some stats about locks. We wrap
583 * pthread functions to known how much time we wait in a lock. */
584
585static uint64_t nsec_now(void) {
586 struct timespec ts;
587
588 clock_gettime(CLOCK_MONOTONIC, &ts);
589 return ((uint64_t) ts.tv_sec * 1000000000ULL +
590 (uint64_t) ts.tv_nsec);
591}
592
593static inline void __ha_rwlock_init(struct ha_rwlock *l)
594{
595 memset(l, 0, sizeof(struct ha_rwlock));
596 __RWLOCK_INIT(&l->lock);
597}
598
599static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
600{
601 __RWLOCK_DESTROY(&l->lock);
602 memset(l, 0, sizeof(struct ha_rwlock));
603}
604
605
606static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
607 const char *func, const char *file, int line)
608{
609 uint64_t start_time;
610
611 if (unlikely(l->info.cur_writer & tid_bit)) {
612 /* the thread is already owning the lock for write */
613 abort();
614 }
615
616 if (unlikely(l->info.cur_readers & tid_bit)) {
617 /* the thread is already owning the lock for read */
618 abort();
619 }
620
621 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
622
623 start_time = nsec_now();
624 __RWLOCK_WRLOCK(&l->lock);
625 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
626
627 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
628
629 l->info.cur_writer = tid_bit;
630 l->info.last_location.function = func;
631 l->info.last_location.file = file;
632 l->info.last_location.line = line;
633
634 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
635}
636
637static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
638 const char *func, const char *file, int line)
639{
640 uint64_t start_time;
641 int r;
642
643 if (unlikely(l->info.cur_writer & tid_bit)) {
644 /* the thread is already owning the lock for write */
645 abort();
646 }
647
648 if (unlikely(l->info.cur_readers & tid_bit)) {
649 /* the thread is already owning the lock for read */
650 abort();
651 }
652
653 /* We set waiting writer because trywrlock could wait for readers to quit */
654 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
655
656 start_time = nsec_now();
657 r = __RWLOCK_TRYWRLOCK(&l->lock);
658 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
659 if (unlikely(r)) {
660 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
661 return r;
662 }
663 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
664
665 l->info.cur_writer = tid_bit;
666 l->info.last_location.function = func;
667 l->info.last_location.file = file;
668 l->info.last_location.line = line;
669
670 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
671
672 return 0;
673}
674
675static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
676 const char *func, const char *file, int line)
677{
678 if (unlikely(!(l->info.cur_writer & tid_bit))) {
679 /* the thread is not owning the lock for write */
680 abort();
681 }
682
683 l->info.cur_writer = 0;
684 l->info.last_location.function = func;
685 l->info.last_location.file = file;
686 l->info.last_location.line = line;
687
688 __RWLOCK_WRUNLOCK(&l->lock);
689
690 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
691}
692
693static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
694{
695 uint64_t start_time;
696
697 if (unlikely(l->info.cur_writer & tid_bit)) {
698 /* the thread is already owning the lock for write */
699 abort();
700 }
701
702 if (unlikely(l->info.cur_readers & tid_bit)) {
703 /* the thread is already owning the lock for read */
704 abort();
705 }
706
707 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
708
709 start_time = nsec_now();
710 __RWLOCK_RDLOCK(&l->lock);
711 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
712 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
713
714 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
715
716 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
717}
718
719static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
720{
721 int r;
722
723 if (unlikely(l->info.cur_writer & tid_bit)) {
724 /* the thread is already owning the lock for write */
725 abort();
726 }
727
728 if (unlikely(l->info.cur_readers & tid_bit)) {
729 /* the thread is already owning the lock for read */
730 abort();
731 }
732
733 /* try read should never wait */
734 r = __RWLOCK_TRYRDLOCK(&l->lock);
735 if (unlikely(r))
736 return r;
737 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
738
739 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
740
741 return 0;
742}
743
744static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
745{
746 if (unlikely(!(l->info.cur_readers & tid_bit))) {
747 /* the thread is not owning the lock for read */
748 abort();
749 }
750
751 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
752
753 __RWLOCK_RDUNLOCK(&l->lock);
754
755 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
756}
757
758static inline void __spin_init(struct ha_spinlock *l)
759{
760 memset(l, 0, sizeof(struct ha_spinlock));
761 __SPIN_INIT(&l->lock);
762}
763
764static inline void __spin_destroy(struct ha_spinlock *l)
765{
766 __SPIN_DESTROY(&l->lock);
767 memset(l, 0, sizeof(struct ha_spinlock));
768}
769
770static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
771 const char *func, const char *file, int line)
772{
773 uint64_t start_time;
774
775 if (unlikely(l->info.owner & tid_bit)) {
776 /* the thread is already owning the lock */
777 abort();
778 }
779
780 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
781
782 start_time = nsec_now();
783 __SPIN_LOCK(&l->lock);
784 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
785
786 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
787
788
789 l->info.owner = tid_bit;
790 l->info.last_location.function = func;
791 l->info.last_location.file = file;
792 l->info.last_location.line = line;
793
794 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
795}
796
797static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
798 const char *func, const char *file, int line)
799{
800 int r;
801
802 if (unlikely(l->info.owner & tid_bit)) {
803 /* the thread is already owning the lock */
804 abort();
805 }
806
807 /* try read should never wait */
808 r = __SPIN_TRYLOCK(&l->lock);
809 if (unlikely(r))
810 return r;
811 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
812
813 l->info.owner = tid_bit;
814 l->info.last_location.function = func;
815 l->info.last_location.file = file;
816 l->info.last_location.line = line;
817
818 return 0;
819}
820
821static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
822 const char *func, const char *file, int line)
823{
824 if (unlikely(!(l->info.owner & tid_bit))) {
825 /* the thread is not owning the lock */
826 abort();
827 }
828
829 l->info.owner = 0;
830 l->info.last_location.function = func;
831 l->info.last_location.file = file;
832 l->info.last_location.line = line;
833
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100834 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200835 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
836}
837
838#else /* DEBUG_THREAD */
839
840#define HA_SPINLOCK_T unsigned long
841
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100842#define HA_SPIN_INIT(l) ({ (*l) = 0; })
843#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
844#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
845#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
846#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200847
848#define HA_RWLOCK_T unsigned long
849
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100850#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
851#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
852#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
853#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
854#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
855#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
856#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
857#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200858
859#endif /* DEBUG_THREAD */
860
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100861#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200862
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100863static __inline int
864__ha_cas_dw(void *target, void *compare, const void *set)
865{
866 char ret;
867
868 __asm __volatile("lock cmpxchg16b %0; setz %3"
869 : "+m" (*(void **)target),
870 "=a" (((void **)compare)[0]),
871 "=d" (((void **)compare)[1]),
872 "=q" (ret)
873 : "a" (((void **)compare)[0]),
874 "d" (((void **)compare)[1]),
875 "b" (((const void **)set)[0]),
876 "c" (((const void **)set)[1])
877 : "memory", "cc");
878 return (ret);
879}
880
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100881/* Use __ha_barrier_atomic* when you're trying to protect data that are
882 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
883 */
884static __inline void
885__ha_barrier_atomic_load(void)
886{
887 __asm __volatile("" ::: "memory");
888}
889
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100890static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100891__ha_barrier_atomic_store(void)
892{
893 __asm __volatile("" ::: "memory");
894}
895
896static __inline void
897__ha_barrier_atomic_full(void)
898{
899 __asm __volatile("" ::: "memory");
900}
901
902static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100903__ha_barrier_load(void)
904{
905 __asm __volatile("lfence" ::: "memory");
906}
907
908static __inline void
909__ha_barrier_store(void)
910{
911 __asm __volatile("sfence" ::: "memory");
912}
913
914static __inline void
915__ha_barrier_full(void)
916{
917 __asm __volatile("mfence" ::: "memory");
918}
919
920#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200921
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100922/* Use __ha_barrier_atomic* when you're trying to protect data that are
923 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
924 */
925static __inline void
926__ha_barrier_atomic_load(void)
927{
928 __asm __volatile("dmb" ::: "memory");
929}
930
931static __inline void
932__ha_barrier_atomic_store(void)
933{
934 __asm __volatile("dsb" ::: "memory");
935}
936
937static __inline void
938__ha_barrier_atomic_full(void)
939{
940 __asm __volatile("dmb" ::: "memory");
941}
942
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100943static __inline void
944__ha_barrier_load(void)
945{
946 __asm __volatile("dmb" ::: "memory");
947}
948
949static __inline void
950__ha_barrier_store(void)
951{
952 __asm __volatile("dsb" ::: "memory");
953}
954
955static __inline void
956__ha_barrier_full(void)
957{
958 __asm __volatile("dmb" ::: "memory");
959}
960
Willy Tarreau41ccb192018-02-14 14:16:28 +0100961static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100962{
963 uint64_t previous;
964 int tmp;
965
966 __asm __volatile("1:"
967 "ldrexd %0, [%4];"
968 "cmp %Q0, %Q2;"
969 "ittt eq;"
970 "cmpeq %R0, %R2;"
971 "strexdeq %1, %3, [%4];"
972 "cmpeq %1, #1;"
973 "beq 1b;"
974 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100975 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100976 : "memory", "cc");
977 tmp = (previous == *(uint64_t *)compare);
978 *(uint64_t *)compare = previous;
979 return (tmp);
980}
981
982#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100983
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100984/* Use __ha_barrier_atomic* when you're trying to protect data that are
985 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
986 */
987static __inline void
988__ha_barrier_atomic_load(void)
989{
990 __asm __volatile("dmb ishld" ::: "memory");
991}
992
993static __inline void
994__ha_barrier_atomic_store(void)
995{
996 __asm __volatile("dmb ishst" ::: "memory");
997}
998
999static __inline void
1000__ha_barrier_atomic_full(void)
1001{
1002 __asm __volatile("dmb ish" ::: "memory");
1003}
1004
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001005static __inline void
1006__ha_barrier_load(void)
1007{
1008 __asm __volatile("dmb ishld" ::: "memory");
1009}
1010
1011static __inline void
1012__ha_barrier_store(void)
1013{
1014 __asm __volatile("dmb ishst" ::: "memory");
1015}
1016
1017static __inline void
1018__ha_barrier_full(void)
1019{
1020 __asm __volatile("dmb ish" ::: "memory");
1021}
1022
1023static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1024{
1025 void *value[2];
1026 uint64_t tmp1, tmp2;
1027
1028 __asm__ __volatile__("1:"
1029 "ldxp %0, %1, [%4];"
1030 "mov %2, %0;"
1031 "mov %3, %1;"
1032 "eor %0, %0, %5;"
1033 "eor %1, %1, %6;"
1034 "orr %1, %0, %1;"
1035 "mov %w0, #0;"
1036 "cbnz %1, 2f;"
1037 "stxp %w0, %7, %8, [%4];"
1038 "cbnz %w0, 1b;"
1039 "mov %w0, #1;"
1040 "2:"
1041 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1042 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1043 : "cc", "memory");
1044
1045 memcpy(compare, &value, sizeof(value));
1046 return (tmp1);
1047}
1048
1049#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001050#define __ha_barrier_atomic_load __sync_synchronize
1051#define __ha_barrier_atomic_store __sync_synchronize
1052#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001053#define __ha_barrier_load __sync_synchronize
1054#define __ha_barrier_store __sync_synchronize
1055#define __ha_barrier_full __sync_synchronize
1056#endif
1057
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001058void ha_spin_init(HA_SPINLOCK_T *l);
1059void ha_rwlock_init(HA_RWLOCK_T *l);
1060
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001061#endif /* USE_THREAD */
1062
Willy Tarreau149ab772019-01-26 14:27:06 +01001063extern int thread_cpus_enabled_at_boot;
1064
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001065static inline void __ha_compiler_barrier(void)
1066{
1067 __asm __volatile("" ::: "memory");
1068}
1069
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001070int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001071int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001072
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001073#endif /* _COMMON_HATHREADS_H */