blob: 0bc6e4505e3fd599926b7b197f7f7d6139a8df30 [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 };
Willy Tarreau441259c2019-05-22 07:48:18 +020049enum { threads_harmless_mask = 0 };
50enum { threads_want_rdv_mask = 0 };
Willy Tarreau0c026f42018-08-01 19:12:20 +020051enum { tid_bit = 1UL };
52enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010053
Willy Tarreau5a6e2242019-05-20 18:56:48 +020054extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020055 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +020056 uint64_t prev_cpu_time; /* previous per thread CPU time */
57 uint64_t prev_mono_time; /* previous system wide monotonic time */
58 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020059 /* pad to cache line (64B) */
60 char __pad[0]; /* unused except to check remaining room */
61 char __end[0] __attribute__((aligned(64)));
62} thread_info[MAX_THREADS];
63
Willy Tarreau8323a372019-05-20 18:57:53 +020064extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
65
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010066#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010067#define __decl_spinlock(lock)
68#define __decl_aligned_spinlock(lock)
69#define __decl_rwlock(lock)
70#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010071
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020072#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020073#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020074#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
75#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020076#define HA_ATOMIC_XADD(val, i) \
77 ({ \
78 typeof((val)) __p_xadd = (val); \
79 typeof(*(val)) __old_xadd = *__p_xadd; \
80 *__p_xadd += i; \
81 __old_xadd; \
82 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020083#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
84#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
85#define HA_ATOMIC_XCHG(val, new) \
86 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020087 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020088 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020089 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020090 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010091#define HA_ATOMIC_BTS(val, bit) \
92 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020093 typeof((val)) __p_bts = (val); \
94 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
95 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
96 if (!__t_bts) \
97 *__p_bts |= __b_bts; \
98 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010099 })
100#define HA_ATOMIC_BTR(val, bit) \
101 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200102 typeof((val)) __p_btr = (val); \
103 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
104 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
105 if (__t_btr) \
106 *__p_btr &= ~__b_btr; \
107 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100108 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200109#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
111#define HA_ATOMIC_UPDATE_MAX(val, new) \
112 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200113 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200114 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200115 if (*(val) < __new_max) \
116 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200117 *(val); \
118 })
119
120#define HA_ATOMIC_UPDATE_MIN(val, new) \
121 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200122 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200123 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200124 if (*(val) > __new_min) \
125 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200126 *(val); \
127 })
128
Willy Tarreaub29dc952017-10-31 18:00:20 +0100129#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200130
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100131#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
132#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
133#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
134#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
135#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200136
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100137#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
138#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
139#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
140#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
141#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
142#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
143#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
144#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200145
William Lallemand6e1796e2018-06-07 11:23:40 +0200146#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
147
Willy Tarreau0c026f42018-08-01 19:12:20 +0200148static inline void ha_set_tid(unsigned int tid)
149{
Willy Tarreau8323a372019-05-20 18:57:53 +0200150 ti = &thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200151}
152
153static inline void ha_thread_relax(void)
154{
155#if _POSIX_PRIORITY_SCHEDULING
156 sched_yield();
157#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200158}
William Lallemand6e1796e2018-06-07 11:23:40 +0200159
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100160static inline void __ha_barrier_atomic_load(void)
161{
162}
163
164static inline void __ha_barrier_atomic_store(void)
165{
166}
167
168static inline void __ha_barrier_atomic_full(void)
169{
170}
171
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100172static inline void __ha_barrier_load(void)
173{
174}
175
176static inline void __ha_barrier_store(void)
177{
178}
179
180static inline void __ha_barrier_full(void)
181{
182}
183
Willy Tarreau60b639c2018-08-02 10:16:17 +0200184static inline void thread_harmless_now()
185{
186}
187
188static inline void thread_harmless_end()
189{
190}
191
192static inline void thread_isolate()
193{
194}
195
196static inline void thread_release()
197{
198}
199
200static inline unsigned long thread_isolated()
201{
202 return 1;
203}
204
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200205#else /* USE_THREAD */
206
207#include <stdio.h>
208#include <stdlib.h>
209#include <string.h>
210#include <pthread.h>
211#include <import/plock.h>
212
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100213#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100214#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100215#endif
216
217#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100218
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100219#define __decl_hathreads(decl) decl
220
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100221/* declare a self-initializing spinlock */
222#define __decl_spinlock(lock) \
223 HA_SPINLOCK_T (lock); \
224 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
225
226/* declare a self-initializing spinlock, aligned on a cache line */
227#define __decl_aligned_spinlock(lock) \
228 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
229 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
230
231/* declare a self-initializing rwlock */
232#define __decl_rwlock(lock) \
233 HA_RWLOCK_T (lock); \
234 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
235
236/* declare a self-initializing rwlock, aligned on a cache line */
237#define __decl_aligned_rwlock(lock) \
238 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
239 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
240
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200241/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
242 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100243
David Carlierec5e8452018-01-11 14:20:43 +0000244#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100245/* gcc < 4.7 */
246
247#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
248#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200249#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100250#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
251#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
252
253/* the CAS is a bit complicated. The older API doesn't support returning the
254 * value and the swap's result at the same time. So here we take what looks
255 * like the safest route, consisting in using the boolean version guaranteeing
256 * that the operation was performed or not, and we snoop a previous value. If
257 * the compare succeeds, we return. If it fails, we return the previous value,
258 * but only if it differs from the expected one. If it's the same it's a race
259 * thus we try again to avoid confusing a possibly sensitive caller.
260 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200261#define HA_ATOMIC_CAS(val, old, new) \
262 ({ \
263 typeof((val)) __val_cas = (val); \
264 typeof((old)) __oldp_cas = (old); \
265 typeof(*(old)) __oldv_cas; \
266 typeof((new)) __new_cas = (new); \
267 int __ret_cas; \
268 do { \
269 __oldv_cas = *__val_cas; \
270 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
271 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
272 if (!__ret_cas) \
273 *__oldp_cas = __oldv_cas; \
274 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100275 })
276
Willy Tarreau6a38b322019-05-11 18:04:24 +0200277#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
278
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200279#define HA_ATOMIC_XCHG(val, new) \
280 ({ \
281 typeof((val)) __val_xchg = (val); \
282 typeof(*(val)) __old_xchg; \
283 typeof((new)) __new_xchg = (new); \
284 do { __old_xchg = *__val_xchg; \
285 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
286 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100287 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100288
289#define HA_ATOMIC_BTS(val, bit) \
290 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200291 typeof(*(val)) __b_bts = (1UL << (bit)); \
292 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100293 })
294
295#define HA_ATOMIC_BTR(val, bit) \
296 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200297 typeof(*(val)) __b_btr = (1UL << (bit)); \
298 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100299 })
300
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200301#define HA_ATOMIC_LOAD(val) \
302 ({ \
303 typeof(*(val)) ret; \
304 __sync_synchronize(); \
305 ret = *(volatile typeof(val))val; \
306 __sync_synchronize(); \
307 ret; \
308 })
309
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200310#define HA_ATOMIC_STORE(val, new) \
311 ({ \
312 typeof((val)) __val_store = (val); \
313 typeof(*(val)) __old_store; \
314 typeof((new)) __new_store = (new); \
315 do { __old_store = *__val_store; \
316 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100317 })
318#else
319/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100320#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 +0200321#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100322#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
323#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
324#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
325#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
326#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100327#define HA_ATOMIC_BTS(val, bit) \
328 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200329 typeof(*(val)) __b_bts = (1UL << (bit)); \
330 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100331 })
332
333#define HA_ATOMIC_BTR(val, bit) \
334 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200335 typeof(*(val)) __b_btr = (1UL << (bit)); \
336 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100337 })
338
Olivier Houchard11353792019-03-07 18:48:22 +0100339#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
340#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200341#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200342
343/* Variants that don't generate any memory barrier.
344 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
345 * that will always generate correct code.
346 * Usually it's fine to use those when updating data that have no dependency,
347 * ie updating a counter. Otherwise a barrier is required.
348 */
349#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 +0200350#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200351#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
352#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
353#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
354#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
355#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
356#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
357#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200358#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200359
360#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100361
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200362#define HA_ATOMIC_UPDATE_MAX(val, new) \
363 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200364 typeof(*(val)) __old_max = *(val); \
365 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200366 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200367 while (__old_max < __new_max && \
368 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
369 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200370 })
371#define HA_ATOMIC_UPDATE_MIN(val, new) \
372 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200373 typeof(*(val)) __old_min = *(val); \
374 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200375 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200376 while (__old_min > __new_min && \
377 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
378 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200379 })
380
Willy Tarreaub29dc952017-10-31 18:00:20 +0100381#define HA_BARRIER() pl_barrier()
382
Willy Tarreau60b639c2018-08-02 10:16:17 +0200383void thread_harmless_till_end();
384void thread_isolate();
385void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200386
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200387extern struct thread_info {
388 pthread_t pthread;
389 clockid_t clock_id;
Willy Tarreau81036f22019-05-20 19:24:50 +0200390 uint64_t prev_cpu_time; /* previous per thread CPU time */
391 uint64_t prev_mono_time; /* previous system wide monotonic time */
392 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200393 /* pad to cache line (64B) */
394 char __pad[0]; /* unused except to check remaining room */
395 char __end[0] __attribute__((aligned(64)));
396} thread_info[MAX_THREADS];
397
Willy Tarreau0c026f42018-08-01 19:12:20 +0200398extern THREAD_LOCAL unsigned int tid; /* The thread id */
399extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200400extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200401extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200402extern volatile unsigned long threads_want_rdv_mask;
403extern volatile unsigned long threads_harmless_mask;
404
405/* explanation for threads_want_rdv_mask and threads_harmless_mask :
406 * - threads_want_rdv_mask is a bit field indicating all threads that have
407 * requested a rendez-vous of other threads using thread_isolate().
408 * - threads_harmless_mask is a bit field indicating all threads that are
409 * currently harmless in that they promise not to access a shared resource.
410 *
411 * For a given thread, its bits in want_rdv and harmless can be translated like
412 * this :
413 *
414 * ----------+----------+----------------------------------------------------
415 * want_rdv | harmless | description
416 * ----------+----------+----------------------------------------------------
417 * 0 | 0 | thread not interested in RDV, possibly harmful
418 * 0 | 1 | thread not interested in RDV but harmless
419 * 1 | 1 | thread interested in RDV and waiting for its turn
420 * 1 | 0 | thread currently working isolated from others
421 * ----------+----------+----------------------------------------------------
422 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200423
William Lallemand6e1796e2018-06-07 11:23:40 +0200424#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
425
Willy Tarreau0c026f42018-08-01 19:12:20 +0200426/* sets the thread ID and the TID bit for the current thread */
427static inline void ha_set_tid(unsigned int data)
428{
429 tid = data;
430 tid_bit = (1UL << tid);
Willy Tarreau8323a372019-05-20 18:57:53 +0200431 ti = &thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200432}
433
Willy Tarreau38171da2019-05-17 16:33:13 +0200434static inline void ha_thread_relax(void)
435{
436#if _POSIX_PRIORITY_SCHEDULING
437 sched_yield();
438#else
439 pl_cpu_relax();
440#endif
441}
442
Willy Tarreau60b639c2018-08-02 10:16:17 +0200443/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
444 * not be touching any unprotected shared resource during this period. Usually
445 * this is called before poll(), but it may also be placed around very slow
446 * calls (eg: some crypto operations). Needs to be terminated using
447 * thread_harmless_end().
448 */
449static inline void thread_harmless_now()
450{
451 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
452}
453
454/* Ends the harmless period started by thread_harmless_now(). Usually this is
455 * placed after the poll() call. If it is discovered that a job was running and
456 * is relying on the thread still being harmless, the thread waits for the
457 * other one to finish.
458 */
459static inline void thread_harmless_end()
460{
461 while (1) {
462 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
463 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
464 break;
465 thread_harmless_till_end();
466 }
467}
468
469/* an isolated thread has harmless cleared and want_rdv set */
470static inline unsigned long thread_isolated()
471{
472 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
473}
474
William Lallemand6e1796e2018-06-07 11:23:40 +0200475
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200476#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
477
Christopher Fauletf51bac22018-01-30 11:04:29 +0100478/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200479enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200480 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200481 TASK_RQ_LOCK,
482 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200483 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200484 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200485 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200486 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200487 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200488 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200489 STK_TABLE_LOCK,
490 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200491 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200492 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200493 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200494 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200495 SSL_LOCK,
496 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200497 PATREF_LOCK,
498 PATEXP_LOCK,
499 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200500 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200501 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200502 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200503 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200504 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200505 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200506 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200507 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100508 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100509 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100510 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200511 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000512 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200513 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200514};
515struct lock_stat {
516 uint64_t nsec_wait_for_write;
517 uint64_t nsec_wait_for_read;
518 uint64_t num_write_locked;
519 uint64_t num_write_unlocked;
520 uint64_t num_read_locked;
521 uint64_t num_read_unlocked;
522};
523
524extern struct lock_stat lock_stats[LOCK_LABELS];
525
526#define __HA_SPINLOCK_T unsigned long
527
528#define __SPIN_INIT(l) ({ (*l) = 0; })
529#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100530#define __SPIN_LOCK(l) pl_take_s(l)
531#define __SPIN_TRYLOCK(l) !pl_try_s(l)
532#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200533
534#define __HA_RWLOCK_T unsigned long
535
536#define __RWLOCK_INIT(l) ({ (*l) = 0; })
537#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
538#define __RWLOCK_WRLOCK(l) pl_take_w(l)
539#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
540#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
541#define __RWLOCK_RDLOCK(l) pl_take_r(l)
542#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
543#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
544
545#define HA_SPINLOCK_T struct ha_spinlock
546
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100547#define HA_SPIN_INIT(l) __spin_init(l)
548#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200549
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100550#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
551#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
552#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200553
554#define HA_RWLOCK_T struct ha_rwlock
555
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100556#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
557#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
558#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
559#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
560#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
561#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
562#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
563#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200564
565struct ha_spinlock {
566 __HA_SPINLOCK_T lock;
567 struct {
568 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
569 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
570 struct {
571 const char *function;
572 const char *file;
573 int line;
574 } last_location; /* location of the last owner */
575 } info;
576};
577
578struct ha_rwlock {
579 __HA_RWLOCK_T lock;
580 struct {
581 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
582 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
583 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
584 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
585 struct {
586 const char *function;
587 const char *file;
588 int line;
589 } last_location; /* location of the last write owner */
590 } info;
591};
592
Christopher Fauletf51bac22018-01-30 11:04:29 +0100593static inline const char *lock_label(enum lock_label label)
594{
595 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100596 case FD_LOCK: return "FD";
597 case TASK_RQ_LOCK: return "TASK_RQ";
598 case TASK_WQ_LOCK: return "TASK_WQ";
599 case POOL_LOCK: return "POOL";
600 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100601 case PROXY_LOCK: return "PROXY";
602 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100603 case LBPRM_LOCK: return "LBPRM";
604 case SIGNALS_LOCK: return "SIGNALS";
605 case STK_TABLE_LOCK: return "STK_TABLE";
606 case STK_SESS_LOCK: return "STK_SESS";
607 case APPLETS_LOCK: return "APPLETS";
608 case PEER_LOCK: return "PEER";
609 case BUF_WQ_LOCK: return "BUF_WQ";
610 case STRMS_LOCK: return "STRMS";
611 case SSL_LOCK: return "SSL";
612 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
613 case PATREF_LOCK: return "PATREF";
614 case PATEXP_LOCK: return "PATEXP";
615 case PATLRU_LOCK: return "PATLRU";
616 case VARS_LOCK: return "VARS";
617 case COMP_POOL_LOCK: return "COMP_POOL";
618 case LUA_LOCK: return "LUA";
619 case NOTIF_LOCK: return "NOTIF";
620 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
621 case DNS_LOCK: return "DNS";
622 case PID_LIST_LOCK: return "PID_LIST";
623 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
624 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100625 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100626 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200627 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000628 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100629 case LOCK_LABELS: break; /* keep compiler happy */
630 };
631 /* only way to come here is consecutive to an internal bug */
632 abort();
633}
634
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200635static inline void show_lock_stats()
636{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200637 int lbl;
638
639 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
640 fprintf(stderr,
641 "Stats about Lock %s: \n"
642 "\t # write lock : %lu\n"
643 "\t # write unlock: %lu (%ld)\n"
644 "\t # wait time for write : %.3f msec\n"
645 "\t # wait time for write/lock: %.3f nsec\n"
646 "\t # read lock : %lu\n"
647 "\t # read unlock : %lu (%ld)\n"
648 "\t # wait time for read : %.3f msec\n"
649 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100650 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200651 lock_stats[lbl].num_write_locked,
652 lock_stats[lbl].num_write_unlocked,
653 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
654 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
655 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
656 lock_stats[lbl].num_read_locked,
657 lock_stats[lbl].num_read_unlocked,
658 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
659 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
660 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
661 }
662}
663
664/* Following functions are used to collect some stats about locks. We wrap
665 * pthread functions to known how much time we wait in a lock. */
666
667static uint64_t nsec_now(void) {
668 struct timespec ts;
669
670 clock_gettime(CLOCK_MONOTONIC, &ts);
671 return ((uint64_t) ts.tv_sec * 1000000000ULL +
672 (uint64_t) ts.tv_nsec);
673}
674
675static inline void __ha_rwlock_init(struct ha_rwlock *l)
676{
677 memset(l, 0, sizeof(struct ha_rwlock));
678 __RWLOCK_INIT(&l->lock);
679}
680
681static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
682{
683 __RWLOCK_DESTROY(&l->lock);
684 memset(l, 0, sizeof(struct ha_rwlock));
685}
686
687
688static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
689 const char *func, const char *file, int line)
690{
691 uint64_t start_time;
692
693 if (unlikely(l->info.cur_writer & tid_bit)) {
694 /* the thread is already owning the lock for write */
695 abort();
696 }
697
698 if (unlikely(l->info.cur_readers & tid_bit)) {
699 /* the thread is already owning the lock for read */
700 abort();
701 }
702
703 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
704
705 start_time = nsec_now();
706 __RWLOCK_WRLOCK(&l->lock);
707 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
708
709 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
710
711 l->info.cur_writer = tid_bit;
712 l->info.last_location.function = func;
713 l->info.last_location.file = file;
714 l->info.last_location.line = line;
715
716 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
717}
718
719static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
720 const char *func, const char *file, int line)
721{
722 uint64_t start_time;
723 int r;
724
725 if (unlikely(l->info.cur_writer & tid_bit)) {
726 /* the thread is already owning the lock for write */
727 abort();
728 }
729
730 if (unlikely(l->info.cur_readers & tid_bit)) {
731 /* the thread is already owning the lock for read */
732 abort();
733 }
734
735 /* We set waiting writer because trywrlock could wait for readers to quit */
736 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
737
738 start_time = nsec_now();
739 r = __RWLOCK_TRYWRLOCK(&l->lock);
740 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
741 if (unlikely(r)) {
742 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
743 return r;
744 }
745 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
746
747 l->info.cur_writer = tid_bit;
748 l->info.last_location.function = func;
749 l->info.last_location.file = file;
750 l->info.last_location.line = line;
751
752 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
753
754 return 0;
755}
756
757static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
758 const char *func, const char *file, int line)
759{
760 if (unlikely(!(l->info.cur_writer & tid_bit))) {
761 /* the thread is not owning the lock for write */
762 abort();
763 }
764
765 l->info.cur_writer = 0;
766 l->info.last_location.function = func;
767 l->info.last_location.file = file;
768 l->info.last_location.line = line;
769
770 __RWLOCK_WRUNLOCK(&l->lock);
771
772 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
773}
774
775static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
776{
777 uint64_t start_time;
778
779 if (unlikely(l->info.cur_writer & tid_bit)) {
780 /* the thread is already owning the lock for write */
781 abort();
782 }
783
784 if (unlikely(l->info.cur_readers & tid_bit)) {
785 /* the thread is already owning the lock for read */
786 abort();
787 }
788
789 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
790
791 start_time = nsec_now();
792 __RWLOCK_RDLOCK(&l->lock);
793 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
794 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
795
796 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
797
798 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
799}
800
801static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
802{
803 int r;
804
805 if (unlikely(l->info.cur_writer & tid_bit)) {
806 /* the thread is already owning the lock for write */
807 abort();
808 }
809
810 if (unlikely(l->info.cur_readers & tid_bit)) {
811 /* the thread is already owning the lock for read */
812 abort();
813 }
814
815 /* try read should never wait */
816 r = __RWLOCK_TRYRDLOCK(&l->lock);
817 if (unlikely(r))
818 return r;
819 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
820
821 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
822
823 return 0;
824}
825
826static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
827{
828 if (unlikely(!(l->info.cur_readers & tid_bit))) {
829 /* the thread is not owning the lock for read */
830 abort();
831 }
832
833 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
834
835 __RWLOCK_RDUNLOCK(&l->lock);
836
837 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
838}
839
840static inline void __spin_init(struct ha_spinlock *l)
841{
842 memset(l, 0, sizeof(struct ha_spinlock));
843 __SPIN_INIT(&l->lock);
844}
845
846static inline void __spin_destroy(struct ha_spinlock *l)
847{
848 __SPIN_DESTROY(&l->lock);
849 memset(l, 0, sizeof(struct ha_spinlock));
850}
851
852static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
853 const char *func, const char *file, int line)
854{
855 uint64_t start_time;
856
857 if (unlikely(l->info.owner & tid_bit)) {
858 /* the thread is already owning the lock */
859 abort();
860 }
861
862 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
863
864 start_time = nsec_now();
865 __SPIN_LOCK(&l->lock);
866 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
867
868 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
869
870
871 l->info.owner = tid_bit;
872 l->info.last_location.function = func;
873 l->info.last_location.file = file;
874 l->info.last_location.line = line;
875
876 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
877}
878
879static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
880 const char *func, const char *file, int line)
881{
882 int r;
883
884 if (unlikely(l->info.owner & tid_bit)) {
885 /* the thread is already owning the lock */
886 abort();
887 }
888
889 /* try read should never wait */
890 r = __SPIN_TRYLOCK(&l->lock);
891 if (unlikely(r))
892 return r;
893 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
894
895 l->info.owner = tid_bit;
896 l->info.last_location.function = func;
897 l->info.last_location.file = file;
898 l->info.last_location.line = line;
899
900 return 0;
901}
902
903static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
904 const char *func, const char *file, int line)
905{
906 if (unlikely(!(l->info.owner & tid_bit))) {
907 /* the thread is not owning the lock */
908 abort();
909 }
910
911 l->info.owner = 0;
912 l->info.last_location.function = func;
913 l->info.last_location.file = file;
914 l->info.last_location.line = line;
915
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100916 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200917 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
918}
919
920#else /* DEBUG_THREAD */
921
922#define HA_SPINLOCK_T unsigned long
923
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100924#define HA_SPIN_INIT(l) ({ (*l) = 0; })
925#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
926#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
927#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
928#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200929
930#define HA_RWLOCK_T unsigned long
931
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100932#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
933#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
934#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
935#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
936#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
937#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
938#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
939#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200940
941#endif /* DEBUG_THREAD */
942
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100943#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200944
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100945static __inline int
946__ha_cas_dw(void *target, void *compare, const void *set)
947{
948 char ret;
949
950 __asm __volatile("lock cmpxchg16b %0; setz %3"
951 : "+m" (*(void **)target),
952 "=a" (((void **)compare)[0]),
953 "=d" (((void **)compare)[1]),
954 "=q" (ret)
955 : "a" (((void **)compare)[0]),
956 "d" (((void **)compare)[1]),
957 "b" (((const void **)set)[0]),
958 "c" (((const void **)set)[1])
959 : "memory", "cc");
960 return (ret);
961}
962
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100963/* Use __ha_barrier_atomic* when you're trying to protect data that are
964 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
965 */
966static __inline void
967__ha_barrier_atomic_load(void)
968{
969 __asm __volatile("" ::: "memory");
970}
971
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100972static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100973__ha_barrier_atomic_store(void)
974{
975 __asm __volatile("" ::: "memory");
976}
977
978static __inline void
979__ha_barrier_atomic_full(void)
980{
981 __asm __volatile("" ::: "memory");
982}
983
984static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100985__ha_barrier_load(void)
986{
987 __asm __volatile("lfence" ::: "memory");
988}
989
990static __inline void
991__ha_barrier_store(void)
992{
993 __asm __volatile("sfence" ::: "memory");
994}
995
996static __inline void
997__ha_barrier_full(void)
998{
999 __asm __volatile("mfence" ::: "memory");
1000}
1001
1002#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001003
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001004/* Use __ha_barrier_atomic* when you're trying to protect data that are
1005 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1006 */
1007static __inline void
1008__ha_barrier_atomic_load(void)
1009{
1010 __asm __volatile("dmb" ::: "memory");
1011}
1012
1013static __inline void
1014__ha_barrier_atomic_store(void)
1015{
1016 __asm __volatile("dsb" ::: "memory");
1017}
1018
1019static __inline void
1020__ha_barrier_atomic_full(void)
1021{
1022 __asm __volatile("dmb" ::: "memory");
1023}
1024
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001025static __inline void
1026__ha_barrier_load(void)
1027{
1028 __asm __volatile("dmb" ::: "memory");
1029}
1030
1031static __inline void
1032__ha_barrier_store(void)
1033{
1034 __asm __volatile("dsb" ::: "memory");
1035}
1036
1037static __inline void
1038__ha_barrier_full(void)
1039{
1040 __asm __volatile("dmb" ::: "memory");
1041}
1042
Willy Tarreau41ccb192018-02-14 14:16:28 +01001043static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001044{
1045 uint64_t previous;
1046 int tmp;
1047
1048 __asm __volatile("1:"
1049 "ldrexd %0, [%4];"
1050 "cmp %Q0, %Q2;"
1051 "ittt eq;"
1052 "cmpeq %R0, %R2;"
1053 "strexdeq %1, %3, [%4];"
1054 "cmpeq %1, #1;"
1055 "beq 1b;"
1056 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001057 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001058 : "memory", "cc");
1059 tmp = (previous == *(uint64_t *)compare);
1060 *(uint64_t *)compare = previous;
1061 return (tmp);
1062}
1063
1064#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001065
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001066/* Use __ha_barrier_atomic* when you're trying to protect data that are
1067 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1068 */
1069static __inline void
1070__ha_barrier_atomic_load(void)
1071{
1072 __asm __volatile("dmb ishld" ::: "memory");
1073}
1074
1075static __inline void
1076__ha_barrier_atomic_store(void)
1077{
1078 __asm __volatile("dmb ishst" ::: "memory");
1079}
1080
1081static __inline void
1082__ha_barrier_atomic_full(void)
1083{
1084 __asm __volatile("dmb ish" ::: "memory");
1085}
1086
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001087static __inline void
1088__ha_barrier_load(void)
1089{
1090 __asm __volatile("dmb ishld" ::: "memory");
1091}
1092
1093static __inline void
1094__ha_barrier_store(void)
1095{
1096 __asm __volatile("dmb ishst" ::: "memory");
1097}
1098
1099static __inline void
1100__ha_barrier_full(void)
1101{
1102 __asm __volatile("dmb ish" ::: "memory");
1103}
1104
1105static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1106{
1107 void *value[2];
1108 uint64_t tmp1, tmp2;
1109
1110 __asm__ __volatile__("1:"
1111 "ldxp %0, %1, [%4];"
1112 "mov %2, %0;"
1113 "mov %3, %1;"
1114 "eor %0, %0, %5;"
1115 "eor %1, %1, %6;"
1116 "orr %1, %0, %1;"
1117 "mov %w0, #0;"
1118 "cbnz %1, 2f;"
1119 "stxp %w0, %7, %8, [%4];"
1120 "cbnz %w0, 1b;"
1121 "mov %w0, #1;"
1122 "2:"
1123 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1124 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1125 : "cc", "memory");
1126
1127 memcpy(compare, &value, sizeof(value));
1128 return (tmp1);
1129}
1130
1131#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001132#define __ha_barrier_atomic_load __sync_synchronize
1133#define __ha_barrier_atomic_store __sync_synchronize
1134#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001135#define __ha_barrier_load __sync_synchronize
1136#define __ha_barrier_store __sync_synchronize
1137#define __ha_barrier_full __sync_synchronize
1138#endif
1139
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001140void ha_spin_init(HA_SPINLOCK_T *l);
1141void ha_rwlock_init(HA_RWLOCK_T *l);
1142
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001143#endif /* USE_THREAD */
1144
Willy Tarreau149ab772019-01-26 14:27:06 +01001145extern int thread_cpus_enabled_at_boot;
1146
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001147static inline void __ha_compiler_barrier(void)
1148{
1149 __asm __volatile("" ::: "memory");
1150}
1151
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001152int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001153int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001154
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001155#ifndef _HA_ATOMIC_CAS
1156#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1157#endif /* !_HA_ATOMIC_CAS */
1158
Willy Tarreau6a38b322019-05-11 18:04:24 +02001159#ifndef _HA_ATOMIC_DWCAS
1160#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1161#endif /* !_HA_ATOMIC_CAS */
1162
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001163#ifndef _HA_ATOMIC_ADD
1164#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1165#endif /* !_HA_ATOMIC_ADD */
1166
1167#ifndef _HA_ATOMIC_XADD
1168#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1169#endif /* !_HA_ATOMIC_SUB */
1170
1171#ifndef _HA_ATOMIC_SUB
1172#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1173#endif /* !_HA_ATOMIC_SUB */
1174
1175#ifndef _HA_ATOMIC_AND
1176#define _HA_ATOMIC_AND HA_ATOMIC_AND
1177#endif /* !_HA_ATOMIC_AND */
1178
1179#ifndef _HA_ATOMIC_OR
1180#define _HA_ATOMIC_OR HA_ATOMIC_OR
1181#endif /* !_HA_ATOMIC_OR */
1182
1183#ifndef _HA_ATOMIC_XCHG
1184#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1185#endif /* !_HA_ATOMIC_XCHG */
1186
1187#ifndef _HA_ATOMIC_STORE
1188#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1189#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001190
1191#ifndef _HA_ATOMIC_LOAD
1192#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1193#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001194#endif /* _COMMON_HATHREADS_H */