gdb/tui: fairer distribution of excess space during apply
[binutils-gdb.git] / gdb / run-on-main-thread.c
index 74ab1e19a57a45f795bbb661784680e65e1898d6..eb22310f5c0fce241c8c2f36a4f8f5179249e344 100644 (file)
@@ -1,5 +1,5 @@
 /* Run a function on the main thread
-   Copyright (C) 2019-2020 Free Software Foundation, Inc.
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "run-on-main-thread.h"
 #include "ser-event.h"
 #if CXX_STD_THREAD
+#include <thread>
 #include <mutex>
 #endif
-#include "event-loop.h"
+#include "gdbsupport/event-loop.h"
 
 /* The serial event used when posting runnables.  */
 
@@ -38,6 +39,10 @@ static std::vector<std::function<void ()>> runnables;
 
 static std::mutex runnable_mutex;
 
+/* The main thread.  */
+
+static std::thread::id main_thread;
+
 #endif
 
 /* Run all the queued runnables.  */
@@ -89,10 +94,26 @@ run_on_main_thread (std::function<void ()> &&func)
   serial_event_set (runnable_event);
 }
 
+/* See run-on-main-thread.h.  */
+
+bool
+is_main_thread ()
+{
+#if CXX_STD_THREAD
+  return std::this_thread::get_id () == main_thread;
+#else
+  return true;
+#endif
+}
+
 void _initialize_run_on_main_thread ();
 void
 _initialize_run_on_main_thread ()
 {
+#if CXX_STD_THREAD
+  main_thread = std::this_thread::get_id ();
+#endif
   runnable_event = make_serial_event ();
-  add_file_handler (serial_event_fd (runnable_event), run_events, nullptr);
+  add_file_handler (serial_event_fd (runnable_event), run_events, nullptr,
+                   "run-on-main-thread");
 }