blob: d1dcbbf44662f59eaf07e33710047170060f618a [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
Christopher Faulet2a944ee2017-11-07 10:42:54 +010097#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
98#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
99#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
100#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
101#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200102
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100103#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
104#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
105#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
106#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
107#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
108#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
109#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
110#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200111
William Lallemand6e1796e2018-06-07 11:23:40 +0200112#define ha_sigmask(how, set, oldset) sigprocmask(how, set, oldset)
113
Willy Tarreau0c026f42018-08-01 19:12:20 +0200114static inline void ha_set_tid(unsigned int tid)
115{
116}
William Lallemand6e1796e2018-06-07 11:23:40 +0200117
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100118static inline void __ha_barrier_load(void)
119{
120}
121
122static inline void __ha_barrier_store(void)
123{
124}
125
126static inline void __ha_barrier_full(void)
127{
128}
129
Willy Tarreau60b639c2018-08-02 10:16:17 +0200130static inline void thread_harmless_now()
131{
132}
133
134static inline void thread_harmless_end()
135{
136}
137
138static inline void thread_isolate()
139{
140}
141
142static inline void thread_release()
143{
144}
145
146static inline unsigned long thread_isolated()
147{
148 return 1;
149}
150
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200151#else /* USE_THREAD */
152
153#include <stdio.h>
154#include <stdlib.h>
155#include <string.h>
156#include <pthread.h>
157#include <import/plock.h>
158
Willy Tarreau421f02e2018-01-20 18:19:22 +0100159#define MAX_THREADS LONGBITS
Willy Tarreau0c026f42018-08-01 19:12:20 +0200160#define MAX_THREADS_MASK ((unsigned long)-1)
Willy Tarreau421f02e2018-01-20 18:19:22 +0100161
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100162#define __decl_hathreads(decl) decl
163
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200164/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
165 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100166
David Carlierec5e8452018-01-11 14:20:43 +0000167#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100168/* gcc < 4.7 */
169
170#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
171#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
172#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
173#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
174
175/* the CAS is a bit complicated. The older API doesn't support returning the
176 * value and the swap's result at the same time. So here we take what looks
177 * like the safest route, consisting in using the boolean version guaranteeing
178 * that the operation was performed or not, and we snoop a previous value. If
179 * the compare succeeds, we return. If it fails, we return the previous value,
180 * but only if it differs from the expected one. If it's the same it's a race
181 * thus we try again to avoid confusing a possibly sensitive caller.
182 */
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200183#define HA_ATOMIC_CAS(val, old, new) \
184 ({ \
185 typeof((val)) __val_cas = (val); \
186 typeof((old)) __oldp_cas = (old); \
187 typeof(*(old)) __oldv_cas; \
188 typeof((new)) __new_cas = (new); \
189 int __ret_cas; \
190 do { \
191 __oldv_cas = *__val_cas; \
192 __ret_cas = __sync_bool_compare_and_swap(__val_cas, *__oldp_cas, __new_cas); \
193 } while (!__ret_cas && *__oldp_cas == __oldv_cas); \
194 if (!__ret_cas) \
195 *__oldp_cas = __oldv_cas; \
196 __ret_cas; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100197 })
198
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200199#define HA_ATOMIC_XCHG(val, new) \
200 ({ \
201 typeof((val)) __val_xchg = (val); \
202 typeof(*(val)) __old_xchg; \
203 typeof((new)) __new_xchg = (new); \
204 do { __old_xchg = *__val_xchg; \
205 } while (!__sync_bool_compare_and_swap(__val_xchg, __old_xchg, __new_xchg)); \
206 __old_xchg; \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100207 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100208
209#define HA_ATOMIC_BTS(val, bit) \
210 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200211 typeof(*(val)) __b_bts = (1UL << (bit)); \
212 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100213 })
214
215#define HA_ATOMIC_BTR(val, bit) \
216 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200217 typeof(*(val)) __b_btr = (1UL << (bit)); \
218 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100219 })
220
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200221#define HA_ATOMIC_STORE(val, new) \
222 ({ \
223 typeof((val)) __val_store = (val); \
224 typeof(*(val)) __old_store; \
225 typeof((new)) __new_store = (new); \
226 do { __old_store = *__val_store; \
227 } while (!__sync_bool_compare_and_swap(__val_store, __old_store, __new_store)); \
Willy Tarreau1a69af62018-01-04 18:49:31 +0100228 })
229#else
230/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200231#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
232#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
233#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
234#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
235#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100236#define HA_ATOMIC_BTS(val, bit) \
237 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200238 typeof(*(val)) __b_bts = (1UL << (bit)); \
239 __sync_fetch_and_or((val), __b_bts) & __b_bts; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100240 })
241
242#define HA_ATOMIC_BTR(val, bit) \
243 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200244 typeof(*(val)) __b_btr = (1UL << (bit)); \
245 __sync_fetch_and_and((val), ~__b_btr) & __b_btr; \
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100246 })
247
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200248#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
249#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100250#endif
251
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200252#define HA_ATOMIC_UPDATE_MAX(val, new) \
253 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200254 typeof(*(val)) __old_max = *(val); \
255 typeof(*(val)) __new_max = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200256 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200257 while (__old_max < __new_max && \
258 !HA_ATOMIC_CAS(val, &__old_max, __new_max)); \
259 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200260 })
261#define HA_ATOMIC_UPDATE_MIN(val, new) \
262 ({ \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200263 typeof(*(val)) __old_min = *(val); \
264 typeof(*(val)) __new_min = (new); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200265 \
Christopher Faulet48aa13f2018-04-09 08:45:43 +0200266 while (__old_min > __new_min && \
267 !HA_ATOMIC_CAS(val, &__old_min, __new_min)); \
268 *(val); \
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200269 })
270
Willy Tarreaub29dc952017-10-31 18:00:20 +0100271#define HA_BARRIER() pl_barrier()
272
Willy Tarreau60b639c2018-08-02 10:16:17 +0200273void thread_harmless_till_end();
274void thread_isolate();
275void thread_release();
Christopher Faulet339fff82017-10-19 11:59:15 +0200276
Willy Tarreau0c026f42018-08-01 19:12:20 +0200277extern THREAD_LOCAL unsigned int tid; /* The thread id */
278extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Fauletddb6c162018-07-20 09:31:53 +0200279extern volatile unsigned long all_threads_mask;
Willy Tarreau60b639c2018-08-02 10:16:17 +0200280extern volatile unsigned long threads_want_rdv_mask;
281extern volatile unsigned long threads_harmless_mask;
282
283/* explanation for threads_want_rdv_mask and threads_harmless_mask :
284 * - threads_want_rdv_mask is a bit field indicating all threads that have
285 * requested a rendez-vous of other threads using thread_isolate().
286 * - threads_harmless_mask is a bit field indicating all threads that are
287 * currently harmless in that they promise not to access a shared resource.
288 *
289 * For a given thread, its bits in want_rdv and harmless can be translated like
290 * this :
291 *
292 * ----------+----------+----------------------------------------------------
293 * want_rdv | harmless | description
294 * ----------+----------+----------------------------------------------------
295 * 0 | 0 | thread not interested in RDV, possibly harmful
296 * 0 | 1 | thread not interested in RDV but harmless
297 * 1 | 1 | thread interested in RDV and waiting for its turn
298 * 1 | 0 | thread currently working isolated from others
299 * ----------+----------+----------------------------------------------------
300 */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200301
William Lallemand6e1796e2018-06-07 11:23:40 +0200302#define ha_sigmask(how, set, oldset) pthread_sigmask(how, set, oldset)
303
Willy Tarreau0c026f42018-08-01 19:12:20 +0200304/* sets the thread ID and the TID bit for the current thread */
305static inline void ha_set_tid(unsigned int data)
306{
307 tid = data;
308 tid_bit = (1UL << tid);
309}
310
Willy Tarreau60b639c2018-08-02 10:16:17 +0200311/* Marks the thread as harmless. Note: this must be true, i.e. the thread must
312 * not be touching any unprotected shared resource during this period. Usually
313 * this is called before poll(), but it may also be placed around very slow
314 * calls (eg: some crypto operations). Needs to be terminated using
315 * thread_harmless_end().
316 */
317static inline void thread_harmless_now()
318{
319 HA_ATOMIC_OR(&threads_harmless_mask, tid_bit);
320}
321
322/* Ends the harmless period started by thread_harmless_now(). Usually this is
323 * placed after the poll() call. If it is discovered that a job was running and
324 * is relying on the thread still being harmless, the thread waits for the
325 * other one to finish.
326 */
327static inline void thread_harmless_end()
328{
329 while (1) {
330 HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit);
331 if (likely((threads_want_rdv_mask & all_threads_mask) == 0))
332 break;
333 thread_harmless_till_end();
334 }
335}
336
337/* an isolated thread has harmless cleared and want_rdv set */
338static inline unsigned long thread_isolated()
339{
340 return threads_want_rdv_mask & ~threads_harmless_mask & tid_bit;
341}
342
William Lallemand6e1796e2018-06-07 11:23:40 +0200343
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200344#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
345
Christopher Fauletf51bac22018-01-30 11:04:29 +0100346/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200347enum lock_label {
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200348 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200349 TASK_RQ_LOCK,
350 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200351 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200352 LISTENER_LOCK,
353 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200354 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200355 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200356 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200357 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200358 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200359 STK_TABLE_LOCK,
360 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200361 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200362 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200363 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200364 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200365 SSL_LOCK,
366 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200367 PATREF_LOCK,
368 PATEXP_LOCK,
369 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200370 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200371 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200372 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200373 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200374 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200375 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200376 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200377 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100378 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100379 START_LOCK,
Christopher Faulet16f45c82018-02-16 11:23:49 +0100380 TLSKEYS_REF_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200381 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200382};
383struct lock_stat {
384 uint64_t nsec_wait_for_write;
385 uint64_t nsec_wait_for_read;
386 uint64_t num_write_locked;
387 uint64_t num_write_unlocked;
388 uint64_t num_read_locked;
389 uint64_t num_read_unlocked;
390};
391
392extern struct lock_stat lock_stats[LOCK_LABELS];
393
394#define __HA_SPINLOCK_T unsigned long
395
396#define __SPIN_INIT(l) ({ (*l) = 0; })
397#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100398#define __SPIN_LOCK(l) pl_take_s(l)
399#define __SPIN_TRYLOCK(l) !pl_try_s(l)
400#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200401
402#define __HA_RWLOCK_T unsigned long
403
404#define __RWLOCK_INIT(l) ({ (*l) = 0; })
405#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
406#define __RWLOCK_WRLOCK(l) pl_take_w(l)
407#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
408#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
409#define __RWLOCK_RDLOCK(l) pl_take_r(l)
410#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
411#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
412
413#define HA_SPINLOCK_T struct ha_spinlock
414
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100415#define HA_SPIN_INIT(l) __spin_init(l)
416#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200417
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100418#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
419#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
420#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200421
422#define HA_RWLOCK_T struct ha_rwlock
423
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100424#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
425#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
426#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
427#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
428#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
429#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
430#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
431#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200432
433struct ha_spinlock {
434 __HA_SPINLOCK_T lock;
435 struct {
436 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
437 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
438 struct {
439 const char *function;
440 const char *file;
441 int line;
442 } last_location; /* location of the last owner */
443 } info;
444};
445
446struct ha_rwlock {
447 __HA_RWLOCK_T lock;
448 struct {
449 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
450 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
451 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
452 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
453 struct {
454 const char *function;
455 const char *file;
456 int line;
457 } last_location; /* location of the last write owner */
458 } info;
459};
460
Christopher Fauletf51bac22018-01-30 11:04:29 +0100461static inline const char *lock_label(enum lock_label label)
462{
463 switch (label) {
Christopher Fauletf51bac22018-01-30 11:04:29 +0100464 case FD_LOCK: return "FD";
465 case TASK_RQ_LOCK: return "TASK_RQ";
466 case TASK_WQ_LOCK: return "TASK_WQ";
467 case POOL_LOCK: return "POOL";
468 case LISTENER_LOCK: return "LISTENER";
469 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
470 case PROXY_LOCK: return "PROXY";
471 case SERVER_LOCK: return "SERVER";
472 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
473 case LBPRM_LOCK: return "LBPRM";
474 case SIGNALS_LOCK: return "SIGNALS";
475 case STK_TABLE_LOCK: return "STK_TABLE";
476 case STK_SESS_LOCK: return "STK_SESS";
477 case APPLETS_LOCK: return "APPLETS";
478 case PEER_LOCK: return "PEER";
479 case BUF_WQ_LOCK: return "BUF_WQ";
480 case STRMS_LOCK: return "STRMS";
481 case SSL_LOCK: return "SSL";
482 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
483 case PATREF_LOCK: return "PATREF";
484 case PATEXP_LOCK: return "PATEXP";
485 case PATLRU_LOCK: return "PATLRU";
486 case VARS_LOCK: return "VARS";
487 case COMP_POOL_LOCK: return "COMP_POOL";
488 case LUA_LOCK: return "LUA";
489 case NOTIF_LOCK: return "NOTIF";
490 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
491 case DNS_LOCK: return "DNS";
492 case PID_LIST_LOCK: return "PID_LIST";
493 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
494 case PIPES_LOCK: return "PIPES";
495 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100496 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100497 case LOCK_LABELS: break; /* keep compiler happy */
498 };
499 /* only way to come here is consecutive to an internal bug */
500 abort();
501}
502
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200503static inline void show_lock_stats()
504{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200505 int lbl;
506
507 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
508 fprintf(stderr,
509 "Stats about Lock %s: \n"
510 "\t # write lock : %lu\n"
511 "\t # write unlock: %lu (%ld)\n"
512 "\t # wait time for write : %.3f msec\n"
513 "\t # wait time for write/lock: %.3f nsec\n"
514 "\t # read lock : %lu\n"
515 "\t # read unlock : %lu (%ld)\n"
516 "\t # wait time for read : %.3f msec\n"
517 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100518 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200519 lock_stats[lbl].num_write_locked,
520 lock_stats[lbl].num_write_unlocked,
521 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
522 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
523 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
524 lock_stats[lbl].num_read_locked,
525 lock_stats[lbl].num_read_unlocked,
526 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
527 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
528 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
529 }
530}
531
532/* Following functions are used to collect some stats about locks. We wrap
533 * pthread functions to known how much time we wait in a lock. */
534
535static uint64_t nsec_now(void) {
536 struct timespec ts;
537
538 clock_gettime(CLOCK_MONOTONIC, &ts);
539 return ((uint64_t) ts.tv_sec * 1000000000ULL +
540 (uint64_t) ts.tv_nsec);
541}
542
543static inline void __ha_rwlock_init(struct ha_rwlock *l)
544{
545 memset(l, 0, sizeof(struct ha_rwlock));
546 __RWLOCK_INIT(&l->lock);
547}
548
549static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
550{
551 __RWLOCK_DESTROY(&l->lock);
552 memset(l, 0, sizeof(struct ha_rwlock));
553}
554
555
556static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
557 const char *func, const char *file, int line)
558{
559 uint64_t start_time;
560
561 if (unlikely(l->info.cur_writer & tid_bit)) {
562 /* the thread is already owning the lock for write */
563 abort();
564 }
565
566 if (unlikely(l->info.cur_readers & tid_bit)) {
567 /* the thread is already owning the lock for read */
568 abort();
569 }
570
571 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
572
573 start_time = nsec_now();
574 __RWLOCK_WRLOCK(&l->lock);
575 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
576
577 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
578
579 l->info.cur_writer = tid_bit;
580 l->info.last_location.function = func;
581 l->info.last_location.file = file;
582 l->info.last_location.line = line;
583
584 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
585}
586
587static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
588 const char *func, const char *file, int line)
589{
590 uint64_t start_time;
591 int r;
592
593 if (unlikely(l->info.cur_writer & tid_bit)) {
594 /* the thread is already owning the lock for write */
595 abort();
596 }
597
598 if (unlikely(l->info.cur_readers & tid_bit)) {
599 /* the thread is already owning the lock for read */
600 abort();
601 }
602
603 /* We set waiting writer because trywrlock could wait for readers to quit */
604 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
605
606 start_time = nsec_now();
607 r = __RWLOCK_TRYWRLOCK(&l->lock);
608 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
609 if (unlikely(r)) {
610 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
611 return r;
612 }
613 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
614
615 l->info.cur_writer = tid_bit;
616 l->info.last_location.function = func;
617 l->info.last_location.file = file;
618 l->info.last_location.line = line;
619
620 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
621
622 return 0;
623}
624
625static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
626 const char *func, const char *file, int line)
627{
628 if (unlikely(!(l->info.cur_writer & tid_bit))) {
629 /* the thread is not owning the lock for write */
630 abort();
631 }
632
633 l->info.cur_writer = 0;
634 l->info.last_location.function = func;
635 l->info.last_location.file = file;
636 l->info.last_location.line = line;
637
638 __RWLOCK_WRUNLOCK(&l->lock);
639
640 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
641}
642
643static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
644{
645 uint64_t start_time;
646
647 if (unlikely(l->info.cur_writer & tid_bit)) {
648 /* the thread is already owning the lock for write */
649 abort();
650 }
651
652 if (unlikely(l->info.cur_readers & tid_bit)) {
653 /* the thread is already owning the lock for read */
654 abort();
655 }
656
657 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
658
659 start_time = nsec_now();
660 __RWLOCK_RDLOCK(&l->lock);
661 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
662 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
663
664 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
665
666 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
667}
668
669static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
670{
671 int r;
672
673 if (unlikely(l->info.cur_writer & tid_bit)) {
674 /* the thread is already owning the lock for write */
675 abort();
676 }
677
678 if (unlikely(l->info.cur_readers & tid_bit)) {
679 /* the thread is already owning the lock for read */
680 abort();
681 }
682
683 /* try read should never wait */
684 r = __RWLOCK_TRYRDLOCK(&l->lock);
685 if (unlikely(r))
686 return r;
687 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
688
689 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
690
691 return 0;
692}
693
694static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
695{
696 if (unlikely(!(l->info.cur_readers & tid_bit))) {
697 /* the thread is not owning the lock for read */
698 abort();
699 }
700
701 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
702
703 __RWLOCK_RDUNLOCK(&l->lock);
704
705 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
706}
707
708static inline void __spin_init(struct ha_spinlock *l)
709{
710 memset(l, 0, sizeof(struct ha_spinlock));
711 __SPIN_INIT(&l->lock);
712}
713
714static inline void __spin_destroy(struct ha_spinlock *l)
715{
716 __SPIN_DESTROY(&l->lock);
717 memset(l, 0, sizeof(struct ha_spinlock));
718}
719
720static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
721 const char *func, const char *file, int line)
722{
723 uint64_t start_time;
724
725 if (unlikely(l->info.owner & tid_bit)) {
726 /* the thread is already owning the lock */
727 abort();
728 }
729
730 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
731
732 start_time = nsec_now();
733 __SPIN_LOCK(&l->lock);
734 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
735
736 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
737
738
739 l->info.owner = tid_bit;
740 l->info.last_location.function = func;
741 l->info.last_location.file = file;
742 l->info.last_location.line = line;
743
744 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
745}
746
747static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
748 const char *func, const char *file, int line)
749{
750 int r;
751
752 if (unlikely(l->info.owner & tid_bit)) {
753 /* the thread is already owning the lock */
754 abort();
755 }
756
757 /* try read should never wait */
758 r = __SPIN_TRYLOCK(&l->lock);
759 if (unlikely(r))
760 return r;
761 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
762
763 l->info.owner = tid_bit;
764 l->info.last_location.function = func;
765 l->info.last_location.file = file;
766 l->info.last_location.line = line;
767
768 return 0;
769}
770
771static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
772 const char *func, const char *file, int line)
773{
774 if (unlikely(!(l->info.owner & tid_bit))) {
775 /* the thread is not owning the lock */
776 abort();
777 }
778
779 l->info.owner = 0;
780 l->info.last_location.function = func;
781 l->info.last_location.file = file;
782 l->info.last_location.line = line;
783
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100784 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200785 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
786}
787
788#else /* DEBUG_THREAD */
789
790#define HA_SPINLOCK_T unsigned long
791
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100792#define HA_SPIN_INIT(l) ({ (*l) = 0; })
793#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
794#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
795#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
796#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200797
798#define HA_RWLOCK_T unsigned long
799
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100800#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
801#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
802#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
803#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
804#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
805#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
806#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
807#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200808
809#endif /* DEBUG_THREAD */
810
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100811#ifdef __x86_64__
812#define HA_HAVE_CAS_DW 1
813#define HA_CAS_IS_8B
814static __inline int
815__ha_cas_dw(void *target, void *compare, const void *set)
816{
817 char ret;
818
819 __asm __volatile("lock cmpxchg16b %0; setz %3"
820 : "+m" (*(void **)target),
821 "=a" (((void **)compare)[0]),
822 "=d" (((void **)compare)[1]),
823 "=q" (ret)
824 : "a" (((void **)compare)[0]),
825 "d" (((void **)compare)[1]),
826 "b" (((const void **)set)[0]),
827 "c" (((const void **)set)[1])
828 : "memory", "cc");
829 return (ret);
830}
831
832static __inline void
833__ha_barrier_load(void)
834{
835 __asm __volatile("lfence" ::: "memory");
836}
837
838static __inline void
839__ha_barrier_store(void)
840{
841 __asm __volatile("sfence" ::: "memory");
842}
843
844static __inline void
845__ha_barrier_full(void)
846{
847 __asm __volatile("mfence" ::: "memory");
848}
849
850#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
851#define HA_HAVE_CAS_DW 1
852static __inline void
853__ha_barrier_load(void)
854{
855 __asm __volatile("dmb" ::: "memory");
856}
857
858static __inline void
859__ha_barrier_store(void)
860{
861 __asm __volatile("dsb" ::: "memory");
862}
863
864static __inline void
865__ha_barrier_full(void)
866{
867 __asm __volatile("dmb" ::: "memory");
868}
869
Willy Tarreau41ccb192018-02-14 14:16:28 +0100870static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100871{
872 uint64_t previous;
873 int tmp;
874
875 __asm __volatile("1:"
876 "ldrexd %0, [%4];"
877 "cmp %Q0, %Q2;"
878 "ittt eq;"
879 "cmpeq %R0, %R2;"
880 "strexdeq %1, %3, [%4];"
881 "cmpeq %1, #1;"
882 "beq 1b;"
883 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100884 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100885 : "memory", "cc");
886 tmp = (previous == *(uint64_t *)compare);
887 *(uint64_t *)compare = previous;
888 return (tmp);
889}
890
891#elif defined (__aarch64__)
892#define HA_HAVE_CAS_DW 1
893#define HA_CAS_IS_8B
894
895static __inline void
896__ha_barrier_load(void)
897{
898 __asm __volatile("dmb ishld" ::: "memory");
899}
900
901static __inline void
902__ha_barrier_store(void)
903{
904 __asm __volatile("dmb ishst" ::: "memory");
905}
906
907static __inline void
908__ha_barrier_full(void)
909{
910 __asm __volatile("dmb ish" ::: "memory");
911}
912
913static __inline int __ha_cas_dw(void *target, void *compare, void *set)
914{
915 void *value[2];
916 uint64_t tmp1, tmp2;
917
918 __asm__ __volatile__("1:"
919 "ldxp %0, %1, [%4];"
920 "mov %2, %0;"
921 "mov %3, %1;"
922 "eor %0, %0, %5;"
923 "eor %1, %1, %6;"
924 "orr %1, %0, %1;"
925 "mov %w0, #0;"
926 "cbnz %1, 2f;"
927 "stxp %w0, %7, %8, [%4];"
928 "cbnz %w0, 1b;"
929 "mov %w0, #1;"
930 "2:"
931 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
932 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
933 : "cc", "memory");
934
935 memcpy(compare, &value, sizeof(value));
936 return (tmp1);
937}
938
939#else
940#define __ha_barrier_load __sync_synchronize
941#define __ha_barrier_store __sync_synchronize
942#define __ha_barrier_full __sync_synchronize
943#endif
944
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200945#endif /* USE_THREAD */
946
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100947static inline void __ha_compiler_barrier(void)
948{
949 __asm __volatile("" ::: "memory");
950}
951
Willy Tarreau0ccd3222018-07-30 10:34:35 +0200952int parse_nbthread(const char *arg, char **err);
Willy Tarreau4037a3f2018-03-28 18:06:47 +0200953
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200954#endif /* _COMMON_HATHREADS_H */