From 04a8be6cc60721b816be6bdb5705ebb24bee2e58 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 10 Jul 2021 14:21:50 +0200 Subject: [PATCH] Keep track of which thread the GUI thread is --- src/wine-host/utils.cpp | 2 ++ src/wine-host/utils.h | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/wine-host/utils.cpp b/src/wine-host/utils.cpp index c6889da8..5967b2c3 100644 --- a/src/wine-host/utils.cpp +++ b/src/wine-host/utils.cpp @@ -106,6 +106,8 @@ MainContext::MainContext() } void MainContext::run() { + gui_thread_id = GetCurrentThreadId(); + context.run(); } diff --git a/src/wine-host/utils.h b/src/wine-host/utils.h index 89da753f..6fc2a8fd 100644 --- a/src/wine-host/utils.h +++ b/src/wine-host/utils.h @@ -234,6 +234,14 @@ class MainContext { */ WatchdogGuard register_watchdog(HostBridge& bridge); + /** + * Returns `true` if the calling thread is the GUI thread, aka the thread + * that called `MainContext::run()`. + */ + inline bool is_gui_thread() const noexcept { + return GetCurrentThreadId() == gui_thread_id.value_or(0); + } + /** * Asynchronously execute a function inside of this main IO context and * return the results as a future. This is used to make sure that operations @@ -316,6 +324,12 @@ class MainContext { void async_handle_watchdog_timer( std::chrono::steady_clock::duration interval); + /** + * The **Windows** thread ID the context is running on, which will be our + * GUI thread. Will be a nullopt until `MainContext::run()` has been called. + */ + std::optional gui_thread_id; + /** * The timer used to periodically handle X11 events and Win32 messages. */