blob: 0f10b48ca013aebca0dafa3c3bb3a39202d592ce [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
25#include <common/config.h>
26
27#define MAX_THREADS_MASK ((unsigned long)-1)
28extern THREAD_LOCAL unsigned int tid; /* The thread id */
Christopher Faulete9a896e2017-11-14 10:16:04 +010029extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020030
31#ifndef USE_THREAD
32
Willy Tarreau421f02e2018-01-20 18:19:22 +010033#define MAX_THREADS 1
34
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010035#define __decl_hathreads(decl)
36
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020037#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
38#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
39#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
40#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
41#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
42#define HA_ATOMIC_XCHG(val, new) \
43 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020044 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020045 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020046 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020047 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010048#define HA_ATOMIC_BTS(val, bit) \
49 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020050 typeof((val)) __p_bts = (val); \
51 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
52 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
53 if (!__t_bts) \
54 *__p_bts |= __b_bts; \
55 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010056 })
57#define HA_ATOMIC_BTR(val, bit) \
58 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020059 typeof((val)) __p_btr = (val); \
60 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
61 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
62 if (__t_btr) \
63 *__p_btr &= ~__b_btr; \
64 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010065 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020066#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
67#define HA_ATOMIC_UPDATE_MAX(val, new) \
68 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020069 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020070 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020071 if (*(val) < __new_max) \
72 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020073 *(val); \
74 })
75
76#define HA_ATOMIC_UPDATE_MIN(val, new) \
77 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020078 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020079 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020080 if (*(val) > __new_min) \
81 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020082 *(val); \
83 })
84
Willy Tarreaub29dc952017-10-31 18:00:20 +010085#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020086
87#define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0)
88#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
89#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
90#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
91#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
92#define THREAD_NO_SYNC() ({ 0; })
93#define THREAD_NEED_SYNC() ({ 1; })
94
Christopher Faulet2a944ee2017-11-07 10:42:54 +010095#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
96#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
97#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
98#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
99#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200100
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100101#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
102#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
103#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
104#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
105#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
106#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
107#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
108#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100110static inline void __ha_barrier_load(void)
111{
112}
113
114static inline void __ha_barrier_store(void)
115{
116}
117
118static inline void __ha_barrier_full(void)
119{
120}
121
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200122#else /* USE_THREAD */
123
124#include <stdio.h>
125#include <stdlib.h>
126#include <string.h>
127#include <pthread.h>
128#include <import/plock.h>
129
Willy Tarreau421f02e2018-01-20 18:19:22 +0100130#define MAX_THREADS LONGBITS
131
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100132#define __decl_hathreads(decl) decl
133
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200134/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
135 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100136
David Carlierec5e8452018-01-11 14:20:43 +0000137#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100138/* gcc < 4.7 */
139
140#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
141#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
142#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
143#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
144
145/* the CAS is a bit complicated. The older API doesn't support returning the
146 * value and the swap's result at the same time. So here we take what looks
147 * like the safest route, consisting in using the boolean version guaranteeing
148 * that the operation was performed or not, and we snoop a previous value. If
149 * the compare succeeds, we return. If it fails, we return the previous value,
150 * but only if it differs from the expected one. If it's the same it's a race
151 * thus we try again to avoid confusing a possibly sensitive caller.
152 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200153#define HA_ATOMIC_CAS(val, old, new) \
154 ({ \
155 typeof((val)) __val_cas = (val); \
156 typeof((old)) __oldp_cas = (old); \
157 typeof(*(old)) __oldv_cas; \
158 typeof((new)) __new_cas = (new); \
159 int __ret_cas; \
160 do { \
161 __oldv_cas = *__val_cas; \
162 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
163 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
164 if (!__ret_cas) \
165 *__oldp_cas = __oldv_cas; \
166 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100167 })
168
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200169#define HA_ATOMIC_XCHG(val, new) \
170 ({ \
171 typeof((val)) __val_xchg = (val); \
172 typeof(*(val)) __old_xchg; \
173 typeof((new)) __new_xchg = (new); \
174 do { __old_xchg = *__val_xchg; \
175 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
176 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100177 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100178
179#define HA_ATOMIC_BTS(val, bit) \
180 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200181 typeof(*(val)) __b_bts = (1UL << (bit)); \
182 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100183 })
184
185#define HA_ATOMIC_BTR(val, bit) \
186 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200187 typeof(*(val)) __b_btr = (1UL << (bit)); \
188 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100189 })
190
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200191#define HA_ATOMIC_STORE(val, new) \
192 ({ \
193 typeof((val)) __val_store = (val); \
194 typeof(*(val)) __old_store; \
195 typeof((new)) __new_store = (new); \
196 do { __old_store = *__val_store; \
197 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100198 })
199#else
200/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200201#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
202#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
203#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
204#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
205#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100206#define HA_ATOMIC_BTS(val, bit) \
207 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200208 typeof(*(val)) __b_bts = (1UL << (bit)); \
209 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100210 })
211
212#define HA_ATOMIC_BTR(val, bit) \
213 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200214 typeof(*(val)) __b_btr = (1UL << (bit)); \
215 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100216 })
217
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200218#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
219#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100220#endif
221
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200222#define HA_ATOMIC_UPDATE_MAX(val, new) \
223 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200224 typeof(*(val)) __old_max = *(val); \
225 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200226 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200227 while (__old_max < __new_max && \
228 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
229 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200230 })
231#define HA_ATOMIC_UPDATE_MIN(val, new) \
232 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200233 typeof(*(val)) __old_min = *(val); \
234 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200235 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200236 while (__old_min > __new_min && \
237 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
238 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200239 })
240
Willy Tarreaub29dc952017-10-31 18:00:20 +0100241#define HA_BARRIER() pl_barrier()
242
Christopher Faulet339fff82017-10-19 11:59:15 +0200243#define THREAD_SYNC_INIT(m) thread_sync_init(m)
244#define THREAD_SYNC_ENABLE() thread_sync_enable()
245#define THREAD_WANT_SYNC() thread_want_sync()
246#define THREAD_ENTER_SYNC() thread_enter_sync()
247#define THREAD_EXIT_SYNC() thread_exit_sync()
248#define THREAD_NO_SYNC() thread_no_sync()
249#define THREAD_NEED_SYNC() thread_need_sync()
250
251int thread_sync_init(unsigned long mask);
252void thread_sync_enable(void);
253void thread_want_sync(void);
254void thread_enter_sync(void);
255void thread_exit_sync(void);
256int thread_no_sync(void);
257int thread_need_sync(void);
258
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200259#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
260
Christopher Fauletf51bac22018-01-30 11:04:29 +0100261/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200262enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200263 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200264 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200265 TASK_RQ_LOCK,
266 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200267 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200268 LISTENER_LOCK,
269 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200270 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200271 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200272 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200273 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200274 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200275 STK_TABLE_LOCK,
276 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200277 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200278 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200279 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200280 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200281 SSL_LOCK,
282 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200283 PATREF_LOCK,
284 PATEXP_LOCK,
285 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200286 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200287 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200288 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200289 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200290 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200291 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200292 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200293 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100294 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100295 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100296 TLSKEYS_REF_LOCK,
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100297 PENDCONN_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200298 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200299};
300struct lock_stat {
301 uint64_t nsec_wait_for_write;
302 uint64_t nsec_wait_for_read;
303 uint64_t num_write_locked;
304 uint64_t num_write_unlocked;
305 uint64_t num_read_locked;
306 uint64_t num_read_unlocked;
307};
308
309extern struct lock_stat lock_stats[LOCK_LABELS];
310
311#define __HA_SPINLOCK_T unsigned long
312
313#define __SPIN_INIT(l) ({ (*l) = 0; })
314#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100315#define __SPIN_LOCK(l) pl_take_s(l)
316#define __SPIN_TRYLOCK(l) !pl_try_s(l)
317#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200318
319#define __HA_RWLOCK_T unsigned long
320
321#define __RWLOCK_INIT(l) ({ (*l) = 0; })
322#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
323#define __RWLOCK_WRLOCK(l) pl_take_w(l)
324#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
325#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
326#define __RWLOCK_RDLOCK(l) pl_take_r(l)
327#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
328#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
329
330#define HA_SPINLOCK_T struct ha_spinlock
331
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332#define HA_SPIN_INIT(l) __spin_init(l)
333#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200334
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
336#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
337#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200338
339#define HA_RWLOCK_T struct ha_rwlock
340
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
342#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
343#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
344#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
345#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
346#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
347#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
348#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200349
350struct ha_spinlock {
351 __HA_SPINLOCK_T lock;
352 struct {
353 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
354 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
355 struct {
356 const char *function;
357 const char *file;
358 int line;
359 } last_location; /* location of the last owner */
360 } info;
361};
362
363struct ha_rwlock {
364 __HA_RWLOCK_T lock;
365 struct {
366 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
367 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
368 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
369 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
370 struct {
371 const char *function;
372 const char *file;
373 int line;
374 } last_location; /* location of the last write owner */
375 } info;
376};
377
Christopher Fauletf51bac22018-01-30 11:04:29 +0100378static inline const char *lock_label(enum lock_label label)
379{
380 switch (label) {
381 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100382 case FD_LOCK: return "FD";
383 case TASK_RQ_LOCK: return "TASK_RQ";
384 case TASK_WQ_LOCK: return "TASK_WQ";
385 case POOL_LOCK: return "POOL";
386 case LISTENER_LOCK: return "LISTENER";
387 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
388 case PROXY_LOCK: return "PROXY";
389 case SERVER_LOCK: return "SERVER";
390 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
391 case LBPRM_LOCK: return "LBPRM";
392 case SIGNALS_LOCK: return "SIGNALS";
393 case STK_TABLE_LOCK: return "STK_TABLE";
394 case STK_SESS_LOCK: return "STK_SESS";
395 case APPLETS_LOCK: return "APPLETS";
396 case PEER_LOCK: return "PEER";
397 case BUF_WQ_LOCK: return "BUF_WQ";
398 case STRMS_LOCK: return "STRMS";
399 case SSL_LOCK: return "SSL";
400 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
401 case PATREF_LOCK: return "PATREF";
402 case PATEXP_LOCK: return "PATEXP";
403 case PATLRU_LOCK: return "PATLRU";
404 case VARS_LOCK: return "VARS";
405 case COMP_POOL_LOCK: return "COMP_POOL";
406 case LUA_LOCK: return "LUA";
407 case NOTIF_LOCK: return "NOTIF";
408 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
409 case DNS_LOCK: return "DNS";
410 case PID_LIST_LOCK: return "PID_LIST";
411 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
412 case PIPES_LOCK: return "PIPES";
413 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100414 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100415 case PENDCONN_LOCK: return "PENDCONN";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100416 case LOCK_LABELS: break; /* keep compiler happy */
417 };
418 /* only way to come here is consecutive to an internal bug */
419 abort();
420}
421
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200422static inline void show_lock_stats()
423{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200424 int lbl;
425
426 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
427 fprintf(stderr,
428 "Stats about Lock %s: \n"
429 "\t # write lock : %lu\n"
430 "\t # write unlock: %lu (%ld)\n"
431 "\t # wait time for write : %.3f msec\n"
432 "\t # wait time for write/lock: %.3f nsec\n"
433 "\t # read lock : %lu\n"
434 "\t # read unlock : %lu (%ld)\n"
435 "\t # wait time for read : %.3f msec\n"
436 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100437 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200438 lock_stats[lbl].num_write_locked,
439 lock_stats[lbl].num_write_unlocked,
440 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
441 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
442 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
443 lock_stats[lbl].num_read_locked,
444 lock_stats[lbl].num_read_unlocked,
445 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
446 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
447 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
448 }
449}
450
451/* Following functions are used to collect some stats about locks. We wrap
452 * pthread functions to known how much time we wait in a lock. */
453
454static uint64_t nsec_now(void) {
455 struct timespec ts;
456
457 clock_gettime(CLOCK_MONOTONIC, &ts);
458 return ((uint64_t) ts.tv_sec * 1000000000ULL +
459 (uint64_t) ts.tv_nsec);
460}
461
462static inline void __ha_rwlock_init(struct ha_rwlock *l)
463{
464 memset(l, 0, sizeof(struct ha_rwlock));
465 __RWLOCK_INIT(&l->lock);
466}
467
468static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
469{
470 __RWLOCK_DESTROY(&l->lock);
471 memset(l, 0, sizeof(struct ha_rwlock));
472}
473
474
475static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
476 const char *func, const char *file, int line)
477{
478 uint64_t start_time;
479
480 if (unlikely(l->info.cur_writer & tid_bit)) {
481 /* the thread is already owning the lock for write */
482 abort();
483 }
484
485 if (unlikely(l->info.cur_readers & tid_bit)) {
486 /* the thread is already owning the lock for read */
487 abort();
488 }
489
490 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
491
492 start_time = nsec_now();
493 __RWLOCK_WRLOCK(&l->lock);
494 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
495
496 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
497
498 l->info.cur_writer = tid_bit;
499 l->info.last_location.function = func;
500 l->info.last_location.file = file;
501 l->info.last_location.line = line;
502
503 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
504}
505
506static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
507 const char *func, const char *file, int line)
508{
509 uint64_t start_time;
510 int r;
511
512 if (unlikely(l->info.cur_writer & tid_bit)) {
513 /* the thread is already owning the lock for write */
514 abort();
515 }
516
517 if (unlikely(l->info.cur_readers & tid_bit)) {
518 /* the thread is already owning the lock for read */
519 abort();
520 }
521
522 /* We set waiting writer because trywrlock could wait for readers to quit */
523 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
524
525 start_time = nsec_now();
526 r = __RWLOCK_TRYWRLOCK(&l->lock);
527 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
528 if (unlikely(r)) {
529 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
530 return r;
531 }
532 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
533
534 l->info.cur_writer = tid_bit;
535 l->info.last_location.function = func;
536 l->info.last_location.file = file;
537 l->info.last_location.line = line;
538
539 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
540
541 return 0;
542}
543
544static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
545 const char *func, const char *file, int line)
546{
547 if (unlikely(!(l->info.cur_writer & tid_bit))) {
548 /* the thread is not owning the lock for write */
549 abort();
550 }
551
552 l->info.cur_writer = 0;
553 l->info.last_location.function = func;
554 l->info.last_location.file = file;
555 l->info.last_location.line = line;
556
557 __RWLOCK_WRUNLOCK(&l->lock);
558
559 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
560}
561
562static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
563{
564 uint64_t start_time;
565
566 if (unlikely(l->info.cur_writer & tid_bit)) {
567 /* the thread is already owning the lock for write */
568 abort();
569 }
570
571 if (unlikely(l->info.cur_readers & tid_bit)) {
572 /* the thread is already owning the lock for read */
573 abort();
574 }
575
576 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
577
578 start_time = nsec_now();
579 __RWLOCK_RDLOCK(&l->lock);
580 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
581 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
582
583 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
584
585 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
586}
587
588static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
589{
590 int r;
591
592 if (unlikely(l->info.cur_writer & tid_bit)) {
593 /* the thread is already owning the lock for write */
594 abort();
595 }
596
597 if (unlikely(l->info.cur_readers & tid_bit)) {
598 /* the thread is already owning the lock for read */
599 abort();
600 }
601
602 /* try read should never wait */
603 r = __RWLOCK_TRYRDLOCK(&l->lock);
604 if (unlikely(r))
605 return r;
606 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
607
608 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
609
610 return 0;
611}
612
613static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
614{
615 if (unlikely(!(l->info.cur_readers & tid_bit))) {
616 /* the thread is not owning the lock for read */
617 abort();
618 }
619
620 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
621
622 __RWLOCK_RDUNLOCK(&l->lock);
623
624 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
625}
626
627static inline void __spin_init(struct ha_spinlock *l)
628{
629 memset(l, 0, sizeof(struct ha_spinlock));
630 __SPIN_INIT(&l->lock);
631}
632
633static inline void __spin_destroy(struct ha_spinlock *l)
634{
635 __SPIN_DESTROY(&l->lock);
636 memset(l, 0, sizeof(struct ha_spinlock));
637}
638
639static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
640 const char *func, const char *file, int line)
641{
642 uint64_t start_time;
643
644 if (unlikely(l->info.owner & tid_bit)) {
645 /* the thread is already owning the lock */
646 abort();
647 }
648
649 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
650
651 start_time = nsec_now();
652 __SPIN_LOCK(&l->lock);
653 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
654
655 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
656
657
658 l->info.owner = tid_bit;
659 l->info.last_location.function = func;
660 l->info.last_location.file = file;
661 l->info.last_location.line = line;
662
663 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
664}
665
666static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
667 const char *func, const char *file, int line)
668{
669 int r;
670
671 if (unlikely(l->info.owner & tid_bit)) {
672 /* the thread is already owning the lock */
673 abort();
674 }
675
676 /* try read should never wait */
677 r = __SPIN_TRYLOCK(&l->lock);
678 if (unlikely(r))
679 return r;
680 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
681
682 l->info.owner = tid_bit;
683 l->info.last_location.function = func;
684 l->info.last_location.file = file;
685 l->info.last_location.line = line;
686
687 return 0;
688}
689
690static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
691 const char *func, const char *file, int line)
692{
693 if (unlikely(!(l->info.owner & tid_bit))) {
694 /* the thread is not owning the lock */
695 abort();
696 }
697
698 l->info.owner = 0;
699 l->info.last_location.function = func;
700 l->info.last_location.file = file;
701 l->info.last_location.line = line;
702
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100703 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200704 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
705}
706
707#else /* DEBUG_THREAD */
708
709#define HA_SPINLOCK_T unsigned long
710
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100711#define HA_SPIN_INIT(l) ({ (*l) = 0; })
712#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
713#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
714#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
715#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200716
717#define HA_RWLOCK_T unsigned long
718
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100719#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
720#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
721#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
722#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
723#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
724#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
725#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
726#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200727
728#endif /* DEBUG_THREAD */
729
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100730#ifdef __x86_64__
731#define HA_HAVE_CAS_DW 1
732#define HA_CAS_IS_8B
733static __inline int
734__ha_cas_dw(void *target, void *compare, const void *set)
735{
736 char ret;
737
738 __asm __volatile("lock cmpxchg16b %0; setz %3"
739 : "+m" (*(void **)target),
740 "=a" (((void **)compare)[0]),
741 "=d" (((void **)compare)[1]),
742 "=q" (ret)
743 : "a" (((void **)compare)[0]),
744 "d" (((void **)compare)[1]),
745 "b" (((const void **)set)[0]),
746 "c" (((const void **)set)[1])
747 : "memory", "cc");
748 return (ret);
749}
750
751static __inline void
752__ha_barrier_load(void)
753{
754 __asm __volatile("lfence" ::: "memory");
755}
756
757static __inline void
758__ha_barrier_store(void)
759{
760 __asm __volatile("sfence" ::: "memory");
761}
762
763static __inline void
764__ha_barrier_full(void)
765{
766 __asm __volatile("mfence" ::: "memory");
767}
768
769#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
770#define HA_HAVE_CAS_DW 1
771static __inline void
772__ha_barrier_load(void)
773{
774 __asm __volatile("dmb" ::: "memory");
775}
776
777static __inline void
778__ha_barrier_store(void)
779{
780 __asm __volatile("dsb" ::: "memory");
781}
782
783static __inline void
784__ha_barrier_full(void)
785{
786 __asm __volatile("dmb" ::: "memory");
787}
788
Willy Tarreau41ccb192018-02-14 14:16:28 +0100789static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100790{
791 uint64_t previous;
792 int tmp;
793
794 __asm __volatile("1:"
795 "ldrexd %0, [%4];"
796 "cmp %Q0, %Q2;"
797 "ittt eq;"
798 "cmpeq %R0, %R2;"
799 "strexdeq %1, %3, [%4];"
800 "cmpeq %1, #1;"
801 "beq 1b;"
802 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100803 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100804 : "memory", "cc");
805 tmp = (previous == *(uint64_t *)compare);
806 *(uint64_t *)compare = previous;
807 return (tmp);
808}
809
810#elif defined (__aarch64__)
811#define HA_HAVE_CAS_DW 1
812#define HA_CAS_IS_8B
813
814static __inline void
815__ha_barrier_load(void)
816{
817 __asm __volatile("dmb ishld" ::: "memory");
818}
819
820static __inline void
821__ha_barrier_store(void)
822{
823 __asm __volatile("dmb ishst" ::: "memory");
824}
825
826static __inline void
827__ha_barrier_full(void)
828{
829 __asm __volatile("dmb ish" ::: "memory");
830}
831
832static __inline int __ha_cas_dw(void *target, void *compare, void *set)
833{
834 void *value[2];
835 uint64_t tmp1, tmp2;
836
837 __asm__ __volatile__("1:"
838 "ldxp %0, %1, [%4];"
839 "mov %2, %0;"
840 "mov %3, %1;"
841 "eor %0, %0, %5;"
842 "eor %1, %1, %6;"
843 "orr %1, %0, %1;"
844 "mov %w0, #0;"
845 "cbnz %1, 2f;"
846 "stxp %w0, %7, %8, [%4];"
847 "cbnz %w0, 1b;"
848 "mov %w0, #1;"
849 "2:"
850 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
851 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
852 : "cc", "memory");
853
854 memcpy(compare, &value, sizeof(value));
855 return (tmp1);
856}
857
858#else
859#define __ha_barrier_load __sync_synchronize
860#define __ha_barrier_store __sync_synchronize
861#define __ha_barrier_full __sync_synchronize
862#endif
863
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200864#endif /* USE_THREAD */
865
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100866static inline void __ha_compiler_barrier(void)
867{
868 __asm __volatile("" ::: "memory");
869}
870
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200871/* Dummy I/O handler used by the sync pipe.*/
872void thread_sync_io_handler(int fd);
873
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200874#endif /* _COMMON_HATHREADS_H */