diff --git a/lib/libimhex/include/hex/api/task_manager.hpp b/lib/libimhex/include/hex/api/task_manager.hpp index 34663621b..d6dfa9b57 100644 --- a/lib/libimhex/include/hex/api/task_manager.hpp +++ b/lib/libimhex/include/hex/api/task_manager.hpp @@ -211,7 +211,19 @@ EXPORT_MODULE namespace hex { * @brief Gets the name of the current thread * @return Name of the thread */ - static std::string getCurrentThreadName(); + static std::string_view getCurrentThreadName(); + + /** + * @brief Sets the ID of the main thread + * @param threadId ID of the main thread + */ + static void setMainThreadId(std::thread::id threadId); + + /** + * @brief Checks if the current thread is the main thread + * @return True if the current thread is the main thread, false otherwise + */ + static bool isMainThread(); /** * @brief Cleans up finished tasks diff --git a/lib/libimhex/source/api/task_manager.cpp b/lib/libimhex/source/api/task_manager.cpp index 15184a096..8e4599279 100644 --- a/lib/libimhex/source/api/task_manager.cpp +++ b/lib/libimhex/source/api/task_manager.cpp @@ -59,6 +59,7 @@ namespace hex { thread_local std::array s_currentThreadName; thread_local Task* s_currentTask = nullptr; + std::thread::id s_mainThreadId; } @@ -526,8 +527,19 @@ namespace hex { #endif } - std::string TaskManager::getCurrentThreadName() { - return s_currentThreadName.data(); + std::string_view TaskManager::getCurrentThreadName() { + if (TaskManager::isMainThread()) + return "Main"; + else + return s_currentThreadName.data(); + } + + void TaskManager::setMainThreadId(std::thread::id threadId) { + s_mainThreadId = threadId; + } + + bool TaskManager::isMainThread() { + return s_mainThreadId == std::this_thread::get_id(); } diff --git a/main/gui/source/main.cpp b/main/gui/source/main.cpp index a67190765..9f819da6f 100644 --- a/main/gui/source/main.cpp +++ b/main/gui/source/main.cpp @@ -29,8 +29,12 @@ int main(int argc, char **argv) { std::setlocale(LC_ALL, "en_US.utf8"); - // Set the main thread's name to "Main" - TaskManager::setCurrentThreadName("Main"); + // Tell the Task Manager that we are the main thread + TaskManager::setMainThreadId(std::this_thread::get_id()); + + // Set the main thread's name. This is the name that will be displayed + // in tools like btop + TaskManager::setCurrentThreadName("ImHex 🔍"); // Setup crash handlers right away to catch crashes as early as possible crash::setupCrashHandlers();