blob: c25f691023cdec30303b376435e0f6c91c4a79ef [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
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100111static inline void __ha_barrier_load(void)
112{
113}
114
115static inline void __ha_barrier_store(void)
116{
117}
118
119static inline void __ha_barrier_full(void)
120{
121}
122
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200123#else /* USE_THREAD */
124
125#include <stdio.h>
126#include <stdlib.h>
127#include <string.h>
128#include <pthread.h>
129#include <import/plock.h>
130
Willy Tarreau421f02e2018-01-20 18:19:22 +0100131#define MAX_THREADS LONGBITS
132
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100133#define __decl_hathreads(decl) decl
134
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200135/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
136 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100137
David Carlierec5e8452018-01-11 14:20:43 +0000138#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100139/* gcc < 4.7 */
140
141#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
142#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
143#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
144#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
145
146/* the CAS is a bit complicated. The older API doesn't support returning the
147 * value and the swap's result at the same time. So here we take what looks
148 * like the safest route, consisting in using the boolean version guaranteeing
149 * that the operation was performed or not, and we snoop a previous value. If
150 * the compare succeeds, we return. If it fails, we return the previous value,
151 * but only if it differs from the expected one. If it's the same it's a race
152 * thus we try again to avoid confusing a possibly sensitive caller.
153 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200154#define HA_ATOMIC_CAS(val, old, new) \
155 ({ \
156 typeof((val)) __val_cas = (val); \
157 typeof((old)) __oldp_cas = (old); \
158 typeof(*(old)) __oldv_cas; \
159 typeof((new)) __new_cas = (new); \
160 int __ret_cas; \
161 do { \
162 __oldv_cas = *__val_cas; \
163 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
164 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
165 if (!__ret_cas) \
166 *__oldp_cas = __oldv_cas; \
167 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100168 })
169
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200170#define HA_ATOMIC_XCHG(val, new) \
171 ({ \
172 typeof((val)) __val_xchg = (val); \
173 typeof(*(val)) __old_xchg; \
174 typeof((new)) __new_xchg = (new); \
175 do { __old_xchg = *__val_xchg; \
176 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
177 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100178 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100179
180#define HA_ATOMIC_BTS(val, bit) \
181 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200182 typeof(*(val)) __b_bts = (1UL << (bit)); \
183 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100184 })
185
186#define HA_ATOMIC_BTR(val, bit) \
187 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200188 typeof(*(val)) __b_btr = (1UL << (bit)); \
189 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100190 })
191
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200192#define HA_ATOMIC_STORE(val, new) \
193 ({ \
194 typeof((val)) __val_store = (val); \
195 typeof(*(val)) __old_store; \
196 typeof((new)) __new_store = (new); \
197 do { __old_store = *__val_store; \
198 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100199 })
200#else
201/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200202#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
203#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
204#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
205#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
206#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100207#define HA_ATOMIC_BTS(val, bit) \
208 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200209 typeof(*(val)) __b_bts = (1UL << (bit)); \
210 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100211 })
212
213#define HA_ATOMIC_BTR(val, bit) \
214 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200215 typeof(*(val)) __b_btr = (1UL << (bit)); \
216 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100217 })
218
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200219#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
220#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100221#endif
222
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200223#define HA_ATOMIC_UPDATE_MAX(val, new) \
224 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200225 typeof(*(val)) __old_max = *(val); \
226 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200227 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200228 while (__old_max < __new_max && \
229 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
230 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200231 })
232#define HA_ATOMIC_UPDATE_MIN(val, new) \
233 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200234 typeof(*(val)) __old_min = *(val); \
235 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200236 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200237 while (__old_min > __new_min && \
238 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
239 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200240 })
241
Willy Tarreaub29dc952017-10-31 18:00:20 +0100242#define HA_BARRIER() pl_barrier()
243
Christopher Faulet339fff82017-10-19 11:59:15 +0200244#define THREAD_SYNC_INIT(m) thread_sync_init(m)
245#define THREAD_SYNC_ENABLE() thread_sync_enable()
246#define THREAD_WANT_SYNC() thread_want_sync()
247#define THREAD_ENTER_SYNC() thread_enter_sync()
248#define THREAD_EXIT_SYNC() thread_exit_sync()
249#define THREAD_NO_SYNC() thread_no_sync()
250#define THREAD_NEED_SYNC() thread_need_sync()
251
252int thread_sync_init(unsigned long mask);
253void thread_sync_enable(void);
254void thread_want_sync(void);
255void thread_enter_sync(void);
256void thread_exit_sync(void);
257int thread_no_sync(void);
258int thread_need_sync(void);
259
Olivier Houchard6b96f722018-04-25 16:58:25 +0200260extern unsigned long all_threads_mask;
261
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200262#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
263
Christopher Fauletf51bac22018-01-30 11:04:29 +0100264/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200265enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200266 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200267 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200268 TASK_RQ_LOCK,
269 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200270 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200271 LISTENER_LOCK,
272 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200273 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200274 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200275 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200276 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200277 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200278 STK_TABLE_LOCK,
279 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200280 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200281 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200282 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200283 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200284 SSL_LOCK,
285 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200286 PATREF_LOCK,
287 PATEXP_LOCK,
288 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200289 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200290 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200291 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200292 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200293 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200294 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200295 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200296 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100297 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100298 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100299 TLSKEYS_REF_LOCK,
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100300 PENDCONN_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200301 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200302};
303struct lock_stat {
304 uint64_t nsec_wait_for_write;
305 uint64_t nsec_wait_for_read;
306 uint64_t num_write_locked;
307 uint64_t num_write_unlocked;
308 uint64_t num_read_locked;
309 uint64_t num_read_unlocked;
310};
311
312extern struct lock_stat lock_stats[LOCK_LABELS];
313
314#define __HA_SPINLOCK_T unsigned long
315
316#define __SPIN_INIT(l) ({ (*l) = 0; })
317#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100318#define __SPIN_LOCK(l) pl_take_s(l)
319#define __SPIN_TRYLOCK(l) !pl_try_s(l)
320#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200321
322#define __HA_RWLOCK_T unsigned long
323
324#define __RWLOCK_INIT(l) ({ (*l) = 0; })
325#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
326#define __RWLOCK_WRLOCK(l) pl_take_w(l)
327#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
328#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
329#define __RWLOCK_RDLOCK(l) pl_take_r(l)
330#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
331#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
332
333#define HA_SPINLOCK_T struct ha_spinlock
334
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335#define HA_SPIN_INIT(l) __spin_init(l)
336#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200337
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
339#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
340#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200341
342#define HA_RWLOCK_T struct ha_rwlock
343
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
345#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
346#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
347#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
348#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
349#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
350#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
351#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200352
353struct ha_spinlock {
354 __HA_SPINLOCK_T lock;
355 struct {
356 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
357 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
358 struct {
359 const char *function;
360 const char *file;
361 int line;
362 } last_location; /* location of the last owner */
363 } info;
364};
365
366struct ha_rwlock {
367 __HA_RWLOCK_T lock;
368 struct {
369 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
370 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
371 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
372 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
373 struct {
374 const char *function;
375 const char *file;
376 int line;
377 } last_location; /* location of the last write owner */
378 } info;
379};
380
Christopher Fauletf51bac22018-01-30 11:04:29 +0100381static inline const char *lock_label(enum lock_label label)
382{
383 switch (label) {
384 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100385 case FD_LOCK: return "FD";
386 case TASK_RQ_LOCK: return "TASK_RQ";
387 case TASK_WQ_LOCK: return "TASK_WQ";
388 case POOL_LOCK: return "POOL";
389 case LISTENER_LOCK: return "LISTENER";
390 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
391 case PROXY_LOCK: return "PROXY";
392 case SERVER_LOCK: return "SERVER";
393 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
394 case LBPRM_LOCK: return "LBPRM";
395 case SIGNALS_LOCK: return "SIGNALS";
396 case STK_TABLE_LOCK: return "STK_TABLE";
397 case STK_SESS_LOCK: return "STK_SESS";
398 case APPLETS_LOCK: return "APPLETS";
399 case PEER_LOCK: return "PEER";
400 case BUF_WQ_LOCK: return "BUF_WQ";
401 case STRMS_LOCK: return "STRMS";
402 case SSL_LOCK: return "SSL";
403 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
404 case PATREF_LOCK: return "PATREF";
405 case PATEXP_LOCK: return "PATEXP";
406 case PATLRU_LOCK: return "PATLRU";
407 case VARS_LOCK: return "VARS";
408 case COMP_POOL_LOCK: return "COMP_POOL";
409 case LUA_LOCK: return "LUA";
410 case NOTIF_LOCK: return "NOTIF";
411 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
412 case DNS_LOCK: return "DNS";
413 case PID_LIST_LOCK: return "PID_LIST";
414 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
415 case PIPES_LOCK: return "PIPES";
416 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100417 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100418 case PENDCONN_LOCK: return "PENDCONN";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100419 case LOCK_LABELS: break; /* keep compiler happy */
420 };
421 /* only way to come here is consecutive to an internal bug */
422 abort();
423}
424
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200425static inline void show_lock_stats()
426{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200427 int lbl;
428
429 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
430 fprintf(stderr,
431 "Stats about Lock %s: \n"
432 "\t # write lock : %lu\n"
433 "\t # write unlock: %lu (%ld)\n"
434 "\t # wait time for write : %.3f msec\n"
435 "\t # wait time for write/lock: %.3f nsec\n"
436 "\t # read lock : %lu\n"
437 "\t # read unlock : %lu (%ld)\n"
438 "\t # wait time for read : %.3f msec\n"
439 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100440 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200441 lock_stats[lbl].num_write_locked,
442 lock_stats[lbl].num_write_unlocked,
443 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
444 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
445 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
446 lock_stats[lbl].num_read_locked,
447 lock_stats[lbl].num_read_unlocked,
448 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
449 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
450 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
451 }
452}
453
454/* Following functions are used to collect some stats about locks. We wrap
455 * pthread functions to known how much time we wait in a lock. */
456
457static uint64_t nsec_now(void) {
458 struct timespec ts;
459
460 clock_gettime(CLOCK_MONOTONIC, &ts);
461 return ((uint64_t) ts.tv_sec * 1000000000ULL +
462 (uint64_t) ts.tv_nsec);
463}
464
465static inline void __ha_rwlock_init(struct ha_rwlock *l)
466{
467 memset(l, 0, sizeof(struct ha_rwlock));
468 __RWLOCK_INIT(&l->lock);
469}
470
471static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
472{
473 __RWLOCK_DESTROY(&l->lock);
474 memset(l, 0, sizeof(struct ha_rwlock));
475}
476
477
478static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
479 const char *func, const char *file, int line)
480{
481 uint64_t start_time;
482
483 if (unlikely(l->info.cur_writer & tid_bit)) {
484 /* the thread is already owning the lock for write */
485 abort();
486 }
487
488 if (unlikely(l->info.cur_readers & tid_bit)) {
489 /* the thread is already owning the lock for read */
490 abort();
491 }
492
493 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
494
495 start_time = nsec_now();
496 __RWLOCK_WRLOCK(&l->lock);
497 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
498
499 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
500
501 l->info.cur_writer = tid_bit;
502 l->info.last_location.function = func;
503 l->info.last_location.file = file;
504 l->info.last_location.line = line;
505
506 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
507}
508
509static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
510 const char *func, const char *file, int line)
511{
512 uint64_t start_time;
513 int r;
514
515 if (unlikely(l->info.cur_writer & tid_bit)) {
516 /* the thread is already owning the lock for write */
517 abort();
518 }
519
520 if (unlikely(l->info.cur_readers & tid_bit)) {
521 /* the thread is already owning the lock for read */
522 abort();
523 }
524
525 /* We set waiting writer because trywrlock could wait for readers to quit */
526 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
527
528 start_time = nsec_now();
529 r = __RWLOCK_TRYWRLOCK(&l->lock);
530 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
531 if (unlikely(r)) {
532 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
533 return r;
534 }
535 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
536
537 l->info.cur_writer = tid_bit;
538 l->info.last_location.function = func;
539 l->info.last_location.file = file;
540 l->info.last_location.line = line;
541
542 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
543
544 return 0;
545}
546
547static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
548 const char *func, const char *file, int line)
549{
550 if (unlikely(!(l->info.cur_writer & tid_bit))) {
551 /* the thread is not owning the lock for write */
552 abort();
553 }
554
555 l->info.cur_writer = 0;
556 l->info.last_location.function = func;
557 l->info.last_location.file = file;
558 l->info.last_location.line = line;
559
560 __RWLOCK_WRUNLOCK(&l->lock);
561
562 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
563}
564
565static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
566{
567 uint64_t start_time;
568
569 if (unlikely(l->info.cur_writer & tid_bit)) {
570 /* the thread is already owning the lock for write */
571 abort();
572 }
573
574 if (unlikely(l->info.cur_readers & tid_bit)) {
575 /* the thread is already owning the lock for read */
576 abort();
577 }
578
579 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
580
581 start_time = nsec_now();
582 __RWLOCK_RDLOCK(&l->lock);
583 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
584 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
585
586 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
587
588 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
589}
590
591static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
592{
593 int r;
594
595 if (unlikely(l->info.cur_writer & tid_bit)) {
596 /* the thread is already owning the lock for write */
597 abort();
598 }
599
600 if (unlikely(l->info.cur_readers & tid_bit)) {
601 /* the thread is already owning the lock for read */
602 abort();
603 }
604
605 /* try read should never wait */
606 r = __RWLOCK_TRYRDLOCK(&l->lock);
607 if (unlikely(r))
608 return r;
609 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
610
611 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
612
613 return 0;
614}
615
616static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
617{
618 if (unlikely(!(l->info.cur_readers & tid_bit))) {
619 /* the thread is not owning the lock for read */
620 abort();
621 }
622
623 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
624
625 __RWLOCK_RDUNLOCK(&l->lock);
626
627 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
628}
629
630static inline void __spin_init(struct ha_spinlock *l)
631{
632 memset(l, 0, sizeof(struct ha_spinlock));
633 __SPIN_INIT(&l->lock);
634}
635
636static inline void __spin_destroy(struct ha_spinlock *l)
637{
638 __SPIN_DESTROY(&l->lock);
639 memset(l, 0, sizeof(struct ha_spinlock));
640}
641
642static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
643 const char *func, const char *file, int line)
644{
645 uint64_t start_time;
646
647 if (unlikely(l->info.owner & tid_bit)) {
648 /* the thread is already owning the lock */
649 abort();
650 }
651
652 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
653
654 start_time = nsec_now();
655 __SPIN_LOCK(&l->lock);
656 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
657
658 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
659
660
661 l->info.owner = tid_bit;
662 l->info.last_location.function = func;
663 l->info.last_location.file = file;
664 l->info.last_location.line = line;
665
666 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
667}
668
669static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
670 const char *func, const char *file, int line)
671{
672 int r;
673
674 if (unlikely(l->info.owner & tid_bit)) {
675 /* the thread is already owning the lock */
676 abort();
677 }
678
679 /* try read should never wait */
680 r = __SPIN_TRYLOCK(&l->lock);
681 if (unlikely(r))
682 return r;
683 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
684
685 l->info.owner = tid_bit;
686 l->info.last_location.function = func;
687 l->info.last_location.file = file;
688 l->info.last_location.line = line;
689
690 return 0;
691}
692
693static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
694 const char *func, const char *file, int line)
695{
696 if (unlikely(!(l->info.owner & tid_bit))) {
697 /* the thread is not owning the lock */
698 abort();
699 }
700
701 l->info.owner = 0;
702 l->info.last_location.function = func;
703 l->info.last_location.file = file;
704 l->info.last_location.line = line;
705
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100706 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200707 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
708}
709
710#else /* DEBUG_THREAD */
711
712#define HA_SPINLOCK_T unsigned long
713
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100714#define HA_SPIN_INIT(l) ({ (*l) = 0; })
715#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
716#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
717#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
718#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200719
720#define HA_RWLOCK_T unsigned long
721
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100722#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
723#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
724#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
725#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
726#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
727#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
728#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
729#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200730
731#endif /* DEBUG_THREAD */
732
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100733#ifdef __x86_64__
734#define HA_HAVE_CAS_DW 1
735#define HA_CAS_IS_8B
736static __inline int
737__ha_cas_dw(void *target, void *compare, const void *set)
738{
739 char ret;
740
741 __asm __volatile("lock cmpxchg16b %0; setz %3"
742 : "+m" (*(void **)target),
743 "=a" (((void **)compare)[0]),
744 "=d" (((void **)compare)[1]),
745 "=q" (ret)
746 : "a" (((void **)compare)[0]),
747 "d" (((void **)compare)[1]),
748 "b" (((const void **)set)[0]),
749 "c" (((const void **)set)[1])
750 : "memory", "cc");
751 return (ret);
752}
753
754static __inline void
755__ha_barrier_load(void)
756{
757 __asm __volatile("lfence" ::: "memory");
758}
759
760static __inline void
761__ha_barrier_store(void)
762{
763 __asm __volatile("sfence" ::: "memory");
764}
765
766static __inline void
767__ha_barrier_full(void)
768{
769 __asm __volatile("mfence" ::: "memory");
770}
771
772#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
773#define HA_HAVE_CAS_DW 1
774static __inline void
775__ha_barrier_load(void)
776{
777 __asm __volatile("dmb" ::: "memory");
778}
779
780static __inline void
781__ha_barrier_store(void)
782{
783 __asm __volatile("dsb" ::: "memory");
784}
785
786static __inline void
787__ha_barrier_full(void)
788{
789 __asm __volatile("dmb" ::: "memory");
790}
791
Willy Tarreau41ccb192018-02-14 14:16:28 +0100792static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100793{
794 uint64_t previous;
795 int tmp;
796
797 __asm __volatile("1:"
798 "ldrexd %0, [%4];"
799 "cmp %Q0, %Q2;"
800 "ittt eq;"
801 "cmpeq %R0, %R2;"
802 "strexdeq %1, %3, [%4];"
803 "cmpeq %1, #1;"
804 "beq 1b;"
805 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100806 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100807 : "memory", "cc");
808 tmp = (previous == *(uint64_t *)compare);
809 *(uint64_t *)compare = previous;
810 return (tmp);
811}
812
813#elif defined (__aarch64__)
814#define HA_HAVE_CAS_DW 1
815#define HA_CAS_IS_8B
816
817static __inline void
818__ha_barrier_load(void)
819{
820 __asm __volatile("dmb ishld" ::: "memory");
821}
822
823static __inline void
824__ha_barrier_store(void)
825{
826 __asm __volatile("dmb ishst" ::: "memory");
827}
828
829static __inline void
830__ha_barrier_full(void)
831{
832 __asm __volatile("dmb ish" ::: "memory");
833}
834
835static __inline int __ha_cas_dw(void *target, void *compare, void *set)
836{
837 void *value[2];
838 uint64_t tmp1, tmp2;
839
840 __asm__ __volatile__("1:"
841 "ldxp %0, %1, [%4];"
842 "mov %2, %0;"
843 "mov %3, %1;"
844 "eor %0, %0, %5;"
845 "eor %1, %1, %6;"
846 "orr %1, %0, %1;"
847 "mov %w0, #0;"
848 "cbnz %1, 2f;"
849 "stxp %w0, %7, %8, [%4];"
850 "cbnz %w0, 1b;"
851 "mov %w0, #1;"
852 "2:"
853 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
854 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
855 : "cc", "memory");
856
857 memcpy(compare, &value, sizeof(value));
858 return (tmp1);
859}
860
861#else
862#define __ha_barrier_load __sync_synchronize
863#define __ha_barrier_store __sync_synchronize
864#define __ha_barrier_full __sync_synchronize
865#endif
866
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200867#endif /* USE_THREAD */
868
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100869static inline void __ha_compiler_barrier(void)
870{
871 __asm __volatile("" ::: "memory");
872}
873
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200874/* Dummy I/O handler used by the sync pipe.*/
875void thread_sync_io_handler(int fd);
876
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200877#endif /* _COMMON_HATHREADS_H */