blob: 95dfb051b2cb20c810dd7fc169abf904ad983feb [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
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020080#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreauc3b59582019-05-27 17:37:20 +020081
82/* warning, n is a pointer to the double value for dwcas */
83#define HA_ATOMIC_DWCAS(val, o, n) \
84 ({ \
85 long *_v = (long*)(val); \
86 long *_o = (long*)(o); \
87 long *_n = (long*)(n); \
88 long _v0 = _v[0], _v1 = _v[1]; \
89 (_v0 == _o[0] && _v1 == _o[1]) ? \
90 (_v[0] = _n[0], _v[1] = _n[1], 1) : \
91 (_o[0] = _v0, _o[1] = _v1, 0); \
92 })
93
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020094#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
95#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020096#define HA_ATOMIC_XADD(val, i) \
97 ({ \
98 typeof((val)) __p_xadd = (val); \
99 typeof(*(val)) __old_xadd = *__p_xadd; \
100 *__p_xadd += i; \
101 __old_xadd; \
102 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200103#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
104#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
105#define HA_ATOMIC_XCHG(val, new) \
106 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200107 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200108 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200109 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100111#define HA_ATOMIC_BTS(val, bit) \
112 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200113 typeof((val)) __p_bts = (val); \
114 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
115 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
116 if (!__t_bts) \
117 *__p_bts |= __b_bts; \
118 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100119 })
120#define HA_ATOMIC_BTR(val, bit) \
121 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200122 typeof((val)) __p_btr = (val); \
123 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
124 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
125 if (__t_btr) \
126 *__p_btr &= ~__b_btr; \
127 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100128 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200129#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200130#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
131#define HA_ATOMIC_UPDATE_MAX(val, new) \
132 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200133 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200134 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200135 if (*(val) < __new_max) \
136 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200137 *(val); \
138 })
139
140#define HA_ATOMIC_UPDATE_MIN(val, new) \
141 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200142 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200143 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200144 if (*(val) > __new_min) \
145 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200146 *(val); \
147 })
148
Willy Tarreaub29dc952017-10-31 18:00:20 +0100149#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200150
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100151#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
152#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
153#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
154#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
155#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200156
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100157#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
158#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
159#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
160#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
161#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
162#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
163#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
164#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200165
William Lallemand6e1796e2018-06-07 11:23:40 +0200166#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
167
Willy Tarreau0c026f42018-08-01 19:12:20 +0200168static inline void ha_set_tid(unsigned int tid)
169{
David Carliera92c5ce2019-09-13 05:03:12 +0100170 ti = &ha_thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200171}
172
Willy Tarreauf0e5da22020-05-01 12:26:03 +0200173static inline unsigned long long ha_get_pthread_id(unsigned int thr)
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200174{
175 return 0;
176}
177
Willy Tarreau38171da2019-05-17 16:33:13 +0200178static inline void ha_thread_relax(void)
179{
180#if _POSIX_PRIORITY_SCHEDULING
181 sched_yield();
182#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200183}
William Lallemand6e1796e2018-06-07 11:23:40 +0200184
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200185/* send signal <sig> to thread <thr> */
186static inline void ha_tkill(unsigned int thr, int sig)
187{
188 raise(sig);
189}
190
191/* send signal <sig> to all threads */
192static inline void ha_tkillall(int sig)
193{
194 raise(sig);
195}
196
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100197static inline void __ha_barrier_atomic_load(void)
198{
199}
200
201static inline void __ha_barrier_atomic_store(void)
202{
203}
204
205static inline void __ha_barrier_atomic_full(void)
206{
207}
208
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100209static inline void __ha_barrier_load(void)
210{
211}
212
213static inline void __ha_barrier_store(void)
214{
215}
216
217static inline void __ha_barrier_full(void)
218{
219}
220
Willy Tarreau60b639c2018-08-02 10:16:17 +0200221static inline void thread_harmless_now()
222{
223}
224
225static inline void thread_harmless_end()
226{
227}
228
229static inline void thread_isolate()
230{
231}
232
233static inline void thread_release()
234{
235}
236
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200237static inline void thread_sync_release()
238{
239}
240
Willy Tarreau60b639c2018-08-02 10:16:17 +0200241static inline unsigned long thread_isolated()
242{
243 return 1;
244}
245
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200246#else /* USE_THREAD */
247
248#include <stdio.h>
249#include <stdlib.h>
250#include <string.h>
251#include <pthread.h>
252#include <import/plock.h>
253
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100254#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100255#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100256#endif
257
258#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100259
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100260#define __decl_hathreads(decl) decl
261
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100262/* declare a self-initializing spinlock */
263#define __decl_spinlock(lock) \
264 HA_SPINLOCK_T (lock); \
265 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
266
267/* declare a self-initializing spinlock, aligned on a cache line */
268#define __decl_aligned_spinlock(lock) \
269 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
270 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
271
272/* declare a self-initializing rwlock */
273#define __decl_rwlock(lock) \
274 HA_RWLOCK_T (lock); \
275 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
276
277/* declare a self-initializing rwlock, aligned on a cache line */
278#define __decl_aligned_rwlock(lock) \
279 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
280 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
281
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200282/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
283 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100284
David Carlierec5e8452018-01-11 14:20:43 +0000285#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100286/* gcc < 4.7 */
287
288#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
289#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200290#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100291#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
292#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
293
294/* the CAS is a bit complicated. The older API doesn't support returning the
295 * value and the swap's result at the same time. So here we take what looks
296 * like the safest route, consisting in using the boolean version guaranteeing
297 * that the operation was performed or not, and we snoop a previous value. If
298 * the compare succeeds, we return. If it fails, we return the previous value,
299 * but only if it differs from the expected one. If it's the same it's a race
300 * thus we try again to avoid confusing a possibly sensitive caller.
301 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200302#define HA_ATOMIC_CAS(val, old, new) \
303 ({ \
304 typeof((val)) __val_cas = (val); \
305 typeof((old)) __oldp_cas = (old); \
306 typeof(*(old)) __oldv_cas; \
307 typeof((new)) __new_cas = (new); \
308 int __ret_cas; \
309 do { \
310 __oldv_cas = *__val_cas; \
311 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
312 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
313 if (!__ret_cas) \
314 *__oldp_cas = __oldv_cas; \
315 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100316 })
317
Willy Tarreauc3b59582019-05-27 17:37:20 +0200318/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200319#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
320
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200321#define HA_ATOMIC_XCHG(val, new) \
322 ({ \
323 typeof((val)) __val_xchg = (val); \
324 typeof(*(val)) __old_xchg; \
325 typeof((new)) __new_xchg = (new); \
326 do { __old_xchg = *__val_xchg; \
327 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
328 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100329 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100330
331#define HA_ATOMIC_BTS(val, bit) \
332 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200333 typeof(*(val)) __b_bts = (1UL << (bit)); \
334 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100335 })
336
337#define HA_ATOMIC_BTR(val, bit) \
338 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200339 typeof(*(val)) __b_btr = (1UL << (bit)); \
340 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100341 })
342
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200343#define HA_ATOMIC_LOAD(val) \
344 ({ \
345 typeof(*(val)) ret; \
346 __sync_synchronize(); \
347 ret = *(volatile typeof(val))val; \
348 __sync_synchronize(); \
349 ret; \
350 })
351
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200352#define HA_ATOMIC_STORE(val, new) \
353 ({ \
354 typeof((val)) __val_store = (val); \
355 typeof(*(val)) __old_store; \
356 typeof((new)) __new_store = (new); \
357 do { __old_store = *__val_store; \
358 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100359 })
360#else
361/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100362#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 +0200363/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200364#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100365#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
366#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
367#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
368#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
369#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100370#define HA_ATOMIC_BTS(val, bit) \
371 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200372 typeof(*(val)) __b_bts = (1UL << (bit)); \
373 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100374 })
375
376#define HA_ATOMIC_BTR(val, bit) \
377 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200378 typeof(*(val)) __b_btr = (1UL << (bit)); \
379 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100380 })
381
Olivier Houchard11353792019-03-07 18:48:22 +0100382#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
383#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200384#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200385
386/* Variants that don't generate any memory barrier.
387 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
388 * that will always generate correct code.
389 * Usually it's fine to use those when updating data that have no dependency,
390 * ie updating a counter. Otherwise a barrier is required.
391 */
392#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 +0200393/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200394#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200395#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
396#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
397#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
398#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
399#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
400#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
401#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200402#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200403
404#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100405
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200406#define HA_ATOMIC_UPDATE_MAX(val, new) \
407 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200408 typeof(*(val)) __old_max = *(val); \
409 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200410 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200411 while (__old_max < __new_max && \
412 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
413 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200414 })
415#define HA_ATOMIC_UPDATE_MIN(val, new) \
416 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200417 typeof(*(val)) __old_min = *(val); \
418 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200419 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200420 while (__old_min > __new_min && \
421 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
422 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200423 })
424
Willy Tarreaub29dc952017-10-31 18:00:20 +0100425#define HA_BARRIER() pl_barrier()
426
Willy Tarreau60b639c2018-08-02 10:16:17 +0200427void thread_harmless_till_end();
428void thread_isolate();
429void thread_release();
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200430void thread_sync_release();
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200431void ha_tkill(unsigned int thr, int sig);
432void ha_tkillall(int sig);
Christopher Faulet339fff82017-10-19 11:59:15 +0200433
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200434extern struct thread_info {
435 pthread_t pthread;
436 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +0200437 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +0200438 uint64_t prev_cpu_time; /* previous per thread CPU time */
439 uint64_t prev_mono_time; /* previous system wide monotonic time */
440 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200441 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200442 /* pad to cache line (64B) */
443 char __pad[0]; /* unused except to check remaining room */
444 char __end[0] __attribute__((aligned(64)));
David Carliera92c5ce2019-09-13 05:03:12 +0100445} ha_thread_info[MAX_THREADS];
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200446
Willy Tarreau0c026f42018-08-01 19:12:20 +0200447extern THREAD_LOCAL unsigned int tid; /* The thread id */
448extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200449extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200450extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200451extern volatile unsigned long threads_want_rdv_mask;
452extern volatile unsigned long threads_harmless_mask;
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200453extern volatile unsigned long threads_sync_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200454
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200455/* explanation for threads_want_rdv_mask, threads_harmless_mask, and
456 * threads_sync_mask :
Willy Tarreau60b639c2018-08-02 10:16:17 +0200457 * - threads_want_rdv_mask is a bit field indicating all threads that have
458 * requested a rendez-vous of other threads using thread_isolate().
459 * - threads_harmless_mask is a bit field indicating all threads that are
460 * currently harmless in that they promise not to access a shared resource.
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200461 * - threads_sync_mask is a bit field indicating that a thread waiting for
462 * others to finish wants to leave synchronized with others and as such
463 * promises to do so as well using thread_sync_release().
Willy Tarreau60b639c2018-08-02 10:16:17 +0200464 *
465 * For a given thread, its bits in want_rdv and harmless can be translated like
466 * this :
467 *
468 * ----------+----------+----------------------------------------------------
469 * want_rdv | harmless | description
470 * ----------+----------+----------------------------------------------------
471 * 0 | 0 | thread not interested in RDV, possibly harmful
472 * 0 | 1 | thread not interested in RDV but harmless
473 * 1 | 1 | thread interested in RDV and waiting for its turn
474 * 1 | 0 | thread currently working isolated from others
475 * ----------+----------+----------------------------------------------------
Willy Tarreau9a1f5732019-06-09 12:20:02 +0200476 *
477 * thread_sync_mask only delays the leaving of threads_sync_release() to make
478 * sure that each thread's harmless bit is cleared before leaving the function.
Willy Tarreau60b639c2018-08-02 10:16:17 +0200479 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200480
William Lallemand6e1796e2018-06-07 11:23:40 +0200481#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
482
Willy Tarreau0c026f42018-08-01 19:12:20 +0200483/* sets the thread ID and the TID bit for the current thread */
484static inline void ha_set_tid(unsigned int data)
485{
486 tid = data;
487 tid_bit = (1UL << tid);
David Carliera92c5ce2019-09-13 05:03:12 +0100488 ti = &ha_thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200489}
490
Willy Tarreauff64d3b2020-05-01 11:28:49 +0200491/* Retrieves the opaque pthread_t of thread <thr> cast to an unsigned long long
492 * since POSIX took great care of not specifying its representation, making it
493 * hard to export for post-mortem analysis. For this reason we copy it into a
494 * union and will use the smallest scalar type at least as large as its size,
495 * which will keep endianness and alignment for all regular sizes. As a last
496 * resort we end up with a long long ligned to the first bytes in memory, which
497 * will be endian-dependent if pthread_t is larger than a long long (not seen
498 * yet).
499 */
500static inline unsigned long long ha_get_pthread_id(unsigned int thr)
501{
502 union {
503 pthread_t t;
504 unsigned long long ll;
505 unsigned int i;
506 unsigned short s;
507 unsigned char c;
508 } u;
509
510 memset(&u, 0, sizeof(u));
511 u.t = ha_thread_info[thr].pthread;
512
513 if (sizeof(u.t) <= sizeof(u.c))
514 return u.c;
515 else if (sizeof(u.t) <= sizeof(u.s))
516 return u.s;
517 else if (sizeof(u.t) <= sizeof(u.i))
518 return u.i;
519 return u.ll;
520}
521
Willy Tarreau38171da2019-05-17 16:33:13 +0200522static inline void ha_thread_relax(void)
523{
524#if _POSIX_PRIORITY_SCHEDULING
525 sched_yield();
526#else
527 pl_cpu_relax();
528#endif
529}
530
Willy Tarreau60b639c2018-08-02 10:16:17 +0200531/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
532 * not be touching any unprotected shared resource during this period. Usually
533 * this is called before poll(), but it may also be placed around very slow
534 * calls (eg: some crypto operations). Needs to be terminated using
535 * thread_harmless_end().
536 */
537static inline void thread_harmless_now()
538{
539 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
540}
541
542/* Ends the harmless period started by thread_harmless_now(). Usually this is
543 * placed after the poll() call. If it is discovered that a job was running and
544 * is relying on the thread still being harmless, the thread waits for the
545 * other one to finish.
546 */
547static inline void thread_harmless_end()
548{
549 while (1) {
550 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
551 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
552 break;
553 thread_harmless_till_end();
554 }
555}
556
557/* an isolated thread has harmless cleared and want_rdv set */
558static inline unsigned long thread_isolated()
559{
560 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
561}
562
William Lallemand6e1796e2018-06-07 11:23:40 +0200563
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200564#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
565
Christopher Fauletf51bac22018-01-30 11:04:29 +0100566/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200567enum lock_label {
Emeric Brunc60def82017-09-27 14:59:38 +0200568 TASK_RQ_LOCK,
569 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200570 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200571 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200572 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200573 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200574 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200575 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200576 STK_TABLE_LOCK,
577 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200578 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200579 PEER_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200580 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200581 SSL_LOCK,
582 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200583 PATREF_LOCK,
584 PATEXP_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200585 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200586 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200587 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200588 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200589 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200590 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200591 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200592 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100593 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100594 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100595 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200596 LOGSRV_LOCK,
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200597 DICT_LOCK,
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200598 PROTO_LOCK,
William Lallemand150bfa82019-09-19 17:12:49 +0200599 CKCH_LOCK,
600 SNI_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000601 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200602 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200603};
604struct lock_stat {
605 uint64_t nsec_wait_for_write;
606 uint64_t nsec_wait_for_read;
607 uint64_t num_write_locked;
608 uint64_t num_write_unlocked;
609 uint64_t num_read_locked;
610 uint64_t num_read_unlocked;
611};
612
613extern struct lock_stat lock_stats[LOCK_LABELS];
614
615#define __HA_SPINLOCK_T unsigned long
616
617#define __SPIN_INIT(l) ({ (*l) = 0; })
618#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100619#define __SPIN_LOCK(l) pl_take_s(l)
620#define __SPIN_TRYLOCK(l) !pl_try_s(l)
621#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200622
623#define __HA_RWLOCK_T unsigned long
624
625#define __RWLOCK_INIT(l) ({ (*l) = 0; })
626#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
627#define __RWLOCK_WRLOCK(l) pl_take_w(l)
628#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
629#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
630#define __RWLOCK_RDLOCK(l) pl_take_r(l)
631#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
632#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
633
634#define HA_SPINLOCK_T struct ha_spinlock
635
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100636#define HA_SPIN_INIT(l) __spin_init(l)
637#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200638
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100639#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
640#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
641#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200642
643#define HA_RWLOCK_T struct ha_rwlock
644
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100645#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
646#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
647#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
648#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
649#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
650#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
651#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
652#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200653
654struct ha_spinlock {
655 __HA_SPINLOCK_T lock;
656 struct {
657 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
658 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
659 struct {
660 const char *function;
661 const char *file;
662 int line;
663 } last_location; /* location of the last owner */
664 } info;
665};
666
667struct ha_rwlock {
668 __HA_RWLOCK_T lock;
669 struct {
670 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
671 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
672 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
673 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
674 struct {
675 const char *function;
676 const char *file;
677 int line;
678 } last_location; /* location of the last write owner */
679 } info;
680};
681
Christopher Fauletf51bac22018-01-30 11:04:29 +0100682static inline const char *lock_label(enum lock_label label)
683{
684 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100685 case TASK_RQ_LOCK: return "TASK_RQ";
686 case TASK_WQ_LOCK: return "TASK_WQ";
687 case POOL_LOCK: return "POOL";
688 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100689 case PROXY_LOCK: return "PROXY";
690 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100691 case LBPRM_LOCK: return "LBPRM";
692 case SIGNALS_LOCK: return "SIGNALS";
693 case STK_TABLE_LOCK: return "STK_TABLE";
694 case STK_SESS_LOCK: return "STK_SESS";
695 case APPLETS_LOCK: return "APPLETS";
696 case PEER_LOCK: return "PEER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100697 case STRMS_LOCK: return "STRMS";
698 case SSL_LOCK: return "SSL";
699 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
700 case PATREF_LOCK: return "PATREF";
701 case PATEXP_LOCK: return "PATEXP";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100702 case VARS_LOCK: return "VARS";
703 case COMP_POOL_LOCK: return "COMP_POOL";
704 case LUA_LOCK: return "LUA";
705 case NOTIF_LOCK: return "NOTIF";
706 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
707 case DNS_LOCK: return "DNS";
708 case PID_LIST_LOCK: return "PID_LIST";
709 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
710 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100711 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100712 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200713 case LOGSRV_LOCK: return "LOGSRV";
Frédéric Lécaille4a3fef82019-05-28 14:47:17 +0200714 case DICT_LOCK: return "DICT";
Willy Tarreaud6e0c032019-07-25 07:53:56 +0200715 case PROTO_LOCK: return "PROTO";
William Lallemand150bfa82019-09-19 17:12:49 +0200716 case CKCH_LOCK: return "CKCH";
717 case SNI_LOCK: return "SNI";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000718 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100719 case LOCK_LABELS: break; /* keep compiler happy */
720 };
721 /* only way to come here is consecutive to an internal bug */
722 abort();
723}
724
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200725static inline void show_lock_stats()
726{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200727 int lbl;
728
729 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
730 fprintf(stderr,
731 "Stats about Lock %s: \n"
732 "\t # write lock : %lu\n"
733 "\t # write unlock: %lu (%ld)\n"
734 "\t # wait time for write : %.3f msec\n"
735 "\t # wait time for write/lock: %.3f nsec\n"
736 "\t # read lock : %lu\n"
737 "\t # read unlock : %lu (%ld)\n"
738 "\t # wait time for read : %.3f msec\n"
739 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100740 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200741 lock_stats[lbl].num_write_locked,
742 lock_stats[lbl].num_write_unlocked,
743 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
744 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
745 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
746 lock_stats[lbl].num_read_locked,
747 lock_stats[lbl].num_read_unlocked,
748 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
749 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
750 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
751 }
752}
753
754/* Following functions are used to collect some stats about locks. We wrap
755 * pthread functions to known how much time we wait in a lock. */
756
757static uint64_t nsec_now(void) {
758 struct timespec ts;
759
760 clock_gettime(CLOCK_MONOTONIC, &ts);
761 return ((uint64_t) ts.tv_sec * 1000000000ULL +
762 (uint64_t) ts.tv_nsec);
763}
764
765static inline void __ha_rwlock_init(struct ha_rwlock *l)
766{
767 memset(l, 0, sizeof(struct ha_rwlock));
768 __RWLOCK_INIT(&l->lock);
769}
770
771static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
772{
773 __RWLOCK_DESTROY(&l->lock);
774 memset(l, 0, sizeof(struct ha_rwlock));
775}
776
777
778static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
779 const char *func, const char *file, int line)
780{
781 uint64_t start_time;
782
783 if (unlikely(l->info.cur_writer & tid_bit)) {
784 /* the thread is already owning the lock for write */
785 abort();
786 }
787
788 if (unlikely(l->info.cur_readers & tid_bit)) {
789 /* the thread is already owning the lock for read */
790 abort();
791 }
792
793 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
794
795 start_time = nsec_now();
796 __RWLOCK_WRLOCK(&l->lock);
797 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
798
799 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
800
801 l->info.cur_writer = tid_bit;
802 l->info.last_location.function = func;
803 l->info.last_location.file = file;
804 l->info.last_location.line = line;
805
806 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
807}
808
809static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
810 const char *func, const char *file, int line)
811{
812 uint64_t start_time;
813 int r;
814
815 if (unlikely(l->info.cur_writer & tid_bit)) {
816 /* the thread is already owning the lock for write */
817 abort();
818 }
819
820 if (unlikely(l->info.cur_readers & tid_bit)) {
821 /* the thread is already owning the lock for read */
822 abort();
823 }
824
825 /* We set waiting writer because trywrlock could wait for readers to quit */
826 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
827
828 start_time = nsec_now();
829 r = __RWLOCK_TRYWRLOCK(&l->lock);
830 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
831 if (unlikely(r)) {
832 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
833 return r;
834 }
835 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
836
837 l->info.cur_writer = tid_bit;
838 l->info.last_location.function = func;
839 l->info.last_location.file = file;
840 l->info.last_location.line = line;
841
842 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
843
844 return 0;
845}
846
847static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
848 const char *func, const char *file, int line)
849{
850 if (unlikely(!(l->info.cur_writer & tid_bit))) {
851 /* the thread is not owning the lock for write */
852 abort();
853 }
854
855 l->info.cur_writer = 0;
856 l->info.last_location.function = func;
857 l->info.last_location.file = file;
858 l->info.last_location.line = line;
859
860 __RWLOCK_WRUNLOCK(&l->lock);
861
862 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
863}
864
865static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
866{
867 uint64_t start_time;
868
869 if (unlikely(l->info.cur_writer & tid_bit)) {
870 /* the thread is already owning the lock for write */
871 abort();
872 }
873
874 if (unlikely(l->info.cur_readers & tid_bit)) {
875 /* the thread is already owning the lock for read */
876 abort();
877 }
878
879 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
880
881 start_time = nsec_now();
882 __RWLOCK_RDLOCK(&l->lock);
883 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
884 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
885
886 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
887
888 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
889}
890
891static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
892{
893 int r;
894
895 if (unlikely(l->info.cur_writer & tid_bit)) {
896 /* the thread is already owning the lock for write */
897 abort();
898 }
899
900 if (unlikely(l->info.cur_readers & tid_bit)) {
901 /* the thread is already owning the lock for read */
902 abort();
903 }
904
905 /* try read should never wait */
906 r = __RWLOCK_TRYRDLOCK(&l->lock);
907 if (unlikely(r))
908 return r;
909 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
910
911 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
912
913 return 0;
914}
915
916static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
917{
918 if (unlikely(!(l->info.cur_readers & tid_bit))) {
919 /* the thread is not owning the lock for read */
920 abort();
921 }
922
923 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
924
925 __RWLOCK_RDUNLOCK(&l->lock);
926
927 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
928}
929
930static inline void __spin_init(struct ha_spinlock *l)
931{
932 memset(l, 0, sizeof(struct ha_spinlock));
933 __SPIN_INIT(&l->lock);
934}
935
936static inline void __spin_destroy(struct ha_spinlock *l)
937{
938 __SPIN_DESTROY(&l->lock);
939 memset(l, 0, sizeof(struct ha_spinlock));
940}
941
942static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
943 const char *func, const char *file, int line)
944{
945 uint64_t start_time;
946
947 if (unlikely(l->info.owner & tid_bit)) {
948 /* the thread is already owning the lock */
949 abort();
950 }
951
952 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
953
954 start_time = nsec_now();
955 __SPIN_LOCK(&l->lock);
956 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
957
958 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
959
960
961 l->info.owner = tid_bit;
962 l->info.last_location.function = func;
963 l->info.last_location.file = file;
964 l->info.last_location.line = line;
965
966 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
967}
968
969static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
970 const char *func, const char *file, int line)
971{
972 int r;
973
974 if (unlikely(l->info.owner & tid_bit)) {
975 /* the thread is already owning the lock */
976 abort();
977 }
978
979 /* try read should never wait */
980 r = __SPIN_TRYLOCK(&l->lock);
981 if (unlikely(r))
982 return r;
983 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
984
985 l->info.owner = tid_bit;
986 l->info.last_location.function = func;
987 l->info.last_location.file = file;
988 l->info.last_location.line = line;
989
990 return 0;
991}
992
993static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
994 const char *func, const char *file, int line)
995{
996 if (unlikely(!(l->info.owner & tid_bit))) {
997 /* the thread is not owning the lock */
998 abort();
999 }
1000
1001 l->info.owner = 0;
1002 l->info.last_location.function = func;
1003 l->info.last_location.file = file;
1004 l->info.last_location.line = line;
1005
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +01001006 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001007 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
1008}
1009
1010#else /* DEBUG_THREAD */
1011
1012#define HA_SPINLOCK_T unsigned long
1013
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001014#define HA_SPIN_INIT(l) ({ (*l) = 0; })
1015#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
1016#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
1017#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
1018#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001019
1020#define HA_RWLOCK_T unsigned long
1021
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
1023#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
1024#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
1025#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
1026#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
1027#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
1028#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
1029#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001030
1031#endif /* DEBUG_THREAD */
1032
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001033#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001034
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001035static __inline int
1036__ha_cas_dw(void *target, void *compare, const void *set)
1037{
1038 char ret;
1039
1040 __asm __volatile("lock cmpxchg16b %0; setz %3"
1041 : "+m" (*(void **)target),
1042 "=a" (((void **)compare)[0]),
1043 "=d" (((void **)compare)[1]),
1044 "=q" (ret)
1045 : "a" (((void **)compare)[0]),
1046 "d" (((void **)compare)[1]),
1047 "b" (((const void **)set)[0]),
1048 "c" (((const void **)set)[1])
1049 : "memory", "cc");
1050 return (ret);
1051}
1052
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001053/* Use __ha_barrier_atomic* when you're trying to protect data that are
1054 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1055 */
1056static __inline void
1057__ha_barrier_atomic_load(void)
1058{
1059 __asm __volatile("" ::: "memory");
1060}
1061
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001062static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001063__ha_barrier_atomic_store(void)
1064{
1065 __asm __volatile("" ::: "memory");
1066}
1067
1068static __inline void
1069__ha_barrier_atomic_full(void)
1070{
1071 __asm __volatile("" ::: "memory");
1072}
1073
1074static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001075__ha_barrier_load(void)
1076{
1077 __asm __volatile("lfence" ::: "memory");
1078}
1079
1080static __inline void
1081__ha_barrier_store(void)
1082{
1083 __asm __volatile("sfence" ::: "memory");
1084}
1085
1086static __inline void
1087__ha_barrier_full(void)
1088{
1089 __asm __volatile("mfence" ::: "memory");
1090}
1091
1092#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001093
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001094/* Use __ha_barrier_atomic* when you're trying to protect data that are
1095 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1096 */
1097static __inline void
1098__ha_barrier_atomic_load(void)
1099{
1100 __asm __volatile("dmb" ::: "memory");
1101}
1102
1103static __inline void
1104__ha_barrier_atomic_store(void)
1105{
1106 __asm __volatile("dsb" ::: "memory");
1107}
1108
1109static __inline void
1110__ha_barrier_atomic_full(void)
1111{
1112 __asm __volatile("dmb" ::: "memory");
1113}
1114
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001115static __inline void
1116__ha_barrier_load(void)
1117{
1118 __asm __volatile("dmb" ::: "memory");
1119}
1120
1121static __inline void
1122__ha_barrier_store(void)
1123{
1124 __asm __volatile("dsb" ::: "memory");
1125}
1126
1127static __inline void
1128__ha_barrier_full(void)
1129{
1130 __asm __volatile("dmb" ::: "memory");
1131}
1132
Willy Tarreau41ccb192018-02-14 14:16:28 +01001133static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001134{
1135 uint64_t previous;
1136 int tmp;
1137
1138 __asm __volatile("1:"
1139 "ldrexd %0, [%4];"
1140 "cmp %Q0, %Q2;"
1141 "ittt eq;"
1142 "cmpeq %R0, %R2;"
1143 "strexdeq %1, %3, [%4];"
1144 "cmpeq %1, #1;"
1145 "beq 1b;"
1146 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001147 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001148 : "memory", "cc");
1149 tmp = (previous == *(uint64_t *)compare);
1150 *(uint64_t *)compare = previous;
1151 return (tmp);
1152}
1153
1154#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001155
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001156/* Use __ha_barrier_atomic* when you're trying to protect data that are
1157 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1158 */
1159static __inline void
1160__ha_barrier_atomic_load(void)
1161{
1162 __asm __volatile("dmb ishld" ::: "memory");
1163}
1164
1165static __inline void
1166__ha_barrier_atomic_store(void)
1167{
1168 __asm __volatile("dmb ishst" ::: "memory");
1169}
1170
1171static __inline void
1172__ha_barrier_atomic_full(void)
1173{
1174 __asm __volatile("dmb ish" ::: "memory");
1175}
1176
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001177static __inline void
1178__ha_barrier_load(void)
1179{
1180 __asm __volatile("dmb ishld" ::: "memory");
1181}
1182
1183static __inline void
1184__ha_barrier_store(void)
1185{
1186 __asm __volatile("dmb ishst" ::: "memory");
1187}
1188
1189static __inline void
1190__ha_barrier_full(void)
1191{
1192 __asm __volatile("dmb ish" ::: "memory");
1193}
1194
1195static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1196{
1197 void *value[2];
1198 uint64_t tmp1, tmp2;
1199
1200 __asm__ __volatile__("1:"
1201 "ldxp %0, %1, [%4];"
1202 "mov %2, %0;"
1203 "mov %3, %1;"
1204 "eor %0, %0, %5;"
1205 "eor %1, %1, %6;"
1206 "orr %1, %0, %1;"
1207 "mov %w0, #0;"
1208 "cbnz %1, 2f;"
1209 "stxp %w0, %7, %8, [%4];"
1210 "cbnz %w0, 1b;"
1211 "mov %w0, #1;"
1212 "2:"
1213 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1214 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1215 : "cc", "memory");
1216
1217 memcpy(compare, &value, sizeof(value));
1218 return (tmp1);
1219}
1220
1221#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001222#define __ha_barrier_atomic_load __sync_synchronize
1223#define __ha_barrier_atomic_store __sync_synchronize
1224#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001225#define __ha_barrier_load __sync_synchronize
1226#define __ha_barrier_store __sync_synchronize
1227#define __ha_barrier_full __sync_synchronize
1228#endif
1229
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001230void ha_spin_init(HA_SPINLOCK_T *l);
1231void ha_rwlock_init(HA_RWLOCK_T *l);
1232
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001233#endif /* USE_THREAD */
1234
Willy Tarreau149ab772019-01-26 14:27:06 +01001235extern int thread_cpus_enabled_at_boot;
1236
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001237static inline void __ha_compiler_barrier(void)
1238{
1239 __asm __volatile("" ::: "memory");
1240}
1241
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001242int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001243int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001244
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001245#ifndef _HA_ATOMIC_CAS
1246#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1247#endif /* !_HA_ATOMIC_CAS */
1248
Willy Tarreau6a38b322019-05-11 18:04:24 +02001249#ifndef _HA_ATOMIC_DWCAS
1250#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1251#endif /* !_HA_ATOMIC_CAS */
1252
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001253#ifndef _HA_ATOMIC_ADD
1254#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1255#endif /* !_HA_ATOMIC_ADD */
1256
1257#ifndef _HA_ATOMIC_XADD
1258#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1259#endif /* !_HA_ATOMIC_SUB */
1260
1261#ifndef _HA_ATOMIC_SUB
1262#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1263#endif /* !_HA_ATOMIC_SUB */
1264
1265#ifndef _HA_ATOMIC_AND
1266#define _HA_ATOMIC_AND HA_ATOMIC_AND
1267#endif /* !_HA_ATOMIC_AND */
1268
1269#ifndef _HA_ATOMIC_OR
1270#define _HA_ATOMIC_OR HA_ATOMIC_OR
1271#endif /* !_HA_ATOMIC_OR */
1272
1273#ifndef _HA_ATOMIC_XCHG
1274#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1275#endif /* !_HA_ATOMIC_XCHG */
1276
1277#ifndef _HA_ATOMIC_STORE
1278#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1279#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001280
1281#ifndef _HA_ATOMIC_LOAD
1282#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1283#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001284#endif /* _COMMON_HATHREADS_H */