blob: 806746770e1b3ad33cb06c69d1374e4b3ad521a3 [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,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100481 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100482 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100483 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200484 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000485 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200486 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200487};
488struct lock_stat {
489 uint64_t nsec_wait_for_write;
490 uint64_t nsec_wait_for_read;
491 uint64_t num_write_locked;
492 uint64_t num_write_unlocked;
493 uint64_t num_read_locked;
494 uint64_t num_read_unlocked;
495};
496
497extern struct lock_stat lock_stats[LOCK_LABELS];
498
499#define __HA_SPINLOCK_T unsigned long
500
501#define __SPIN_INIT(l) ({ (*l) = 0; })
502#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100503#define __SPIN_LOCK(l) pl_take_s(l)
504#define __SPIN_TRYLOCK(l) !pl_try_s(l)
505#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200506
507#define __HA_RWLOCK_T unsigned long
508
509#define __RWLOCK_INIT(l) ({ (*l) = 0; })
510#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
511#define __RWLOCK_WRLOCK(l) pl_take_w(l)
512#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
513#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
514#define __RWLOCK_RDLOCK(l) pl_take_r(l)
515#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
516#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
517
518#define HA_SPINLOCK_T struct ha_spinlock
519
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100520#define HA_SPIN_INIT(l) __spin_init(l)
521#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200522
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100523#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
524#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
525#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200526
527#define HA_RWLOCK_T struct ha_rwlock
528
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100529#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
530#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
531#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
532#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
533#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
534#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
535#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
536#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200537
538struct ha_spinlock {
539 __HA_SPINLOCK_T lock;
540 struct {
541 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
542 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
543 struct {
544 const char *function;
545 const char *file;
546 int line;
547 } last_location; /* location of the last owner */
548 } info;
549};
550
551struct ha_rwlock {
552 __HA_RWLOCK_T lock;
553 struct {
554 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
555 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
556 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
557 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
558 struct {
559 const char *function;
560 const char *file;
561 int line;
562 } last_location; /* location of the last write owner */
563 } info;
564};
565
Christopher Fauletf51bac22018-01-30 11:04:29 +0100566static inline const char *lock_label(enum lock_label label)
567{
568 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100569 case FD_LOCK: return "FD";
570 case TASK_RQ_LOCK: return "TASK_RQ";
571 case TASK_WQ_LOCK: return "TASK_WQ";
572 case POOL_LOCK: return "POOL";
573 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100574 case PROXY_LOCK: return "PROXY";
575 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100576 case LBPRM_LOCK: return "LBPRM";
577 case SIGNALS_LOCK: return "SIGNALS";
578 case STK_TABLE_LOCK: return "STK_TABLE";
579 case STK_SESS_LOCK: return "STK_SESS";
580 case APPLETS_LOCK: return "APPLETS";
581 case PEER_LOCK: return "PEER";
582 case BUF_WQ_LOCK: return "BUF_WQ";
583 case STRMS_LOCK: return "STRMS";
584 case SSL_LOCK: return "SSL";
585 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
586 case PATREF_LOCK: return "PATREF";
587 case PATEXP_LOCK: return "PATEXP";
588 case PATLRU_LOCK: return "PATLRU";
589 case VARS_LOCK: return "VARS";
590 case COMP_POOL_LOCK: return "COMP_POOL";
591 case LUA_LOCK: return "LUA";
592 case NOTIF_LOCK: return "NOTIF";
593 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
594 case DNS_LOCK: return "DNS";
595 case PID_LIST_LOCK: return "PID_LIST";
596 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
597 case PIPES_LOCK: return "PIPES";
598 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100599 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100600 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200601 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000602 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100603 case LOCK_LABELS: break; /* keep compiler happy */
604 };
605 /* only way to come here is consecutive to an internal bug */
606 abort();
607}
608
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200609static inline void show_lock_stats()
610{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200611 int lbl;
612
613 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
614 fprintf(stderr,
615 "Stats about Lock %s: \n"
616 "\t # write lock : %lu\n"
617 "\t # write unlock: %lu (%ld)\n"
618 "\t # wait time for write : %.3f msec\n"
619 "\t # wait time for write/lock: %.3f nsec\n"
620 "\t # read lock : %lu\n"
621 "\t # read unlock : %lu (%ld)\n"
622 "\t # wait time for read : %.3f msec\n"
623 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100624 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200625 lock_stats[lbl].num_write_locked,
626 lock_stats[lbl].num_write_unlocked,
627 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
628 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
629 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
630 lock_stats[lbl].num_read_locked,
631 lock_stats[lbl].num_read_unlocked,
632 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
633 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
634 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
635 }
636}
637
638/* Following functions are used to collect some stats about locks. We wrap
639 * pthread functions to known how much time we wait in a lock. */
640
641static uint64_t nsec_now(void) {
642 struct timespec ts;
643
644 clock_gettime(CLOCK_MONOTONIC, &ts);
645 return ((uint64_t) ts.tv_sec * 1000000000ULL +
646 (uint64_t) ts.tv_nsec);
647}
648
649static inline void __ha_rwlock_init(struct ha_rwlock *l)
650{
651 memset(l, 0, sizeof(struct ha_rwlock));
652 __RWLOCK_INIT(&l->lock);
653}
654
655static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
656{
657 __RWLOCK_DESTROY(&l->lock);
658 memset(l, 0, sizeof(struct ha_rwlock));
659}
660
661
662static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
663 const char *func, const char *file, int line)
664{
665 uint64_t start_time;
666
667 if (unlikely(l->info.cur_writer & tid_bit)) {
668 /* the thread is already owning the lock for write */
669 abort();
670 }
671
672 if (unlikely(l->info.cur_readers & tid_bit)) {
673 /* the thread is already owning the lock for read */
674 abort();
675 }
676
677 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
678
679 start_time = nsec_now();
680 __RWLOCK_WRLOCK(&l->lock);
681 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
682
683 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
684
685 l->info.cur_writer = tid_bit;
686 l->info.last_location.function = func;
687 l->info.last_location.file = file;
688 l->info.last_location.line = line;
689
690 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
691}
692
693static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
694 const char *func, const char *file, int line)
695{
696 uint64_t start_time;
697 int r;
698
699 if (unlikely(l->info.cur_writer & tid_bit)) {
700 /* the thread is already owning the lock for write */
701 abort();
702 }
703
704 if (unlikely(l->info.cur_readers & tid_bit)) {
705 /* the thread is already owning the lock for read */
706 abort();
707 }
708
709 /* We set waiting writer because trywrlock could wait for readers to quit */
710 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
711
712 start_time = nsec_now();
713 r = __RWLOCK_TRYWRLOCK(&l->lock);
714 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
715 if (unlikely(r)) {
716 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
717 return r;
718 }
719 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
720
721 l->info.cur_writer = tid_bit;
722 l->info.last_location.function = func;
723 l->info.last_location.file = file;
724 l->info.last_location.line = line;
725
726 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
727
728 return 0;
729}
730
731static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
732 const char *func, const char *file, int line)
733{
734 if (unlikely(!(l->info.cur_writer & tid_bit))) {
735 /* the thread is not owning the lock for write */
736 abort();
737 }
738
739 l->info.cur_writer = 0;
740 l->info.last_location.function = func;
741 l->info.last_location.file = file;
742 l->info.last_location.line = line;
743
744 __RWLOCK_WRUNLOCK(&l->lock);
745
746 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
747}
748
749static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
750{
751 uint64_t start_time;
752
753 if (unlikely(l->info.cur_writer & tid_bit)) {
754 /* the thread is already owning the lock for write */
755 abort();
756 }
757
758 if (unlikely(l->info.cur_readers & tid_bit)) {
759 /* the thread is already owning the lock for read */
760 abort();
761 }
762
763 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
764
765 start_time = nsec_now();
766 __RWLOCK_RDLOCK(&l->lock);
767 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
768 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
769
770 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
771
772 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
773}
774
775static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
776{
777 int r;
778
779 if (unlikely(l->info.cur_writer & tid_bit)) {
780 /* the thread is already owning the lock for write */
781 abort();
782 }
783
784 if (unlikely(l->info.cur_readers & tid_bit)) {
785 /* the thread is already owning the lock for read */
786 abort();
787 }
788
789 /* try read should never wait */
790 r = __RWLOCK_TRYRDLOCK(&l->lock);
791 if (unlikely(r))
792 return r;
793 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
794
795 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
796
797 return 0;
798}
799
800static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
801{
802 if (unlikely(!(l->info.cur_readers & tid_bit))) {
803 /* the thread is not owning the lock for read */
804 abort();
805 }
806
807 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
808
809 __RWLOCK_RDUNLOCK(&l->lock);
810
811 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
812}
813
814static inline void __spin_init(struct ha_spinlock *l)
815{
816 memset(l, 0, sizeof(struct ha_spinlock));
817 __SPIN_INIT(&l->lock);
818}
819
820static inline void __spin_destroy(struct ha_spinlock *l)
821{
822 __SPIN_DESTROY(&l->lock);
823 memset(l, 0, sizeof(struct ha_spinlock));
824}
825
826static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
827 const char *func, const char *file, int line)
828{
829 uint64_t start_time;
830
831 if (unlikely(l->info.owner & tid_bit)) {
832 /* the thread is already owning the lock */
833 abort();
834 }
835
836 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
837
838 start_time = nsec_now();
839 __SPIN_LOCK(&l->lock);
840 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
841
842 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
843
844
845 l->info.owner = tid_bit;
846 l->info.last_location.function = func;
847 l->info.last_location.file = file;
848 l->info.last_location.line = line;
849
850 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
851}
852
853static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
854 const char *func, const char *file, int line)
855{
856 int r;
857
858 if (unlikely(l->info.owner & tid_bit)) {
859 /* the thread is already owning the lock */
860 abort();
861 }
862
863 /* try read should never wait */
864 r = __SPIN_TRYLOCK(&l->lock);
865 if (unlikely(r))
866 return r;
867 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
868
869 l->info.owner = tid_bit;
870 l->info.last_location.function = func;
871 l->info.last_location.file = file;
872 l->info.last_location.line = line;
873
874 return 0;
875}
876
877static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
878 const char *func, const char *file, int line)
879{
880 if (unlikely(!(l->info.owner & tid_bit))) {
881 /* the thread is not owning the lock */
882 abort();
883 }
884
885 l->info.owner = 0;
886 l->info.last_location.function = func;
887 l->info.last_location.file = file;
888 l->info.last_location.line = line;
889
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100890 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200891 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
892}
893
894#else /* DEBUG_THREAD */
895
896#define HA_SPINLOCK_T unsigned long
897
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100898#define HA_SPIN_INIT(l) ({ (*l) = 0; })
899#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
900#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
901#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
902#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200903
904#define HA_RWLOCK_T unsigned long
905
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100906#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
907#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
908#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
909#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
910#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
911#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
912#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
913#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200914
915#endif /* DEBUG_THREAD */
916
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100917#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200918
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100919static __inline int
920__ha_cas_dw(void *target, void *compare, const void *set)
921{
922 char ret;
923
924 __asm __volatile("lock cmpxchg16b %0; setz %3"
925 : "+m" (*(void **)target),
926 "=a" (((void **)compare)[0]),
927 "=d" (((void **)compare)[1]),
928 "=q" (ret)
929 : "a" (((void **)compare)[0]),
930 "d" (((void **)compare)[1]),
931 "b" (((const void **)set)[0]),
932 "c" (((const void **)set)[1])
933 : "memory", "cc");
934 return (ret);
935}
936
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100937/* Use __ha_barrier_atomic* when you're trying to protect data that are
938 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
939 */
940static __inline void
941__ha_barrier_atomic_load(void)
942{
943 __asm __volatile("" ::: "memory");
944}
945
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100946static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100947__ha_barrier_atomic_store(void)
948{
949 __asm __volatile("" ::: "memory");
950}
951
952static __inline void
953__ha_barrier_atomic_full(void)
954{
955 __asm __volatile("" ::: "memory");
956}
957
958static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100959__ha_barrier_load(void)
960{
961 __asm __volatile("lfence" ::: "memory");
962}
963
964static __inline void
965__ha_barrier_store(void)
966{
967 __asm __volatile("sfence" ::: "memory");
968}
969
970static __inline void
971__ha_barrier_full(void)
972{
973 __asm __volatile("mfence" ::: "memory");
974}
975
976#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200977
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100978/* Use __ha_barrier_atomic* when you're trying to protect data that are
979 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
980 */
981static __inline void
982__ha_barrier_atomic_load(void)
983{
984 __asm __volatile("dmb" ::: "memory");
985}
986
987static __inline void
988__ha_barrier_atomic_store(void)
989{
990 __asm __volatile("dsb" ::: "memory");
991}
992
993static __inline void
994__ha_barrier_atomic_full(void)
995{
996 __asm __volatile("dmb" ::: "memory");
997}
998
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100999static __inline void
1000__ha_barrier_load(void)
1001{
1002 __asm __volatile("dmb" ::: "memory");
1003}
1004
1005static __inline void
1006__ha_barrier_store(void)
1007{
1008 __asm __volatile("dsb" ::: "memory");
1009}
1010
1011static __inline void
1012__ha_barrier_full(void)
1013{
1014 __asm __volatile("dmb" ::: "memory");
1015}
1016
Willy Tarreau41ccb192018-02-14 14:16:28 +01001017static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001018{
1019 uint64_t previous;
1020 int tmp;
1021
1022 __asm __volatile("1:"
1023 "ldrexd %0, [%4];"
1024 "cmp %Q0, %Q2;"
1025 "ittt eq;"
1026 "cmpeq %R0, %R2;"
1027 "strexdeq %1, %3, [%4];"
1028 "cmpeq %1, #1;"
1029 "beq 1b;"
1030 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001031 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001032 : "memory", "cc");
1033 tmp = (previous == *(uint64_t *)compare);
1034 *(uint64_t *)compare = previous;
1035 return (tmp);
1036}
1037
1038#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001039
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001040/* Use __ha_barrier_atomic* when you're trying to protect data that are
1041 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1042 */
1043static __inline void
1044__ha_barrier_atomic_load(void)
1045{
1046 __asm __volatile("dmb ishld" ::: "memory");
1047}
1048
1049static __inline void
1050__ha_barrier_atomic_store(void)
1051{
1052 __asm __volatile("dmb ishst" ::: "memory");
1053}
1054
1055static __inline void
1056__ha_barrier_atomic_full(void)
1057{
1058 __asm __volatile("dmb ish" ::: "memory");
1059}
1060
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001061static __inline void
1062__ha_barrier_load(void)
1063{
1064 __asm __volatile("dmb ishld" ::: "memory");
1065}
1066
1067static __inline void
1068__ha_barrier_store(void)
1069{
1070 __asm __volatile("dmb ishst" ::: "memory");
1071}
1072
1073static __inline void
1074__ha_barrier_full(void)
1075{
1076 __asm __volatile("dmb ish" ::: "memory");
1077}
1078
1079static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1080{
1081 void *value[2];
1082 uint64_t tmp1, tmp2;
1083
1084 __asm__ __volatile__("1:"
1085 "ldxp %0, %1, [%4];"
1086 "mov %2, %0;"
1087 "mov %3, %1;"
1088 "eor %0, %0, %5;"
1089 "eor %1, %1, %6;"
1090 "orr %1, %0, %1;"
1091 "mov %w0, #0;"
1092 "cbnz %1, 2f;"
1093 "stxp %w0, %7, %8, [%4];"
1094 "cbnz %w0, 1b;"
1095 "mov %w0, #1;"
1096 "2:"
1097 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1098 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1099 : "cc", "memory");
1100
1101 memcpy(compare, &value, sizeof(value));
1102 return (tmp1);
1103}
1104
1105#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001106#define __ha_barrier_atomic_load __sync_synchronize
1107#define __ha_barrier_atomic_store __sync_synchronize
1108#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001109#define __ha_barrier_load __sync_synchronize
1110#define __ha_barrier_store __sync_synchronize
1111#define __ha_barrier_full __sync_synchronize
1112#endif
1113
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001114void ha_spin_init(HA_SPINLOCK_T *l);
1115void ha_rwlock_init(HA_RWLOCK_T *l);
1116
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001117#endif /* USE_THREAD */
1118
Willy Tarreau149ab772019-01-26 14:27:06 +01001119extern int thread_cpus_enabled_at_boot;
1120
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001121static inline void __ha_compiler_barrier(void)
1122{
1123 __asm __volatile("" ::: "memory");
1124}
1125
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001126int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001127int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001128
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001129#ifndef _HA_ATOMIC_CAS
1130#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1131#endif /* !_HA_ATOMIC_CAS */
1132
Willy Tarreau6a38b322019-05-11 18:04:24 +02001133#ifndef _HA_ATOMIC_DWCAS
1134#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1135#endif /* !_HA_ATOMIC_CAS */
1136
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001137#ifndef _HA_ATOMIC_ADD
1138#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1139#endif /* !_HA_ATOMIC_ADD */
1140
1141#ifndef _HA_ATOMIC_XADD
1142#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1143#endif /* !_HA_ATOMIC_SUB */
1144
1145#ifndef _HA_ATOMIC_SUB
1146#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1147#endif /* !_HA_ATOMIC_SUB */
1148
1149#ifndef _HA_ATOMIC_AND
1150#define _HA_ATOMIC_AND HA_ATOMIC_AND
1151#endif /* !_HA_ATOMIC_AND */
1152
1153#ifndef _HA_ATOMIC_OR
1154#define _HA_ATOMIC_OR HA_ATOMIC_OR
1155#endif /* !_HA_ATOMIC_OR */
1156
1157#ifndef _HA_ATOMIC_XCHG
1158#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1159#endif /* !_HA_ATOMIC_XCHG */
1160
1161#ifndef _HA_ATOMIC_STORE
1162#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1163#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001164
1165#ifndef _HA_ATOMIC_LOAD
1166#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1167#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001168#endif /* _COMMON_HATHREADS_H */