blob: 32099094c0ddc6a38a1b87529eebac16ed0af4cc [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
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020041/* thread info flags, for thread_info[].flags */
42#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 Tarreau0c026f42018-08-01 19:12:20 +020056enum { tid_bit = 1UL };
57enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010058
Willy Tarreau5a6e2242019-05-20 18:56:48 +020059extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020060 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +020061 uint64_t prev_cpu_time; /* previous per thread CPU time */
62 uint64_t prev_mono_time; /* previous system wide monotonic time */
63 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020064 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020065 /* pad to cache line (64B) */
66 char __pad[0]; /* unused except to check remaining room */
67 char __end[0] __attribute__((aligned(64)));
68} thread_info[MAX_THREADS];
69
Willy Tarreau8323a372019-05-20 18:57:53 +020070extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
71
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010072#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010073#define __decl_spinlock(lock)
74#define __decl_aligned_spinlock(lock)
75#define __decl_rwlock(lock)
76#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010077
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020078#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020079#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020080#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
81#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020082#define HA_ATOMIC_XADD(val, i) \
83 ({ \
84 typeof((val)) __p_xadd = (val); \
85 typeof(*(val)) __old_xadd = *__p_xadd; \
86 *__p_xadd += i; \
87 __old_xadd; \
88 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020089#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
90#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
91#define HA_ATOMIC_XCHG(val, new) \
92 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020093 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020094 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020095 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020096 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010097#define HA_ATOMIC_BTS(val, bit) \
98 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020099 typeof((val)) __p_bts = (val); \
100 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
101 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
102 if (!__t_bts) \
103 *__p_bts |= __b_bts; \
104 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100105 })
106#define HA_ATOMIC_BTR(val, bit) \
107 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200108 typeof((val)) __p_btr = (val); \
109 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
110 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
111 if (__t_btr) \
112 *__p_btr &= ~__b_btr; \
113 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100114 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200115#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
117#define HA_ATOMIC_UPDATE_MAX(val, new) \
118 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200119 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200120 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200121 if (*(val) < __new_max) \
122 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200123 *(val); \
124 })
125
126#define HA_ATOMIC_UPDATE_MIN(val, new) \
127 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200128 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200129 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200130 if (*(val) > __new_min) \
131 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200132 *(val); \
133 })
134
Willy Tarreaub29dc952017-10-31 18:00:20 +0100135#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200136
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100137#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
138#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
139#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
140#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
141#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200142
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100143#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
144#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
145#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
146#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
147#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
148#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
149#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
150#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200151
William Lallemand6e1796e2018-06-07 11:23:40 +0200152#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
153
Willy Tarreau0c026f42018-08-01 19:12:20 +0200154static inline void ha_set_tid(unsigned int tid)
155{
Willy Tarreau8323a372019-05-20 18:57:53 +0200156 ti = &thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200157}
158
159static inline void ha_thread_relax(void)
160{
161#if _POSIX_PRIORITY_SCHEDULING
162 sched_yield();
163#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200164}
William Lallemand6e1796e2018-06-07 11:23:40 +0200165
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200166/* send signal <sig> to thread <thr> */
167static inline void ha_tkill(unsigned int thr, int sig)
168{
169 raise(sig);
170}
171
172/* send signal <sig> to all threads */
173static inline void ha_tkillall(int sig)
174{
175 raise(sig);
176}
177
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100178static inline void __ha_barrier_atomic_load(void)
179{
180}
181
182static inline void __ha_barrier_atomic_store(void)
183{
184}
185
186static inline void __ha_barrier_atomic_full(void)
187{
188}
189
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100190static inline void __ha_barrier_load(void)
191{
192}
193
194static inline void __ha_barrier_store(void)
195{
196}
197
198static inline void __ha_barrier_full(void)
199{
200}
201
Willy Tarreau60b639c2018-08-02 10:16:17 +0200202static inline void thread_harmless_now()
203{
204}
205
206static inline void thread_harmless_end()
207{
208}
209
210static inline void thread_isolate()
211{
212}
213
214static inline void thread_release()
215{
216}
217
218static inline unsigned long thread_isolated()
219{
220 return 1;
221}
222
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200223#else /* USE_THREAD */
224
225#include <stdio.h>
226#include <stdlib.h>
227#include <string.h>
228#include <pthread.h>
229#include <import/plock.h>
230
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100231#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100232#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100233#endif
234
235#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100236
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100237#define __decl_hathreads(decl) decl
238
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100239/* declare a self-initializing spinlock */
240#define __decl_spinlock(lock) \
241 HA_SPINLOCK_T (lock); \
242 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
243
244/* declare a self-initializing spinlock, aligned on a cache line */
245#define __decl_aligned_spinlock(lock) \
246 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
247 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
248
249/* declare a self-initializing rwlock */
250#define __decl_rwlock(lock) \
251 HA_RWLOCK_T (lock); \
252 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
253
254/* declare a self-initializing rwlock, aligned on a cache line */
255#define __decl_aligned_rwlock(lock) \
256 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
257 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
258
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200259/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
260 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100261
David Carlierec5e8452018-01-11 14:20:43 +0000262#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100263/* gcc < 4.7 */
264
265#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
266#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200267#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100268#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
269#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
270
271/* the CAS is a bit complicated. The older API doesn't support returning the
272 * value and the swap's result at the same time. So here we take what looks
273 * like the safest route, consisting in using the boolean version guaranteeing
274 * that the operation was performed or not, and we snoop a previous value. If
275 * the compare succeeds, we return. If it fails, we return the previous value,
276 * but only if it differs from the expected one. If it's the same it's a race
277 * thus we try again to avoid confusing a possibly sensitive caller.
278 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200279#define HA_ATOMIC_CAS(val, old, new) \
280 ({ \
281 typeof((val)) __val_cas = (val); \
282 typeof((old)) __oldp_cas = (old); \
283 typeof(*(old)) __oldv_cas; \
284 typeof((new)) __new_cas = (new); \
285 int __ret_cas; \
286 do { \
287 __oldv_cas = *__val_cas; \
288 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
289 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
290 if (!__ret_cas) \
291 *__oldp_cas = __oldv_cas; \
292 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100293 })
294
Willy Tarreau6a38b322019-05-11 18:04:24 +0200295#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
296
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200297#define HA_ATOMIC_XCHG(val, new) \
298 ({ \
299 typeof((val)) __val_xchg = (val); \
300 typeof(*(val)) __old_xchg; \
301 typeof((new)) __new_xchg = (new); \
302 do { __old_xchg = *__val_xchg; \
303 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
304 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100305 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100306
307#define HA_ATOMIC_BTS(val, bit) \
308 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200309 typeof(*(val)) __b_bts = (1UL << (bit)); \
310 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100311 })
312
313#define HA_ATOMIC_BTR(val, bit) \
314 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200315 typeof(*(val)) __b_btr = (1UL << (bit)); \
316 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100317 })
318
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200319#define HA_ATOMIC_LOAD(val) \
320 ({ \
321 typeof(*(val)) ret; \
322 __sync_synchronize(); \
323 ret = *(volatile typeof(val))val; \
324 __sync_synchronize(); \
325 ret; \
326 })
327
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200328#define HA_ATOMIC_STORE(val, new) \
329 ({ \
330 typeof((val)) __val_store = (val); \
331 typeof(*(val)) __old_store; \
332 typeof((new)) __new_store = (new); \
333 do { __old_store = *__val_store; \
334 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100335 })
336#else
337/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100338#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
Willy Tarreau6a38b322019-05-11 18:04:24 +0200339#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100340#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
341#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
342#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
343#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
344#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100345#define HA_ATOMIC_BTS(val, bit) \
346 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200347 typeof(*(val)) __b_bts = (1UL << (bit)); \
348 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100349 })
350
351#define HA_ATOMIC_BTR(val, bit) \
352 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200353 typeof(*(val)) __b_btr = (1UL << (bit)); \
354 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100355 })
356
Olivier Houchard11353792019-03-07 18:48:22 +0100357#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
358#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200359#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200360
361/* Variants that don't generate any memory barrier.
362 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
363 * that will always generate correct code.
364 * Usually it's fine to use those when updating data that have no dependency,
365 * ie updating a counter. Otherwise a barrier is required.
366 */
367#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
Willy Tarreau6a38b322019-05-11 18:04:24 +0200368#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200369#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
370#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
371#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
372#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
373#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
374#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
375#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200376#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200377
378#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100379
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200380#define HA_ATOMIC_UPDATE_MAX(val, new) \
381 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200382 typeof(*(val)) __old_max = *(val); \
383 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200384 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200385 while (__old_max < __new_max && \
386 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
387 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200388 })
389#define HA_ATOMIC_UPDATE_MIN(val, new) \
390 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200391 typeof(*(val)) __old_min = *(val); \
392 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200393 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200394 while (__old_min > __new_min && \
395 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
396 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200397 })
398
Willy Tarreaub29dc952017-10-31 18:00:20 +0100399#define HA_BARRIER() pl_barrier()
400
Willy Tarreau60b639c2018-08-02 10:16:17 +0200401void thread_harmless_till_end();
402void thread_isolate();
403void thread_release();
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200404void ha_tkill(unsigned int thr, int sig);
405void ha_tkillall(int sig);
Christopher Faulet339fff82017-10-19 11:59:15 +0200406
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200407extern struct thread_info {
408 pthread_t pthread;
409 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +0200410 uint64_t prev_cpu_time; /* previous per thread CPU time */
411 uint64_t prev_mono_time; /* previous system wide monotonic time */
412 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200413 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200414 /* pad to cache line (64B) */
415 char __pad[0]; /* unused except to check remaining room */
416 char __end[0] __attribute__((aligned(64)));
417} thread_info[MAX_THREADS];
418
Willy Tarreau0c026f42018-08-01 19:12:20 +0200419extern THREAD_LOCAL unsigned int tid; /* The thread id */
420extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200421extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200422extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200423extern volatile unsigned long threads_want_rdv_mask;
424extern volatile unsigned long threads_harmless_mask;
425
426/* explanation for threads_want_rdv_mask and threads_harmless_mask :
427 * - threads_want_rdv_mask is a bit field indicating all threads that have
428 * requested a rendez-vous of other threads using thread_isolate().
429 * - threads_harmless_mask is a bit field indicating all threads that are
430 * currently harmless in that they promise not to access a shared resource.
431 *
432 * For a given thread, its bits in want_rdv and harmless can be translated like
433 * this :
434 *
435 * ----------+----------+----------------------------------------------------
436 * want_rdv | harmless | description
437 * ----------+----------+----------------------------------------------------
438 * 0 | 0 | thread not interested in RDV, possibly harmful
439 * 0 | 1 | thread not interested in RDV but harmless
440 * 1 | 1 | thread interested in RDV and waiting for its turn
441 * 1 | 0 | thread currently working isolated from others
442 * ----------+----------+----------------------------------------------------
443 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200444
William Lallemand6e1796e2018-06-07 11:23:40 +0200445#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
446
Willy Tarreau0c026f42018-08-01 19:12:20 +0200447/* sets the thread ID and the TID bit for the current thread */
448static inline void ha_set_tid(unsigned int data)
449{
450 tid = data;
451 tid_bit = (1UL << tid);
Willy Tarreau8323a372019-05-20 18:57:53 +0200452 ti = &thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200453}
454
Willy Tarreau38171da2019-05-17 16:33:13 +0200455static inline void ha_thread_relax(void)
456{
457#if _POSIX_PRIORITY_SCHEDULING
458 sched_yield();
459#else
460 pl_cpu_relax();
461#endif
462}
463
Willy Tarreau60b639c2018-08-02 10:16:17 +0200464/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
465 * not be touching any unprotected shared resource during this period. Usually
466 * this is called before poll(), but it may also be placed around very slow
467 * calls (eg: some crypto operations). Needs to be terminated using
468 * thread_harmless_end().
469 */
470static inline void thread_harmless_now()
471{
472 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
473}
474
475/* Ends the harmless period started by thread_harmless_now(). Usually this is
476 * placed after the poll() call. If it is discovered that a job was running and
477 * is relying on the thread still being harmless, the thread waits for the
478 * other one to finish.
479 */
480static inline void thread_harmless_end()
481{
482 while (1) {
483 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
484 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
485 break;
486 thread_harmless_till_end();
487 }
488}
489
490/* an isolated thread has harmless cleared and want_rdv set */
491static inline unsigned long thread_isolated()
492{
493 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
494}
495
William Lallemand6e1796e2018-06-07 11:23:40 +0200496
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200497#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
498
Christopher Fauletf51bac22018-01-30 11:04:29 +0100499/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200500enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200501 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200502 TASK_RQ_LOCK,
503 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200504 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200505 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200506 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200507 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200508 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200509 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200510 STK_TABLE_LOCK,
511 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200512 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200513 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200514 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200515 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200516 SSL_LOCK,
517 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200518 PATREF_LOCK,
519 PATEXP_LOCK,
520 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200521 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200522 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200523 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200524 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200525 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200526 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200527 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200528 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100529 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100530 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100531 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200532 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000533 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200534 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200535};
536struct lock_stat {
537 uint64_t nsec_wait_for_write;
538 uint64_t nsec_wait_for_read;
539 uint64_t num_write_locked;
540 uint64_t num_write_unlocked;
541 uint64_t num_read_locked;
542 uint64_t num_read_unlocked;
543};
544
545extern struct lock_stat lock_stats[LOCK_LABELS];
546
547#define __HA_SPINLOCK_T unsigned long
548
549#define __SPIN_INIT(l) ({ (*l) = 0; })
550#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100551#define __SPIN_LOCK(l) pl_take_s(l)
552#define __SPIN_TRYLOCK(l) !pl_try_s(l)
553#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200554
555#define __HA_RWLOCK_T unsigned long
556
557#define __RWLOCK_INIT(l) ({ (*l) = 0; })
558#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
559#define __RWLOCK_WRLOCK(l) pl_take_w(l)
560#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
561#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
562#define __RWLOCK_RDLOCK(l) pl_take_r(l)
563#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
564#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
565
566#define HA_SPINLOCK_T struct ha_spinlock
567
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100568#define HA_SPIN_INIT(l) __spin_init(l)
569#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200570
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100571#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
572#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
573#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200574
575#define HA_RWLOCK_T struct ha_rwlock
576
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100577#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
578#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
579#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
580#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
581#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
582#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
583#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
584#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200585
586struct ha_spinlock {
587 __HA_SPINLOCK_T lock;
588 struct {
589 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
590 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
591 struct {
592 const char *function;
593 const char *file;
594 int line;
595 } last_location; /* location of the last owner */
596 } info;
597};
598
599struct ha_rwlock {
600 __HA_RWLOCK_T lock;
601 struct {
602 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
603 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
604 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
605 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
606 struct {
607 const char *function;
608 const char *file;
609 int line;
610 } last_location; /* location of the last write owner */
611 } info;
612};
613
Christopher Fauletf51bac22018-01-30 11:04:29 +0100614static inline const char *lock_label(enum lock_label label)
615{
616 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100617 case FD_LOCK: return "FD";
618 case TASK_RQ_LOCK: return "TASK_RQ";
619 case TASK_WQ_LOCK: return "TASK_WQ";
620 case POOL_LOCK: return "POOL";
621 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100622 case PROXY_LOCK: return "PROXY";
623 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100624 case LBPRM_LOCK: return "LBPRM";
625 case SIGNALS_LOCK: return "SIGNALS";
626 case STK_TABLE_LOCK: return "STK_TABLE";
627 case STK_SESS_LOCK: return "STK_SESS";
628 case APPLETS_LOCK: return "APPLETS";
629 case PEER_LOCK: return "PEER";
630 case BUF_WQ_LOCK: return "BUF_WQ";
631 case STRMS_LOCK: return "STRMS";
632 case SSL_LOCK: return "SSL";
633 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
634 case PATREF_LOCK: return "PATREF";
635 case PATEXP_LOCK: return "PATEXP";
636 case PATLRU_LOCK: return "PATLRU";
637 case VARS_LOCK: return "VARS";
638 case COMP_POOL_LOCK: return "COMP_POOL";
639 case LUA_LOCK: return "LUA";
640 case NOTIF_LOCK: return "NOTIF";
641 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
642 case DNS_LOCK: return "DNS";
643 case PID_LIST_LOCK: return "PID_LIST";
644 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
645 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100646 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100647 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200648 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000649 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100650 case LOCK_LABELS: break; /* keep compiler happy */
651 };
652 /* only way to come here is consecutive to an internal bug */
653 abort();
654}
655
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200656static inline void show_lock_stats()
657{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200658 int lbl;
659
660 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
661 fprintf(stderr,
662 "Stats about Lock %s: \n"
663 "\t # write lock : %lu\n"
664 "\t # write unlock: %lu (%ld)\n"
665 "\t # wait time for write : %.3f msec\n"
666 "\t # wait time for write/lock: %.3f nsec\n"
667 "\t # read lock : %lu\n"
668 "\t # read unlock : %lu (%ld)\n"
669 "\t # wait time for read : %.3f msec\n"
670 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100671 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200672 lock_stats[lbl].num_write_locked,
673 lock_stats[lbl].num_write_unlocked,
674 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
675 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
676 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
677 lock_stats[lbl].num_read_locked,
678 lock_stats[lbl].num_read_unlocked,
679 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
680 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
681 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
682 }
683}
684
685/* Following functions are used to collect some stats about locks. We wrap
686 * pthread functions to known how much time we wait in a lock. */
687
688static uint64_t nsec_now(void) {
689 struct timespec ts;
690
691 clock_gettime(CLOCK_MONOTONIC, &ts);
692 return ((uint64_t) ts.tv_sec * 1000000000ULL +
693 (uint64_t) ts.tv_nsec);
694}
695
696static inline void __ha_rwlock_init(struct ha_rwlock *l)
697{
698 memset(l, 0, sizeof(struct ha_rwlock));
699 __RWLOCK_INIT(&l->lock);
700}
701
702static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
703{
704 __RWLOCK_DESTROY(&l->lock);
705 memset(l, 0, sizeof(struct ha_rwlock));
706}
707
708
709static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
710 const char *func, const char *file, int line)
711{
712 uint64_t start_time;
713
714 if (unlikely(l->info.cur_writer & tid_bit)) {
715 /* the thread is already owning the lock for write */
716 abort();
717 }
718
719 if (unlikely(l->info.cur_readers & tid_bit)) {
720 /* the thread is already owning the lock for read */
721 abort();
722 }
723
724 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
725
726 start_time = nsec_now();
727 __RWLOCK_WRLOCK(&l->lock);
728 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
729
730 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
731
732 l->info.cur_writer = tid_bit;
733 l->info.last_location.function = func;
734 l->info.last_location.file = file;
735 l->info.last_location.line = line;
736
737 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
738}
739
740static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
741 const char *func, const char *file, int line)
742{
743 uint64_t start_time;
744 int r;
745
746 if (unlikely(l->info.cur_writer & tid_bit)) {
747 /* the thread is already owning the lock for write */
748 abort();
749 }
750
751 if (unlikely(l->info.cur_readers & tid_bit)) {
752 /* the thread is already owning the lock for read */
753 abort();
754 }
755
756 /* We set waiting writer because trywrlock could wait for readers to quit */
757 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
758
759 start_time = nsec_now();
760 r = __RWLOCK_TRYWRLOCK(&l->lock);
761 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
762 if (unlikely(r)) {
763 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
764 return r;
765 }
766 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
767
768 l->info.cur_writer = tid_bit;
769 l->info.last_location.function = func;
770 l->info.last_location.file = file;
771 l->info.last_location.line = line;
772
773 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
774
775 return 0;
776}
777
778static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
779 const char *func, const char *file, int line)
780{
781 if (unlikely(!(l->info.cur_writer & tid_bit))) {
782 /* the thread is not owning the lock for write */
783 abort();
784 }
785
786 l->info.cur_writer = 0;
787 l->info.last_location.function = func;
788 l->info.last_location.file = file;
789 l->info.last_location.line = line;
790
791 __RWLOCK_WRUNLOCK(&l->lock);
792
793 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
794}
795
796static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
797{
798 uint64_t start_time;
799
800 if (unlikely(l->info.cur_writer & tid_bit)) {
801 /* the thread is already owning the lock for write */
802 abort();
803 }
804
805 if (unlikely(l->info.cur_readers & tid_bit)) {
806 /* the thread is already owning the lock for read */
807 abort();
808 }
809
810 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
811
812 start_time = nsec_now();
813 __RWLOCK_RDLOCK(&l->lock);
814 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
815 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
816
817 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
818
819 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
820}
821
822static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
823{
824 int r;
825
826 if (unlikely(l->info.cur_writer & tid_bit)) {
827 /* the thread is already owning the lock for write */
828 abort();
829 }
830
831 if (unlikely(l->info.cur_readers & tid_bit)) {
832 /* the thread is already owning the lock for read */
833 abort();
834 }
835
836 /* try read should never wait */
837 r = __RWLOCK_TRYRDLOCK(&l->lock);
838 if (unlikely(r))
839 return r;
840 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
841
842 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
843
844 return 0;
845}
846
847static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
848{
849 if (unlikely(!(l->info.cur_readers & tid_bit))) {
850 /* the thread is not owning the lock for read */
851 abort();
852 }
853
854 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
855
856 __RWLOCK_RDUNLOCK(&l->lock);
857
858 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
859}
860
861static inline void __spin_init(struct ha_spinlock *l)
862{
863 memset(l, 0, sizeof(struct ha_spinlock));
864 __SPIN_INIT(&l->lock);
865}
866
867static inline void __spin_destroy(struct ha_spinlock *l)
868{
869 __SPIN_DESTROY(&l->lock);
870 memset(l, 0, sizeof(struct ha_spinlock));
871}
872
873static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
874 const char *func, const char *file, int line)
875{
876 uint64_t start_time;
877
878 if (unlikely(l->info.owner & tid_bit)) {
879 /* the thread is already owning the lock */
880 abort();
881 }
882
883 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
884
885 start_time = nsec_now();
886 __SPIN_LOCK(&l->lock);
887 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
888
889 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
890
891
892 l->info.owner = tid_bit;
893 l->info.last_location.function = func;
894 l->info.last_location.file = file;
895 l->info.last_location.line = line;
896
897 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
898}
899
900static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
901 const char *func, const char *file, int line)
902{
903 int r;
904
905 if (unlikely(l->info.owner & tid_bit)) {
906 /* the thread is already owning the lock */
907 abort();
908 }
909
910 /* try read should never wait */
911 r = __SPIN_TRYLOCK(&l->lock);
912 if (unlikely(r))
913 return r;
914 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
915
916 l->info.owner = tid_bit;
917 l->info.last_location.function = func;
918 l->info.last_location.file = file;
919 l->info.last_location.line = line;
920
921 return 0;
922}
923
924static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
925 const char *func, const char *file, int line)
926{
927 if (unlikely(!(l->info.owner & tid_bit))) {
928 /* the thread is not owning the lock */
929 abort();
930 }
931
932 l->info.owner = 0;
933 l->info.last_location.function = func;
934 l->info.last_location.file = file;
935 l->info.last_location.line = line;
936
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100937 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200938 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
939}
940
941#else /* DEBUG_THREAD */
942
943#define HA_SPINLOCK_T unsigned long
944
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100945#define HA_SPIN_INIT(l) ({ (*l) = 0; })
946#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
947#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
948#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
949#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200950
951#define HA_RWLOCK_T unsigned long
952
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100953#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
954#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
955#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
956#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
957#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
958#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
959#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
960#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200961
962#endif /* DEBUG_THREAD */
963
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100964#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200965
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100966static __inline int
967__ha_cas_dw(void *target, void *compare, const void *set)
968{
969 char ret;
970
971 __asm __volatile("lock cmpxchg16b %0; setz %3"
972 : "+m" (*(void **)target),
973 "=a" (((void **)compare)[0]),
974 "=d" (((void **)compare)[1]),
975 "=q" (ret)
976 : "a" (((void **)compare)[0]),
977 "d" (((void **)compare)[1]),
978 "b" (((const void **)set)[0]),
979 "c" (((const void **)set)[1])
980 : "memory", "cc");
981 return (ret);
982}
983
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100984/* Use __ha_barrier_atomic* when you're trying to protect data that are
985 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
986 */
987static __inline void
988__ha_barrier_atomic_load(void)
989{
990 __asm __volatile("" ::: "memory");
991}
992
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100993static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100994__ha_barrier_atomic_store(void)
995{
996 __asm __volatile("" ::: "memory");
997}
998
999static __inline void
1000__ha_barrier_atomic_full(void)
1001{
1002 __asm __volatile("" ::: "memory");
1003}
1004
1005static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001006__ha_barrier_load(void)
1007{
1008 __asm __volatile("lfence" ::: "memory");
1009}
1010
1011static __inline void
1012__ha_barrier_store(void)
1013{
1014 __asm __volatile("sfence" ::: "memory");
1015}
1016
1017static __inline void
1018__ha_barrier_full(void)
1019{
1020 __asm __volatile("mfence" ::: "memory");
1021}
1022
1023#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001024
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001025/* Use __ha_barrier_atomic* when you're trying to protect data that are
1026 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1027 */
1028static __inline void
1029__ha_barrier_atomic_load(void)
1030{
1031 __asm __volatile("dmb" ::: "memory");
1032}
1033
1034static __inline void
1035__ha_barrier_atomic_store(void)
1036{
1037 __asm __volatile("dsb" ::: "memory");
1038}
1039
1040static __inline void
1041__ha_barrier_atomic_full(void)
1042{
1043 __asm __volatile("dmb" ::: "memory");
1044}
1045
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001046static __inline void
1047__ha_barrier_load(void)
1048{
1049 __asm __volatile("dmb" ::: "memory");
1050}
1051
1052static __inline void
1053__ha_barrier_store(void)
1054{
1055 __asm __volatile("dsb" ::: "memory");
1056}
1057
1058static __inline void
1059__ha_barrier_full(void)
1060{
1061 __asm __volatile("dmb" ::: "memory");
1062}
1063
Willy Tarreau41ccb192018-02-14 14:16:28 +01001064static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001065{
1066 uint64_t previous;
1067 int tmp;
1068
1069 __asm __volatile("1:"
1070 "ldrexd %0, [%4];"
1071 "cmp %Q0, %Q2;"
1072 "ittt eq;"
1073 "cmpeq %R0, %R2;"
1074 "strexdeq %1, %3, [%4];"
1075 "cmpeq %1, #1;"
1076 "beq 1b;"
1077 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001078 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001079 : "memory", "cc");
1080 tmp = (previous == *(uint64_t *)compare);
1081 *(uint64_t *)compare = previous;
1082 return (tmp);
1083}
1084
1085#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001086
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001087/* Use __ha_barrier_atomic* when you're trying to protect data that are
1088 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1089 */
1090static __inline void
1091__ha_barrier_atomic_load(void)
1092{
1093 __asm __volatile("dmb ishld" ::: "memory");
1094}
1095
1096static __inline void
1097__ha_barrier_atomic_store(void)
1098{
1099 __asm __volatile("dmb ishst" ::: "memory");
1100}
1101
1102static __inline void
1103__ha_barrier_atomic_full(void)
1104{
1105 __asm __volatile("dmb ish" ::: "memory");
1106}
1107
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001108static __inline void
1109__ha_barrier_load(void)
1110{
1111 __asm __volatile("dmb ishld" ::: "memory");
1112}
1113
1114static __inline void
1115__ha_barrier_store(void)
1116{
1117 __asm __volatile("dmb ishst" ::: "memory");
1118}
1119
1120static __inline void
1121__ha_barrier_full(void)
1122{
1123 __asm __volatile("dmb ish" ::: "memory");
1124}
1125
1126static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1127{
1128 void *value[2];
1129 uint64_t tmp1, tmp2;
1130
1131 __asm__ __volatile__("1:"
1132 "ldxp %0, %1, [%4];"
1133 "mov %2, %0;"
1134 "mov %3, %1;"
1135 "eor %0, %0, %5;"
1136 "eor %1, %1, %6;"
1137 "orr %1, %0, %1;"
1138 "mov %w0, #0;"
1139 "cbnz %1, 2f;"
1140 "stxp %w0, %7, %8, [%4];"
1141 "cbnz %w0, 1b;"
1142 "mov %w0, #1;"
1143 "2:"
1144 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1145 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1146 : "cc", "memory");
1147
1148 memcpy(compare, &value, sizeof(value));
1149 return (tmp1);
1150}
1151
1152#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001153#define __ha_barrier_atomic_load __sync_synchronize
1154#define __ha_barrier_atomic_store __sync_synchronize
1155#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001156#define __ha_barrier_load __sync_synchronize
1157#define __ha_barrier_store __sync_synchronize
1158#define __ha_barrier_full __sync_synchronize
1159#endif
1160
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001161void ha_spin_init(HA_SPINLOCK_T *l);
1162void ha_rwlock_init(HA_RWLOCK_T *l);
1163
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001164#endif /* USE_THREAD */
1165
Willy Tarreau149ab772019-01-26 14:27:06 +01001166extern int thread_cpus_enabled_at_boot;
1167
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001168static inline void __ha_compiler_barrier(void)
1169{
1170 __asm __volatile("" ::: "memory");
1171}
1172
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001173int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001174int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001175
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001176#ifndef _HA_ATOMIC_CAS
1177#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1178#endif /* !_HA_ATOMIC_CAS */
1179
Willy Tarreau6a38b322019-05-11 18:04:24 +02001180#ifndef _HA_ATOMIC_DWCAS
1181#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1182#endif /* !_HA_ATOMIC_CAS */
1183
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001184#ifndef _HA_ATOMIC_ADD
1185#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1186#endif /* !_HA_ATOMIC_ADD */
1187
1188#ifndef _HA_ATOMIC_XADD
1189#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1190#endif /* !_HA_ATOMIC_SUB */
1191
1192#ifndef _HA_ATOMIC_SUB
1193#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1194#endif /* !_HA_ATOMIC_SUB */
1195
1196#ifndef _HA_ATOMIC_AND
1197#define _HA_ATOMIC_AND HA_ATOMIC_AND
1198#endif /* !_HA_ATOMIC_AND */
1199
1200#ifndef _HA_ATOMIC_OR
1201#define _HA_ATOMIC_OR HA_ATOMIC_OR
1202#endif /* !_HA_ATOMIC_OR */
1203
1204#ifndef _HA_ATOMIC_XCHG
1205#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1206#endif /* !_HA_ATOMIC_XCHG */
1207
1208#ifndef _HA_ATOMIC_STORE
1209#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1210#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001211
1212#ifndef _HA_ATOMIC_LOAD
1213#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1214#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001215#endif /* _COMMON_HATHREADS_H */