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
)
77 catch (const gdb_exception_forced_quit
&e
)
79 /* GDB is terminating, so:
80 - make sure this is propagated, and
81 - no need to keep running things, so propagate immediately. */
84 catch (const gdb_exception_quit
&e
)
86 /* Should cancelation of a runnable event cancel the execution of
87 the following one? The answer is not clear, so keep doing what
88 we've done so far: ignore this exception. */
90 catch (const gdb_exception
&)
92 /* Ignore exceptions in the callback. */
97 /* See run-on-main-thread.h. */
100 run_on_main_thread (std::function
<void ()> &&func
)
103 std::lock_guard
<std::mutex
> lock (runnable_mutex
);
105 runnables
.emplace_back (std::move (func
));
106 serial_event_set (runnable_event
);
110 static bool main_thread_id_initialized
= false;
113 /* See run-on-main-thread.h. */
119 /* Initialize main_thread_id on first use of is_main_thread. */
120 if (!main_thread_id_initialized
)
122 main_thread_id_initialized
= true;
124 main_thread_id
= std::this_thread::get_id ();
127 return std::this_thread::get_id () == main_thread_id
;
133 void _initialize_run_on_main_thread ();
135 _initialize_run_on_main_thread ()
138 /* The variable main_thread_id should be initialized when entering main, or
139 at an earlier use, so it should already be initialized here. */
140 gdb_assert (main_thread_id_initialized
);
142 /* Assume that we execute this in the main thread. */
143 gdb_assert (is_main_thread ());
145 runnable_event
= make_serial_event ();
146 add_file_handler (serial_event_fd (runnable_event
), run_events
, nullptr,
147 "run-on-main-thread");
149 /* A runnable may refer to an extension language. So, we want to
150 make sure any pending ones have been deleted before the extension
151 languages are shut down. */
152 add_final_cleanup ([] ()
155 std::lock_guard
lock (runnable_mutex
);