blob: 0ba56d0d0054838c1454244c3906b1c89cab2d26 [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 Tarreau2beaaf72019-05-22 08:43:34 +020025#include <signal.h>
Willy Tarreau38171da2019-05-17 16:33:13 +020026#include <unistd.h>
27#ifdef _POSIX_PRIORITY_SCHEDULING
28#include <sched.h>
29#endif
30
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020031#include <common/config.h>
Willy Tarreau90fa97b2018-11-25 19:46:08 +010032#include <common/initcall.h>
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020033
Willy Tarreau0ccd3222018-07-30 10:34:35 +020034/* Note about all_threads_mask :
Willy Tarreauda9e9392019-02-02 17:03:41 +010035 * - this variable is comprised between 1 and LONGBITS.
36 * - with threads support disabled, this symbol is defined as constant 1UL.
37 * - with threads enabled, it contains the mask of enabled threads. Thus if
38 * only one thread is enabled, it equals 1.
Willy Tarreau0ccd3222018-07-30 10:34:35 +020039 */
40
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020041/* thread info flags, for thread_info[].flags */
42#define TI_FL_STUCK 0x00000001
43
44
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020045#ifndef USE_THREAD
46
Willy Tarreau421f02e2018-01-20 18:19:22 +010047#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020048#define MAX_THREADS_MASK 1
49
50/* Only way found to replace variables with constants that are optimized away
51 * at build time.
52 */
53enum { all_threads_mask = 1UL };
Willy Tarreau441259c2019-05-22 07:48:18 +020054enum { threads_harmless_mask = 0 };
55enum { threads_want_rdv_mask = 0 };
Willy Tarreau0c026f42018-08-01 19:12:20 +020056enum { tid_bit = 1UL };
57enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010058
Willy Tarreau5a6e2242019-05-20 18:56:48 +020059extern struct thread_info {
Willy Tarreau624dcbf2019-05-20 20:23:06 +020060 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +020061 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +020062 uint64_t prev_cpu_time; /* previous per thread CPU time */
63 uint64_t prev_mono_time; /* previous system wide monotonic time */
64 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +020065 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +020066 /* pad to cache line (64B) */
67 char __pad[0]; /* unused except to check remaining room */
68 char __end[0] __attribute__((aligned(64)));
69} thread_info[MAX_THREADS];
70
Willy Tarreau8323a372019-05-20 18:57:53 +020071extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
72
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010073#define __decl_hathreads(decl)
Willy Tarreau90fa97b2018-11-25 19:46:08 +010074#define __decl_spinlock(lock)
75#define __decl_aligned_spinlock(lock)
76#define __decl_rwlock(lock)
77#define __decl_aligned_rwlock(lock)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010078
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020079#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
Willy Tarreauc3b59582019-05-27 17:37:20 +020080
81/* warning, n is a pointer to the double value for dwcas */
82#define HA_ATOMIC_DWCAS(val, o, n) \
83 ({ \
84 long *_v = (long*)(val); \
85 long *_o = (long*)(o); \
86 long *_n = (long*)(n); \
87 long _v0 = _v[0], _v1 = _v[1]; \
88 (_v0 == _o[0] && _v1 == _o[1]) ? \
89 (_v[0] = _n[0], _v[1] = _n[1], 1) : \
90 (_o[0] = _v0, _o[1] = _v1, 0); \
91 })
92
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020093#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
94#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
Willy Tarreau9378df82018-09-05 16:11:03 +020095#define HA_ATOMIC_XADD(val, i) \
96 ({ \
97 typeof((val)) __p_xadd = (val); \
98 typeof(*(val)) __old_xadd = *__p_xadd; \
99 *__p_xadd += i; \
100 __old_xadd; \
101 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200102#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
103#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
104#define HA_ATOMIC_XCHG(val, new) \
105 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200106 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200107 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200108 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100110#define HA_ATOMIC_BTS(val, bit) \
111 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200112 typeof((val)) __p_bts = (val); \
113 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
114 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
115 if (!__t_bts) \
116 *__p_bts |= __b_bts; \
117 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100118 })
119#define HA_ATOMIC_BTR(val, bit) \
120 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200121 typeof((val)) __p_btr = (val); \
122 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
123 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
124 if (__t_btr) \
125 *__p_btr &= ~__b_btr; \
126 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100127 })
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200128#define HA_ATOMIC_LOAD(val) *(val)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200129#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
130#define HA_ATOMIC_UPDATE_MAX(val, new) \
131 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200132 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200133 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200134 if (*(val) < __new_max) \
135 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200136 *(val); \
137 })
138
139#define HA_ATOMIC_UPDATE_MIN(val, new) \
140 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200141 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200142 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200143 if (*(val) > __new_min) \
144 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200145 *(val); \
146 })
147
Willy Tarreaub29dc952017-10-31 18:00:20 +0100148#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +0200149
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100150#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
151#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
152#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
153#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
154#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200155
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100156#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
157#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
158#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
159#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
160#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
161#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
162#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
163#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200164
William Lallemand6e1796e2018-06-07 11:23:40 +0200165#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
166
Willy Tarreau0c026f42018-08-01 19:12:20 +0200167static inline void ha_set_tid(unsigned int tid)
168{
Willy Tarreau8323a372019-05-20 18:57:53 +0200169 ti = &thread_info[tid];
Willy Tarreau38171da2019-05-17 16:33:13 +0200170}
171
172static inline void ha_thread_relax(void)
173{
174#if _POSIX_PRIORITY_SCHEDULING
175 sched_yield();
176#endif
Willy Tarreau0c026f42018-08-01 19:12:20 +0200177}
William Lallemand6e1796e2018-06-07 11:23:40 +0200178
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200179/* send signal <sig> to thread <thr> */
180static inline void ha_tkill(unsigned int thr, int sig)
181{
182 raise(sig);
183}
184
185/* send signal <sig> to all threads */
186static inline void ha_tkillall(int sig)
187{
188 raise(sig);
189}
190
Olivier Houchard9abcf6e2019-03-07 18:45:00 +0100191static inline void __ha_barrier_atomic_load(void)
192{
193}
194
195static inline void __ha_barrier_atomic_store(void)
196{
197}
198
199static inline void __ha_barrier_atomic_full(void)
200{
201}
202
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100203static inline void __ha_barrier_load(void)
204{
205}
206
207static inline void __ha_barrier_store(void)
208{
209}
210
211static inline void __ha_barrier_full(void)
212{
213}
214
Willy Tarreau60b639c2018-08-02 10:16:17 +0200215static inline void thread_harmless_now()
216{
217}
218
219static inline void thread_harmless_end()
220{
221}
222
223static inline void thread_isolate()
224{
225}
226
227static inline void thread_release()
228{
229}
230
231static inline unsigned long thread_isolated()
232{
233 return 1;
234}
235
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200236#else /* USE_THREAD */
237
238#include <stdio.h>
239#include <stdlib.h>
240#include <string.h>
241#include <pthread.h>
242#include <import/plock.h>
243
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100244#ifndef MAX_THREADS
Willy Tarreau421f02e2018-01-20 18:19:22 +0100245#define MAX_THREADS LONGBITS
Willy Tarreauf5809cd2019-01-26 13:35:03 +0100246#endif
247
248#define MAX_THREADS_MASK (~0UL >> (LONGBITS - MAX_THREADS))
Willy Tarreau421f02e2018-01-20 18:19:22 +0100249
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100250#define __decl_hathreads(decl) decl
251
Willy Tarreau90fa97b2018-11-25 19:46:08 +0100252/* declare a self-initializing spinlock */
253#define __decl_spinlock(lock) \
254 HA_SPINLOCK_T (lock); \
255 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
256
257/* declare a self-initializing spinlock, aligned on a cache line */
258#define __decl_aligned_spinlock(lock) \
259 HA_SPINLOCK_T (lock) __attribute__((aligned(64))); \
260 INITCALL1(STG_LOCK, ha_spin_init, &(lock))
261
262/* declare a self-initializing rwlock */
263#define __decl_rwlock(lock) \
264 HA_RWLOCK_T (lock); \
265 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
266
267/* declare a self-initializing rwlock, aligned on a cache line */
268#define __decl_aligned_rwlock(lock) \
269 HA_RWLOCK_T (lock) __attribute__((aligned(64))); \
270 INITCALL1(STG_LOCK, ha_rwlock_init, &(lock))
271
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200272/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
273 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100274
David Carlierec5e8452018-01-11 14:20:43 +0000275#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100276/* gcc < 4.7 */
277
278#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
279#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
Willy Tarreau9378df82018-09-05 16:11:03 +0200280#define HA_ATOMIC_XADD(val, i) __sync_fetch_and_add(val, i)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100281#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
282#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
283
284/* the CAS is a bit complicated. The older API doesn't support returning the
285 * value and the swap's result at the same time. So here we take what looks
286 * like the safest route, consisting in using the boolean version guaranteeing
287 * that the operation was performed or not, and we snoop a previous value. If
288 * the compare succeeds, we return. If it fails, we return the previous value,
289 * but only if it differs from the expected one. If it's the same it's a race
290 * thus we try again to avoid confusing a possibly sensitive caller.
291 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200292#define HA_ATOMIC_CAS(val, old, new) \
293 ({ \
294 typeof((val)) __val_cas = (val); \
295 typeof((old)) __oldp_cas = (old); \
296 typeof(*(old)) __oldv_cas; \
297 typeof((new)) __new_cas = (new); \
298 int __ret_cas; \
299 do { \
300 __oldv_cas = *__val_cas; \
301 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
302 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
303 if (!__ret_cas) \
304 *__oldp_cas = __oldv_cas; \
305 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100306 })
307
Willy Tarreauc3b59582019-05-27 17:37:20 +0200308/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200309#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
310
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200311#define HA_ATOMIC_XCHG(val, new) \
312 ({ \
313 typeof((val)) __val_xchg = (val); \
314 typeof(*(val)) __old_xchg; \
315 typeof((new)) __new_xchg = (new); \
316 do { __old_xchg = *__val_xchg; \
317 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
318 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100319 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100320
321#define HA_ATOMIC_BTS(val, bit) \
322 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200323 typeof(*(val)) __b_bts = (1UL << (bit)); \
324 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100325 })
326
327#define HA_ATOMIC_BTR(val, bit) \
328 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200329 typeof(*(val)) __b_btr = (1UL << (bit)); \
330 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100331 })
332
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200333#define HA_ATOMIC_LOAD(val) \
334 ({ \
335 typeof(*(val)) ret; \
336 __sync_synchronize(); \
337 ret = *(volatile typeof(val))val; \
338 __sync_synchronize(); \
339 ret; \
340 })
341
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200342#define HA_ATOMIC_STORE(val, new) \
343 ({ \
344 typeof((val)) __val_store = (val); \
345 typeof(*(val)) __old_store; \
346 typeof((new)) __new_store = (new); \
347 do { __old_store = *__val_store; \
348 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100349 })
350#else
351/* gcc >= 4.7 */
Olivier Houchard11353792019-03-07 18:48:22 +0100352#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
Willy Tarreauc3b59582019-05-27 17:37:20 +0200353/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200354#define HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard11353792019-03-07 18:48:22 +0100355#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_SEQ_CST)
356#define HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_SEQ_CST)
357#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_SEQ_CST)
358#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_SEQ_CST)
359#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_SEQ_CST)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100360#define HA_ATOMIC_BTS(val, bit) \
361 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200362 typeof(*(val)) __b_bts = (1UL << (bit)); \
363 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100364 })
365
366#define HA_ATOMIC_BTR(val, bit) \
367 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200368 typeof(*(val)) __b_btr = (1UL << (bit)); \
369 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100370 })
371
Olivier Houchard11353792019-03-07 18:48:22 +0100372#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_SEQ_CST)
373#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_SEQ_CST)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200374#define HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_SEQ_CST)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200375
376/* Variants that don't generate any memory barrier.
377 * If you're unsure how to deal with barriers, just use the HA_ATOMIC_* version,
378 * that will always generate correct code.
379 * Usually it's fine to use those when updating data that have no dependency,
380 * ie updating a counter. Otherwise a barrier is required.
381 */
382#define _HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)
Willy Tarreauc3b59582019-05-27 17:37:20 +0200383/* warning, n is a pointer to the double value for dwcas */
Willy Tarreau6a38b322019-05-11 18:04:24 +0200384#define _HA_ATOMIC_DWCAS(val, o, n) __ha_cas_dw(val, o, n)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200385#define _HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, __ATOMIC_RELAXED)
386#define _HA_ATOMIC_XADD(val, i) __atomic_fetch_add(val, i, __ATOMIC_RELAXED)
387#define _HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, __ATOMIC_RELAXED)
388#define _HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, __ATOMIC_RELAXED)
389#define _HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, __ATOMIC_RELAXED)
390#define _HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, __ATOMIC_RELAXED)
391#define _HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, __ATOMIC_RELAXED)
Olivier Houchard9ce62b52019-04-30 13:38:02 +0200392#define _HA_ATOMIC_LOAD(val) __atomic_load_n(val, __ATOMIC_RELAXED)
Olivier Houchard3212a2c2019-04-15 21:14:25 +0200393
394#endif /* gcc >= 4.7 */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100395
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200396#define HA_ATOMIC_UPDATE_MAX(val, new) \
397 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200398 typeof(*(val)) __old_max = *(val); \
399 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200400 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200401 while (__old_max < __new_max && \
402 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
403 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200404 })
405#define HA_ATOMIC_UPDATE_MIN(val, new) \
406 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200407 typeof(*(val)) __old_min = *(val); \
408 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200409 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200410 while (__old_min > __new_min && \
411 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
412 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200413 })
414
Willy Tarreaub29dc952017-10-31 18:00:20 +0100415#define HA_BARRIER() pl_barrier()
416
Willy Tarreau60b639c2018-08-02 10:16:17 +0200417void thread_harmless_till_end();
418void thread_isolate();
419void thread_release();
Willy Tarreau2beaaf72019-05-22 08:43:34 +0200420void ha_tkill(unsigned int thr, int sig);
421void ha_tkillall(int sig);
Christopher Faulet339fff82017-10-19 11:59:15 +0200422
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200423extern struct thread_info {
424 pthread_t pthread;
425 clockid_t clock_id;
Willy Tarreau430f5902019-05-21 20:01:26 +0200426 timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
Willy Tarreau81036f22019-05-20 19:24:50 +0200427 uint64_t prev_cpu_time; /* previous per thread CPU time */
428 uint64_t prev_mono_time; /* previous system wide monotonic time */
429 unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaue6a02fa2019-05-22 07:06:44 +0200430 unsigned int flags; /* thread info flags, TI_FL_* */
Willy Tarreau5a6e2242019-05-20 18:56:48 +0200431 /* pad to cache line (64B) */
432 char __pad[0]; /* unused except to check remaining room */
433 char __end[0] __attribute__((aligned(64)));
434} thread_info[MAX_THREADS];
435
Willy Tarreau0c026f42018-08-01 19:12:20 +0200436extern THREAD_LOCAL unsigned int tid; /* The thread id */
437extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Willy Tarreau8323a372019-05-20 18:57:53 +0200438extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200439extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200440extern volatile unsigned long threads_want_rdv_mask;
441extern volatile unsigned long threads_harmless_mask;
442
443/* explanation for threads_want_rdv_mask and threads_harmless_mask :
444 * - threads_want_rdv_mask is a bit field indicating all threads that have
445 * requested a rendez-vous of other threads using thread_isolate().
446 * - threads_harmless_mask is a bit field indicating all threads that are
447 * currently harmless in that they promise not to access a shared resource.
448 *
449 * For a given thread, its bits in want_rdv and harmless can be translated like
450 * this :
451 *
452 * ----------+----------+----------------------------------------------------
453 * want_rdv | harmless | description
454 * ----------+----------+----------------------------------------------------
455 * 0 | 0 | thread not interested in RDV, possibly harmful
456 * 0 | 1 | thread not interested in RDV but harmless
457 * 1 | 1 | thread interested in RDV and waiting for its turn
458 * 1 | 0 | thread currently working isolated from others
459 * ----------+----------+----------------------------------------------------
460 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200461
William Lallemand6e1796e2018-06-07 11:23:40 +0200462#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
463
Willy Tarreau0c026f42018-08-01 19:12:20 +0200464/* sets the thread ID and the TID bit for the current thread */
465static inline void ha_set_tid(unsigned int data)
466{
467 tid = data;
468 tid_bit = (1UL << tid);
Willy Tarreau8323a372019-05-20 18:57:53 +0200469 ti = &thread_info[tid];
Willy Tarreau0c026f42018-08-01 19:12:20 +0200470}
471
Willy Tarreau38171da2019-05-17 16:33:13 +0200472static inline void ha_thread_relax(void)
473{
474#if _POSIX_PRIORITY_SCHEDULING
475 sched_yield();
476#else
477 pl_cpu_relax();
478#endif
479}
480
Willy Tarreau60b639c2018-08-02 10:16:17 +0200481/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
482 * not be touching any unprotected shared resource during this period. Usually
483 * this is called before poll(), but it may also be placed around very slow
484 * calls (eg: some crypto operations). Needs to be terminated using
485 * thread_harmless_end().
486 */
487static inline void thread_harmless_now()
488{
489 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
490}
491
492/* Ends the harmless period started by thread_harmless_now(). Usually this is
493 * placed after the poll() call. If it is discovered that a job was running and
494 * is relying on the thread still being harmless, the thread waits for the
495 * other one to finish.
496 */
497static inline void thread_harmless_end()
498{
499 while (1) {
500 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
501 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
502 break;
503 thread_harmless_till_end();
504 }
505}
506
507/* an isolated thread has harmless cleared and want_rdv set */
508static inline unsigned long thread_isolated()
509{
510 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
511}
512
William Lallemand6e1796e2018-06-07 11:23:40 +0200513
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200514#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
515
Christopher Fauletf51bac22018-01-30 11:04:29 +0100516/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200517enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200518 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200519 TASK_RQ_LOCK,
520 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200521 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200522 LISTENER_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200523 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200524 SERVER_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200525 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200526 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200527 STK_TABLE_LOCK,
528 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200529 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200530 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200531 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200532 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200533 SSL_LOCK,
534 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200535 PATREF_LOCK,
536 PATEXP_LOCK,
537 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200538 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200539 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200540 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200541 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200542 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200543 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200544 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200545 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100546 PIPES_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100547 TLSKEYS_REF_LOCK,
Willy Tarreau34d4b522018-10-29 18:02:54 +0100548 AUTH_LOCK,
Frédéric Lécailled803e472019-04-25 07:42:09 +0200549 LOGSRV_LOCK,
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000550 OTHER_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200551 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200552};
553struct lock_stat {
554 uint64_t nsec_wait_for_write;
555 uint64_t nsec_wait_for_read;
556 uint64_t num_write_locked;
557 uint64_t num_write_unlocked;
558 uint64_t num_read_locked;
559 uint64_t num_read_unlocked;
560};
561
562extern struct lock_stat lock_stats[LOCK_LABELS];
563
564#define __HA_SPINLOCK_T unsigned long
565
566#define __SPIN_INIT(l) ({ (*l) = 0; })
567#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100568#define __SPIN_LOCK(l) pl_take_s(l)
569#define __SPIN_TRYLOCK(l) !pl_try_s(l)
570#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200571
572#define __HA_RWLOCK_T unsigned long
573
574#define __RWLOCK_INIT(l) ({ (*l) = 0; })
575#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
576#define __RWLOCK_WRLOCK(l) pl_take_w(l)
577#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
578#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
579#define __RWLOCK_RDLOCK(l) pl_take_r(l)
580#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
581#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
582
583#define HA_SPINLOCK_T struct ha_spinlock
584
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100585#define HA_SPIN_INIT(l) __spin_init(l)
586#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200587
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100588#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
589#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
590#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200591
592#define HA_RWLOCK_T struct ha_rwlock
593
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100594#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
595#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
596#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
597#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
598#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
599#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
600#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
601#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200602
603struct ha_spinlock {
604 __HA_SPINLOCK_T lock;
605 struct {
606 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
607 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
608 struct {
609 const char *function;
610 const char *file;
611 int line;
612 } last_location; /* location of the last owner */
613 } info;
614};
615
616struct ha_rwlock {
617 __HA_RWLOCK_T lock;
618 struct {
619 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
620 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
621 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
622 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
623 struct {
624 const char *function;
625 const char *file;
626 int line;
627 } last_location; /* location of the last write owner */
628 } info;
629};
630
Christopher Fauletf51bac22018-01-30 11:04:29 +0100631static inline const char *lock_label(enum lock_label label)
632{
633 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100634 case FD_LOCK: return "FD";
635 case TASK_RQ_LOCK: return "TASK_RQ";
636 case TASK_WQ_LOCK: return "TASK_WQ";
637 case POOL_LOCK: return "POOL";
638 case LISTENER_LOCK: return "LISTENER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100639 case PROXY_LOCK: return "PROXY";
640 case SERVER_LOCK: return "SERVER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100641 case LBPRM_LOCK: return "LBPRM";
642 case SIGNALS_LOCK: return "SIGNALS";
643 case STK_TABLE_LOCK: return "STK_TABLE";
644 case STK_SESS_LOCK: return "STK_SESS";
645 case APPLETS_LOCK: return "APPLETS";
646 case PEER_LOCK: return "PEER";
647 case BUF_WQ_LOCK: return "BUF_WQ";
648 case STRMS_LOCK: return "STRMS";
649 case SSL_LOCK: return "SSL";
650 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
651 case PATREF_LOCK: return "PATREF";
652 case PATEXP_LOCK: return "PATEXP";
653 case PATLRU_LOCK: return "PATLRU";
654 case VARS_LOCK: return "VARS";
655 case COMP_POOL_LOCK: return "COMP_POOL";
656 case LUA_LOCK: return "LUA";
657 case NOTIF_LOCK: return "NOTIF";
658 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
659 case DNS_LOCK: return "DNS";
660 case PID_LIST_LOCK: return "PID_LIST";
661 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
662 case PIPES_LOCK: return "PIPES";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100663 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Willy Tarreau34d4b522018-10-29 18:02:54 +0100664 case AUTH_LOCK: return "AUTH";
Frédéric Lécailled803e472019-04-25 07:42:09 +0200665 case LOGSRV_LOCK: return "LOGSRV";
Ben51Degrees4ddf59d2019-02-05 13:24:00 +0000666 case OTHER_LOCK: return "OTHER";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100667 case LOCK_LABELS: break; /* keep compiler happy */
668 };
669 /* only way to come here is consecutive to an internal bug */
670 abort();
671}
672
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200673static inline void show_lock_stats()
674{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200675 int lbl;
676
677 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
678 fprintf(stderr,
679 "Stats about Lock %s: \n"
680 "\t # write lock : %lu\n"
681 "\t # write unlock: %lu (%ld)\n"
682 "\t # wait time for write : %.3f msec\n"
683 "\t # wait time for write/lock: %.3f nsec\n"
684 "\t # read lock : %lu\n"
685 "\t # read unlock : %lu (%ld)\n"
686 "\t # wait time for read : %.3f msec\n"
687 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100688 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200689 lock_stats[lbl].num_write_locked,
690 lock_stats[lbl].num_write_unlocked,
691 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
692 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
693 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
694 lock_stats[lbl].num_read_locked,
695 lock_stats[lbl].num_read_unlocked,
696 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
697 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
698 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
699 }
700}
701
702/* Following functions are used to collect some stats about locks. We wrap
703 * pthread functions to known how much time we wait in a lock. */
704
705static uint64_t nsec_now(void) {
706 struct timespec ts;
707
708 clock_gettime(CLOCK_MONOTONIC, &ts);
709 return ((uint64_t) ts.tv_sec * 1000000000ULL +
710 (uint64_t) ts.tv_nsec);
711}
712
713static inline void __ha_rwlock_init(struct ha_rwlock *l)
714{
715 memset(l, 0, sizeof(struct ha_rwlock));
716 __RWLOCK_INIT(&l->lock);
717}
718
719static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
720{
721 __RWLOCK_DESTROY(&l->lock);
722 memset(l, 0, sizeof(struct ha_rwlock));
723}
724
725
726static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
727 const char *func, const char *file, int line)
728{
729 uint64_t start_time;
730
731 if (unlikely(l->info.cur_writer & tid_bit)) {
732 /* the thread is already owning the lock for write */
733 abort();
734 }
735
736 if (unlikely(l->info.cur_readers & tid_bit)) {
737 /* the thread is already owning the lock for read */
738 abort();
739 }
740
741 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
742
743 start_time = nsec_now();
744 __RWLOCK_WRLOCK(&l->lock);
745 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
746
747 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
748
749 l->info.cur_writer = tid_bit;
750 l->info.last_location.function = func;
751 l->info.last_location.file = file;
752 l->info.last_location.line = line;
753
754 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
755}
756
757static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
758 const char *func, const char *file, int line)
759{
760 uint64_t start_time;
761 int r;
762
763 if (unlikely(l->info.cur_writer & tid_bit)) {
764 /* the thread is already owning the lock for write */
765 abort();
766 }
767
768 if (unlikely(l->info.cur_readers & tid_bit)) {
769 /* the thread is already owning the lock for read */
770 abort();
771 }
772
773 /* We set waiting writer because trywrlock could wait for readers to quit */
774 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
775
776 start_time = nsec_now();
777 r = __RWLOCK_TRYWRLOCK(&l->lock);
778 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
779 if (unlikely(r)) {
780 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
781 return r;
782 }
783 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
784
785 l->info.cur_writer = tid_bit;
786 l->info.last_location.function = func;
787 l->info.last_location.file = file;
788 l->info.last_location.line = line;
789
790 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
791
792 return 0;
793}
794
795static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
796 const char *func, const char *file, int line)
797{
798 if (unlikely(!(l->info.cur_writer & tid_bit))) {
799 /* the thread is not owning the lock for write */
800 abort();
801 }
802
803 l->info.cur_writer = 0;
804 l->info.last_location.function = func;
805 l->info.last_location.file = file;
806 l->info.last_location.line = line;
807
808 __RWLOCK_WRUNLOCK(&l->lock);
809
810 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
811}
812
813static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
814{
815 uint64_t start_time;
816
817 if (unlikely(l->info.cur_writer & tid_bit)) {
818 /* the thread is already owning the lock for write */
819 abort();
820 }
821
822 if (unlikely(l->info.cur_readers & tid_bit)) {
823 /* the thread is already owning the lock for read */
824 abort();
825 }
826
827 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
828
829 start_time = nsec_now();
830 __RWLOCK_RDLOCK(&l->lock);
831 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
832 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
833
834 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
835
836 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
837}
838
839static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
840{
841 int r;
842
843 if (unlikely(l->info.cur_writer & tid_bit)) {
844 /* the thread is already owning the lock for write */
845 abort();
846 }
847
848 if (unlikely(l->info.cur_readers & tid_bit)) {
849 /* the thread is already owning the lock for read */
850 abort();
851 }
852
853 /* try read should never wait */
854 r = __RWLOCK_TRYRDLOCK(&l->lock);
855 if (unlikely(r))
856 return r;
857 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
858
859 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
860
861 return 0;
862}
863
864static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
865{
866 if (unlikely(!(l->info.cur_readers & tid_bit))) {
867 /* the thread is not owning the lock for read */
868 abort();
869 }
870
871 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
872
873 __RWLOCK_RDUNLOCK(&l->lock);
874
875 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
876}
877
878static inline void __spin_init(struct ha_spinlock *l)
879{
880 memset(l, 0, sizeof(struct ha_spinlock));
881 __SPIN_INIT(&l->lock);
882}
883
884static inline void __spin_destroy(struct ha_spinlock *l)
885{
886 __SPIN_DESTROY(&l->lock);
887 memset(l, 0, sizeof(struct ha_spinlock));
888}
889
890static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
891 const char *func, const char *file, int line)
892{
893 uint64_t start_time;
894
895 if (unlikely(l->info.owner & tid_bit)) {
896 /* the thread is already owning the lock */
897 abort();
898 }
899
900 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
901
902 start_time = nsec_now();
903 __SPIN_LOCK(&l->lock);
904 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
905
906 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
907
908
909 l->info.owner = tid_bit;
910 l->info.last_location.function = func;
911 l->info.last_location.file = file;
912 l->info.last_location.line = line;
913
914 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
915}
916
917static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
918 const char *func, const char *file, int line)
919{
920 int r;
921
922 if (unlikely(l->info.owner & tid_bit)) {
923 /* the thread is already owning the lock */
924 abort();
925 }
926
927 /* try read should never wait */
928 r = __SPIN_TRYLOCK(&l->lock);
929 if (unlikely(r))
930 return r;
931 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
932
933 l->info.owner = tid_bit;
934 l->info.last_location.function = func;
935 l->info.last_location.file = file;
936 l->info.last_location.line = line;
937
938 return 0;
939}
940
941static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
942 const char *func, const char *file, int line)
943{
944 if (unlikely(!(l->info.owner & tid_bit))) {
945 /* the thread is not owning the lock */
946 abort();
947 }
948
949 l->info.owner = 0;
950 l->info.last_location.function = func;
951 l->info.last_location.file = file;
952 l->info.last_location.line = line;
953
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100954 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200955 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
956}
957
958#else /* DEBUG_THREAD */
959
960#define HA_SPINLOCK_T unsigned long
961
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100962#define HA_SPIN_INIT(l) ({ (*l) = 0; })
963#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
964#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
965#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
966#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200967
968#define HA_RWLOCK_T unsigned long
969
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100970#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
971#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
972#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
973#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
974#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
975#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
976#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
977#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200978
979#endif /* DEBUG_THREAD */
980
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100981#ifdef __x86_64__
Willy Tarreau2325d8a2018-10-10 18:29:23 +0200982
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100983static __inline int
984__ha_cas_dw(void *target, void *compare, const void *set)
985{
986 char ret;
987
988 __asm __volatile("lock cmpxchg16b %0; setz %3"
989 : "+m" (*(void **)target),
990 "=a" (((void **)compare)[0]),
991 "=d" (((void **)compare)[1]),
992 "=q" (ret)
993 : "a" (((void **)compare)[0]),
994 "d" (((void **)compare)[1]),
995 "b" (((const void **)set)[0]),
996 "c" (((const void **)set)[1])
997 : "memory", "cc");
998 return (ret);
999}
1000
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001001/* Use __ha_barrier_atomic* when you're trying to protect data that are
1002 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1003 */
1004static __inline void
1005__ha_barrier_atomic_load(void)
1006{
1007 __asm __volatile("" ::: "memory");
1008}
1009
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001010static __inline void
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001011__ha_barrier_atomic_store(void)
1012{
1013 __asm __volatile("" ::: "memory");
1014}
1015
1016static __inline void
1017__ha_barrier_atomic_full(void)
1018{
1019 __asm __volatile("" ::: "memory");
1020}
1021
1022static __inline void
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001023__ha_barrier_load(void)
1024{
1025 __asm __volatile("lfence" ::: "memory");
1026}
1027
1028static __inline void
1029__ha_barrier_store(void)
1030{
1031 __asm __volatile("sfence" ::: "memory");
1032}
1033
1034static __inline void
1035__ha_barrier_full(void)
1036{
1037 __asm __volatile("mfence" ::: "memory");
1038}
1039
1040#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
Willy Tarreau2325d8a2018-10-10 18:29:23 +02001041
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001042/* Use __ha_barrier_atomic* when you're trying to protect data that are
1043 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1044 */
1045static __inline void
1046__ha_barrier_atomic_load(void)
1047{
1048 __asm __volatile("dmb" ::: "memory");
1049}
1050
1051static __inline void
1052__ha_barrier_atomic_store(void)
1053{
1054 __asm __volatile("dsb" ::: "memory");
1055}
1056
1057static __inline void
1058__ha_barrier_atomic_full(void)
1059{
1060 __asm __volatile("dmb" ::: "memory");
1061}
1062
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001063static __inline void
1064__ha_barrier_load(void)
1065{
1066 __asm __volatile("dmb" ::: "memory");
1067}
1068
1069static __inline void
1070__ha_barrier_store(void)
1071{
1072 __asm __volatile("dsb" ::: "memory");
1073}
1074
1075static __inline void
1076__ha_barrier_full(void)
1077{
1078 __asm __volatile("dmb" ::: "memory");
1079}
1080
Willy Tarreau41ccb192018-02-14 14:16:28 +01001081static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001082{
1083 uint64_t previous;
1084 int tmp;
1085
1086 __asm __volatile("1:"
1087 "ldrexd %0, [%4];"
1088 "cmp %Q0, %Q2;"
1089 "ittt eq;"
1090 "cmpeq %R0, %R2;"
1091 "strexdeq %1, %3, [%4];"
1092 "cmpeq %1, #1;"
1093 "beq 1b;"
1094 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +01001095 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001096 : "memory", "cc");
1097 tmp = (previous == *(uint64_t *)compare);
1098 *(uint64_t *)compare = previous;
1099 return (tmp);
1100}
1101
1102#elif defined (__aarch64__)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001103
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001104/* Use __ha_barrier_atomic* when you're trying to protect data that are
1105 * are modified using HA_ATOMIC* (except HA_ATOMIC_STORE)
1106 */
1107static __inline void
1108__ha_barrier_atomic_load(void)
1109{
1110 __asm __volatile("dmb ishld" ::: "memory");
1111}
1112
1113static __inline void
1114__ha_barrier_atomic_store(void)
1115{
1116 __asm __volatile("dmb ishst" ::: "memory");
1117}
1118
1119static __inline void
1120__ha_barrier_atomic_full(void)
1121{
1122 __asm __volatile("dmb ish" ::: "memory");
1123}
1124
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001125static __inline void
1126__ha_barrier_load(void)
1127{
1128 __asm __volatile("dmb ishld" ::: "memory");
1129}
1130
1131static __inline void
1132__ha_barrier_store(void)
1133{
1134 __asm __volatile("dmb ishst" ::: "memory");
1135}
1136
1137static __inline void
1138__ha_barrier_full(void)
1139{
1140 __asm __volatile("dmb ish" ::: "memory");
1141}
1142
1143static __inline int __ha_cas_dw(void *target, void *compare, void *set)
1144{
1145 void *value[2];
1146 uint64_t tmp1, tmp2;
1147
1148 __asm__ __volatile__("1:"
1149 "ldxp %0, %1, [%4];"
1150 "mov %2, %0;"
1151 "mov %3, %1;"
1152 "eor %0, %0, %5;"
1153 "eor %1, %1, %6;"
1154 "orr %1, %0, %1;"
1155 "mov %w0, #0;"
1156 "cbnz %1, 2f;"
1157 "stxp %w0, %7, %8, [%4];"
1158 "cbnz %w0, 1b;"
1159 "mov %w0, #1;"
1160 "2:"
1161 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
1162 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
1163 : "cc", "memory");
1164
1165 memcpy(compare, &value, sizeof(value));
1166 return (tmp1);
1167}
1168
1169#else
Olivier Houchard9abcf6e2019-03-07 18:45:00 +01001170#define __ha_barrier_atomic_load __sync_synchronize
1171#define __ha_barrier_atomic_store __sync_synchronize
1172#define __ha_barrier_atomic_full __sync_synchronize
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001173#define __ha_barrier_load __sync_synchronize
1174#define __ha_barrier_store __sync_synchronize
1175#define __ha_barrier_full __sync_synchronize
1176#endif
1177
Willy Tarreaua8ae77d2018-11-25 19:28:23 +01001178void ha_spin_init(HA_SPINLOCK_T *l);
1179void ha_rwlock_init(HA_RWLOCK_T *l);
1180
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001181#endif /* USE_THREAD */
1182
Willy Tarreau149ab772019-01-26 14:27:06 +01001183extern int thread_cpus_enabled_at_boot;
1184
Olivier Houchardf61f0cb2017-12-21 17:13:05 +01001185static inline void __ha_compiler_barrier(void)
1186{
1187 __asm __volatile("" ::: "memory");
1188}
1189
Willy Tarreau0ccd3222018-07-30 10:34:35 +02001190int parse_nbthread(const char *arg, char **err);
Willy Tarreau149ab772019-01-26 14:27:06 +01001191int thread_get_default_count();
Willy Tarreau4037a3f2018-03-28 18:06:47 +02001192
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001193#ifndef _HA_ATOMIC_CAS
1194#define _HA_ATOMIC_CAS HA_ATOMIC_CAS
1195#endif /* !_HA_ATOMIC_CAS */
1196
Willy Tarreau6a38b322019-05-11 18:04:24 +02001197#ifndef _HA_ATOMIC_DWCAS
1198#define _HA_ATOMIC_DWCAS HA_ATOMIC_DWCAS
1199#endif /* !_HA_ATOMIC_CAS */
1200
Olivier Houchardd0c3b882019-03-07 18:55:31 +01001201#ifndef _HA_ATOMIC_ADD
1202#define _HA_ATOMIC_ADD HA_ATOMIC_ADD
1203#endif /* !_HA_ATOMIC_ADD */
1204
1205#ifndef _HA_ATOMIC_XADD
1206#define _HA_ATOMIC_XADD HA_ATOMIC_XADD
1207#endif /* !_HA_ATOMIC_SUB */
1208
1209#ifndef _HA_ATOMIC_SUB
1210#define _HA_ATOMIC_SUB HA_ATOMIC_SUB
1211#endif /* !_HA_ATOMIC_SUB */
1212
1213#ifndef _HA_ATOMIC_AND
1214#define _HA_ATOMIC_AND HA_ATOMIC_AND
1215#endif /* !_HA_ATOMIC_AND */
1216
1217#ifndef _HA_ATOMIC_OR
1218#define _HA_ATOMIC_OR HA_ATOMIC_OR
1219#endif /* !_HA_ATOMIC_OR */
1220
1221#ifndef _HA_ATOMIC_XCHG
1222#define _HA_ATOMIC_XCHG HA_ATOMIC_XCHG
1223#endif /* !_HA_ATOMIC_XCHG */
1224
1225#ifndef _HA_ATOMIC_STORE
1226#define _HA_ATOMIC_STORE HA_ATOMIC_STORE
1227#endif /* !_HA_ATOMIC_STORE */
Olivier Houchard9ce62b52019-04-30 13:38:02 +02001228
1229#ifndef _HA_ATOMIC_LOAD
1230#define _HA_ATOMIC_LOAD HA_ATOMIC_LOAD
1231#endif /* !_HA_ATOMIC_LOAD */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001232#endif /* _COMMON_HATHREADS_H */