blob: 2be5915c0825a059c1ae98cb663d393ce73c548a [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
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010052#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010053#define __decl_spinlock(lock)
54#define __decl_aligned_spinlock(lock)
55#define __decl_rwlock(lock)
56#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010057
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020058#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreau6a38b322019-05-11 18:04:24 +020059#define HA_ATOMIC_DWCAS(val, o, n) ({((*val) == (*o)) ? (*(val) = (n) , 1) : (*(o) = *(val), 0);})
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020060#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
61#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020062#define HA_ATOMIC_XADD(val, i) \
63 ({ \
64 typeof((val)) __p_xadd = (val); \
65 typeof(*(val)) __old_xadd = *__p_xadd; \
66 *__p_xadd += i; \
67 __old_xadd; \
68 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020069#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
70#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
71#define HA_ATOMIC_XCHG(val, new) \
72 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020073 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020074 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020075 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020076 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010077#define HA_ATOMIC_BTS(val, bit) \
78 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020079 typeof((val)) __p_bts = (val); \
80 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
81 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
82 if (!__t_bts) \
83 *__p_bts |= __b_bts; \
84 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010085 })
86#define HA_ATOMIC_BTR(val, bit) \
87 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020088 typeof((val)) __p_btr = (val); \
89 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
90 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
91 if (__t_btr) \
92 *__p_btr &= ~__b_btr; \
93 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010094 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +020095#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020096#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
97#define HA_ATOMIC_UPDATE_MAX(val, new) \
98 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020099 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200100 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200101 if (*(val) < __new_max) \
102 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200103 *(val); \
104 })
105
106#define HA_ATOMIC_UPDATE_MIN(val, new) \
107 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200108 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200110 if (*(val) > __new_min) \
111 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200112 *(val); \
113 })
114
Willy Tarreaub29dc952017-10-31 18:00:20 +0100115#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200116
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100117#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
118#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
119#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
120#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
121#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200122
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100123#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
124#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
125#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
126#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
127#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
128#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
129#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
130#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200131
William Lallemand6e1796e2018-06-07 11:23:40 +0200132#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
133
Willy Tarreau0c026f42018-08-01 19:12:20 +0200134static inline void ha_set_tid(unsigned int tid)
135{
Willy Tarreau38171da2019-05-17 16:33:13 +0200136}
137
138static inline void ha_thread_relax(void)
139{
140#if _POSIX_PRIORITY_SCHEDULING
141 sched_yield();
142#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200143}
William Lallemand6e1796e2018-06-07 11:23:40 +0200144
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100145static inline void __ha_barrier_atomic_load(void)
146{
147}
148
149static inline void __ha_barrier_atomic_store(void)
150{
151}
152
153static inline void __ha_barrier_atomic_full(void)
154{
155}
156
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100157static inline void __ha_barrier_load(void)
158{
159}
160
161static inline void __ha_barrier_store(void)
162{
163}
164
165static inline void __ha_barrier_full(void)
166{
167}
168
Willy Tarreau60b639c2018-08-02 10:16:17 +0200169static inline void thread_harmless_now()
170{
171}
172
173static inline void thread_harmless_end()
174{
175}
176
177static inline void thread_isolate()
178{
179}
180
181static inline void thread_release()
182{
183}
184
185static inline unsigned long thread_isolated()
186{
187 return 1;
188}
189
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200190#else /* USE_THREAD */
191
192#include <stdio.h>
193#include <stdlib.h>
194#include <string.h>
195#include <pthread.h>
196#include <import/plock.h>
197
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100198#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100199#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100200#endif
201
202#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100203
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100204#define __decl_hathreads(decl) decl
205
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100206/* declare a self-initializing spinlock */
207#define __decl_spinlock(lock) \
208 HA_SPINLOCK_T (lock); \
209 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
210
211/* declare a self-initializing spinlock, aligned on a cache line */
212#define __decl_aligned_spinlock(lock) \
213 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
214 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
215
216/* declare a self-initializing rwlock */
217#define __decl_rwlock(lock) \
218 HA_RWLOCK_T (lock); \
219 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
220
221/* declare a self-initializing rwlock, aligned on a cache line */
222#define __decl_aligned_rwlock(lock) \
223 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
224 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
225
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200226/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
227 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100228
David Carlierec5e8452018-01-11 14:20:43 +0000229#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100230/* gcc < 4.7 */
231
232#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
233#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200234#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100235#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
236#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
237
238/* the CAS is a bit complicated. The older API doesn't support returning the
239 * value and the swap's result at the same time. So here we take what looks
240 * like the safest route, consisting in using the boolean version guaranteeing
241 * that the operation was performed or not, and we snoop a previous value. If
242 * the compare succeeds, we return. If it fails, we return the previous value,
243 * but only if it differs from the expected one. If it's the same it's a race
244 * thus we try again to avoid confusing a possibly sensitive caller.
245 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200246#define HA_ATOMIC_CAS(val, old, new) \
247 ({ \
248 typeof((val)) __val_cas = (val); \
249 typeof((old)) __oldp_cas = (old); \
250 typeof(*(old)) __oldv_cas; \
251 typeof((new)) __new_cas = (new); \
252 int __ret_cas; \
253 do { \
254 __oldv_cas = *__val_cas; \
255 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
256 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
257 if (!__ret_cas) \
258 *__oldp_cas = __oldv_cas; \
259 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100260 })
261
Willy Tarreau6a38b322019-05-11 18:04:24 +0200262#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
263
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200264#define HA_ATOMIC_XCHG(val, new) \
265 ({ \
266 typeof((val)) __val_xchg = (val); \
267 typeof(*(val)) __old_xchg; \
268 typeof((new)) __new_xchg = (new); \
269 do { __old_xchg = *__val_xchg; \
270 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
271 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100272 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100273
274#define HA_ATOMIC_BTS(val, bit) \
275 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200276 typeof(*(val)) __b_bts = (1UL << (bit)); \
277 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100278 })
279
280#define HA_ATOMIC_BTR(val, bit) \
281 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200282 typeof(*(val)) __b_btr = (1UL << (bit)); \
283 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100284 })
285
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200286#define HA_ATOMIC_LOAD(val) \
287 ({ \
288 typeof(*(val)) ret; \
289 __sync_synchronize(); \
290 ret = *(volatile typeof(val))val; \
291 __sync_synchronize(); \
292 ret; \
293 })
294
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200295#define HA_ATOMIC_STORE(val, new) \
296 ({ \
297 typeof((val)) __val_store = (val); \
298 typeof(*(val)) __old_store; \
299 typeof((new)) __new_store = (new); \
300 do { __old_store = *__val_store; \
301 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100302 })
303#else
304/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100305#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 +0200306#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100307#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
308#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
309#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
310#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
311#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100312#define HA_ATOMIC_BTS(val, bit) \
313 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200314 typeof(*(val)) __b_bts = (1UL << (bit)); \
315 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100316 })
317
318#define HA_ATOMIC_BTR(val, bit) \
319 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200320 typeof(*(val)) __b_btr = (1UL << (bit)); \
321 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100322 })
323
Olivier Houchard11353792019-03-07 18:48:22 +0100324#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
325#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200326#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200327
328/* Variants that don't generate any memory barrier.
329 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
330 * that will always generate correct code.
331 * Usually it's fine to use those when updating data that have no dependency,
332 * ie updating a counter. Otherwise a barrier is required.
333 */
334#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 +0200335#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200336#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
337#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
338#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
339#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
340#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
341#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
342#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200343#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200344
345#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100346
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200347#define HA_ATOMIC_UPDATE_MAX(val, new) \
348 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200349 typeof(*(val)) __old_max = *(val); \
350 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200351 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200352 while (__old_max < __new_max && \
353 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
354 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200355 })
356#define HA_ATOMIC_UPDATE_MIN(val, new) \
357 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200358 typeof(*(val)) __old_min = *(val); \
359 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200360 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200361 while (__old_min > __new_min && \
362 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
363 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200364 })
365
Willy Tarreaub29dc952017-10-31 18:00:20 +0100366#define HA_BARRIER() pl_barrier()
367
Willy Tarreau60b639c2018-08-02 10:16:17 +0200368void thread_harmless_till_end();
369void thread_isolate();
370void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200371
Willy Tarreau0c026f42018-08-01 19:12:20 +0200372extern THREAD_LOCAL unsigned int tid; /* The thread id */
373extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200374extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200375extern volatile unsigned long threads_want_rdv_mask;
376extern volatile unsigned long threads_harmless_mask;
377
378/* explanation for threads_want_rdv_mask and threads_harmless_mask :
379 * - threads_want_rdv_mask is a bit field indicating all threads that have
380 * requested a rendez-vous of other threads using thread_isolate().
381 * - threads_harmless_mask is a bit field indicating all threads that are
382 * currently harmless in that they promise not to access a shared resource.
383 *
384 * For a given thread, its bits in want_rdv and harmless can be translated like
385 * this :
386 *
387 * ----------+----------+----------------------------------------------------
388 * want_rdv | harmless | description
389 * ----------+----------+----------------------------------------------------
390 * 0 | 0 | thread not interested in RDV, possibly harmful
391 * 0 | 1 | thread not interested in RDV but harmless
392 * 1 | 1 | thread interested in RDV and waiting for its turn
393 * 1 | 0 | thread currently working isolated from others
394 * ----------+----------+----------------------------------------------------
395 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200396
William Lallemand6e1796e2018-06-07 11:23:40 +0200397#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
398
Willy Tarreau0c026f42018-08-01 19:12:20 +0200399/* sets the thread ID and the TID bit for the current thread */
400static inline void ha_set_tid(unsigned int data)
401{
402 tid = data;
403 tid_bit = (1UL << tid);
404}
405
Willy Tarreau38171da2019-05-17 16:33:13 +0200406static inline void ha_thread_relax(void)
407{
408#if _POSIX_PRIORITY_SCHEDULING
409 sched_yield();
410#else
411 pl_cpu_relax();
412#endif
413}
414
Willy Tarreau60b639c2018-08-02 10:16:17 +0200415/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
416 * not be touching any unprotected shared resource during this period. Usually
417 * this is called before poll(), but it may also be placed around very slow
418 * calls (eg: some crypto operations). Needs to be terminated using
419 * thread_harmless_end().
420 */
421static inline void thread_harmless_now()
422{
423 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
424}
425
426/* Ends the harmless period started by thread_harmless_now(). Usually this is
427 * placed after the poll() call. If it is discovered that a job was running and
428 * is relying on the thread still being harmless, the thread waits for the
429 * other one to finish.
430 */
431static inline void thread_harmless_end()
432{
433 while (1) {
434 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
435 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
436 break;
437 thread_harmless_till_end();
438 }
439}
440
441/* an isolated thread has harmless cleared and want_rdv set */
442static inline unsigned long thread_isolated()
443{
444 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
445}
446
William Lallemand6e1796e2018-06-07 11:23:40 +0200447
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200448#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
449
Christopher Fauletf51bac22018-01-30 11:04:29 +0100450/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200451enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200452 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200453 TASK_RQ_LOCK,
454 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200455 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200456 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200457 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200458 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200459 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200460 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200461 STK_TABLE_LOCK,
462 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200463 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200464 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200465 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200466 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200467 SSL_LOCK,
468 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200469 PATREF_LOCK,
470 PATEXP_LOCK,
471 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200472 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200473 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200474 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200475 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200476 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200477 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200478 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200479 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100480 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100481 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100482 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200483 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000484 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200485 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200486};
487struct lock_stat {
488 uint64_t nsec_wait_for_write;
489 uint64_t nsec_wait_for_read;
490 uint64_t num_write_locked;
491 uint64_t num_write_unlocked;
492 uint64_t num_read_locked;
493 uint64_t num_read_unlocked;
494};
495
496extern struct lock_stat lock_stats[LOCK_LABELS];
497
498#define __HA_SPINLOCK_T unsigned long
499
500#define __SPIN_INIT(l) ({ (*l) = 0; })
501#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100502#define __SPIN_LOCK(l) pl_take_s(l)
503#define __SPIN_TRYLOCK(l) !pl_try_s(l)
504#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200505
506#define __HA_RWLOCK_T unsigned long
507
508#define __RWLOCK_INIT(l) ({ (*l) = 0; })
509#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
510#define __RWLOCK_WRLOCK(l) pl_take_w(l)
511#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
512#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
513#define __RWLOCK_RDLOCK(l) pl_take_r(l)
514#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
515#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
516
517#define HA_SPINLOCK_T struct ha_spinlock
518
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100519#define HA_SPIN_INIT(l) __spin_init(l)
520#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200521
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100522#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
523#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
524#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200525
526#define HA_RWLOCK_T struct ha_rwlock
527
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100528#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
529#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
530#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
531#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
532#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
533#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
534#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
535#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200536
537struct ha_spinlock {
538 __HA_SPINLOCK_T lock;
539 struct {
540 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
541 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
542 struct {
543 const char *function;
544 const char *file;
545 int line;
546 } last_location; /* location of the last owner */
547 } info;
548};
549
550struct ha_rwlock {
551 __HA_RWLOCK_T lock;
552 struct {
553 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
554 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
555 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
556 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
557 struct {
558 const char *function;
559 const char *file;
560 int line;
561 } last_location; /* location of the last write owner */
562 } info;
563};
564
Christopher Fauletf51bac22018-01-30 11:04:29 +0100565static inline const char *lock_label(enum lock_label label)
566{
567 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100568 case FD_LOCK: return "FD";
569 case TASK_RQ_LOCK: return "TASK_RQ";
570 case TASK_WQ_LOCK: return "TASK_WQ";
571 case POOL_LOCK: return "POOL";
572 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100573 case PROXY_LOCK: return "PROXY";
574 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100575 case LBPRM_LOCK: return "LBPRM";
576 case SIGNALS_LOCK: return "SIGNALS";
577 case STK_TABLE_LOCK: return "STK_TABLE";
578 case STK_SESS_LOCK: return "STK_SESS";
579 case APPLETS_LOCK: return "APPLETS";
580 case PEER_LOCK: return "PEER";
581 case BUF_WQ_LOCK: return "BUF_WQ";
582 case STRMS_LOCK: return "STRMS";
583 case SSL_LOCK: return "SSL";
584 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
585 case PATREF_LOCK: return "PATREF";
586 case PATEXP_LOCK: return "PATEXP";
587 case PATLRU_LOCK: return "PATLRU";
588 case VARS_LOCK: return "VARS";
589 case COMP_POOL_LOCK: return "COMP_POOL";
590 case LUA_LOCK: return "LUA";
591 case NOTIF_LOCK: return "NOTIF";
592 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
593 case DNS_LOCK: return "DNS";
594 case PID_LIST_LOCK: return "PID_LIST";
595 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
596 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100597 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100598 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200599 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000600 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100601 case LOCK_LABELS: break; /* keep compiler happy */
602 };
603 /* only way to come here is consecutive to an internal bug */
604 abort();
605}
606
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200607static inline void show_lock_stats()
608{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200609 int lbl;
610
611 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
612 fprintf(stderr,
613 "Stats about Lock %s: \n"
614 "\t # write lock : %lu\n"
615 "\t # write unlock: %lu (%ld)\n"
616 "\t # wait time for write : %.3f msec\n"
617 "\t # wait time for write/lock: %.3f nsec\n"
618 "\t # read lock : %lu\n"
619 "\t # read unlock : %lu (%ld)\n"
620 "\t # wait time for read : %.3f msec\n"
621 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100622 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200623 lock_stats[lbl].num_write_locked,
624 lock_stats[lbl].num_write_unlocked,
625 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
626 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
627 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
628 lock_stats[lbl].num_read_locked,
629 lock_stats[lbl].num_read_unlocked,
630 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
631 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
632 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
633 }
634}
635
636/* Following functions are used to collect some stats about locks. We wrap
637 * pthread functions to known how much time we wait in a lock. */
638
639static uint64_t nsec_now(void) {
640 struct timespec ts;
641
642 clock_gettime(CLOCK_MONOTONIC, &ts);
643 return ((uint64_t) ts.tv_sec * 1000000000ULL +
644 (uint64_t) ts.tv_nsec);
645}
646
647static inline void __ha_rwlock_init(struct ha_rwlock *l)
648{
649 memset(l, 0, sizeof(struct ha_rwlock));
650 __RWLOCK_INIT(&l->lock);
651}
652
653static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
654{
655 __RWLOCK_DESTROY(&l->lock);
656 memset(l, 0, sizeof(struct ha_rwlock));
657}
658
659
660static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
661 const char *func, const char *file, int line)
662{
663 uint64_t start_time;
664
665 if (unlikely(l->info.cur_writer & tid_bit)) {
666 /* the thread is already owning the lock for write */
667 abort();
668 }
669
670 if (unlikely(l->info.cur_readers & tid_bit)) {
671 /* the thread is already owning the lock for read */
672 abort();
673 }
674
675 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
676
677 start_time = nsec_now();
678 __RWLOCK_WRLOCK(&l->lock);
679 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
680
681 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
682
683 l->info.cur_writer = tid_bit;
684 l->info.last_location.function = func;
685 l->info.last_location.file = file;
686 l->info.last_location.line = line;
687
688 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
689}
690
691static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
692 const char *func, const char *file, int line)
693{
694 uint64_t start_time;
695 int r;
696
697 if (unlikely(l->info.cur_writer & tid_bit)) {
698 /* the thread is already owning the lock for write */
699 abort();
700 }
701
702 if (unlikely(l->info.cur_readers & tid_bit)) {
703 /* the thread is already owning the lock for read */
704 abort();
705 }
706
707 /* We set waiting writer because trywrlock could wait for readers to quit */
708 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
709
710 start_time = nsec_now();
711 r = __RWLOCK_TRYWRLOCK(&l->lock);
712 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
713 if (unlikely(r)) {
714 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
715 return r;
716 }
717 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
718
719 l->info.cur_writer = tid_bit;
720 l->info.last_location.function = func;
721 l->info.last_location.file = file;
722 l->info.last_location.line = line;
723
724 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
725
726 return 0;
727}
728
729static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
730 const char *func, const char *file, int line)
731{
732 if (unlikely(!(l->info.cur_writer & tid_bit))) {
733 /* the thread is not owning the lock for write */
734 abort();
735 }
736
737 l->info.cur_writer = 0;
738 l->info.last_location.function = func;
739 l->info.last_location.file = file;
740 l->info.last_location.line = line;
741
742 __RWLOCK_WRUNLOCK(&l->lock);
743
744 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
745}
746
747static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
748{
749 uint64_t start_time;
750
751 if (unlikely(l->info.cur_writer & tid_bit)) {
752 /* the thread is already owning the lock for write */
753 abort();
754 }
755
756 if (unlikely(l->info.cur_readers & tid_bit)) {
757 /* the thread is already owning the lock for read */
758 abort();
759 }
760
761 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
762
763 start_time = nsec_now();
764 __RWLOCK_RDLOCK(&l->lock);
765 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
766 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
767
768 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
769
770 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
771}
772
773static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
774{
775 int r;
776
777 if (unlikely(l->info.cur_writer & tid_bit)) {
778 /* the thread is already owning the lock for write */
779 abort();
780 }
781
782 if (unlikely(l->info.cur_readers & tid_bit)) {
783 /* the thread is already owning the lock for read */
784 abort();
785 }
786
787 /* try read should never wait */
788 r = __RWLOCK_TRYRDLOCK(&l->lock);
789 if (unlikely(r))
790 return r;
791 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
792
793 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
794
795 return 0;
796}
797
798static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
799{
800 if (unlikely(!(l->info.cur_readers & tid_bit))) {
801 /* the thread is not owning the lock for read */
802 abort();
803 }
804
805 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
806
807 __RWLOCK_RDUNLOCK(&l->lock);
808
809 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
810}
811
812static inline void __spin_init(struct ha_spinlock *l)
813{
814 memset(l, 0, sizeof(struct ha_spinlock));
815 __SPIN_INIT(&l->lock);
816}
817
818static inline void __spin_destroy(struct ha_spinlock *l)
819{
820 __SPIN_DESTROY(&l->lock);
821 memset(l, 0, sizeof(struct ha_spinlock));
822}
823
824static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
825 const char *func, const char *file, int line)
826{
827 uint64_t start_time;
828
829 if (unlikely(l->info.owner & tid_bit)) {
830 /* the thread is already owning the lock */
831 abort();
832 }
833
834 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
835
836 start_time = nsec_now();
837 __SPIN_LOCK(&l->lock);
838 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
839
840 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
841
842
843 l->info.owner = tid_bit;
844 l->info.last_location.function = func;
845 l->info.last_location.file = file;
846 l->info.last_location.line = line;
847
848 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
849}
850
851static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
852 const char *func, const char *file, int line)
853{
854 int r;
855
856 if (unlikely(l->info.owner & tid_bit)) {
857 /* the thread is already owning the lock */
858 abort();
859 }
860
861 /* try read should never wait */
862 r = __SPIN_TRYLOCK(&l->lock);
863 if (unlikely(r))
864 return r;
865 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
866
867 l->info.owner = tid_bit;
868 l->info.last_location.function = func;
869 l->info.last_location.file = file;
870 l->info.last_location.line = line;
871
872 return 0;
873}
874
875static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
876 const char *func, const char *file, int line)
877{
878 if (unlikely(!(l->info.owner & tid_bit))) {
879 /* the thread is not owning the lock */
880 abort();
881 }
882
883 l->info.owner = 0;
884 l->info.last_location.function = func;
885 l->info.last_location.file = file;
886 l->info.last_location.line = line;
887
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100888 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200889 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
890}
891
892#else /* DEBUG_THREAD */
893
894#define HA_SPINLOCK_T unsigned long
895
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100896#define HA_SPIN_INIT(l) ({ (*l) = 0; })
897#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
898#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
899#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
900#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200901
902#define HA_RWLOCK_T unsigned long
903
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100904#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
905#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
906#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
907#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
908#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
909#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
910#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
911#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200912
913#endif /* DEBUG_THREAD */
914
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100915#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200916
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100917static __inline int
918__ha_cas_dw(void *target, void *compare, const void *set)
919{
920 char ret;
921
922 __asm __volatile("lock cmpxchg16b %0; setz %3"
923 : "+m" (*(void **)target),
924 "=a" (((void **)compare)[0]),
925 "=d" (((void **)compare)[1]),
926 "=q" (ret)
927 : "a" (((void **)compare)[0]),
928 "d" (((void **)compare)[1]),
929 "b" (((const void **)set)[0]),
930 "c" (((const void **)set)[1])
931 : "memory", "cc");
932 return (ret);
933}
934
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100935/* Use __ha_barrier_atomic* when you're trying to protect data that are
936 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
937 */
938static __inline void
939__ha_barrier_atomic_load(void)
940{
941 __asm __volatile("" ::: "memory");
942}
943
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100944static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100945__ha_barrier_atomic_store(void)
946{
947 __asm __volatile("" ::: "memory");
948}
949
950static __inline void
951__ha_barrier_atomic_full(void)
952{
953 __asm __volatile("" ::: "memory");
954}
955
956static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100957__ha_barrier_load(void)
958{
959 __asm __volatile("lfence" ::: "memory");
960}
961
962static __inline void
963__ha_barrier_store(void)
964{
965 __asm __volatile("sfence" ::: "memory");
966}
967
968static __inline void
969__ha_barrier_full(void)
970{
971 __asm __volatile("mfence" ::: "memory");
972}
973
974#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200975
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100976/* Use __ha_barrier_atomic* when you're trying to protect data that are
977 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
978 */
979static __inline void
980__ha_barrier_atomic_load(void)
981{
982 __asm __volatile("dmb" ::: "memory");
983}
984
985static __inline void
986__ha_barrier_atomic_store(void)
987{
988 __asm __volatile("dsb" ::: "memory");
989}
990
991static __inline void
992__ha_barrier_atomic_full(void)
993{
994 __asm __volatile("dmb" ::: "memory");
995}
996
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100997static __inline void
998__ha_barrier_load(void)
999{
1000 __asm __volatile("dmb" ::: "memory");
1001}
1002
1003static __inline void
1004__ha_barrier_store(void)
1005{
1006 __asm __volatile("dsb" ::: "memory");
1007}
1008
1009static __inline void
1010__ha_barrier_full(void)
1011{
1012 __asm __volatile("dmb" ::: "memory");
1013}
1014
Willy Tarreau41ccb192018-02-14 14:16:28 +01001015static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001016{
1017 uint64_t previous;
1018 int tmp;
1019
1020 __asm __volatile("1:"
1021 "ldrexd %0, [%4];"
1022 "cmp %Q0, %Q2;"
1023 "ittt eq;"
1024 "cmpeq %R0, %R2;"
1025 "strexdeq %1, %3, [%4];"
1026 "cmpeq %1, #1;"
1027 "beq 1b;"
1028 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001029 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001030 : "memory", "cc");
1031 tmp = (previous == *(uint64_t *)compare);
1032 *(uint64_t *)compare = previous;
1033 return (tmp);
1034}
1035
1036#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001037
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001038/* Use __ha_barrier_atomic* when you're trying to protect data that are
1039 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1040 */
1041static __inline void
1042__ha_barrier_atomic_load(void)
1043{
1044 __asm __volatile("dmb ishld" ::: "memory");
1045}
1046
1047static __inline void
1048__ha_barrier_atomic_store(void)
1049{
1050 __asm __volatile("dmb ishst" ::: "memory");
1051}
1052
1053static __inline void
1054__ha_barrier_atomic_full(void)
1055{
1056 __asm __volatile("dmb ish" ::: "memory");
1057}
1058
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001059static __inline void
1060__ha_barrier_load(void)
1061{
1062 __asm __volatile("dmb ishld" ::: "memory");
1063}
1064
1065static __inline void
1066__ha_barrier_store(void)
1067{
1068 __asm __volatile("dmb ishst" ::: "memory");
1069}
1070
1071static __inline void
1072__ha_barrier_full(void)
1073{
1074 __asm __volatile("dmb ish" ::: "memory");
1075}
1076
1077static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1078{
1079 void *value[2];
1080 uint64_t tmp1, tmp2;
1081
1082 __asm__ __volatile__("1:"
1083 "ldxp %0, %1, [%4];"
1084 "mov %2, %0;"
1085 "mov %3, %1;"
1086 "eor %0, %0, %5;"
1087 "eor %1, %1, %6;"
1088 "orr %1, %0, %1;"
1089 "mov %w0, #0;"
1090 "cbnz %1, 2f;"
1091 "stxp %w0, %7, %8, [%4];"
1092 "cbnz %w0, 1b;"
1093 "mov %w0, #1;"
1094 "2:"
1095 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1096 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1097 : "cc", "memory");
1098
1099 memcpy(compare, &value, sizeof(value));
1100 return (tmp1);
1101}
1102
1103#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001104#define __ha_barrier_atomic_load __sync_synchronize
1105#define __ha_barrier_atomic_store __sync_synchronize
1106#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001107#define __ha_barrier_load __sync_synchronize
1108#define __ha_barrier_store __sync_synchronize
1109#define __ha_barrier_full __sync_synchronize
1110#endif
1111
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001112void ha_spin_init(HA_SPINLOCK_T *l);
1113void ha_rwlock_init(HA_RWLOCK_T *l);
1114
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001115#endif /* USE_THREAD */
1116
Willy Tarreau149ab772019-01-26 14:27:06 +01001117extern int thread_cpus_enabled_at_boot;
1118
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001119static inline void __ha_compiler_barrier(void)
1120{
1121 __asm __volatile("" ::: "memory");
1122}
1123
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001124int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001125int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001126
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001127#ifndef _HA_ATOMIC_CAS
1128#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1129#endif /* !_HA_ATOMIC_CAS */
1130
Willy Tarreau6a38b322019-05-11 18:04:24 +02001131#ifndef _HA_ATOMIC_DWCAS
1132#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1133#endif /* !_HA_ATOMIC_CAS */
1134
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001135#ifndef _HA_ATOMIC_ADD
1136#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1137#endif /* !_HA_ATOMIC_ADD */
1138
1139#ifndef _HA_ATOMIC_XADD
1140#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1141#endif /* !_HA_ATOMIC_SUB */
1142
1143#ifndef _HA_ATOMIC_SUB
1144#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1145#endif /* !_HA_ATOMIC_SUB */
1146
1147#ifndef _HA_ATOMIC_AND
1148#define _HA_ATOMIC_AND HA_ATOMIC_AND
1149#endif /* !_HA_ATOMIC_AND */
1150
1151#ifndef _HA_ATOMIC_OR
1152#define _HA_ATOMIC_OR HA_ATOMIC_OR
1153#endif /* !_HA_ATOMIC_OR */
1154
1155#ifndef _HA_ATOMIC_XCHG
1156#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1157#endif /* !_HA_ATOMIC_XCHG */
1158
1159#ifndef _HA_ATOMIC_STORE
1160#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1161#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001162
1163#ifndef _HA_ATOMIC_LOAD
1164#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1165#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001166#endif /* _COMMON_HATHREADS_H */