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