More updated translations
[binutils-gdb.git] / gdb / run-on-main-thread.c
blob746ea35ee9e6c4f87fa5c0b58f1a416e9a075842
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"
21 #if CXX_STD_THREAD
22 #include <thread>
23 #include <mutex>
24 #endif
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;
35 #if CXX_STD_THREAD
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;
45 #endif
47 /* Run all the queued runnables. */
49 static void
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
55 the runnables. */
57 #if CXX_STD_THREAD
58 std::lock_guard<std::mutex> lock (runnable_mutex);
59 #endif
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
63 event loop. */
64 serial_event_clear (runnable_event);
66 /* Move the vector in case running a runnable pushes a new
67 runnable. */
68 local = std::move (runnables);
71 for (auto &item : local)
73 try
75 item ();
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. */
82 throw;
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. */
99 void
100 run_on_main_thread (std::function<void ()> &&func)
102 #if CXX_STD_THREAD
103 std::lock_guard<std::mutex> lock (runnable_mutex);
104 #endif
105 runnables.emplace_back (std::move (func));
106 serial_event_set (runnable_event);
109 #if CXX_STD_THREAD
110 static bool main_thread_id_initialized = false;
111 #endif
113 /* See run-on-main-thread.h. */
115 bool
116 is_main_thread ()
118 #if CXX_STD_THREAD
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;
128 #else
129 return true;
130 #endif
133 void _initialize_run_on_main_thread ();
134 void
135 _initialize_run_on_main_thread ()
137 #if CXX_STD_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 ());
144 #endif
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 ([] ()
154 #if CXX_STD_THREAD
155 std::lock_guard lock (runnable_mutex);
156 #endif
157 runnables.clear ();