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