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 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 14 | #include <stdlib.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 15 | #include <fcntl.h> |
| 16 | |
Willy Tarreau | 0493149 | 2017-11-03 23:39:25 +0100 | [diff] [blame] | 17 | #include <common/cfgparse.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 18 | #include <common/hathreads.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 19 | #include <common/standard.h> |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 20 | #include <types/global.h> |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 21 | #include <proto/fd.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 22 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef USE_THREAD |
| 25 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 26 | volatile unsigned long threads_want_rdv_mask = 0; |
| 27 | volatile unsigned long threads_harmless_mask = 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 28 | volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame] | 29 | THREAD_LOCAL unsigned int tid = 0; |
| 30 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
| 31 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 32 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 33 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 34 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 35 | #endif |
| 36 | |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 37 | /* Marks the thread as harmless until the last thread using the rendez-vous |
| 38 | * point quits. Given that we can wait for a long time, sched_yield() is used |
| 39 | * when available to offer the CPU resources to competing threads if needed. |
| 40 | */ |
| 41 | void thread_harmless_till_end() |
| 42 | { |
| 43 | HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 44 | while (threads_want_rdv_mask & all_threads_mask) { |
| 45 | #if _POSIX_PRIORITY_SCHEDULING |
| 46 | sched_yield(); |
| 47 | #else |
| 48 | pl_cpu_relax(); |
| 49 | #endif |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /* Isolates the current thread : request the ability to work while all other |
| 54 | * threads are harmless. Only returns once all of them are harmless, with the |
| 55 | * current thread's bit in threads_harmless_mask cleared. Needs to be completed |
| 56 | * using thread_release(). |
| 57 | */ |
| 58 | void thread_isolate() |
| 59 | { |
| 60 | unsigned long old; |
| 61 | |
| 62 | HA_ATOMIC_OR(&threads_harmless_mask, tid_bit); |
| 63 | __ha_barrier_store(); |
| 64 | HA_ATOMIC_OR(&threads_want_rdv_mask, tid_bit); |
| 65 | |
| 66 | /* wait for all threads to become harmless */ |
| 67 | old = threads_harmless_mask; |
| 68 | while (1) { |
| 69 | if (unlikely((old & all_threads_mask) != all_threads_mask)) |
| 70 | old = threads_harmless_mask; |
| 71 | else if (HA_ATOMIC_CAS(&threads_harmless_mask, &old, old & ~tid_bit)) |
| 72 | break; |
| 73 | |
| 74 | #if _POSIX_PRIORITY_SCHEDULING |
| 75 | sched_yield(); |
| 76 | #else |
| 77 | pl_cpu_relax(); |
| 78 | #endif |
| 79 | } |
| 80 | /* one thread gets released at a time here, with its harmess bit off. |
| 81 | * The loss of this bit makes the other one continue to spin while the |
| 82 | * thread is working alone. |
| 83 | */ |
| 84 | } |
| 85 | |
| 86 | /* Cancels the effect of thread_isolate() by releasing the current thread's bit |
| 87 | * in threads_want_rdv_mask and by marking this thread as harmless until the |
| 88 | * last worker finishes. |
| 89 | */ |
| 90 | void thread_release() |
| 91 | { |
Willy Tarreau | a9c0252 | 2018-10-16 16:11:56 +0200 | [diff] [blame] | 92 | HA_ATOMIC_AND(&threads_want_rdv_mask, ~tid_bit); |
| 93 | thread_harmless_end(); |
Willy Tarreau | 60b639c | 2018-08-02 10:16:17 +0200 | [diff] [blame] | 94 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | a8ae77d | 2018-11-25 19:28:23 +0100 | [diff] [blame] | 96 | /* these calls are used as callbacks at init time */ |
| 97 | void ha_spin_init(HA_SPINLOCK_T *l) |
| 98 | { |
| 99 | HA_SPIN_INIT(l); |
| 100 | } |
| 101 | |
| 102 | /* these calls are used as callbacks at init time */ |
| 103 | void ha_rwlock_init(HA_RWLOCK_T *l) |
| 104 | { |
| 105 | HA_RWLOCK_INIT(l); |
| 106 | } |
| 107 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 108 | __attribute__((constructor)) |
| 109 | static void __hathreads_init(void) |
| 110 | { |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 111 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 112 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 113 | #endif |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Willy Tarreau | 8459f25 | 2018-12-15 16:48:14 +0100 | [diff] [blame] | 116 | REGISTER_BUILD_OPTS("Built with multi-threading support."); |
| 117 | |
| 118 | #else |
| 119 | |
| 120 | REGISTER_BUILD_OPTS("Built without multi-threading support (USE_THREAD not set)."); |
| 121 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 122 | #endif // USE_THREAD |
| 123 | |
| 124 | |
| 125 | /* Parse the number of threads in argument <arg>, returns it and adjusts a few |
| 126 | * internal variables accordingly, or fails and returns zero with an error |
| 127 | * reason in <errmsg>. May be called multiple times while parsing. |
| 128 | */ |
| 129 | int parse_nbthread(const char *arg, char **err) |
| 130 | { |
| 131 | long nbthread; |
| 132 | char *errptr; |
| 133 | |
| 134 | nbthread = strtol(arg, &errptr, 10); |
| 135 | if (!*arg || *errptr) { |
| 136 | memprintf(err, "passed a missing or unparsable integer value in '%s'", arg); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | #ifndef USE_THREAD |
| 141 | if (nbthread != 1) { |
| 142 | memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD"); |
| 143 | return 0; |
| 144 | } |
| 145 | #else |
| 146 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
| 147 | memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* we proceed like this to be sure never to overflow the left shift */ |
| 152 | all_threads_mask = 1UL << (nbthread - 1); |
| 153 | all_threads_mask |= all_threads_mask - 1; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 154 | #endif |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 155 | return nbthread; |
| 156 | } |