blob: cc49b4983c92ffd982a80bdbdfc7b79a8fead65b [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 })
48#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
49#define HA_ATOMIC_UPDATE_MAX(val, new) \
50 ({ \
51 typeof(*(val)) __new = (new); \
52 \
53 if (*(val) < __new) \
54 *(val) = __new; \
55 *(val); \
56 })
57
58#define HA_ATOMIC_UPDATE_MIN(val, new) \
59 ({ \
60 typeof(*(val)) __new = (new); \
61 \
62 if (*(val) > __new) \
63 *(val) = __new; \
64 *(val); \
65 })
66
Willy Tarreaub29dc952017-10-31 18:00:20 +010067#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020068
69#define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0)
70#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
71#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
72#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
73#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
74#define THREAD_NO_SYNC() ({ 0; })
75#define THREAD_NEED_SYNC() ({ 1; })
76
Christopher Faulet2a944ee2017-11-07 10:42:54 +010077#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
78#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
79#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
80#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
81#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020082
Christopher Faulet2a944ee2017-11-07 10:42:54 +010083#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
84#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
85#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
86#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
87#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
88#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
89#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
90#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020091
Olivier Houchardf61f0cb2017-12-21 17:13:05 +010092static inline void __ha_barrier_load(void)
93{
94}
95
96static inline void __ha_barrier_store(void)
97{
98}
99
100static inline void __ha_barrier_full(void)
101{
102}
103
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200104#else /* USE_THREAD */
105
106#include <stdio.h>
107#include <stdlib.h>
108#include <string.h>
109#include <pthread.h>
110#include <import/plock.h>
111
Willy Tarreau421f02e2018-01-20 18:19:22 +0100112#define MAX_THREADS LONGBITS
113
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100114#define __decl_hathreads(decl) decl
115
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200116/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
117 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100118
David Carlierec5e8452018-01-11 14:20:43 +0000119#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100120/* gcc < 4.7 */
121
122#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
123#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
124#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
125#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
126
127/* the CAS is a bit complicated. The older API doesn't support returning the
128 * value and the swap's result at the same time. So here we take what looks
129 * like the safest route, consisting in using the boolean version guaranteeing
130 * that the operation was performed or not, and we snoop a previous value. If
131 * the compare succeeds, we return. If it fails, we return the previous value,
132 * but only if it differs from the expected one. If it's the same it's a race
133 * thus we try again to avoid confusing a possibly sensitive caller.
134 */
135#define HA_ATOMIC_CAS(val, old, new) \
136 ({ \
137 typeof((val)) __val = (val); \
138 typeof((old)) __oldp = (old); \
139 typeof(*(old)) __oldv; \
140 typeof((new)) __new = (new); \
141 int __ret; \
142 do { \
143 __oldv = *__val; \
144 __ret = __sync_bool_compare_and_swap(__val, *__oldp, __new); \
145 } while (!__ret && *__oldp == __oldv); \
146 if (!__ret) \
147 *__oldp = __oldv; \
148 __ret; \
149 })
150
151#define HA_ATOMIC_XCHG(val, new) \
152 ({ \
153 typeof((val)) __val = (val); \
154 typeof(*(val)) __old; \
155 typeof((new)) __new = (new); \
156 do { __old = *__val; \
157 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
158 __old; \
159 })
160#define HA_ATOMIC_STORE(val, new) \
161 ({ \
162 typeof((val)) __val = (val); \
163 typeof(*(val)) __old; \
164 typeof((new)) __new = (new); \
165 do { __old = *__val; \
166 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
167 })
168#else
169/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200170#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
171#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
172#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
173#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
174#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
175#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
176#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100177#endif
178
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200179#define HA_ATOMIC_UPDATE_MAX(val, new) \
180 ({ \
181 typeof(*(val)) __old = *(val); \
182 typeof(*(val)) __new = (new); \
183 \
184 while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
185 (*val); \
186 })
187#define HA_ATOMIC_UPDATE_MIN(val, new) \
188 ({ \
189 typeof((*val)) __old = *(val); \
190 typeof((*val)) __new = (new); \
191 \
192 while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
193 (*val); \
194 })
195
Willy Tarreaub29dc952017-10-31 18:00:20 +0100196#define HA_BARRIER() pl_barrier()
197
Christopher Faulet339fff82017-10-19 11:59:15 +0200198#define THREAD_SYNC_INIT(m) thread_sync_init(m)
199#define THREAD_SYNC_ENABLE() thread_sync_enable()
200#define THREAD_WANT_SYNC() thread_want_sync()
201#define THREAD_ENTER_SYNC() thread_enter_sync()
202#define THREAD_EXIT_SYNC() thread_exit_sync()
203#define THREAD_NO_SYNC() thread_no_sync()
204#define THREAD_NEED_SYNC() thread_need_sync()
205
206int thread_sync_init(unsigned long mask);
207void thread_sync_enable(void);
208void thread_want_sync(void);
209void thread_enter_sync(void);
210void thread_exit_sync(void);
211int thread_no_sync(void);
212int thread_need_sync(void);
213
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200214#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
215
Christopher Fauletf51bac22018-01-30 11:04:29 +0100216/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200217enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200218 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200219 FDCACHE_LOCK,
220 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200221 TASK_RQ_LOCK,
222 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200223 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200224 LISTENER_LOCK,
225 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200226 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200227 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200228 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200229 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200230 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200231 STK_TABLE_LOCK,
232 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200233 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200234 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200235 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200236 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200237 SSL_LOCK,
238 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200239 PATREF_LOCK,
240 PATEXP_LOCK,
241 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200242 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200243 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200244 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200245 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200246 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200247 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200248 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200249 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100250 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100251 START_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200252 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200253};
254struct lock_stat {
255 uint64_t nsec_wait_for_write;
256 uint64_t nsec_wait_for_read;
257 uint64_t num_write_locked;
258 uint64_t num_write_unlocked;
259 uint64_t num_read_locked;
260 uint64_t num_read_unlocked;
261};
262
263extern struct lock_stat lock_stats[LOCK_LABELS];
264
265#define __HA_SPINLOCK_T unsigned long
266
267#define __SPIN_INIT(l) ({ (*l) = 0; })
268#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100269#define __SPIN_LOCK(l) pl_take_s(l)
270#define __SPIN_TRYLOCK(l) !pl_try_s(l)
271#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200272
273#define __HA_RWLOCK_T unsigned long
274
275#define __RWLOCK_INIT(l) ({ (*l) = 0; })
276#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
277#define __RWLOCK_WRLOCK(l) pl_take_w(l)
278#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
279#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
280#define __RWLOCK_RDLOCK(l) pl_take_r(l)
281#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
282#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
283
284#define HA_SPINLOCK_T struct ha_spinlock
285
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100286#define HA_SPIN_INIT(l) __spin_init(l)
287#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200288
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100289#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
290#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
291#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200292
293#define HA_RWLOCK_T struct ha_rwlock
294
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100295#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
296#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
297#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
298#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
299#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
300#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
301#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
302#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200303
304struct ha_spinlock {
305 __HA_SPINLOCK_T lock;
306 struct {
307 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
308 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
309 struct {
310 const char *function;
311 const char *file;
312 int line;
313 } last_location; /* location of the last owner */
314 } info;
315};
316
317struct ha_rwlock {
318 __HA_RWLOCK_T lock;
319 struct {
320 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
321 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
322 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
323 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
324 struct {
325 const char *function;
326 const char *file;
327 int line;
328 } last_location; /* location of the last write owner */
329 } info;
330};
331
Christopher Fauletf51bac22018-01-30 11:04:29 +0100332static inline const char *lock_label(enum lock_label label)
333{
334 switch (label) {
335 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
336 case FDCACHE_LOCK: return "FDCACHE";
337 case FD_LOCK: return "FD";
338 case TASK_RQ_LOCK: return "TASK_RQ";
339 case TASK_WQ_LOCK: return "TASK_WQ";
340 case POOL_LOCK: return "POOL";
341 case LISTENER_LOCK: return "LISTENER";
342 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
343 case PROXY_LOCK: return "PROXY";
344 case SERVER_LOCK: return "SERVER";
345 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
346 case LBPRM_LOCK: return "LBPRM";
347 case SIGNALS_LOCK: return "SIGNALS";
348 case STK_TABLE_LOCK: return "STK_TABLE";
349 case STK_SESS_LOCK: return "STK_SESS";
350 case APPLETS_LOCK: return "APPLETS";
351 case PEER_LOCK: return "PEER";
352 case BUF_WQ_LOCK: return "BUF_WQ";
353 case STRMS_LOCK: return "STRMS";
354 case SSL_LOCK: return "SSL";
355 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
356 case PATREF_LOCK: return "PATREF";
357 case PATEXP_LOCK: return "PATEXP";
358 case PATLRU_LOCK: return "PATLRU";
359 case VARS_LOCK: return "VARS";
360 case COMP_POOL_LOCK: return "COMP_POOL";
361 case LUA_LOCK: return "LUA";
362 case NOTIF_LOCK: return "NOTIF";
363 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
364 case DNS_LOCK: return "DNS";
365 case PID_LIST_LOCK: return "PID_LIST";
366 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
367 case PIPES_LOCK: return "PIPES";
368 case START_LOCK: return "START";
369 case LOCK_LABELS: break; /* keep compiler happy */
370 };
371 /* only way to come here is consecutive to an internal bug */
372 abort();
373}
374
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200375static inline void show_lock_stats()
376{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200377 int lbl;
378
379 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
380 fprintf(stderr,
381 "Stats about Lock %s: \n"
382 "\t # write lock : %lu\n"
383 "\t # write unlock: %lu (%ld)\n"
384 "\t # wait time for write : %.3f msec\n"
385 "\t # wait time for write/lock: %.3f nsec\n"
386 "\t # read lock : %lu\n"
387 "\t # read unlock : %lu (%ld)\n"
388 "\t # wait time for read : %.3f msec\n"
389 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100390 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200391 lock_stats[lbl].num_write_locked,
392 lock_stats[lbl].num_write_unlocked,
393 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
394 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
395 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
396 lock_stats[lbl].num_read_locked,
397 lock_stats[lbl].num_read_unlocked,
398 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
399 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
400 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
401 }
402}
403
404/* Following functions are used to collect some stats about locks. We wrap
405 * pthread functions to known how much time we wait in a lock. */
406
407static uint64_t nsec_now(void) {
408 struct timespec ts;
409
410 clock_gettime(CLOCK_MONOTONIC, &ts);
411 return ((uint64_t) ts.tv_sec * 1000000000ULL +
412 (uint64_t) ts.tv_nsec);
413}
414
415static inline void __ha_rwlock_init(struct ha_rwlock *l)
416{
417 memset(l, 0, sizeof(struct ha_rwlock));
418 __RWLOCK_INIT(&l->lock);
419}
420
421static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
422{
423 __RWLOCK_DESTROY(&l->lock);
424 memset(l, 0, sizeof(struct ha_rwlock));
425}
426
427
428static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
429 const char *func, const char *file, int line)
430{
431 uint64_t start_time;
432
433 if (unlikely(l->info.cur_writer & tid_bit)) {
434 /* the thread is already owning the lock for write */
435 abort();
436 }
437
438 if (unlikely(l->info.cur_readers & tid_bit)) {
439 /* the thread is already owning the lock for read */
440 abort();
441 }
442
443 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
444
445 start_time = nsec_now();
446 __RWLOCK_WRLOCK(&l->lock);
447 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
448
449 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
450
451 l->info.cur_writer = tid_bit;
452 l->info.last_location.function = func;
453 l->info.last_location.file = file;
454 l->info.last_location.line = line;
455
456 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
457}
458
459static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
460 const char *func, const char *file, int line)
461{
462 uint64_t start_time;
463 int r;
464
465 if (unlikely(l->info.cur_writer & tid_bit)) {
466 /* the thread is already owning the lock for write */
467 abort();
468 }
469
470 if (unlikely(l->info.cur_readers & tid_bit)) {
471 /* the thread is already owning the lock for read */
472 abort();
473 }
474
475 /* We set waiting writer because trywrlock could wait for readers to quit */
476 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
477
478 start_time = nsec_now();
479 r = __RWLOCK_TRYWRLOCK(&l->lock);
480 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
481 if (unlikely(r)) {
482 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
483 return r;
484 }
485 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
486
487 l->info.cur_writer = tid_bit;
488 l->info.last_location.function = func;
489 l->info.last_location.file = file;
490 l->info.last_location.line = line;
491
492 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
493
494 return 0;
495}
496
497static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
498 const char *func, const char *file, int line)
499{
500 if (unlikely(!(l->info.cur_writer & tid_bit))) {
501 /* the thread is not owning the lock for write */
502 abort();
503 }
504
505 l->info.cur_writer = 0;
506 l->info.last_location.function = func;
507 l->info.last_location.file = file;
508 l->info.last_location.line = line;
509
510 __RWLOCK_WRUNLOCK(&l->lock);
511
512 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
513}
514
515static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
516{
517 uint64_t start_time;
518
519 if (unlikely(l->info.cur_writer & tid_bit)) {
520 /* the thread is already owning the lock for write */
521 abort();
522 }
523
524 if (unlikely(l->info.cur_readers & tid_bit)) {
525 /* the thread is already owning the lock for read */
526 abort();
527 }
528
529 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
530
531 start_time = nsec_now();
532 __RWLOCK_RDLOCK(&l->lock);
533 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
534 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
535
536 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
537
538 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
539}
540
541static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
542{
543 int r;
544
545 if (unlikely(l->info.cur_writer & tid_bit)) {
546 /* the thread is already owning the lock for write */
547 abort();
548 }
549
550 if (unlikely(l->info.cur_readers & tid_bit)) {
551 /* the thread is already owning the lock for read */
552 abort();
553 }
554
555 /* try read should never wait */
556 r = __RWLOCK_TRYRDLOCK(&l->lock);
557 if (unlikely(r))
558 return r;
559 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
560
561 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
562
563 return 0;
564}
565
566static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
567{
568 if (unlikely(!(l->info.cur_readers & tid_bit))) {
569 /* the thread is not owning the lock for read */
570 abort();
571 }
572
573 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
574
575 __RWLOCK_RDUNLOCK(&l->lock);
576
577 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
578}
579
580static inline void __spin_init(struct ha_spinlock *l)
581{
582 memset(l, 0, sizeof(struct ha_spinlock));
583 __SPIN_INIT(&l->lock);
584}
585
586static inline void __spin_destroy(struct ha_spinlock *l)
587{
588 __SPIN_DESTROY(&l->lock);
589 memset(l, 0, sizeof(struct ha_spinlock));
590}
591
592static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
593 const char *func, const char *file, int line)
594{
595 uint64_t start_time;
596
597 if (unlikely(l->info.owner & tid_bit)) {
598 /* the thread is already owning the lock */
599 abort();
600 }
601
602 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
603
604 start_time = nsec_now();
605 __SPIN_LOCK(&l->lock);
606 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
607
608 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
609
610
611 l->info.owner = tid_bit;
612 l->info.last_location.function = func;
613 l->info.last_location.file = file;
614 l->info.last_location.line = line;
615
616 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
617}
618
619static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
620 const char *func, const char *file, int line)
621{
622 int r;
623
624 if (unlikely(l->info.owner & tid_bit)) {
625 /* the thread is already owning the lock */
626 abort();
627 }
628
629 /* try read should never wait */
630 r = __SPIN_TRYLOCK(&l->lock);
631 if (unlikely(r))
632 return r;
633 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
634
635 l->info.owner = tid_bit;
636 l->info.last_location.function = func;
637 l->info.last_location.file = file;
638 l->info.last_location.line = line;
639
640 return 0;
641}
642
643static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
644 const char *func, const char *file, int line)
645{
646 if (unlikely(!(l->info.owner & tid_bit))) {
647 /* the thread is not owning the lock */
648 abort();
649 }
650
651 l->info.owner = 0;
652 l->info.last_location.function = func;
653 l->info.last_location.file = file;
654 l->info.last_location.line = line;
655
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100656 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200657 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
658}
659
660#else /* DEBUG_THREAD */
661
662#define HA_SPINLOCK_T unsigned long
663
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100664#define HA_SPIN_INIT(l) ({ (*l) = 0; })
665#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
666#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
667#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
668#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200669
670#define HA_RWLOCK_T unsigned long
671
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100672#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
673#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
674#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
675#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
676#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
677#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
678#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
679#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200680
681#endif /* DEBUG_THREAD */
682
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100683#ifdef __x86_64__
684#define HA_HAVE_CAS_DW 1
685#define HA_CAS_IS_8B
686static __inline int
687__ha_cas_dw(void *target, void *compare, const void *set)
688{
689 char ret;
690
691 __asm __volatile("lock cmpxchg16b %0; setz %3"
692 : "+m" (*(void **)target),
693 "=a" (((void **)compare)[0]),
694 "=d" (((void **)compare)[1]),
695 "=q" (ret)
696 : "a" (((void **)compare)[0]),
697 "d" (((void **)compare)[1]),
698 "b" (((const void **)set)[0]),
699 "c" (((const void **)set)[1])
700 : "memory", "cc");
701 return (ret);
702}
703
704static __inline void
705__ha_barrier_load(void)
706{
707 __asm __volatile("lfence" ::: "memory");
708}
709
710static __inline void
711__ha_barrier_store(void)
712{
713 __asm __volatile("sfence" ::: "memory");
714}
715
716static __inline void
717__ha_barrier_full(void)
718{
719 __asm __volatile("mfence" ::: "memory");
720}
721
722#elif defined(__arm__) && (defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__))
723#define HA_HAVE_CAS_DW 1
724static __inline void
725__ha_barrier_load(void)
726{
727 __asm __volatile("dmb" ::: "memory");
728}
729
730static __inline void
731__ha_barrier_store(void)
732{
733 __asm __volatile("dsb" ::: "memory");
734}
735
736static __inline void
737__ha_barrier_full(void)
738{
739 __asm __volatile("dmb" ::: "memory");
740}
741
742static __inline int __ha_cas_dw(void *target, void *compare, void *set)
743{
744 uint64_t previous;
745 int tmp;
746
747 __asm __volatile("1:"
748 "ldrexd %0, [%4];"
749 "cmp %Q0, %Q2;"
750 "ittt eq;"
751 "cmpeq %R0, %R2;"
752 "strexdeq %1, %3, [%4];"
753 "cmpeq %1, #1;"
754 "beq 1b;"
755 : "=&r" (previous), "=&r" (tmp)
756 : "r" (compare), "r" (set), "r" (target)
757 : "memory", "cc");
758 tmp = (previous == *(uint64_t *)compare);
759 *(uint64_t *)compare = previous;
760 return (tmp);
761}
762
763#elif defined (__aarch64__)
764#define HA_HAVE_CAS_DW 1
765#define HA_CAS_IS_8B
766
767static __inline void
768__ha_barrier_load(void)
769{
770 __asm __volatile("dmb ishld" ::: "memory");
771}
772
773static __inline void
774__ha_barrier_store(void)
775{
776 __asm __volatile("dmb ishst" ::: "memory");
777}
778
779static __inline void
780__ha_barrier_full(void)
781{
782 __asm __volatile("dmb ish" ::: "memory");
783}
784
785static __inline int __ha_cas_dw(void *target, void *compare, void *set)
786{
787 void *value[2];
788 uint64_t tmp1, tmp2;
789
790 __asm__ __volatile__("1:"
791 "ldxp %0, %1, [%4];"
792 "mov %2, %0;"
793 "mov %3, %1;"
794 "eor %0, %0, %5;"
795 "eor %1, %1, %6;"
796 "orr %1, %0, %1;"
797 "mov %w0, #0;"
798 "cbnz %1, 2f;"
799 "stxp %w0, %7, %8, [%4];"
800 "cbnz %w0, 1b;"
801 "mov %w0, #1;"
802 "2:"
803 : "=&r" (tmp1), "=&r" (tmp2), "=&r" (value[0]), "=&r" (value[1])
804 : "r" (target), "r" (((void **)(compare))[0]), "r" (((void **)(compare))[1]), "r" (((void **)(set))[0]), "r" (((void **)(set))[1])
805 : "cc", "memory");
806
807 memcpy(compare, &value, sizeof(value));
808 return (tmp1);
809}
810
811#else
812#define __ha_barrier_load __sync_synchronize
813#define __ha_barrier_store __sync_synchronize
814#define __ha_barrier_full __sync_synchronize
815#endif
816
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200817#endif /* USE_THREAD */
818
Olivier Houchardf61f0cb2017-12-21 17:13:05 +0100819static inline void __ha_compiler_barrier(void)
820{
821 __asm __volatile("" ::: "memory");
822}
823
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200824#endif /* _COMMON_HATHREADS_H */