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