diff --git a/src/BambuStudio.cpp b/src/BambuStudio.cpp index e09c304cd5..aceb5dc583 100644 --- a/src/BambuStudio.cpp +++ b/src/BambuStudio.cpp @@ -393,6 +393,8 @@ int CLI::run(int argc, char **argv) { // Mark the main thread for the debugger and for runtime checks. set_current_thread_name("bambustu_main"); + // Save the thread ID of the main thread. + save_main_thread_id(); #ifdef __WXGTK__ // On Linux, wxGTK has no support for Wayland, and the app crashes on diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index 9a823135b3..48f1fa7b0e 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -593,14 +593,8 @@ std::string AppConfig::load() void AppConfig::save() { - { - // Returns "undefined" if the thread naming functionality is not supported by the operating system. - std::optional current_thread_name = get_current_thread_name(); - if (current_thread_name && *current_thread_name != "bambustu_main" && *current_thread_name != "main") { - BOOST_LOG_TRIVIAL(error) << __FUNCTION__<<", current_thread_name is " << *current_thread_name; - throw CriticalException("Calling AppConfig::save() from a worker thread, thread name: " + *current_thread_name); - } - } + if (! is_main_thread_active()) + throw CriticalException("Calling AppConfig::save() from a worker thread!"); // The config is first written to a file with a PID suffix and then moved // to avoid race conditions with multiple instances of Slic3r @@ -872,12 +866,8 @@ std::string AppConfig::load() void AppConfig::save() { - { - // Returns "undefined" if the thread naming functionality is not supported by the operating system. - std::optional current_thread_name = get_current_thread_name(); - if (current_thread_name && *current_thread_name != "bambustu_main") - throw CriticalException("Calling AppConfig::save() from a worker thread!"); - } + if (! is_main_thread_active()) + throw CriticalException("Calling AppConfig::save() from a worker thread!"); // The config is first written to a file with a PID suffix and then moved // to avoid race conditions with multiple instances of Slic3r diff --git a/src/libslic3r/Thread.cpp b/src/libslic3r/Thread.cpp index 4e7bd073a2..3030b6d194 100644 --- a/src/libslic3r/Thread.cpp +++ b/src/libslic3r/Thread.cpp @@ -187,6 +187,26 @@ std::optional get_current_thread_name() #endif // _WIN32 +// To be called at the start of the application to save the current thread ID as the main (UI) thread ID. +static boost::thread::id g_main_thread_id; + +void save_main_thread_id() +{ + g_main_thread_id = boost::this_thread::get_id(); +} + +// Retrieve the cached main (UI) thread ID. +boost::thread::id get_main_thread_id() +{ + return g_main_thread_id; +} + +// Checks whether the main (UI) thread is active. +bool is_main_thread_active() +{ + return get_main_thread_id() == boost::this_thread::get_id(); +} + // Spawn (n - 1) worker threads on Intel TBB thread pool and name them by an index and a system thread ID. // Also it sets locale of the worker threads to "C" for the G-code generator to produce "." as a decimal separator. void name_tbb_thread_pool_threads_set_locale() diff --git a/src/libslic3r/Thread.hpp b/src/libslic3r/Thread.hpp index 9afe13b42d..f9dc07456c 100644 --- a/src/libslic3r/Thread.hpp +++ b/src/libslic3r/Thread.hpp @@ -26,6 +26,13 @@ inline bool set_thread_name(boost::thread &thread, const std::string &thread_nam bool set_current_thread_name(const char *thread_name); inline bool set_current_thread_name(const std::string &thread_name) { return set_current_thread_name(thread_name.c_str()); } +// To be called at the start of the application to save the current thread ID as the main (UI) thread ID. +void save_main_thread_id(); +// Retrieve the cached main (UI) thread ID. +boost::thread::id get_main_thread_id(); +// Checks whether the main (UI) thread is active. +bool is_main_thread_active(); + // Returns nullopt if not supported. // Not supported by OSX. // Naming threads is only supported on newer Windows 10.