Fix regression on Windows with WOW64
[binutils-gdb.git] / gdbsupport / thread-pool.cc
1 /* Thread pool
2
3 Copyright (C) 2019-2022 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 "common-defs.h"
21 #include "gdbsupport/thread-pool.h"
22
23 #if CXX_STD_THREAD
24
25 #include "gdbsupport/alt-stack.h"
26 #include "gdbsupport/block-signals.h"
27 #include <algorithm>
28 #include <system_error>
29
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
38
39 #ifdef USE_PTHREAD_SETNAME_NP
40
41 #include <pthread.h>
42
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. */
47
48 ATTRIBUTE_UNUSED static void
49 set_thread_name (int (*set_name) (pthread_t, const char *, void *),
50 const char *name)
51 {
52 set_name (pthread_self (), "%s", const_cast<char *> (name));
53 }
54
55 ATTRIBUTE_UNUSED static void
56 set_thread_name (int (*set_name) (pthread_t, const char *), 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 set_thread_name (int (*set_name) (const char *), const char *name)
65 {
66 set_name (name);
67 }
68
69 #endif /* USE_PTHREAD_SETNAME_NP */
70 #endif /* CXX_STD_THREAD */
71
72 namespace gdb
73 {
74
75 /* The thread pool detach()s its threads, so that the threads will not
76 prevent the process from exiting. However, it was discovered that
77 if any detached threads were still waiting on a condition variable,
78 then the condition variable's destructor would wait for the threads
79 to exit -- defeating the purpose.
80
81 Allocating the thread pool on the heap and simply "leaking" it
82 avoids this problem.
83 */
84 thread_pool *thread_pool::g_thread_pool = new thread_pool ();
85
86 thread_pool::~thread_pool ()
87 {
88 /* Because this is a singleton, we don't need to clean up. The
89 threads are detached so that they won't prevent process exit.
90 And, cleaning up here would be actively harmful in at least one
91 case -- see the comment by the definition of g_thread_pool. */
92 }
93
94 void
95 thread_pool::set_thread_count (size_t num_threads)
96 {
97 #if CXX_STD_THREAD
98 std::lock_guard<std::mutex> guard (m_tasks_mutex);
99
100 /* If the new size is larger, start some new threads. */
101 if (m_thread_count < num_threads)
102 {
103 /* Ensure that signals used by gdb are blocked in the new
104 threads. */
105 block_signals blocker;
106 for (size_t i = m_thread_count; i < num_threads; ++i)
107 {
108 try
109 {
110 std::thread thread (&thread_pool::thread_function, this);
111 thread.detach ();
112 }
113 catch (const std::system_error &)
114 {
115 /* libstdc++ may not implement std::thread, and will
116 throw an exception on use. It seems fine to ignore
117 this, and any other sort of startup failure here. */
118 num_threads = i;
119 break;
120 }
121 }
122 }
123 /* If the new size is smaller, terminate some existing threads. */
124 if (num_threads < m_thread_count)
125 {
126 for (size_t i = num_threads; i < m_thread_count; ++i)
127 m_tasks.emplace ();
128 m_tasks_cv.notify_all ();
129 }
130
131 m_thread_count = num_threads;
132 #else
133 /* No threads available, simply ignore the request. */
134 #endif /* CXX_STD_THREAD */
135 }
136
137 void
138 thread_pool::do_post_task (std::packaged_task<void ()> &&func)
139 {
140 std::packaged_task<void ()> t (std::move (func));
141
142 #if CXX_STD_THREAD
143 if (m_thread_count != 0)
144 {
145 std::lock_guard<std::mutex> guard (m_tasks_mutex);
146 m_tasks.emplace (std::move (t));
147 m_tasks_cv.notify_one ();
148 }
149 else
150 #endif
151 {
152 /* Just execute it now. */
153 t ();
154 }
155 }
156
157 #if CXX_STD_THREAD
158
159 void
160 thread_pool::thread_function ()
161 {
162 #ifdef USE_PTHREAD_SETNAME_NP
163 /* This must be done here, because on macOS one can only set the
164 name of the current thread. */
165 set_thread_name (pthread_setname_np, "gdb worker");
166 #endif
167
168 /* Ensure that SIGSEGV is delivered to an alternate signal
169 stack. */
170 gdb::alternate_signal_stack signal_stack;
171
172 while (true)
173 {
174 optional<task_t> t;
175
176 {
177 /* We want to hold the lock while examining the task list, but
178 not while invoking the task function. */
179 std::unique_lock<std::mutex> guard (m_tasks_mutex);
180 while (m_tasks.empty ())
181 m_tasks_cv.wait (guard);
182 t = std::move (m_tasks.front());
183 m_tasks.pop ();
184 }
185
186 if (!t.has_value ())
187 break;
188 (*t) ();
189 }
190 }
191
192 #endif /* CXX_STD_THREAD */
193
194 } /* namespace gdb */