blob: 260cc7e1eab258ba1b9893df52673a3cd4824349 [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
Christopher Fauletf51bac22018-01-30 11:04:29 +0100204/* WARNING!!! if you update this enum, please also keep lock_label() up to date below */
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200205enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200206 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200207 FDCACHE_LOCK,
208 FD_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200209 TASK_RQ_LOCK,
210 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200211 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200212 LISTENER_LOCK,
213 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200214 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200215 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200216 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200217 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200218 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200219 STK_TABLE_LOCK,
220 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200221 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200222 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200223 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200224 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200225 SSL_LOCK,
226 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200227 PATREF_LOCK,
228 PATEXP_LOCK,
229 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200230 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200231 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200232 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200233 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200234 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200235 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200236 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200237 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100238 PIPES_LOCK,
Willy Tarreau1605c7a2018-01-23 19:01:49 +0100239 START_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
Christopher Fauletf51bac22018-01-30 11:04:29 +0100320static inline const char *lock_label(enum lock_label label)
321{
322 switch (label) {
323 case THREAD_SYNC_LOCK: return "THREAD_SYNC";
324 case FDCACHE_LOCK: return "FDCACHE";
325 case FD_LOCK: return "FD";
326 case TASK_RQ_LOCK: return "TASK_RQ";
327 case TASK_WQ_LOCK: return "TASK_WQ";
328 case POOL_LOCK: return "POOL";
329 case LISTENER_LOCK: return "LISTENER";
330 case LISTENER_QUEUE_LOCK: return "LISTENER_QUEUE";
331 case PROXY_LOCK: return "PROXY";
332 case SERVER_LOCK: return "SERVER";
333 case UPDATED_SERVERS_LOCK: return "UPDATED_SERVERS";
334 case LBPRM_LOCK: return "LBPRM";
335 case SIGNALS_LOCK: return "SIGNALS";
336 case STK_TABLE_LOCK: return "STK_TABLE";
337 case STK_SESS_LOCK: return "STK_SESS";
338 case APPLETS_LOCK: return "APPLETS";
339 case PEER_LOCK: return "PEER";
340 case BUF_WQ_LOCK: return "BUF_WQ";
341 case STRMS_LOCK: return "STRMS";
342 case SSL_LOCK: return "SSL";
343 case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS";
344 case PATREF_LOCK: return "PATREF";
345 case PATEXP_LOCK: return "PATEXP";
346 case PATLRU_LOCK: return "PATLRU";
347 case VARS_LOCK: return "VARS";
348 case COMP_POOL_LOCK: return "COMP_POOL";
349 case LUA_LOCK: return "LUA";
350 case NOTIF_LOCK: return "NOTIF";
351 case SPOE_APPLET_LOCK: return "SPOE_APPLET";
352 case DNS_LOCK: return "DNS";
353 case PID_LIST_LOCK: return "PID_LIST";
354 case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS";
355 case PIPES_LOCK: return "PIPES";
356 case START_LOCK: return "START";
357 case LOCK_LABELS: break; /* keep compiler happy */
358 };
359 /* only way to come here is consecutive to an internal bug */
360 abort();
361}
362
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200363static inline void show_lock_stats()
364{
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200365 int lbl;
366
367 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
368 fprintf(stderr,
369 "Stats about Lock %s: \n"
370 "\t # write lock : %lu\n"
371 "\t # write unlock: %lu (%ld)\n"
372 "\t # wait time for write : %.3f msec\n"
373 "\t # wait time for write/lock: %.3f nsec\n"
374 "\t # read lock : %lu\n"
375 "\t # read unlock : %lu (%ld)\n"
376 "\t # wait time for read : %.3f msec\n"
377 "\t # wait time for read/lock : %.3f nsec\n",
Christopher Fauletf51bac22018-01-30 11:04:29 +0100378 lock_label(lbl),
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200379 lock_stats[lbl].num_write_locked,
380 lock_stats[lbl].num_write_unlocked,
381 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
382 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
383 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
384 lock_stats[lbl].num_read_locked,
385 lock_stats[lbl].num_read_unlocked,
386 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
387 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
388 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
389 }
390}
391
392/* Following functions are used to collect some stats about locks. We wrap
393 * pthread functions to known how much time we wait in a lock. */
394
395static uint64_t nsec_now(void) {
396 struct timespec ts;
397
398 clock_gettime(CLOCK_MONOTONIC, &ts);
399 return ((uint64_t) ts.tv_sec * 1000000000ULL +
400 (uint64_t) ts.tv_nsec);
401}
402
403static inline void __ha_rwlock_init(struct ha_rwlock *l)
404{
405 memset(l, 0, sizeof(struct ha_rwlock));
406 __RWLOCK_INIT(&l->lock);
407}
408
409static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
410{
411 __RWLOCK_DESTROY(&l->lock);
412 memset(l, 0, sizeof(struct ha_rwlock));
413}
414
415
416static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
417 const char *func, const char *file, int line)
418{
419 uint64_t start_time;
420
421 if (unlikely(l->info.cur_writer & tid_bit)) {
422 /* the thread is already owning the lock for write */
423 abort();
424 }
425
426 if (unlikely(l->info.cur_readers & tid_bit)) {
427 /* the thread is already owning the lock for read */
428 abort();
429 }
430
431 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
432
433 start_time = nsec_now();
434 __RWLOCK_WRLOCK(&l->lock);
435 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
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
447static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
448 const char *func, const char *file, int line)
449{
450 uint64_t start_time;
451 int r;
452
453 if (unlikely(l->info.cur_writer & tid_bit)) {
454 /* the thread is already owning the lock for write */
455 abort();
456 }
457
458 if (unlikely(l->info.cur_readers & tid_bit)) {
459 /* the thread is already owning the lock for read */
460 abort();
461 }
462
463 /* We set waiting writer because trywrlock could wait for readers to quit */
464 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
465
466 start_time = nsec_now();
467 r = __RWLOCK_TRYWRLOCK(&l->lock);
468 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
469 if (unlikely(r)) {
470 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
471 return r;
472 }
473 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
474
475 l->info.cur_writer = tid_bit;
476 l->info.last_location.function = func;
477 l->info.last_location.file = file;
478 l->info.last_location.line = line;
479
480 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
481
482 return 0;
483}
484
485static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
486 const char *func, const char *file, int line)
487{
488 if (unlikely(!(l->info.cur_writer & tid_bit))) {
489 /* the thread is not owning the lock for write */
490 abort();
491 }
492
493 l->info.cur_writer = 0;
494 l->info.last_location.function = func;
495 l->info.last_location.file = file;
496 l->info.last_location.line = line;
497
498 __RWLOCK_WRUNLOCK(&l->lock);
499
500 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
501}
502
503static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
504{
505 uint64_t start_time;
506
507 if (unlikely(l->info.cur_writer & tid_bit)) {
508 /* the thread is already owning the lock for write */
509 abort();
510 }
511
512 if (unlikely(l->info.cur_readers & tid_bit)) {
513 /* the thread is already owning the lock for read */
514 abort();
515 }
516
517 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
518
519 start_time = nsec_now();
520 __RWLOCK_RDLOCK(&l->lock);
521 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
522 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
523
524 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
525
526 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
527}
528
529static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
530{
531 int r;
532
533 if (unlikely(l->info.cur_writer & tid_bit)) {
534 /* the thread is already owning the lock for write */
535 abort();
536 }
537
538 if (unlikely(l->info.cur_readers & tid_bit)) {
539 /* the thread is already owning the lock for read */
540 abort();
541 }
542
543 /* try read should never wait */
544 r = __RWLOCK_TRYRDLOCK(&l->lock);
545 if (unlikely(r))
546 return r;
547 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
548
549 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
550
551 return 0;
552}
553
554static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
555{
556 if (unlikely(!(l->info.cur_readers & tid_bit))) {
557 /* the thread is not owning the lock for read */
558 abort();
559 }
560
561 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
562
563 __RWLOCK_RDUNLOCK(&l->lock);
564
565 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
566}
567
568static inline void __spin_init(struct ha_spinlock *l)
569{
570 memset(l, 0, sizeof(struct ha_spinlock));
571 __SPIN_INIT(&l->lock);
572}
573
574static inline void __spin_destroy(struct ha_spinlock *l)
575{
576 __SPIN_DESTROY(&l->lock);
577 memset(l, 0, sizeof(struct ha_spinlock));
578}
579
580static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
581 const char *func, const char *file, int line)
582{
583 uint64_t start_time;
584
585 if (unlikely(l->info.owner & tid_bit)) {
586 /* the thread is already owning the lock */
587 abort();
588 }
589
590 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
591
592 start_time = nsec_now();
593 __SPIN_LOCK(&l->lock);
594 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
595
596 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
597
598
599 l->info.owner = tid_bit;
600 l->info.last_location.function = func;
601 l->info.last_location.file = file;
602 l->info.last_location.line = line;
603
604 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
605}
606
607static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
608 const char *func, const char *file, int line)
609{
610 int r;
611
612 if (unlikely(l->info.owner & tid_bit)) {
613 /* the thread is already owning the lock */
614 abort();
615 }
616
617 /* try read should never wait */
618 r = __SPIN_TRYLOCK(&l->lock);
619 if (unlikely(r))
620 return r;
621 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
622
623 l->info.owner = tid_bit;
624 l->info.last_location.function = func;
625 l->info.last_location.file = file;
626 l->info.last_location.line = line;
627
628 return 0;
629}
630
631static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
632 const char *func, const char *file, int line)
633{
634 if (unlikely(!(l->info.owner & tid_bit))) {
635 /* the thread is not owning the lock */
636 abort();
637 }
638
639 l->info.owner = 0;
640 l->info.last_location.function = func;
641 l->info.last_location.file = file;
642 l->info.last_location.line = line;
643
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100644 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200645 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
646}
647
648#else /* DEBUG_THREAD */
649
650#define HA_SPINLOCK_T unsigned long
651
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100652#define HA_SPIN_INIT(l) ({ (*l) = 0; })
653#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
654#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
655#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
656#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200657
658#define HA_RWLOCK_T unsigned long
659
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100660#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
661#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
662#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
663#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
664#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
665#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
666#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
667#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200668
669#endif /* DEBUG_THREAD */
670
671#endif /* USE_THREAD */
672
673#endif /* _COMMON_HATHREADS_H */