Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * functions about threads. |
| 3 | * |
| 4 | * Copyright (C) 2017 Christopher Fauet - cfaulet@haproxy.com |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 14 | #include <unistd.h> |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 15 | #include <stdlib.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 16 | #include <fcntl.h> |
| 17 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 18 | #ifdef USE_CPU_AFFINITY |
| 19 | #include <sched.h> |
| 20 | #endif |
| 21 | |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 22 | #ifdef __FreeBSD__ |
| 23 | #include <sys/cpuset.h> |
| 24 | #endif |
| 25 | |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 26 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 27 | #include <haproxy/fd.h> |
| 28 | #include <haproxy/global.h> |
Willy Tarreau | 11bd6f7 | 2021-05-08 20:33:02 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 30 | #include <haproxy/thread.h> |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 31 | #include <haproxy/time.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 32 | #include <haproxy/tools.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 33 | |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 34 | struct thread_info ha_thread_info[MAX_THREADS] = { }; |
| 35 | THREAD_LOCAL struct thread_info *ti = &ha_thread_info[0]; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 36 | |
| 37 | #ifdef USE_THREAD |
| 38 | |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 39 | volatile unsigned long threads_want_rdv_mask __read_mostly = 0; |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 40 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 41 | volatile unsigned long threads_idle_mask = 0; |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 42 | volatile unsigned long threads_sync_mask = 0; |
Willy Tarreau | 56c3b8b | 2021-04-10 17:28:18 +0200 | [diff] [blame] | 43 | volatile unsigned long all_threads_mask __read_mostly = 1; // nbthread 1 assumed by default |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 44 | THREAD_LOCAL unsigned int tid = 0; |
| 45 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 46 | int thread_cpus_enabled_at_boot = 1; |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 48 | /* Marks the thread as harmless until the last thread using the rendez-vous |
Christopher Faulet | a9a9e9a | 2021-03-25 14:11:36 +0100 | [diff] [blame] | 49 | * point quits, excluding the current one. Thus an isolated thread may be safely |
| 50 | * marked as harmless. Given that we can wait for a long time, sched_yield() is |
| 51 | * used when available to offer the CPU resources to competing threads if |
| 52 | * needed. |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 53 | */ |
| 54 | void thread_harmless_till_end() |
| 55 | { |
Willy Tarreau | 286363b | 2021-08-04 10:33:57 +0200 | [diff] [blame] | 56 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 57 | while (threads_want_rdv_mask & all_threads_mask & ~tid_bit) { |
| 58 | ha_thread_relax(); |
| 59 | } |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* Isolates the current thread : request the ability to work while all other |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 63 | * threads are harmless, as defined by thread_harmless_now() (i.e. they're not |
| 64 | * going to touch any visible memory area). Only returns once all of them are |
| 65 | * harmless, with the current thread's bit in threads_harmless_mask cleared. |
| 66 | * Needs to be completed using thread_release(). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 67 | */ |
| 68 | void thread_isolate() |
| 69 | { |
| 70 | unsigned long old; |
| 71 | |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 72 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 73 | __ha_barrier_atomic_store(); |
| 74 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 75 | |
| 76 | /* wait for all threads to become harmless */ |
| 77 | old = threads_harmless_mask; |
| 78 | while (1) { |
| 79 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 80 | old = threads_harmless_mask; |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 81 | else if (_HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 82 | break; |
| 83 | |
Willy Tarreau | 38171da | 2019-05-17 16:33:13 +0200 | [diff] [blame] | 84 | ha_thread_relax(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 85 | } |
| 86 | /* one thread gets released at a time here, with its harmess bit off. |
| 87 | * The loss of this bit makes the other one continue to spin while the |
| 88 | * thread is working alone. |
| 89 | */ |
| 90 | } |
| 91 | |
Willy Tarreau | 88d1c5d | 2021-08-04 11:44:17 +0200 | [diff] [blame] | 92 | /* Isolates the current thread : request the ability to work while all other |
| 93 | * threads are idle, as defined by thread_idle_now(). It only returns once |
| 94 | * all of them are both harmless and idle, with the current thread's bit in |
| 95 | * threads_harmless_mask and idle_mask cleared. Needs to be completed using |
| 96 | * thread_release(). By doing so the thread also engages in being safe against |
| 97 | * any actions that other threads might be about to start under the same |
| 98 | * conditions. This specifically targets destruction of any internal structure, |
| 99 | * which implies that the current thread may not hold references to any object. |
| 100 | * |
| 101 | * Note that a concurrent thread_isolate() will usually win against |
| 102 | * thread_isolate_full() as it doesn't consider the idle_mask, allowing it to |
| 103 | * get back to the poller or any other fully idle location, that will |
| 104 | * ultimately release this one. |
| 105 | */ |
| 106 | void thread_isolate_full() |
| 107 | { |
| 108 | unsigned long old; |
| 109 | |
| 110 | _HA_ATOMIC_OR(&threads_idle_mask, tid_bit); |
| 111 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 112 | __ha_barrier_atomic_store(); |
| 113 | _HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
| 114 | |
| 115 | /* wait for all threads to become harmless */ |
| 116 | old = threads_harmless_mask; |
| 117 | while (1) { |
| 118 | unsigned long idle = _HA_ATOMIC_LOAD(&threads_idle_mask); |
| 119 | |
| 120 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 121 | old = _HA_ATOMIC_LOAD(&threads_harmless_mask); |
| 122 | else if ((idle & all_threads_mask) == all_threads_mask && |
| 123 | _HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
| 124 | break; |
| 125 | |
| 126 | ha_thread_relax(); |
| 127 | } |
| 128 | |
| 129 | /* we're not idle anymore at this point. Other threads waiting on this |
| 130 | * condition will need to wait until out next pass to the poller, or |
| 131 | * our next call to thread_isolate_full(). |
| 132 | */ |
| 133 | _HA_ATOMIC_AND(&threads_idle_mask, ~tid_bit); |
| 134 | } |
| 135 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 136 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
Willy Tarreau | f519cfa | 2021-08-04 11:22:07 +0200 | [diff] [blame] | 137 | * in threads_want_rdv_mask. This immediately allows other threads to expect be |
| 138 | * executed, though they will first have to wait for this thread to become |
| 139 | * harmless again (possibly by reaching the poller again). |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 140 | */ |
| 141 | void thread_release() |
| 142 | { |
Olivier Houchard | b23a61f | 2019-03-08 18:51:17 +0100 | [diff] [blame] | 143 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 144 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 145 | |
Willy Tarreau | 9a1f573 | 2019-06-09 12:20:02 +0200 | [diff] [blame] | 146 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 147 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 148 | * last worker finishes. The difference with thread_release() is that this one |
| 149 | * will not leave the function before others are notified to do the same, so it |
| 150 | * guarantees that the current thread will not pass through a subsequent call |
| 151 | * to thread_isolate() before others finish. |
| 152 | */ |
| 153 | void thread_sync_release() |
| 154 | { |
| 155 | _HA_ATOMIC_OR(&threads_sync_mask, tid_bit); |
| 156 | __ha_barrier_atomic_store(); |
| 157 | _HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
| 158 | |
| 159 | while (threads_want_rdv_mask & all_threads_mask) { |
| 160 | _HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 161 | while (threads_want_rdv_mask & all_threads_mask) |
| 162 | ha_thread_relax(); |
| 163 | HA_ATOMIC_AND(&threads_harmless_mask, ~tid_bit); |
| 164 | } |
| 165 | |
| 166 | /* the current thread is not harmless anymore, thread_isolate() |
| 167 | * is forced to wait till all waiters finish. |
| 168 | */ |
| 169 | _HA_ATOMIC_AND(&threads_sync_mask, ~tid_bit); |
| 170 | while (threads_sync_mask & all_threads_mask) |
| 171 | ha_thread_relax(); |
| 172 | } |
| 173 | |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 174 | /* send signal <sig> to thread <thr> */ |
| 175 | void ha_tkill(unsigned int thr, int sig) |
| 176 | { |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 177 | pthread_kill(ha_thread_info[thr].pthread, sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* send signal <sig> to all threads. The calling thread is signaled last in |
| 181 | * order to allow all threads to synchronize in the handler. |
| 182 | */ |
| 183 | void ha_tkillall(int sig) |
| 184 | { |
| 185 | unsigned int thr; |
| 186 | |
| 187 | for (thr = 0; thr < global.nbthread; thr++) { |
| 188 | if (!(all_threads_mask & (1UL << thr))) |
| 189 | continue; |
| 190 | if (thr == tid) |
| 191 | continue; |
David Carlier | a92c5ce | 2019-09-13 05:03:12 +0100 | [diff] [blame] | 192 | pthread_kill(ha_thread_info[thr].pthread, sig); |
Willy Tarreau | 2beaaf7 | 2019-05-22 08:43:34 +0200 | [diff] [blame] | 193 | } |
| 194 | raise(sig); |
| 195 | } |
| 196 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 197 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 198 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 199 | { |
| 200 | HA_SPIN_INIT(l); |
| 201 | } |
| 202 | |
Willy Tarreau | 3d18498 | 2020-10-18 10:20:59 +0200 | [diff] [blame] | 203 | /* these calls are used as callbacks at init time when debugging is on */ |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 204 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 205 | { |
| 206 | HA_RWLOCK_INIT(l); |
| 207 | } |
| 208 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 209 | /* returns the number of CPUs the current process is enabled to run on */ |
| 210 | static int thread_cpus_enabled() |
| 211 | { |
| 212 | int ret = 1; |
| 213 | |
| 214 | #ifdef USE_CPU_AFFINITY |
| 215 | #if defined(__linux__) && defined(CPU_COUNT) |
| 216 | cpu_set_t mask; |
| 217 | |
| 218 | if (sched_getaffinity(0, sizeof(mask), &mask) == 0) |
| 219 | ret = CPU_COUNT(&mask); |
Olivier Houchard | 46453d3 | 2019-04-11 00:06:47 +0200 | [diff] [blame] | 220 | #elif defined(__FreeBSD__) && defined(USE_CPU_AFFINITY) |
| 221 | cpuset_t cpuset; |
| 222 | if (cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, |
| 223 | sizeof(cpuset), &cpuset) == 0) |
| 224 | ret = CPU_COUNT(&cpuset); |
David CARLIER | 6a90601 | 2021-01-15 08:09:56 +0000 | [diff] [blame] | 225 | #elif defined(__APPLE__) |
| 226 | ret = (int)sysconf(_SC_NPROCESSORS_ONLN); |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 227 | #endif |
| 228 | #endif |
| 229 | ret = MAX(ret, 1); |
| 230 | ret = MIN(ret, MAX_THREADS); |
| 231 | return ret; |
| 232 | } |
| 233 | |
Amaury Denoyelle | 4c9efde | 2021-03-31 16:57:39 +0200 | [diff] [blame] | 234 | /* Returns 1 if the cpu set is currently restricted for the process else 0. |
| 235 | * Currently only implemented for the Linux platform. |
| 236 | */ |
| 237 | int thread_cpu_mask_forced() |
| 238 | { |
| 239 | #if defined(__linux__) |
| 240 | const int cpus_avail = sysconf(_SC_NPROCESSORS_ONLN); |
| 241 | return cpus_avail != thread_cpus_enabled(); |
| 242 | #else |
| 243 | return 0; |
| 244 | #endif |
| 245 | } |
| 246 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 247 | /* Below come the lock-debugging functions */ |
| 248 | |
| 249 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 250 | |
| 251 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 252 | |
| 253 | /* this is only used below */ |
| 254 | static const char *lock_label(enum lock_label label) |
| 255 | { |
| 256 | switch (label) { |
| 257 | case TASK_RQ_LOCK: return "TASK_RQ"; |
| 258 | case TASK_WQ_LOCK: return "TASK_WQ"; |
| 259 | case LISTENER_LOCK: return "LISTENER"; |
| 260 | case PROXY_LOCK: return "PROXY"; |
| 261 | case SERVER_LOCK: return "SERVER"; |
| 262 | case LBPRM_LOCK: return "LBPRM"; |
| 263 | case SIGNALS_LOCK: return "SIGNALS"; |
| 264 | case STK_TABLE_LOCK: return "STK_TABLE"; |
| 265 | case STK_SESS_LOCK: return "STK_SESS"; |
| 266 | case APPLETS_LOCK: return "APPLETS"; |
| 267 | case PEER_LOCK: return "PEER"; |
| 268 | case SHCTX_LOCK: return "SHCTX"; |
| 269 | case SSL_LOCK: return "SSL"; |
| 270 | case SSL_GEN_CERTS_LOCK: return "SSL_GEN_CERTS"; |
| 271 | case PATREF_LOCK: return "PATREF"; |
| 272 | case PATEXP_LOCK: return "PATEXP"; |
| 273 | case VARS_LOCK: return "VARS"; |
| 274 | case COMP_POOL_LOCK: return "COMP_POOL"; |
| 275 | case LUA_LOCK: return "LUA"; |
| 276 | case NOTIF_LOCK: return "NOTIF"; |
| 277 | case SPOE_APPLET_LOCK: return "SPOE_APPLET"; |
| 278 | case DNS_LOCK: return "DNS"; |
| 279 | case PID_LIST_LOCK: return "PID_LIST"; |
| 280 | case EMAIL_ALERTS_LOCK: return "EMAIL_ALERTS"; |
| 281 | case PIPES_LOCK: return "PIPES"; |
| 282 | case TLSKEYS_REF_LOCK: return "TLSKEYS_REF"; |
| 283 | case AUTH_LOCK: return "AUTH"; |
| 284 | case LOGSRV_LOCK: return "LOGSRV"; |
| 285 | case DICT_LOCK: return "DICT"; |
| 286 | case PROTO_LOCK: return "PROTO"; |
| 287 | case QUEUE_LOCK: return "QUEUE"; |
| 288 | case CKCH_LOCK: return "CKCH"; |
| 289 | case SNI_LOCK: return "SNI"; |
| 290 | case SSL_SERVER_LOCK: return "SSL_SERVER"; |
| 291 | case SFT_LOCK: return "SFT"; |
| 292 | case IDLE_CONNS_LOCK: return "IDLE_CONNS"; |
| 293 | case QUIC_LOCK: return "QUIC"; |
| 294 | case OTHER_LOCK: return "OTHER"; |
| 295 | case DEBUG1_LOCK: return "DEBUG1"; |
| 296 | case DEBUG2_LOCK: return "DEBUG2"; |
| 297 | case DEBUG3_LOCK: return "DEBUG3"; |
| 298 | case DEBUG4_LOCK: return "DEBUG4"; |
| 299 | case DEBUG5_LOCK: return "DEBUG5"; |
| 300 | case LOCK_LABELS: break; /* keep compiler happy */ |
| 301 | }; |
| 302 | /* only way to come here is consecutive to an internal bug */ |
| 303 | abort(); |
| 304 | } |
| 305 | |
| 306 | void show_lock_stats() |
| 307 | { |
| 308 | int lbl; |
| 309 | |
| 310 | for (lbl = 0; lbl < LOCK_LABELS; lbl++) { |
| 311 | if (!lock_stats[lbl].num_write_locked && |
| 312 | !lock_stats[lbl].num_seek_locked && |
| 313 | !lock_stats[lbl].num_read_locked) { |
| 314 | fprintf(stderr, |
| 315 | "Stats about Lock %s: not used\n", |
| 316 | lock_label(lbl)); |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | fprintf(stderr, |
| 321 | "Stats about Lock %s: \n", |
| 322 | lock_label(lbl)); |
| 323 | |
| 324 | if (lock_stats[lbl].num_write_locked) |
| 325 | fprintf(stderr, |
| 326 | "\t # write lock : %lu\n" |
| 327 | "\t # write unlock: %lu (%ld)\n" |
| 328 | "\t # wait time for write : %.3f msec\n" |
| 329 | "\t # wait time for write/lock: %.3f nsec\n", |
| 330 | lock_stats[lbl].num_write_locked, |
| 331 | lock_stats[lbl].num_write_unlocked, |
| 332 | lock_stats[lbl].num_write_unlocked - lock_stats[lbl].num_write_locked, |
| 333 | (double)lock_stats[lbl].nsec_wait_for_write / 1000000.0, |
| 334 | lock_stats[lbl].num_write_locked ? ((double)lock_stats[lbl].nsec_wait_for_write / (double)lock_stats[lbl].num_write_locked) : 0); |
| 335 | |
| 336 | if (lock_stats[lbl].num_seek_locked) |
| 337 | fprintf(stderr, |
| 338 | "\t # seek lock : %lu\n" |
| 339 | "\t # seek unlock : %lu (%ld)\n" |
| 340 | "\t # wait time for seek : %.3f msec\n" |
| 341 | "\t # wait time for seek/lock : %.3f nsec\n", |
| 342 | lock_stats[lbl].num_seek_locked, |
| 343 | lock_stats[lbl].num_seek_unlocked, |
| 344 | lock_stats[lbl].num_seek_unlocked - lock_stats[lbl].num_seek_locked, |
| 345 | (double)lock_stats[lbl].nsec_wait_for_seek / 1000000.0, |
| 346 | lock_stats[lbl].num_seek_locked ? ((double)lock_stats[lbl].nsec_wait_for_seek / (double)lock_stats[lbl].num_seek_locked) : 0); |
| 347 | |
| 348 | if (lock_stats[lbl].num_read_locked) |
| 349 | fprintf(stderr, |
| 350 | "\t # read lock : %lu\n" |
| 351 | "\t # read unlock : %lu (%ld)\n" |
| 352 | "\t # wait time for read : %.3f msec\n" |
| 353 | "\t # wait time for read/lock : %.3f nsec\n", |
| 354 | lock_stats[lbl].num_read_locked, |
| 355 | lock_stats[lbl].num_read_unlocked, |
| 356 | lock_stats[lbl].num_read_unlocked - lock_stats[lbl].num_read_locked, |
| 357 | (double)lock_stats[lbl].nsec_wait_for_read / 1000000.0, |
| 358 | lock_stats[lbl].num_read_locked ? ((double)lock_stats[lbl].nsec_wait_for_read / (double)lock_stats[lbl].num_read_locked) : 0); |
| 359 | } |
| 360 | } |
| 361 | |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 362 | void __ha_rwlock_init(struct ha_rwlock *l) |
| 363 | { |
| 364 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 365 | __RWLOCK_INIT(&l->lock); |
| 366 | } |
| 367 | |
| 368 | void __ha_rwlock_destroy(struct ha_rwlock *l) |
| 369 | { |
| 370 | __RWLOCK_DESTROY(&l->lock); |
| 371 | memset(l, 0, sizeof(struct ha_rwlock)); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | void __ha_rwlock_wrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 376 | const char *func, const char *file, int line) |
| 377 | { |
| 378 | uint64_t start_time; |
| 379 | |
| 380 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 381 | abort(); |
| 382 | |
| 383 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 384 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 385 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 386 | __RWLOCK_WRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 387 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 388 | |
| 389 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 390 | |
| 391 | l->info.cur_writer = tid_bit; |
| 392 | l->info.last_location.function = func; |
| 393 | l->info.last_location.file = file; |
| 394 | l->info.last_location.line = line; |
| 395 | |
| 396 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 397 | } |
| 398 | |
| 399 | int __ha_rwlock_trywrlock(enum lock_label lbl, struct ha_rwlock *l, |
| 400 | const char *func, const char *file, int line) |
| 401 | { |
| 402 | uint64_t start_time; |
| 403 | int r; |
| 404 | |
| 405 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 406 | abort(); |
| 407 | |
| 408 | /* We set waiting writer because trywrlock could wait for readers to quit */ |
| 409 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 410 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 411 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 412 | r = __RWLOCK_TRYWRLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 413 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 414 | if (unlikely(r)) { |
| 415 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 416 | return r; |
| 417 | } |
| 418 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 419 | |
| 420 | l->info.cur_writer = tid_bit; |
| 421 | l->info.last_location.function = func; |
| 422 | l->info.last_location.file = file; |
| 423 | l->info.last_location.line = line; |
| 424 | |
| 425 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | void __ha_rwlock_wrunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 431 | const char *func, const char *file, int line) |
| 432 | { |
| 433 | if (unlikely(!(l->info.cur_writer & tid_bit))) { |
| 434 | /* the thread is not owning the lock for write */ |
| 435 | abort(); |
| 436 | } |
| 437 | |
| 438 | l->info.cur_writer = 0; |
| 439 | l->info.last_location.function = func; |
| 440 | l->info.last_location.file = file; |
| 441 | l->info.last_location.line = line; |
| 442 | |
| 443 | __RWLOCK_WRUNLOCK(&l->lock); |
| 444 | |
| 445 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 446 | } |
| 447 | |
| 448 | void __ha_rwlock_rdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 449 | { |
| 450 | uint64_t start_time; |
| 451 | |
| 452 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 453 | abort(); |
| 454 | |
| 455 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 456 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 457 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 458 | __RWLOCK_RDLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 459 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 460 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 461 | |
| 462 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 463 | |
| 464 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 465 | } |
| 466 | |
| 467 | int __ha_rwlock_tryrdlock(enum lock_label lbl,struct ha_rwlock *l) |
| 468 | { |
| 469 | int r; |
| 470 | |
| 471 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 472 | abort(); |
| 473 | |
| 474 | /* try read should never wait */ |
| 475 | r = __RWLOCK_TRYRDLOCK(&l->lock); |
| 476 | if (unlikely(r)) |
| 477 | return r; |
| 478 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 479 | |
| 480 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | void __ha_rwlock_rdunlock(enum lock_label lbl,struct ha_rwlock *l) |
| 486 | { |
| 487 | if (unlikely(!(l->info.cur_readers & tid_bit))) { |
| 488 | /* the thread is not owning the lock for read */ |
| 489 | abort(); |
| 490 | } |
| 491 | |
| 492 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 493 | |
| 494 | __RWLOCK_RDUNLOCK(&l->lock); |
| 495 | |
| 496 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_unlocked); |
| 497 | } |
| 498 | |
| 499 | void __ha_rwlock_wrtord(enum lock_label lbl, struct ha_rwlock *l, |
| 500 | const char *func, const char *file, int line) |
| 501 | { |
| 502 | uint64_t start_time; |
| 503 | |
| 504 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 505 | abort(); |
| 506 | |
| 507 | if (!(l->info.cur_writer & tid_bit)) |
| 508 | abort(); |
| 509 | |
| 510 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 511 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 512 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 513 | __RWLOCK_WRTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 514 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 515 | |
| 516 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 517 | |
| 518 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 519 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 520 | l->info.last_location.function = func; |
| 521 | l->info.last_location.file = file; |
| 522 | l->info.last_location.line = line; |
| 523 | |
| 524 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 525 | } |
| 526 | |
| 527 | void __ha_rwlock_wrtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 528 | const char *func, const char *file, int line) |
| 529 | { |
| 530 | uint64_t start_time; |
| 531 | |
| 532 | if ((l->info.cur_readers | l->info.cur_seeker) & tid_bit) |
| 533 | abort(); |
| 534 | |
| 535 | if (!(l->info.cur_writer & tid_bit)) |
| 536 | abort(); |
| 537 | |
| 538 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 539 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 540 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 541 | __RWLOCK_WRTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 542 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 543 | |
| 544 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 545 | |
| 546 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 547 | HA_ATOMIC_AND(&l->info.cur_writer, ~tid_bit); |
| 548 | l->info.last_location.function = func; |
| 549 | l->info.last_location.file = file; |
| 550 | l->info.last_location.line = line; |
| 551 | |
| 552 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 553 | } |
| 554 | |
| 555 | void __ha_rwlock_sklock(enum lock_label lbl, struct ha_rwlock *l, |
| 556 | const char *func, const char *file, int line) |
| 557 | { |
| 558 | uint64_t start_time; |
| 559 | |
| 560 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 561 | abort(); |
| 562 | |
| 563 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 564 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 565 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 566 | __RWLOCK_SKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 567 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 568 | |
| 569 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 570 | |
| 571 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 572 | l->info.last_location.function = func; |
| 573 | l->info.last_location.file = file; |
| 574 | l->info.last_location.line = line; |
| 575 | |
| 576 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 577 | } |
| 578 | |
| 579 | void __ha_rwlock_sktowr(enum lock_label lbl, struct ha_rwlock *l, |
| 580 | const char *func, const char *file, int line) |
| 581 | { |
| 582 | uint64_t start_time; |
| 583 | |
| 584 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 585 | abort(); |
| 586 | |
| 587 | if (!(l->info.cur_seeker & tid_bit)) |
| 588 | abort(); |
| 589 | |
| 590 | HA_ATOMIC_OR(&l->info.wait_writers, tid_bit); |
| 591 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 592 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 593 | __RWLOCK_SKTOWR(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 594 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 595 | |
| 596 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 597 | |
| 598 | HA_ATOMIC_OR(&l->info.cur_writer, tid_bit); |
| 599 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 600 | l->info.last_location.function = func; |
| 601 | l->info.last_location.file = file; |
| 602 | l->info.last_location.line = line; |
| 603 | |
| 604 | HA_ATOMIC_AND(&l->info.wait_writers, ~tid_bit); |
| 605 | } |
| 606 | |
| 607 | void __ha_rwlock_sktord(enum lock_label lbl, struct ha_rwlock *l, |
| 608 | const char *func, const char *file, int line) |
| 609 | { |
| 610 | uint64_t start_time; |
| 611 | |
| 612 | if ((l->info.cur_readers | l->info.cur_writer) & tid_bit) |
| 613 | abort(); |
| 614 | |
| 615 | if (!(l->info.cur_seeker & tid_bit)) |
| 616 | abort(); |
| 617 | |
| 618 | HA_ATOMIC_OR(&l->info.wait_readers, tid_bit); |
| 619 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 620 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 621 | __RWLOCK_SKTORD(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 622 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_read, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 623 | |
| 624 | HA_ATOMIC_INC(&lock_stats[lbl].num_read_locked); |
| 625 | |
| 626 | HA_ATOMIC_OR(&l->info.cur_readers, tid_bit); |
| 627 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 628 | l->info.last_location.function = func; |
| 629 | l->info.last_location.file = file; |
| 630 | l->info.last_location.line = line; |
| 631 | |
| 632 | HA_ATOMIC_AND(&l->info.wait_readers, ~tid_bit); |
| 633 | } |
| 634 | |
| 635 | void __ha_rwlock_skunlock(enum lock_label lbl,struct ha_rwlock *l, |
| 636 | const char *func, const char *file, int line) |
| 637 | { |
| 638 | if (!(l->info.cur_seeker & tid_bit)) |
| 639 | abort(); |
| 640 | |
| 641 | HA_ATOMIC_AND(&l->info.cur_seeker, ~tid_bit); |
| 642 | l->info.last_location.function = func; |
| 643 | l->info.last_location.file = file; |
| 644 | l->info.last_location.line = line; |
| 645 | |
| 646 | __RWLOCK_SKUNLOCK(&l->lock); |
| 647 | |
| 648 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_unlocked); |
| 649 | } |
| 650 | |
| 651 | int __ha_rwlock_trysklock(enum lock_label lbl, struct ha_rwlock *l, |
| 652 | const char *func, const char *file, int line) |
| 653 | { |
| 654 | uint64_t start_time; |
| 655 | int r; |
| 656 | |
| 657 | if ((l->info.cur_readers | l->info.cur_seeker | l->info.cur_writer) & tid_bit) |
| 658 | abort(); |
| 659 | |
| 660 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 661 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 662 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 663 | r = __RWLOCK_TRYSKLOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 664 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 665 | |
| 666 | if (likely(!r)) { |
| 667 | /* got the lock ! */ |
| 668 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 669 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 670 | l->info.last_location.function = func; |
| 671 | l->info.last_location.file = file; |
| 672 | l->info.last_location.line = line; |
| 673 | } |
| 674 | |
| 675 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 676 | return r; |
| 677 | } |
| 678 | |
| 679 | int __ha_rwlock_tryrdtosk(enum lock_label lbl, struct ha_rwlock *l, |
| 680 | const char *func, const char *file, int line) |
| 681 | { |
| 682 | uint64_t start_time; |
| 683 | int r; |
| 684 | |
| 685 | if ((l->info.cur_writer | l->info.cur_seeker) & tid_bit) |
| 686 | abort(); |
| 687 | |
| 688 | if (!(l->info.cur_readers & tid_bit)) |
| 689 | abort(); |
| 690 | |
| 691 | HA_ATOMIC_OR(&l->info.wait_seekers, tid_bit); |
| 692 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 693 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 694 | r = __RWLOCK_TRYRDTOSK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 695 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_seek, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 696 | |
| 697 | if (likely(!r)) { |
| 698 | /* got the lock ! */ |
| 699 | HA_ATOMIC_INC(&lock_stats[lbl].num_seek_locked); |
| 700 | HA_ATOMIC_OR(&l->info.cur_seeker, tid_bit); |
| 701 | HA_ATOMIC_AND(&l->info.cur_readers, ~tid_bit); |
| 702 | l->info.last_location.function = func; |
| 703 | l->info.last_location.file = file; |
| 704 | l->info.last_location.line = line; |
| 705 | } |
| 706 | |
| 707 | HA_ATOMIC_AND(&l->info.wait_seekers, ~tid_bit); |
| 708 | return r; |
| 709 | } |
| 710 | |
| 711 | void __spin_init(struct ha_spinlock *l) |
| 712 | { |
| 713 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 714 | __SPIN_INIT(&l->lock); |
| 715 | } |
| 716 | |
| 717 | void __spin_destroy(struct ha_spinlock *l) |
| 718 | { |
| 719 | __SPIN_DESTROY(&l->lock); |
| 720 | memset(l, 0, sizeof(struct ha_spinlock)); |
| 721 | } |
| 722 | |
| 723 | void __spin_lock(enum lock_label lbl, struct ha_spinlock *l, |
| 724 | const char *func, const char *file, int line) |
| 725 | { |
| 726 | uint64_t start_time; |
| 727 | |
| 728 | if (unlikely(l->info.owner & tid_bit)) { |
| 729 | /* the thread is already owning the lock */ |
| 730 | abort(); |
| 731 | } |
| 732 | |
| 733 | HA_ATOMIC_OR(&l->info.waiters, tid_bit); |
| 734 | |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 735 | start_time = now_mono_time(); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 736 | __SPIN_LOCK(&l->lock); |
Willy Tarreau | dced3eb | 2021-10-05 18:48:23 +0200 | [diff] [blame^] | 737 | HA_ATOMIC_ADD(&lock_stats[lbl].nsec_wait_for_write, (now_mono_time() - start_time)); |
Willy Tarreau | 407ef89 | 2021-10-05 18:39:27 +0200 | [diff] [blame] | 738 | |
| 739 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 740 | |
| 741 | |
| 742 | l->info.owner = tid_bit; |
| 743 | l->info.last_location.function = func; |
| 744 | l->info.last_location.file = file; |
| 745 | l->info.last_location.line = line; |
| 746 | |
| 747 | HA_ATOMIC_AND(&l->info.waiters, ~tid_bit); |
| 748 | } |
| 749 | |
| 750 | int __spin_trylock(enum lock_label lbl, struct ha_spinlock *l, |
| 751 | const char *func, const char *file, int line) |
| 752 | { |
| 753 | int r; |
| 754 | |
| 755 | if (unlikely(l->info.owner & tid_bit)) { |
| 756 | /* the thread is already owning the lock */ |
| 757 | abort(); |
| 758 | } |
| 759 | |
| 760 | /* try read should never wait */ |
| 761 | r = __SPIN_TRYLOCK(&l->lock); |
| 762 | if (unlikely(r)) |
| 763 | return r; |
| 764 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_locked); |
| 765 | |
| 766 | l->info.owner = tid_bit; |
| 767 | l->info.last_location.function = func; |
| 768 | l->info.last_location.file = file; |
| 769 | l->info.last_location.line = line; |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | void __spin_unlock(enum lock_label lbl, struct ha_spinlock *l, |
| 775 | const char *func, const char *file, int line) |
| 776 | { |
| 777 | if (unlikely(!(l->info.owner & tid_bit))) { |
| 778 | /* the thread is not owning the lock */ |
| 779 | abort(); |
| 780 | } |
| 781 | |
| 782 | l->info.owner = 0; |
| 783 | l->info.last_location.function = func; |
| 784 | l->info.last_location.file = file; |
| 785 | l->info.last_location.line = line; |
| 786 | |
| 787 | __SPIN_UNLOCK(&l->lock); |
| 788 | HA_ATOMIC_INC(&lock_stats[lbl].num_write_unlocked); |
| 789 | } |
| 790 | |
| 791 | #endif // defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 792 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 793 | /* Depending on the platform and how libpthread was built, pthread_exit() may |
| 794 | * involve some code in libgcc_s that would be loaded on exit for the first |
| 795 | * time, causing aborts if the process is chrooted. It's harmless bit very |
| 796 | * dirty. There isn't much we can do to make sure libgcc_s is loaded only if |
| 797 | * needed, so what we do here is that during early boot we create a dummy |
| 798 | * thread that immediately exits. This will lead to libgcc_s being loaded |
| 799 | * during boot on the platforms where it's required. |
| 800 | */ |
| 801 | static void *dummy_thread_function(void *data) |
| 802 | { |
| 803 | pthread_exit(NULL); |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | static inline void preload_libgcc_s(void) |
| 808 | { |
| 809 | pthread_t dummy_thread; |
| 810 | pthread_create(&dummy_thread, NULL, dummy_thread_function, NULL); |
| 811 | pthread_join(dummy_thread, NULL); |
| 812 | } |
| 813 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 814 | __attribute__((constructor)) |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 815 | static void __thread_init(void) |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 816 | { |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 817 | char *ptr = NULL; |
| 818 | |
| 819 | if (MAX_THREADS < 1 || MAX_THREADS > LONGBITS) { |
| 820 | ha_alert("MAX_THREADS value must be between 1 and %d inclusive; " |
| 821 | "HAProxy was built with value %d, please fix it and rebuild.\n", |
| 822 | LONGBITS, MAX_THREADS); |
| 823 | exit(1); |
| 824 | } |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 825 | |
Willy Tarreau | f734ebf | 2020-09-09 17:07:54 +0200 | [diff] [blame] | 826 | preload_libgcc_s(); |
Willy Tarreau | 77b9822 | 2020-09-02 08:04:35 +0200 | [diff] [blame] | 827 | |
Willy Tarreau | 149ab77 | 2019-01-26 14:27:06 +0100 | [diff] [blame] | 828 | thread_cpus_enabled_at_boot = thread_cpus_enabled(); |
| 829 | |
| 830 | memprintf(&ptr, "Built with multi-threading support (MAX_THREADS=%d, default=%d).", |
| 831 | MAX_THREADS, thread_cpus_enabled_at_boot); |
Willy Tarreau | f5809cd | 2019-01-26 13:35:03 +0100 | [diff] [blame] | 832 | hap_register_build_opts(ptr, 1); |
| 833 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 834 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 835 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 836 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 839 | #else |
| 840 | |
| 841 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 842 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 843 | #endif // USE_THREAD |
| 844 | |
| 845 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 846 | /* Parse the "nbthread" global directive, which takes an integer argument that |
| 847 | * contains the desired number of threads. |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 848 | */ |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 849 | static int cfg_parse_nbthread(char **args, int section_type, struct proxy *curpx, |
| 850 | const struct proxy *defpx, const char *file, int line, |
| 851 | char **err) |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 852 | { |
| 853 | long nbthread; |
| 854 | char *errptr; |
| 855 | |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 856 | if (too_many_args(1, args, err, NULL)) |
| 857 | return -1; |
| 858 | |
| 859 | nbthread = strtol(args[1], &errptr, 10); |
| 860 | if (!*args[1] || *errptr) { |
| 861 | memprintf(err, "'%s' passed a missing or unparsable integer value in '%s'", args[0], args[1]); |
| 862 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | #ifndef USE_THREAD |
| 866 | if (nbthread != 1) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 867 | memprintf(err, "'%s' specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD", args[0]); |
| 868 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 869 | } |
| 870 | #else |
| 871 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 872 | memprintf(err, "'%s' value must be between 1 and %d (was %ld)", args[0], MAX_THREADS, nbthread); |
| 873 | return -1; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | fc64736 | 2019-02-02 17:05:03 +0100 | [diff] [blame] | 876 | all_threads_mask = nbits(nbthread); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 877 | #endif |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 878 | |
| 879 | HA_DIAG_WARNING_COND(global.nbthread, |
| 880 | "parsing [%s:%d] : nbthread is already defined and will be overridden.\n", |
| 881 | file, line); |
| 882 | |
| 883 | global.nbthread = nbthread; |
| 884 | return 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 885 | } |
Willy Tarreau | 51ec03a | 2021-09-22 11:55:22 +0200 | [diff] [blame] | 886 | |
| 887 | /* config keyword parsers */ |
| 888 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 889 | { CFG_GLOBAL, "nbthread", cfg_parse_nbthread, 0 }, |
| 890 | { 0, NULL, NULL } |
| 891 | }}; |
| 892 | |
| 893 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |