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