3 Copyright (C) 2019-2020 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"
24 #include "gdbsupport/thread-pool.h"
25 #include "gdbsupport/alt-stack.h"
26 #include "gdbsupport/block-signals.h"
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. */
33 #ifdef HAVE_PTHREAD_SETNAME_NP
34 #define USE_PTHREAD_SETNAME_NP
38 #ifdef USE_PTHREAD_SETNAME_NP
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
47 ATTRIBUTE_UNUSED
static void
48 set_thread_name (int (*set_name
) (pthread_t
, const char *, void *),
51 set_name (pthread_self (), "%s", const_cast<char *> (name
));
54 ATTRIBUTE_UNUSED
static void
55 set_thread_name (int (*set_name
) (pthread_t
, const char *), const char *name
)
57 set_name (pthread_self (), name
);
60 /* The macOS man page says that pthread_setname_np returns "void", but
61 the headers actually declare it returning "int". */
62 ATTRIBUTE_UNUSED
static void
63 set_thread_name (int (*set_name
) (const char *), const char *name
)
68 #endif /* USE_PTHREAD_SETNAME_NP */
73 /* The thread pool detach()s its threads, so that the threads will not
74 prevent the process from exiting. However, it was discovered that
75 if any detached threads were still waiting on a condition variable,
76 then the condition variable's destructor would wait for the threads
77 to exit -- defeating the purpose.
79 Allocating the thread pool on the heap and simply "leaking" it
82 thread_pool
*thread_pool::g_thread_pool
= new thread_pool ();
84 thread_pool::~thread_pool ()
86 /* Because this is a singleton, we don't need to clean up. The
87 threads are detached so that they won't prevent process exit.
88 And, cleaning up here would be actively harmful in at least one
89 case -- see the comment by the definition of g_thread_pool. */
93 thread_pool::set_thread_count (size_t num_threads
)
95 std::lock_guard
<std::mutex
> guard (m_tasks_mutex
);
97 /* If the new size is larger, start some new threads. */
98 if (m_thread_count
< num_threads
)
100 /* Ensure that signals used by gdb are blocked in the new
102 block_signals blocker
;
103 for (size_t i
= m_thread_count
; i
< num_threads
; ++i
)
105 std::thread
thread (&thread_pool::thread_function
, this);
109 /* If the new size is smaller, terminate some existing threads. */
110 if (num_threads
< m_thread_count
)
112 for (size_t i
= num_threads
; i
< m_thread_count
; ++i
)
114 m_tasks_cv
.notify_all ();
117 m_thread_count
= num_threads
;
121 thread_pool::post_task (std::function
<void ()> func
)
123 std::packaged_task
<void ()> t (func
);
124 std::future
<void> f
= t
.get_future ();
126 if (m_thread_count
== 0)
128 /* Just execute it now. */
133 std::lock_guard
<std::mutex
> guard (m_tasks_mutex
);
134 m_tasks
.emplace (std::move (t
));
135 m_tasks_cv
.notify_one ();
141 thread_pool::thread_function ()
143 #ifdef USE_PTHREAD_SETNAME_NP
144 /* This must be done here, because on macOS one can only set the
145 name of the current thread. */
146 set_thread_name (pthread_setname_np
, "gdb worker");
149 /* Ensure that SIGSEGV is delivered to an alternate signal
151 gdb::alternate_signal_stack signal_stack
;
158 /* We want to hold the lock while examining the task list, but
159 not while invoking the task function. */
160 std::unique_lock
<std::mutex
> guard (m_tasks_mutex
);
161 while (m_tasks
.empty ())
162 m_tasks_cv
.wait (guard
);
163 t
= std::move (m_tasks
.front());
175 #endif /* CXX_STD_THREAD */