blob: 6323e9a369dcc89d456fb044ff86b78b2a96cf02 [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
63#define SPIN_INIT(l) do { /* do nothing */ } while(0)
64#define SPIN_DESTROY(l) do { /* do nothing */ } while(0)
65#define SPIN_LOCK(lbl, l) do { /* do nothing */ } while(0)
66#define SPIN_TRYLOCK(lbl, l) ({ 0; })
67#define SPIN_UNLOCK(lbl, l) do { /* do nothing */ } while(0)
68
69#define RWLOCK_INIT(l) do { /* do nothing */ } while(0)
70#define RWLOCK_DESTROY(l) do { /* do nothing */ } while(0)
71#define RWLOCK_WRLOCK(lbl, l) do { /* do nothing */ } while(0)
72#define RWLOCK_TRYWRLOCK(lbl, l) ({ 0; })
73#define RWLOCK_WRUNLOCK(lbl, l) do { /* do nothing */ } while(0)
74#define RWLOCK_RDLOCK(lbl, l) do { /* do nothing */ } while(0)
75#define RWLOCK_TRYRDLOCK(lbl, l) ({ 0; })
76#define RWLOCK_RDUNLOCK(lbl, l) do { /* do nothing */ } while(0)
77
78#else /* USE_THREAD */
79
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <pthread.h>
84#include <import/plock.h>
85
86/* TODO: thread: For now, we rely on GCC builtins but it could be a good idea to
87 * have a header file regrouping all functions dealing with threads. */
88#define HA_ATOMIC_CAS(val, old, new) __atomic_compare_exchange_n(val, old, new, 0, 0, 0)
89#define HA_ATOMIC_ADD(val, i) __atomic_add_fetch(val, i, 0)
90#define HA_ATOMIC_SUB(val, i) __atomic_sub_fetch(val, i, 0)
91#define HA_ATOMIC_AND(val, flags) __atomic_and_fetch(val, flags, 0)
92#define HA_ATOMIC_OR(val, flags) __atomic_or_fetch(val, flags, 0)
93#define HA_ATOMIC_XCHG(val, new) __atomic_exchange_n(val, new, 0)
94#define HA_ATOMIC_STORE(val, new) __atomic_store_n(val, new, 0)
95#define HA_ATOMIC_UPDATE_MAX(val, new) \
96 ({ \
97 typeof(*(val)) __old = *(val); \
98 typeof(*(val)) __new = (new); \
99 \
100 while (__old < __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
101 (*val); \
102 })
103#define HA_ATOMIC_UPDATE_MIN(val, new) \
104 ({ \
105 typeof((*val)) __old = *(val); \
106 typeof((*val)) __new = (new); \
107 \
108 while (__old > __new && !HA_ATOMIC_CAS(val, &__old, __new)); \
109 (*val); \
110 })
111
112#if defined(DEBUG_THREAD) || defined(DEBUG_FULL)
113
114enum lock_label {
115 LOCK_LABELS = 0
116};
117struct lock_stat {
118 uint64_t nsec_wait_for_write;
119 uint64_t nsec_wait_for_read;
120 uint64_t num_write_locked;
121 uint64_t num_write_unlocked;
122 uint64_t num_read_locked;
123 uint64_t num_read_unlocked;
124};
125
126extern struct lock_stat lock_stats[LOCK_LABELS];
127
128#define __HA_SPINLOCK_T unsigned long
129
130#define __SPIN_INIT(l) ({ (*l) = 0; })
131#define __SPIN_DESTROY(l) ({ (*l) = 0; })
132#define __SPIN_LOCK(l) pl_take_w(l)
133#define __SPIN_TRYLOCK(l) !pl_try_w(l)
134#define __SPIN_UNLOCK(l) pl_drop_w(l)
135
136#define __HA_RWLOCK_T unsigned long
137
138#define __RWLOCK_INIT(l) ({ (*l) = 0; })
139#define __RWLOCK_DESTROY(l) ({ (*l) = 0; })
140#define __RWLOCK_WRLOCK(l) pl_take_w(l)
141#define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l)
142#define __RWLOCK_WRUNLOCK(l) pl_drop_w(l)
143#define __RWLOCK_RDLOCK(l) pl_take_r(l)
144#define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l)
145#define __RWLOCK_RDUNLOCK(l) pl_drop_r(l)
146
147#define HA_SPINLOCK_T struct ha_spinlock
148
149#define SPIN_INIT(l) __spin_init(l)
150#define SPIN_DESTROY(l) __spin_destroy(l)
151
152#define SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__)
153#define SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__)
154#define SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__)
155
156#define HA_RWLOCK_T struct ha_rwlock
157
158#define RWLOCK_INIT(l) __ha_rwlock_init((l))
159#define RWLOCK_DESTROY(l) __ha_rwlock_destroy((l))
160#define RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__)
161#define RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__)
162#define RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__)
163#define RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l)
164#define RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l)
165#define RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l)
166
167struct ha_spinlock {
168 __HA_SPINLOCK_T lock;
169 struct {
170 unsigned long owner; /* a bit is set to 1 << tid for the lock owner */
171 unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */
172 struct {
173 const char *function;
174 const char *file;
175 int line;
176 } last_location; /* location of the last owner */
177 } info;
178};
179
180struct ha_rwlock {
181 __HA_RWLOCK_T lock;
182 struct {
183 unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */
184 unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */
185 unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */
186 unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */
187 struct {
188 const char *function;
189 const char *file;
190 int line;
191 } last_location; /* location of the last write owner */
192 } info;
193};
194
195static inline void show_lock_stats()
196{
197 const char *labels[LOCK_LABELS] = {};
198 int lbl;
199
200 for (lbl = 0; lbl < LOCK_LABELS; lbl++) {
201 fprintf(stderr,
202 "Stats about Lock %s: \n"
203 "\t # write lock : %lu\n"
204 "\t # write unlock: %lu (%ld)\n"
205 "\t # wait time for write : %.3f msec\n"
206 "\t # wait time for write/lock: %.3f nsec\n"
207 "\t # read lock : %lu\n"
208 "\t # read unlock : %lu (%ld)\n"
209 "\t # wait time for read : %.3f msec\n"
210 "\t # wait time for read/lock : %.3f nsec\n",
211 labels[lbl],
212 lock_stats[lbl].num_write_locked,
213 lock_stats[lbl].num_write_unlocked,
214 lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked,
215 (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0,
216 lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0,
217 lock_stats[lbl].num_read_locked,
218 lock_stats[lbl].num_read_unlocked,
219 lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked,
220 (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0,
221 lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0);
222 }
223}
224
225/* Following functions are used to collect some stats about locks. We wrap
226 * pthread functions to known how much time we wait in a lock. */
227
228static uint64_t nsec_now(void) {
229 struct timespec ts;
230
231 clock_gettime(CLOCK_MONOTONIC, &ts);
232 return ((uint64_t) ts.tv_sec * 1000000000ULL +
233 (uint64_t) ts.tv_nsec);
234}
235
236static inline void __ha_rwlock_init(struct ha_rwlock *l)
237{
238 memset(l, 0, sizeof(struct ha_rwlock));
239 __RWLOCK_INIT(&l->lock);
240}
241
242static inline void __ha_rwlock_destroy(struct ha_rwlock *l)
243{
244 __RWLOCK_DESTROY(&l->lock);
245 memset(l, 0, sizeof(struct ha_rwlock));
246}
247
248
249static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l,
250 const char *func, const char *file, int line)
251{
252 uint64_t start_time;
253
254 if (unlikely(l->info.cur_writer & tid_bit)) {
255 /* the thread is already owning the lock for write */
256 abort();
257 }
258
259 if (unlikely(l->info.cur_readers & tid_bit)) {
260 /* the thread is already owning the lock for read */
261 abort();
262 }
263
264 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
265
266 start_time = nsec_now();
267 __RWLOCK_WRLOCK(&l->lock);
268 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
269
270 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
271
272 l->info.cur_writer = tid_bit;
273 l->info.last_location.function = func;
274 l->info.last_location.file = file;
275 l->info.last_location.line = line;
276
277 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
278}
279
280static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l,
281 const char *func, const char *file, int line)
282{
283 uint64_t start_time;
284 int r;
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 /* We set waiting writer because trywrlock could wait for readers to quit */
297 HA_ATOMIC_OR(&l->info.wait_writers, tid_bit);
298
299 start_time = nsec_now();
300 r = __RWLOCK_TRYWRLOCK(&l->lock);
301 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
302 if (unlikely(r)) {
303 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
304 return r;
305 }
306 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
307
308 l->info.cur_writer = tid_bit;
309 l->info.last_location.function = func;
310 l->info.last_location.file = file;
311 l->info.last_location.line = line;
312
313 HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit);
314
315 return 0;
316}
317
318static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l,
319 const char *func, const char *file, int line)
320{
321 if (unlikely(!(l->info.cur_writer & tid_bit))) {
322 /* the thread is not owning the lock for write */
323 abort();
324 }
325
326 l->info.cur_writer = 0;
327 l->info.last_location.function = func;
328 l->info.last_location.file = file;
329 l->info.last_location.line = line;
330
331 __RWLOCK_WRUNLOCK(&l->lock);
332
333 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
334}
335
336static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l)
337{
338 uint64_t start_time;
339
340 if (unlikely(l->info.cur_writer & tid_bit)) {
341 /* the thread is already owning the lock for write */
342 abort();
343 }
344
345 if (unlikely(l->info.cur_readers & tid_bit)) {
346 /* the thread is already owning the lock for read */
347 abort();
348 }
349
350 HA_ATOMIC_OR(&l->info.wait_readers, tid_bit);
351
352 start_time = nsec_now();
353 __RWLOCK_RDLOCK(&l->lock);
354 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time));
355 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
356
357 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
358
359 HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit);
360}
361
362static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l)
363{
364 int r;
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 /* try read should never wait */
377 r = __RWLOCK_TRYRDLOCK(&l->lock);
378 if (unlikely(r))
379 return r;
380 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1);
381
382 HA_ATOMIC_OR(&l->info.cur_readers, tid_bit);
383
384 return 0;
385}
386
387static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l)
388{
389 if (unlikely(!(l->info.cur_readers & tid_bit))) {
390 /* the thread is not owning the lock for read */
391 abort();
392 }
393
394 HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit);
395
396 __RWLOCK_RDUNLOCK(&l->lock);
397
398 HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1);
399}
400
401static inline void __spin_init(struct ha_spinlock *l)
402{
403 memset(l, 0, sizeof(struct ha_spinlock));
404 __SPIN_INIT(&l->lock);
405}
406
407static inline void __spin_destroy(struct ha_spinlock *l)
408{
409 __SPIN_DESTROY(&l->lock);
410 memset(l, 0, sizeof(struct ha_spinlock));
411}
412
413static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l,
414 const char *func, const char *file, int line)
415{
416 uint64_t start_time;
417
418 if (unlikely(l->info.owner & tid_bit)) {
419 /* the thread is already owning the lock */
420 abort();
421 }
422
423 HA_ATOMIC_OR(&l->info.waiters, tid_bit);
424
425 start_time = nsec_now();
426 __SPIN_LOCK(&l->lock);
427 HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time));
428
429 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
430
431
432 l->info.owner = tid_bit;
433 l->info.last_location.function = func;
434 l->info.last_location.file = file;
435 l->info.last_location.line = line;
436
437 HA_ATOMIC_AND(&l->info.waiters, ~tid_bit);
438}
439
440static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l,
441 const char *func, const char *file, int line)
442{
443 int r;
444
445 if (unlikely(l->info.owner & tid_bit)) {
446 /* the thread is already owning the lock */
447 abort();
448 }
449
450 /* try read should never wait */
451 r = __SPIN_TRYLOCK(&l->lock);
452 if (unlikely(r))
453 return r;
454 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1);
455
456 l->info.owner = tid_bit;
457 l->info.last_location.function = func;
458 l->info.last_location.file = file;
459 l->info.last_location.line = line;
460
461 return 0;
462}
463
464static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l,
465 const char *func, const char *file, int line)
466{
467 if (unlikely(!(l->info.owner & tid_bit))) {
468 /* the thread is not owning the lock */
469 abort();
470 }
471
472 l->info.owner = 0;
473 l->info.last_location.function = func;
474 l->info.last_location.file = file;
475 l->info.last_location.line = line;
476
477 __RWLOCK_WRUNLOCK(&l->lock);
478
479 HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1);
480}
481
482#else /* DEBUG_THREAD */
483
484#define HA_SPINLOCK_T unsigned long
485
486#define SPIN_INIT(l) ({ (*l) = 0; })
487#define SPIN_DESTROY(l) ({ (*l) = 0; })
488#define SPIN_LOCK(lbl, l) pl_take_w(l)
489#define SPIN_TRYLOCK(lbl, l) !pl_try_w(l)
490#define SPIN_UNLOCK(lbl, l) pl_drop_w(l)
491
492#define HA_RWLOCK_T unsigned long
493
494#define RWLOCK_INIT(l) ({ (*l) = 0; })
495#define RWLOCK_DESTROY(l) ({ (*l) = 0; })
496#define RWLOCK_WRLOCK(lbl,l) pl_take_w(l)
497#define RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l)
498#define RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l)
499#define RWLOCK_RDLOCK(lbl,l) pl_take_r(l)
500#define RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l)
501#define RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l)
502
503#endif /* DEBUG_THREAD */
504
505#endif /* USE_THREAD */
506
507#endif /* _COMMON_HATHREADS_H */