blob: 1350f190464ff79ca5549c42b91109b3191ac948 [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
Willy Tarreau2beaaf72019-05-22 08:43:34 +020025#include <signal.h>
Willy Tarreau38171da2019-05-17 16:33:13 +020026#include <unistd.h>
27#ifdef _POSIX_PRIORITY_SCHEDULING
28#include <sched.h>
29#endif
30
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020031#include <common/config.h>
Willy Tarreau90fa97b2018-11-25 19:46:08 +010032#include <common/initcall.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020033
Willy Tarreau0ccd3222018-07-30 10:34:35 +020034/* Note about all_threads_mask :
Willy Tarreauda9e9392019-02-02 17:03:41 +010035 * - this variable is comprised between 1 and LONGBITS.
36 * - with threads support disabled, this symbol is defined as constant 1UL.
37 * - with threads enabled, it contains the mask of enabled threads. Thus if
38 * only one thread is enabled, it equals 1.
Willy Tarreau0ccd3222018-07-30 10:34:35 +020039 */
40
David Carliera92c5ce2019-09-13 05:03:12 +010041/* thread info flags, for ha_thread_info[].flags */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020042#define TI_FL_STUCK 0x00000001
43
44
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020045#ifndef USE_THREAD
46
Willy Tarreau421f02e2018-01-20 18:19:22 +010047#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020048#define MAX_THREADS_MASK 1
49
50/* Only way found to replace variables with constants that are optimized away
51 * at build time.
52 */
53enum { all_threads_mask = 1UL };
Willy Tarreau441259c2019-05-22 07:48:18 +020054enum { threads_harmless_mask = 0 };
55enum { threads_want_rdv_mask = 0 };
Willy Tarreau9a1f5732019-06-09 12:20:02 +020056enum { threads_sync_mask = 0 };
Willy Tarreau0c026f42018-08-01 19:12:20 +020057enum { tid_bit = 1UL };
58enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010059
Willy Tarreau5a6e2242019-05-20 18:56:48 +020060extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020061 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +020062 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +020063 uint64_t prev_cpu_time; /* previous per thread CPU time */
64 uint64_t prev_mono_time; /* previous system wide monotonic time */
65 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020066 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020067 /* pad to cache line (64B) */
68 char __pad[0]; /* unused except to check remaining room */
69 char __end[0] __attribute__((aligned(64)));
David Carliera92c5ce2019-09-13 05:03:12 +010070} ha_thread_info[MAX_THREADS];
Willy Tarreau5a6e2242019-05-20 18:56:48 +020071
Willy Tarreau8323a372019-05-20 18:57:53 +020072extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
73
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010074#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010075#define __decl_spinlock(lock)
76#define __decl_aligned_spinlock(lock)
77#define __decl_rwlock(lock)
78#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010079
Willy Tarreaud66345d2020-05-05 15:58:00 +020080#define HA_ATOMIC_CAS(val, old, new) \
81 ({ \
82 typeof(val) _v = (val); \
83 typeof(old) _o = (old); \
84 (*_v == *_o) ? ((*_v = (new)), 1) : ((*_o = *_v), 0); \
85 })
Willy Tarreauc3b59582019-05-27 17:37:20 +020086
87/* warning, n is a pointer to the double value for dwcas */
88#define HA_ATOMIC_DWCAS(val, o, n) \
89 ({ \
90 long *_v = (long*)(val); \
91 long *_o = (long*)(o); \
92 long *_n = (long*)(n); \
93 long _v0 = _v[0], _v1 = _v[1]; \
94 (_v0 == _o[0] && _v1 == _o[1]) ? \
95 (_v[0] = _n[0], _v[1] = _n[1], 1) : \
96 (_o[0] = _v0, _o[1] = _v1, 0); \
97 })
98
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020099#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
100#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +0200101#define HA_ATOMIC_XADD(val, i) \
102 ({ \
103 typeof((val)) __p_xadd = (val); \
104 typeof(*(val)) __old_xadd = *__p_xadd; \
105 *__p_xadd += i; \
106 __old_xadd; \
107 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200108#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
109#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
110#define HA_ATOMIC_XCHG(val, new) \
111 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200112 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200113 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200114 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200115 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100116#define HA_ATOMIC_BTS(val, bit) \
117 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200118 typeof((val)) __p_bts = (val); \
119 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
120 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
121 if (!__t_bts) \
122 *__p_bts |= __b_bts; \
123 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100124 })
125#define HA_ATOMIC_BTR(val, bit) \
126 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200127 typeof((val)) __p_btr = (val); \
128 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
129 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
130 if (__t_btr) \
131 *__p_btr &= ~__b_btr; \
132 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100133 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200134#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200135#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
136#define HA_ATOMIC_UPDATE_MAX(val, new) \
137 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200138 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200139 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200140 if (*(val) < __new_max) \
141 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200142 *(val); \
143 })
144
145#define HA_ATOMIC_UPDATE_MIN(val, new) \
146 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200147 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200148 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200149 if (*(val) > __new_min) \
150 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200151 *(val); \
152 })
153
Willy Tarreaub29dc952017-10-31 18:00:20 +0100154#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200155
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100156#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
157#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
158#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
159#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
160#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200161
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100162#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
163#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
164#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
165#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
166#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
167#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
168#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
169#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200170
William Lallemand6e1796e2018-06-07 11:23:40 +0200171#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
172
Willy Tarreau0c026f42018-08-01 19:12:20 +0200173static inline void ha_set_tid(unsigned int tid)
174{
David Carliera92c5ce2019-09-13 05:03:12 +0100175 ti = &ha_thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200176}
177
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200178static inline unsigned long long ha_get_pthread_id(unsigned int thr)
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200179{
180 return 0;
181}
182
Willy Tarreau38171da2019-05-17 16:33:13 +0200183static inline void ha_thread_relax(void)
184{
185#if _POSIX_PRIORITY_SCHEDULING
186 sched_yield();
187#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200188}
William Lallemand6e1796e2018-06-07 11:23:40 +0200189
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200190/* send signal <sig> to thread <thr> */
191static inline void ha_tkill(unsigned int thr, int sig)
192{
193 raise(sig);
194}
195
196/* send signal <sig> to all threads */
197static inline void ha_tkillall(int sig)
198{
199 raise(sig);
200}
201
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100202static inline void __ha_barrier_atomic_load(void)
203{
204}
205
206static inline void __ha_barrier_atomic_store(void)
207{
208}
209
210static inline void __ha_barrier_atomic_full(void)
211{
212}
213
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100214static inline void __ha_barrier_load(void)
215{
216}
217
218static inline void __ha_barrier_store(void)
219{
220}
221
222static inline void __ha_barrier_full(void)
223{
224}
225
Willy Tarreau60b639c2018-08-02 10:16:17 +0200226static inline void thread_harmless_now()
227{
228}
229
230static inline void thread_harmless_end()
231{
232}
233
234static inline void thread_isolate()
235{
236}
237
238static inline void thread_release()
239{
240}
241
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200242static inline void thread_sync_release()
243{
244}
245
Willy Tarreau60b639c2018-08-02 10:16:17 +0200246static inline unsigned long thread_isolated()
247{
248 return 1;
249}
250
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200251#else /* USE_THREAD */
252
253#include <stdio.h>
254#include <stdlib.h>
255#include <string.h>
256#include <pthread.h>
257#include <import/plock.h>
258
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100259#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100260#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100261#endif
262
263#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100264
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100265#define __decl_hathreads(decl) decl
266
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100267/* declare a self-initializing spinlock */
268#define __decl_spinlock(lock) \
269 HA_SPINLOCK_T (lock); \
270 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
271
272/* declare a self-initializing spinlock, aligned on a cache line */
273#define __decl_aligned_spinlock(lock) \
274 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
275 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
276
277/* declare a self-initializing rwlock */
278#define __decl_rwlock(lock) \
279 HA_RWLOCK_T (lock); \
280 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
281
282/* declare a self-initializing rwlock, aligned on a cache line */
283#define __decl_aligned_rwlock(lock) \
284 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
285 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
286
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200287/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
288 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100289
David Carlierec5e8452018-01-11 14:20:43 +0000290#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100291/* gcc < 4.7 */
292
293#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
294#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200295#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100296#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
297#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
298
299/* the CAS is a bit complicated. The older API doesn't support returning the
300 * value and the swap's result at the same time. So here we take what looks
301 * like the safest route, consisting in using the boolean version guaranteeing
302 * that the operation was performed or not, and we snoop a previous value. If
303 * the compare succeeds, we return. If it fails, we return the previous value,
304 * but only if it differs from the expected one. If it's the same it's a race
305 * thus we try again to avoid confusing a possibly sensitive caller.
306 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200307#define HA_ATOMIC_CAS(val, old, new) \
308 ({ \
309 typeof((val)) __val_cas = (val); \
310 typeof((old)) __oldp_cas = (old); \
311 typeof(*(old)) __oldv_cas; \
312 typeof((new)) __new_cas = (new); \
313 int __ret_cas; \
314 do { \
315 __oldv_cas = *__val_cas; \
316 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
317 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
318 if (!__ret_cas) \
319 *__oldp_cas = __oldv_cas; \
320 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100321 })
322
Willy Tarreauc3b59582019-05-27 17:37:20 +0200323/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200324#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
325
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200326#define HA_ATOMIC_XCHG(val, new) \
327 ({ \
328 typeof((val)) __val_xchg = (val); \
329 typeof(*(val)) __old_xchg; \
330 typeof((new)) __new_xchg = (new); \
331 do { __old_xchg = *__val_xchg; \
332 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
333 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100334 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100335
336#define HA_ATOMIC_BTS(val, bit) \
337 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200338 typeof(*(val)) __b_bts = (1UL << (bit)); \
339 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100340 })
341
342#define HA_ATOMIC_BTR(val, bit) \
343 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200344 typeof(*(val)) __b_btr = (1UL << (bit)); \
345 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100346 })
347
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200348#define HA_ATOMIC_LOAD(val) \
349 ({ \
350 typeof(*(val)) ret; \
351 __sync_synchronize(); \
352 ret = *(volatile typeof(val))val; \
353 __sync_synchronize(); \
354 ret; \
355 })
356
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200357#define HA_ATOMIC_STORE(val, new) \
358 ({ \
359 typeof((val)) __val_store = (val); \
360 typeof(*(val)) __old_store; \
361 typeof((new)) __new_store = (new); \
362 do { __old_store = *__val_store; \
363 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100364 })
365#else
366/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100367#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
Willy Tarreauc3b59582019-05-27 17:37:20 +0200368/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200369#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100370#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
371#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
372#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
373#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
374#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100375#define HA_ATOMIC_BTS(val, bit) \
376 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200377 typeof(*(val)) __b_bts = (1UL << (bit)); \
378 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100379 })
380
381#define HA_ATOMIC_BTR(val, bit) \
382 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200383 typeof(*(val)) __b_btr = (1UL << (bit)); \
384 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100385 })
386
Olivier Houchard11353792019-03-07 18:48:22 +0100387#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
388#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200389#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200390
391/* Variants that don't generate any memory barrier.
392 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
393 * that will always generate correct code.
394 * Usually it's fine to use those when updating data that have no dependency,
395 * ie updating a counter. Otherwise a barrier is required.
396 */
397#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
Willy Tarreauc3b59582019-05-27 17:37:20 +0200398/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200399#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200400#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
401#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
402#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
403#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
404#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
405#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
406#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200407#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200408
409#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100410
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200411#define HA_ATOMIC_UPDATE_MAX(val, new) \
412 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200413 typeof(*(val)) __old_max = *(val); \
414 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200415 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200416 while (__old_max < __new_max && \
417 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
418 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200419 })
420#define HA_ATOMIC_UPDATE_MIN(val, new) \
421 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200422 typeof(*(val)) __old_min = *(val); \
423 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200424 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200425 while (__old_min > __new_min && \
426 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
427 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200428 })
429
Willy Tarreaub29dc952017-10-31 18:00:20 +0100430#define HA_BARRIER() pl_barrier()
431
Willy Tarreau60b639c2018-08-02 10:16:17 +0200432void thread_harmless_till_end();
433void thread_isolate();
434void thread_release();
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200435void thread_sync_release();
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200436void ha_tkill(unsigned int thr, int sig);
437void ha_tkillall(int sig);
Christopher Faulet339fff82017-10-19 11:59:15 +0200438
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200439extern struct thread_info {
440 pthread_t pthread;
441 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +0200442 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +0200443 uint64_t prev_cpu_time; /* previous per thread CPU time */
444 uint64_t prev_mono_time; /* previous system wide monotonic time */
445 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200446 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200447 /* pad to cache line (64B) */
448 char __pad[0]; /* unused except to check remaining room */
449 char __end[0] __attribute__((aligned(64)));
David Carliera92c5ce2019-09-13 05:03:12 +0100450} ha_thread_info[MAX_THREADS];
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200451
Willy Tarreau0c026f42018-08-01 19:12:20 +0200452extern THREAD_LOCAL unsigned int tid; /* The thread id */
453extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200454extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200455extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200456extern volatile unsigned long threads_want_rdv_mask;
457extern volatile unsigned long threads_harmless_mask;
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200458extern volatile unsigned long threads_sync_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200459
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200460/* explanation for threads_want_rdv_mask, threads_harmless_mask, and
461 * threads_sync_mask :
Willy Tarreau60b639c2018-08-02 10:16:17 +0200462 * - threads_want_rdv_mask is a bit field indicating all threads that have
463 * requested a rendez-vous of other threads using thread_isolate().
464 * - threads_harmless_mask is a bit field indicating all threads that are
465 * currently harmless in that they promise not to access a shared resource.
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200466 * - threads_sync_mask is a bit field indicating that a thread waiting for
467 * others to finish wants to leave synchronized with others and as such
468 * promises to do so as well using thread_sync_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +0200469 *
470 * For a given thread, its bits in want_rdv and harmless can be translated like
471 * this :
472 *
473 * ----------+----------+----------------------------------------------------
474 * want_rdv | harmless | description
475 * ----------+----------+----------------------------------------------------
476 * 0 | 0 | thread not interested in RDV, possibly harmful
477 * 0 | 1 | thread not interested in RDV but harmless
478 * 1 | 1 | thread interested in RDV and waiting for its turn
479 * 1 | 0 | thread currently working isolated from others
480 * ----------+----------+----------------------------------------------------
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200481 *
482 * thread_sync_mask only delays the leaving of threads_sync_release() to make
483 * sure that each thread's harmless bit is cleared before leaving the function.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200484 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200485
William Lallemand6e1796e2018-06-07 11:23:40 +0200486#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
487
Willy Tarreau0c026f42018-08-01 19:12:20 +0200488/* sets the thread ID and the TID bit for the current thread */
489static inline void ha_set_tid(unsigned int data)
490{
491 tid = data;
492 tid_bit = (1UL << tid);
David Carliera92c5ce2019-09-13 05:03:12 +0100493 ti = &ha_thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200494}
495
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200496/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
497 * since POSIX took great care of not specifying its representation, making it
498 * hard to export for post-mortem analysis. For this reason we copy it into a
499 * union and will use the smallest scalar type at least as large as its size,
500 * which will keep endianness and alignment for all regular sizes. As a last
501 * resort we end up with a long long ligned to the first bytes in memory, which
502 * will be endian-dependent if pthread_t is larger than a long long (not seen
503 * yet).
504 */
505static inline unsigned long long ha_get_pthread_id(unsigned int thr)
506{
507 union {
508 pthread_t t;
509 unsigned long long ll;
510 unsigned int i;
511 unsigned short s;
512 unsigned char c;
513 } u;
514
515 memset(&u, 0, sizeof(u));
516 u.t = ha_thread_info[thr].pthread;
517
518 if (sizeof(u.t) <= sizeof(u.c))
519 return u.c;
520 else if (sizeof(u.t) <= sizeof(u.s))
521 return u.s;
522 else if (sizeof(u.t) <= sizeof(u.i))
523 return u.i;
524 return u.ll;
525}
526
Willy Tarreau38171da2019-05-17 16:33:13 +0200527static inline void ha_thread_relax(void)
528{
529#if _POSIX_PRIORITY_SCHEDULING
530 sched_yield();
531#else
532 pl_cpu_relax();
533#endif
534}
535
Willy Tarreau60b639c2018-08-02 10:16:17 +0200536/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
537 * not be touching any unprotected shared resource during this period. Usually
538 * this is called before poll(), but it may also be placed around very slow
539 * calls (eg: some crypto operations). Needs to be terminated using
540 * thread_harmless_end().
541 */
542static inline void thread_harmless_now()
543{
544 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
545}
546
547/* Ends the harmless period started by thread_harmless_now(). Usually this is
548 * placed after the poll() call. If it is discovered that a job was running and
549 * is relying on the thread still being harmless, the thread waits for the
550 * other one to finish.
551 */
552static inline void thread_harmless_end()
553{
554 while (1) {
555 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
556 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
557 break;
558 thread_harmless_till_end();
559 }
560}
561
562/* an isolated thread has harmless cleared and want_rdv set */
563static inline unsigned long thread_isolated()
564{
565 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
566}
567
William Lallemand6e1796e2018-06-07 11:23:40 +0200568
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200569#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
570
Christopher Fauletf51bac22018-01-30 11:04:29 +0100571/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200572enum lock_label {
Emeric Brunc60def82017-09-27 14:59:38 +0200573 TASK_RQ_LOCK,
574 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200575 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200576 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200577 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200578 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200579 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200580 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200581 STK_TABLE_LOCK,
582 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200583 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200584 PEER_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200585 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200586 SSL_LOCK,
587 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200588 PATREF_LOCK,
589 PATEXP_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200590 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200591 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200592 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200593 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200594 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200595 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200596 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200597 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100598 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100599 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100600 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200601 LOGSRV_LOCK,
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200602 DICT_LOCK,
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200603 PROTO_LOCK,
William Lallemand150bfa82019-09-19 17:12:49 +0200604 CKCH_LOCK,
605 SNI_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000606 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200607 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200608};
609struct lock_stat {
610 uint64_t nsec_wait_for_write;
611 uint64_t nsec_wait_for_read;
612 uint64_t num_write_locked;
613 uint64_t num_write_unlocked;
614 uint64_t num_read_locked;
615 uint64_t num_read_unlocked;
616};
617
618extern struct lock_stat lock_stats[LOCK_LABELS];
619
620#define __HA_SPINLOCK_T unsigned long
621
622#define __SPIN_INIT(l) ({ (*l) = 0; })
623#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100624#define __SPIN_LOCK(l) pl_take_s(l)
625#define __SPIN_TRYLOCK(l) !pl_try_s(l)
626#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200627
628#define __HA_RWLOCK_T unsigned long
629
630#define __RWLOCK_INIT(l) ({ (*l) = 0; })
631#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
632#define __RWLOCK_WRLOCK(l) pl_take_w(l)
633#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
634#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
635#define __RWLOCK_RDLOCK(l) pl_take_r(l)
636#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
637#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
638
639#define HA_SPINLOCK_T struct ha_spinlock
640
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100641#define HA_SPIN_INIT(l) __spin_init(l)
642#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200643
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100644#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
645#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
646#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200647
648#define HA_RWLOCK_T struct ha_rwlock
649
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100650#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
651#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
652#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
653#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
654#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
655#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
656#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
657#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200658
659struct ha_spinlock {
660 __HA_SPINLOCK_T lock;
661 struct {
662 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
663 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
664 struct {
665 const char *function;
666 const char *file;
667 int line;
668 } last_location; /* location of the last owner */
669 } info;
670};
671
672struct ha_rwlock {
673 __HA_RWLOCK_T lock;
674 struct {
675 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
676 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
677 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
678 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
679 struct {
680 const char *function;
681 const char *file;
682 int line;
683 } last_location; /* location of the last write owner */
684 } info;
685};
686
Christopher Fauletf51bac22018-01-30 11:04:29 +0100687static inline const char *lock_label(enum lock_label label)
688{
689 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100690 case TASK_RQ_LOCK: return "TASK_RQ";
691 case TASK_WQ_LOCK: return "TASK_WQ";
692 case POOL_LOCK: return "POOL";
693 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100694 case PROXY_LOCK: return "PROXY";
695 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100696 case LBPRM_LOCK: return "LBPRM";
697 case SIGNALS_LOCK: return "SIGNALS";
698 case STK_TABLE_LOCK: return "STK_TABLE";
699 case STK_SESS_LOCK: return "STK_SESS";
700 case APPLETS_LOCK: return "APPLETS";
701 case PEER_LOCK: return "PEER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100702 case STRMS_LOCK: return "STRMS";
703 case SSL_LOCK: return "SSL";
704 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
705 case PATREF_LOCK: return "PATREF";
706 case PATEXP_LOCK: return "PATEXP";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100707 case VARS_LOCK: return "VARS";
708 case COMP_POOL_LOCK: return "COMP_POOL";
709 case LUA_LOCK: return "LUA";
710 case NOTIF_LOCK: return "NOTIF";
711 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
712 case DNS_LOCK: return "DNS";
713 case PID_LIST_LOCK: return "PID_LIST";
714 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
715 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100716 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100717 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200718 case LOGSRV_LOCK: return "LOGSRV";
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200719 case DICT_LOCK: return "DICT";
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200720 case PROTO_LOCK: return "PROTO";
William Lallemand150bfa82019-09-19 17:12:49 +0200721 case CKCH_LOCK: return "CKCH";
722 case SNI_LOCK: return "SNI";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000723 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100724 case LOCK_LABELS: break; /* keep compiler happy */
725 };
726 /* only way to come here is consecutive to an internal bug */
727 abort();
728}
729
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200730static inline void show_lock_stats()
731{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200732 int lbl;
733
734 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
735 fprintf(stderr,
736 "Stats about Lock %s: \n"
737 "\t # write lock : %lu\n"
738 "\t # write unlock: %lu (%ld)\n"
739 "\t # wait time for write : %.3f msec\n"
740 "\t # wait time for write/lock: %.3f nsec\n"
741 "\t # read lock : %lu\n"
742 "\t # read unlock : %lu (%ld)\n"
743 "\t # wait time for read : %.3f msec\n"
744 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100745 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200746 lock_stats[lbl].num_write_locked,
747 lock_stats[lbl].num_write_unlocked,
748 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
749 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
750 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
751 lock_stats[lbl].num_read_locked,
752 lock_stats[lbl].num_read_unlocked,
753 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
754 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
755 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
756 }
757}
758
759/* Following functions are used to collect some stats about locks. We wrap
760 * pthread functions to known how much time we wait in a lock. */
761
762static uint64_t nsec_now(void) {
763 struct timespec ts;
764
765 clock_gettime(CLOCK_MONOTONIC, &ts);
766 return ((uint64_t) ts.tv_sec * 1000000000ULL +
767 (uint64_t) ts.tv_nsec);
768}
769
770static inline void __ha_rwlock_init(struct ha_rwlock *l)
771{
772 memset(l, 0, sizeof(struct ha_rwlock));
773 __RWLOCK_INIT(&l->lock);
774}
775
776static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
777{
778 __RWLOCK_DESTROY(&l->lock);
779 memset(l, 0, sizeof(struct ha_rwlock));
780}
781
782
783static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
784 const char *func, const char *file, int line)
785{
786 uint64_t start_time;
787
788 if (unlikely(l->info.cur_writer & tid_bit)) {
789 /* the thread is already owning the lock for write */
790 abort();
791 }
792
793 if (unlikely(l->info.cur_readers & tid_bit)) {
794 /* the thread is already owning the lock for read */
795 abort();
796 }
797
798 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
799
800 start_time = nsec_now();
801 __RWLOCK_WRLOCK(&l->lock);
802 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
803
804 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
805
806 l->info.cur_writer = tid_bit;
807 l->info.last_location.function = func;
808 l->info.last_location.file = file;
809 l->info.last_location.line = line;
810
811 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
812}
813
814static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
815 const char *func, const char *file, int line)
816{
817 uint64_t start_time;
818 int r;
819
820 if (unlikely(l->info.cur_writer & tid_bit)) {
821 /* the thread is already owning the lock for write */
822 abort();
823 }
824
825 if (unlikely(l->info.cur_readers & tid_bit)) {
826 /* the thread is already owning the lock for read */
827 abort();
828 }
829
830 /* We set waiting writer because trywrlock could wait for readers to quit */
831 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
832
833 start_time = nsec_now();
834 r = __RWLOCK_TRYWRLOCK(&l->lock);
835 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
836 if (unlikely(r)) {
837 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
838 return r;
839 }
840 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
841
842 l->info.cur_writer = tid_bit;
843 l->info.last_location.function = func;
844 l->info.last_location.file = file;
845 l->info.last_location.line = line;
846
847 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
848
849 return 0;
850}
851
852static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
853 const char *func, const char *file, int line)
854{
855 if (unlikely(!(l->info.cur_writer & tid_bit))) {
856 /* the thread is not owning the lock for write */
857 abort();
858 }
859
860 l->info.cur_writer = 0;
861 l->info.last_location.function = func;
862 l->info.last_location.file = file;
863 l->info.last_location.line = line;
864
865 __RWLOCK_WRUNLOCK(&l->lock);
866
867 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
868}
869
870static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
871{
872 uint64_t start_time;
873
874 if (unlikely(l->info.cur_writer & tid_bit)) {
875 /* the thread is already owning the lock for write */
876 abort();
877 }
878
879 if (unlikely(l->info.cur_readers & tid_bit)) {
880 /* the thread is already owning the lock for read */
881 abort();
882 }
883
884 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
885
886 start_time = nsec_now();
887 __RWLOCK_RDLOCK(&l->lock);
888 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
889 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
890
891 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
892
893 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
894}
895
896static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
897{
898 int r;
899
900 if (unlikely(l->info.cur_writer & tid_bit)) {
901 /* the thread is already owning the lock for write */
902 abort();
903 }
904
905 if (unlikely(l->info.cur_readers & tid_bit)) {
906 /* the thread is already owning the lock for read */
907 abort();
908 }
909
910 /* try read should never wait */
911 r = __RWLOCK_TRYRDLOCK(&l->lock);
912 if (unlikely(r))
913 return r;
914 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
915
916 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
917
918 return 0;
919}
920
921static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
922{
923 if (unlikely(!(l->info.cur_readers & tid_bit))) {
924 /* the thread is not owning the lock for read */
925 abort();
926 }
927
928 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
929
930 __RWLOCK_RDUNLOCK(&l->lock);
931
932 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
933}
934
935static inline void __spin_init(struct ha_spinlock *l)
936{
937 memset(l, 0, sizeof(struct ha_spinlock));
938 __SPIN_INIT(&l->lock);
939}
940
941static inline void __spin_destroy(struct ha_spinlock *l)
942{
943 __SPIN_DESTROY(&l->lock);
944 memset(l, 0, sizeof(struct ha_spinlock));
945}
946
947static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
948 const char *func, const char *file, int line)
949{
950 uint64_t start_time;
951
952 if (unlikely(l->info.owner & tid_bit)) {
953 /* the thread is already owning the lock */
954 abort();
955 }
956
957 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
958
959 start_time = nsec_now();
960 __SPIN_LOCK(&l->lock);
961 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
962
963 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
964
965
966 l->info.owner = tid_bit;
967 l->info.last_location.function = func;
968 l->info.last_location.file = file;
969 l->info.last_location.line = line;
970
971 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
972}
973
974static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
975 const char *func, const char *file, int line)
976{
977 int r;
978
979 if (unlikely(l->info.owner & tid_bit)) {
980 /* the thread is already owning the lock */
981 abort();
982 }
983
984 /* try read should never wait */
985 r = __SPIN_TRYLOCK(&l->lock);
986 if (unlikely(r))
987 return r;
988 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
989
990 l->info.owner = tid_bit;
991 l->info.last_location.function = func;
992 l->info.last_location.file = file;
993 l->info.last_location.line = line;
994
995 return 0;
996}
997
998static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
999 const char *func, const char *file, int line)
1000{
1001 if (unlikely(!(l->info.owner & tid_bit))) {
1002 /* the thread is not owning the lock */
1003 abort();
1004 }
1005
1006 l->info.owner = 0;
1007 l->info.last_location.function = func;
1008 l->info.last_location.file = file;
1009 l->info.last_location.line = line;
1010
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +01001011 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001012 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
1013}
1014
1015#else /* DEBUG_THREAD */
1016
1017#define HA_SPINLOCK_T unsigned long
1018
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001019#define HA_SPIN_INIT(l) ({ (*l) = 0; })
1020#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
1021#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
1022#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
1023#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001024
1025#define HA_RWLOCK_T unsigned long
1026
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001027#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
1028#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
1029#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
1030#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
1031#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
1032#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
1033#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
1034#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001035
1036#endif /* DEBUG_THREAD */
1037
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001038#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001039
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001040static __inline int
1041__ha_cas_dw(void *target, void *compare, const void *set)
1042{
1043 char ret;
1044
1045 __asm __volatile("lock cmpxchg16b %0; setz %3"
1046 : "+m" (*(void **)target),
1047 "=a" (((void **)compare)[0]),
1048 "=d" (((void **)compare)[1]),
1049 "=q" (ret)
1050 : "a" (((void **)compare)[0]),
1051 "d" (((void **)compare)[1]),
1052 "b" (((const void **)set)[0]),
1053 "c" (((const void **)set)[1])
1054 : "memory", "cc");
1055 return (ret);
1056}
1057
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001058/* Use __ha_barrier_atomic* when you're trying to protect data that are
1059 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1060 */
1061static __inline void
1062__ha_barrier_atomic_load(void)
1063{
1064 __asm __volatile("" ::: "memory");
1065}
1066
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001067static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001068__ha_barrier_atomic_store(void)
1069{
1070 __asm __volatile("" ::: "memory");
1071}
1072
1073static __inline void
1074__ha_barrier_atomic_full(void)
1075{
1076 __asm __volatile("" ::: "memory");
1077}
1078
1079static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001080__ha_barrier_load(void)
1081{
1082 __asm __volatile("lfence" ::: "memory");
1083}
1084
1085static __inline void
1086__ha_barrier_store(void)
1087{
1088 __asm __volatile("sfence" ::: "memory");
1089}
1090
1091static __inline void
1092__ha_barrier_full(void)
1093{
1094 __asm __volatile("mfence" ::: "memory");
1095}
1096
1097#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001098
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001099/* Use __ha_barrier_atomic* when you're trying to protect data that are
1100 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1101 */
1102static __inline void
1103__ha_barrier_atomic_load(void)
1104{
1105 __asm __volatile("dmb" ::: "memory");
1106}
1107
1108static __inline void
1109__ha_barrier_atomic_store(void)
1110{
1111 __asm __volatile("dsb" ::: "memory");
1112}
1113
1114static __inline void
1115__ha_barrier_atomic_full(void)
1116{
1117 __asm __volatile("dmb" ::: "memory");
1118}
1119
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001120static __inline void
1121__ha_barrier_load(void)
1122{
1123 __asm __volatile("dmb" ::: "memory");
1124}
1125
1126static __inline void
1127__ha_barrier_store(void)
1128{
1129 __asm __volatile("dsb" ::: "memory");
1130}
1131
1132static __inline void
1133__ha_barrier_full(void)
1134{
1135 __asm __volatile("dmb" ::: "memory");
1136}
1137
Willy Tarreau41ccb192018-02-14 14:16:28 +01001138static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001139{
1140 uint64_t previous;
1141 int tmp;
1142
1143 __asm __volatile("1:"
1144 "ldrexd %0, [%4];"
1145 "cmp %Q0, %Q2;"
1146 "ittt eq;"
1147 "cmpeq %R0, %R2;"
1148 "strexdeq %1, %3, [%4];"
1149 "cmpeq %1, #1;"
1150 "beq 1b;"
1151 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001152 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001153 : "memory", "cc");
1154 tmp = (previous == *(uint64_t *)compare);
1155 *(uint64_t *)compare = previous;
1156 return (tmp);
1157}
1158
1159#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001160
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001161/* Use __ha_barrier_atomic* when you're trying to protect data that are
1162 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1163 */
1164static __inline void
1165__ha_barrier_atomic_load(void)
1166{
1167 __asm __volatile("dmb ishld" ::: "memory");
1168}
1169
1170static __inline void
1171__ha_barrier_atomic_store(void)
1172{
1173 __asm __volatile("dmb ishst" ::: "memory");
1174}
1175
1176static __inline void
1177__ha_barrier_atomic_full(void)
1178{
1179 __asm __volatile("dmb ish" ::: "memory");
1180}
1181
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001182static __inline void
1183__ha_barrier_load(void)
1184{
1185 __asm __volatile("dmb ishld" ::: "memory");
1186}
1187
1188static __inline void
1189__ha_barrier_store(void)
1190{
1191 __asm __volatile("dmb ishst" ::: "memory");
1192}
1193
1194static __inline void
1195__ha_barrier_full(void)
1196{
1197 __asm __volatile("dmb ish" ::: "memory");
1198}
1199
1200static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1201{
1202 void *value[2];
1203 uint64_t tmp1, tmp2;
1204
1205 __asm__ __volatile__("1:"
1206 "ldxp %0, %1, [%4];"
1207 "mov %2, %0;"
1208 "mov %3, %1;"
1209 "eor %0, %0, %5;"
1210 "eor %1, %1, %6;"
1211 "orr %1, %0, %1;"
1212 "mov %w0, #0;"
1213 "cbnz %1, 2f;"
1214 "stxp %w0, %7, %8, [%4];"
1215 "cbnz %w0, 1b;"
1216 "mov %w0, #1;"
1217 "2:"
1218 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1219 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1220 : "cc", "memory");
1221
1222 memcpy(compare, &value, sizeof(value));
1223 return (tmp1);
1224}
1225
1226#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001227#define __ha_barrier_atomic_load __sync_synchronize
1228#define __ha_barrier_atomic_store __sync_synchronize
1229#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001230#define __ha_barrier_load __sync_synchronize
1231#define __ha_barrier_store __sync_synchronize
1232#define __ha_barrier_full __sync_synchronize
1233#endif
1234
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001235void ha_spin_init(HA_SPINLOCK_T *l);
1236void ha_rwlock_init(HA_RWLOCK_T *l);
1237
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001238#endif /* USE_THREAD */
1239
Willy Tarreau149ab772019-01-26 14:27:06 +01001240extern int thread_cpus_enabled_at_boot;
1241
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001242static inline void __ha_compiler_barrier(void)
1243{
1244 __asm __volatile("" ::: "memory");
1245}
1246
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001247int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001248int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001249
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001250#ifndef _HA_ATOMIC_CAS
1251#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1252#endif /* !_HA_ATOMIC_CAS */
1253
Willy Tarreau6a38b322019-05-11 18:04:24 +02001254#ifndef _HA_ATOMIC_DWCAS
1255#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1256#endif /* !_HA_ATOMIC_CAS */
1257
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001258#ifndef _HA_ATOMIC_ADD
1259#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1260#endif /* !_HA_ATOMIC_ADD */
1261
1262#ifndef _HA_ATOMIC_XADD
1263#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1264#endif /* !_HA_ATOMIC_SUB */
1265
1266#ifndef _HA_ATOMIC_SUB
1267#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1268#endif /* !_HA_ATOMIC_SUB */
1269
1270#ifndef _HA_ATOMIC_AND
1271#define _HA_ATOMIC_AND HA_ATOMIC_AND
1272#endif /* !_HA_ATOMIC_AND */
1273
1274#ifndef _HA_ATOMIC_OR
1275#define _HA_ATOMIC_OR HA_ATOMIC_OR
1276#endif /* !_HA_ATOMIC_OR */
1277
1278#ifndef _HA_ATOMIC_XCHG
1279#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1280#endif /* !_HA_ATOMIC_XCHG */
1281
1282#ifndef _HA_ATOMIC_STORE
1283#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1284#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001285
1286#ifndef _HA_ATOMIC_LOAD
1287#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1288#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001289#endif /* _COMMON_HATHREADS_H */