Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 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) |
| 28 | extern THREAD_LOCAL unsigned int tid; /* The thread id */ |
| 29 | extern 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 Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 63 | |
| 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 Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 72 | #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 Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 121 | #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 | |
| 129 | int thread_sync_init(unsigned long mask); |
| 130 | void thread_sync_enable(void); |
| 131 | void thread_want_sync(void); |
| 132 | void thread_enter_sync(void); |
| 133 | void thread_exit_sync(void); |
| 134 | int thread_no_sync(void); |
| 135 | int thread_need_sync(void); |
| 136 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 137 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 138 | |
| 139 | enum lock_label { |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 140 | THREAD_SYNC_LOCK = 0, |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 141 | FDTAB_LOCK, |
| 142 | FDCACHE_LOCK, |
| 143 | FD_LOCK, |
| 144 | POLL_LOCK, |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 145 | TASK_RQ_LOCK, |
| 146 | TASK_WQ_LOCK, |
Christopher Faulet | b349e48 | 2017-08-29 09:52:38 +0200 | [diff] [blame] | 147 | POOL_LOCK, |
Christopher Faulet | 8d8aa0d | 2017-05-30 15:36:50 +0200 | [diff] [blame] | 148 | LISTENER_LOCK, |
| 149 | LISTENER_QUEUE_LOCK, |
Christopher Faulet | ff8abcd | 2017-06-02 15:33:24 +0200 | [diff] [blame] | 150 | PROXY_LOCK, |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 151 | SERVER_LOCK, |
Christopher Faulet | 5d42e09 | 2017-10-16 12:00:40 +0200 | [diff] [blame] | 152 | UPDATED_SERVERS_LOCK, |
Christopher Faulet | 5b51755 | 2017-06-09 14:17:53 +0200 | [diff] [blame] | 153 | LBPRM_LOCK, |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 154 | SIGNALS_LOCK, |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 155 | STK_TABLE_LOCK, |
| 156 | STK_SESS_LOCK, |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 157 | APPLETS_LOCK, |
Emeric Brun | 80527f5 | 2017-06-19 17:46:37 +0200 | [diff] [blame] | 158 | PEER_LOCK, |
Emeric Brun | a1dd243 | 2017-06-21 15:42:52 +0200 | [diff] [blame] | 159 | BUF_WQ_LOCK, |
Emeric Brun | 6b35e9b | 2017-06-30 16:23:45 +0200 | [diff] [blame] | 160 | STRMS_LOCK, |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 161 | SSL_LOCK, |
| 162 | SSL_GEN_CERTS_LOCK, |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 163 | PATREF_LOCK, |
| 164 | PATEXP_LOCK, |
| 165 | PATLRU_LOCK, |
Christopher Faulet | e95f2c3 | 2017-07-24 16:30:34 +0200 | [diff] [blame] | 166 | VARS_LOCK, |
Christopher Faulet | 8ca3b4b | 2017-07-25 11:07:15 +0200 | [diff] [blame] | 167 | COMP_POOL_LOCK, |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 168 | LUA_LOCK, |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 169 | LOCK_LABELS |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 170 | }; |
| 171 | struct lock_stat { |
| 172 | uint64_t nsec_wait_for_write; |
| 173 | uint64_t nsec_wait_for_read; |
| 174 | uint64_t num_write_locked; |
| 175 | uint64_t num_write_unlocked; |
| 176 | uint64_t num_read_locked; |
| 177 | uint64_t num_read_unlocked; |
| 178 | }; |
| 179 | |
| 180 | extern struct lock_stat lock_stats[LOCK_LABELS]; |
| 181 | |
| 182 | #define __HA_SPINLOCK_T unsigned long |
| 183 | |
| 184 | #define __SPIN_INIT(l) ({ (*l) = 0; }) |
| 185 | #define __SPIN_DESTROY(l) ({ (*l) = 0; }) |
| 186 | #define __SPIN_LOCK(l) pl_take_w(l) |
| 187 | #define __SPIN_TRYLOCK(l) !pl_try_w(l) |
| 188 | #define __SPIN_UNLOCK(l) pl_drop_w(l) |
| 189 | |
| 190 | #define __HA_RWLOCK_T unsigned long |
| 191 | |
| 192 | #define __RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 193 | #define __RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 194 | #define __RWLOCK_WRLOCK(l) pl_take_w(l) |
| 195 | #define __RWLOCK_TRYWRLOCK(l) !pl_try_w(l) |
| 196 | #define __RWLOCK_WRUNLOCK(l) pl_drop_w(l) |
| 197 | #define __RWLOCK_RDLOCK(l) pl_take_r(l) |
| 198 | #define __RWLOCK_TRYRDLOCK(l) !pl_try_r(l) |
| 199 | #define __RWLOCK_RDUNLOCK(l) pl_drop_r(l) |
| 200 | |
| 201 | #define HA_SPINLOCK_T struct ha_spinlock |
| 202 | |
| 203 | #define SPIN_INIT(l) __spin_init(l) |
| 204 | #define SPIN_DESTROY(l) __spin_destroy(l) |
| 205 | |
| 206 | #define SPIN_LOCK(lbl, l) __spin_lock(lbl, l, __func__, __FILE__, __LINE__) |
| 207 | #define SPIN_TRYLOCK(lbl, l) __spin_trylock(lbl, l, __func__, __FILE__, __LINE__) |
| 208 | #define SPIN_UNLOCK(lbl, l) __spin_unlock(lbl, l, __func__, __FILE__, __LINE__) |
| 209 | |
| 210 | #define HA_RWLOCK_T struct ha_rwlock |
| 211 | |
| 212 | #define RWLOCK_INIT(l) __ha_rwlock_init((l)) |
| 213 | #define RWLOCK_DESTROY(l) __ha_rwlock_destroy((l)) |
| 214 | #define RWLOCK_WRLOCK(lbl,l) __ha_rwlock_wrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 215 | #define RWLOCK_TRYWRLOCK(lbl,l) __ha_rwlock_trywrlock(lbl, l, __func__, __FILE__, __LINE__) |
| 216 | #define RWLOCK_WRUNLOCK(lbl,l) __ha_rwlock_wrunlock(lbl, l, __func__, __FILE__, __LINE__) |
| 217 | #define RWLOCK_RDLOCK(lbl,l) __ha_rwlock_rdlock(lbl, l) |
| 218 | #define RWLOCK_TRYRDLOCK(lbl,l) __ha_rwlock_tryrdlock(lbl, l) |
| 219 | #define RWLOCK_RDUNLOCK(lbl,l) __ha_rwlock_rdunlock(lbl, l) |
| 220 | |
| 221 | struct ha_spinlock { |
| 222 | __HA_SPINLOCK_T lock; |
| 223 | struct { |
| 224 | unsigned long owner; /* a bit is set to 1 << tid for the lock owner */ |
| 225 | unsigned long waiters; /* a bit is set to 1 << tid for waiting threads */ |
| 226 | struct { |
| 227 | const char *function; |
| 228 | const char *file; |
| 229 | int line; |
| 230 | } last_location; /* location of the last owner */ |
| 231 | } info; |
| 232 | }; |
| 233 | |
| 234 | struct ha_rwlock { |
| 235 | __HA_RWLOCK_T lock; |
| 236 | struct { |
| 237 | unsigned long cur_writer; /* a bit is set to 1 << tid for the lock owner */ |
| 238 | unsigned long wait_writers; /* a bit is set to 1 << tid for waiting writers */ |
| 239 | unsigned long cur_readers; /* a bit is set to 1 << tid for current readers */ |
| 240 | unsigned long wait_readers; /* a bit is set to 1 << tid for waiting waiters */ |
| 241 | struct { |
| 242 | const char *function; |
| 243 | const char *file; |
| 244 | int line; |
| 245 | } last_location; /* location of the last write owner */ |
| 246 | } info; |
| 247 | }; |
| 248 | |
| 249 | static inline void show_lock_stats() |
| 250 | { |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 251 | const char *labels[LOCK_LABELS] = {"THREAD_SYNC", "FDTAB", "FDCACHE", "FD", "POLL", |
Christopher Faulet | b79a94c | 2017-05-30 15:34:30 +0200 | [diff] [blame] | 252 | "TASK_RQ", "TASK_WQ", "POOL", |
Christopher Faulet | 29f77e8 | 2017-06-08 14:04:45 +0200 | [diff] [blame] | 253 | "LISTENER", "LISTENER_QUEUE", "PROXY", "SERVER", |
Emeric Brun | 1138fd0 | 2017-06-19 12:38:55 +0200 | [diff] [blame] | 254 | "UPDATED_SERVERS", "LBPRM", "SIGNALS", "STK_TABLE", "STK_SESS", |
Emeric Brun | b5997f7 | 2017-07-03 11:34:05 +0200 | [diff] [blame] | 255 | "APPLETS", "PEER", "BUF_WQ", "STREAMS", "SSL", "SSL_GEN_CERTS", |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 256 | "PATREF", "PATEXP", "PATLRU", "VARS", "COMP_POOL", "LUA" }; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 257 | int lbl; |
| 258 | |
| 259 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 260 | fprintf(stderr, |
| 261 | "Stats about Lock %s: \n" |
| 262 | "\t # write lock : %lu\n" |
| 263 | "\t # write unlock: %lu (%ld)\n" |
| 264 | "\t # wait time for write : %.3f msec\n" |
| 265 | "\t # wait time for write/lock: %.3f nsec\n" |
| 266 | "\t # read lock : %lu\n" |
| 267 | "\t # read unlock : %lu (%ld)\n" |
| 268 | "\t # wait time for read : %.3f msec\n" |
| 269 | "\t # wait time for read/lock : %.3f nsec\n", |
| 270 | labels[lbl], |
| 271 | lock_stats[lbl].num_write_locked, |
| 272 | lock_stats[lbl].num_write_unlocked, |
| 273 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 274 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 275 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0, |
| 276 | lock_stats[lbl].num_read_locked, |
| 277 | lock_stats[lbl].num_read_unlocked, |
| 278 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 279 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 280 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* Following functions are used to collect some stats about locks. We wrap |
| 285 | * pthread functions to known how much time we wait in a lock. */ |
| 286 | |
| 287 | static uint64_t nsec_now(void) { |
| 288 | struct timespec ts; |
| 289 | |
| 290 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 291 | return ((uint64_t) ts.tv_sec * 1000000000ULL + |
| 292 | (uint64_t) ts.tv_nsec); |
| 293 | } |
| 294 | |
| 295 | static inline void __ha_rwlock_init(struct ha_rwlock *l) |
| 296 | { |
| 297 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 298 | __RWLOCK_INIT(&l->lock); |
| 299 | } |
| 300 | |
| 301 | static inline void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 302 | { |
| 303 | __RWLOCK_DESTROY(&l->lock); |
| 304 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | static inline void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 309 | const char *func, const char *file, int line) |
| 310 | { |
| 311 | uint64_t start_time; |
| 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 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 324 | |
| 325 | start_time = nsec_now(); |
| 326 | __RWLOCK_WRLOCK(&l->lock); |
| 327 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 328 | |
| 329 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 330 | |
| 331 | l->info.cur_writer = tid_bit; |
| 332 | l->info.last_location.function = func; |
| 333 | l->info.last_location.file = file; |
| 334 | l->info.last_location.line = line; |
| 335 | |
| 336 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 337 | } |
| 338 | |
| 339 | static inline int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 340 | const char *func, const char *file, int line) |
| 341 | { |
| 342 | uint64_t start_time; |
| 343 | int r; |
| 344 | |
| 345 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 346 | /* the thread is already owning the lock for write */ |
| 347 | abort(); |
| 348 | } |
| 349 | |
| 350 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 351 | /* the thread is already owning the lock for read */ |
| 352 | abort(); |
| 353 | } |
| 354 | |
| 355 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 356 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 357 | |
| 358 | start_time = nsec_now(); |
| 359 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
| 360 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 361 | if (unlikely(r)) { |
| 362 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 363 | return r; |
| 364 | } |
| 365 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 366 | |
| 367 | l->info.cur_writer = tid_bit; |
| 368 | l->info.last_location.function = func; |
| 369 | l->info.last_location.file = file; |
| 370 | l->info.last_location.line = line; |
| 371 | |
| 372 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static inline void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 378 | const char *func, const char *file, int line) |
| 379 | { |
| 380 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 381 | /* the thread is not owning the lock for write */ |
| 382 | abort(); |
| 383 | } |
| 384 | |
| 385 | l->info.cur_writer = 0; |
| 386 | l->info.last_location.function = func; |
| 387 | l->info.last_location.file = file; |
| 388 | l->info.last_location.line = line; |
| 389 | |
| 390 | __RWLOCK_WRUNLOCK(&l->lock); |
| 391 | |
| 392 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 393 | } |
| 394 | |
| 395 | static inline void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 396 | { |
| 397 | uint64_t start_time; |
| 398 | |
| 399 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 400 | /* the thread is already owning the lock for write */ |
| 401 | abort(); |
| 402 | } |
| 403 | |
| 404 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 405 | /* the thread is already owning the lock for read */ |
| 406 | abort(); |
| 407 | } |
| 408 | |
| 409 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 410 | |
| 411 | start_time = nsec_now(); |
| 412 | __RWLOCK_RDLOCK(&l->lock); |
| 413 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (nsec_now() - start_time)); |
| 414 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 415 | |
| 416 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 417 | |
| 418 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 419 | } |
| 420 | |
| 421 | static inline int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 422 | { |
| 423 | int r; |
| 424 | |
| 425 | if (unlikely(l->info.cur_writer & tid_bit)) { |
| 426 | /* the thread is already owning the lock for write */ |
| 427 | abort(); |
| 428 | } |
| 429 | |
| 430 | if (unlikely(l->info.cur_readers & tid_bit)) { |
| 431 | /* the thread is already owning the lock for read */ |
| 432 | abort(); |
| 433 | } |
| 434 | |
| 435 | /* try read should never wait */ |
| 436 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 437 | if (unlikely(r)) |
| 438 | return r; |
| 439 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_locked, 1); |
| 440 | |
| 441 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static inline void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 447 | { |
| 448 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 449 | /* the thread is not owning the lock for read */ |
| 450 | abort(); |
| 451 | } |
| 452 | |
| 453 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 454 | |
| 455 | __RWLOCK_RDUNLOCK(&l->lock); |
| 456 | |
| 457 | HA_ATOMIC_ADD(&lock_stats[lbl].num_read_unlocked, 1); |
| 458 | } |
| 459 | |
| 460 | static inline void __spin_init(struct ha_spinlock *l) |
| 461 | { |
| 462 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 463 | __SPIN_INIT(&l->lock); |
| 464 | } |
| 465 | |
| 466 | static inline void __spin_destroy(struct ha_spinlock *l) |
| 467 | { |
| 468 | __SPIN_DESTROY(&l->lock); |
| 469 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 470 | } |
| 471 | |
| 472 | static inline void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 473 | const char *func, const char *file, int line) |
| 474 | { |
| 475 | uint64_t start_time; |
| 476 | |
| 477 | if (unlikely(l->info.owner & tid_bit)) { |
| 478 | /* the thread is already owning the lock */ |
| 479 | abort(); |
| 480 | } |
| 481 | |
| 482 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 483 | |
| 484 | start_time = nsec_now(); |
| 485 | __SPIN_LOCK(&l->lock); |
| 486 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (nsec_now() - start_time)); |
| 487 | |
| 488 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 489 | |
| 490 | |
| 491 | l->info.owner = tid_bit; |
| 492 | l->info.last_location.function = func; |
| 493 | l->info.last_location.file = file; |
| 494 | l->info.last_location.line = line; |
| 495 | |
| 496 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 497 | } |
| 498 | |
| 499 | static inline int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 500 | const char *func, const char *file, int line) |
| 501 | { |
| 502 | int r; |
| 503 | |
| 504 | if (unlikely(l->info.owner & tid_bit)) { |
| 505 | /* the thread is already owning the lock */ |
| 506 | abort(); |
| 507 | } |
| 508 | |
| 509 | /* try read should never wait */ |
| 510 | r = __SPIN_TRYLOCK(&l->lock); |
| 511 | if (unlikely(r)) |
| 512 | return r; |
| 513 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_locked, 1); |
| 514 | |
| 515 | l->info.owner = tid_bit; |
| 516 | l->info.last_location.function = func; |
| 517 | l->info.last_location.file = file; |
| 518 | l->info.last_location.line = line; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static inline void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 524 | const char *func, const char *file, int line) |
| 525 | { |
| 526 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 527 | /* the thread is not owning the lock */ |
| 528 | abort(); |
| 529 | } |
| 530 | |
| 531 | l->info.owner = 0; |
| 532 | l->info.last_location.function = func; |
| 533 | l->info.last_location.file = file; |
| 534 | l->info.last_location.line = line; |
| 535 | |
| 536 | __RWLOCK_WRUNLOCK(&l->lock); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 537 | HA_ATOMIC_ADD(&lock_stats[lbl].num_write_unlocked, 1); |
| 538 | } |
| 539 | |
| 540 | #else /* DEBUG_THREAD */ |
| 541 | |
| 542 | #define HA_SPINLOCK_T unsigned long |
| 543 | |
| 544 | #define SPIN_INIT(l) ({ (*l) = 0; }) |
| 545 | #define SPIN_DESTROY(l) ({ (*l) = 0; }) |
| 546 | #define SPIN_LOCK(lbl, l) pl_take_w(l) |
| 547 | #define SPIN_TRYLOCK(lbl, l) !pl_try_w(l) |
| 548 | #define SPIN_UNLOCK(lbl, l) pl_drop_w(l) |
| 549 | |
| 550 | #define HA_RWLOCK_T unsigned long |
| 551 | |
| 552 | #define RWLOCK_INIT(l) ({ (*l) = 0; }) |
| 553 | #define RWLOCK_DESTROY(l) ({ (*l) = 0; }) |
| 554 | #define RWLOCK_WRLOCK(lbl,l) pl_take_w(l) |
| 555 | #define RWLOCK_TRYWRLOCK(lbl,l) !pl_try_w(l) |
| 556 | #define RWLOCK_WRUNLOCK(lbl,l) pl_drop_w(l) |
| 557 | #define RWLOCK_RDLOCK(lbl,l) pl_take_r(l) |
| 558 | #define RWLOCK_TRYRDLOCK(lbl,l) !pl_try_r(l) |
| 559 | #define RWLOCK_RDUNLOCK(lbl,l) pl_drop_r(l) |
| 560 | |
| 561 | #endif /* DEBUG_THREAD */ |
| 562 | |
| 563 | #endif /* USE_THREAD */ |
| 564 | |
| 565 | #endif /* _COMMON_HATHREADS_H */ |