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