1 /* Thread pool 2 3 Copyright (C) 2019-2024 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program 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 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "gdbsupport/thread-pool.h" 21 22 #if CXX_STD_THREAD 23 24 #include "gdbsupport/alt-stack.h" 25 #include "gdbsupport/block-signals.h" 26 #include <algorithm> 27 #include <system_error> 28 29 /* On the off chance that we have the pthread library on a Windows 30 host, but std::thread is not using it, avoid calling 31 pthread_setname_np on Windows. */ 32 #ifndef _WIN32 33 #ifdef HAVE_PTHREAD_SETNAME_NP 34 #define USE_PTHREAD_SETNAME_NP 35 #endif 36 #endif 37 38 #ifdef USE_PTHREAD_SETNAME_NP 39 40 #include <pthread.h> 41 42 /* Handle platform discrepancies in pthread_setname_np: macOS uses a 43 single-argument form, while Linux uses a two-argument form. NetBSD 44 takes a printf-style format and an argument. This wrapper handles the 45 difference. */ 46 47 ATTRIBUTE_UNUSED static void 48 do_set_thread_name (int (*set_name) (pthread_t, const char *, void *), 49 const char *name) 50 { 51 set_name (pthread_self (), "%s", const_cast<char *> (name)); 52 } 53 54 ATTRIBUTE_UNUSED static void 55 do_set_thread_name (int (*set_name) (pthread_t, const char *), 56 const char *name) 57 { 58 set_name (pthread_self (), name); 59 } 60 61 /* The macOS man page says that pthread_setname_np returns "void", but 62 the headers actually declare it returning "int". */ 63 ATTRIBUTE_UNUSED static void 64 do_set_thread_name (int (*set_name) (const char *), const char *name) 65 { 66 set_name (name); 67 } 68 69 static void 70 set_thread_name (const char *name) 71 { 72 do_set_thread_name (pthread_setname_np, name); 73 } 74 75 #elif defined (USE_WIN32API) 76 77 #include <windows.h> 78 79 typedef HRESULT WINAPI (SetThreadDescription_ftype) (HANDLE, PCWSTR); 80 static SetThreadDescription_ftype *dyn_SetThreadDescription; 81 static bool initialized; 82 83 static void 84 init_windows () 85 { 86 initialized = true; 87 88 HMODULE hm = LoadLibrary (TEXT ("kernel32.dll")); 89 if (hm) 90 dyn_SetThreadDescription 91 = (SetThreadDescription_ftype *) GetProcAddress (hm, 92 "SetThreadDescription"); 93 94 /* On some versions of Windows, this function is only available in 95 KernelBase.dll, not kernel32.dll. */ 96 if (dyn_SetThreadDescription == nullptr) 97 { 98 hm = LoadLibrary (TEXT ("KernelBase.dll")); 99 if (hm) 100 dyn_SetThreadDescription 101 = (SetThreadDescription_ftype *) GetProcAddress (hm, 102 "SetThreadDescription"); 103 } 104 } 105 106 static void 107 do_set_thread_name (const wchar_t *name) 108 { 109 if (!initialized) 110 init_windows (); 111 112 if (dyn_SetThreadDescription != nullptr) 113 dyn_SetThreadDescription (GetCurrentThread (), name); 114 } 115 116 #define set_thread_name(NAME) do_set_thread_name (L ## NAME) 117 118 #else /* USE_WIN32API */ 119 120 static void 121 set_thread_name (const char *name) 122 { 123 } 124 125 #endif 126 127 #endif /* CXX_STD_THREAD */ 128 129 namespace gdb 130 { 131 132 /* The thread pool detach()s its threads, so that the threads will not 133 prevent the process from exiting. However, it was discovered that 134 if any detached threads were still waiting on a condition variable, 135 then the condition variable's destructor would wait for the threads 136 to exit -- defeating the purpose. 137 138 Allocating the thread pool on the heap and simply "leaking" it 139 avoids this problem. 140 */ 141 thread_pool *thread_pool::g_thread_pool = new thread_pool (); 142 143 thread_pool::~thread_pool () 144 { 145 /* Because this is a singleton, we don't need to clean up. The 146 threads are detached so that they won't prevent process exit. 147 And, cleaning up here would be actively harmful in at least one 148 case -- see the comment by the definition of g_thread_pool. */ 149 } 150 151 void 152 thread_pool::set_thread_count (size_t num_threads) 153 { 154 #if CXX_STD_THREAD 155 std::lock_guard<std::mutex> guard (m_tasks_mutex); 156 m_sized_at_least_once = true; 157 158 /* If the new size is larger, start some new threads. */ 159 if (m_thread_count < num_threads) 160 { 161 /* Ensure that signals used by gdb are blocked in the new 162 threads. */ 163 block_signals blocker; 164 for (size_t i = m_thread_count; i < num_threads; ++i) 165 { 166 try 167 { 168 std::thread thread (&thread_pool::thread_function, this); 169 thread.detach (); 170 } 171 catch (const std::system_error &) 172 { 173 /* libstdc++ may not implement std::thread, and will 174 throw an exception on use. It seems fine to ignore 175 this, and any other sort of startup failure here. */ 176 num_threads = i; 177 break; 178 } 179 } 180 } 181 /* If the new size is smaller, terminate some existing threads. */ 182 if (num_threads < m_thread_count) 183 { 184 for (size_t i = num_threads; i < m_thread_count; ++i) 185 m_tasks.emplace (); 186 m_tasks_cv.notify_all (); 187 } 188 189 m_thread_count = num_threads; 190 #else 191 /* No threads available, simply ignore the request. */ 192 #endif /* CXX_STD_THREAD */ 193 } 194 195 #if CXX_STD_THREAD 196 197 void 198 thread_pool::do_post_task (std::packaged_task<void ()> &&func) 199 { 200 /* This assert is here to check that no tasks are posted to the pool between 201 its initialization and sizing. */ 202 gdb_assert (m_sized_at_least_once); 203 std::packaged_task<void ()> t (std::move (func)); 204 205 if (m_thread_count != 0) 206 { 207 std::lock_guard<std::mutex> guard (m_tasks_mutex); 208 m_tasks.emplace (std::move (t)); 209 m_tasks_cv.notify_one (); 210 } 211 else 212 { 213 /* Just execute it now. */ 214 t (); 215 } 216 } 217 218 void 219 thread_pool::thread_function () 220 { 221 /* This must be done here, because on macOS one can only set the 222 name of the current thread. */ 223 set_thread_name ("gdb worker"); 224 225 /* Ensure that SIGSEGV is delivered to an alternate signal 226 stack. */ 227 gdb::alternate_signal_stack signal_stack; 228 229 while (true) 230 { 231 std::optional<task_t> t; 232 233 { 234 /* We want to hold the lock while examining the task list, but 235 not while invoking the task function. */ 236 std::unique_lock<std::mutex> guard (m_tasks_mutex); 237 while (m_tasks.empty ()) 238 m_tasks_cv.wait (guard); 239 t = std::move (m_tasks.front()); 240 m_tasks.pop (); 241 } 242 243 if (!t.has_value ()) 244 break; 245 (*t) (); 246 } 247 } 248 249 #endif /* CXX_STD_THREAD */ 250 251 } /* namespace gdb */ 252