run-on-main-thread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Run a function on the main thread
  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 "defs.h"
  15. #include "run-on-main-thread.h"
  16. #include "ser-event.h"
  17. #if CXX_STD_THREAD
  18. #include <thread>
  19. #include <mutex>
  20. #endif
  21. #include "gdbsupport/event-loop.h"
  22. /* The serial event used when posting runnables. */
  23. static struct serial_event *runnable_event;
  24. /* Runnables that have been posted. */
  25. static std::vector<std::function<void ()>> runnables;
  26. #if CXX_STD_THREAD
  27. /* Mutex to hold when handling RUNNABLE_EVENT or RUNNABLES. */
  28. static std::mutex runnable_mutex;
  29. /* The main thread. */
  30. static std::thread::id main_thread;
  31. #endif
  32. /* Run all the queued runnables. */
  33. static void
  34. run_events (int error, gdb_client_data client_data)
  35. {
  36. std::vector<std::function<void ()>> local;
  37. /* Hold the lock while changing the globals, but not while running
  38. the runnables. */
  39. {
  40. #if CXX_STD_THREAD
  41. std::lock_guard<std::mutex> lock (runnable_mutex);
  42. #endif
  43. /* Clear the event fd. Do this before flushing the events list,
  44. so that any new event post afterwards is sure to re-awaken the
  45. event loop. */
  46. serial_event_clear (runnable_event);
  47. /* Move the vector in case running a runnable pushes a new
  48. runnable. */
  49. local = std::move (runnables);
  50. }
  51. for (auto &item : local)
  52. {
  53. try
  54. {
  55. item ();
  56. }
  57. catch (...)
  58. {
  59. /* Ignore exceptions in the callback. */
  60. }
  61. }
  62. }
  63. /* See run-on-main-thread.h. */
  64. void
  65. run_on_main_thread (std::function<void ()> &&func)
  66. {
  67. #if CXX_STD_THREAD
  68. std::lock_guard<std::mutex> lock (runnable_mutex);
  69. #endif
  70. runnables.emplace_back (std::move (func));
  71. serial_event_set (runnable_event);
  72. }
  73. /* See run-on-main-thread.h. */
  74. bool
  75. is_main_thread ()
  76. {
  77. #if CXX_STD_THREAD
  78. return std::this_thread::get_id () == main_thread;
  79. #else
  80. return true;
  81. #endif
  82. }
  83. void _initialize_run_on_main_thread ();
  84. void
  85. _initialize_run_on_main_thread ()
  86. {
  87. #if CXX_STD_THREAD
  88. main_thread = std::this_thread::get_id ();
  89. #endif
  90. runnable_event = make_serial_event ();
  91. add_file_handler (serial_event_fd (runnable_event), run_events, nullptr,
  92. "run-on-main-thread");
  93. }