Implement a thread pool
[deliverable/binutils-gdb.git] / gdb / gdbsupport / thread-pool.c
CommitLineData
a0b57563
CB
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
29namespace gdb
30{
31
32/* The thread pool detach()s its threads, so that the threads will not
33 prevent the process from exiting. However, it was discovered that
34 if any detached threads were still waiting on a condition variable,
35 then the condition variable's destructor would wait for the threads
36 to exit -- defeating the purpose.
37
38 Allocating the thread pool on the heap and simply "leaking" it
39 avoids this problem.
40*/
41thread_pool *thread_pool::g_thread_pool = new thread_pool ();
42
43thread_pool::~thread_pool ()
44{
45 /* Because this is a singleton, we don't need to clean up. The
46 threads are detached so that they won't prevent process exit.
47 And, cleaning up here would be actively harmful in at least one
48 case -- see the comment by the definition of g_thread_pool. */
49}
50
51void
52thread_pool::set_thread_count (size_t num_threads)
53{
54 std::lock_guard<std::mutex> guard (m_tasks_mutex);
55
56 /* If the new size is larger, start some new threads. */
57 if (m_thread_count < num_threads)
58 {
59 /* Ensure that signals used by gdb are blocked in the new
60 threads. */
61 block_signals blocker;
62 for (size_t i = m_thread_count; i < num_threads; ++i)
63 {
64 std::thread thread (&thread_pool::thread_function, this);
65 thread.detach ();
66 }
67 }
68 /* If the new size is smaller, terminate some existing threads. */
69 if (num_threads < m_thread_count)
70 {
71 for (size_t i = num_threads; i < m_thread_count; ++i)
72 m_tasks.emplace ();
73 m_tasks_cv.notify_all ();
74 }
75
76 m_thread_count = num_threads;
77}
78
79std::future<void>
80thread_pool::post_task (std::function<void ()> func)
81{
82 std::packaged_task<void ()> t (func);
83 std::future<void> f = t.get_future ();
84
85 if (m_thread_count == 0)
86 {
87 /* Just execute it now. */
88 t ();
89 }
90 else
91 {
92 std::lock_guard<std::mutex> guard (m_tasks_mutex);
93 m_tasks.emplace (std::move (t));
94 m_tasks_cv.notify_one ();
95 }
96 return f;
97}
98
99void
100thread_pool::thread_function ()
101{
102 /* Ensure that SIGSEGV is delivered to an alternate signal
103 stack. */
104 gdb::alternate_signal_stack signal_stack;
105
106 while (true)
107 {
108 optional<task> t;
109
110 {
111 /* We want to hold the lock while examining the task list, but
112 not while invoking the task function. */
113 std::unique_lock<std::mutex> guard (m_tasks_mutex);
114 while (m_tasks.empty ())
115 m_tasks_cv.wait (guard);
116 t = std::move (m_tasks.front());
117 m_tasks.pop ();
118 }
119
120 if (!t.has_value ())
121 break;
122 (*t) ();
123 }
124}
125
126}
127
128#endif /* CXX_STD_THREAD */
This page took 0.026695 seconds and 4 git commands to generate.