blob: 0ec1ad8dc6dffcb6f2eceb24bdf8ce7530c46840 [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 FDCACHE_LOCK,
207 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200208 TASK_RQ_LOCK,
209 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200210 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200211 LISTENER_LOCK,
212 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200213 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200214 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200215 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200216 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200217 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200218 STK_TABLE_LOCK,
219 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200220 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200221 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200222 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200223 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200224 SSL_LOCK,
225 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200226 PATREF_LOCK,
227 PATEXP_LOCK,
228 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200229 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200230 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200231 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200232 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200233 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200234 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200235 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200236 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100237 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100238 START_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200239 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200240};
241struct lock_stat {
242 uint64_t nsec_wait_for_write;
243 uint64_t nsec_wait_for_read;
244 uint64_t num_write_locked;
245 uint64_t num_write_unlocked;
246 uint64_t num_read_locked;
247 uint64_t num_read_unlocked;
248};
249
250extern struct lock_stat lock_stats[LOCK_LABELS];
251
252#define __HA_SPINLOCK_T unsigned long
253
254#define __SPIN_INIT(l) ({ (*l) = 0; })
255#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100256#define __SPIN_LOCK(l) pl_take_s(l)
257#define __SPIN_TRYLOCK(l) !pl_try_s(l)
258#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200259
260#define __HA_RWLOCK_T unsigned long
261
262#define __RWLOCK_INIT(l) ({ (*l) = 0; })
263#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
264#define __RWLOCK_WRLOCK(l) pl_take_w(l)
265#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
266#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
267#define __RWLOCK_RDLOCK(l) pl_take_r(l)
268#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
269#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
270
271#define HA_SPINLOCK_T struct ha_spinlock
272
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100273#define HA_SPIN_INIT(l) __spin_init(l)
274#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200275
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100276#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
277#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
278#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200279
280#define HA_RWLOCK_T struct ha_rwlock
281
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100282#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
283#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
284#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
285#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
286#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
287#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
288#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
289#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200290
291struct ha_spinlock {
292 __HA_SPINLOCK_T lock;
293 struct {
294 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
295 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
296 struct {
297 const char *function;
298 const char *file;
299 int line;
300 } last_location; /* location of the last owner */
301 } info;
302};
303
304struct ha_rwlock {
305 __HA_RWLOCK_T lock;
306 struct {
307 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
308 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
309 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
310 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
311 struct {
312 const char *function;
313 const char *file;
314 int line;
315 } last_location; /* location of the last write owner */
316 } info;
317};
318
319static inline void show_lock_stats()
320{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200321 const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL",
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200322 "TASK_RQ", "TASK_WQ", "POOL",
Christopher Faulet29f77e82017-06-08 14:04:45 +0200323 "LISTENER", "LISTENER_QUEUE", "PROXY", "SERVER",
Emeric Brun1138fd02017-06-19 12:38:55 +0200324 "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS",
Emeric Brunb5997f72017-07-03 11:34:05 +0200325 "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS",
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200326 "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA",
Emeric Brund8b3b652017-11-07 11:19:48 +0100327 "NOTIF", "SPOE_APPLET", "DNS", "PID_LIST", "EMAIL_ALERTS",
328 "PIPES" };
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200329 int lbl;
330
331 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
332 fprintf(stderr,
333 "Stats about Lock %s: \n"
334 "\t # write lock : %lu\n"
335 "\t # write unlock: %lu (%ld)\n"
336 "\t # wait time for write : %.3f msec\n"
337 "\t # wait time for write/lock: %.3f nsec\n"
338 "\t # read lock : %lu\n"
339 "\t # read unlock : %lu (%ld)\n"
340 "\t # wait time for read : %.3f msec\n"
341 "\t # wait time for read/lock : %.3f nsec\n",
342 labels[lbl],
343 lock_stats[lbl].num_write_locked,
344 lock_stats[lbl].num_write_unlocked,
345 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
346 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
347 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
348 lock_stats[lbl].num_read_locked,
349 lock_stats[lbl].num_read_unlocked,
350 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
351 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
352 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
353 }
354}
355
356/* Following functions are used to collect some stats about locks. We wrap
357 * pthread functions to known how much time we wait in a lock. */
358
359static uint64_t nsec_now(void) {
360 struct timespec ts;
361
362 clock_gettime(CLOCK_MONOTONIC, &ts);
363 return ((uint64_t) ts.tv_sec * 1000000000ULL +
364 (uint64_t) ts.tv_nsec);
365}
366
367static inline void __ha_rwlock_init(struct ha_rwlock *l)
368{
369 memset(l, 0, sizeof(struct ha_rwlock));
370 __RWLOCK_INIT(&l->lock);
371}
372
373static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
374{
375 __RWLOCK_DESTROY(&l->lock);
376 memset(l, 0, sizeof(struct ha_rwlock));
377}
378
379
380static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
381 const char *func, const char *file, int line)
382{
383 uint64_t start_time;
384
385 if (unlikely(l->info.cur_writer & tid_bit)) {
386 /* the thread is already owning the lock for write */
387 abort();
388 }
389
390 if (unlikely(l->info.cur_readers & tid_bit)) {
391 /* the thread is already owning the lock for read */
392 abort();
393 }
394
395 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
396
397 start_time = nsec_now();
398 __RWLOCK_WRLOCK(&l->lock);
399 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
400
401 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
402
403 l->info.cur_writer = tid_bit;
404 l->info.last_location.function = func;
405 l->info.last_location.file = file;
406 l->info.last_location.line = line;
407
408 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
409}
410
411static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
412 const char *func, const char *file, int line)
413{
414 uint64_t start_time;
415 int r;
416
417 if (unlikely(l->info.cur_writer & tid_bit)) {
418 /* the thread is already owning the lock for write */
419 abort();
420 }
421
422 if (unlikely(l->info.cur_readers & tid_bit)) {
423 /* the thread is already owning the lock for read */
424 abort();
425 }
426
427 /* We set waiting writer because trywrlock could wait for readers to quit */
428 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
429
430 start_time = nsec_now();
431 r = __RWLOCK_TRYWRLOCK(&l->lock);
432 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
433 if (unlikely(r)) {
434 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
435 return r;
436 }
437 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
438
439 l->info.cur_writer = tid_bit;
440 l->info.last_location.function = func;
441 l->info.last_location.file = file;
442 l->info.last_location.line = line;
443
444 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
445
446 return 0;
447}
448
449static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
450 const char *func, const char *file, int line)
451{
452 if (unlikely(!(l->info.cur_writer & tid_bit))) {
453 /* the thread is not owning the lock for write */
454 abort();
455 }
456
457 l->info.cur_writer = 0;
458 l->info.last_location.function = func;
459 l->info.last_location.file = file;
460 l->info.last_location.line = line;
461
462 __RWLOCK_WRUNLOCK(&l->lock);
463
464 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
465}
466
467static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
468{
469 uint64_t start_time;
470
471 if (unlikely(l->info.cur_writer & tid_bit)) {
472 /* the thread is already owning the lock for write */
473 abort();
474 }
475
476 if (unlikely(l->info.cur_readers & tid_bit)) {
477 /* the thread is already owning the lock for read */
478 abort();
479 }
480
481 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
482
483 start_time = nsec_now();
484 __RWLOCK_RDLOCK(&l->lock);
485 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
486 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
487
488 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
489
490 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
491}
492
493static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
494{
495 int r;
496
497 if (unlikely(l->info.cur_writer & tid_bit)) {
498 /* the thread is already owning the lock for write */
499 abort();
500 }
501
502 if (unlikely(l->info.cur_readers & tid_bit)) {
503 /* the thread is already owning the lock for read */
504 abort();
505 }
506
507 /* try read should never wait */
508 r = __RWLOCK_TRYRDLOCK(&l->lock);
509 if (unlikely(r))
510 return r;
511 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
512
513 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
514
515 return 0;
516}
517
518static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
519{
520 if (unlikely(!(l->info.cur_readers & tid_bit))) {
521 /* the thread is not owning the lock for read */
522 abort();
523 }
524
525 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
526
527 __RWLOCK_RDUNLOCK(&l->lock);
528
529 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
530}
531
532static inline void __spin_init(struct ha_spinlock *l)
533{
534 memset(l, 0, sizeof(struct ha_spinlock));
535 __SPIN_INIT(&l->lock);
536}
537
538static inline void __spin_destroy(struct ha_spinlock *l)
539{
540 __SPIN_DESTROY(&l->lock);
541 memset(l, 0, sizeof(struct ha_spinlock));
542}
543
544static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
545 const char *func, const char *file, int line)
546{
547 uint64_t start_time;
548
549 if (unlikely(l->info.owner & tid_bit)) {
550 /* the thread is already owning the lock */
551 abort();
552 }
553
554 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
555
556 start_time = nsec_now();
557 __SPIN_LOCK(&l->lock);
558 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
559
560 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
561
562
563 l->info.owner = tid_bit;
564 l->info.last_location.function = func;
565 l->info.last_location.file = file;
566 l->info.last_location.line = line;
567
568 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
569}
570
571static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
572 const char *func, const char *file, int line)
573{
574 int r;
575
576 if (unlikely(l->info.owner & tid_bit)) {
577 /* the thread is already owning the lock */
578 abort();
579 }
580
581 /* try read should never wait */
582 r = __SPIN_TRYLOCK(&l->lock);
583 if (unlikely(r))
584 return r;
585 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
586
587 l->info.owner = tid_bit;
588 l->info.last_location.function = func;
589 l->info.last_location.file = file;
590 l->info.last_location.line = line;
591
592 return 0;
593}
594
595static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
596 const char *func, const char *file, int line)
597{
598 if (unlikely(!(l->info.owner & tid_bit))) {
599 /* the thread is not owning the lock */
600 abort();
601 }
602
603 l->info.owner = 0;
604 l->info.last_location.function = func;
605 l->info.last_location.file = file;
606 l->info.last_location.line = line;
607
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100608 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200609 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
610}
611
612#else /* DEBUG_THREAD */
613
614#define HA_SPINLOCK_T unsigned long
615
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100616#define HA_SPIN_INIT(l) ({ (*l) = 0; })
617#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
618#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
619#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
620#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200621
622#define HA_RWLOCK_T unsigned long
623
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100624#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
625#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
626#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
627#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
628#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
629#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
630#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
631#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200632
633#endif /* DEBUG_THREAD */
634
635#endif /* USE_THREAD */
636
637#endif /* _COMMON_HATHREADS_H */