blob: 3b8fb0bd844f134df07f68d8374095d40c2294e7 [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 */
29extern THREAD_LOCAL unsigned int tid_bit; /* The bit corresponding to the thread id */
30
31#ifndef USE_THREAD
32
33#define HA_ATOMIC_CAS(val, old, new) ({((*val) == (*old)) ? (*(val) = (new) , 1) : (*(old) = *(val), 0);})
34#define HA_ATOMIC_ADD(val, i) ({*(val) += (i);})
35#define HA_ATOMIC_SUB(val, i) ({*(val) -= (i);})
36#define HA_ATOMIC_AND(val, flags) ({*(val) &= (flags);})
37#define HA_ATOMIC_OR(val, flags) ({*(val) |= (flags);})
38#define HA_ATOMIC_XCHG(val, new) \
39 ({ \
40 typeof(*(val)) __old = *(val); \
41 *(val) = new; \
42 __old; \
43 })
44#define HA_ATOMIC_STORE(val, new) ({*(val) = new;})
45#define HA_ATOMIC_UPDATE_MAX(val, new) \
46 ({ \
47 typeof(*(val)) __new = (new); \
48 \
49 if (*(val) < __new) \
50 *(val) = __new; \
51 *(val); \
52 })
53
54#define HA_ATOMIC_UPDATE_MIN(val, new) \
55 ({ \
56 typeof(*(val)) __new = (new); \
57 \
58 if (*(val) > __new) \
59 *(val) = __new; \
60 *(val); \
61 })
62
Willy Tarreaub29dc952017-10-31 18:00:20 +010063#define HA_BARRIER() do { } while (0)
Christopher Faulet339fff82017-10-19 11:59:15 +020064
65#define THREAD_SYNC_INIT(m) do { /* do nothing */ } while(0)
66#define THREAD_SYNC_ENABLE() do { /* do nothing */ } while(0)
67#define THREAD_WANT_SYNC() do { /* do nothing */ } while(0)
68#define THREAD_ENTER_SYNC() do { /* do nothing */ } while(0)
69#define THREAD_EXIT_SYNC() do { /* do nothing */ } while(0)
70#define THREAD_NO_SYNC() ({ 0; })
71#define THREAD_NEED_SYNC() ({ 1; })
72
Christopher Faulet2a944ee2017-11-07 10:42:54 +010073#define HA_SPIN_INIT(l) do { /* do nothing */ } while(0)
74#define HA_SPIN_DESTROY(l) do { /* do nothing */ } while(0)
75#define HA_SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
76#define HA_SPIN_TRYLOCK(lbl, l) ({ 0; })
77#define HA_SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020078
Christopher Faulet2a944ee2017-11-07 10:42:54 +010079#define HA_RWLOCK_INIT(l) do { /* do nothing */ } while(0)
80#define HA_RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
81#define HA_RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
82#define HA_RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
83#define HA_RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
84#define HA_RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
85#define HA_RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
86#define HA_RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +020087
88#else /* USE_THREAD */
89
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <pthread.h>
94#include <import/plock.h>
95
96/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
97 * have a header file regrouping all functions dealing with threads. */
98#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
99#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
100#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
101#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
102#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
103#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
104#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
105#define HA_ATOMIC_UPDATE_MAX(val, new) \
106 ({ \
107 typeof(*(val)) __old = *(val); \
108 typeof(*(val)) __new = (new); \
109 \
110 while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
111 (*val); \
112 })
113#define HA_ATOMIC_UPDATE_MIN(val, new) \
114 ({ \
115 typeof((*val)) __old = *(val); \
116 typeof((*val)) __new = (new); \
117 \
118 while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
119 (*val); \
120 })
121
Willy Tarreaub29dc952017-10-31 18:00:20 +0100122#define HA_BARRIER() pl_barrier()
123
Christopher Faulet339fff82017-10-19 11:59:15 +0200124#define THREAD_SYNC_INIT(m) thread_sync_init(m)
125#define THREAD_SYNC_ENABLE() thread_sync_enable()
126#define THREAD_WANT_SYNC() thread_want_sync()
127#define THREAD_ENTER_SYNC() thread_enter_sync()
128#define THREAD_EXIT_SYNC() thread_exit_sync()
129#define THREAD_NO_SYNC() thread_no_sync()
130#define THREAD_NEED_SYNC() thread_need_sync()
131
132int thread_sync_init(unsigned long mask);
133void thread_sync_enable(void);
134void thread_want_sync(void);
135void thread_enter_sync(void);
136void thread_exit_sync(void);
137int thread_no_sync(void);
138int thread_need_sync(void);
139
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200140#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
141
142enum lock_label {
Christopher Faulet339fff82017-10-19 11:59:15 +0200143 THREAD_SYNC_LOCK = 0,
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200144 FDTAB_LOCK,
145 FDCACHE_LOCK,
146 FD_LOCK,
147 POLL_LOCK,
Emeric Brunc60def82017-09-27 14:59:38 +0200148 TASK_RQ_LOCK,
149 TASK_WQ_LOCK,
Christopher Fauletb349e482017-08-29 09:52:38 +0200150 POOL_LOCK,
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200151 LISTENER_LOCK,
152 LISTENER_QUEUE_LOCK,
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200153 PROXY_LOCK,
Christopher Faulet29f77e82017-06-08 14:04:45 +0200154 SERVER_LOCK,
Christopher Faulet5d42e092017-10-16 12:00:40 +0200155 UPDATED_SERVERS_LOCK,
Christopher Faulet5b517552017-06-09 14:17:53 +0200156 LBPRM_LOCK,
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200157 SIGNALS_LOCK,
Emeric Brun819fc6f2017-06-13 19:37:32 +0200158 STK_TABLE_LOCK,
159 STK_SESS_LOCK,
Emeric Brun1138fd02017-06-19 12:38:55 +0200160 APPLETS_LOCK,
Emeric Brun80527f52017-06-19 17:46:37 +0200161 PEER_LOCK,
Emeric Bruna1dd2432017-06-21 15:42:52 +0200162 BUF_WQ_LOCK,
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200163 STRMS_LOCK,
Emeric Brun821bb9b2017-06-15 16:37:39 +0200164 SSL_LOCK,
165 SSL_GEN_CERTS_LOCK,
Emeric Brunb5997f72017-07-03 11:34:05 +0200166 PATREF_LOCK,
167 PATEXP_LOCK,
168 PATLRU_LOCK,
Christopher Faulete95f2c32017-07-24 16:30:34 +0200169 VARS_LOCK,
Christopher Faulet8ca3b4b2017-07-25 11:07:15 +0200170 COMP_POOL_LOCK,
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200171 LUA_LOCK,
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200172 NOTIF_LOCK,
Christopher Faulet24289f22017-09-25 14:48:02 +0200173 SPOE_APPLET_LOCK,
Christopher Fauletb2812a62017-10-04 16:17:58 +0200174 DNS_LOCK,
Christopher Fauletcfda8472017-10-20 15:40:23 +0200175 PID_LIST_LOCK,
Christopher Fauletc2a89a62017-10-23 15:54:24 +0200176 EMAIL_ALERTS_LOCK,
Emeric Brund8b3b652017-11-07 11:19:48 +0100177 PIPES_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200178 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200179};
180struct lock_stat {
181 uint64_t nsec_wait_for_write;
182 uint64_t nsec_wait_for_read;
183 uint64_t num_write_locked;
184 uint64_t num_write_unlocked;
185 uint64_t num_read_locked;
186 uint64_t num_read_unlocked;
187};
188
189extern struct lock_stat lock_stats[LOCK_LABELS];
190
191#define __HA_SPINLOCK_T unsigned long
192
193#define __SPIN_INIT(l) ({ (*l) = 0; })
194#define __SPIN_DESTROY(l) ({ (*l) = 0; })
Willy Tarreau88ac59b2017-11-06 01:03:26 +0100195#define __SPIN_LOCK(l) pl_take_s(l)
196#define __SPIN_TRYLOCK(l) !pl_try_s(l)
197#define __SPIN_UNLOCK(l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200198
199#define __HA_RWLOCK_T unsigned long
200
201#define __RWLOCK_INIT(l) ({ (*l) = 0; })
202#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
203#define __RWLOCK_WRLOCK(l) pl_take_w(l)
204#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
205#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
206#define __RWLOCK_RDLOCK(l) pl_take_r(l)
207#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
208#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
209
210#define HA_SPINLOCK_T struct ha_spinlock
211
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100212#define HA_SPIN_INIT(l) __spin_init(l)
213#define HA_SPIN_DESTROY(l) __spin_destroy(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200214
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100215#define HA_SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
216#define HA_SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
217#define HA_SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200218
219#define HA_RWLOCK_T struct ha_rwlock
220
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100221#define HA_RWLOCK_INIT(l) __ha_rwlock_init((l))
222#define HA_RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
223#define HA_RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
224#define HA_RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
225#define HA_RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
226#define HA_RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
227#define HA_RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
228#define HA_RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200229
230struct ha_spinlock {
231 __HA_SPINLOCK_T lock;
232 struct {
233 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
234 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
235 struct {
236 const char *function;
237 const char *file;
238 int line;
239 } last_location; /* location of the last owner */
240 } info;
241};
242
243struct ha_rwlock {
244 __HA_RWLOCK_T lock;
245 struct {
246 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
247 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
248 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
249 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
250 struct {
251 const char *function;
252 const char *file;
253 int line;
254 } last_location; /* location of the last write owner */
255 } info;
256};
257
258static inline void show_lock_stats()
259{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200260 const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL",
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200261 "TASK_RQ", "TASK_WQ", "POOL",
Christopher Faulet29f77e82017-06-08 14:04:45 +0200262 "LISTENER", "LISTENER_QUEUE", "PROXY", "SERVER",
Emeric Brun1138fd02017-06-19 12:38:55 +0200263 "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS",
Emeric Brunb5997f72017-07-03 11:34:05 +0200264 "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS",
Thierry FOURNIER738a6d72017-07-17 00:14:07 +0200265 "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA",
Emeric Brund8b3b652017-11-07 11:19:48 +0100266 "NOTIF", "SPOE_APPLET", "DNS", "PID_LIST", "EMAIL_ALERTS",
267 "PIPES" };
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200268 int lbl;
269
270 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
271 fprintf(stderr,
272 "Stats about Lock %s: \n"
273 "\t # write lock : %lu\n"
274 "\t # write unlock: %lu (%ld)\n"
275 "\t # wait time for write : %.3f msec\n"
276 "\t # wait time for write/lock: %.3f nsec\n"
277 "\t # read lock : %lu\n"
278 "\t # read unlock : %lu (%ld)\n"
279 "\t # wait time for read : %.3f msec\n"
280 "\t # wait time for read/lock : %.3f nsec\n",
281 labels[lbl],
282 lock_stats[lbl].num_write_locked,
283 lock_stats[lbl].num_write_unlocked,
284 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
285 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
286 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
287 lock_stats[lbl].num_read_locked,
288 lock_stats[lbl].num_read_unlocked,
289 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
290 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
291 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
292 }
293}
294
295/* Following functions are used to collect some stats about locks. We wrap
296 * pthread functions to known how much time we wait in a lock. */
297
298static uint64_t nsec_now(void) {
299 struct timespec ts;
300
301 clock_gettime(CLOCK_MONOTONIC, &ts);
302 return ((uint64_t) ts.tv_sec * 1000000000ULL +
303 (uint64_t) ts.tv_nsec);
304}
305
306static inline void __ha_rwlock_init(struct ha_rwlock *l)
307{
308 memset(l, 0, sizeof(struct ha_rwlock));
309 __RWLOCK_INIT(&l->lock);
310}
311
312static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
313{
314 __RWLOCK_DESTROY(&l->lock);
315 memset(l, 0, sizeof(struct ha_rwlock));
316}
317
318
319static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
320 const char *func, const char *file, int line)
321{
322 uint64_t start_time;
323
324 if (unlikely(l->info.cur_writer & tid_bit)) {
325 /* the thread is already owning the lock for write */
326 abort();
327 }
328
329 if (unlikely(l->info.cur_readers & tid_bit)) {
330 /* the thread is already owning the lock for read */
331 abort();
332 }
333
334 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
335
336 start_time = nsec_now();
337 __RWLOCK_WRLOCK(&l->lock);
338 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
339
340 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
341
342 l->info.cur_writer = tid_bit;
343 l->info.last_location.function = func;
344 l->info.last_location.file = file;
345 l->info.last_location.line = line;
346
347 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
348}
349
350static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
351 const char *func, const char *file, int line)
352{
353 uint64_t start_time;
354 int r;
355
356 if (unlikely(l->info.cur_writer & tid_bit)) {
357 /* the thread is already owning the lock for write */
358 abort();
359 }
360
361 if (unlikely(l->info.cur_readers & tid_bit)) {
362 /* the thread is already owning the lock for read */
363 abort();
364 }
365
366 /* We set waiting writer because trywrlock could wait for readers to quit */
367 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
368
369 start_time = nsec_now();
370 r = __RWLOCK_TRYWRLOCK(&l->lock);
371 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
372 if (unlikely(r)) {
373 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
374 return r;
375 }
376 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
377
378 l->info.cur_writer = tid_bit;
379 l->info.last_location.function = func;
380 l->info.last_location.file = file;
381 l->info.last_location.line = line;
382
383 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
384
385 return 0;
386}
387
388static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
389 const char *func, const char *file, int line)
390{
391 if (unlikely(!(l->info.cur_writer & tid_bit))) {
392 /* the thread is not owning the lock for write */
393 abort();
394 }
395
396 l->info.cur_writer = 0;
397 l->info.last_location.function = func;
398 l->info.last_location.file = file;
399 l->info.last_location.line = line;
400
401 __RWLOCK_WRUNLOCK(&l->lock);
402
403 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
404}
405
406static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
407{
408 uint64_t start_time;
409
410 if (unlikely(l->info.cur_writer & tid_bit)) {
411 /* the thread is already owning the lock for write */
412 abort();
413 }
414
415 if (unlikely(l->info.cur_readers & tid_bit)) {
416 /* the thread is already owning the lock for read */
417 abort();
418 }
419
420 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
421
422 start_time = nsec_now();
423 __RWLOCK_RDLOCK(&l->lock);
424 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
425 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
426
427 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
428
429 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
430}
431
432static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
433{
434 int r;
435
436 if (unlikely(l->info.cur_writer & tid_bit)) {
437 /* the thread is already owning the lock for write */
438 abort();
439 }
440
441 if (unlikely(l->info.cur_readers & tid_bit)) {
442 /* the thread is already owning the lock for read */
443 abort();
444 }
445
446 /* try read should never wait */
447 r = __RWLOCK_TRYRDLOCK(&l->lock);
448 if (unlikely(r))
449 return r;
450 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
451
452 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
453
454 return 0;
455}
456
457static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
458{
459 if (unlikely(!(l->info.cur_readers & tid_bit))) {
460 /* the thread is not owning the lock for read */
461 abort();
462 }
463
464 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
465
466 __RWLOCK_RDUNLOCK(&l->lock);
467
468 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
469}
470
471static inline void __spin_init(struct ha_spinlock *l)
472{
473 memset(l, 0, sizeof(struct ha_spinlock));
474 __SPIN_INIT(&l->lock);
475}
476
477static inline void __spin_destroy(struct ha_spinlock *l)
478{
479 __SPIN_DESTROY(&l->lock);
480 memset(l, 0, sizeof(struct ha_spinlock));
481}
482
483static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
484 const char *func, const char *file, int line)
485{
486 uint64_t start_time;
487
488 if (unlikely(l->info.owner & tid_bit)) {
489 /* the thread is already owning the lock */
490 abort();
491 }
492
493 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
494
495 start_time = nsec_now();
496 __SPIN_LOCK(&l->lock);
497 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
498
499 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
500
501
502 l->info.owner = tid_bit;
503 l->info.last_location.function = func;
504 l->info.last_location.file = file;
505 l->info.last_location.line = line;
506
507 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
508}
509
510static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
511 const char *func, const char *file, int line)
512{
513 int r;
514
515 if (unlikely(l->info.owner & tid_bit)) {
516 /* the thread is already owning the lock */
517 abort();
518 }
519
520 /* try read should never wait */
521 r = __SPIN_TRYLOCK(&l->lock);
522 if (unlikely(r))
523 return r;
524 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
525
526 l->info.owner = tid_bit;
527 l->info.last_location.function = func;
528 l->info.last_location.file = file;
529 l->info.last_location.line = line;
530
531 return 0;
532}
533
534static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
535 const char *func, const char *file, int line)
536{
537 if (unlikely(!(l->info.owner & tid_bit))) {
538 /* the thread is not owning the lock */
539 abort();
540 }
541
542 l->info.owner = 0;
543 l->info.last_location.function = func;
544 l->info.last_location.file = file;
545 l->info.last_location.line = line;
546
Willy Tarreau7c2a2ad2017-11-02 16:26:02 +0100547 __SPIN_UNLOCK(&l->lock);
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200548 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
549}
550
551#else /* DEBUG_THREAD */
552
553#define HA_SPINLOCK_T unsigned long
554
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100555#define HA_SPIN_INIT(l) ({ (*l) = 0; })
556#define HA_SPIN_DESTROY(l) ({ (*l) = 0; })
557#define HA_SPIN_LOCK(lbl, l) pl_take_s(l)
558#define HA_SPIN_TRYLOCK(lbl, l) !pl_try_s(l)
559#define HA_SPIN_UNLOCK(lbl, l) pl_drop_s(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200560
561#define HA_RWLOCK_T unsigned long
562
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100563#define HA_RWLOCK_INIT(l) ({ (*l) = 0; })
564#define HA_RWLOCK_DESTROY(l) ({ (*l) = 0; })
565#define HA_RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
566#define HA_RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
567#define HA_RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
568#define HA_RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
569#define HA_RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
570#define HA_RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200571
572#endif /* DEBUG_THREAD */
573
574#endif /* USE_THREAD */
575
576#endif /* _COMMON_HATHREADS_H */