blob: 5c4ceca7c1383ef1a539f63d08f8187425b69494 [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
Willy Tarreau0cd82e82018-05-23 19:54:43 +020034#define all_threads_mask 0UL
Willy Tarreau421f02e2018-01-20 18:19:22 +010035
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010036#define __decl_hathreads(decl)
37
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020038#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
39#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
40#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
41#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
42#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
43#define HA_ATOMIC_XCHG(val, new) \
44 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020045 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020046 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020047 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020048 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010049#define HA_ATOMIC_BTS(val, bit) \
50 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020051 typeof((val)) __p_bts = (val); \
52 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
53 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
54 if (!__t_bts) \
55 *__p_bts |= __b_bts; \
56 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010057 })
58#define HA_ATOMIC_BTR(val, bit) \
59 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020060 typeof((val)) __p_btr = (val); \
61 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
62 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
63 if (__t_btr) \
64 *__p_btr &= ~__b_btr; \
65 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010066 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020067#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
68#define HA_ATOMIC_UPDATE_MAX(val, new) \
69 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020070 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020071 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020072 if (*(val) < __new_max) \
73 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020074 *(val); \
75 })
76
77#define HA_ATOMIC_UPDATE_MIN(val, new) \
78 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020079 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020080 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020081 if (*(val) > __new_min) \
82 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020083 *(val); \
84 })
85
Willy Tarreaub29dc952017-10-31 18:00:20 +010086#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020087
88#define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0)
89#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
90#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
91#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
92#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
93#define THREAD_NO_SYNC() ({ 0; })
94#define THREAD_NEED_SYNC() ({ 1; })
95
Christopher Faulet2a944ee2017-11-07 10:42:54 +010096#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
97#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
98#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
99#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
100#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200101
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100102#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
103#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
104#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
105#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
106#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
107#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
108#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
109#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110
William Lallemand6e1796e2018-06-07 11:23:40 +0200111#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
112
113
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100114static inline void __ha_barrier_load(void)
115{
116}
117
118static inline void __ha_barrier_store(void)
119{
120}
121
122static inline void __ha_barrier_full(void)
123{
124}
125
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200126#else /* USE_THREAD */
127
128#include <stdio.h>
129#include <stdlib.h>
130#include <string.h>
131#include <pthread.h>
132#include <import/plock.h>
133
Willy Tarreau421f02e2018-01-20 18:19:22 +0100134#define MAX_THREADS LONGBITS
135
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100136#define __decl_hathreads(decl) decl
137
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200138/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
139 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100140
David Carlierec5e8452018-01-11 14:20:43 +0000141#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100142/* gcc < 4.7 */
143
144#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
145#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
146#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
147#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
148
149/* the CAS is a bit complicated. The older API doesn't support returning the
150 * value and the swap's result at the same time. So here we take what looks
151 * like the safest route, consisting in using the boolean version guaranteeing
152 * that the operation was performed or not, and we snoop a previous value. If
153 * the compare succeeds, we return. If it fails, we return the previous value,
154 * but only if it differs from the expected one. If it's the same it's a race
155 * thus we try again to avoid confusing a possibly sensitive caller.
156 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200157#define HA_ATOMIC_CAS(val, old, new) \
158 ({ \
159 typeof((val)) __val_cas = (val); \
160 typeof((old)) __oldp_cas = (old); \
161 typeof(*(old)) __oldv_cas; \
162 typeof((new)) __new_cas = (new); \
163 int __ret_cas; \
164 do { \
165 __oldv_cas = *__val_cas; \
166 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
167 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
168 if (!__ret_cas) \
169 *__oldp_cas = __oldv_cas; \
170 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100171 })
172
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200173#define HA_ATOMIC_XCHG(val, new) \
174 ({ \
175 typeof((val)) __val_xchg = (val); \
176 typeof(*(val)) __old_xchg; \
177 typeof((new)) __new_xchg = (new); \
178 do { __old_xchg = *__val_xchg; \
179 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
180 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100181 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100182
183#define HA_ATOMIC_BTS(val, bit) \
184 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200185 typeof(*(val)) __b_bts = (1UL << (bit)); \
186 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100187 })
188
189#define HA_ATOMIC_BTR(val, bit) \
190 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200191 typeof(*(val)) __b_btr = (1UL << (bit)); \
192 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100193 })
194
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200195#define HA_ATOMIC_STORE(val, new) \
196 ({ \
197 typeof((val)) __val_store = (val); \
198 typeof(*(val)) __old_store; \
199 typeof((new)) __new_store = (new); \
200 do { __old_store = *__val_store; \
201 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100202 })
203#else
204/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200205#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
206#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
207#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
208#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
209#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100210#define HA_ATOMIC_BTS(val, bit) \
211 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200212 typeof(*(val)) __b_bts = (1UL << (bit)); \
213 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100214 })
215
216#define HA_ATOMIC_BTR(val, bit) \
217 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200218 typeof(*(val)) __b_btr = (1UL << (bit)); \
219 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100220 })
221
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200222#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
223#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100224#endif
225
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200226#define HA_ATOMIC_UPDATE_MAX(val, new) \
227 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200228 typeof(*(val)) __old_max = *(val); \
229 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200230 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200231 while (__old_max < __new_max && \
232 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
233 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200234 })
235#define HA_ATOMIC_UPDATE_MIN(val, new) \
236 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200237 typeof(*(val)) __old_min = *(val); \
238 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200239 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200240 while (__old_min > __new_min && \
241 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
242 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200243 })
244
Willy Tarreaub29dc952017-10-31 18:00:20 +0100245#define HA_BARRIER() pl_barrier()
246
Christopher Faulet339fff82017-10-19 11:59:15 +0200247#define THREAD_SYNC_INIT(m) thread_sync_init(m)
248#define THREAD_SYNC_ENABLE() thread_sync_enable()
249#define THREAD_WANT_SYNC() thread_want_sync()
250#define THREAD_ENTER_SYNC() thread_enter_sync()
251#define THREAD_EXIT_SYNC() thread_exit_sync()
252#define THREAD_NO_SYNC() thread_no_sync()
253#define THREAD_NEED_SYNC() thread_need_sync()
254
255int thread_sync_init(unsigned long mask);
256void thread_sync_enable(void);
257void thread_want_sync(void);
258void thread_enter_sync(void);
259void thread_exit_sync(void);
260int thread_no_sync(void);
261int thread_need_sync(void);
262
Olivier Houchard6b96f722018-04-25 16:58:25 +0200263extern unsigned long all_threads_mask;
264
William Lallemand6e1796e2018-06-07 11:23:40 +0200265#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
266
267
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200268#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
269
Christopher Fauletf51bac22018-01-30 11:04:29 +0100270/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200271enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200272 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200273 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200274 TASK_RQ_LOCK,
275 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200276 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200277 LISTENER_LOCK,
278 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200279 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200280 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200281 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200282 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200283 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200284 STK_TABLE_LOCK,
285 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200286 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200287 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200288 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200289 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200290 SSL_LOCK,
291 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200292 PATREF_LOCK,
293 PATEXP_LOCK,
294 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200295 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200296 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200297 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200298 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200299 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200300 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200301 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200302 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100303 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100304 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100305 TLSKEYS_REF_LOCK,
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100306 PENDCONN_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200307 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200308};
309struct lock_stat {
310 uint64_t nsec_wait_for_write;
311 uint64_t nsec_wait_for_read;
312 uint64_t num_write_locked;
313 uint64_t num_write_unlocked;
314 uint64_t num_read_locked;
315 uint64_t num_read_unlocked;
316};
317
318extern struct lock_stat lock_stats[LOCK_LABELS];
319
320#define __HA_SPINLOCK_T unsigned long
321
322#define __SPIN_INIT(l) ({ (*l) = 0; })
323#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100324#define __SPIN_LOCK(l) pl_take_s(l)
325#define __SPIN_TRYLOCK(l) !pl_try_s(l)
326#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200327
328#define __HA_RWLOCK_T unsigned long
329
330#define __RWLOCK_INIT(l) ({ (*l) = 0; })
331#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
332#define __RWLOCK_WRLOCK(l) pl_take_w(l)
333#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
334#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
335#define __RWLOCK_RDLOCK(l) pl_take_r(l)
336#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
337#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
338
339#define HA_SPINLOCK_T struct ha_spinlock
340
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341#define HA_SPIN_INIT(l) __spin_init(l)
342#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200343
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
345#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
346#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200347
348#define HA_RWLOCK_T struct ha_rwlock
349
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
351#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
352#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
353#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
354#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
355#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
356#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
357#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200358
359struct ha_spinlock {
360 __HA_SPINLOCK_T lock;
361 struct {
362 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
363 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
364 struct {
365 const char *function;
366 const char *file;
367 int line;
368 } last_location; /* location of the last owner */
369 } info;
370};
371
372struct ha_rwlock {
373 __HA_RWLOCK_T lock;
374 struct {
375 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
376 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
377 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
378 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
379 struct {
380 const char *function;
381 const char *file;
382 int line;
383 } last_location; /* location of the last write owner */
384 } info;
385};
386
Christopher Fauletf51bac22018-01-30 11:04:29 +0100387static inline const char *lock_label(enum lock_label label)
388{
389 switch (label) {
390 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100391 case FD_LOCK: return "FD";
392 case TASK_RQ_LOCK: return "TASK_RQ";
393 case TASK_WQ_LOCK: return "TASK_WQ";
394 case POOL_LOCK: return "POOL";
395 case LISTENER_LOCK: return "LISTENER";
396 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
397 case PROXY_LOCK: return "PROXY";
398 case SERVER_LOCK: return "SERVER";
399 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
400 case LBPRM_LOCK: return "LBPRM";
401 case SIGNALS_LOCK: return "SIGNALS";
402 case STK_TABLE_LOCK: return "STK_TABLE";
403 case STK_SESS_LOCK: return "STK_SESS";
404 case APPLETS_LOCK: return "APPLETS";
405 case PEER_LOCK: return "PEER";
406 case BUF_WQ_LOCK: return "BUF_WQ";
407 case STRMS_LOCK: return "STRMS";
408 case SSL_LOCK: return "SSL";
409 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
410 case PATREF_LOCK: return "PATREF";
411 case PATEXP_LOCK: return "PATEXP";
412 case PATLRU_LOCK: return "PATLRU";
413 case VARS_LOCK: return "VARS";
414 case COMP_POOL_LOCK: return "COMP_POOL";
415 case LUA_LOCK: return "LUA";
416 case NOTIF_LOCK: return "NOTIF";
417 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
418 case DNS_LOCK: return "DNS";
419 case PID_LIST_LOCK: return "PID_LIST";
420 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
421 case PIPES_LOCK: return "PIPES";
422 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100423 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100424 case PENDCONN_LOCK: return "PENDCONN";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100425 case LOCK_LABELS: break; /* keep compiler happy */
426 };
427 /* only way to come here is consecutive to an internal bug */
428 abort();
429}
430
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200431static inline void show_lock_stats()
432{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200433 int lbl;
434
435 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
436 fprintf(stderr,
437 "Stats about Lock %s: \n"
438 "\t # write lock : %lu\n"
439 "\t # write unlock: %lu (%ld)\n"
440 "\t # wait time for write : %.3f msec\n"
441 "\t # wait time for write/lock: %.3f nsec\n"
442 "\t # read lock : %lu\n"
443 "\t # read unlock : %lu (%ld)\n"
444 "\t # wait time for read : %.3f msec\n"
445 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100446 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200447 lock_stats[lbl].num_write_locked,
448 lock_stats[lbl].num_write_unlocked,
449 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
450 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
451 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
452 lock_stats[lbl].num_read_locked,
453 lock_stats[lbl].num_read_unlocked,
454 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
455 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
456 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
457 }
458}
459
460/* Following functions are used to collect some stats about locks. We wrap
461 * pthread functions to known how much time we wait in a lock. */
462
463static uint64_t nsec_now(void) {
464 struct timespec ts;
465
466 clock_gettime(CLOCK_MONOTONIC, &ts);
467 return ((uint64_t) ts.tv_sec * 1000000000ULL +
468 (uint64_t) ts.tv_nsec);
469}
470
471static inline void __ha_rwlock_init(struct ha_rwlock *l)
472{
473 memset(l, 0, sizeof(struct ha_rwlock));
474 __RWLOCK_INIT(&l->lock);
475}
476
477static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
478{
479 __RWLOCK_DESTROY(&l->lock);
480 memset(l, 0, sizeof(struct ha_rwlock));
481}
482
483
484static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
485 const char *func, const char *file, int line)
486{
487 uint64_t start_time;
488
489 if (unlikely(l->info.cur_writer & tid_bit)) {
490 /* the thread is already owning the lock for write */
491 abort();
492 }
493
494 if (unlikely(l->info.cur_readers & tid_bit)) {
495 /* the thread is already owning the lock for read */
496 abort();
497 }
498
499 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
500
501 start_time = nsec_now();
502 __RWLOCK_WRLOCK(&l->lock);
503 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
504
505 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
506
507 l->info.cur_writer = tid_bit;
508 l->info.last_location.function = func;
509 l->info.last_location.file = file;
510 l->info.last_location.line = line;
511
512 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
513}
514
515static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
516 const char *func, const char *file, int line)
517{
518 uint64_t start_time;
519 int r;
520
521 if (unlikely(l->info.cur_writer & tid_bit)) {
522 /* the thread is already owning the lock for write */
523 abort();
524 }
525
526 if (unlikely(l->info.cur_readers & tid_bit)) {
527 /* the thread is already owning the lock for read */
528 abort();
529 }
530
531 /* We set waiting writer because trywrlock could wait for readers to quit */
532 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
533
534 start_time = nsec_now();
535 r = __RWLOCK_TRYWRLOCK(&l->lock);
536 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
537 if (unlikely(r)) {
538 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
539 return r;
540 }
541 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
542
543 l->info.cur_writer = tid_bit;
544 l->info.last_location.function = func;
545 l->info.last_location.file = file;
546 l->info.last_location.line = line;
547
548 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
549
550 return 0;
551}
552
553static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
554 const char *func, const char *file, int line)
555{
556 if (unlikely(!(l->info.cur_writer & tid_bit))) {
557 /* the thread is not owning the lock for write */
558 abort();
559 }
560
561 l->info.cur_writer = 0;
562 l->info.last_location.function = func;
563 l->info.last_location.file = file;
564 l->info.last_location.line = line;
565
566 __RWLOCK_WRUNLOCK(&l->lock);
567
568 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
569}
570
571static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
572{
573 uint64_t start_time;
574
575 if (unlikely(l->info.cur_writer & tid_bit)) {
576 /* the thread is already owning the lock for write */
577 abort();
578 }
579
580 if (unlikely(l->info.cur_readers & tid_bit)) {
581 /* the thread is already owning the lock for read */
582 abort();
583 }
584
585 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
586
587 start_time = nsec_now();
588 __RWLOCK_RDLOCK(&l->lock);
589 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
590 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
591
592 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
593
594 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
595}
596
597static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
598{
599 int r;
600
601 if (unlikely(l->info.cur_writer & tid_bit)) {
602 /* the thread is already owning the lock for write */
603 abort();
604 }
605
606 if (unlikely(l->info.cur_readers & tid_bit)) {
607 /* the thread is already owning the lock for read */
608 abort();
609 }
610
611 /* try read should never wait */
612 r = __RWLOCK_TRYRDLOCK(&l->lock);
613 if (unlikely(r))
614 return r;
615 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
616
617 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
618
619 return 0;
620}
621
622static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
623{
624 if (unlikely(!(l->info.cur_readers & tid_bit))) {
625 /* the thread is not owning the lock for read */
626 abort();
627 }
628
629 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
630
631 __RWLOCK_RDUNLOCK(&l->lock);
632
633 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
634}
635
636static inline void __spin_init(struct ha_spinlock *l)
637{
638 memset(l, 0, sizeof(struct ha_spinlock));
639 __SPIN_INIT(&l->lock);
640}
641
642static inline void __spin_destroy(struct ha_spinlock *l)
643{
644 __SPIN_DESTROY(&l->lock);
645 memset(l, 0, sizeof(struct ha_spinlock));
646}
647
648static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
649 const char *func, const char *file, int line)
650{
651 uint64_t start_time;
652
653 if (unlikely(l->info.owner & tid_bit)) {
654 /* the thread is already owning the lock */
655 abort();
656 }
657
658 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
659
660 start_time = nsec_now();
661 __SPIN_LOCK(&l->lock);
662 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
663
664 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
665
666
667 l->info.owner = tid_bit;
668 l->info.last_location.function = func;
669 l->info.last_location.file = file;
670 l->info.last_location.line = line;
671
672 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
673}
674
675static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
676 const char *func, const char *file, int line)
677{
678 int r;
679
680 if (unlikely(l->info.owner & tid_bit)) {
681 /* the thread is already owning the lock */
682 abort();
683 }
684
685 /* try read should never wait */
686 r = __SPIN_TRYLOCK(&l->lock);
687 if (unlikely(r))
688 return r;
689 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
690
691 l->info.owner = tid_bit;
692 l->info.last_location.function = func;
693 l->info.last_location.file = file;
694 l->info.last_location.line = line;
695
696 return 0;
697}
698
699static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
700 const char *func, const char *file, int line)
701{
702 if (unlikely(!(l->info.owner & tid_bit))) {
703 /* the thread is not owning the lock */
704 abort();
705 }
706
707 l->info.owner = 0;
708 l->info.last_location.function = func;
709 l->info.last_location.file = file;
710 l->info.last_location.line = line;
711
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100712 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200713 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
714}
715
716#else /* DEBUG_THREAD */
717
718#define HA_SPINLOCK_T unsigned long
719
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100720#define HA_SPIN_INIT(l) ({ (*l) = 0; })
721#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
722#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
723#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
724#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200725
726#define HA_RWLOCK_T unsigned long
727
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100728#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
729#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
730#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
731#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
732#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
733#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
734#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
735#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200736
737#endif /* DEBUG_THREAD */
738
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100739#ifdef __x86_64__
740#define HA_HAVE_CAS_DW 1
741#define HA_CAS_IS_8B
742static __inline int
743__ha_cas_dw(void *target, void *compare, const void *set)
744{
745 char ret;
746
747 __asm __volatile("lock cmpxchg16b %0; setz %3"
748 : "+m" (*(void **)target),
749 "=a" (((void **)compare)[0]),
750 "=d" (((void **)compare)[1]),
751 "=q" (ret)
752 : "a" (((void **)compare)[0]),
753 "d" (((void **)compare)[1]),
754 "b" (((const void **)set)[0]),
755 "c" (((const void **)set)[1])
756 : "memory", "cc");
757 return (ret);
758}
759
760static __inline void
761__ha_barrier_load(void)
762{
763 __asm __volatile("lfence" ::: "memory");
764}
765
766static __inline void
767__ha_barrier_store(void)
768{
769 __asm __volatile("sfence" ::: "memory");
770}
771
772static __inline void
773__ha_barrier_full(void)
774{
775 __asm __volatile("mfence" ::: "memory");
776}
777
778#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
779#define HA_HAVE_CAS_DW 1
780static __inline void
781__ha_barrier_load(void)
782{
783 __asm __volatile("dmb" ::: "memory");
784}
785
786static __inline void
787__ha_barrier_store(void)
788{
789 __asm __volatile("dsb" ::: "memory");
790}
791
792static __inline void
793__ha_barrier_full(void)
794{
795 __asm __volatile("dmb" ::: "memory");
796}
797
Willy Tarreau41ccb192018-02-14 14:16:28 +0100798static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100799{
800 uint64_t previous;
801 int tmp;
802
803 __asm __volatile("1:"
804 "ldrexd %0, [%4];"
805 "cmp %Q0, %Q2;"
806 "ittt eq;"
807 "cmpeq %R0, %R2;"
808 "strexdeq %1, %3, [%4];"
809 "cmpeq %1, #1;"
810 "beq 1b;"
811 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100812 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100813 : "memory", "cc");
814 tmp = (previous == *(uint64_t *)compare);
815 *(uint64_t *)compare = previous;
816 return (tmp);
817}
818
819#elif defined (__aarch64__)
820#define HA_HAVE_CAS_DW 1
821#define HA_CAS_IS_8B
822
823static __inline void
824__ha_barrier_load(void)
825{
826 __asm __volatile("dmb ishld" ::: "memory");
827}
828
829static __inline void
830__ha_barrier_store(void)
831{
832 __asm __volatile("dmb ishst" ::: "memory");
833}
834
835static __inline void
836__ha_barrier_full(void)
837{
838 __asm __volatile("dmb ish" ::: "memory");
839}
840
841static __inline int __ha_cas_dw(void *target, void *compare, void *set)
842{
843 void *value[2];
844 uint64_t tmp1, tmp2;
845
846 __asm__ __volatile__("1:"
847 "ldxp %0, %1, [%4];"
848 "mov %2, %0;"
849 "mov %3, %1;"
850 "eor %0, %0, %5;"
851 "eor %1, %1, %6;"
852 "orr %1, %0, %1;"
853 "mov %w0, #0;"
854 "cbnz %1, 2f;"
855 "stxp %w0, %7, %8, [%4];"
856 "cbnz %w0, 1b;"
857 "mov %w0, #1;"
858 "2:"
859 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
860 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
861 : "cc", "memory");
862
863 memcpy(compare, &value, sizeof(value));
864 return (tmp1);
865}
866
867#else
868#define __ha_barrier_load __sync_synchronize
869#define __ha_barrier_store __sync_synchronize
870#define __ha_barrier_full __sync_synchronize
871#endif
872
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200873#endif /* USE_THREAD */
874
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100875static inline void __ha_compiler_barrier(void)
876{
877 __asm __volatile("" ::: "memory");
878}
879
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200880/* Dummy I/O handler used by the sync pipe.*/
881void thread_sync_io_handler(int fd);
882
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200883#endif /* _COMMON_HATHREADS_H */