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