1 /* Run a function on the main thread
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "run-on-main-thread.h"
20 #include "ser-event.h"
25 #include "gdbsupport/event-loop.h"
27 /* The serial event used when posting runnables. */
29 static struct serial_event
*runnable_event
;
31 /* Runnables that have been posted. */
33 static std::vector
<std::function
<void ()>> runnables
;
37 /* Mutex to hold when handling RUNNABLE_EVENT or RUNNABLES. */
39 static std::mutex runnable_mutex
;
41 /* The main thread's thread id. */
43 static std::thread::id main_thread_id
;
47 /* Run all the queued runnables. */
50 run_events (int error
, gdb_client_data client_data
)
52 std::vector
<std::function
<void ()>> local
;
54 /* Hold the lock while changing the globals, but not while running
58 std::lock_guard
<std::mutex
> lock (runnable_mutex
);
61 /* Clear the event fd. Do this before flushing the events list,
62 so that any new event post afterwards is sure to re-awaken the
64 serial_event_clear (runnable_event
);
66 /* Move the vector in case running a runnable pushes a new
68 local
= std::move (runnables
);
71 for (auto &item
: local
)
79 /* Ignore exceptions in the callback. */
84 /* See run-on-main-thread.h. */
87 run_on_main_thread (std::function
<void ()> &&func
)
90 std::lock_guard
<std::mutex
> lock (runnable_mutex
);
92 runnables
.emplace_back (std::move (func
));
93 serial_event_set (runnable_event
);
97 static bool main_thread_id_initialized
= false;
100 /* See run-on-main-thread.h. */
106 /* Initialize main_thread_id on first use of is_main_thread. */
107 if (!main_thread_id_initialized
)
109 main_thread_id_initialized
= true;
111 main_thread_id
= std::this_thread::get_id ();
114 return std::this_thread::get_id () == main_thread_id
;
120 void _initialize_run_on_main_thread ();
122 _initialize_run_on_main_thread ()
125 /* The variable main_thread_id should be initialized when entering main, or
126 at an earlier use, so it should already be initialized here. */
127 gdb_assert (main_thread_id_initialized
);
129 /* Assume that we execute this in the main thread. */
130 gdb_assert (is_main_thread ());
132 runnable_event
= make_serial_event ();
133 add_file_handler (serial_event_fd (runnable_event
), run_events
, nullptr,
134 "run-on-main-thread");
136 /* A runnable may refer to an extension language. So, we want to
137 make sure any pending ones have been deleted before the extension
138 languages are shut down. */
139 add_final_cleanup ([] ()
142 std::lock_guard
lock (runnable_mutex
);