Automatic date update in version.in
[binutils-gdb.git] / gdbsupport / thread-pool.cc
blob3674e910f1db24c07dfffbd274f1caa3a7ecd2ef
1 /* Thread pool
3 Copyright (C) 2019-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "common-defs.h"
21 #include "gdbsupport/thread-pool.h"
23 #if CXX_STD_THREAD
25 #include "gdbsupport/alt-stack.h"
26 #include "gdbsupport/block-signals.h"
27 #include <algorithm>
28 #include <system_error>
30 /* On the off chance that we have the pthread library on a Windows
31 host, but std::thread is not using it, avoid calling
32 pthread_setname_np on Windows. */
33 #ifndef _WIN32
34 #ifdef HAVE_PTHREAD_SETNAME_NP
35 #define USE_PTHREAD_SETNAME_NP
36 #endif
37 #endif
39 #ifdef USE_PTHREAD_SETNAME_NP
41 #include <pthread.h>
43 /* Handle platform discrepancies in pthread_setname_np: macOS uses a
44 single-argument form, while Linux uses a two-argument form. NetBSD
45 takes a printf-style format and an argument. This wrapper handles the
46 difference. */
48 ATTRIBUTE_UNUSED static void
49 do_set_thread_name (int (*set_name) (pthread_t, const char *, void *),
50 const char *name)
52 set_name (pthread_self (), "%s", const_cast<char *> (name));
55 ATTRIBUTE_UNUSED static void
56 do_set_thread_name (int (*set_name) (pthread_t, const char *),
57 const char *name)
59 set_name (pthread_self (), name);
62 /* The macOS man page says that pthread_setname_np returns "void", but
63 the headers actually declare it returning "int". */
64 ATTRIBUTE_UNUSED static void
65 do_set_thread_name (int (*set_name) (const char *), const char *name)
67 set_name (name);
70 static void
71 set_thread_name (const char *name)
73 do_set_thread_name (pthread_setname_np, name);
76 #elif defined (USE_WIN32API)
78 #include <windows.h>
80 typedef HRESULT WINAPI (SetThreadDescription_ftype) (HANDLE, PCWSTR);
81 static SetThreadDescription_ftype *dyn_SetThreadDescription;
82 static bool initialized;
84 static void
85 init_windows ()
87 initialized = true;
89 HMODULE hm = LoadLibrary (TEXT ("kernel32.dll"));
90 if (hm)
91 dyn_SetThreadDescription
92 = (SetThreadDescription_ftype *) GetProcAddress (hm,
93 "SetThreadDescription");
95 /* On some versions of Windows, this function is only available in
96 KernelBase.dll, not kernel32.dll. */
97 if (dyn_SetThreadDescription == nullptr)
99 hm = LoadLibrary (TEXT ("KernelBase.dll"));
100 if (hm)
101 dyn_SetThreadDescription
102 = (SetThreadDescription_ftype *) GetProcAddress (hm,
103 "SetThreadDescription");
107 static void
108 do_set_thread_name (const wchar_t *name)
110 if (!initialized)
111 init_windows ();
113 if (dyn_SetThreadDescription != nullptr)
114 dyn_SetThreadDescription (GetCurrentThread (), name);
117 #define set_thread_name(NAME) do_set_thread_name (L ## NAME)
119 #else /* USE_WIN32API */
121 static void
122 set_thread_name (const char *name)
126 #endif
128 #endif /* CXX_STD_THREAD */
130 namespace gdb
133 /* The thread pool detach()s its threads, so that the threads will not
134 prevent the process from exiting. However, it was discovered that
135 if any detached threads were still waiting on a condition variable,
136 then the condition variable's destructor would wait for the threads
137 to exit -- defeating the purpose.
139 Allocating the thread pool on the heap and simply "leaking" it
140 avoids this problem.
142 thread_pool *thread_pool::g_thread_pool = new thread_pool ();
144 thread_pool::~thread_pool ()
146 /* Because this is a singleton, we don't need to clean up. The
147 threads are detached so that they won't prevent process exit.
148 And, cleaning up here would be actively harmful in at least one
149 case -- see the comment by the definition of g_thread_pool. */
152 void
153 thread_pool::set_thread_count (size_t num_threads)
155 #if CXX_STD_THREAD
156 std::lock_guard<std::mutex> guard (m_tasks_mutex);
158 /* If the new size is larger, start some new threads. */
159 if (m_thread_count < num_threads)
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)
168 std::thread thread (&thread_pool::thread_function, this);
169 thread.detach ();
171 catch (const std::system_error &)
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;
181 /* If the new size is smaller, terminate some existing threads. */
182 if (num_threads < m_thread_count)
184 for (size_t i = num_threads; i < m_thread_count; ++i)
185 m_tasks.emplace ();
186 m_tasks_cv.notify_all ();
189 m_thread_count = num_threads;
190 #else
191 /* No threads available, simply ignore the request. */
192 #endif /* CXX_STD_THREAD */
195 void
196 thread_pool::do_post_task (std::packaged_task<void ()> &&func)
198 std::packaged_task<void ()> t (std::move (func));
200 #if CXX_STD_THREAD
201 if (m_thread_count != 0)
203 std::lock_guard<std::mutex> guard (m_tasks_mutex);
204 m_tasks.emplace (std::move (t));
205 m_tasks_cv.notify_one ();
207 else
208 #endif
210 /* Just execute it now. */
211 t ();
215 #if CXX_STD_THREAD
217 void
218 thread_pool::thread_function ()
220 /* This must be done here, because on macOS one can only set the
221 name of the current thread. */
222 set_thread_name ("gdb worker");
224 /* Ensure that SIGSEGV is delivered to an alternate signal
225 stack. */
226 gdb::alternate_signal_stack signal_stack;
228 while (true)
230 optional<task_t> t;
233 /* We want to hold the lock while examining the task list, but
234 not while invoking the task function. */
235 std::unique_lock<std::mutex> guard (m_tasks_mutex);
236 while (m_tasks.empty ())
237 m_tasks_cv.wait (guard);
238 t = std::move (m_tasks.front());
239 m_tasks.pop ();
242 if (!t.has_value ())
243 break;
244 (*t) ();
248 #endif /* CXX_STD_THREAD */
250 } /* namespace gdb */