blob: 143cf2ca2b7954b4b099733ddf4023ce497ed103 [file] [log] [blame]
Christopher Faulet1a2b56e2017-10-12 16:09:09 +02001/*
2 * include/common/hathreads.h
3 * definitions, macros and inline functions about threads.
4 *
5 * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _COMMON_HATHREADS_H
23#define _COMMON_HATHREADS_H
24
25#include <common/config.h>
26
27#define MAX_THREADS_MASK ((unsigned long)-1)
28extern THREAD_LOCAL unsigned int tid; /* The thread id */
Christopher Faulete9a896e2017-11-14 10:16:04 +010029extern THREAD_LOCAL unsigned long tid_bit; /* The bit corresponding to the thread id */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020030
31#ifndef USE_THREAD
32
Willy Tarreau421f02e2018-01-20 18:19:22 +010033#define MAX_THREADS 1
34
Christopher Faulet9dcf9b62017-11-13 10:34:01 +010035#define __decl_hathreads(decl)
36
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020037#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
38#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
39#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
40#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
41#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
42#define HA_ATOMIC_XCHG(val, new) \
43 ({ \
44 typeof(*(val)) __old = *(val); \
45 *(val) = new; \
46 __old; \
47 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +010048#define HA_ATOMIC_BTS(val, bit) \
49 ({ \
50 typeof((val)) __p = (val); \
51 typeof(*__p) __b = (1UL << (bit)); \
52 typeof(*__p) __t = *__p & __b; \
53 if (!__t) \
54 *__p |= __b; \
55 __t; \
56 })
57#define HA_ATOMIC_BTR(val, bit) \
58 ({ \
59 typeof((val)) __p = (val); \
60 typeof(*__p) __b = (1UL << (bit)); \
61 typeof(*__p) __t = *__p & __b; \
62 if (__t) \
63 *__p &= ~__b; \
64 __t; \
65 })
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020066#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
67#define HA_ATOMIC_UPDATE_MAX(val, new) \
68 ({ \
69 typeof(*(val)) __new = (new); \
70 \
71 if (*(val) < __new) \
72 *(val) = __new; \
73 *(val); \
74 })
75
76#define HA_ATOMIC_UPDATE_MIN(val, new) \
77 ({ \
78 typeof(*(val)) __new = (new); \
79 \
80 if (*(val) > __new) \
81 *(val) = __new; \
82 *(val); \
83 })
84
Willy Tarreaub29dc952017-10-31 18:00:20 +010085#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020086
87#define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0)
88#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
89#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
90#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
91#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
92#define THREAD_NO_SYNC() ({ 0; })
93#define THREAD_NEED_SYNC() ({ 1; })
94
Christopher Faulet2a944ee2017-11-07 10:42:54 +010095#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
96#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
97#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
98#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
99#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200100
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100101#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
102#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
103#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
104#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
105#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
106#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
107#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
108#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200109
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100110static inline void __ha_barrier_load(void)
111{
112}
113
114static inline void __ha_barrier_store(void)
115{
116}
117
118static inline void __ha_barrier_full(void)
119{
120}
121
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200122#else /* USE_THREAD */
123
124#include <stdio.h>
125#include <stdlib.h>
126#include <string.h>
127#include <pthread.h>
128#include <import/plock.h>
129
Willy Tarreau421f02e2018-01-20 18:19:22 +0100130#define MAX_THREADS LONGBITS
131
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100132#define __decl_hathreads(decl) decl
133
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200134/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
135 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100136
David Carlierec5e8452018-01-11 14:20:43 +0000137#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100138/* gcc < 4.7 */
139
140#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
141#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
142#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
143#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
144
145/* the CAS is a bit complicated. The older API doesn't support returning the
146 * value and the swap's result at the same time. So here we take what looks
147 * like the safest route, consisting in using the boolean version guaranteeing
148 * that the operation was performed or not, and we snoop a previous value. If
149 * the compare succeeds, we return. If it fails, we return the previous value,
150 * but only if it differs from the expected one. If it's the same it's a race
151 * thus we try again to avoid confusing a possibly sensitive caller.
152 */
153#define HA_ATOMIC_CAS(val, old, new) \
154 ({ \
155 typeof((val)) __val = (val); \
156 typeof((old)) __oldp = (old); \
157 typeof(*(old)) __oldv; \
158 typeof((new)) __new = (new); \
159 int __ret; \
160 do { \
161 __oldv = *__val; \
162 __ret = __sync_bool_compare_and_swap(__val, *__oldp, __new); \
163 } while (!__ret && *__oldp == __oldv); \
164 if (!__ret) \
165 *__oldp = __oldv; \
166 __ret; \
167 })
168
169#define HA_ATOMIC_XCHG(val, new) \
170 ({ \
171 typeof((val)) __val = (val); \
172 typeof(*(val)) __old; \
173 typeof((new)) __new = (new); \
174 do { __old = *__val; \
175 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
176 __old; \
177 })
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100178
179#define HA_ATOMIC_BTS(val, bit) \
180 ({ \
181 typeof(*(val)) __b = (1UL << (bit)); \
182 __sync_fetch_and_or((val), __b) & __b; \
183 })
184
185#define HA_ATOMIC_BTR(val, bit) \
186 ({ \
187 typeof(*(val)) __b = (1UL << (bit)); \
188 __sync_fetch_and_and((val), ~__b) & __b; \
189 })
190
Willy Tarreau1a69af62018-01-04 18:49:31 +0100191#define HA_ATOMIC_STORE(val, new) \
192 ({ \
193 typeof((val)) __val = (val); \
194 typeof(*(val)) __old; \
195 typeof((new)) __new = (new); \
196 do { __old = *__val; \
197 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
198 })
199#else
200/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200201#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
202#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
203#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
204#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
205#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
Willy Tarreau5266b3e2018-01-25 17:43:58 +0100206#define HA_ATOMIC_BTS(val, bit) \
207 ({ \
208 typeof(*(val)) __b = (1UL << (bit)); \
209 __sync_fetch_and_or((val), __b) & __b; \
210 })
211
212#define HA_ATOMIC_BTR(val, bit) \
213 ({ \
214 typeof(*(val)) __b = (1UL << (bit)); \
215 __sync_fetch_and_and((val), ~__b) & __b; \
216 })
217
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200218#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
219#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100220#endif
221
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200222#define HA_ATOMIC_UPDATE_MAX(val, new) \
223 ({ \
224 typeof(*(val)) __old = *(val); \
225 typeof(*(val)) __new = (new); \
226 \
227 while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
228 (*val); \
229 })
230#define HA_ATOMIC_UPDATE_MIN(val, new) \
231 ({ \
232 typeof((*val)) __old = *(val); \
233 typeof((*val)) __new = (new); \
234 \
235 while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
236 (*val); \
237 })
238
Willy Tarreaub29dc952017-10-31 18:00:20 +0100239#define HA_BARRIER() pl_barrier()
240
Christopher Faulet339fff82017-10-19 11:59:15 +0200241#define THREAD_SYNC_INIT(m) thread_sync_init(m)
242#define THREAD_SYNC_ENABLE() thread_sync_enable()
243#define THREAD_WANT_SYNC() thread_want_sync()
244#define THREAD_ENTER_SYNC() thread_enter_sync()
245#define THREAD_EXIT_SYNC() thread_exit_sync()
246#define THREAD_NO_SYNC() thread_no_sync()
247#define THREAD_NEED_SYNC() thread_need_sync()
248
249int thread_sync_init(unsigned long mask);
250void thread_sync_enable(void);
251void thread_want_sync(void);
252void thread_enter_sync(void);
253void thread_exit_sync(void);
254int thread_no_sync(void);
255int thread_need_sync(void);
256
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200257#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
258
Christopher Fauletf51bac22018-01-30 11:04:29 +0100259/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200260enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200261 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200262 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200263 TASK_RQ_LOCK,
264 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200265 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200266 LISTENER_LOCK,
267 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200268 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200269 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200270 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200271 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200272 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200273 STK_TABLE_LOCK,
274 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200275 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200276 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200277 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200278 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200279 SSL_LOCK,
280 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200281 PATREF_LOCK,
282 PATEXP_LOCK,
283 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200284 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200285 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200286 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200287 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200288 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200289 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200290 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200291 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100292 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100293 START_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200294 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200295};
296struct lock_stat {
297 uint64_t nsec_wait_for_write;
298 uint64_t nsec_wait_for_read;
299 uint64_t num_write_locked;
300 uint64_t num_write_unlocked;
301 uint64_t num_read_locked;
302 uint64_t num_read_unlocked;
303};
304
305extern struct lock_stat lock_stats[LOCK_LABELS];
306
307#define __HA_SPINLOCK_T unsigned long
308
309#define __SPIN_INIT(l) ({ (*l) = 0; })
310#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100311#define __SPIN_LOCK(l) pl_take_s(l)
312#define __SPIN_TRYLOCK(l) !pl_try_s(l)
313#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200314
315#define __HA_RWLOCK_T unsigned long
316
317#define __RWLOCK_INIT(l) ({ (*l) = 0; })
318#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
319#define __RWLOCK_WRLOCK(l) pl_take_w(l)
320#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
321#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
322#define __RWLOCK_RDLOCK(l) pl_take_r(l)
323#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
324#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
325
326#define HA_SPINLOCK_T struct ha_spinlock
327
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100328#define HA_SPIN_INIT(l) __spin_init(l)
329#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200330
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
332#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
333#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200334
335#define HA_RWLOCK_T struct ha_rwlock
336
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
338#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
339#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
340#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
341#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
342#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
343#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
344#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200345
346struct ha_spinlock {
347 __HA_SPINLOCK_T lock;
348 struct {
349 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
350 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
351 struct {
352 const char *function;
353 const char *file;
354 int line;
355 } last_location; /* location of the last owner */
356 } info;
357};
358
359struct ha_rwlock {
360 __HA_RWLOCK_T lock;
361 struct {
362 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
363 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
364 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
365 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
366 struct {
367 const char *function;
368 const char *file;
369 int line;
370 } last_location; /* location of the last write owner */
371 } info;
372};
373
Christopher Fauletf51bac22018-01-30 11:04:29 +0100374static inline const char *lock_label(enum lock_label label)
375{
376 switch (label) {
377 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100378 case FD_LOCK: return "FD";
379 case TASK_RQ_LOCK: return "TASK_RQ";
380 case TASK_WQ_LOCK: return "TASK_WQ";
381 case POOL_LOCK: return "POOL";
382 case LISTENER_LOCK: return "LISTENER";
383 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
384 case PROXY_LOCK: return "PROXY";
385 case SERVER_LOCK: return "SERVER";
386 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
387 case LBPRM_LOCK: return "LBPRM";
388 case SIGNALS_LOCK: return "SIGNALS";
389 case STK_TABLE_LOCK: return "STK_TABLE";
390 case STK_SESS_LOCK: return "STK_SESS";
391 case APPLETS_LOCK: return "APPLETS";
392 case PEER_LOCK: return "PEER";
393 case BUF_WQ_LOCK: return "BUF_WQ";
394 case STRMS_LOCK: return "STRMS";
395 case SSL_LOCK: return "SSL";
396 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
397 case PATREF_LOCK: return "PATREF";
398 case PATEXP_LOCK: return "PATEXP";
399 case PATLRU_LOCK: return "PATLRU";
400 case VARS_LOCK: return "VARS";
401 case COMP_POOL_LOCK: return "COMP_POOL";
402 case LUA_LOCK: return "LUA";
403 case NOTIF_LOCK: return "NOTIF";
404 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
405 case DNS_LOCK: return "DNS";
406 case PID_LIST_LOCK: return "PID_LIST";
407 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
408 case PIPES_LOCK: return "PIPES";
409 case START_LOCK: return "START";
410 case LOCK_LABELS: break; /* keep compiler happy */
411 };
412 /* only way to come here is consecutive to an internal bug */
413 abort();
414}
415
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200416static inline void show_lock_stats()
417{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200418 int lbl;
419
420 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
421 fprintf(stderr,
422 "Stats about Lock %s: \n"
423 "\t # write lock : %lu\n"
424 "\t # write unlock: %lu (%ld)\n"
425 "\t # wait time for write : %.3f msec\n"
426 "\t # wait time for write/lock: %.3f nsec\n"
427 "\t # read lock : %lu\n"
428 "\t # read unlock : %lu (%ld)\n"
429 "\t # wait time for read : %.3f msec\n"
430 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100431 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200432 lock_stats[lbl].num_write_locked,
433 lock_stats[lbl].num_write_unlocked,
434 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
435 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
436 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
437 lock_stats[lbl].num_read_locked,
438 lock_stats[lbl].num_read_unlocked,
439 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
440 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
441 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
442 }
443}
444
445/* Following functions are used to collect some stats about locks. We wrap
446 * pthread functions to known how much time we wait in a lock. */
447
448static uint64_t nsec_now(void) {
449 struct timespec ts;
450
451 clock_gettime(CLOCK_MONOTONIC, &ts);
452 return ((uint64_t) ts.tv_sec * 1000000000ULL +
453 (uint64_t) ts.tv_nsec);
454}
455
456static inline void __ha_rwlock_init(struct ha_rwlock *l)
457{
458 memset(l, 0, sizeof(struct ha_rwlock));
459 __RWLOCK_INIT(&l->lock);
460}
461
462static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
463{
464 __RWLOCK_DESTROY(&l->lock);
465 memset(l, 0, sizeof(struct ha_rwlock));
466}
467
468
469static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
470 const char *func, const char *file, int line)
471{
472 uint64_t start_time;
473
474 if (unlikely(l->info.cur_writer & tid_bit)) {
475 /* the thread is already owning the lock for write */
476 abort();
477 }
478
479 if (unlikely(l->info.cur_readers & tid_bit)) {
480 /* the thread is already owning the lock for read */
481 abort();
482 }
483
484 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
485
486 start_time = nsec_now();
487 __RWLOCK_WRLOCK(&l->lock);
488 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
489
490 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
491
492 l->info.cur_writer = tid_bit;
493 l->info.last_location.function = func;
494 l->info.last_location.file = file;
495 l->info.last_location.line = line;
496
497 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
498}
499
500static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
501 const char *func, const char *file, int line)
502{
503 uint64_t start_time;
504 int r;
505
506 if (unlikely(l->info.cur_writer & tid_bit)) {
507 /* the thread is already owning the lock for write */
508 abort();
509 }
510
511 if (unlikely(l->info.cur_readers & tid_bit)) {
512 /* the thread is already owning the lock for read */
513 abort();
514 }
515
516 /* We set waiting writer because trywrlock could wait for readers to quit */
517 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
518
519 start_time = nsec_now();
520 r = __RWLOCK_TRYWRLOCK(&l->lock);
521 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
522 if (unlikely(r)) {
523 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
524 return r;
525 }
526 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
527
528 l->info.cur_writer = tid_bit;
529 l->info.last_location.function = func;
530 l->info.last_location.file = file;
531 l->info.last_location.line = line;
532
533 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
534
535 return 0;
536}
537
538static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
539 const char *func, const char *file, int line)
540{
541 if (unlikely(!(l->info.cur_writer & tid_bit))) {
542 /* the thread is not owning the lock for write */
543 abort();
544 }
545
546 l->info.cur_writer = 0;
547 l->info.last_location.function = func;
548 l->info.last_location.file = file;
549 l->info.last_location.line = line;
550
551 __RWLOCK_WRUNLOCK(&l->lock);
552
553 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
554}
555
556static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
557{
558 uint64_t start_time;
559
560 if (unlikely(l->info.cur_writer & tid_bit)) {
561 /* the thread is already owning the lock for write */
562 abort();
563 }
564
565 if (unlikely(l->info.cur_readers & tid_bit)) {
566 /* the thread is already owning the lock for read */
567 abort();
568 }
569
570 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
571
572 start_time = nsec_now();
573 __RWLOCK_RDLOCK(&l->lock);
574 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
575 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
576
577 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
578
579 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
580}
581
582static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
583{
584 int r;
585
586 if (unlikely(l->info.cur_writer & tid_bit)) {
587 /* the thread is already owning the lock for write */
588 abort();
589 }
590
591 if (unlikely(l->info.cur_readers & tid_bit)) {
592 /* the thread is already owning the lock for read */
593 abort();
594 }
595
596 /* try read should never wait */
597 r = __RWLOCK_TRYRDLOCK(&l->lock);
598 if (unlikely(r))
599 return r;
600 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
601
602 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
603
604 return 0;
605}
606
607static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
608{
609 if (unlikely(!(l->info.cur_readers & tid_bit))) {
610 /* the thread is not owning the lock for read */
611 abort();
612 }
613
614 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
615
616 __RWLOCK_RDUNLOCK(&l->lock);
617
618 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
619}
620
621static inline void __spin_init(struct ha_spinlock *l)
622{
623 memset(l, 0, sizeof(struct ha_spinlock));
624 __SPIN_INIT(&l->lock);
625}
626
627static inline void __spin_destroy(struct ha_spinlock *l)
628{
629 __SPIN_DESTROY(&l->lock);
630 memset(l, 0, sizeof(struct ha_spinlock));
631}
632
633static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
634 const char *func, const char *file, int line)
635{
636 uint64_t start_time;
637
638 if (unlikely(l->info.owner & tid_bit)) {
639 /* the thread is already owning the lock */
640 abort();
641 }
642
643 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
644
645 start_time = nsec_now();
646 __SPIN_LOCK(&l->lock);
647 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
648
649 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
650
651
652 l->info.owner = tid_bit;
653 l->info.last_location.function = func;
654 l->info.last_location.file = file;
655 l->info.last_location.line = line;
656
657 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
658}
659
660static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
661 const char *func, const char *file, int line)
662{
663 int r;
664
665 if (unlikely(l->info.owner & tid_bit)) {
666 /* the thread is already owning the lock */
667 abort();
668 }
669
670 /* try read should never wait */
671 r = __SPIN_TRYLOCK(&l->lock);
672 if (unlikely(r))
673 return r;
674 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
675
676 l->info.owner = tid_bit;
677 l->info.last_location.function = func;
678 l->info.last_location.file = file;
679 l->info.last_location.line = line;
680
681 return 0;
682}
683
684static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
685 const char *func, const char *file, int line)
686{
687 if (unlikely(!(l->info.owner & tid_bit))) {
688 /* the thread is not owning the lock */
689 abort();
690 }
691
692 l->info.owner = 0;
693 l->info.last_location.function = func;
694 l->info.last_location.file = file;
695 l->info.last_location.line = line;
696
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100697 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200698 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
699}
700
701#else /* DEBUG_THREAD */
702
703#define HA_SPINLOCK_T unsigned long
704
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100705#define HA_SPIN_INIT(l) ({ (*l) = 0; })
706#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
707#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
708#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
709#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200710
711#define HA_RWLOCK_T unsigned long
712
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100713#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
714#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
715#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
716#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
717#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
718#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
719#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
720#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200721
722#endif /* DEBUG_THREAD */
723
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100724#ifdef __x86_64__
725#define HA_HAVE_CAS_DW 1
726#define HA_CAS_IS_8B
727static __inline int
728__ha_cas_dw(void *target, void *compare, const void *set)
729{
730 char ret;
731
732 __asm __volatile("lock cmpxchg16b %0; setz %3"
733 : "+m" (*(void **)target),
734 "=a" (((void **)compare)[0]),
735 "=d" (((void **)compare)[1]),
736 "=q" (ret)
737 : "a" (((void **)compare)[0]),
738 "d" (((void **)compare)[1]),
739 "b" (((const void **)set)[0]),
740 "c" (((const void **)set)[1])
741 : "memory", "cc");
742 return (ret);
743}
744
745static __inline void
746__ha_barrier_load(void)
747{
748 __asm __volatile("lfence" ::: "memory");
749}
750
751static __inline void
752__ha_barrier_store(void)
753{
754 __asm __volatile("sfence" ::: "memory");
755}
756
757static __inline void
758__ha_barrier_full(void)
759{
760 __asm __volatile("mfence" ::: "memory");
761}
762
763#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
764#define HA_HAVE_CAS_DW 1
765static __inline void
766__ha_barrier_load(void)
767{
768 __asm __volatile("dmb" ::: "memory");
769}
770
771static __inline void
772__ha_barrier_store(void)
773{
774 __asm __volatile("dsb" ::: "memory");
775}
776
777static __inline void
778__ha_barrier_full(void)
779{
780 __asm __volatile("dmb" ::: "memory");
781}
782
Willy Tarreau41ccb192018-02-14 14:16:28 +0100783static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100784{
785 uint64_t previous;
786 int tmp;
787
788 __asm __volatile("1:"
789 "ldrexd %0, [%4];"
790 "cmp %Q0, %Q2;"
791 "ittt eq;"
792 "cmpeq %R0, %R2;"
793 "strexdeq %1, %3, [%4];"
794 "cmpeq %1, #1;"
795 "beq 1b;"
796 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100797 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100798 : "memory", "cc");
799 tmp = (previous == *(uint64_t *)compare);
800 *(uint64_t *)compare = previous;
801 return (tmp);
802}
803
804#elif defined (__aarch64__)
805#define HA_HAVE_CAS_DW 1
806#define HA_CAS_IS_8B
807
808static __inline void
809__ha_barrier_load(void)
810{
811 __asm __volatile("dmb ishld" ::: "memory");
812}
813
814static __inline void
815__ha_barrier_store(void)
816{
817 __asm __volatile("dmb ishst" ::: "memory");
818}
819
820static __inline void
821__ha_barrier_full(void)
822{
823 __asm __volatile("dmb ish" ::: "memory");
824}
825
826static __inline int __ha_cas_dw(void *target, void *compare, void *set)
827{
828 void *value[2];
829 uint64_t tmp1, tmp2;
830
831 __asm__ __volatile__("1:"
832 "ldxp %0, %1, [%4];"
833 "mov %2, %0;"
834 "mov %3, %1;"
835 "eor %0, %0, %5;"
836 "eor %1, %1, %6;"
837 "orr %1, %0, %1;"
838 "mov %w0, #0;"
839 "cbnz %1, 2f;"
840 "stxp %w0, %7, %8, [%4];"
841 "cbnz %w0, 1b;"
842 "mov %w0, #1;"
843 "2:"
844 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
845 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
846 : "cc", "memory");
847
848 memcpy(compare, &value, sizeof(value));
849 return (tmp1);
850}
851
852#else
853#define __ha_barrier_load __sync_synchronize
854#define __ha_barrier_store __sync_synchronize
855#define __ha_barrier_full __sync_synchronize
856#endif
857
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200858#endif /* USE_THREAD */
859
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100860static inline void __ha_compiler_barrier(void)
861{
862 __asm __volatile("" ::: "memory");
863}
864
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200865#endif /* _COMMON_HATHREADS_H */