blob: 1042c26b6836926f13599c56a9c15764bc3a917e [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 Tarreau38171da2019-05-17 16:33:13 +020025#include <unistd.h>
26#ifdef _POSIX_PRIORITY_SCHEDULING
27#include <sched.h>
28#endif
29
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020030#include <common/config.h>
Willy Tarreau90fa97b2018-11-25 19:46:08 +010031#include <common/initcall.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020032
Willy Tarreau0ccd3222018-07-30 10:34:35 +020033/* Note about all_threads_mask :
Willy Tarreauda9e9392019-02-02 17:03:41 +010034 * - this variable is comprised between 1 and LONGBITS.
35 * - with threads support disabled, this symbol is defined as constant 1UL.
36 * - with threads enabled, it contains the mask of enabled threads. Thus if
37 * only one thread is enabled, it equals 1.
Willy Tarreau0ccd3222018-07-30 10:34:35 +020038 */
39
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020040#ifndef USE_THREAD
41
Willy Tarreau421f02e2018-01-20 18:19:22 +010042#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020043#define MAX_THREADS_MASK 1
44
45/* Only way found to replace variables with constants that are optimized away
46 * at build time.
47 */
48enum { all_threads_mask = 1UL };
49enum { tid_bit = 1UL };
50enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010051
Willy Tarreau5a6e2242019-05-20 18:56:48 +020052extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020053 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +020054 uint64_t prev_cpu_time; /* previous per thread CPU time */
55 uint64_t prev_mono_time; /* previous system wide monotonic time */
56 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020057 /* pad to cache line (64B) */
58 char __pad[0]; /* unused except to check remaining room */
59 char __end[0] __attribute__((aligned(64)));
60} thread_info[MAX_THREADS];
61
Willy Tarreau8323a372019-05-20 18:57:53 +020062extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
63
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010064#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010065#define __decl_spinlock(lock)
66#define __decl_aligned_spinlock(lock)
67#define __decl_rwlock(lock)
68#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010069
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020070#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020071#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020072#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
73#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020074#define HA_ATOMIC_XADD(val, i) \
75 ({ \
76 typeof((val)) __p_xadd = (val); \
77 typeof(*(val)) __old_xadd = *__p_xadd; \
78 *__p_xadd += i; \
79 __old_xadd; \
80 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020081#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
82#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
83#define HA_ATOMIC_XCHG(val, new) \
84 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020085 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020086 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020087 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020088 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010089#define HA_ATOMIC_BTS(val, bit) \
90 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020091 typeof((val)) __p_bts = (val); \
92 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
93 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
94 if (!__t_bts) \
95 *__p_bts |= __b_bts; \
96 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010097 })
98#define HA_ATOMIC_BTR(val, bit) \
99 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200100 typeof((val)) __p_btr = (val); \
101 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
102 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
103 if (__t_btr) \
104 *__p_btr &= ~__b_btr; \
105 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100106 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200107#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200108#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
109#define HA_ATOMIC_UPDATE_MAX(val, new) \
110 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200111 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200112 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200113 if (*(val) < __new_max) \
114 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200115 *(val); \
116 })
117
118#define HA_ATOMIC_UPDATE_MIN(val, new) \
119 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200120 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200121 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200122 if (*(val) > __new_min) \
123 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200124 *(val); \
125 })
126
Willy Tarreaub29dc952017-10-31 18:00:20 +0100127#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200128
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100129#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
130#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
131#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
132#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
133#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200134
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100135#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
136#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
137#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
138#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
139#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
140#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
141#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
142#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200143
William Lallemand6e1796e2018-06-07 11:23:40 +0200144#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
145
Willy Tarreau0c026f42018-08-01 19:12:20 +0200146static inline void ha_set_tid(unsigned int tid)
147{
Willy Tarreau8323a372019-05-20 18:57:53 +0200148 ti = &thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200149}
150
151static inline void ha_thread_relax(void)
152{
153#if _POSIX_PRIORITY_SCHEDULING
154 sched_yield();
155#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200156}
William Lallemand6e1796e2018-06-07 11:23:40 +0200157
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100158static inline void __ha_barrier_atomic_load(void)
159{
160}
161
162static inline void __ha_barrier_atomic_store(void)
163{
164}
165
166static inline void __ha_barrier_atomic_full(void)
167{
168}
169
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100170static inline void __ha_barrier_load(void)
171{
172}
173
174static inline void __ha_barrier_store(void)
175{
176}
177
178static inline void __ha_barrier_full(void)
179{
180}
181
Willy Tarreau60b639c2018-08-02 10:16:17 +0200182static inline void thread_harmless_now()
183{
184}
185
186static inline void thread_harmless_end()
187{
188}
189
190static inline void thread_isolate()
191{
192}
193
194static inline void thread_release()
195{
196}
197
198static inline unsigned long thread_isolated()
199{
200 return 1;
201}
202
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200203#else /* USE_THREAD */
204
205#include <stdio.h>
206#include <stdlib.h>
207#include <string.h>
208#include <pthread.h>
209#include <import/plock.h>
210
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100211#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100212#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100213#endif
214
215#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100216
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100217#define __decl_hathreads(decl) decl
218
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100219/* declare a self-initializing spinlock */
220#define __decl_spinlock(lock) \
221 HA_SPINLOCK_T (lock); \
222 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
223
224/* declare a self-initializing spinlock, aligned on a cache line */
225#define __decl_aligned_spinlock(lock) \
226 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
227 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
228
229/* declare a self-initializing rwlock */
230#define __decl_rwlock(lock) \
231 HA_RWLOCK_T (lock); \
232 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
233
234/* declare a self-initializing rwlock, aligned on a cache line */
235#define __decl_aligned_rwlock(lock) \
236 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
237 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
238
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200239/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
240 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100241
David Carlierec5e8452018-01-11 14:20:43 +0000242#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100243/* gcc < 4.7 */
244
245#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
246#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200247#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100248#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
249#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
250
251/* the CAS is a bit complicated. The older API doesn't support returning the
252 * value and the swap's result at the same time. So here we take what looks
253 * like the safest route, consisting in using the boolean version guaranteeing
254 * that the operation was performed or not, and we snoop a previous value. If
255 * the compare succeeds, we return. If it fails, we return the previous value,
256 * but only if it differs from the expected one. If it's the same it's a race
257 * thus we try again to avoid confusing a possibly sensitive caller.
258 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200259#define HA_ATOMIC_CAS(val, old, new) \
260 ({ \
261 typeof((val)) __val_cas = (val); \
262 typeof((old)) __oldp_cas = (old); \
263 typeof(*(old)) __oldv_cas; \
264 typeof((new)) __new_cas = (new); \
265 int __ret_cas; \
266 do { \
267 __oldv_cas = *__val_cas; \
268 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
269 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
270 if (!__ret_cas) \
271 *__oldp_cas = __oldv_cas; \
272 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100273 })
274
Willy Tarreau6a38b322019-05-11 18:04:24 +0200275#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
276
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200277#define HA_ATOMIC_XCHG(val, new) \
278 ({ \
279 typeof((val)) __val_xchg = (val); \
280 typeof(*(val)) __old_xchg; \
281 typeof((new)) __new_xchg = (new); \
282 do { __old_xchg = *__val_xchg; \
283 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
284 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100285 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100286
287#define HA_ATOMIC_BTS(val, bit) \
288 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200289 typeof(*(val)) __b_bts = (1UL << (bit)); \
290 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100291 })
292
293#define HA_ATOMIC_BTR(val, bit) \
294 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200295 typeof(*(val)) __b_btr = (1UL << (bit)); \
296 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100297 })
298
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200299#define HA_ATOMIC_LOAD(val) \
300 ({ \
301 typeof(*(val)) ret; \
302 __sync_synchronize(); \
303 ret = *(volatile typeof(val))val; \
304 __sync_synchronize(); \
305 ret; \
306 })
307
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200308#define HA_ATOMIC_STORE(val, new) \
309 ({ \
310 typeof((val)) __val_store = (val); \
311 typeof(*(val)) __old_store; \
312 typeof((new)) __new_store = (new); \
313 do { __old_store = *__val_store; \
314 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100315 })
316#else
317/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100318#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 +0200319#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100320#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
321#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
322#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
323#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
324#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100325#define HA_ATOMIC_BTS(val, bit) \
326 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200327 typeof(*(val)) __b_bts = (1UL << (bit)); \
328 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100329 })
330
331#define HA_ATOMIC_BTR(val, bit) \
332 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200333 typeof(*(val)) __b_btr = (1UL << (bit)); \
334 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100335 })
336
Olivier Houchard11353792019-03-07 18:48:22 +0100337#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
338#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200339#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200340
341/* Variants that don't generate any memory barrier.
342 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
343 * that will always generate correct code.
344 * Usually it's fine to use those when updating data that have no dependency,
345 * ie updating a counter. Otherwise a barrier is required.
346 */
347#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 +0200348#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200349#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
350#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
351#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
352#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
353#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
354#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
355#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200356#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200357
358#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100359
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200360#define HA_ATOMIC_UPDATE_MAX(val, new) \
361 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200362 typeof(*(val)) __old_max = *(val); \
363 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200364 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200365 while (__old_max < __new_max && \
366 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
367 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200368 })
369#define HA_ATOMIC_UPDATE_MIN(val, new) \
370 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200371 typeof(*(val)) __old_min = *(val); \
372 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200373 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200374 while (__old_min > __new_min && \
375 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
376 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200377 })
378
Willy Tarreaub29dc952017-10-31 18:00:20 +0100379#define HA_BARRIER() pl_barrier()
380
Willy Tarreau60b639c2018-08-02 10:16:17 +0200381void thread_harmless_till_end();
382void thread_isolate();
383void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200384
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200385extern struct thread_info {
386 pthread_t pthread;
387 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +0200388 uint64_t prev_cpu_time; /* previous per thread CPU time */
389 uint64_t prev_mono_time; /* previous system wide monotonic time */
390 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200391 /* pad to cache line (64B) */
392 char __pad[0]; /* unused except to check remaining room */
393 char __end[0] __attribute__((aligned(64)));
394} thread_info[MAX_THREADS];
395
Willy Tarreau0c026f42018-08-01 19:12:20 +0200396extern THREAD_LOCAL unsigned int tid; /* The thread id */
397extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200398extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200399extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200400extern volatile unsigned long threads_want_rdv_mask;
401extern volatile unsigned long threads_harmless_mask;
402
403/* explanation for threads_want_rdv_mask and threads_harmless_mask :
404 * - threads_want_rdv_mask is a bit field indicating all threads that have
405 * requested a rendez-vous of other threads using thread_isolate().
406 * - threads_harmless_mask is a bit field indicating all threads that are
407 * currently harmless in that they promise not to access a shared resource.
408 *
409 * For a given thread, its bits in want_rdv and harmless can be translated like
410 * this :
411 *
412 * ----------+----------+----------------------------------------------------
413 * want_rdv | harmless | description
414 * ----------+----------+----------------------------------------------------
415 * 0 | 0 | thread not interested in RDV, possibly harmful
416 * 0 | 1 | thread not interested in RDV but harmless
417 * 1 | 1 | thread interested in RDV and waiting for its turn
418 * 1 | 0 | thread currently working isolated from others
419 * ----------+----------+----------------------------------------------------
420 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200421
William Lallemand6e1796e2018-06-07 11:23:40 +0200422#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
423
Willy Tarreau0c026f42018-08-01 19:12:20 +0200424/* sets the thread ID and the TID bit for the current thread */
425static inline void ha_set_tid(unsigned int data)
426{
427 tid = data;
428 tid_bit = (1UL << tid);
Willy Tarreau8323a372019-05-20 18:57:53 +0200429 ti = &thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200430}
431
Willy Tarreau38171da2019-05-17 16:33:13 +0200432static inline void ha_thread_relax(void)
433{
434#if _POSIX_PRIORITY_SCHEDULING
435 sched_yield();
436#else
437 pl_cpu_relax();
438#endif
439}
440
Willy Tarreau60b639c2018-08-02 10:16:17 +0200441/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
442 * not be touching any unprotected shared resource during this period. Usually
443 * this is called before poll(), but it may also be placed around very slow
444 * calls (eg: some crypto operations). Needs to be terminated using
445 * thread_harmless_end().
446 */
447static inline void thread_harmless_now()
448{
449 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
450}
451
452/* Ends the harmless period started by thread_harmless_now(). Usually this is
453 * placed after the poll() call. If it is discovered that a job was running and
454 * is relying on the thread still being harmless, the thread waits for the
455 * other one to finish.
456 */
457static inline void thread_harmless_end()
458{
459 while (1) {
460 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
461 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
462 break;
463 thread_harmless_till_end();
464 }
465}
466
467/* an isolated thread has harmless cleared and want_rdv set */
468static inline unsigned long thread_isolated()
469{
470 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
471}
472
William Lallemand6e1796e2018-06-07 11:23:40 +0200473
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200474#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
475
Christopher Fauletf51bac22018-01-30 11:04:29 +0100476/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200477enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200478 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200479 TASK_RQ_LOCK,
480 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200481 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200482 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200483 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200484 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200485 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200486 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200487 STK_TABLE_LOCK,
488 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200489 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200490 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200491 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200492 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200493 SSL_LOCK,
494 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200495 PATREF_LOCK,
496 PATEXP_LOCK,
497 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200498 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200499 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200500 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200501 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200502 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200503 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200504 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200505 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100506 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100507 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100508 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200509 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000510 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200511 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200512};
513struct lock_stat {
514 uint64_t nsec_wait_for_write;
515 uint64_t nsec_wait_for_read;
516 uint64_t num_write_locked;
517 uint64_t num_write_unlocked;
518 uint64_t num_read_locked;
519 uint64_t num_read_unlocked;
520};
521
522extern struct lock_stat lock_stats[LOCK_LABELS];
523
524#define __HA_SPINLOCK_T unsigned long
525
526#define __SPIN_INIT(l) ({ (*l) = 0; })
527#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100528#define __SPIN_LOCK(l) pl_take_s(l)
529#define __SPIN_TRYLOCK(l) !pl_try_s(l)
530#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200531
532#define __HA_RWLOCK_T unsigned long
533
534#define __RWLOCK_INIT(l) ({ (*l) = 0; })
535#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
536#define __RWLOCK_WRLOCK(l) pl_take_w(l)
537#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
538#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
539#define __RWLOCK_RDLOCK(l) pl_take_r(l)
540#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
541#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
542
543#define HA_SPINLOCK_T struct ha_spinlock
544
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100545#define HA_SPIN_INIT(l) __spin_init(l)
546#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200547
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100548#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
549#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
550#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200551
552#define HA_RWLOCK_T struct ha_rwlock
553
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100554#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
555#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
556#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
557#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
558#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
559#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
560#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
561#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200562
563struct ha_spinlock {
564 __HA_SPINLOCK_T lock;
565 struct {
566 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
567 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
568 struct {
569 const char *function;
570 const char *file;
571 int line;
572 } last_location; /* location of the last owner */
573 } info;
574};
575
576struct ha_rwlock {
577 __HA_RWLOCK_T lock;
578 struct {
579 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
580 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
581 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
582 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
583 struct {
584 const char *function;
585 const char *file;
586 int line;
587 } last_location; /* location of the last write owner */
588 } info;
589};
590
Christopher Fauletf51bac22018-01-30 11:04:29 +0100591static inline const char *lock_label(enum lock_label label)
592{
593 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100594 case FD_LOCK: return "FD";
595 case TASK_RQ_LOCK: return "TASK_RQ";
596 case TASK_WQ_LOCK: return "TASK_WQ";
597 case POOL_LOCK: return "POOL";
598 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100599 case PROXY_LOCK: return "PROXY";
600 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100601 case LBPRM_LOCK: return "LBPRM";
602 case SIGNALS_LOCK: return "SIGNALS";
603 case STK_TABLE_LOCK: return "STK_TABLE";
604 case STK_SESS_LOCK: return "STK_SESS";
605 case APPLETS_LOCK: return "APPLETS";
606 case PEER_LOCK: return "PEER";
607 case BUF_WQ_LOCK: return "BUF_WQ";
608 case STRMS_LOCK: return "STRMS";
609 case SSL_LOCK: return "SSL";
610 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
611 case PATREF_LOCK: return "PATREF";
612 case PATEXP_LOCK: return "PATEXP";
613 case PATLRU_LOCK: return "PATLRU";
614 case VARS_LOCK: return "VARS";
615 case COMP_POOL_LOCK: return "COMP_POOL";
616 case LUA_LOCK: return "LUA";
617 case NOTIF_LOCK: return "NOTIF";
618 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
619 case DNS_LOCK: return "DNS";
620 case PID_LIST_LOCK: return "PID_LIST";
621 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
622 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100623 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100624 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200625 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000626 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100627 case LOCK_LABELS: break; /* keep compiler happy */
628 };
629 /* only way to come here is consecutive to an internal bug */
630 abort();
631}
632
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200633static inline void show_lock_stats()
634{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200635 int lbl;
636
637 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
638 fprintf(stderr,
639 "Stats about Lock %s: \n"
640 "\t # write lock : %lu\n"
641 "\t # write unlock: %lu (%ld)\n"
642 "\t # wait time for write : %.3f msec\n"
643 "\t # wait time for write/lock: %.3f nsec\n"
644 "\t # read lock : %lu\n"
645 "\t # read unlock : %lu (%ld)\n"
646 "\t # wait time for read : %.3f msec\n"
647 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100648 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200649 lock_stats[lbl].num_write_locked,
650 lock_stats[lbl].num_write_unlocked,
651 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
652 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
653 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
654 lock_stats[lbl].num_read_locked,
655 lock_stats[lbl].num_read_unlocked,
656 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
657 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
658 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
659 }
660}
661
662/* Following functions are used to collect some stats about locks. We wrap
663 * pthread functions to known how much time we wait in a lock. */
664
665static uint64_t nsec_now(void) {
666 struct timespec ts;
667
668 clock_gettime(CLOCK_MONOTONIC, &ts);
669 return ((uint64_t) ts.tv_sec * 1000000000ULL +
670 (uint64_t) ts.tv_nsec);
671}
672
673static inline void __ha_rwlock_init(struct ha_rwlock *l)
674{
675 memset(l, 0, sizeof(struct ha_rwlock));
676 __RWLOCK_INIT(&l->lock);
677}
678
679static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
680{
681 __RWLOCK_DESTROY(&l->lock);
682 memset(l, 0, sizeof(struct ha_rwlock));
683}
684
685
686static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
687 const char *func, const char *file, int line)
688{
689 uint64_t start_time;
690
691 if (unlikely(l->info.cur_writer & tid_bit)) {
692 /* the thread is already owning the lock for write */
693 abort();
694 }
695
696 if (unlikely(l->info.cur_readers & tid_bit)) {
697 /* the thread is already owning the lock for read */
698 abort();
699 }
700
701 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
702
703 start_time = nsec_now();
704 __RWLOCK_WRLOCK(&l->lock);
705 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
706
707 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
708
709 l->info.cur_writer = tid_bit;
710 l->info.last_location.function = func;
711 l->info.last_location.file = file;
712 l->info.last_location.line = line;
713
714 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
715}
716
717static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
718 const char *func, const char *file, int line)
719{
720 uint64_t start_time;
721 int r;
722
723 if (unlikely(l->info.cur_writer & tid_bit)) {
724 /* the thread is already owning the lock for write */
725 abort();
726 }
727
728 if (unlikely(l->info.cur_readers & tid_bit)) {
729 /* the thread is already owning the lock for read */
730 abort();
731 }
732
733 /* We set waiting writer because trywrlock could wait for readers to quit */
734 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
735
736 start_time = nsec_now();
737 r = __RWLOCK_TRYWRLOCK(&l->lock);
738 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
739 if (unlikely(r)) {
740 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
741 return r;
742 }
743 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
744
745 l->info.cur_writer = tid_bit;
746 l->info.last_location.function = func;
747 l->info.last_location.file = file;
748 l->info.last_location.line = line;
749
750 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
751
752 return 0;
753}
754
755static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
756 const char *func, const char *file, int line)
757{
758 if (unlikely(!(l->info.cur_writer & tid_bit))) {
759 /* the thread is not owning the lock for write */
760 abort();
761 }
762
763 l->info.cur_writer = 0;
764 l->info.last_location.function = func;
765 l->info.last_location.file = file;
766 l->info.last_location.line = line;
767
768 __RWLOCK_WRUNLOCK(&l->lock);
769
770 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
771}
772
773static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
774{
775 uint64_t start_time;
776
777 if (unlikely(l->info.cur_writer & tid_bit)) {
778 /* the thread is already owning the lock for write */
779 abort();
780 }
781
782 if (unlikely(l->info.cur_readers & tid_bit)) {
783 /* the thread is already owning the lock for read */
784 abort();
785 }
786
787 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
788
789 start_time = nsec_now();
790 __RWLOCK_RDLOCK(&l->lock);
791 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
792 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
793
794 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
795
796 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
797}
798
799static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
800{
801 int r;
802
803 if (unlikely(l->info.cur_writer & tid_bit)) {
804 /* the thread is already owning the lock for write */
805 abort();
806 }
807
808 if (unlikely(l->info.cur_readers & tid_bit)) {
809 /* the thread is already owning the lock for read */
810 abort();
811 }
812
813 /* try read should never wait */
814 r = __RWLOCK_TRYRDLOCK(&l->lock);
815 if (unlikely(r))
816 return r;
817 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
818
819 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
820
821 return 0;
822}
823
824static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
825{
826 if (unlikely(!(l->info.cur_readers & tid_bit))) {
827 /* the thread is not owning the lock for read */
828 abort();
829 }
830
831 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
832
833 __RWLOCK_RDUNLOCK(&l->lock);
834
835 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
836}
837
838static inline void __spin_init(struct ha_spinlock *l)
839{
840 memset(l, 0, sizeof(struct ha_spinlock));
841 __SPIN_INIT(&l->lock);
842}
843
844static inline void __spin_destroy(struct ha_spinlock *l)
845{
846 __SPIN_DESTROY(&l->lock);
847 memset(l, 0, sizeof(struct ha_spinlock));
848}
849
850static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
851 const char *func, const char *file, int line)
852{
853 uint64_t start_time;
854
855 if (unlikely(l->info.owner & tid_bit)) {
856 /* the thread is already owning the lock */
857 abort();
858 }
859
860 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
861
862 start_time = nsec_now();
863 __SPIN_LOCK(&l->lock);
864 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
865
866 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
867
868
869 l->info.owner = tid_bit;
870 l->info.last_location.function = func;
871 l->info.last_location.file = file;
872 l->info.last_location.line = line;
873
874 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
875}
876
877static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
878 const char *func, const char *file, int line)
879{
880 int r;
881
882 if (unlikely(l->info.owner & tid_bit)) {
883 /* the thread is already owning the lock */
884 abort();
885 }
886
887 /* try read should never wait */
888 r = __SPIN_TRYLOCK(&l->lock);
889 if (unlikely(r))
890 return r;
891 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
892
893 l->info.owner = tid_bit;
894 l->info.last_location.function = func;
895 l->info.last_location.file = file;
896 l->info.last_location.line = line;
897
898 return 0;
899}
900
901static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
902 const char *func, const char *file, int line)
903{
904 if (unlikely(!(l->info.owner & tid_bit))) {
905 /* the thread is not owning the lock */
906 abort();
907 }
908
909 l->info.owner = 0;
910 l->info.last_location.function = func;
911 l->info.last_location.file = file;
912 l->info.last_location.line = line;
913
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100914 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200915 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
916}
917
918#else /* DEBUG_THREAD */
919
920#define HA_SPINLOCK_T unsigned long
921
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100922#define HA_SPIN_INIT(l) ({ (*l) = 0; })
923#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
924#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
925#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
926#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200927
928#define HA_RWLOCK_T unsigned long
929
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100930#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
931#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
932#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
933#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
934#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
935#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
936#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
937#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200938
939#endif /* DEBUG_THREAD */
940
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100941#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200942
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100943static __inline int
944__ha_cas_dw(void *target, void *compare, const void *set)
945{
946 char ret;
947
948 __asm __volatile("lock cmpxchg16b %0; setz %3"
949 : "+m" (*(void **)target),
950 "=a" (((void **)compare)[0]),
951 "=d" (((void **)compare)[1]),
952 "=q" (ret)
953 : "a" (((void **)compare)[0]),
954 "d" (((void **)compare)[1]),
955 "b" (((const void **)set)[0]),
956 "c" (((const void **)set)[1])
957 : "memory", "cc");
958 return (ret);
959}
960
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100961/* Use __ha_barrier_atomic* when you're trying to protect data that are
962 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
963 */
964static __inline void
965__ha_barrier_atomic_load(void)
966{
967 __asm __volatile("" ::: "memory");
968}
969
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100970static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100971__ha_barrier_atomic_store(void)
972{
973 __asm __volatile("" ::: "memory");
974}
975
976static __inline void
977__ha_barrier_atomic_full(void)
978{
979 __asm __volatile("" ::: "memory");
980}
981
982static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100983__ha_barrier_load(void)
984{
985 __asm __volatile("lfence" ::: "memory");
986}
987
988static __inline void
989__ha_barrier_store(void)
990{
991 __asm __volatile("sfence" ::: "memory");
992}
993
994static __inline void
995__ha_barrier_full(void)
996{
997 __asm __volatile("mfence" ::: "memory");
998}
999
1000#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001001
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001002/* Use __ha_barrier_atomic* when you're trying to protect data that are
1003 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1004 */
1005static __inline void
1006__ha_barrier_atomic_load(void)
1007{
1008 __asm __volatile("dmb" ::: "memory");
1009}
1010
1011static __inline void
1012__ha_barrier_atomic_store(void)
1013{
1014 __asm __volatile("dsb" ::: "memory");
1015}
1016
1017static __inline void
1018__ha_barrier_atomic_full(void)
1019{
1020 __asm __volatile("dmb" ::: "memory");
1021}
1022
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001023static __inline void
1024__ha_barrier_load(void)
1025{
1026 __asm __volatile("dmb" ::: "memory");
1027}
1028
1029static __inline void
1030__ha_barrier_store(void)
1031{
1032 __asm __volatile("dsb" ::: "memory");
1033}
1034
1035static __inline void
1036__ha_barrier_full(void)
1037{
1038 __asm __volatile("dmb" ::: "memory");
1039}
1040
Willy Tarreau41ccb192018-02-14 14:16:28 +01001041static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001042{
1043 uint64_t previous;
1044 int tmp;
1045
1046 __asm __volatile("1:"
1047 "ldrexd %0, [%4];"
1048 "cmp %Q0, %Q2;"
1049 "ittt eq;"
1050 "cmpeq %R0, %R2;"
1051 "strexdeq %1, %3, [%4];"
1052 "cmpeq %1, #1;"
1053 "beq 1b;"
1054 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001055 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001056 : "memory", "cc");
1057 tmp = (previous == *(uint64_t *)compare);
1058 *(uint64_t *)compare = previous;
1059 return (tmp);
1060}
1061
1062#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001063
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001064/* Use __ha_barrier_atomic* when you're trying to protect data that are
1065 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1066 */
1067static __inline void
1068__ha_barrier_atomic_load(void)
1069{
1070 __asm __volatile("dmb ishld" ::: "memory");
1071}
1072
1073static __inline void
1074__ha_barrier_atomic_store(void)
1075{
1076 __asm __volatile("dmb ishst" ::: "memory");
1077}
1078
1079static __inline void
1080__ha_barrier_atomic_full(void)
1081{
1082 __asm __volatile("dmb ish" ::: "memory");
1083}
1084
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001085static __inline void
1086__ha_barrier_load(void)
1087{
1088 __asm __volatile("dmb ishld" ::: "memory");
1089}
1090
1091static __inline void
1092__ha_barrier_store(void)
1093{
1094 __asm __volatile("dmb ishst" ::: "memory");
1095}
1096
1097static __inline void
1098__ha_barrier_full(void)
1099{
1100 __asm __volatile("dmb ish" ::: "memory");
1101}
1102
1103static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1104{
1105 void *value[2];
1106 uint64_t tmp1, tmp2;
1107
1108 __asm__ __volatile__("1:"
1109 "ldxp %0, %1, [%4];"
1110 "mov %2, %0;"
1111 "mov %3, %1;"
1112 "eor %0, %0, %5;"
1113 "eor %1, %1, %6;"
1114 "orr %1, %0, %1;"
1115 "mov %w0, #0;"
1116 "cbnz %1, 2f;"
1117 "stxp %w0, %7, %8, [%4];"
1118 "cbnz %w0, 1b;"
1119 "mov %w0, #1;"
1120 "2:"
1121 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1122 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1123 : "cc", "memory");
1124
1125 memcpy(compare, &value, sizeof(value));
1126 return (tmp1);
1127}
1128
1129#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001130#define __ha_barrier_atomic_load __sync_synchronize
1131#define __ha_barrier_atomic_store __sync_synchronize
1132#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001133#define __ha_barrier_load __sync_synchronize
1134#define __ha_barrier_store __sync_synchronize
1135#define __ha_barrier_full __sync_synchronize
1136#endif
1137
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001138void ha_spin_init(HA_SPINLOCK_T *l);
1139void ha_rwlock_init(HA_RWLOCK_T *l);
1140
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001141#endif /* USE_THREAD */
1142
Willy Tarreau149ab772019-01-26 14:27:06 +01001143extern int thread_cpus_enabled_at_boot;
1144
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001145static inline void __ha_compiler_barrier(void)
1146{
1147 __asm __volatile("" ::: "memory");
1148}
1149
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001150int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001151int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001152
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001153#ifndef _HA_ATOMIC_CAS
1154#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1155#endif /* !_HA_ATOMIC_CAS */
1156
Willy Tarreau6a38b322019-05-11 18:04:24 +02001157#ifndef _HA_ATOMIC_DWCAS
1158#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1159#endif /* !_HA_ATOMIC_CAS */
1160
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001161#ifndef _HA_ATOMIC_ADD
1162#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1163#endif /* !_HA_ATOMIC_ADD */
1164
1165#ifndef _HA_ATOMIC_XADD
1166#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1167#endif /* !_HA_ATOMIC_SUB */
1168
1169#ifndef _HA_ATOMIC_SUB
1170#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1171#endif /* !_HA_ATOMIC_SUB */
1172
1173#ifndef _HA_ATOMIC_AND
1174#define _HA_ATOMIC_AND HA_ATOMIC_AND
1175#endif /* !_HA_ATOMIC_AND */
1176
1177#ifndef _HA_ATOMIC_OR
1178#define _HA_ATOMIC_OR HA_ATOMIC_OR
1179#endif /* !_HA_ATOMIC_OR */
1180
1181#ifndef _HA_ATOMIC_XCHG
1182#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1183#endif /* !_HA_ATOMIC_XCHG */
1184
1185#ifndef _HA_ATOMIC_STORE
1186#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1187#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001188
1189#ifndef _HA_ATOMIC_LOAD
1190#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1191#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001192#endif /* _COMMON_HATHREADS_H */