blob: b3c45641ff21a980331032d7c395103df1f5c444 [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 Tarreau5a6e2242019-05-20 18:56:48 +020054 /* pad to cache line (64B) */
55 char __pad[0]; /* unused except to check remaining room */
56 char __end[0] __attribute__((aligned(64)));
57} thread_info[MAX_THREADS];
58
Willy Tarreau8323a372019-05-20 18:57:53 +020059extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
60
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010061#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010062#define __decl_spinlock(lock)
63#define __decl_aligned_spinlock(lock)
64#define __decl_rwlock(lock)
65#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010066
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020067#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020068#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020069#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
70#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020071#define HA_ATOMIC_XADD(val, i) \
72 ({ \
73 typeof((val)) __p_xadd = (val); \
74 typeof(*(val)) __old_xadd = *__p_xadd; \
75 *__p_xadd += i; \
76 __old_xadd; \
77 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020078#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
79#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
80#define HA_ATOMIC_XCHG(val, new) \
81 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020082 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020083 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020084 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020085 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010086#define HA_ATOMIC_BTS(val, bit) \
87 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020088 typeof((val)) __p_bts = (val); \
89 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
90 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
91 if (!__t_bts) \
92 *__p_bts |= __b_bts; \
93 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010094 })
95#define HA_ATOMIC_BTR(val, bit) \
96 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020097 typeof((val)) __p_btr = (val); \
98 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
99 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
100 if (__t_btr) \
101 *__p_btr &= ~__b_btr; \
102 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100103 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200104#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200105#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
106#define HA_ATOMIC_UPDATE_MAX(val, new) \
107 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200108 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200110 if (*(val) < __new_max) \
111 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200112 *(val); \
113 })
114
115#define HA_ATOMIC_UPDATE_MIN(val, new) \
116 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200117 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200118 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200119 if (*(val) > __new_min) \
120 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200121 *(val); \
122 })
123
Willy Tarreaub29dc952017-10-31 18:00:20 +0100124#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200125
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100126#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
127#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
128#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
129#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
130#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200131
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100132#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
133#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
134#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
135#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
136#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
137#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
138#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
139#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200140
William Lallemand6e1796e2018-06-07 11:23:40 +0200141#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
142
Willy Tarreau0c026f42018-08-01 19:12:20 +0200143static inline void ha_set_tid(unsigned int tid)
144{
Willy Tarreau8323a372019-05-20 18:57:53 +0200145 ti = &thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200146}
147
148static inline void ha_thread_relax(void)
149{
150#if _POSIX_PRIORITY_SCHEDULING
151 sched_yield();
152#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200153}
William Lallemand6e1796e2018-06-07 11:23:40 +0200154
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100155static inline void __ha_barrier_atomic_load(void)
156{
157}
158
159static inline void __ha_barrier_atomic_store(void)
160{
161}
162
163static inline void __ha_barrier_atomic_full(void)
164{
165}
166
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100167static inline void __ha_barrier_load(void)
168{
169}
170
171static inline void __ha_barrier_store(void)
172{
173}
174
175static inline void __ha_barrier_full(void)
176{
177}
178
Willy Tarreau60b639c2018-08-02 10:16:17 +0200179static inline void thread_harmless_now()
180{
181}
182
183static inline void thread_harmless_end()
184{
185}
186
187static inline void thread_isolate()
188{
189}
190
191static inline void thread_release()
192{
193}
194
195static inline unsigned long thread_isolated()
196{
197 return 1;
198}
199
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200200#else /* USE_THREAD */
201
202#include <stdio.h>
203#include <stdlib.h>
204#include <string.h>
205#include <pthread.h>
206#include <import/plock.h>
207
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100208#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100209#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100210#endif
211
212#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100213
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100214#define __decl_hathreads(decl) decl
215
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100216/* declare a self-initializing spinlock */
217#define __decl_spinlock(lock) \
218 HA_SPINLOCK_T (lock); \
219 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
220
221/* declare a self-initializing spinlock, aligned on a cache line */
222#define __decl_aligned_spinlock(lock) \
223 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
224 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
225
226/* declare a self-initializing rwlock */
227#define __decl_rwlock(lock) \
228 HA_RWLOCK_T (lock); \
229 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
230
231/* declare a self-initializing rwlock, aligned on a cache line */
232#define __decl_aligned_rwlock(lock) \
233 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
234 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
235
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200236/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
237 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100238
David Carlierec5e8452018-01-11 14:20:43 +0000239#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100240/* gcc < 4.7 */
241
242#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
243#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200244#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100245#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
246#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
247
248/* the CAS is a bit complicated. The older API doesn't support returning the
249 * value and the swap's result at the same time. So here we take what looks
250 * like the safest route, consisting in using the boolean version guaranteeing
251 * that the operation was performed or not, and we snoop a previous value. If
252 * the compare succeeds, we return. If it fails, we return the previous value,
253 * but only if it differs from the expected one. If it's the same it's a race
254 * thus we try again to avoid confusing a possibly sensitive caller.
255 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200256#define HA_ATOMIC_CAS(val, old, new) \
257 ({ \
258 typeof((val)) __val_cas = (val); \
259 typeof((old)) __oldp_cas = (old); \
260 typeof(*(old)) __oldv_cas; \
261 typeof((new)) __new_cas = (new); \
262 int __ret_cas; \
263 do { \
264 __oldv_cas = *__val_cas; \
265 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
266 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
267 if (!__ret_cas) \
268 *__oldp_cas = __oldv_cas; \
269 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100270 })
271
Willy Tarreau6a38b322019-05-11 18:04:24 +0200272#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
273
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200274#define HA_ATOMIC_XCHG(val, new) \
275 ({ \
276 typeof((val)) __val_xchg = (val); \
277 typeof(*(val)) __old_xchg; \
278 typeof((new)) __new_xchg = (new); \
279 do { __old_xchg = *__val_xchg; \
280 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
281 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100282 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100283
284#define HA_ATOMIC_BTS(val, bit) \
285 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200286 typeof(*(val)) __b_bts = (1UL << (bit)); \
287 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100288 })
289
290#define HA_ATOMIC_BTR(val, bit) \
291 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200292 typeof(*(val)) __b_btr = (1UL << (bit)); \
293 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100294 })
295
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200296#define HA_ATOMIC_LOAD(val) \
297 ({ \
298 typeof(*(val)) ret; \
299 __sync_synchronize(); \
300 ret = *(volatile typeof(val))val; \
301 __sync_synchronize(); \
302 ret; \
303 })
304
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200305#define HA_ATOMIC_STORE(val, new) \
306 ({ \
307 typeof((val)) __val_store = (val); \
308 typeof(*(val)) __old_store; \
309 typeof((new)) __new_store = (new); \
310 do { __old_store = *__val_store; \
311 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100312 })
313#else
314/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100315#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 +0200316#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100317#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
318#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
319#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
320#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
321#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100322#define HA_ATOMIC_BTS(val, bit) \
323 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200324 typeof(*(val)) __b_bts = (1UL << (bit)); \
325 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100326 })
327
328#define HA_ATOMIC_BTR(val, bit) \
329 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200330 typeof(*(val)) __b_btr = (1UL << (bit)); \
331 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100332 })
333
Olivier Houchard11353792019-03-07 18:48:22 +0100334#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
335#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200336#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200337
338/* Variants that don't generate any memory barrier.
339 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
340 * that will always generate correct code.
341 * Usually it's fine to use those when updating data that have no dependency,
342 * ie updating a counter. Otherwise a barrier is required.
343 */
344#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 +0200345#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200346#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
347#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
348#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
349#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
350#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
351#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
352#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200353#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200354
355#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100356
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200357#define HA_ATOMIC_UPDATE_MAX(val, new) \
358 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200359 typeof(*(val)) __old_max = *(val); \
360 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200361 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200362 while (__old_max < __new_max && \
363 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
364 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200365 })
366#define HA_ATOMIC_UPDATE_MIN(val, new) \
367 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200368 typeof(*(val)) __old_min = *(val); \
369 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200370 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200371 while (__old_min > __new_min && \
372 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
373 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200374 })
375
Willy Tarreaub29dc952017-10-31 18:00:20 +0100376#define HA_BARRIER() pl_barrier()
377
Willy Tarreau60b639c2018-08-02 10:16:17 +0200378void thread_harmless_till_end();
379void thread_isolate();
380void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200381
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200382extern struct thread_info {
383 pthread_t pthread;
384 clockid_t clock_id;
385 /* pad to cache line (64B) */
386 char __pad[0]; /* unused except to check remaining room */
387 char __end[0] __attribute__((aligned(64)));
388} thread_info[MAX_THREADS];
389
Willy Tarreau0c026f42018-08-01 19:12:20 +0200390extern THREAD_LOCAL unsigned int tid; /* The thread id */
391extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200392extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200393extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200394extern volatile unsigned long threads_want_rdv_mask;
395extern volatile unsigned long threads_harmless_mask;
396
397/* explanation for threads_want_rdv_mask and threads_harmless_mask :
398 * - threads_want_rdv_mask is a bit field indicating all threads that have
399 * requested a rendez-vous of other threads using thread_isolate().
400 * - threads_harmless_mask is a bit field indicating all threads that are
401 * currently harmless in that they promise not to access a shared resource.
402 *
403 * For a given thread, its bits in want_rdv and harmless can be translated like
404 * this :
405 *
406 * ----------+----------+----------------------------------------------------
407 * want_rdv | harmless | description
408 * ----------+----------+----------------------------------------------------
409 * 0 | 0 | thread not interested in RDV, possibly harmful
410 * 0 | 1 | thread not interested in RDV but harmless
411 * 1 | 1 | thread interested in RDV and waiting for its turn
412 * 1 | 0 | thread currently working isolated from others
413 * ----------+----------+----------------------------------------------------
414 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200415
William Lallemand6e1796e2018-06-07 11:23:40 +0200416#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
417
Willy Tarreau0c026f42018-08-01 19:12:20 +0200418/* sets the thread ID and the TID bit for the current thread */
419static inline void ha_set_tid(unsigned int data)
420{
421 tid = data;
422 tid_bit = (1UL << tid);
Willy Tarreau8323a372019-05-20 18:57:53 +0200423 ti = &thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200424}
425
Willy Tarreau38171da2019-05-17 16:33:13 +0200426static inline void ha_thread_relax(void)
427{
428#if _POSIX_PRIORITY_SCHEDULING
429 sched_yield();
430#else
431 pl_cpu_relax();
432#endif
433}
434
Willy Tarreau60b639c2018-08-02 10:16:17 +0200435/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
436 * not be touching any unprotected shared resource during this period. Usually
437 * this is called before poll(), but it may also be placed around very slow
438 * calls (eg: some crypto operations). Needs to be terminated using
439 * thread_harmless_end().
440 */
441static inline void thread_harmless_now()
442{
443 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
444}
445
446/* Ends the harmless period started by thread_harmless_now(). Usually this is
447 * placed after the poll() call. If it is discovered that a job was running and
448 * is relying on the thread still being harmless, the thread waits for the
449 * other one to finish.
450 */
451static inline void thread_harmless_end()
452{
453 while (1) {
454 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
455 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
456 break;
457 thread_harmless_till_end();
458 }
459}
460
461/* an isolated thread has harmless cleared and want_rdv set */
462static inline unsigned long thread_isolated()
463{
464 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
465}
466
William Lallemand6e1796e2018-06-07 11:23:40 +0200467
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200468#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
469
Christopher Fauletf51bac22018-01-30 11:04:29 +0100470/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200471enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200472 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200473 TASK_RQ_LOCK,
474 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200475 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200476 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200477 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200478 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200479 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200480 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200481 STK_TABLE_LOCK,
482 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200483 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200484 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200485 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200486 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200487 SSL_LOCK,
488 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200489 PATREF_LOCK,
490 PATEXP_LOCK,
491 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200492 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200493 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200494 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200495 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200496 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200497 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200498 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200499 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100500 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100501 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100502 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200503 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000504 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200505 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200506};
507struct lock_stat {
508 uint64_t nsec_wait_for_write;
509 uint64_t nsec_wait_for_read;
510 uint64_t num_write_locked;
511 uint64_t num_write_unlocked;
512 uint64_t num_read_locked;
513 uint64_t num_read_unlocked;
514};
515
516extern struct lock_stat lock_stats[LOCK_LABELS];
517
518#define __HA_SPINLOCK_T unsigned long
519
520#define __SPIN_INIT(l) ({ (*l) = 0; })
521#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100522#define __SPIN_LOCK(l) pl_take_s(l)
523#define __SPIN_TRYLOCK(l) !pl_try_s(l)
524#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200525
526#define __HA_RWLOCK_T unsigned long
527
528#define __RWLOCK_INIT(l) ({ (*l) = 0; })
529#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
530#define __RWLOCK_WRLOCK(l) pl_take_w(l)
531#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
532#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
533#define __RWLOCK_RDLOCK(l) pl_take_r(l)
534#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
535#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
536
537#define HA_SPINLOCK_T struct ha_spinlock
538
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100539#define HA_SPIN_INIT(l) __spin_init(l)
540#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200541
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100542#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
543#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
544#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200545
546#define HA_RWLOCK_T struct ha_rwlock
547
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100548#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
549#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
550#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
551#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
552#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
553#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
554#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
555#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200556
557struct ha_spinlock {
558 __HA_SPINLOCK_T lock;
559 struct {
560 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
561 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
562 struct {
563 const char *function;
564 const char *file;
565 int line;
566 } last_location; /* location of the last owner */
567 } info;
568};
569
570struct ha_rwlock {
571 __HA_RWLOCK_T lock;
572 struct {
573 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
574 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
575 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
576 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
577 struct {
578 const char *function;
579 const char *file;
580 int line;
581 } last_location; /* location of the last write owner */
582 } info;
583};
584
Christopher Fauletf51bac22018-01-30 11:04:29 +0100585static inline const char *lock_label(enum lock_label label)
586{
587 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100588 case FD_LOCK: return "FD";
589 case TASK_RQ_LOCK: return "TASK_RQ";
590 case TASK_WQ_LOCK: return "TASK_WQ";
591 case POOL_LOCK: return "POOL";
592 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100593 case PROXY_LOCK: return "PROXY";
594 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100595 case LBPRM_LOCK: return "LBPRM";
596 case SIGNALS_LOCK: return "SIGNALS";
597 case STK_TABLE_LOCK: return "STK_TABLE";
598 case STK_SESS_LOCK: return "STK_SESS";
599 case APPLETS_LOCK: return "APPLETS";
600 case PEER_LOCK: return "PEER";
601 case BUF_WQ_LOCK: return "BUF_WQ";
602 case STRMS_LOCK: return "STRMS";
603 case SSL_LOCK: return "SSL";
604 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
605 case PATREF_LOCK: return "PATREF";
606 case PATEXP_LOCK: return "PATEXP";
607 case PATLRU_LOCK: return "PATLRU";
608 case VARS_LOCK: return "VARS";
609 case COMP_POOL_LOCK: return "COMP_POOL";
610 case LUA_LOCK: return "LUA";
611 case NOTIF_LOCK: return "NOTIF";
612 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
613 case DNS_LOCK: return "DNS";
614 case PID_LIST_LOCK: return "PID_LIST";
615 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
616 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100617 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100618 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200619 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000620 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100621 case LOCK_LABELS: break; /* keep compiler happy */
622 };
623 /* only way to come here is consecutive to an internal bug */
624 abort();
625}
626
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200627static inline void show_lock_stats()
628{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200629 int lbl;
630
631 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
632 fprintf(stderr,
633 "Stats about Lock %s: \n"
634 "\t # write lock : %lu\n"
635 "\t # write unlock: %lu (%ld)\n"
636 "\t # wait time for write : %.3f msec\n"
637 "\t # wait time for write/lock: %.3f nsec\n"
638 "\t # read lock : %lu\n"
639 "\t # read unlock : %lu (%ld)\n"
640 "\t # wait time for read : %.3f msec\n"
641 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100642 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200643 lock_stats[lbl].num_write_locked,
644 lock_stats[lbl].num_write_unlocked,
645 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
646 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
647 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
648 lock_stats[lbl].num_read_locked,
649 lock_stats[lbl].num_read_unlocked,
650 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
651 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
652 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
653 }
654}
655
656/* Following functions are used to collect some stats about locks. We wrap
657 * pthread functions to known how much time we wait in a lock. */
658
659static uint64_t nsec_now(void) {
660 struct timespec ts;
661
662 clock_gettime(CLOCK_MONOTONIC, &ts);
663 return ((uint64_t) ts.tv_sec * 1000000000ULL +
664 (uint64_t) ts.tv_nsec);
665}
666
667static inline void __ha_rwlock_init(struct ha_rwlock *l)
668{
669 memset(l, 0, sizeof(struct ha_rwlock));
670 __RWLOCK_INIT(&l->lock);
671}
672
673static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
674{
675 __RWLOCK_DESTROY(&l->lock);
676 memset(l, 0, sizeof(struct ha_rwlock));
677}
678
679
680static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
681 const char *func, const char *file, int line)
682{
683 uint64_t start_time;
684
685 if (unlikely(l->info.cur_writer & tid_bit)) {
686 /* the thread is already owning the lock for write */
687 abort();
688 }
689
690 if (unlikely(l->info.cur_readers & tid_bit)) {
691 /* the thread is already owning the lock for read */
692 abort();
693 }
694
695 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
696
697 start_time = nsec_now();
698 __RWLOCK_WRLOCK(&l->lock);
699 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
700
701 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
702
703 l->info.cur_writer = tid_bit;
704 l->info.last_location.function = func;
705 l->info.last_location.file = file;
706 l->info.last_location.line = line;
707
708 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
709}
710
711static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
712 const char *func, const char *file, int line)
713{
714 uint64_t start_time;
715 int r;
716
717 if (unlikely(l->info.cur_writer & tid_bit)) {
718 /* the thread is already owning the lock for write */
719 abort();
720 }
721
722 if (unlikely(l->info.cur_readers & tid_bit)) {
723 /* the thread is already owning the lock for read */
724 abort();
725 }
726
727 /* We set waiting writer because trywrlock could wait for readers to quit */
728 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
729
730 start_time = nsec_now();
731 r = __RWLOCK_TRYWRLOCK(&l->lock);
732 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
733 if (unlikely(r)) {
734 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
735 return r;
736 }
737 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
738
739 l->info.cur_writer = tid_bit;
740 l->info.last_location.function = func;
741 l->info.last_location.file = file;
742 l->info.last_location.line = line;
743
744 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
745
746 return 0;
747}
748
749static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
750 const char *func, const char *file, int line)
751{
752 if (unlikely(!(l->info.cur_writer & tid_bit))) {
753 /* the thread is not owning the lock for write */
754 abort();
755 }
756
757 l->info.cur_writer = 0;
758 l->info.last_location.function = func;
759 l->info.last_location.file = file;
760 l->info.last_location.line = line;
761
762 __RWLOCK_WRUNLOCK(&l->lock);
763
764 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
765}
766
767static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
768{
769 uint64_t start_time;
770
771 if (unlikely(l->info.cur_writer & tid_bit)) {
772 /* the thread is already owning the lock for write */
773 abort();
774 }
775
776 if (unlikely(l->info.cur_readers & tid_bit)) {
777 /* the thread is already owning the lock for read */
778 abort();
779 }
780
781 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
782
783 start_time = nsec_now();
784 __RWLOCK_RDLOCK(&l->lock);
785 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
786 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
787
788 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
789
790 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
791}
792
793static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
794{
795 int r;
796
797 if (unlikely(l->info.cur_writer & tid_bit)) {
798 /* the thread is already owning the lock for write */
799 abort();
800 }
801
802 if (unlikely(l->info.cur_readers & tid_bit)) {
803 /* the thread is already owning the lock for read */
804 abort();
805 }
806
807 /* try read should never wait */
808 r = __RWLOCK_TRYRDLOCK(&l->lock);
809 if (unlikely(r))
810 return r;
811 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
812
813 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
814
815 return 0;
816}
817
818static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
819{
820 if (unlikely(!(l->info.cur_readers & tid_bit))) {
821 /* the thread is not owning the lock for read */
822 abort();
823 }
824
825 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
826
827 __RWLOCK_RDUNLOCK(&l->lock);
828
829 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
830}
831
832static inline void __spin_init(struct ha_spinlock *l)
833{
834 memset(l, 0, sizeof(struct ha_spinlock));
835 __SPIN_INIT(&l->lock);
836}
837
838static inline void __spin_destroy(struct ha_spinlock *l)
839{
840 __SPIN_DESTROY(&l->lock);
841 memset(l, 0, sizeof(struct ha_spinlock));
842}
843
844static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
845 const char *func, const char *file, int line)
846{
847 uint64_t start_time;
848
849 if (unlikely(l->info.owner & tid_bit)) {
850 /* the thread is already owning the lock */
851 abort();
852 }
853
854 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
855
856 start_time = nsec_now();
857 __SPIN_LOCK(&l->lock);
858 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
859
860 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
861
862
863 l->info.owner = tid_bit;
864 l->info.last_location.function = func;
865 l->info.last_location.file = file;
866 l->info.last_location.line = line;
867
868 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
869}
870
871static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
872 const char *func, const char *file, int line)
873{
874 int r;
875
876 if (unlikely(l->info.owner & tid_bit)) {
877 /* the thread is already owning the lock */
878 abort();
879 }
880
881 /* try read should never wait */
882 r = __SPIN_TRYLOCK(&l->lock);
883 if (unlikely(r))
884 return r;
885 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
886
887 l->info.owner = tid_bit;
888 l->info.last_location.function = func;
889 l->info.last_location.file = file;
890 l->info.last_location.line = line;
891
892 return 0;
893}
894
895static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
896 const char *func, const char *file, int line)
897{
898 if (unlikely(!(l->info.owner & tid_bit))) {
899 /* the thread is not owning the lock */
900 abort();
901 }
902
903 l->info.owner = 0;
904 l->info.last_location.function = func;
905 l->info.last_location.file = file;
906 l->info.last_location.line = line;
907
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100908 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200909 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
910}
911
912#else /* DEBUG_THREAD */
913
914#define HA_SPINLOCK_T unsigned long
915
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100916#define HA_SPIN_INIT(l) ({ (*l) = 0; })
917#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
918#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
919#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
920#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200921
922#define HA_RWLOCK_T unsigned long
923
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100924#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
925#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
926#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
927#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
928#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
929#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
930#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
931#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200932
933#endif /* DEBUG_THREAD */
934
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100935#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200936
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100937static __inline int
938__ha_cas_dw(void *target, void *compare, const void *set)
939{
940 char ret;
941
942 __asm __volatile("lock cmpxchg16b %0; setz %3"
943 : "+m" (*(void **)target),
944 "=a" (((void **)compare)[0]),
945 "=d" (((void **)compare)[1]),
946 "=q" (ret)
947 : "a" (((void **)compare)[0]),
948 "d" (((void **)compare)[1]),
949 "b" (((const void **)set)[0]),
950 "c" (((const void **)set)[1])
951 : "memory", "cc");
952 return (ret);
953}
954
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100955/* Use __ha_barrier_atomic* when you're trying to protect data that are
956 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
957 */
958static __inline void
959__ha_barrier_atomic_load(void)
960{
961 __asm __volatile("" ::: "memory");
962}
963
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100964static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100965__ha_barrier_atomic_store(void)
966{
967 __asm __volatile("" ::: "memory");
968}
969
970static __inline void
971__ha_barrier_atomic_full(void)
972{
973 __asm __volatile("" ::: "memory");
974}
975
976static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100977__ha_barrier_load(void)
978{
979 __asm __volatile("lfence" ::: "memory");
980}
981
982static __inline void
983__ha_barrier_store(void)
984{
985 __asm __volatile("sfence" ::: "memory");
986}
987
988static __inline void
989__ha_barrier_full(void)
990{
991 __asm __volatile("mfence" ::: "memory");
992}
993
994#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200995
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100996/* Use __ha_barrier_atomic* when you're trying to protect data that are
997 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
998 */
999static __inline void
1000__ha_barrier_atomic_load(void)
1001{
1002 __asm __volatile("dmb" ::: "memory");
1003}
1004
1005static __inline void
1006__ha_barrier_atomic_store(void)
1007{
1008 __asm __volatile("dsb" ::: "memory");
1009}
1010
1011static __inline void
1012__ha_barrier_atomic_full(void)
1013{
1014 __asm __volatile("dmb" ::: "memory");
1015}
1016
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001017static __inline void
1018__ha_barrier_load(void)
1019{
1020 __asm __volatile("dmb" ::: "memory");
1021}
1022
1023static __inline void
1024__ha_barrier_store(void)
1025{
1026 __asm __volatile("dsb" ::: "memory");
1027}
1028
1029static __inline void
1030__ha_barrier_full(void)
1031{
1032 __asm __volatile("dmb" ::: "memory");
1033}
1034
Willy Tarreau41ccb192018-02-14 14:16:28 +01001035static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001036{
1037 uint64_t previous;
1038 int tmp;
1039
1040 __asm __volatile("1:"
1041 "ldrexd %0, [%4];"
1042 "cmp %Q0, %Q2;"
1043 "ittt eq;"
1044 "cmpeq %R0, %R2;"
1045 "strexdeq %1, %3, [%4];"
1046 "cmpeq %1, #1;"
1047 "beq 1b;"
1048 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001049 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001050 : "memory", "cc");
1051 tmp = (previous == *(uint64_t *)compare);
1052 *(uint64_t *)compare = previous;
1053 return (tmp);
1054}
1055
1056#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001057
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001058/* Use __ha_barrier_atomic* when you're trying to protect data that are
1059 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1060 */
1061static __inline void
1062__ha_barrier_atomic_load(void)
1063{
1064 __asm __volatile("dmb ishld" ::: "memory");
1065}
1066
1067static __inline void
1068__ha_barrier_atomic_store(void)
1069{
1070 __asm __volatile("dmb ishst" ::: "memory");
1071}
1072
1073static __inline void
1074__ha_barrier_atomic_full(void)
1075{
1076 __asm __volatile("dmb ish" ::: "memory");
1077}
1078
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001079static __inline void
1080__ha_barrier_load(void)
1081{
1082 __asm __volatile("dmb ishld" ::: "memory");
1083}
1084
1085static __inline void
1086__ha_barrier_store(void)
1087{
1088 __asm __volatile("dmb ishst" ::: "memory");
1089}
1090
1091static __inline void
1092__ha_barrier_full(void)
1093{
1094 __asm __volatile("dmb ish" ::: "memory");
1095}
1096
1097static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1098{
1099 void *value[2];
1100 uint64_t tmp1, tmp2;
1101
1102 __asm__ __volatile__("1:"
1103 "ldxp %0, %1, [%4];"
1104 "mov %2, %0;"
1105 "mov %3, %1;"
1106 "eor %0, %0, %5;"
1107 "eor %1, %1, %6;"
1108 "orr %1, %0, %1;"
1109 "mov %w0, #0;"
1110 "cbnz %1, 2f;"
1111 "stxp %w0, %7, %8, [%4];"
1112 "cbnz %w0, 1b;"
1113 "mov %w0, #1;"
1114 "2:"
1115 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1116 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1117 : "cc", "memory");
1118
1119 memcpy(compare, &value, sizeof(value));
1120 return (tmp1);
1121}
1122
1123#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001124#define __ha_barrier_atomic_load __sync_synchronize
1125#define __ha_barrier_atomic_store __sync_synchronize
1126#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001127#define __ha_barrier_load __sync_synchronize
1128#define __ha_barrier_store __sync_synchronize
1129#define __ha_barrier_full __sync_synchronize
1130#endif
1131
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001132void ha_spin_init(HA_SPINLOCK_T *l);
1133void ha_rwlock_init(HA_RWLOCK_T *l);
1134
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001135#endif /* USE_THREAD */
1136
Willy Tarreau149ab772019-01-26 14:27:06 +01001137extern int thread_cpus_enabled_at_boot;
1138
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001139static inline void __ha_compiler_barrier(void)
1140{
1141 __asm __volatile("" ::: "memory");
1142}
1143
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001144int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001145int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001146
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001147#ifndef _HA_ATOMIC_CAS
1148#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1149#endif /* !_HA_ATOMIC_CAS */
1150
Willy Tarreau6a38b322019-05-11 18:04:24 +02001151#ifndef _HA_ATOMIC_DWCAS
1152#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1153#endif /* !_HA_ATOMIC_CAS */
1154
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001155#ifndef _HA_ATOMIC_ADD
1156#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1157#endif /* !_HA_ATOMIC_ADD */
1158
1159#ifndef _HA_ATOMIC_XADD
1160#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1161#endif /* !_HA_ATOMIC_SUB */
1162
1163#ifndef _HA_ATOMIC_SUB
1164#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1165#endif /* !_HA_ATOMIC_SUB */
1166
1167#ifndef _HA_ATOMIC_AND
1168#define _HA_ATOMIC_AND HA_ATOMIC_AND
1169#endif /* !_HA_ATOMIC_AND */
1170
1171#ifndef _HA_ATOMIC_OR
1172#define _HA_ATOMIC_OR HA_ATOMIC_OR
1173#endif /* !_HA_ATOMIC_OR */
1174
1175#ifndef _HA_ATOMIC_XCHG
1176#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1177#endif /* !_HA_ATOMIC_XCHG */
1178
1179#ifndef _HA_ATOMIC_STORE
1180#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1181#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001182
1183#ifndef _HA_ATOMIC_LOAD
1184#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1185#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001186#endif /* _COMMON_HATHREADS_H */