gdb, testsuite: Fix return value in gdb.base/foll-fork.exp
[binutils-gdb.git] / gdb / run-on-main-thread.c
blobe30dabaff03267b6d10c2f64d2ac16bc921adf17
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 (...)
79 /* Ignore exceptions in the callback. */
84 /* See run-on-main-thread.h. */
86 void
87 run_on_main_thread (std::function<void ()> &&func)
89 #if CXX_STD_THREAD
90 std::lock_guard<std::mutex> lock (runnable_mutex);
91 #endif
92 runnables.emplace_back (std::move (func));
93 serial_event_set (runnable_event);
96 #if CXX_STD_THREAD
97 static bool main_thread_id_initialized = false;
98 #endif
100 /* See run-on-main-thread.h. */
102 bool
103 is_main_thread ()
105 #if CXX_STD_THREAD
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;
115 #else
116 return true;
117 #endif
120 void _initialize_run_on_main_thread ();
121 void
122 _initialize_run_on_main_thread ()
124 #if CXX_STD_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 ());
131 #endif
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 ([] ()
141 #if CXX_STD_THREAD
142 std::lock_guard lock (runnable_mutex);
143 #endif
144 runnables.clear ();