blob: f8195368620cbf0cba28de6d00ede8ee0b50f610 [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
92#else /* USE_THREAD */
93
94#include <stdio.h>
95#include <stdlib.h>
96#include <string.h>
97#include <pthread.h>
98#include <import/plock.h>
99
Willy Tarreau421f02e2018-01-20 18:19:22 +0100100#define MAX_THREADS LONGBITS
101
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100102#define __decl_hathreads(decl) decl
103
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200104/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
105 * have a header file regrouping all functions dealing with threads. */
Willy Tarreau1a69af62018-01-04 18:49:31 +0100106
David Carlierec5e8452018-01-11 14:20:43 +0000107#if defined(__GNUC__) && (__GNUC__ < 4 || __GNUC__ == 4 && __GNUC_MINOR__ < 7) && !defined(__clang__)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100108/* gcc < 4.7 */
109
110#define HA_ATOMIC_ADD(val, i) __sync_add_and_fetch(val, i)
111#define HA_ATOMIC_SUB(val, i) __sync_sub_and_fetch(val, i)
112#define HA_ATOMIC_AND(val, flags) __sync_and_and_fetch(val, flags)
113#define HA_ATOMIC_OR(val, flags) __sync_or_and_fetch(val, flags)
114
115/* the CAS is a bit complicated. The older API doesn't support returning the
116 * value and the swap's result at the same time. So here we take what looks
117 * like the safest route, consisting in using the boolean version guaranteeing
118 * that the operation was performed or not, and we snoop a previous value. If
119 * the compare succeeds, we return. If it fails, we return the previous value,
120 * but only if it differs from the expected one. If it's the same it's a race
121 * thus we try again to avoid confusing a possibly sensitive caller.
122 */
123#define HA_ATOMIC_CAS(val, old, new) \
124 ({ \
125 typeof((val)) __val = (val); \
126 typeof((old)) __oldp = (old); \
127 typeof(*(old)) __oldv; \
128 typeof((new)) __new = (new); \
129 int __ret; \
130 do { \
131 __oldv = *__val; \
132 __ret = __sync_bool_compare_and_swap(__val, *__oldp, __new); \
133 } while (!__ret && *__oldp == __oldv); \
134 if (!__ret) \
135 *__oldp = __oldv; \
136 __ret; \
137 })
138
139#define HA_ATOMIC_XCHG(val, new) \
140 ({ \
141 typeof((val)) __val = (val); \
142 typeof(*(val)) __old; \
143 typeof((new)) __new = (new); \
144 do { __old = *__val; \
145 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
146 __old; \
147 })
148#define HA_ATOMIC_STORE(val, new) \
149 ({ \
150 typeof((val)) __val = (val); \
151 typeof(*(val)) __old; \
152 typeof((new)) __new = (new); \
153 do { __old = *__val; \
154 } while (!__sync_bool_compare_and_swap(__val, __old, __new)); \
155 })
156#else
157/* gcc >= 4.7 */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200158#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
159#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
160#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
161#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
162#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
163#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
164#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
Willy Tarreau1a69af62018-01-04 18:49:31 +0100165#endif
166
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200167#define HA_ATOMIC_UPDATE_MAX(val, new) \
168 ({ \
169 typeof(*(val)) __old = *(val); \
170 typeof(*(val)) __new = (new); \
171 \
172 while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
173 (*val); \
174 })
175#define HA_ATOMIC_UPDATE_MIN(val, new) \
176 ({ \
177 typeof((*val)) __old = *(val); \
178 typeof((*val)) __new = (new); \
179 \
180 while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
181 (*val); \
182 })
183
Willy Tarreaub29dc952017-10-31 18:00:20 +0100184#define HA_BARRIER() pl_barrier()
185
Christopher Faulet339fff82017-10-19 11:59:15 +0200186#define THREAD_SYNC_INIT(m) thread_sync_init(m)
187#define THREAD_SYNC_ENABLE() thread_sync_enable()
188#define THREAD_WANT_SYNC() thread_want_sync()
189#define THREAD_ENTER_SYNC() thread_enter_sync()
190#define THREAD_EXIT_SYNC() thread_exit_sync()
191#define THREAD_NO_SYNC() thread_no_sync()
192#define THREAD_NEED_SYNC() thread_need_sync()
193
194int thread_sync_init(unsigned long mask);
195void thread_sync_enable(void);
196void thread_want_sync(void);
197void thread_enter_sync(void);
198void thread_exit_sync(void);
199int thread_no_sync(void);
200int thread_need_sync(void);
201
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200202#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
203
204enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200205 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200206 FDTAB_LOCK,
207 FDCACHE_LOCK,
208 FD_LOCK,
209 POLL_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200210 TASK_RQ_LOCK,
211 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200212 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200213 LISTENER_LOCK,
214 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200215 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200216 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200217 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200218 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200219 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200220 STK_TABLE_LOCK,
221 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200222 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200223 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200224 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200225 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200226 SSL_LOCK,
227 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200228 PATREF_LOCK,
229 PATEXP_LOCK,
230 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200231 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200232 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200233 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200234 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200235 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200236 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200237 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200238 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100239 PIPES_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200240 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200241};
242struct lock_stat {
243 uint64_t nsec_wait_for_write;
244 uint64_t nsec_wait_for_read;
245 uint64_t num_write_locked;
246 uint64_t num_write_unlocked;
247 uint64_t num_read_locked;
248 uint64_t num_read_unlocked;
249};
250
251extern struct lock_stat lock_stats[LOCK_LABELS];
252
253#define __HA_SPINLOCK_T unsigned long
254
255#define __SPIN_INIT(l) ({ (*l) = 0; })
256#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100257#define __SPIN_LOCK(l) pl_take_s(l)
258#define __SPIN_TRYLOCK(l) !pl_try_s(l)
259#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200260
261#define __HA_RWLOCK_T unsigned long
262
263#define __RWLOCK_INIT(l) ({ (*l) = 0; })
264#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
265#define __RWLOCK_WRLOCK(l) pl_take_w(l)
266#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
267#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
268#define __RWLOCK_RDLOCK(l) pl_take_r(l)
269#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
270#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
271
272#define HA_SPINLOCK_T struct ha_spinlock
273
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100274#define HA_SPIN_INIT(l) __spin_init(l)
275#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200276
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100277#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
278#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
279#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200280
281#define HA_RWLOCK_T struct ha_rwlock
282
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100283#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
284#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
285#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
286#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
287#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
288#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
289#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
290#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200291
292struct ha_spinlock {
293 __HA_SPINLOCK_T lock;
294 struct {
295 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
296 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
297 struct {
298 const char *function;
299 const char *file;
300 int line;
301 } last_location; /* location of the last owner */
302 } info;
303};
304
305struct ha_rwlock {
306 __HA_RWLOCK_T lock;
307 struct {
308 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
309 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
310 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
311 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
312 struct {
313 const char *function;
314 const char *file;
315 int line;
316 } last_location; /* location of the last write owner */
317 } info;
318};
319
320static inline void show_lock_stats()
321{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200322 const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL",
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200323 "TASK_RQ", "TASK_WQ", "POOL",
Christopher Faulet29f77e82017-06-08 14:04:45 +0200324 "LISTENER", "LISTENER_QUEUE", "PROXY", "SERVER",
Emeric Brun1138fd02017-06-19 12:38:55 +0200325 "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS",
Emeric Brunb5997f72017-07-03 11:34:05 +0200326 "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS",
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200327 "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA",
Emeric Brund8b3b652017-11-07 11:19:48 +0100328 "NOTIF", "SPOE_APPLET", "DNS", "PID_LIST", "EMAIL_ALERTS",
329 "PIPES" };
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200330 int lbl;
331
332 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
333 fprintf(stderr,
334 "Stats about Lock %s: \n"
335 "\t # write lock : %lu\n"
336 "\t # write unlock: %lu (%ld)\n"
337 "\t # wait time for write : %.3f msec\n"
338 "\t # wait time for write/lock: %.3f nsec\n"
339 "\t # read lock : %lu\n"
340 "\t # read unlock : %lu (%ld)\n"
341 "\t # wait time for read : %.3f msec\n"
342 "\t # wait time for read/lock : %.3f nsec\n",
343 labels[lbl],
344 lock_stats[lbl].num_write_locked,
345 lock_stats[lbl].num_write_unlocked,
346 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
347 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
348 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
349 lock_stats[lbl].num_read_locked,
350 lock_stats[lbl].num_read_unlocked,
351 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
352 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
353 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
354 }
355}
356
357/* Following functions are used to collect some stats about locks. We wrap
358 * pthread functions to known how much time we wait in a lock. */
359
360static uint64_t nsec_now(void) {
361 struct timespec ts;
362
363 clock_gettime(CLOCK_MONOTONIC, &ts);
364 return ((uint64_t) ts.tv_sec * 1000000000ULL +
365 (uint64_t) ts.tv_nsec);
366}
367
368static inline void __ha_rwlock_init(struct ha_rwlock *l)
369{
370 memset(l, 0, sizeof(struct ha_rwlock));
371 __RWLOCK_INIT(&l->lock);
372}
373
374static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
375{
376 __RWLOCK_DESTROY(&l->lock);
377 memset(l, 0, sizeof(struct ha_rwlock));
378}
379
380
381static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
382 const char *func, const char *file, int line)
383{
384 uint64_t start_time;
385
386 if (unlikely(l->info.cur_writer & tid_bit)) {
387 /* the thread is already owning the lock for write */
388 abort();
389 }
390
391 if (unlikely(l->info.cur_readers & tid_bit)) {
392 /* the thread is already owning the lock for read */
393 abort();
394 }
395
396 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
397
398 start_time = nsec_now();
399 __RWLOCK_WRLOCK(&l->lock);
400 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
401
402 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
403
404 l->info.cur_writer = tid_bit;
405 l->info.last_location.function = func;
406 l->info.last_location.file = file;
407 l->info.last_location.line = line;
408
409 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
410}
411
412static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
413 const char *func, const char *file, int line)
414{
415 uint64_t start_time;
416 int r;
417
418 if (unlikely(l->info.cur_writer & tid_bit)) {
419 /* the thread is already owning the lock for write */
420 abort();
421 }
422
423 if (unlikely(l->info.cur_readers & tid_bit)) {
424 /* the thread is already owning the lock for read */
425 abort();
426 }
427
428 /* We set waiting writer because trywrlock could wait for readers to quit */
429 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
430
431 start_time = nsec_now();
432 r = __RWLOCK_TRYWRLOCK(&l->lock);
433 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
434 if (unlikely(r)) {
435 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
436 return r;
437 }
438 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
439
440 l->info.cur_writer = tid_bit;
441 l->info.last_location.function = func;
442 l->info.last_location.file = file;
443 l->info.last_location.line = line;
444
445 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
446
447 return 0;
448}
449
450static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
451 const char *func, const char *file, int line)
452{
453 if (unlikely(!(l->info.cur_writer & tid_bit))) {
454 /* the thread is not owning the lock for write */
455 abort();
456 }
457
458 l->info.cur_writer = 0;
459 l->info.last_location.function = func;
460 l->info.last_location.file = file;
461 l->info.last_location.line = line;
462
463 __RWLOCK_WRUNLOCK(&l->lock);
464
465 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
466}
467
468static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
469{
470 uint64_t start_time;
471
472 if (unlikely(l->info.cur_writer & tid_bit)) {
473 /* the thread is already owning the lock for write */
474 abort();
475 }
476
477 if (unlikely(l->info.cur_readers & tid_bit)) {
478 /* the thread is already owning the lock for read */
479 abort();
480 }
481
482 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
483
484 start_time = nsec_now();
485 __RWLOCK_RDLOCK(&l->lock);
486 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
487 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
488
489 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
490
491 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
492}
493
494static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
495{
496 int r;
497
498 if (unlikely(l->info.cur_writer & tid_bit)) {
499 /* the thread is already owning the lock for write */
500 abort();
501 }
502
503 if (unlikely(l->info.cur_readers & tid_bit)) {
504 /* the thread is already owning the lock for read */
505 abort();
506 }
507
508 /* try read should never wait */
509 r = __RWLOCK_TRYRDLOCK(&l->lock);
510 if (unlikely(r))
511 return r;
512 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
513
514 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
515
516 return 0;
517}
518
519static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
520{
521 if (unlikely(!(l->info.cur_readers & tid_bit))) {
522 /* the thread is not owning the lock for read */
523 abort();
524 }
525
526 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
527
528 __RWLOCK_RDUNLOCK(&l->lock);
529
530 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
531}
532
533static inline void __spin_init(struct ha_spinlock *l)
534{
535 memset(l, 0, sizeof(struct ha_spinlock));
536 __SPIN_INIT(&l->lock);
537}
538
539static inline void __spin_destroy(struct ha_spinlock *l)
540{
541 __SPIN_DESTROY(&l->lock);
542 memset(l, 0, sizeof(struct ha_spinlock));
543}
544
545static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
546 const char *func, const char *file, int line)
547{
548 uint64_t start_time;
549
550 if (unlikely(l->info.owner & tid_bit)) {
551 /* the thread is already owning the lock */
552 abort();
553 }
554
555 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
556
557 start_time = nsec_now();
558 __SPIN_LOCK(&l->lock);
559 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
560
561 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
562
563
564 l->info.owner = tid_bit;
565 l->info.last_location.function = func;
566 l->info.last_location.file = file;
567 l->info.last_location.line = line;
568
569 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
570}
571
572static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
573 const char *func, const char *file, int line)
574{
575 int r;
576
577 if (unlikely(l->info.owner & tid_bit)) {
578 /* the thread is already owning the lock */
579 abort();
580 }
581
582 /* try read should never wait */
583 r = __SPIN_TRYLOCK(&l->lock);
584 if (unlikely(r))
585 return r;
586 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
587
588 l->info.owner = tid_bit;
589 l->info.last_location.function = func;
590 l->info.last_location.file = file;
591 l->info.last_location.line = line;
592
593 return 0;
594}
595
596static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
597 const char *func, const char *file, int line)
598{
599 if (unlikely(!(l->info.owner & tid_bit))) {
600 /* the thread is not owning the lock */
601 abort();
602 }
603
604 l->info.owner = 0;
605 l->info.last_location.function = func;
606 l->info.last_location.file = file;
607 l->info.last_location.line = line;
608
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100609 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200610 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
611}
612
613#else /* DEBUG_THREAD */
614
615#define HA_SPINLOCK_T unsigned long
616
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100617#define HA_SPIN_INIT(l) ({ (*l) = 0; })
618#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
619#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
620#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
621#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200622
623#define HA_RWLOCK_T unsigned long
624
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100625#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
626#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
627#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
628#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
629#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
630#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
631#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
632#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200633
634#endif /* DEBUG_THREAD */
635
636#endif /* USE_THREAD */
637
638#endif /* _COMMON_HATHREADS_H */