blob: 30009cc82486cafa6f6e110388882df027111c96 [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 Faulet16f45c82018-02-16 11:23:49 +0100294 TLSKEYS_REF_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200295 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200296};
297struct lock_stat {
298 uint64_t nsec_wait_for_write;
299 uint64_t nsec_wait_for_read;
300 uint64_t num_write_locked;
301 uint64_t num_write_unlocked;
302 uint64_t num_read_locked;
303 uint64_t num_read_unlocked;
304};
305
306extern struct lock_stat lock_stats[LOCK_LABELS];
307
308#define __HA_SPINLOCK_T unsigned long
309
310#define __SPIN_INIT(l) ({ (*l) = 0; })
311#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100312#define __SPIN_LOCK(l) pl_take_s(l)
313#define __SPIN_TRYLOCK(l) !pl_try_s(l)
314#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200315
316#define __HA_RWLOCK_T unsigned long
317
318#define __RWLOCK_INIT(l) ({ (*l) = 0; })
319#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
320#define __RWLOCK_WRLOCK(l) pl_take_w(l)
321#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
322#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
323#define __RWLOCK_RDLOCK(l) pl_take_r(l)
324#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
325#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
326
327#define HA_SPINLOCK_T struct ha_spinlock
328
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329#define HA_SPIN_INIT(l) __spin_init(l)
330#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200331
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
333#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
334#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200335
336#define HA_RWLOCK_T struct ha_rwlock
337
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100338#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
339#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
340#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
341#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
342#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
343#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
344#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
345#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200346
347struct ha_spinlock {
348 __HA_SPINLOCK_T lock;
349 struct {
350 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
351 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
352 struct {
353 const char *function;
354 const char *file;
355 int line;
356 } last_location; /* location of the last owner */
357 } info;
358};
359
360struct ha_rwlock {
361 __HA_RWLOCK_T lock;
362 struct {
363 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
364 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
365 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
366 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
367 struct {
368 const char *function;
369 const char *file;
370 int line;
371 } last_location; /* location of the last write owner */
372 } info;
373};
374
Christopher Fauletf51bac22018-01-30 11:04:29 +0100375static inline const char *lock_label(enum lock_label label)
376{
377 switch (label) {
378 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100379 case FD_LOCK: return "FD";
380 case TASK_RQ_LOCK: return "TASK_RQ";
381 case TASK_WQ_LOCK: return "TASK_WQ";
382 case POOL_LOCK: return "POOL";
383 case LISTENER_LOCK: return "LISTENER";
384 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
385 case PROXY_LOCK: return "PROXY";
386 case SERVER_LOCK: return "SERVER";
387 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
388 case LBPRM_LOCK: return "LBPRM";
389 case SIGNALS_LOCK: return "SIGNALS";
390 case STK_TABLE_LOCK: return "STK_TABLE";
391 case STK_SESS_LOCK: return "STK_SESS";
392 case APPLETS_LOCK: return "APPLETS";
393 case PEER_LOCK: return "PEER";
394 case BUF_WQ_LOCK: return "BUF_WQ";
395 case STRMS_LOCK: return "STRMS";
396 case SSL_LOCK: return "SSL";
397 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
398 case PATREF_LOCK: return "PATREF";
399 case PATEXP_LOCK: return "PATEXP";
400 case PATLRU_LOCK: return "PATLRU";
401 case VARS_LOCK: return "VARS";
402 case COMP_POOL_LOCK: return "COMP_POOL";
403 case LUA_LOCK: return "LUA";
404 case NOTIF_LOCK: return "NOTIF";
405 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
406 case DNS_LOCK: return "DNS";
407 case PID_LIST_LOCK: return "PID_LIST";
408 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
409 case PIPES_LOCK: return "PIPES";
410 case START_LOCK: return "START";
Christopher Faulet16f45c82018-02-16 11:23:49 +0100411 case TLSKEYS_REF_LOCK: return "TLSKEYS_REF";
Christopher Fauletf51bac22018-01-30 11:04:29 +0100412 case LOCK_LABELS: break; /* keep compiler happy */
413 };
414 /* only way to come here is consecutive to an internal bug */
415 abort();
416}
417
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200418static inline void show_lock_stats()
419{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200420 int lbl;
421
422 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
423 fprintf(stderr,
424 "Stats about Lock %s: \n"
425 "\t # write lock : %lu\n"
426 "\t # write unlock: %lu (%ld)\n"
427 "\t # wait time for write : %.3f msec\n"
428 "\t # wait time for write/lock: %.3f nsec\n"
429 "\t # read lock : %lu\n"
430 "\t # read unlock : %lu (%ld)\n"
431 "\t # wait time for read : %.3f msec\n"
432 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100433 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200434 lock_stats[lbl].num_write_locked,
435 lock_stats[lbl].num_write_unlocked,
436 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
437 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
438 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
439 lock_stats[lbl].num_read_locked,
440 lock_stats[lbl].num_read_unlocked,
441 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
442 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
443 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
444 }
445}
446
447/* Following functions are used to collect some stats about locks. We wrap
448 * pthread functions to known how much time we wait in a lock. */
449
450static uint64_t nsec_now(void) {
451 struct timespec ts;
452
453 clock_gettime(CLOCK_MONOTONIC, &ts);
454 return ((uint64_t) ts.tv_sec * 1000000000ULL +
455 (uint64_t) ts.tv_nsec);
456}
457
458static inline void __ha_rwlock_init(struct ha_rwlock *l)
459{
460 memset(l, 0, sizeof(struct ha_rwlock));
461 __RWLOCK_INIT(&l->lock);
462}
463
464static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
465{
466 __RWLOCK_DESTROY(&l->lock);
467 memset(l, 0, sizeof(struct ha_rwlock));
468}
469
470
471static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
472 const char *func, const char *file, int line)
473{
474 uint64_t start_time;
475
476 if (unlikely(l->info.cur_writer & tid_bit)) {
477 /* the thread is already owning the lock for write */
478 abort();
479 }
480
481 if (unlikely(l->info.cur_readers & tid_bit)) {
482 /* the thread is already owning the lock for read */
483 abort();
484 }
485
486 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
487
488 start_time = nsec_now();
489 __RWLOCK_WRLOCK(&l->lock);
490 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
491
492 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
493
494 l->info.cur_writer = tid_bit;
495 l->info.last_location.function = func;
496 l->info.last_location.file = file;
497 l->info.last_location.line = line;
498
499 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
500}
501
502static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
503 const char *func, const char *file, int line)
504{
505 uint64_t start_time;
506 int r;
507
508 if (unlikely(l->info.cur_writer & tid_bit)) {
509 /* the thread is already owning the lock for write */
510 abort();
511 }
512
513 if (unlikely(l->info.cur_readers & tid_bit)) {
514 /* the thread is already owning the lock for read */
515 abort();
516 }
517
518 /* We set waiting writer because trywrlock could wait for readers to quit */
519 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
520
521 start_time = nsec_now();
522 r = __RWLOCK_TRYWRLOCK(&l->lock);
523 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
524 if (unlikely(r)) {
525 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
526 return r;
527 }
528 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
529
530 l->info.cur_writer = tid_bit;
531 l->info.last_location.function = func;
532 l->info.last_location.file = file;
533 l->info.last_location.line = line;
534
535 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
536
537 return 0;
538}
539
540static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
541 const char *func, const char *file, int line)
542{
543 if (unlikely(!(l->info.cur_writer & tid_bit))) {
544 /* the thread is not owning the lock for write */
545 abort();
546 }
547
548 l->info.cur_writer = 0;
549 l->info.last_location.function = func;
550 l->info.last_location.file = file;
551 l->info.last_location.line = line;
552
553 __RWLOCK_WRUNLOCK(&l->lock);
554
555 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
556}
557
558static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
559{
560 uint64_t start_time;
561
562 if (unlikely(l->info.cur_writer & tid_bit)) {
563 /* the thread is already owning the lock for write */
564 abort();
565 }
566
567 if (unlikely(l->info.cur_readers & tid_bit)) {
568 /* the thread is already owning the lock for read */
569 abort();
570 }
571
572 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
573
574 start_time = nsec_now();
575 __RWLOCK_RDLOCK(&l->lock);
576 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
577 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
578
579 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
580
581 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
582}
583
584static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
585{
586 int r;
587
588 if (unlikely(l->info.cur_writer & tid_bit)) {
589 /* the thread is already owning the lock for write */
590 abort();
591 }
592
593 if (unlikely(l->info.cur_readers & tid_bit)) {
594 /* the thread is already owning the lock for read */
595 abort();
596 }
597
598 /* try read should never wait */
599 r = __RWLOCK_TRYRDLOCK(&l->lock);
600 if (unlikely(r))
601 return r;
602 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
603
604 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
605
606 return 0;
607}
608
609static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
610{
611 if (unlikely(!(l->info.cur_readers & tid_bit))) {
612 /* the thread is not owning the lock for read */
613 abort();
614 }
615
616 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
617
618 __RWLOCK_RDUNLOCK(&l->lock);
619
620 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
621}
622
623static inline void __spin_init(struct ha_spinlock *l)
624{
625 memset(l, 0, sizeof(struct ha_spinlock));
626 __SPIN_INIT(&l->lock);
627}
628
629static inline void __spin_destroy(struct ha_spinlock *l)
630{
631 __SPIN_DESTROY(&l->lock);
632 memset(l, 0, sizeof(struct ha_spinlock));
633}
634
635static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
636 const char *func, const char *file, int line)
637{
638 uint64_t start_time;
639
640 if (unlikely(l->info.owner & tid_bit)) {
641 /* the thread is already owning the lock */
642 abort();
643 }
644
645 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
646
647 start_time = nsec_now();
648 __SPIN_LOCK(&l->lock);
649 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
650
651 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
652
653
654 l->info.owner = tid_bit;
655 l->info.last_location.function = func;
656 l->info.last_location.file = file;
657 l->info.last_location.line = line;
658
659 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
660}
661
662static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
663 const char *func, const char *file, int line)
664{
665 int r;
666
667 if (unlikely(l->info.owner & tid_bit)) {
668 /* the thread is already owning the lock */
669 abort();
670 }
671
672 /* try read should never wait */
673 r = __SPIN_TRYLOCK(&l->lock);
674 if (unlikely(r))
675 return r;
676 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
677
678 l->info.owner = tid_bit;
679 l->info.last_location.function = func;
680 l->info.last_location.file = file;
681 l->info.last_location.line = line;
682
683 return 0;
684}
685
686static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
687 const char *func, const char *file, int line)
688{
689 if (unlikely(!(l->info.owner & tid_bit))) {
690 /* the thread is not owning the lock */
691 abort();
692 }
693
694 l->info.owner = 0;
695 l->info.last_location.function = func;
696 l->info.last_location.file = file;
697 l->info.last_location.line = line;
698
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100699 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200700 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
701}
702
703#else /* DEBUG_THREAD */
704
705#define HA_SPINLOCK_T unsigned long
706
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100707#define HA_SPIN_INIT(l) ({ (*l) = 0; })
708#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
709#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
710#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
711#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200712
713#define HA_RWLOCK_T unsigned long
714
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100715#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
716#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
717#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
718#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
719#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
720#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
721#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
722#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200723
724#endif /* DEBUG_THREAD */
725
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100726#ifdef __x86_64__
727#define HA_HAVE_CAS_DW 1
728#define HA_CAS_IS_8B
729static __inline int
730__ha_cas_dw(void *target, void *compare, const void *set)
731{
732 char ret;
733
734 __asm __volatile("lock cmpxchg16b %0; setz %3"
735 : "+m" (*(void **)target),
736 "=a" (((void **)compare)[0]),
737 "=d" (((void **)compare)[1]),
738 "=q" (ret)
739 : "a" (((void **)compare)[0]),
740 "d" (((void **)compare)[1]),
741 "b" (((const void **)set)[0]),
742 "c" (((const void **)set)[1])
743 : "memory", "cc");
744 return (ret);
745}
746
747static __inline void
748__ha_barrier_load(void)
749{
750 __asm __volatile("lfence" ::: "memory");
751}
752
753static __inline void
754__ha_barrier_store(void)
755{
756 __asm __volatile("sfence" ::: "memory");
757}
758
759static __inline void
760__ha_barrier_full(void)
761{
762 __asm __volatile("mfence" ::: "memory");
763}
764
765#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
766#define HA_HAVE_CAS_DW 1
767static __inline void
768__ha_barrier_load(void)
769{
770 __asm __volatile("dmb" ::: "memory");
771}
772
773static __inline void
774__ha_barrier_store(void)
775{
776 __asm __volatile("dsb" ::: "memory");
777}
778
779static __inline void
780__ha_barrier_full(void)
781{
782 __asm __volatile("dmb" ::: "memory");
783}
784
Willy Tarreau41ccb192018-02-14 14:16:28 +0100785static __inline int __ha_cas_dw(void *target, void *compare, const void *set)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100786{
787 uint64_t previous;
788 int tmp;
789
790 __asm __volatile("1:"
791 "ldrexd %0, [%4];"
792 "cmp %Q0, %Q2;"
793 "ittt eq;"
794 "cmpeq %R0, %R2;"
795 "strexdeq %1, %3, [%4];"
796 "cmpeq %1, #1;"
797 "beq 1b;"
798 : "=&r" (previous), "=&r" (tmp)
Willy Tarreau41ccb192018-02-14 14:16:28 +0100799 : "r" (*(uint64_t *)compare), "r" (*(uint64_t *)set), "r" (target)
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100800 : "memory", "cc");
801 tmp = (previous == *(uint64_t *)compare);
802 *(uint64_t *)compare = previous;
803 return (tmp);
804}
805
806#elif defined (__aarch64__)
807#define HA_HAVE_CAS_DW 1
808#define HA_CAS_IS_8B
809
810static __inline void
811__ha_barrier_load(void)
812{
813 __asm __volatile("dmb ishld" ::: "memory");
814}
815
816static __inline void
817__ha_barrier_store(void)
818{
819 __asm __volatile("dmb ishst" ::: "memory");
820}
821
822static __inline void
823__ha_barrier_full(void)
824{
825 __asm __volatile("dmb ish" ::: "memory");
826}
827
828static __inline int __ha_cas_dw(void *target, void *compare, void *set)
829{
830 void *value[2];
831 uint64_t tmp1, tmp2;
832
833 __asm__ __volatile__("1:"
834 "ldxp %0, %1, [%4];"
835 "mov %2, %0;"
836 "mov %3, %1;"
837 "eor %0, %0, %5;"
838 "eor %1, %1, %6;"
839 "orr %1, %0, %1;"
840 "mov %w0, #0;"
841 "cbnz %1, 2f;"
842 "stxp %w0, %7, %8, [%4];"
843 "cbnz %w0, 1b;"
844 "mov %w0, #1;"
845 "2:"
846 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
847 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
848 : "cc", "memory");
849
850 memcpy(compare, &value, sizeof(value));
851 return (tmp1);
852}
853
854#else
855#define __ha_barrier_load __sync_synchronize
856#define __ha_barrier_store __sync_synchronize
857#define __ha_barrier_full __sync_synchronize
858#endif
859
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200860#endif /* USE_THREAD */
861
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100862static inline void __ha_compiler_barrier(void)
863{
864 __asm __volatile("" ::: "memory");
865}
866
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200867#endif /* _COMMON_HATHREADS_H */