blob: e27ecc63f6cf9bb7e8e235fcd38be52c20968ef6 [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
Olivier Houchard6b96f722018-04-25 16:58:25 +0200259extern unsigned long all_threads_mask;
260
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200261#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
262
Christopher Fauletf51bac22018-01-30 11:04:29 +0100263/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200264enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200265 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200266 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200267 TASK_RQ_LOCK,
268 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200269 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200270 LISTENER_LOCK,
271 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200272 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200273 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200274 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200275 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200276 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200277 STK_TABLE_LOCK,
278 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200279 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200280 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200281 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200282 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200283 SSL_LOCK,
284 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200285 PATREF_LOCK,
286 PATEXP_LOCK,
287 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200288 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200289 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200290 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200291 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200292 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200293 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200294 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200295 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100296 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100297 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100298 TLSKEYS_REF_LOCK,
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100299 PENDCONN_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200300 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200301};
302struct lock_stat {
303 uint64_t nsec_wait_for_write;
304 uint64_t nsec_wait_for_read;
305 uint64_t num_write_locked;
306 uint64_t num_write_unlocked;
307 uint64_t num_read_locked;
308 uint64_t num_read_unlocked;
309};
310
311extern struct lock_stat lock_stats[LOCK_LABELS];
312
313#define __HA_SPINLOCK_T unsigned long
314
315#define __SPIN_INIT(l) ({ (*l) = 0; })
316#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100317#define __SPIN_LOCK(l) pl_take_s(l)
318#define __SPIN_TRYLOCK(l) !pl_try_s(l)
319#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200320
321#define __HA_RWLOCK_T unsigned long
322
323#define __RWLOCK_INIT(l) ({ (*l) = 0; })
324#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
325#define __RWLOCK_WRLOCK(l) pl_take_w(l)
326#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
327#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
328#define __RWLOCK_RDLOCK(l) pl_take_r(l)
329#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
330#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
331
332#define HA_SPINLOCK_T struct ha_spinlock
333
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100334#define HA_SPIN_INIT(l) __spin_init(l)
335#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200336
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
338#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
339#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200340
341#define HA_RWLOCK_T struct ha_rwlock
342
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
344#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
345#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
346#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
347#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
348#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
349#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
350#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200351
352struct ha_spinlock {
353 __HA_SPINLOCK_T lock;
354 struct {
355 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
356 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
357 struct {
358 const char *function;
359 const char *file;
360 int line;
361 } last_location; /* location of the last owner */
362 } info;
363};
364
365struct ha_rwlock {
366 __HA_RWLOCK_T lock;
367 struct {
368 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
369 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
370 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
371 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
372 struct {
373 const char *function;
374 const char *file;
375 int line;
376 } last_location; /* location of the last write owner */
377 } info;
378};
379
Christopher Fauletf51bac22018-01-30 11:04:29 +0100380static inline const char *lock_label(enum lock_label label)
381{
382 switch (label) {
383 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100384 case FD_LOCK: return "FD";
385 case TASK_RQ_LOCK: return "TASK_RQ";
386 case TASK_WQ_LOCK: return "TASK_WQ";
387 case POOL_LOCK: return "POOL";
388 case LISTENER_LOCK: return "LISTENER";
389 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
390 case PROXY_LOCK: return "PROXY";
391 case SERVER_LOCK: return "SERVER";
392 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
393 case LBPRM_LOCK: return "LBPRM";
394 case SIGNALS_LOCK: return "SIGNALS";
395 case STK_TABLE_LOCK: return "STK_TABLE";
396 case STK_SESS_LOCK: return "STK_SESS";
397 case APPLETS_LOCK: return "APPLETS";
398 case PEER_LOCK: return "PEER";
399 case BUF_WQ_LOCK: return "BUF_WQ";
400 case STRMS_LOCK: return "STRMS";
401 case SSL_LOCK: return "SSL";
402 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
403 case PATREF_LOCK: return "PATREF";
404 case PATEXP_LOCK: return "PATEXP";
405 case PATLRU_LOCK: return "PATLRU";
406 case VARS_LOCK: return "VARS";
407 case COMP_POOL_LOCK: return "COMP_POOL";
408 case LUA_LOCK: return "LUA";
409 case NOTIF_LOCK: return "NOTIF";
410 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
411 case DNS_LOCK: return "DNS";
412 case PID_LIST_LOCK: return "PID_LIST";
413 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
414 case PIPES_LOCK: return "PIPES";
415 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100416 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100417 case PENDCONN_LOCK: return "PENDCONN";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100418 case LOCK_LABELS: break; /* keep compiler happy */
419 };
420 /* only way to come here is consecutive to an internal bug */
421 abort();
422}
423
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200424static inline void show_lock_stats()
425{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200426 int lbl;
427
428 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
429 fprintf(stderr,
430 "Stats about Lock %s: \n"
431 "\t # write lock : %lu\n"
432 "\t # write unlock: %lu (%ld)\n"
433 "\t # wait time for write : %.3f msec\n"
434 "\t # wait time for write/lock: %.3f nsec\n"
435 "\t # read lock : %lu\n"
436 "\t # read unlock : %lu (%ld)\n"
437 "\t # wait time for read : %.3f msec\n"
438 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100439 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200440 lock_stats[lbl].num_write_locked,
441 lock_stats[lbl].num_write_unlocked,
442 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
443 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
444 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
445 lock_stats[lbl].num_read_locked,
446 lock_stats[lbl].num_read_unlocked,
447 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
448 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
449 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
450 }
451}
452
453/* Following functions are used to collect some stats about locks. We wrap
454 * pthread functions to known how much time we wait in a lock. */
455
456static uint64_t nsec_now(void) {
457 struct timespec ts;
458
459 clock_gettime(CLOCK_MONOTONIC, &ts);
460 return ((uint64_t) ts.tv_sec * 1000000000ULL +
461 (uint64_t) ts.tv_nsec);
462}
463
464static inline void __ha_rwlock_init(struct ha_rwlock *l)
465{
466 memset(l, 0, sizeof(struct ha_rwlock));
467 __RWLOCK_INIT(&l->lock);
468}
469
470static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
471{
472 __RWLOCK_DESTROY(&l->lock);
473 memset(l, 0, sizeof(struct ha_rwlock));
474}
475
476
477static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
478 const char *func, const char *file, int line)
479{
480 uint64_t start_time;
481
482 if (unlikely(l->info.cur_writer & tid_bit)) {
483 /* the thread is already owning the lock for write */
484 abort();
485 }
486
487 if (unlikely(l->info.cur_readers & tid_bit)) {
488 /* the thread is already owning the lock for read */
489 abort();
490 }
491
492 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
493
494 start_time = nsec_now();
495 __RWLOCK_WRLOCK(&l->lock);
496 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
497
498 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
499
500 l->info.cur_writer = tid_bit;
501 l->info.last_location.function = func;
502 l->info.last_location.file = file;
503 l->info.last_location.line = line;
504
505 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
506}
507
508static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
509 const char *func, const char *file, int line)
510{
511 uint64_t start_time;
512 int r;
513
514 if (unlikely(l->info.cur_writer & tid_bit)) {
515 /* the thread is already owning the lock for write */
516 abort();
517 }
518
519 if (unlikely(l->info.cur_readers & tid_bit)) {
520 /* the thread is already owning the lock for read */
521 abort();
522 }
523
524 /* We set waiting writer because trywrlock could wait for readers to quit */
525 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
526
527 start_time = nsec_now();
528 r = __RWLOCK_TRYWRLOCK(&l->lock);
529 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
530 if (unlikely(r)) {
531 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
532 return r;
533 }
534 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
535
536 l->info.cur_writer = tid_bit;
537 l->info.last_location.function = func;
538 l->info.last_location.file = file;
539 l->info.last_location.line = line;
540
541 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
542
543 return 0;
544}
545
546static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
547 const char *func, const char *file, int line)
548{
549 if (unlikely(!(l->info.cur_writer & tid_bit))) {
550 /* the thread is not owning the lock for write */
551 abort();
552 }
553
554 l->info.cur_writer = 0;
555 l->info.last_location.function = func;
556 l->info.last_location.file = file;
557 l->info.last_location.line = line;
558
559 __RWLOCK_WRUNLOCK(&l->lock);
560
561 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
562}
563
564static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
565{
566 uint64_t start_time;
567
568 if (unlikely(l->info.cur_writer & tid_bit)) {
569 /* the thread is already owning the lock for write */
570 abort();
571 }
572
573 if (unlikely(l->info.cur_readers & tid_bit)) {
574 /* the thread is already owning the lock for read */
575 abort();
576 }
577
578 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
579
580 start_time = nsec_now();
581 __RWLOCK_RDLOCK(&l->lock);
582 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
583 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
584
585 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
586
587 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
588}
589
590static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
591{
592 int r;
593
594 if (unlikely(l->info.cur_writer & tid_bit)) {
595 /* the thread is already owning the lock for write */
596 abort();
597 }
598
599 if (unlikely(l->info.cur_readers & tid_bit)) {
600 /* the thread is already owning the lock for read */
601 abort();
602 }
603
604 /* try read should never wait */
605 r = __RWLOCK_TRYRDLOCK(&l->lock);
606 if (unlikely(r))
607 return r;
608 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
609
610 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
611
612 return 0;
613}
614
615static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
616{
617 if (unlikely(!(l->info.cur_readers & tid_bit))) {
618 /* the thread is not owning the lock for read */
619 abort();
620 }
621
622 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
623
624 __RWLOCK_RDUNLOCK(&l->lock);
625
626 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
627}
628
629static inline void __spin_init(struct ha_spinlock *l)
630{
631 memset(l, 0, sizeof(struct ha_spinlock));
632 __SPIN_INIT(&l->lock);
633}
634
635static inline void __spin_destroy(struct ha_spinlock *l)
636{
637 __SPIN_DESTROY(&l->lock);
638 memset(l, 0, sizeof(struct ha_spinlock));
639}
640
641static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
642 const char *func, const char *file, int line)
643{
644 uint64_t start_time;
645
646 if (unlikely(l->info.owner & tid_bit)) {
647 /* the thread is already owning the lock */
648 abort();
649 }
650
651 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
652
653 start_time = nsec_now();
654 __SPIN_LOCK(&l->lock);
655 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
656
657 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
658
659
660 l->info.owner = tid_bit;
661 l->info.last_location.function = func;
662 l->info.last_location.file = file;
663 l->info.last_location.line = line;
664
665 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
666}
667
668static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
669 const char *func, const char *file, int line)
670{
671 int r;
672
673 if (unlikely(l->info.owner & tid_bit)) {
674 /* the thread is already owning the lock */
675 abort();
676 }
677
678 /* try read should never wait */
679 r = __SPIN_TRYLOCK(&l->lock);
680 if (unlikely(r))
681 return r;
682 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
683
684 l->info.owner = tid_bit;
685 l->info.last_location.function = func;
686 l->info.last_location.file = file;
687 l->info.last_location.line = line;
688
689 return 0;
690}
691
692static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
693 const char *func, const char *file, int line)
694{
695 if (unlikely(!(l->info.owner & tid_bit))) {
696 /* the thread is not owning the lock */
697 abort();
698 }
699
700 l->info.owner = 0;
701 l->info.last_location.function = func;
702 l->info.last_location.file = file;
703 l->info.last_location.line = line;
704
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100705 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200706 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
707}
708
709#else /* DEBUG_THREAD */
710
711#define HA_SPINLOCK_T unsigned long
712
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713#define HA_SPIN_INIT(l) ({ (*l) = 0; })
714#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
715#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
716#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
717#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200718
719#define HA_RWLOCK_T unsigned long
720
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100721#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
722#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
723#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
724#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
725#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
726#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
727#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
728#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200729
730#endif /* DEBUG_THREAD */
731
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100732#ifdef __x86_64__
733#define HA_HAVE_CAS_DW 1
734#define HA_CAS_IS_8B
735static __inline int
736__ha_cas_dw(void *target, void *compare, const void *set)
737{
738 char ret;
739
740 __asm __volatile("lock cmpxchg16b %0; setz %3"
741 : "+m" (*(void **)target),
742 "=a" (((void **)compare)[0]),
743 "=d" (((void **)compare)[1]),
744 "=q" (ret)
745 : "a" (((void **)compare)[0]),
746 "d" (((void **)compare)[1]),
747 "b" (((const void **)set)[0]),
748 "c" (((const void **)set)[1])
749 : "memory", "cc");
750 return (ret);
751}
752
753static __inline void
754__ha_barrier_load(void)
755{
756 __asm __volatile("lfence" ::: "memory");
757}
758
759static __inline void
760__ha_barrier_store(void)
761{
762 __asm __volatile("sfence" ::: "memory");
763}
764
765static __inline void
766__ha_barrier_full(void)
767{
768 __asm __volatile("mfence" ::: "memory");
769}
770
771#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
772#define HA_HAVE_CAS_DW 1
773static __inline void
774__ha_barrier_load(void)
775{
776 __asm __volatile("dmb" ::: "memory");
777}
778
779static __inline void
780__ha_barrier_store(void)
781{
782 __asm __volatile("dsb" ::: "memory");
783}
784
785static __inline void
786__ha_barrier_full(void)
787{
788 __asm __volatile("dmb" ::: "memory");
789}
790
Willy Tarreau41ccb192018-02-14 14:16:28 +0100791static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100792{
793 uint64_t previous;
794 int tmp;
795
796 __asm __volatile("1:"
797 "ldrexd %0, [%4];"
798 "cmp %Q0, %Q2;"
799 "ittt eq;"
800 "cmpeq %R0, %R2;"
801 "strexdeq %1, %3, [%4];"
802 "cmpeq %1, #1;"
803 "beq 1b;"
804 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100805 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100806 : "memory", "cc");
807 tmp = (previous == *(uint64_t *)compare);
808 *(uint64_t *)compare = previous;
809 return (tmp);
810}
811
812#elif defined (__aarch64__)
813#define HA_HAVE_CAS_DW 1
814#define HA_CAS_IS_8B
815
816static __inline void
817__ha_barrier_load(void)
818{
819 __asm __volatile("dmb ishld" ::: "memory");
820}
821
822static __inline void
823__ha_barrier_store(void)
824{
825 __asm __volatile("dmb ishst" ::: "memory");
826}
827
828static __inline void
829__ha_barrier_full(void)
830{
831 __asm __volatile("dmb ish" ::: "memory");
832}
833
834static __inline int __ha_cas_dw(void *target, void *compare, void *set)
835{
836 void *value[2];
837 uint64_t tmp1, tmp2;
838
839 __asm__ __volatile__("1:"
840 "ldxp %0, %1, [%4];"
841 "mov %2, %0;"
842 "mov %3, %1;"
843 "eor %0, %0, %5;"
844 "eor %1, %1, %6;"
845 "orr %1, %0, %1;"
846 "mov %w0, #0;"
847 "cbnz %1, 2f;"
848 "stxp %w0, %7, %8, [%4];"
849 "cbnz %w0, 1b;"
850 "mov %w0, #1;"
851 "2:"
852 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
853 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
854 : "cc", "memory");
855
856 memcpy(compare, &value, sizeof(value));
857 return (tmp1);
858}
859
860#else
861#define __ha_barrier_load __sync_synchronize
862#define __ha_barrier_store __sync_synchronize
863#define __ha_barrier_full __sync_synchronize
864#endif
865
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200866#endif /* USE_THREAD */
867
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100868static inline void __ha_compiler_barrier(void)
869{
870 __asm __volatile("" ::: "memory");
871}
872
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200873/* Dummy I/O handler used by the sync pipe.*/
874void thread_sync_io_handler(int fd);
875
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200876#endif /* _COMMON_HATHREADS_H */