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> |
| 20 | #include <proto/fd.h> |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 21 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 22 | |
Willy Tarreau | e96e61c | 2018-03-29 18:54:33 +0200 | [diff] [blame] | 23 | /* Dummy I/O handler used by the sync pipe.*/ |
| 24 | void thread_sync_io_handler(int fd) |
| 25 | { |
| 26 | } |
| 27 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 28 | #ifdef USE_THREAD |
| 29 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 30 | static HA_SPINLOCK_T sync_lock; |
| 31 | static int threads_sync_pipe[2]; |
| 32 | static unsigned long threads_want_sync = 0; |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 33 | volatile unsigned long all_threads_mask = 1; // nbthread 1 assumed by default |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame^] | 34 | THREAD_LOCAL unsigned int tid = 0; |
| 35 | THREAD_LOCAL unsigned long tid_bit = (1UL << 0); |
| 36 | |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 37 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 38 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 39 | struct lock_stat lock_stats[LOCK_LABELS]; |
| 40 | #endif |
| 41 | |
Olivier Houchard | 3e12304 | 2018-07-27 17:06:59 +0200 | [diff] [blame] | 42 | /* Initializes the sync point. It creates a pipe used by threads to wake up all |
| 43 | * others when a sync is requested. It also initializes the mask of all created |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 44 | * threads. It returns 0 on success and -1 if an error occurred. |
| 45 | */ |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 46 | int thread_sync_init() |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 47 | { |
| 48 | int rfd; |
| 49 | |
| 50 | if (pipe(threads_sync_pipe) < 0) |
| 51 | return -1; |
| 52 | |
| 53 | rfd = threads_sync_pipe[0]; |
| 54 | fcntl(rfd, F_SETFL, O_NONBLOCK); |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 55 | fd_insert(rfd, thread_sync_io_handler, thread_sync_io_handler, MAX_THREADS_MASK); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* Enables the sync point. */ |
| 60 | void thread_sync_enable(void) |
| 61 | { |
| 62 | fd_want_recv(threads_sync_pipe[0]); |
| 63 | } |
| 64 | |
| 65 | /* Called when a thread want to pass into the sync point. It subscribes the |
| 66 | * current thread in threads waiting for sync by update a bit-field. It this is |
| 67 | * the first one, it wakeup all other threads by writing on the sync pipe. |
| 68 | */ |
| 69 | void thread_want_sync() |
| 70 | { |
| 71 | if (all_threads_mask) { |
Christopher Faulet | 81991d3 | 2017-12-02 09:53:24 +0100 | [diff] [blame] | 72 | if (threads_want_sync & tid_bit) |
| 73 | return; |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 74 | if (HA_ATOMIC_OR(&threads_want_sync, tid_bit) == tid_bit) |
| 75 | shut_your_big_mouth_gcc(write(threads_sync_pipe[1], "S", 1)); |
| 76 | } |
| 77 | else { |
| 78 | threads_want_sync = 1; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* Returns 1 if no thread has requested a sync. Otherwise, it returns 0. */ |
| 83 | int thread_no_sync() |
| 84 | { |
Christopher Faulet | 148b16e | 2018-05-02 16:58:40 +0200 | [diff] [blame] | 85 | return (threads_want_sync == 0UL); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Returns 1 if the current thread has requested a sync. Otherwise, it returns |
| 89 | * 0. |
| 90 | */ |
| 91 | int thread_need_sync() |
| 92 | { |
Christopher Faulet | 148b16e | 2018-05-02 16:58:40 +0200 | [diff] [blame] | 93 | return ((threads_want_sync & tid_bit) != 0UL); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /* Thread barrier. Synchronizes all threads at the barrier referenced by |
| 97 | * <barrier>. The calling thread shall block until all other threads have called |
| 98 | * thread_sync_barrier specifying the same barrier. |
| 99 | * |
| 100 | * If you need to use several barriers at differnt points, you need to use a |
| 101 | * different <barrier> for each point. |
| 102 | */ |
| 103 | static inline void thread_sync_barrier(volatile unsigned long *barrier) |
| 104 | { |
| 105 | unsigned long old = all_threads_mask; |
| 106 | |
| 107 | HA_ATOMIC_CAS(barrier, &old, 0); |
Christopher Faulet | 209d02a | 2017-10-27 23:01:38 +0200 | [diff] [blame] | 108 | HA_ATOMIC_OR(barrier, tid_bit); |
Willy Tarreau | 3ea2490 | 2018-07-27 07:47:24 +0200 | [diff] [blame] | 109 | |
| 110 | /* Note below: we need to wait for all threads to join here, but in |
| 111 | * case several threads are scheduled on the same CPU, busy polling |
| 112 | * will instead degrade the performance, forcing other threads to |
| 113 | * wait longer (typically in epoll_wait()). Let's use sched_yield() |
| 114 | * when available instead. |
| 115 | */ |
| 116 | while ((*barrier & all_threads_mask) != all_threads_mask) { |
| 117 | #if _POSIX_PRIORITY_SCHEDULING |
| 118 | sched_yield(); |
| 119 | #else |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 120 | pl_cpu_relax(); |
Willy Tarreau | 3ea2490 | 2018-07-27 07:47:24 +0200 | [diff] [blame] | 121 | #endif |
| 122 | } |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Enter into the sync point and lock it if the current thread has requested a |
| 126 | * sync. */ |
| 127 | void thread_enter_sync() |
| 128 | { |
| 129 | static volatile unsigned long barrier = 0; |
| 130 | |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame^] | 131 | if (!(all_threads_mask & (all_threads_mask - 1))) |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 132 | return; |
| 133 | |
| 134 | thread_sync_barrier(&barrier); |
| 135 | if (threads_want_sync & tid_bit) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 136 | HA_SPIN_LOCK(THREAD_SYNC_LOCK, &sync_lock); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Exit from the sync point and unlock it if it was previously locked. If the |
| 140 | * current thread is the last one to have requested a sync, the sync pipe is |
| 141 | * flushed. |
| 142 | */ |
| 143 | void thread_exit_sync() |
| 144 | { |
| 145 | static volatile unsigned long barrier = 0; |
| 146 | |
Willy Tarreau | 0c026f4 | 2018-08-01 19:12:20 +0200 | [diff] [blame^] | 147 | if (!(all_threads_mask & (all_threads_mask - 1))) |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | if (threads_want_sync & tid_bit) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 151 | HA_SPIN_UNLOCK(THREAD_SYNC_LOCK, &sync_lock); |
Christopher Faulet | 339fff8 | 2017-10-19 11:59:15 +0200 | [diff] [blame] | 152 | |
| 153 | if (HA_ATOMIC_AND(&threads_want_sync, ~tid_bit) == 0) { |
| 154 | char c; |
| 155 | |
| 156 | shut_your_big_mouth_gcc(read(threads_sync_pipe[0], &c, 1)); |
| 157 | fd_done_recv(threads_sync_pipe[0]); |
| 158 | } |
| 159 | |
| 160 | thread_sync_barrier(&barrier); |
| 161 | } |
| 162 | |
| 163 | |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 164 | __attribute__((constructor)) |
| 165 | static void __hathreads_init(void) |
| 166 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 167 | HA_SPIN_INIT(&sync_lock); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 168 | #if defined(DEBUG_THREAD) || defined(DEBUG_FULL) |
| 169 | memset(lock_stats, 0, sizeof(lock_stats)); |
| 170 | #endif |
Willy Tarreau | 6dbd3e9 | 2017-11-05 11:50:18 +0100 | [diff] [blame] | 171 | hap_register_build_opts("Built with multi-threading support.", 0); |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 174 | #endif // USE_THREAD |
| 175 | |
| 176 | |
| 177 | /* Parse the number of threads in argument <arg>, returns it and adjusts a few |
| 178 | * internal variables accordingly, or fails and returns zero with an error |
| 179 | * reason in <errmsg>. May be called multiple times while parsing. |
| 180 | */ |
| 181 | int parse_nbthread(const char *arg, char **err) |
| 182 | { |
| 183 | long nbthread; |
| 184 | char *errptr; |
| 185 | |
| 186 | nbthread = strtol(arg, &errptr, 10); |
| 187 | if (!*arg || *errptr) { |
| 188 | memprintf(err, "passed a missing or unparsable integer value in '%s'", arg); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | #ifndef USE_THREAD |
| 193 | if (nbthread != 1) { |
| 194 | memprintf(err, "specified with a value other than 1 while HAProxy is not compiled with threads support. Please check build options for USE_THREAD"); |
| 195 | return 0; |
| 196 | } |
| 197 | #else |
| 198 | if (nbthread < 1 || nbthread > MAX_THREADS) { |
| 199 | memprintf(err, "value must be between 1 and %d (was %ld)", MAX_THREADS, nbthread); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* we proceed like this to be sure never to overflow the left shift */ |
| 204 | all_threads_mask = 1UL << (nbthread - 1); |
| 205 | all_threads_mask |= all_threads_mask - 1; |
Christopher Faulet | 1a2b56e | 2017-10-12 16:09:09 +0200 | [diff] [blame] | 206 | #endif |
Willy Tarreau | 0ccd322 | 2018-07-30 10:34:35 +0200 | [diff] [blame] | 207 | return nbthread; |
| 208 | } |