blob: 4cf3db9bee839672d9c40eab7e8ead81cbe771e3 [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
Willy Tarreau0ccd3222018-07-30 10:34:35 +020027/* Note about all_threads_mask :
28 * - with threads support disabled, this symbol is defined as zero (0UL).
29 * - with threads enabled, this variable is never zero, it contains the mask
30 * of enabled threads. Thus if only one thread is enabled, it equals 1.
31 */
32
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020033#ifndef USE_THREAD
34
Willy Tarreau421f02e2018-01-20 18:19:22 +010035#define MAX_THREADS 1
Willy Tarreau0c026f42018-08-01 19:12:20 +020036#define MAX_THREADS_MASK 1
37
38/* Only way found to replace variables with constants that are optimized away
39 * at build time.
40 */
41enum { all_threads_mask = 1UL };
42enum { tid_bit = 1UL };
43enum { tid = 0 };
Willy Tarreau421f02e2018-01-20 18:19:22 +010044
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010045#define __decl_hathreads(decl)
46
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020047#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
48#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
49#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
50#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
51#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
52#define HA_ATOMIC_XCHG(val, new) \
53 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020054 typeof(*(val)) __old_xchg = *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020055 *(val) = new; \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020056 __old_xchg; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020057 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010058#define HA_ATOMIC_BTS(val, bit) \
59 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020060 typeof((val)) __p_bts = (val); \
61 typeof(*__p_bts) __b_bts = (1UL << (bit)); \
62 typeof(*__p_bts) __t_bts = *__p_bts & __b_bts; \
63 if (!__t_bts) \
64 *__p_bts |= __b_bts; \
65 __t_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010066 })
67#define HA_ATOMIC_BTR(val, bit) \
68 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020069 typeof((val)) __p_btr = (val); \
70 typeof(*__p_btr) __b_btr = (1UL << (bit)); \
71 typeof(*__p_btr) __t_btr = *__p_btr & __b_btr; \
72 if (__t_btr) \
73 *__p_btr &= ~__b_btr; \
74 __t_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +010075 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020076#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
77#define HA_ATOMIC_UPDATE_MAX(val, new) \
78 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020079 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020080 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020081 if (*(val) < __new_max) \
82 *(val) = __new_max; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020083 *(val); \
84 })
85
86#define HA_ATOMIC_UPDATE_MIN(val, new) \
87 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020088 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020089 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +020090 if (*(val) > __new_min) \
91 *(val) = __new_min; \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020092 *(val); \
93 })
94
Willy Tarreaub29dc952017-10-31 18:00:20 +010095#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020096
Willy Tarreau0ccd3222018-07-30 10:34:35 +020097#define THREAD_SYNC_INIT() do { /* do nothing */ } while(0)
Christopher Faulet339fff82017-10-19 11:59:15 +020098#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
99#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
100#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
101#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
102#define THREAD_NO_SYNC() ({ 0; })
103#define THREAD_NEED_SYNC() ({ 1; })
104
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100105#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
106#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
107#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
108#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
109#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200110
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100111#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
112#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
113#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
114#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
115#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
116#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
117#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
118#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200119
William Lallemand6e1796e2018-06-07 11:23:40 +0200120#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
121
Willy Tarreau0c026f42018-08-01 19:12:20 +0200122static inline void ha_set_tid(unsigned int tid)
123{
124}
William Lallemand6e1796e2018-06-07 11:23:40 +0200125
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100126static inline void __ha_barrier_load(void)
127{
128}
129
130static inline void __ha_barrier_store(void)
131{
132}
133
134static inline void __ha_barrier_full(void)
135{
136}
137
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200138#else /* USE_THREAD */
139
140#include <stdio.h>
141#include <stdlib.h>
142#include <string.h>
143#include <pthread.h>
144#include <import/plock.h>
145
Willy Tarreau421f02e2018-01-20 18:19:22 +0100146#define MAX_THREADS LONGBITS
Willy Tarreau0c026f42018-08-01 19:12:20 +0200147#define MAX_THREADS_MASK ((unsigned long)-1)
Willy Tarreau421f02e2018-01-20 18:19:22 +0100148
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100149#define __decl_hathreads(decl) decl
150
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200151/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
152 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100153
David Carlierec5e8452018-01-11 14:20:43 +0000154#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100155/* gcc < 4.7 */
156
157#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
158#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
159#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
160#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
161
162/* the CAS is a bit complicated. The older API doesn't support returning the
163 * value and the swap's result at the same time. So here we take what looks
164 * like the safest route, consisting in using the boolean version guaranteeing
165 * that the operation was performed or not, and we snoop a previous value. If
166 * the compare succeeds, we return. If it fails, we return the previous value,
167 * but only if it differs from the expected one. If it's the same it's a race
168 * thus we try again to avoid confusing a possibly sensitive caller.
169 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200170#define HA_ATOMIC_CAS(val, old, new) \
171 ({ \
172 typeof((val)) __val_cas = (val); \
173 typeof((old)) __oldp_cas = (old); \
174 typeof(*(old)) __oldv_cas; \
175 typeof((new)) __new_cas = (new); \
176 int __ret_cas; \
177 do { \
178 __oldv_cas = *__val_cas; \
179 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
180 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
181 if (!__ret_cas) \
182 *__oldp_cas = __oldv_cas; \
183 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100184 })
185
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200186#define HA_ATOMIC_XCHG(val, new) \
187 ({ \
188 typeof((val)) __val_xchg = (val); \
189 typeof(*(val)) __old_xchg; \
190 typeof((new)) __new_xchg = (new); \
191 do { __old_xchg = *__val_xchg; \
192 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
193 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100194 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100195
196#define HA_ATOMIC_BTS(val, bit) \
197 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200198 typeof(*(val)) __b_bts = (1UL << (bit)); \
199 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100200 })
201
202#define HA_ATOMIC_BTR(val, bit) \
203 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200204 typeof(*(val)) __b_btr = (1UL << (bit)); \
205 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100206 })
207
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200208#define HA_ATOMIC_STORE(val, new) \
209 ({ \
210 typeof((val)) __val_store = (val); \
211 typeof(*(val)) __old_store; \
212 typeof((new)) __new_store = (new); \
213 do { __old_store = *__val_store; \
214 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100215 })
216#else
217/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200218#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
219#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
220#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
221#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
222#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100223#define HA_ATOMIC_BTS(val, bit) \
224 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200225 typeof(*(val)) __b_bts = (1UL << (bit)); \
226 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100227 })
228
229#define HA_ATOMIC_BTR(val, bit) \
230 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200231 typeof(*(val)) __b_btr = (1UL << (bit)); \
232 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100233 })
234
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200235#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
236#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100237#endif
238
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200239#define HA_ATOMIC_UPDATE_MAX(val, new) \
240 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200241 typeof(*(val)) __old_max = *(val); \
242 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200243 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200244 while (__old_max < __new_max && \
245 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
246 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200247 })
248#define HA_ATOMIC_UPDATE_MIN(val, new) \
249 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200250 typeof(*(val)) __old_min = *(val); \
251 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200252 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200253 while (__old_min > __new_min && \
254 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
255 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200256 })
257
Willy Tarreaub29dc952017-10-31 18:00:20 +0100258#define HA_BARRIER() pl_barrier()
259
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200260#define THREAD_SYNC_INIT() thread_sync_init()
Christopher Faulet339fff82017-10-19 11:59:15 +0200261#define THREAD_SYNC_ENABLE() thread_sync_enable()
262#define THREAD_WANT_SYNC() thread_want_sync()
263#define THREAD_ENTER_SYNC() thread_enter_sync()
264#define THREAD_EXIT_SYNC() thread_exit_sync()
265#define THREAD_NO_SYNC() thread_no_sync()
266#define THREAD_NEED_SYNC() thread_need_sync()
267
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200268int thread_sync_init();
Christopher Faulet339fff82017-10-19 11:59:15 +0200269void thread_sync_enable(void);
270void thread_want_sync(void);
271void thread_enter_sync(void);
272void thread_exit_sync(void);
273int thread_no_sync(void);
274int thread_need_sync(void);
275
Willy Tarreau0c026f42018-08-01 19:12:20 +0200276extern THREAD_LOCAL unsigned int tid; /* The thread id */
277extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200278extern volatile unsigned long all_threads_mask;
Olivier Houchard6b96f722018-04-25 16:58:25 +0200279
William Lallemand6e1796e2018-06-07 11:23:40 +0200280#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
281
Willy Tarreau0c026f42018-08-01 19:12:20 +0200282/* sets the thread ID and the TID bit for the current thread */
283static inline void ha_set_tid(unsigned int data)
284{
285 tid = data;
286 tid_bit = (1UL << tid);
287}
288
William Lallemand6e1796e2018-06-07 11:23:40 +0200289
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200290#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
291
Christopher Fauletf51bac22018-01-30 11:04:29 +0100292/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200293enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200294 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200295 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200296 TASK_RQ_LOCK,
297 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200298 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200299 LISTENER_LOCK,
300 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200301 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200302 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200303 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200304 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200305 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200306 STK_TABLE_LOCK,
307 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200308 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200309 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200310 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200311 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200312 SSL_LOCK,
313 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200314 PATREF_LOCK,
315 PATEXP_LOCK,
316 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200317 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200318 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200319 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200320 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200321 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200322 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200323 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200324 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100325 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100326 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100327 TLSKEYS_REF_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200328 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200329};
330struct lock_stat {
331 uint64_t nsec_wait_for_write;
332 uint64_t nsec_wait_for_read;
333 uint64_t num_write_locked;
334 uint64_t num_write_unlocked;
335 uint64_t num_read_locked;
336 uint64_t num_read_unlocked;
337};
338
339extern struct lock_stat lock_stats[LOCK_LABELS];
340
341#define __HA_SPINLOCK_T unsigned long
342
343#define __SPIN_INIT(l) ({ (*l) = 0; })
344#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100345#define __SPIN_LOCK(l) pl_take_s(l)
346#define __SPIN_TRYLOCK(l) !pl_try_s(l)
347#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200348
349#define __HA_RWLOCK_T unsigned long
350
351#define __RWLOCK_INIT(l) ({ (*l) = 0; })
352#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
353#define __RWLOCK_WRLOCK(l) pl_take_w(l)
354#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
355#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
356#define __RWLOCK_RDLOCK(l) pl_take_r(l)
357#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
358#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
359
360#define HA_SPINLOCK_T struct ha_spinlock
361
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362#define HA_SPIN_INIT(l) __spin_init(l)
363#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200364
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100365#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
366#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
367#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200368
369#define HA_RWLOCK_T struct ha_rwlock
370
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100371#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
372#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
373#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
374#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
375#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
376#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
377#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
378#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200379
380struct ha_spinlock {
381 __HA_SPINLOCK_T lock;
382 struct {
383 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
384 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
385 struct {
386 const char *function;
387 const char *file;
388 int line;
389 } last_location; /* location of the last owner */
390 } info;
391};
392
393struct ha_rwlock {
394 __HA_RWLOCK_T lock;
395 struct {
396 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
397 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
398 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
399 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
400 struct {
401 const char *function;
402 const char *file;
403 int line;
404 } last_location; /* location of the last write owner */
405 } info;
406};
407
Christopher Fauletf51bac22018-01-30 11:04:29 +0100408static inline const char *lock_label(enum lock_label label)
409{
410 switch (label) {
411 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100412 case FD_LOCK: return "FD";
413 case TASK_RQ_LOCK: return "TASK_RQ";
414 case TASK_WQ_LOCK: return "TASK_WQ";
415 case POOL_LOCK: return "POOL";
416 case LISTENER_LOCK: return "LISTENER";
417 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
418 case PROXY_LOCK: return "PROXY";
419 case SERVER_LOCK: return "SERVER";
420 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
421 case LBPRM_LOCK: return "LBPRM";
422 case SIGNALS_LOCK: return "SIGNALS";
423 case STK_TABLE_LOCK: return "STK_TABLE";
424 case STK_SESS_LOCK: return "STK_SESS";
425 case APPLETS_LOCK: return "APPLETS";
426 case PEER_LOCK: return "PEER";
427 case BUF_WQ_LOCK: return "BUF_WQ";
428 case STRMS_LOCK: return "STRMS";
429 case SSL_LOCK: return "SSL";
430 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
431 case PATREF_LOCK: return "PATREF";
432 case PATEXP_LOCK: return "PATEXP";
433 case PATLRU_LOCK: return "PATLRU";
434 case VARS_LOCK: return "VARS";
435 case COMP_POOL_LOCK: return "COMP_POOL";
436 case LUA_LOCK: return "LUA";
437 case NOTIF_LOCK: return "NOTIF";
438 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
439 case DNS_LOCK: return "DNS";
440 case PID_LIST_LOCK: return "PID_LIST";
441 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
442 case PIPES_LOCK: return "PIPES";
443 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100444 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100445 case LOCK_LABELS: break; /* keep compiler happy */
446 };
447 /* only way to come here is consecutive to an internal bug */
448 abort();
449}
450
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200451static inline void show_lock_stats()
452{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200453 int lbl;
454
455 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
456 fprintf(stderr,
457 "Stats about Lock %s: \n"
458 "\t # write lock : %lu\n"
459 "\t # write unlock: %lu (%ld)\n"
460 "\t # wait time for write : %.3f msec\n"
461 "\t # wait time for write/lock: %.3f nsec\n"
462 "\t # read lock : %lu\n"
463 "\t # read unlock : %lu (%ld)\n"
464 "\t # wait time for read : %.3f msec\n"
465 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100466 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200467 lock_stats[lbl].num_write_locked,
468 lock_stats[lbl].num_write_unlocked,
469 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
470 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
471 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
472 lock_stats[lbl].num_read_locked,
473 lock_stats[lbl].num_read_unlocked,
474 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
475 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
476 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
477 }
478}
479
480/* Following functions are used to collect some stats about locks. We wrap
481 * pthread functions to known how much time we wait in a lock. */
482
483static uint64_t nsec_now(void) {
484 struct timespec ts;
485
486 clock_gettime(CLOCK_MONOTONIC, &ts);
487 return ((uint64_t) ts.tv_sec * 1000000000ULL +
488 (uint64_t) ts.tv_nsec);
489}
490
491static inline void __ha_rwlock_init(struct ha_rwlock *l)
492{
493 memset(l, 0, sizeof(struct ha_rwlock));
494 __RWLOCK_INIT(&l->lock);
495}
496
497static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
498{
499 __RWLOCK_DESTROY(&l->lock);
500 memset(l, 0, sizeof(struct ha_rwlock));
501}
502
503
504static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
505 const char *func, const char *file, int line)
506{
507 uint64_t start_time;
508
509 if (unlikely(l->info.cur_writer & tid_bit)) {
510 /* the thread is already owning the lock for write */
511 abort();
512 }
513
514 if (unlikely(l->info.cur_readers & tid_bit)) {
515 /* the thread is already owning the lock for read */
516 abort();
517 }
518
519 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
520
521 start_time = nsec_now();
522 __RWLOCK_WRLOCK(&l->lock);
523 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
524
525 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
526
527 l->info.cur_writer = tid_bit;
528 l->info.last_location.function = func;
529 l->info.last_location.file = file;
530 l->info.last_location.line = line;
531
532 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
533}
534
535static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
536 const char *func, const char *file, int line)
537{
538 uint64_t start_time;
539 int r;
540
541 if (unlikely(l->info.cur_writer & tid_bit)) {
542 /* the thread is already owning the lock for write */
543 abort();
544 }
545
546 if (unlikely(l->info.cur_readers & tid_bit)) {
547 /* the thread is already owning the lock for read */
548 abort();
549 }
550
551 /* We set waiting writer because trywrlock could wait for readers to quit */
552 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
553
554 start_time = nsec_now();
555 r = __RWLOCK_TRYWRLOCK(&l->lock);
556 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
557 if (unlikely(r)) {
558 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
559 return r;
560 }
561 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
562
563 l->info.cur_writer = tid_bit;
564 l->info.last_location.function = func;
565 l->info.last_location.file = file;
566 l->info.last_location.line = line;
567
568 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
569
570 return 0;
571}
572
573static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
574 const char *func, const char *file, int line)
575{
576 if (unlikely(!(l->info.cur_writer & tid_bit))) {
577 /* the thread is not owning the lock for write */
578 abort();
579 }
580
581 l->info.cur_writer = 0;
582 l->info.last_location.function = func;
583 l->info.last_location.file = file;
584 l->info.last_location.line = line;
585
586 __RWLOCK_WRUNLOCK(&l->lock);
587
588 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
589}
590
591static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
592{
593 uint64_t start_time;
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 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
606
607 start_time = nsec_now();
608 __RWLOCK_RDLOCK(&l->lock);
609 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
610 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
611
612 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
613
614 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
615}
616
617static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
618{
619 int r;
620
621 if (unlikely(l->info.cur_writer & tid_bit)) {
622 /* the thread is already owning the lock for write */
623 abort();
624 }
625
626 if (unlikely(l->info.cur_readers & tid_bit)) {
627 /* the thread is already owning the lock for read */
628 abort();
629 }
630
631 /* try read should never wait */
632 r = __RWLOCK_TRYRDLOCK(&l->lock);
633 if (unlikely(r))
634 return r;
635 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
636
637 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
638
639 return 0;
640}
641
642static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
643{
644 if (unlikely(!(l->info.cur_readers & tid_bit))) {
645 /* the thread is not owning the lock for read */
646 abort();
647 }
648
649 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
650
651 __RWLOCK_RDUNLOCK(&l->lock);
652
653 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
654}
655
656static inline void __spin_init(struct ha_spinlock *l)
657{
658 memset(l, 0, sizeof(struct ha_spinlock));
659 __SPIN_INIT(&l->lock);
660}
661
662static inline void __spin_destroy(struct ha_spinlock *l)
663{
664 __SPIN_DESTROY(&l->lock);
665 memset(l, 0, sizeof(struct ha_spinlock));
666}
667
668static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
669 const char *func, const char *file, int line)
670{
671 uint64_t start_time;
672
673 if (unlikely(l->info.owner & tid_bit)) {
674 /* the thread is already owning the lock */
675 abort();
676 }
677
678 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
679
680 start_time = nsec_now();
681 __SPIN_LOCK(&l->lock);
682 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
683
684 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
685
686
687 l->info.owner = tid_bit;
688 l->info.last_location.function = func;
689 l->info.last_location.file = file;
690 l->info.last_location.line = line;
691
692 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
693}
694
695static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
696 const char *func, const char *file, int line)
697{
698 int r;
699
700 if (unlikely(l->info.owner & tid_bit)) {
701 /* the thread is already owning the lock */
702 abort();
703 }
704
705 /* try read should never wait */
706 r = __SPIN_TRYLOCK(&l->lock);
707 if (unlikely(r))
708 return r;
709 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
710
711 l->info.owner = tid_bit;
712 l->info.last_location.function = func;
713 l->info.last_location.file = file;
714 l->info.last_location.line = line;
715
716 return 0;
717}
718
719static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
720 const char *func, const char *file, int line)
721{
722 if (unlikely(!(l->info.owner & tid_bit))) {
723 /* the thread is not owning the lock */
724 abort();
725 }
726
727 l->info.owner = 0;
728 l->info.last_location.function = func;
729 l->info.last_location.file = file;
730 l->info.last_location.line = line;
731
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100732 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200733 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
734}
735
736#else /* DEBUG_THREAD */
737
738#define HA_SPINLOCK_T unsigned long
739
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100740#define HA_SPIN_INIT(l) ({ (*l) = 0; })
741#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
742#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
743#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
744#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200745
746#define HA_RWLOCK_T unsigned long
747
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100748#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
749#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
750#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
751#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
752#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
753#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
754#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
755#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200756
757#endif /* DEBUG_THREAD */
758
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100759#ifdef __x86_64__
760#define HA_HAVE_CAS_DW 1
761#define HA_CAS_IS_8B
762static __inline int
763__ha_cas_dw(void *target, void *compare, const void *set)
764{
765 char ret;
766
767 __asm __volatile("lock cmpxchg16b %0; setz %3"
768 : "+m" (*(void **)target),
769 "=a" (((void **)compare)[0]),
770 "=d" (((void **)compare)[1]),
771 "=q" (ret)
772 : "a" (((void **)compare)[0]),
773 "d" (((void **)compare)[1]),
774 "b" (((const void **)set)[0]),
775 "c" (((const void **)set)[1])
776 : "memory", "cc");
777 return (ret);
778}
779
780static __inline void
781__ha_barrier_load(void)
782{
783 __asm __volatile("lfence" ::: "memory");
784}
785
786static __inline void
787__ha_barrier_store(void)
788{
789 __asm __volatile("sfence" ::: "memory");
790}
791
792static __inline void
793__ha_barrier_full(void)
794{
795 __asm __volatile("mfence" ::: "memory");
796}
797
798#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
799#define HA_HAVE_CAS_DW 1
800static __inline void
801__ha_barrier_load(void)
802{
803 __asm __volatile("dmb" ::: "memory");
804}
805
806static __inline void
807__ha_barrier_store(void)
808{
809 __asm __volatile("dsb" ::: "memory");
810}
811
812static __inline void
813__ha_barrier_full(void)
814{
815 __asm __volatile("dmb" ::: "memory");
816}
817
Willy Tarreau41ccb192018-02-14 14:16:28 +0100818static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100819{
820 uint64_t previous;
821 int tmp;
822
823 __asm __volatile("1:"
824 "ldrexd %0, [%4];"
825 "cmp %Q0, %Q2;"
826 "ittt eq;"
827 "cmpeq %R0, %R2;"
828 "strexdeq %1, %3, [%4];"
829 "cmpeq %1, #1;"
830 "beq 1b;"
831 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100832 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100833 : "memory", "cc");
834 tmp = (previous == *(uint64_t *)compare);
835 *(uint64_t *)compare = previous;
836 return (tmp);
837}
838
839#elif defined (__aarch64__)
840#define HA_HAVE_CAS_DW 1
841#define HA_CAS_IS_8B
842
843static __inline void
844__ha_barrier_load(void)
845{
846 __asm __volatile("dmb ishld" ::: "memory");
847}
848
849static __inline void
850__ha_barrier_store(void)
851{
852 __asm __volatile("dmb ishst" ::: "memory");
853}
854
855static __inline void
856__ha_barrier_full(void)
857{
858 __asm __volatile("dmb ish" ::: "memory");
859}
860
861static __inline int __ha_cas_dw(void *target, void *compare, void *set)
862{
863 void *value[2];
864 uint64_t tmp1, tmp2;
865
866 __asm__ __volatile__("1:"
867 "ldxp %0, %1, [%4];"
868 "mov %2, %0;"
869 "mov %3, %1;"
870 "eor %0, %0, %5;"
871 "eor %1, %1, %6;"
872 "orr %1, %0, %1;"
873 "mov %w0, #0;"
874 "cbnz %1, 2f;"
875 "stxp %w0, %7, %8, [%4];"
876 "cbnz %w0, 1b;"
877 "mov %w0, #1;"
878 "2:"
879 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
880 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
881 : "cc", "memory");
882
883 memcpy(compare, &value, sizeof(value));
884 return (tmp1);
885}
886
887#else
888#define __ha_barrier_load __sync_synchronize
889#define __ha_barrier_store __sync_synchronize
890#define __ha_barrier_full __sync_synchronize
891#endif
892
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200893#endif /* USE_THREAD */
894
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100895static inline void __ha_compiler_barrier(void)
896{
897 __asm __volatile("" ::: "memory");
898}
899
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200900/* Dummy I/O handler used by the sync pipe.*/
901void thread_sync_io_handler(int fd);
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200902int parse_nbthread(const char *arg, char **err);
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200903
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200904#endif /* _COMMON_HATHREADS_H */