diff options
Diffstat (limited to 'gdb/run-on-main-thread.c')
-rw-r--r-- | gdb/run-on-main-thread.c | 43 |
1 files changed, 11 insertions, 32 deletions
diff --git a/gdb/run-on-main-thread.c b/gdb/run-on-main-thread.c index edaab08..d68811b 100644 --- a/gdb/run-on-main-thread.c +++ b/gdb/run-on-main-thread.c @@ -18,10 +18,8 @@ #include "run-on-main-thread.h" #include "ser-event.h" -#if CXX_STD_THREAD -#include <thread> -#include <mutex> -#endif +#include "gdbsupport/cleanups.h" +#include "gdbsupport/cxx-thread.h" #include "gdbsupport/event-loop.h" /* The serial event used when posting runnables. */ @@ -32,17 +30,13 @@ static struct serial_event *runnable_event; static std::vector<std::function<void ()>> runnables; -#if CXX_STD_THREAD - /* Mutex to hold when handling RUNNABLE_EVENT or RUNNABLES. */ -static std::mutex runnable_mutex; +static gdb::mutex runnable_mutex; /* The main thread's thread id. */ -static std::thread::id main_thread_id; - -#endif +static gdb::thread::id main_thread_id; /* Run all the queued runnables. */ @@ -54,9 +48,7 @@ run_events (int error, gdb_client_data client_data) /* Hold the lock while changing the globals, but not while running the runnables. */ { -#if CXX_STD_THREAD - std::lock_guard<std::mutex> lock (runnable_mutex); -#endif + gdb::lock_guard<gdb::mutex> lock (runnable_mutex); /* Clear the event fd. Do this before flushing the events list, so that any new event post afterwards is sure to re-awaken the @@ -99,49 +91,38 @@ run_events (int error, gdb_client_data client_data) void run_on_main_thread (std::function<void ()> &&func) { -#if CXX_STD_THREAD - std::lock_guard<std::mutex> lock (runnable_mutex); -#endif + gdb::lock_guard<gdb::mutex> lock (runnable_mutex); runnables.emplace_back (std::move (func)); serial_event_set (runnable_event); } -#if CXX_STD_THREAD static bool main_thread_id_initialized = false; -#endif /* See run-on-main-thread.h. */ bool is_main_thread () { -#if CXX_STD_THREAD /* Initialize main_thread_id on first use of is_main_thread. */ if (!main_thread_id_initialized) { main_thread_id_initialized = true; - main_thread_id = std::this_thread::get_id (); + main_thread_id = gdb::this_thread::get_id (); } - return std::this_thread::get_id () == main_thread_id; -#else - return true; -#endif + return gdb::this_thread::get_id () == main_thread_id; } -void _initialize_run_on_main_thread (); -void -_initialize_run_on_main_thread () +INIT_GDB_FILE (run_on_main_thread) { -#if CXX_STD_THREAD /* The variable main_thread_id should be initialized when entering main, or at an earlier use, so it should already be initialized here. */ gdb_assert (main_thread_id_initialized); /* Assume that we execute this in the main thread. */ gdb_assert (is_main_thread ()); -#endif + runnable_event = make_serial_event (); add_file_handler (serial_event_fd (runnable_event), run_events, nullptr, "run-on-main-thread"); @@ -151,9 +132,7 @@ _initialize_run_on_main_thread () languages are shut down. */ add_final_cleanup ([] () { -#if CXX_STD_THREAD - std::lock_guard lock (runnable_mutex); -#endif + gdb::lock_guard<gdb::mutex> lock (runnable_mutex); runnables.clear (); }); } |