]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbsupport/thread-pool.c
Fix pthread_setname_np build error
[thirdparty/binutils-gdb.git] / gdb / gdbsupport / thread-pool.c
1 /* Thread pool
2
3 Copyright (C) 2019 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
22 #if CXX_STD_THREAD
23
24 #include "gdbsupport/thread-pool.h"
25 #include "gdbsupport/alt-stack.h"
26 #include "gdbsupport/block-signals.h"
27 #include <algorithm>
28 #include "diagnostics.h"
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 DIAGNOSTIC_PUSH
44 DIAGNOSTIC_IGNORE_UNUSED_FUNCTION
45
46 /* Handle platform discrepancies in pthread_setname_np: macOS uses a
47 single-argument form, while Linux uses a two-argument form. This
48 wrapper handles the difference. */
49
50 static void
51 set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
52 {
53 set_name (pthread_self (), name);
54 }
55
56 /* The macOS man page says that pthread_setname_np returns "void", but
57 the headers actually declare it returning "int". */
58 static void
59 set_thread_name (int (*set_name) (const char *), const char *name)
60 {
61 set_name (name);
62 }
63
64 DIAGNOSTIC_POP
65
66 #endif /* USE_PTHREAD_SETNAME_NP */
67
68 namespace gdb
69 {
70
71 /* The thread pool detach()s its threads, so that the threads will not
72 prevent the process from exiting. However, it was discovered that
73 if any detached threads were still waiting on a condition variable,
74 then the condition variable's destructor would wait for the threads
75 to exit -- defeating the purpose.
76
77 Allocating the thread pool on the heap and simply "leaking" it
78 avoids this problem.
79 */
80 thread_pool *thread_pool::g_thread_pool = new thread_pool ();
81
82 thread_pool::~thread_pool ()
83 {
84 /* Because this is a singleton, we don't need to clean up. The
85 threads are detached so that they won't prevent process exit.
86 And, cleaning up here would be actively harmful in at least one
87 case -- see the comment by the definition of g_thread_pool. */
88 }
89
90 void
91 thread_pool::set_thread_count (size_t num_threads)
92 {
93 std::lock_guard<std::mutex> guard (m_tasks_mutex);
94
95 /* If the new size is larger, start some new threads. */
96 if (m_thread_count < num_threads)
97 {
98 /* Ensure that signals used by gdb are blocked in the new
99 threads. */
100 block_signals blocker;
101 for (size_t i = m_thread_count; i < num_threads; ++i)
102 {
103 std::thread thread (&thread_pool::thread_function, this);
104 thread.detach ();
105 }
106 }
107 /* If the new size is smaller, terminate some existing threads. */
108 if (num_threads < m_thread_count)
109 {
110 for (size_t i = num_threads; i < m_thread_count; ++i)
111 m_tasks.emplace ();
112 m_tasks_cv.notify_all ();
113 }
114
115 m_thread_count = num_threads;
116 }
117
118 std::future<void>
119 thread_pool::post_task (std::function<void ()> func)
120 {
121 std::packaged_task<void ()> t (func);
122 std::future<void> f = t.get_future ();
123
124 if (m_thread_count == 0)
125 {
126 /* Just execute it now. */
127 t ();
128 }
129 else
130 {
131 std::lock_guard<std::mutex> guard (m_tasks_mutex);
132 m_tasks.emplace (std::move (t));
133 m_tasks_cv.notify_one ();
134 }
135 return f;
136 }
137
138 void
139 thread_pool::thread_function ()
140 {
141 #ifdef USE_PTHREAD_SETNAME_NP
142 /* This must be done here, because on macOS one can only set the
143 name of the current thread. */
144 set_thread_name (pthread_setname_np, "gdb worker");
145 #endif
146
147 /* Ensure that SIGSEGV is delivered to an alternate signal
148 stack. */
149 gdb::alternate_signal_stack signal_stack;
150
151 while (true)
152 {
153 optional<task> t;
154
155 {
156 /* We want to hold the lock while examining the task list, but
157 not while invoking the task function. */
158 std::unique_lock<std::mutex> guard (m_tasks_mutex);
159 while (m_tasks.empty ())
160 m_tasks_cv.wait (guard);
161 t = std::move (m_tasks.front());
162 m_tasks.pop ();
163 }
164
165 if (!t.has_value ())
166 break;
167 (*t) ();
168 }
169 }
170
171 }
172
173 #endif /* CXX_STD_THREAD */