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