blob: 068b2a1530a7b9bb3b3fed1996010bb6b84714d0 [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
Willy Tarreau0ccd3222018-07-30 10:34:35 +020031/* Note about all_threads_mask :
32 * - with threads support disabled, this symbol is defined as zero (0UL).
33 * - with threads enabled, this variable is never zero, it contains the mask
34 * of enabled threads. Thus if only one thread is enabled, it equals 1.
35 */
36
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020037#ifndef USE_THREAD
38
Willy Tarreau421f02e2018-01-20 18:19:22 +010039#define MAX_THREADS 1
Willy Tarreau0cd82e82018-05-23 19:54:43 +020040#define all_threads_mask 0UL
Willy Tarreau421f02e2018-01-20 18:19:22 +010041
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010042#define __decl_hathreads(decl)
43
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020044#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
45#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
46#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
47#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
48#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
49#define HA_ATOMIC_XCHG(val, new) \
50 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020051 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020052 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020053 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020054 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010055#define HA_ATOMIC_BTS(val, bit) \
56 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020057 typeof((val)) __p_bts = (val); \
58 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
59 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
60 if (!__t_bts) \
61 *__p_bts |= __b_bts; \
62 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010063 })
64#define HA_ATOMIC_BTR(val, bit) \
65 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020066 typeof((val)) __p_btr = (val); \
67 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
68 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
69 if (__t_btr) \
70 *__p_btr &= ~__b_btr; \
71 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010072 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020073#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
74#define HA_ATOMIC_UPDATE_MAX(val, new) \
75 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020076 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020077 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020078 if (*(val) < __new_max) \
79 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020080 *(val); \
81 })
82
83#define HA_ATOMIC_UPDATE_MIN(val, new) \
84 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020085 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020086 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020087 if (*(val) > __new_min) \
88 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020089 *(val); \
90 })
91
Willy Tarreaub29dc952017-10-31 18:00:20 +010092#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020093
Willy Tarreau0ccd3222018-07-30 10:34:35 +020094#define THREAD_SYNC_INIT() do { /* do nothing */ } while(0)
Christopher Faulet339fff82017-10-19 11:59:15 +020095#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
96#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
97#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
98#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
99#define THREAD_NO_SYNC() ({ 0; })
100#define THREAD_NEED_SYNC() ({ 1; })
101
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100102#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
103#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
104#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
105#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
106#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200107
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100108#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
109#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
110#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
111#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
112#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
113#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
114#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
115#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116
William Lallemand6e1796e2018-06-07 11:23:40 +0200117#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
118
119
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100120static inline void __ha_barrier_load(void)
121{
122}
123
124static inline void __ha_barrier_store(void)
125{
126}
127
128static inline void __ha_barrier_full(void)
129{
130}
131
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200132#else /* USE_THREAD */
133
134#include <stdio.h>
135#include <stdlib.h>
136#include <string.h>
137#include <pthread.h>
138#include <import/plock.h>
139
Willy Tarreau421f02e2018-01-20 18:19:22 +0100140#define MAX_THREADS LONGBITS
141
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100142#define __decl_hathreads(decl) decl
143
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200144/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
145 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100146
David Carlierec5e8452018-01-11 14:20:43 +0000147#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100148/* gcc < 4.7 */
149
150#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
151#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
152#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
153#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
154
155/* the CAS is a bit complicated. The older API doesn't support returning the
156 * value and the swap's result at the same time. So here we take what looks
157 * like the safest route, consisting in using the boolean version guaranteeing
158 * that the operation was performed or not, and we snoop a previous value. If
159 * the compare succeeds, we return. If it fails, we return the previous value,
160 * but only if it differs from the expected one. If it's the same it's a race
161 * thus we try again to avoid confusing a possibly sensitive caller.
162 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200163#define HA_ATOMIC_CAS(val, old, new) \
164 ({ \
165 typeof((val)) __val_cas = (val); \
166 typeof((old)) __oldp_cas = (old); \
167 typeof(*(old)) __oldv_cas; \
168 typeof((new)) __new_cas = (new); \
169 int __ret_cas; \
170 do { \
171 __oldv_cas = *__val_cas; \
172 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
173 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
174 if (!__ret_cas) \
175 *__oldp_cas = __oldv_cas; \
176 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100177 })
178
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200179#define HA_ATOMIC_XCHG(val, new) \
180 ({ \
181 typeof((val)) __val_xchg = (val); \
182 typeof(*(val)) __old_xchg; \
183 typeof((new)) __new_xchg = (new); \
184 do { __old_xchg = *__val_xchg; \
185 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
186 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100187 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100188
189#define HA_ATOMIC_BTS(val, bit) \
190 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200191 typeof(*(val)) __b_bts = (1UL << (bit)); \
192 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100193 })
194
195#define HA_ATOMIC_BTR(val, bit) \
196 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200197 typeof(*(val)) __b_btr = (1UL << (bit)); \
198 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100199 })
200
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200201#define HA_ATOMIC_STORE(val, new) \
202 ({ \
203 typeof((val)) __val_store = (val); \
204 typeof(*(val)) __old_store; \
205 typeof((new)) __new_store = (new); \
206 do { __old_store = *__val_store; \
207 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100208 })
209#else
210/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200211#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
212#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
213#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
214#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
215#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100216#define HA_ATOMIC_BTS(val, bit) \
217 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200218 typeof(*(val)) __b_bts = (1UL << (bit)); \
219 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100220 })
221
222#define HA_ATOMIC_BTR(val, bit) \
223 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200224 typeof(*(val)) __b_btr = (1UL << (bit)); \
225 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100226 })
227
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200228#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
229#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100230#endif
231
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200232#define HA_ATOMIC_UPDATE_MAX(val, new) \
233 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200234 typeof(*(val)) __old_max = *(val); \
235 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200236 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200237 while (__old_max < __new_max && \
238 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
239 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200240 })
241#define HA_ATOMIC_UPDATE_MIN(val, new) \
242 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200243 typeof(*(val)) __old_min = *(val); \
244 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200245 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200246 while (__old_min > __new_min && \
247 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
248 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200249 })
250
Willy Tarreaub29dc952017-10-31 18:00:20 +0100251#define HA_BARRIER() pl_barrier()
252
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200253#define THREAD_SYNC_INIT() thread_sync_init()
Christopher Faulet339fff82017-10-19 11:59:15 +0200254#define THREAD_SYNC_ENABLE() thread_sync_enable()
255#define THREAD_WANT_SYNC() thread_want_sync()
256#define THREAD_ENTER_SYNC() thread_enter_sync()
257#define THREAD_EXIT_SYNC() thread_exit_sync()
258#define THREAD_NO_SYNC() thread_no_sync()
259#define THREAD_NEED_SYNC() thread_need_sync()
260
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200261int thread_sync_init();
Christopher Faulet339fff82017-10-19 11:59:15 +0200262void thread_sync_enable(void);
263void thread_want_sync(void);
264void thread_enter_sync(void);
265void thread_exit_sync(void);
266int thread_no_sync(void);
267int thread_need_sync(void);
268
Christopher Fauletddb6c162018-07-20 09:31:53 +0200269extern volatile unsigned long all_threads_mask;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200270
William Lallemand6e1796e2018-06-07 11:23:40 +0200271#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
272
273
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200274#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
275
Christopher Fauletf51bac22018-01-30 11:04:29 +0100276/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200277enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200278 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200279 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200280 TASK_RQ_LOCK,
281 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200282 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200283 LISTENER_LOCK,
284 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200285 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200286 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200287 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200288 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200289 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200290 STK_TABLE_LOCK,
291 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200292 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200293 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200294 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200295 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200296 SSL_LOCK,
297 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200298 PATREF_LOCK,
299 PATEXP_LOCK,
300 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200301 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200302 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200303 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200304 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200305 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200306 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200307 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200308 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100309 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100310 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100311 TLSKEYS_REF_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200312 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200313};
314struct lock_stat {
315 uint64_t nsec_wait_for_write;
316 uint64_t nsec_wait_for_read;
317 uint64_t num_write_locked;
318 uint64_t num_write_unlocked;
319 uint64_t num_read_locked;
320 uint64_t num_read_unlocked;
321};
322
323extern struct lock_stat lock_stats[LOCK_LABELS];
324
325#define __HA_SPINLOCK_T unsigned long
326
327#define __SPIN_INIT(l) ({ (*l) = 0; })
328#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100329#define __SPIN_LOCK(l) pl_take_s(l)
330#define __SPIN_TRYLOCK(l) !pl_try_s(l)
331#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200332
333#define __HA_RWLOCK_T unsigned long
334
335#define __RWLOCK_INIT(l) ({ (*l) = 0; })
336#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
337#define __RWLOCK_WRLOCK(l) pl_take_w(l)
338#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
339#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
340#define __RWLOCK_RDLOCK(l) pl_take_r(l)
341#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
342#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
343
344#define HA_SPINLOCK_T struct ha_spinlock
345
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346#define HA_SPIN_INIT(l) __spin_init(l)
347#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200348
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
350#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
351#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200352
353#define HA_RWLOCK_T struct ha_rwlock
354
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100355#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
356#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
357#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
358#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
359#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
360#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
361#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
362#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200363
364struct ha_spinlock {
365 __HA_SPINLOCK_T lock;
366 struct {
367 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
368 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
369 struct {
370 const char *function;
371 const char *file;
372 int line;
373 } last_location; /* location of the last owner */
374 } info;
375};
376
377struct ha_rwlock {
378 __HA_RWLOCK_T lock;
379 struct {
380 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
381 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
382 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
383 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
384 struct {
385 const char *function;
386 const char *file;
387 int line;
388 } last_location; /* location of the last write owner */
389 } info;
390};
391
Christopher Fauletf51bac22018-01-30 11:04:29 +0100392static inline const char *lock_label(enum lock_label label)
393{
394 switch (label) {
395 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100396 case FD_LOCK: return "FD";
397 case TASK_RQ_LOCK: return "TASK_RQ";
398 case TASK_WQ_LOCK: return "TASK_WQ";
399 case POOL_LOCK: return "POOL";
400 case LISTENER_LOCK: return "LISTENER";
401 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
402 case PROXY_LOCK: return "PROXY";
403 case SERVER_LOCK: return "SERVER";
404 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
405 case LBPRM_LOCK: return "LBPRM";
406 case SIGNALS_LOCK: return "SIGNALS";
407 case STK_TABLE_LOCK: return "STK_TABLE";
408 case STK_SESS_LOCK: return "STK_SESS";
409 case APPLETS_LOCK: return "APPLETS";
410 case PEER_LOCK: return "PEER";
411 case BUF_WQ_LOCK: return "BUF_WQ";
412 case STRMS_LOCK: return "STRMS";
413 case SSL_LOCK: return "SSL";
414 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
415 case PATREF_LOCK: return "PATREF";
416 case PATEXP_LOCK: return "PATEXP";
417 case PATLRU_LOCK: return "PATLRU";
418 case VARS_LOCK: return "VARS";
419 case COMP_POOL_LOCK: return "COMP_POOL";
420 case LUA_LOCK: return "LUA";
421 case NOTIF_LOCK: return "NOTIF";
422 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
423 case DNS_LOCK: return "DNS";
424 case PID_LIST_LOCK: return "PID_LIST";
425 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
426 case PIPES_LOCK: return "PIPES";
427 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100428 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100429 case LOCK_LABELS: break; /* keep compiler happy */
430 };
431 /* only way to come here is consecutive to an internal bug */
432 abort();
433}
434
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200435static inline void show_lock_stats()
436{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200437 int lbl;
438
439 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
440 fprintf(stderr,
441 "Stats about Lock %s: \n"
442 "\t # write lock : %lu\n"
443 "\t # write unlock: %lu (%ld)\n"
444 "\t # wait time for write : %.3f msec\n"
445 "\t # wait time for write/lock: %.3f nsec\n"
446 "\t # read lock : %lu\n"
447 "\t # read unlock : %lu (%ld)\n"
448 "\t # wait time for read : %.3f msec\n"
449 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100450 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200451 lock_stats[lbl].num_write_locked,
452 lock_stats[lbl].num_write_unlocked,
453 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
454 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
455 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
456 lock_stats[lbl].num_read_locked,
457 lock_stats[lbl].num_read_unlocked,
458 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
459 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
460 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
461 }
462}
463
464/* Following functions are used to collect some stats about locks. We wrap
465 * pthread functions to known how much time we wait in a lock. */
466
467static uint64_t nsec_now(void) {
468 struct timespec ts;
469
470 clock_gettime(CLOCK_MONOTONIC, &ts);
471 return ((uint64_t) ts.tv_sec * 1000000000ULL +
472 (uint64_t) ts.tv_nsec);
473}
474
475static inline void __ha_rwlock_init(struct ha_rwlock *l)
476{
477 memset(l, 0, sizeof(struct ha_rwlock));
478 __RWLOCK_INIT(&l->lock);
479}
480
481static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
482{
483 __RWLOCK_DESTROY(&l->lock);
484 memset(l, 0, sizeof(struct ha_rwlock));
485}
486
487
488static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
489 const char *func, const char *file, int line)
490{
491 uint64_t start_time;
492
493 if (unlikely(l->info.cur_writer & tid_bit)) {
494 /* the thread is already owning the lock for write */
495 abort();
496 }
497
498 if (unlikely(l->info.cur_readers & tid_bit)) {
499 /* the thread is already owning the lock for read */
500 abort();
501 }
502
503 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
504
505 start_time = nsec_now();
506 __RWLOCK_WRLOCK(&l->lock);
507 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
508
509 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
510
511 l->info.cur_writer = tid_bit;
512 l->info.last_location.function = func;
513 l->info.last_location.file = file;
514 l->info.last_location.line = line;
515
516 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
517}
518
519static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
520 const char *func, const char *file, int line)
521{
522 uint64_t start_time;
523 int r;
524
525 if (unlikely(l->info.cur_writer & tid_bit)) {
526 /* the thread is already owning the lock for write */
527 abort();
528 }
529
530 if (unlikely(l->info.cur_readers & tid_bit)) {
531 /* the thread is already owning the lock for read */
532 abort();
533 }
534
535 /* We set waiting writer because trywrlock could wait for readers to quit */
536 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
537
538 start_time = nsec_now();
539 r = __RWLOCK_TRYWRLOCK(&l->lock);
540 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
541 if (unlikely(r)) {
542 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
543 return r;
544 }
545 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
546
547 l->info.cur_writer = tid_bit;
548 l->info.last_location.function = func;
549 l->info.last_location.file = file;
550 l->info.last_location.line = line;
551
552 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
553
554 return 0;
555}
556
557static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
558 const char *func, const char *file, int line)
559{
560 if (unlikely(!(l->info.cur_writer & tid_bit))) {
561 /* the thread is not owning the lock for write */
562 abort();
563 }
564
565 l->info.cur_writer = 0;
566 l->info.last_location.function = func;
567 l->info.last_location.file = file;
568 l->info.last_location.line = line;
569
570 __RWLOCK_WRUNLOCK(&l->lock);
571
572 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
573}
574
575static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
576{
577 uint64_t start_time;
578
579 if (unlikely(l->info.cur_writer & tid_bit)) {
580 /* the thread is already owning the lock for write */
581 abort();
582 }
583
584 if (unlikely(l->info.cur_readers & tid_bit)) {
585 /* the thread is already owning the lock for read */
586 abort();
587 }
588
589 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
590
591 start_time = nsec_now();
592 __RWLOCK_RDLOCK(&l->lock);
593 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
594 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
595
596 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
597
598 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
599}
600
601static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
602{
603 int r;
604
605 if (unlikely(l->info.cur_writer & tid_bit)) {
606 /* the thread is already owning the lock for write */
607 abort();
608 }
609
610 if (unlikely(l->info.cur_readers & tid_bit)) {
611 /* the thread is already owning the lock for read */
612 abort();
613 }
614
615 /* try read should never wait */
616 r = __RWLOCK_TRYRDLOCK(&l->lock);
617 if (unlikely(r))
618 return r;
619 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
620
621 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
622
623 return 0;
624}
625
626static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
627{
628 if (unlikely(!(l->info.cur_readers & tid_bit))) {
629 /* the thread is not owning the lock for read */
630 abort();
631 }
632
633 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
634
635 __RWLOCK_RDUNLOCK(&l->lock);
636
637 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
638}
639
640static inline void __spin_init(struct ha_spinlock *l)
641{
642 memset(l, 0, sizeof(struct ha_spinlock));
643 __SPIN_INIT(&l->lock);
644}
645
646static inline void __spin_destroy(struct ha_spinlock *l)
647{
648 __SPIN_DESTROY(&l->lock);
649 memset(l, 0, sizeof(struct ha_spinlock));
650}
651
652static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
653 const char *func, const char *file, int line)
654{
655 uint64_t start_time;
656
657 if (unlikely(l->info.owner & tid_bit)) {
658 /* the thread is already owning the lock */
659 abort();
660 }
661
662 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
663
664 start_time = nsec_now();
665 __SPIN_LOCK(&l->lock);
666 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
667
668 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
669
670
671 l->info.owner = tid_bit;
672 l->info.last_location.function = func;
673 l->info.last_location.file = file;
674 l->info.last_location.line = line;
675
676 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
677}
678
679static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
680 const char *func, const char *file, int line)
681{
682 int r;
683
684 if (unlikely(l->info.owner & tid_bit)) {
685 /* the thread is already owning the lock */
686 abort();
687 }
688
689 /* try read should never wait */
690 r = __SPIN_TRYLOCK(&l->lock);
691 if (unlikely(r))
692 return r;
693 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
694
695 l->info.owner = tid_bit;
696 l->info.last_location.function = func;
697 l->info.last_location.file = file;
698 l->info.last_location.line = line;
699
700 return 0;
701}
702
703static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
704 const char *func, const char *file, int line)
705{
706 if (unlikely(!(l->info.owner & tid_bit))) {
707 /* the thread is not owning the lock */
708 abort();
709 }
710
711 l->info.owner = 0;
712 l->info.last_location.function = func;
713 l->info.last_location.file = file;
714 l->info.last_location.line = line;
715
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100716 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200717 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
718}
719
720#else /* DEBUG_THREAD */
721
722#define HA_SPINLOCK_T unsigned long
723
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100724#define HA_SPIN_INIT(l) ({ (*l) = 0; })
725#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
726#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
727#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
728#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200729
730#define HA_RWLOCK_T unsigned long
731
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100732#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
733#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
734#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
735#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
736#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
737#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
738#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
739#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200740
741#endif /* DEBUG_THREAD */
742
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100743#ifdef __x86_64__
744#define HA_HAVE_CAS_DW 1
745#define HA_CAS_IS_8B
746static __inline int
747__ha_cas_dw(void *target, void *compare, const void *set)
748{
749 char ret;
750
751 __asm __volatile("lock cmpxchg16b %0; setz %3"
752 : "+m" (*(void **)target),
753 "=a" (((void **)compare)[0]),
754 "=d" (((void **)compare)[1]),
755 "=q" (ret)
756 : "a" (((void **)compare)[0]),
757 "d" (((void **)compare)[1]),
758 "b" (((const void **)set)[0]),
759 "c" (((const void **)set)[1])
760 : "memory", "cc");
761 return (ret);
762}
763
764static __inline void
765__ha_barrier_load(void)
766{
767 __asm __volatile("lfence" ::: "memory");
768}
769
770static __inline void
771__ha_barrier_store(void)
772{
773 __asm __volatile("sfence" ::: "memory");
774}
775
776static __inline void
777__ha_barrier_full(void)
778{
779 __asm __volatile("mfence" ::: "memory");
780}
781
782#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
783#define HA_HAVE_CAS_DW 1
784static __inline void
785__ha_barrier_load(void)
786{
787 __asm __volatile("dmb" ::: "memory");
788}
789
790static __inline void
791__ha_barrier_store(void)
792{
793 __asm __volatile("dsb" ::: "memory");
794}
795
796static __inline void
797__ha_barrier_full(void)
798{
799 __asm __volatile("dmb" ::: "memory");
800}
801
Willy Tarreau41ccb192018-02-14 14:16:28 +0100802static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100803{
804 uint64_t previous;
805 int tmp;
806
807 __asm __volatile("1:"
808 "ldrexd %0, [%4];"
809 "cmp %Q0, %Q2;"
810 "ittt eq;"
811 "cmpeq %R0, %R2;"
812 "strexdeq %1, %3, [%4];"
813 "cmpeq %1, #1;"
814 "beq 1b;"
815 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100816 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100817 : "memory", "cc");
818 tmp = (previous == *(uint64_t *)compare);
819 *(uint64_t *)compare = previous;
820 return (tmp);
821}
822
823#elif defined (__aarch64__)
824#define HA_HAVE_CAS_DW 1
825#define HA_CAS_IS_8B
826
827static __inline void
828__ha_barrier_load(void)
829{
830 __asm __volatile("dmb ishld" ::: "memory");
831}
832
833static __inline void
834__ha_barrier_store(void)
835{
836 __asm __volatile("dmb ishst" ::: "memory");
837}
838
839static __inline void
840__ha_barrier_full(void)
841{
842 __asm __volatile("dmb ish" ::: "memory");
843}
844
845static __inline int __ha_cas_dw(void *target, void *compare, void *set)
846{
847 void *value[2];
848 uint64_t tmp1, tmp2;
849
850 __asm__ __volatile__("1:"
851 "ldxp %0, %1, [%4];"
852 "mov %2, %0;"
853 "mov %3, %1;"
854 "eor %0, %0, %5;"
855 "eor %1, %1, %6;"
856 "orr %1, %0, %1;"
857 "mov %w0, #0;"
858 "cbnz %1, 2f;"
859 "stxp %w0, %7, %8, [%4];"
860 "cbnz %w0, 1b;"
861 "mov %w0, #1;"
862 "2:"
863 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
864 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
865 : "cc", "memory");
866
867 memcpy(compare, &value, sizeof(value));
868 return (tmp1);
869}
870
871#else
872#define __ha_barrier_load __sync_synchronize
873#define __ha_barrier_store __sync_synchronize
874#define __ha_barrier_full __sync_synchronize
875#endif
876
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200877#endif /* USE_THREAD */
878
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100879static inline void __ha_compiler_barrier(void)
880{
881 __asm __volatile("" ::: "memory");
882}
883
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200884/* Dummy I/O handler used by the sync pipe.*/
885void thread_sync_io_handler(int fd);
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200886int parse_nbthread(const char *arg, char **err);
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200887
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200888#endif /* _COMMON_HATHREADS_H */