thread-pool.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* Thread pool
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #if CXX_STD_THREAD
  16. #include "gdbsupport/thread-pool.h"
  17. #include "gdbsupport/alt-stack.h"
  18. #include "gdbsupport/block-signals.h"
  19. #include <algorithm>
  20. #include <system_error>
  21. /* On the off chance that we have the pthread library on a Windows
  22. host, but std::thread is not using it, avoid calling
  23. pthread_setname_np on Windows. */
  24. #ifndef _WIN32
  25. #ifdef HAVE_PTHREAD_SETNAME_NP
  26. #define USE_PTHREAD_SETNAME_NP
  27. #endif
  28. #endif
  29. #ifdef USE_PTHREAD_SETNAME_NP
  30. #include <pthread.h>
  31. /* Handle platform discrepancies in pthread_setname_np: macOS uses a
  32. single-argument form, while Linux uses a two-argument form. NetBSD
  33. takes a printf-style format and an argument. This wrapper handles the
  34. difference. */
  35. ATTRIBUTE_UNUSED static void
  36. set_thread_name (int (*set_name) (pthread_t, const char *, void *),
  37. const char *name)
  38. {
  39. set_name (pthread_self (), "%s", const_cast<char *> (name));
  40. }
  41. ATTRIBUTE_UNUSED static void
  42. set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
  43. {
  44. set_name (pthread_self (), name);
  45. }
  46. /* The macOS man page says that pthread_setname_np returns "void", but
  47. the headers actually declare it returning "int". */
  48. ATTRIBUTE_UNUSED static void
  49. set_thread_name (int (*set_name) (const char *), const char *name)
  50. {
  51. set_name (name);
  52. }
  53. #endif /* USE_PTHREAD_SETNAME_NP */
  54. namespace gdb
  55. {
  56. /* The thread pool detach()s its threads, so that the threads will not
  57. prevent the process from exiting. However, it was discovered that
  58. if any detached threads were still waiting on a condition variable,
  59. then the condition variable's destructor would wait for the threads
  60. to exit -- defeating the purpose.
  61. Allocating the thread pool on the heap and simply "leaking" it
  62. avoids this problem.
  63. */
  64. thread_pool *thread_pool::g_thread_pool = new thread_pool ();
  65. thread_pool::~thread_pool ()
  66. {
  67. /* Because this is a singleton, we don't need to clean up. The
  68. threads are detached so that they won't prevent process exit.
  69. And, cleaning up here would be actively harmful in at least one
  70. case -- see the comment by the definition of g_thread_pool. */
  71. }
  72. void
  73. thread_pool::set_thread_count (size_t num_threads)
  74. {
  75. std::lock_guard<std::mutex> guard (m_tasks_mutex);
  76. /* If the new size is larger, start some new threads. */
  77. if (m_thread_count < num_threads)
  78. {
  79. /* Ensure that signals used by gdb are blocked in the new
  80. threads. */
  81. block_signals blocker;
  82. for (size_t i = m_thread_count; i < num_threads; ++i)
  83. {
  84. try
  85. {
  86. std::thread thread (&thread_pool::thread_function, this);
  87. thread.detach ();
  88. }
  89. catch (const std::system_error &)
  90. {
  91. /* libstdc++ may not implement std::thread, and will
  92. throw an exception on use. It seems fine to ignore
  93. this, and any other sort of startup failure here. */
  94. num_threads = i;
  95. break;
  96. }
  97. }
  98. }
  99. /* If the new size is smaller, terminate some existing threads. */
  100. if (num_threads < m_thread_count)
  101. {
  102. for (size_t i = num_threads; i < m_thread_count; ++i)
  103. m_tasks.emplace ();
  104. m_tasks_cv.notify_all ();
  105. }
  106. m_thread_count = num_threads;
  107. }
  108. std::future<void>
  109. thread_pool::post_task (std::function<void ()> &&func)
  110. {
  111. std::packaged_task<void ()> t (std::move (func));
  112. std::future<void> f = t.get_future ();
  113. if (m_thread_count == 0)
  114. {
  115. /* Just execute it now. */
  116. t ();
  117. }
  118. else
  119. {
  120. std::lock_guard<std::mutex> guard (m_tasks_mutex);
  121. m_tasks.emplace (std::move (t));
  122. m_tasks_cv.notify_one ();
  123. }
  124. return f;
  125. }
  126. void
  127. thread_pool::thread_function ()
  128. {
  129. #ifdef USE_PTHREAD_SETNAME_NP
  130. /* This must be done here, because on macOS one can only set the
  131. name of the current thread. */
  132. set_thread_name (pthread_setname_np, "gdb worker");
  133. #endif
  134. /* Ensure that SIGSEGV is delivered to an alternate signal
  135. stack. */
  136. gdb::alternate_signal_stack signal_stack;
  137. while (true)
  138. {
  139. optional<task> t;
  140. {
  141. /* We want to hold the lock while examining the task list, but
  142. not while invoking the task function. */
  143. std::unique_lock<std::mutex> guard (m_tasks_mutex);
  144. while (m_tasks.empty ())
  145. m_tasks_cv.wait (guard);
  146. t = std::move (m_tasks.front());
  147. m_tasks.pop ();
  148. }
  149. if (!t.has_value ())
  150. break;
  151. (*t) ();
  152. }
  153. }
  154. }
  155. #endif /* CXX_STD_THREAD */