blob: ae1009a9f9ab84ba360a40795e0cb100a2f8cb87 [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 Tarreau5778fea2020-05-09 09:08:09 +020047#define THREAD_LOCAL /* empty */
Willy Tarreau421f02e2018-01-20 18:19:22 +010048#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020049#define MAX_THREADS_MASK 1
50
51/* Only way found to replace variables with constants that are optimized away
52 * at build time.
53 */
54enum { all_threads_mask = 1UL };
Willy Tarreau441259c2019-05-22 07:48:18 +020055enum { threads_harmless_mask = 0 };
56enum { threads_want_rdv_mask = 0 };
Willy Tarreau9a1f5732019-06-09 12:20:02 +020057enum { threads_sync_mask = 0 };
Willy Tarreau0c026f42018-08-01 19:12:20 +020058enum { tid_bit = 1UL };
59enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010060
Willy Tarreau5a6e2242019-05-20 18:56:48 +020061extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020062 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +020063 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +020064 uint64_t prev_cpu_time; /* previous per thread CPU time */
65 uint64_t prev_mono_time; /* previous system wide monotonic time */
66 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020067 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020068 /* pad to cache line (64B) */
69 char __pad[0]; /* unused except to check remaining room */
70 char __end[0] __attribute__((aligned(64)));
David Carliera92c5ce2019-09-13 05:03:12 +010071} ha_thread_info[MAX_THREADS];
Willy Tarreau5a6e2242019-05-20 18:56:48 +020072
Willy Tarreau8323a372019-05-20 18:57:53 +020073extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
74
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010075#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010076#define __decl_spinlock(lock)
77#define __decl_aligned_spinlock(lock)
78#define __decl_rwlock(lock)
79#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010080
Willy Tarreaud66345d2020-05-05 15:58:00 +020081#define HA_ATOMIC_CAS(val, old, new) \
82 ({ \
83 typeof(val) _v = (val); \
84 typeof(old) _o = (old); \
85 (*_v == *_o) ? ((*_v = (new)), 1) : ((*_o = *_v), 0); \
86 })
Willy Tarreauc3b59582019-05-27 17:37:20 +020087
88/* warning, n is a pointer to the double value for dwcas */
89#define HA_ATOMIC_DWCAS(val, o, n) \
90 ({ \
91 long *_v = (long*)(val); \
92 long *_o = (long*)(o); \
93 long *_n = (long*)(n); \
94 long _v0 = _v[0], _v1 = _v[1]; \
95 (_v0 == _o[0] && _v1 == _o[1]) ? \
96 (_v[0] = _n[0], _v[1] = _n[1], 1) : \
97 (_o[0] = _v0, _o[1] = _v1, 0); \
98 })
99
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200100#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
101#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +0200102#define HA_ATOMIC_XADD(val, i) \
103 ({ \
104 typeof((val)) __p_xadd = (val); \
105 typeof(*(val)) __old_xadd = *__p_xadd; \
106 *__p_xadd += i; \
107 __old_xadd; \
108 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
110#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
111#define HA_ATOMIC_XCHG(val, new) \
112 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200113 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200114 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200115 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100117#define HA_ATOMIC_BTS(val, bit) \
118 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200119 typeof((val)) __p_bts = (val); \
120 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
121 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
122 if (!__t_bts) \
123 *__p_bts |= __b_bts; \
124 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100125 })
126#define HA_ATOMIC_BTR(val, bit) \
127 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200128 typeof((val)) __p_btr = (val); \
129 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
130 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
131 if (__t_btr) \
132 *__p_btr &= ~__b_btr; \
133 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100134 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200135#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200136#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
137#define HA_ATOMIC_UPDATE_MAX(val, new) \
138 ({ \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200139 typeof(val) __val = (val); \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200140 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200141 \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200142 if (*__val < __new_max) \
143 *__val = __new_max; \
144 *__val; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200145 })
146
147#define HA_ATOMIC_UPDATE_MIN(val, new) \
148 ({ \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200149 typeof(val) __val = (val); \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200150 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200151 \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200152 if (*__val > __new_min) \
153 *__val = __new_min; \
154 *__val; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200155 })
156
Willy Tarreaub29dc952017-10-31 18:00:20 +0100157#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200158
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100159#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
160#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
161#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
162#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
163#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200164
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100165#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
166#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
167#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
168#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
169#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
170#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
171#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
172#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200173
William Lallemand6e1796e2018-06-07 11:23:40 +0200174#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
175
Willy Tarreau0c026f42018-08-01 19:12:20 +0200176static inline void ha_set_tid(unsigned int tid)
177{
David Carliera92c5ce2019-09-13 05:03:12 +0100178 ti = &ha_thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200179}
180
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200181static inline unsigned long long ha_get_pthread_id(unsigned int thr)
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200182{
183 return 0;
184}
185
Willy Tarreau38171da2019-05-17 16:33:13 +0200186static inline void ha_thread_relax(void)
187{
188#if _POSIX_PRIORITY_SCHEDULING
189 sched_yield();
190#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200191}
William Lallemand6e1796e2018-06-07 11:23:40 +0200192
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200193/* send signal <sig> to thread <thr> */
194static inline void ha_tkill(unsigned int thr, int sig)
195{
196 raise(sig);
197}
198
199/* send signal <sig> to all threads */
200static inline void ha_tkillall(int sig)
201{
202 raise(sig);
203}
204
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100205static inline void __ha_barrier_atomic_load(void)
206{
207}
208
209static inline void __ha_barrier_atomic_store(void)
210{
211}
212
213static inline void __ha_barrier_atomic_full(void)
214{
215}
216
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100217static inline void __ha_barrier_load(void)
218{
219}
220
221static inline void __ha_barrier_store(void)
222{
223}
224
225static inline void __ha_barrier_full(void)
226{
227}
228
Willy Tarreau60b639c2018-08-02 10:16:17 +0200229static inline void thread_harmless_now()
230{
231}
232
233static inline void thread_harmless_end()
234{
235}
236
237static inline void thread_isolate()
238{
239}
240
241static inline void thread_release()
242{
243}
244
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200245static inline void thread_sync_release()
246{
247}
248
Willy Tarreau60b639c2018-08-02 10:16:17 +0200249static inline unsigned long thread_isolated()
250{
251 return 1;
252}
253
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200254#else /* USE_THREAD */
255
256#include <stdio.h>
257#include <stdlib.h>
258#include <string.h>
259#include <pthread.h>
260#include <import/plock.h>
261
Willy Tarreau5778fea2020-05-09 09:08:09 +0200262#define THREAD_LOCAL __thread
263
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100264#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100265#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100266#endif
267
268#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100269
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100270#define __decl_hathreads(decl) decl
271
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100272/* declare a self-initializing spinlock */
273#define __decl_spinlock(lock) \
274 HA_SPINLOCK_T (lock); \
275 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
276
277/* declare a self-initializing spinlock, aligned on a cache line */
278#define __decl_aligned_spinlock(lock) \
279 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
280 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
281
282/* declare a self-initializing rwlock */
283#define __decl_rwlock(lock) \
284 HA_RWLOCK_T (lock); \
285 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
286
287/* declare a self-initializing rwlock, aligned on a cache line */
288#define __decl_aligned_rwlock(lock) \
289 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
290 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
291
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200292/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
293 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100294
David Carlierec5e8452018-01-11 14:20:43 +0000295#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100296/* gcc < 4.7 */
297
298#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
299#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200300#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100301#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
302#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
303
304/* the CAS is a bit complicated. The older API doesn't support returning the
305 * value and the swap's result at the same time. So here we take what looks
306 * like the safest route, consisting in using the boolean version guaranteeing
307 * that the operation was performed or not, and we snoop a previous value. If
308 * the compare succeeds, we return. If it fails, we return the previous value,
309 * but only if it differs from the expected one. If it's the same it's a race
310 * thus we try again to avoid confusing a possibly sensitive caller.
311 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200312#define HA_ATOMIC_CAS(val, old, new) \
313 ({ \
314 typeof((val)) __val_cas = (val); \
315 typeof((old)) __oldp_cas = (old); \
316 typeof(*(old)) __oldv_cas; \
317 typeof((new)) __new_cas = (new); \
318 int __ret_cas; \
319 do { \
320 __oldv_cas = *__val_cas; \
321 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
322 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
323 if (!__ret_cas) \
324 *__oldp_cas = __oldv_cas; \
325 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100326 })
327
Willy Tarreauc3b59582019-05-27 17:37:20 +0200328/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200329#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
330
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200331#define HA_ATOMIC_XCHG(val, new) \
332 ({ \
333 typeof((val)) __val_xchg = (val); \
334 typeof(*(val)) __old_xchg; \
335 typeof((new)) __new_xchg = (new); \
336 do { __old_xchg = *__val_xchg; \
337 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
338 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100339 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100340
341#define HA_ATOMIC_BTS(val, bit) \
342 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200343 typeof(*(val)) __b_bts = (1UL << (bit)); \
344 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100345 })
346
347#define HA_ATOMIC_BTR(val, bit) \
348 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200349 typeof(*(val)) __b_btr = (1UL << (bit)); \
350 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100351 })
352
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200353#define HA_ATOMIC_LOAD(val) \
354 ({ \
355 typeof(*(val)) ret; \
356 __sync_synchronize(); \
357 ret = *(volatile typeof(val))val; \
358 __sync_synchronize(); \
359 ret; \
360 })
361
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200362#define HA_ATOMIC_STORE(val, new) \
363 ({ \
364 typeof((val)) __val_store = (val); \
365 typeof(*(val)) __old_store; \
366 typeof((new)) __new_store = (new); \
367 do { __old_store = *__val_store; \
368 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100369 })
370#else
371/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100372#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 +0200373/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200374#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100375#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
376#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
377#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
378#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
379#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100380#define HA_ATOMIC_BTS(val, bit) \
381 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200382 typeof(*(val)) __b_bts = (1UL << (bit)); \
383 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100384 })
385
386#define HA_ATOMIC_BTR(val, bit) \
387 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200388 typeof(*(val)) __b_btr = (1UL << (bit)); \
389 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100390 })
391
Olivier Houchard11353792019-03-07 18:48:22 +0100392#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
393#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200394#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200395
396/* Variants that don't generate any memory barrier.
397 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
398 * that will always generate correct code.
399 * Usually it's fine to use those when updating data that have no dependency,
400 * ie updating a counter. Otherwise a barrier is required.
401 */
402#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 +0200403/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200404#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200405#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
406#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
407#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
408#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
409#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
410#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
411#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200412#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200413
414#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100415
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200416#define HA_ATOMIC_UPDATE_MAX(val, new) \
417 ({ \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200418 typeof(val) __val = (val); \
419 typeof(*(val)) __old_max = *__val; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200420 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200421 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200422 while (__old_max < __new_max && \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200423 !HA_ATOMIC_CAS(__val, &__old_max, __new_max)); \
424 *__val; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200425 })
426#define HA_ATOMIC_UPDATE_MIN(val, new) \
427 ({ \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200428 typeof(val) __val = (val); \
429 typeof(*(val)) __old_min = *__val; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200430 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200431 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200432 while (__old_min > __new_min && \
Willy Tarreaua4d9ee32020-05-05 16:13:36 +0200433 !HA_ATOMIC_CAS(__val, &__old_min, __new_min)); \
434 *__val; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200435 })
436
Willy Tarreaub29dc952017-10-31 18:00:20 +0100437#define HA_BARRIER() pl_barrier()
438
Willy Tarreau60b639c2018-08-02 10:16:17 +0200439void thread_harmless_till_end();
440void thread_isolate();
441void thread_release();
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200442void thread_sync_release();
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200443void ha_tkill(unsigned int thr, int sig);
444void ha_tkillall(int sig);
Christopher Faulet339fff82017-10-19 11:59:15 +0200445
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200446extern struct thread_info {
447 pthread_t pthread;
448 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +0200449 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +0200450 uint64_t prev_cpu_time; /* previous per thread CPU time */
451 uint64_t prev_mono_time; /* previous system wide monotonic time */
452 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200453 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200454 /* pad to cache line (64B) */
455 char __pad[0]; /* unused except to check remaining room */
456 char __end[0] __attribute__((aligned(64)));
David Carliera92c5ce2019-09-13 05:03:12 +0100457} ha_thread_info[MAX_THREADS];
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200458
Willy Tarreau0c026f42018-08-01 19:12:20 +0200459extern THREAD_LOCAL unsigned int tid; /* The thread id */
460extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200461extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200462extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200463extern volatile unsigned long threads_want_rdv_mask;
464extern volatile unsigned long threads_harmless_mask;
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200465extern volatile unsigned long threads_sync_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200466
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200467/* explanation for threads_want_rdv_mask, threads_harmless_mask, and
468 * threads_sync_mask :
Willy Tarreau60b639c2018-08-02 10:16:17 +0200469 * - threads_want_rdv_mask is a bit field indicating all threads that have
470 * requested a rendez-vous of other threads using thread_isolate().
471 * - threads_harmless_mask is a bit field indicating all threads that are
472 * currently harmless in that they promise not to access a shared resource.
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200473 * - threads_sync_mask is a bit field indicating that a thread waiting for
474 * others to finish wants to leave synchronized with others and as such
475 * promises to do so as well using thread_sync_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +0200476 *
477 * For a given thread, its bits in want_rdv and harmless can be translated like
478 * this :
479 *
480 * ----------+----------+----------------------------------------------------
481 * want_rdv | harmless | description
482 * ----------+----------+----------------------------------------------------
483 * 0 | 0 | thread not interested in RDV, possibly harmful
484 * 0 | 1 | thread not interested in RDV but harmless
485 * 1 | 1 | thread interested in RDV and waiting for its turn
486 * 1 | 0 | thread currently working isolated from others
487 * ----------+----------+----------------------------------------------------
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200488 *
489 * thread_sync_mask only delays the leaving of threads_sync_release() to make
490 * sure that each thread's harmless bit is cleared before leaving the function.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200491 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200492
William Lallemand6e1796e2018-06-07 11:23:40 +0200493#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
494
Willy Tarreau0c026f42018-08-01 19:12:20 +0200495/* sets the thread ID and the TID bit for the current thread */
496static inline void ha_set_tid(unsigned int data)
497{
498 tid = data;
499 tid_bit = (1UL << tid);
David Carliera92c5ce2019-09-13 05:03:12 +0100500 ti = &ha_thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200501}
502
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200503/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
504 * since POSIX took great care of not specifying its representation, making it
505 * hard to export for post-mortem analysis. For this reason we copy it into a
506 * union and will use the smallest scalar type at least as large as its size,
507 * which will keep endianness and alignment for all regular sizes. As a last
508 * resort we end up with a long long ligned to the first bytes in memory, which
509 * will be endian-dependent if pthread_t is larger than a long long (not seen
510 * yet).
511 */
512static inline unsigned long long ha_get_pthread_id(unsigned int thr)
513{
514 union {
515 pthread_t t;
516 unsigned long long ll;
517 unsigned int i;
518 unsigned short s;
519 unsigned char c;
520 } u;
521
522 memset(&u, 0, sizeof(u));
523 u.t = ha_thread_info[thr].pthread;
524
525 if (sizeof(u.t) <= sizeof(u.c))
526 return u.c;
527 else if (sizeof(u.t) <= sizeof(u.s))
528 return u.s;
529 else if (sizeof(u.t) <= sizeof(u.i))
530 return u.i;
531 return u.ll;
532}
533
Willy Tarreau38171da2019-05-17 16:33:13 +0200534static inline void ha_thread_relax(void)
535{
536#if _POSIX_PRIORITY_SCHEDULING
537 sched_yield();
538#else
539 pl_cpu_relax();
540#endif
541}
542
Willy Tarreau60b639c2018-08-02 10:16:17 +0200543/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
544 * not be touching any unprotected shared resource during this period. Usually
545 * this is called before poll(), but it may also be placed around very slow
546 * calls (eg: some crypto operations). Needs to be terminated using
547 * thread_harmless_end().
548 */
549static inline void thread_harmless_now()
550{
551 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
552}
553
554/* Ends the harmless period started by thread_harmless_now(). Usually this is
555 * placed after the poll() call. If it is discovered that a job was running and
556 * is relying on the thread still being harmless, the thread waits for the
557 * other one to finish.
558 */
559static inline void thread_harmless_end()
560{
561 while (1) {
562 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
563 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
564 break;
565 thread_harmless_till_end();
566 }
567}
568
569/* an isolated thread has harmless cleared and want_rdv set */
570static inline unsigned long thread_isolated()
571{
572 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
573}
574
William Lallemand6e1796e2018-06-07 11:23:40 +0200575
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200576#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
577
Christopher Fauletf51bac22018-01-30 11:04:29 +0100578/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200579enum lock_label {
Emeric Brunc60def82017-09-27 14:59:38 +0200580 TASK_RQ_LOCK,
581 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200582 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200583 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200584 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200585 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200586 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200587 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200588 STK_TABLE_LOCK,
589 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200590 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200591 PEER_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200592 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200593 SSL_LOCK,
594 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200595 PATREF_LOCK,
596 PATEXP_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200597 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200598 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200599 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200600 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200601 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200602 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200603 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200604 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100605 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100606 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100607 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200608 LOGSRV_LOCK,
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200609 DICT_LOCK,
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200610 PROTO_LOCK,
William Lallemand150bfa82019-09-19 17:12:49 +0200611 CKCH_LOCK,
612 SNI_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000613 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200614 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200615};
616struct lock_stat {
617 uint64_t nsec_wait_for_write;
618 uint64_t nsec_wait_for_read;
619 uint64_t num_write_locked;
620 uint64_t num_write_unlocked;
621 uint64_t num_read_locked;
622 uint64_t num_read_unlocked;
623};
624
625extern struct lock_stat lock_stats[LOCK_LABELS];
626
627#define __HA_SPINLOCK_T unsigned long
628
629#define __SPIN_INIT(l) ({ (*l) = 0; })
630#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100631#define __SPIN_LOCK(l) pl_take_s(l)
632#define __SPIN_TRYLOCK(l) !pl_try_s(l)
633#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200634
635#define __HA_RWLOCK_T unsigned long
636
637#define __RWLOCK_INIT(l) ({ (*l) = 0; })
638#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
639#define __RWLOCK_WRLOCK(l) pl_take_w(l)
640#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
641#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
642#define __RWLOCK_RDLOCK(l) pl_take_r(l)
643#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
644#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
645
646#define HA_SPINLOCK_T struct ha_spinlock
647
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100648#define HA_SPIN_INIT(l) __spin_init(l)
649#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200650
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100651#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
652#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
653#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200654
655#define HA_RWLOCK_T struct ha_rwlock
656
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100657#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
658#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
659#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
660#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
661#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
662#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
663#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
664#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200665
666struct ha_spinlock {
667 __HA_SPINLOCK_T lock;
668 struct {
669 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
670 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
671 struct {
672 const char *function;
673 const char *file;
674 int line;
675 } last_location; /* location of the last owner */
676 } info;
677};
678
679struct ha_rwlock {
680 __HA_RWLOCK_T lock;
681 struct {
682 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
683 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
684 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
685 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
686 struct {
687 const char *function;
688 const char *file;
689 int line;
690 } last_location; /* location of the last write owner */
691 } info;
692};
693
Christopher Fauletf51bac22018-01-30 11:04:29 +0100694static inline const char *lock_label(enum lock_label label)
695{
696 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100697 case TASK_RQ_LOCK: return "TASK_RQ";
698 case TASK_WQ_LOCK: return "TASK_WQ";
699 case POOL_LOCK: return "POOL";
700 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100701 case PROXY_LOCK: return "PROXY";
702 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100703 case LBPRM_LOCK: return "LBPRM";
704 case SIGNALS_LOCK: return "SIGNALS";
705 case STK_TABLE_LOCK: return "STK_TABLE";
706 case STK_SESS_LOCK: return "STK_SESS";
707 case APPLETS_LOCK: return "APPLETS";
708 case PEER_LOCK: return "PEER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100709 case STRMS_LOCK: return "STRMS";
710 case SSL_LOCK: return "SSL";
711 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
712 case PATREF_LOCK: return "PATREF";
713 case PATEXP_LOCK: return "PATEXP";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100714 case VARS_LOCK: return "VARS";
715 case COMP_POOL_LOCK: return "COMP_POOL";
716 case LUA_LOCK: return "LUA";
717 case NOTIF_LOCK: return "NOTIF";
718 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
719 case DNS_LOCK: return "DNS";
720 case PID_LIST_LOCK: return "PID_LIST";
721 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
722 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100723 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100724 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200725 case LOGSRV_LOCK: return "LOGSRV";
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200726 case DICT_LOCK: return "DICT";
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200727 case PROTO_LOCK: return "PROTO";
William Lallemand150bfa82019-09-19 17:12:49 +0200728 case CKCH_LOCK: return "CKCH";
729 case SNI_LOCK: return "SNI";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000730 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100731 case LOCK_LABELS: break; /* keep compiler happy */
732 };
733 /* only way to come here is consecutive to an internal bug */
734 abort();
735}
736
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200737static inline void show_lock_stats()
738{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200739 int lbl;
740
741 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
742 fprintf(stderr,
743 "Stats about Lock %s: \n"
744 "\t # write lock : %lu\n"
745 "\t # write unlock: %lu (%ld)\n"
746 "\t # wait time for write : %.3f msec\n"
747 "\t # wait time for write/lock: %.3f nsec\n"
748 "\t # read lock : %lu\n"
749 "\t # read unlock : %lu (%ld)\n"
750 "\t # wait time for read : %.3f msec\n"
751 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100752 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200753 lock_stats[lbl].num_write_locked,
754 lock_stats[lbl].num_write_unlocked,
755 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
756 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
757 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
758 lock_stats[lbl].num_read_locked,
759 lock_stats[lbl].num_read_unlocked,
760 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
761 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
762 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
763 }
764}
765
766/* Following functions are used to collect some stats about locks. We wrap
767 * pthread functions to known how much time we wait in a lock. */
768
769static uint64_t nsec_now(void) {
770 struct timespec ts;
771
772 clock_gettime(CLOCK_MONOTONIC, &ts);
773 return ((uint64_t) ts.tv_sec * 1000000000ULL +
774 (uint64_t) ts.tv_nsec);
775}
776
777static inline void __ha_rwlock_init(struct ha_rwlock *l)
778{
779 memset(l, 0, sizeof(struct ha_rwlock));
780 __RWLOCK_INIT(&l->lock);
781}
782
783static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
784{
785 __RWLOCK_DESTROY(&l->lock);
786 memset(l, 0, sizeof(struct ha_rwlock));
787}
788
789
790static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
791 const char *func, const char *file, int line)
792{
793 uint64_t start_time;
794
795 if (unlikely(l->info.cur_writer & tid_bit)) {
796 /* the thread is already owning the lock for write */
797 abort();
798 }
799
800 if (unlikely(l->info.cur_readers & tid_bit)) {
801 /* the thread is already owning the lock for read */
802 abort();
803 }
804
805 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
806
807 start_time = nsec_now();
808 __RWLOCK_WRLOCK(&l->lock);
809 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
810
811 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
812
813 l->info.cur_writer = 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 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
819}
820
821static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
822 const char *func, const char *file, int line)
823{
824 uint64_t start_time;
825 int r;
826
827 if (unlikely(l->info.cur_writer & tid_bit)) {
828 /* the thread is already owning the lock for write */
829 abort();
830 }
831
832 if (unlikely(l->info.cur_readers & tid_bit)) {
833 /* the thread is already owning the lock for read */
834 abort();
835 }
836
837 /* We set waiting writer because trywrlock could wait for readers to quit */
838 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
839
840 start_time = nsec_now();
841 r = __RWLOCK_TRYWRLOCK(&l->lock);
842 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
843 if (unlikely(r)) {
844 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
845 return r;
846 }
847 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
848
849 l->info.cur_writer = tid_bit;
850 l->info.last_location.function = func;
851 l->info.last_location.file = file;
852 l->info.last_location.line = line;
853
854 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
855
856 return 0;
857}
858
859static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
860 const char *func, const char *file, int line)
861{
862 if (unlikely(!(l->info.cur_writer & tid_bit))) {
863 /* the thread is not owning the lock for write */
864 abort();
865 }
866
867 l->info.cur_writer = 0;
868 l->info.last_location.function = func;
869 l->info.last_location.file = file;
870 l->info.last_location.line = line;
871
872 __RWLOCK_WRUNLOCK(&l->lock);
873
874 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
875}
876
877static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
878{
879 uint64_t start_time;
880
881 if (unlikely(l->info.cur_writer & tid_bit)) {
882 /* the thread is already owning the lock for write */
883 abort();
884 }
885
886 if (unlikely(l->info.cur_readers & tid_bit)) {
887 /* the thread is already owning the lock for read */
888 abort();
889 }
890
891 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
892
893 start_time = nsec_now();
894 __RWLOCK_RDLOCK(&l->lock);
895 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
896 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
897
898 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
899
900 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
901}
902
903static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
904{
905 int r;
906
907 if (unlikely(l->info.cur_writer & tid_bit)) {
908 /* the thread is already owning the lock for write */
909 abort();
910 }
911
912 if (unlikely(l->info.cur_readers & tid_bit)) {
913 /* the thread is already owning the lock for read */
914 abort();
915 }
916
917 /* try read should never wait */
918 r = __RWLOCK_TRYRDLOCK(&l->lock);
919 if (unlikely(r))
920 return r;
921 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
922
923 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
924
925 return 0;
926}
927
928static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
929{
930 if (unlikely(!(l->info.cur_readers & tid_bit))) {
931 /* the thread is not owning the lock for read */
932 abort();
933 }
934
935 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
936
937 __RWLOCK_RDUNLOCK(&l->lock);
938
939 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
940}
941
942static inline void __spin_init(struct ha_spinlock *l)
943{
944 memset(l, 0, sizeof(struct ha_spinlock));
945 __SPIN_INIT(&l->lock);
946}
947
948static inline void __spin_destroy(struct ha_spinlock *l)
949{
950 __SPIN_DESTROY(&l->lock);
951 memset(l, 0, sizeof(struct ha_spinlock));
952}
953
954static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
955 const char *func, const char *file, int line)
956{
957 uint64_t start_time;
958
959 if (unlikely(l->info.owner & tid_bit)) {
960 /* the thread is already owning the lock */
961 abort();
962 }
963
964 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
965
966 start_time = nsec_now();
967 __SPIN_LOCK(&l->lock);
968 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
969
970 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
971
972
973 l->info.owner = tid_bit;
974 l->info.last_location.function = func;
975 l->info.last_location.file = file;
976 l->info.last_location.line = line;
977
978 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
979}
980
981static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
982 const char *func, const char *file, int line)
983{
984 int r;
985
986 if (unlikely(l->info.owner & tid_bit)) {
987 /* the thread is already owning the lock */
988 abort();
989 }
990
991 /* try read should never wait */
992 r = __SPIN_TRYLOCK(&l->lock);
993 if (unlikely(r))
994 return r;
995 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
996
997 l->info.owner = tid_bit;
998 l->info.last_location.function = func;
999 l->info.last_location.file = file;
1000 l->info.last_location.line = line;
1001
1002 return 0;
1003}
1004
1005static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
1006 const char *func, const char *file, int line)
1007{
1008 if (unlikely(!(l->info.owner & tid_bit))) {
1009 /* the thread is not owning the lock */
1010 abort();
1011 }
1012
1013 l->info.owner = 0;
1014 l->info.last_location.function = func;
1015 l->info.last_location.file = file;
1016 l->info.last_location.line = line;
1017
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +01001018 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001019 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
1020}
1021
1022#else /* DEBUG_THREAD */
1023
1024#define HA_SPINLOCK_T unsigned long
1025
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001026#define HA_SPIN_INIT(l) ({ (*l) = 0; })
1027#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
1028#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
1029#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
1030#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001031
1032#define HA_RWLOCK_T unsigned long
1033
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001034#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
1035#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
1036#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
1037#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
1038#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
1039#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
1040#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
1041#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001042
1043#endif /* DEBUG_THREAD */
1044
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001045#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001046
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001047static __inline int
1048__ha_cas_dw(void *target, void *compare, const void *set)
1049{
1050 char ret;
1051
1052 __asm __volatile("lock cmpxchg16b %0; setz %3"
1053 : "+m" (*(void **)target),
1054 "=a" (((void **)compare)[0]),
1055 "=d" (((void **)compare)[1]),
1056 "=q" (ret)
1057 : "a" (((void **)compare)[0]),
1058 "d" (((void **)compare)[1]),
1059 "b" (((const void **)set)[0]),
1060 "c" (((const void **)set)[1])
1061 : "memory", "cc");
1062 return (ret);
1063}
1064
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001065/* Use __ha_barrier_atomic* when you're trying to protect data that are
1066 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1067 */
1068static __inline void
1069__ha_barrier_atomic_load(void)
1070{
1071 __asm __volatile("" ::: "memory");
1072}
1073
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001074static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001075__ha_barrier_atomic_store(void)
1076{
1077 __asm __volatile("" ::: "memory");
1078}
1079
1080static __inline void
1081__ha_barrier_atomic_full(void)
1082{
1083 __asm __volatile("" ::: "memory");
1084}
1085
1086static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001087__ha_barrier_load(void)
1088{
1089 __asm __volatile("lfence" ::: "memory");
1090}
1091
1092static __inline void
1093__ha_barrier_store(void)
1094{
1095 __asm __volatile("sfence" ::: "memory");
1096}
1097
1098static __inline void
1099__ha_barrier_full(void)
1100{
1101 __asm __volatile("mfence" ::: "memory");
1102}
1103
1104#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001105
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001106/* Use __ha_barrier_atomic* when you're trying to protect data that are
1107 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1108 */
1109static __inline void
1110__ha_barrier_atomic_load(void)
1111{
1112 __asm __volatile("dmb" ::: "memory");
1113}
1114
1115static __inline void
1116__ha_barrier_atomic_store(void)
1117{
1118 __asm __volatile("dsb" ::: "memory");
1119}
1120
1121static __inline void
1122__ha_barrier_atomic_full(void)
1123{
1124 __asm __volatile("dmb" ::: "memory");
1125}
1126
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001127static __inline void
1128__ha_barrier_load(void)
1129{
1130 __asm __volatile("dmb" ::: "memory");
1131}
1132
1133static __inline void
1134__ha_barrier_store(void)
1135{
1136 __asm __volatile("dsb" ::: "memory");
1137}
1138
1139static __inline void
1140__ha_barrier_full(void)
1141{
1142 __asm __volatile("dmb" ::: "memory");
1143}
1144
Willy Tarreau41ccb192018-02-14 14:16:28 +01001145static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001146{
1147 uint64_t previous;
1148 int tmp;
1149
1150 __asm __volatile("1:"
1151 "ldrexd %0, [%4];"
1152 "cmp %Q0, %Q2;"
1153 "ittt eq;"
1154 "cmpeq %R0, %R2;"
1155 "strexdeq %1, %3, [%4];"
1156 "cmpeq %1, #1;"
1157 "beq 1b;"
1158 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001159 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001160 : "memory", "cc");
1161 tmp = (previous == *(uint64_t *)compare);
1162 *(uint64_t *)compare = previous;
1163 return (tmp);
1164}
1165
1166#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001167
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001168/* Use __ha_barrier_atomic* when you're trying to protect data that are
1169 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1170 */
1171static __inline void
1172__ha_barrier_atomic_load(void)
1173{
1174 __asm __volatile("dmb ishld" ::: "memory");
1175}
1176
1177static __inline void
1178__ha_barrier_atomic_store(void)
1179{
1180 __asm __volatile("dmb ishst" ::: "memory");
1181}
1182
1183static __inline void
1184__ha_barrier_atomic_full(void)
1185{
1186 __asm __volatile("dmb ish" ::: "memory");
1187}
1188
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001189static __inline void
1190__ha_barrier_load(void)
1191{
1192 __asm __volatile("dmb ishld" ::: "memory");
1193}
1194
1195static __inline void
1196__ha_barrier_store(void)
1197{
1198 __asm __volatile("dmb ishst" ::: "memory");
1199}
1200
1201static __inline void
1202__ha_barrier_full(void)
1203{
1204 __asm __volatile("dmb ish" ::: "memory");
1205}
1206
1207static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1208{
1209 void *value[2];
1210 uint64_t tmp1, tmp2;
1211
1212 __asm__ __volatile__("1:"
1213 "ldxp %0, %1, [%4];"
1214 "mov %2, %0;"
1215 "mov %3, %1;"
1216 "eor %0, %0, %5;"
1217 "eor %1, %1, %6;"
1218 "orr %1, %0, %1;"
1219 "mov %w0, #0;"
1220 "cbnz %1, 2f;"
1221 "stxp %w0, %7, %8, [%4];"
1222 "cbnz %w0, 1b;"
1223 "mov %w0, #1;"
1224 "2:"
1225 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1226 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1227 : "cc", "memory");
1228
1229 memcpy(compare, &value, sizeof(value));
1230 return (tmp1);
1231}
1232
1233#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001234#define __ha_barrier_atomic_load __sync_synchronize
1235#define __ha_barrier_atomic_store __sync_synchronize
1236#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001237#define __ha_barrier_load __sync_synchronize
1238#define __ha_barrier_store __sync_synchronize
1239#define __ha_barrier_full __sync_synchronize
1240#endif
1241
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001242void ha_spin_init(HA_SPINLOCK_T *l);
1243void ha_rwlock_init(HA_RWLOCK_T *l);
1244
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001245#endif /* USE_THREAD */
1246
Willy Tarreau149ab772019-01-26 14:27:06 +01001247extern int thread_cpus_enabled_at_boot;
1248
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001249static inline void __ha_compiler_barrier(void)
1250{
1251 __asm __volatile("" ::: "memory");
1252}
1253
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001254int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001255int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001256
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001257#ifndef _HA_ATOMIC_CAS
1258#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1259#endif /* !_HA_ATOMIC_CAS */
1260
Willy Tarreau6a38b322019-05-11 18:04:24 +02001261#ifndef _HA_ATOMIC_DWCAS
1262#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1263#endif /* !_HA_ATOMIC_CAS */
1264
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001265#ifndef _HA_ATOMIC_ADD
1266#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1267#endif /* !_HA_ATOMIC_ADD */
1268
1269#ifndef _HA_ATOMIC_XADD
1270#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1271#endif /* !_HA_ATOMIC_SUB */
1272
1273#ifndef _HA_ATOMIC_SUB
1274#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1275#endif /* !_HA_ATOMIC_SUB */
1276
1277#ifndef _HA_ATOMIC_AND
1278#define _HA_ATOMIC_AND HA_ATOMIC_AND
1279#endif /* !_HA_ATOMIC_AND */
1280
1281#ifndef _HA_ATOMIC_OR
1282#define _HA_ATOMIC_OR HA_ATOMIC_OR
1283#endif /* !_HA_ATOMIC_OR */
1284
1285#ifndef _HA_ATOMIC_XCHG
1286#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1287#endif /* !_HA_ATOMIC_XCHG */
1288
1289#ifndef _HA_ATOMIC_STORE
1290#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1291#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001292
1293#ifndef _HA_ATOMIC_LOAD
1294#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1295#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001296#endif /* _COMMON_HATHREADS_H */