blob: 80a4b24431d6687399f5c802a6ebf5100a464c2c [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 Fauletb79a94c2017-05-30 15:34:30 +0200151 SIGNALS_LOCK,
Christopher Faulet339fff82017-10-19 11:59:15 +0200152 LOCK_LABELS
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200153};
154struct lock_stat {
155 uint64_t nsec_wait_for_write;
156 uint64_t nsec_wait_for_read;
157 uint64_t num_write_locked;
158 uint64_t num_write_unlocked;
159 uint64_t num_read_locked;
160 uint64_t num_read_unlocked;
161};
162
163extern struct lock_stat lock_stats[LOCK_LABELS];
164
165#define __HA_SPINLOCK_T unsigned long
166
167#define __SPIN_INIT(l) ({ (*l) = 0; })
168#define __SPIN_DESTROY(l) ({ (*l) = 0; })
169#define __SPIN_LOCK(l) pl_take_w(l)
170#define __SPIN_TRYLOCK(l) !pl_try_w(l)
171#define __SPIN_UNLOCK(l) pl_drop_w(l)
172
173#define __HA_RWLOCK_T unsigned long
174
175#define __RWLOCK_INIT(l) ({ (*l) = 0; })
176#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
177#define __RWLOCK_WRLOCK(l) pl_take_w(l)
178#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
179#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
180#define __RWLOCK_RDLOCK(l) pl_take_r(l)
181#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
182#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
183
184#define HA_SPINLOCK_T struct ha_spinlock
185
186#define SPIN_INIT(l) __spin_init(l)
187#define SPIN_DESTROY(l) __spin_destroy(l)
188
189#define SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
190#define SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
191#define SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
192
193#define HA_RWLOCK_T struct ha_rwlock
194
195#define RWLOCK_INIT(l) __ha_rwlock_init((l))
196#define RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
197#define RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
198#define RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
199#define RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
200#define RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
201#define RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
202#define RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
203
204struct ha_spinlock {
205 __HA_SPINLOCK_T lock;
206 struct {
207 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
208 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
209 struct {
210 const char *function;
211 const char *file;
212 int line;
213 } last_location; /* location of the last owner */
214 } info;
215};
216
217struct ha_rwlock {
218 __HA_RWLOCK_T lock;
219 struct {
220 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
221 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
222 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
223 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
224 struct {
225 const char *function;
226 const char *file;
227 int line;
228 } last_location; /* location of the last write owner */
229 } info;
230};
231
232static inline void show_lock_stats()
233{
Christopher Fauletd4604ad2017-05-29 10:40:41 +0200234 const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL",
Christopher Fauletb79a94c2017-05-30 15:34:30 +0200235 "TASK_RQ", "TASK_WQ", "POOL",
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200236 "LISTENER", "LISTENER_QUEUE", "PROXY", "SIGNALS" };
Christopher Faulet1a2b56e2017-10-12 16:09:09 +0200237 int lbl;
238
239 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
240 fprintf(stderr,
241 "Stats about Lock %s: \n"
242 "\t # write lock : %lu\n"
243 "\t # write unlock: %lu (%ld)\n"
244 "\t # wait time for write : %.3f msec\n"
245 "\t # wait time for write/lock: %.3f nsec\n"
246 "\t # read lock : %lu\n"
247 "\t # read unlock : %lu (%ld)\n"
248 "\t # wait time for read : %.3f msec\n"
249 "\t # wait time for read/lock : %.3f nsec\n",
250 labels[lbl],
251 lock_stats[lbl].num_write_locked,
252 lock_stats[lbl].num_write_unlocked,
253 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
254 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
255 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
256 lock_stats[lbl].num_read_locked,
257 lock_stats[lbl].num_read_unlocked,
258 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
259 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
260 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
261 }
262}
263
264/* Following functions are used to collect some stats about locks. We wrap
265 * pthread functions to known how much time we wait in a lock. */
266
267static uint64_t nsec_now(void) {
268 struct timespec ts;
269
270 clock_gettime(CLOCK_MONOTONIC, &ts);
271 return ((uint64_t) ts.tv_sec * 1000000000ULL +
272 (uint64_t) ts.tv_nsec);
273}
274
275static inline void __ha_rwlock_init(struct ha_rwlock *l)
276{
277 memset(l, 0, sizeof(struct ha_rwlock));
278 __RWLOCK_INIT(&l->lock);
279}
280
281static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
282{
283 __RWLOCK_DESTROY(&l->lock);
284 memset(l, 0, sizeof(struct ha_rwlock));
285}
286
287
288static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
289 const char *func, const char *file, int line)
290{
291 uint64_t start_time;
292
293 if (unlikely(l->info.cur_writer & tid_bit)) {
294 /* the thread is already owning the lock for write */
295 abort();
296 }
297
298 if (unlikely(l->info.cur_readers & tid_bit)) {
299 /* the thread is already owning the lock for read */
300 abort();
301 }
302
303 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
304
305 start_time = nsec_now();
306 __RWLOCK_WRLOCK(&l->lock);
307 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
308
309 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
310
311 l->info.cur_writer = tid_bit;
312 l->info.last_location.function = func;
313 l->info.last_location.file = file;
314 l->info.last_location.line = line;
315
316 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
317}
318
319static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
320 const char *func, const char *file, int line)
321{
322 uint64_t start_time;
323 int r;
324
325 if (unlikely(l->info.cur_writer & tid_bit)) {
326 /* the thread is already owning the lock for write */
327 abort();
328 }
329
330 if (unlikely(l->info.cur_readers & tid_bit)) {
331 /* the thread is already owning the lock for read */
332 abort();
333 }
334
335 /* We set waiting writer because trywrlock could wait for readers to quit */
336 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
337
338 start_time = nsec_now();
339 r = __RWLOCK_TRYWRLOCK(&l->lock);
340 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
341 if (unlikely(r)) {
342 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
343 return r;
344 }
345 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
346
347 l->info.cur_writer = tid_bit;
348 l->info.last_location.function = func;
349 l->info.last_location.file = file;
350 l->info.last_location.line = line;
351
352 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
353
354 return 0;
355}
356
357static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
358 const char *func, const char *file, int line)
359{
360 if (unlikely(!(l->info.cur_writer & tid_bit))) {
361 /* the thread is not owning the lock for write */
362 abort();
363 }
364
365 l->info.cur_writer = 0;
366 l->info.last_location.function = func;
367 l->info.last_location.file = file;
368 l->info.last_location.line = line;
369
370 __RWLOCK_WRUNLOCK(&l->lock);
371
372 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
373}
374
375static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
376{
377 uint64_t start_time;
378
379 if (unlikely(l->info.cur_writer & tid_bit)) {
380 /* the thread is already owning the lock for write */
381 abort();
382 }
383
384 if (unlikely(l->info.cur_readers & tid_bit)) {
385 /* the thread is already owning the lock for read */
386 abort();
387 }
388
389 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
390
391 start_time = nsec_now();
392 __RWLOCK_RDLOCK(&l->lock);
393 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
394 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
395
396 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
397
398 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
399}
400
401static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
402{
403 int r;
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 /* try read should never wait */
416 r = __RWLOCK_TRYRDLOCK(&l->lock);
417 if (unlikely(r))
418 return r;
419 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
420
421 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
422
423 return 0;
424}
425
426static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
427{
428 if (unlikely(!(l->info.cur_readers & tid_bit))) {
429 /* the thread is not owning the lock for read */
430 abort();
431 }
432
433 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
434
435 __RWLOCK_RDUNLOCK(&l->lock);
436
437 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
438}
439
440static inline void __spin_init(struct ha_spinlock *l)
441{
442 memset(l, 0, sizeof(struct ha_spinlock));
443 __SPIN_INIT(&l->lock);
444}
445
446static inline void __spin_destroy(struct ha_spinlock *l)
447{
448 __SPIN_DESTROY(&l->lock);
449 memset(l, 0, sizeof(struct ha_spinlock));
450}
451
452static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
453 const char *func, const char *file, int line)
454{
455 uint64_t start_time;
456
457 if (unlikely(l->info.owner & tid_bit)) {
458 /* the thread is already owning the lock */
459 abort();
460 }
461
462 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
463
464 start_time = nsec_now();
465 __SPIN_LOCK(&l->lock);
466 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
467
468 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
469
470
471 l->info.owner = tid_bit;
472 l->info.last_location.function = func;
473 l->info.last_location.file = file;
474 l->info.last_location.line = line;
475
476 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
477}
478
479static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
480 const char *func, const char *file, int line)
481{
482 int r;
483
484 if (unlikely(l->info.owner & tid_bit)) {
485 /* the thread is already owning the lock */
486 abort();
487 }
488
489 /* try read should never wait */
490 r = __SPIN_TRYLOCK(&l->lock);
491 if (unlikely(r))
492 return r;
493 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
494
495 l->info.owner = tid_bit;
496 l->info.last_location.function = func;
497 l->info.last_location.file = file;
498 l->info.last_location.line = line;
499
500 return 0;
501}
502
503static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
504 const char *func, const char *file, int line)
505{
506 if (unlikely(!(l->info.owner & tid_bit))) {
507 /* the thread is not owning the lock */
508 abort();
509 }
510
511 l->info.owner = 0;
512 l->info.last_location.function = func;
513 l->info.last_location.file = file;
514 l->info.last_location.line = line;
515
516 __RWLOCK_WRUNLOCK(&l->lock);
517
518 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
519}
520
521#else /* DEBUG_THREAD */
522
523#define HA_SPINLOCK_T unsigned long
524
525#define SPIN_INIT(l) ({ (*l) = 0; })
526#define SPIN_DESTROY(l) ({ (*l) = 0; })
527#define SPIN_LOCK(lbl, l) pl_take_w(l)
528#define SPIN_TRYLOCK(lbl, l) !pl_try_w(l)
529#define SPIN_UNLOCK(lbl, l) pl_drop_w(l)
530
531#define HA_RWLOCK_T unsigned long
532
533#define RWLOCK_INIT(l) ({ (*l) = 0; })
534#define RWLOCK_DESTROY(l) ({ (*l) = 0; })
535#define RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
536#define RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
537#define RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
538#define RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
539#define RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
540#define RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
541
542#endif /* DEBUG_THREAD */
543
544#endif /* USE_THREAD */
545
546#endif /* _COMMON_HATHREADS_H */