From 723406dfeadfb21a5b5a6f1bc62c4d9aed137505 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 22 Oct 2020 14:45:15 +0200 Subject: [PATCH] Workaround for OSX non-compliant implementation of pthread_getname_np / pthread_setname_np --- src/libslic3r/AppConfig.cpp | 4 ++++ src/libslic3r/Thread.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/libslic3r/AppConfig.cpp b/src/libslic3r/AppConfig.cpp index acc8c27c7..609fac75d 100644 --- a/src/libslic3r/AppConfig.cpp +++ b/src/libslic3r/AppConfig.cpp @@ -2,6 +2,7 @@ #include "libslic3r/Utils.hpp" #include "AppConfig.hpp" #include "Exception.hpp" +#include "Thread.hpp" #include #include @@ -212,6 +213,9 @@ std::string AppConfig::load() void AppConfig::save() { + if (get_current_thread_name() != "slic3r_main") + 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 const auto path = config_path(); diff --git a/src/libslic3r/Thread.cpp b/src/libslic3r/Thread.cpp index fa7f2e88d..e72545994 100644 --- a/src/libslic3r/Thread.cpp +++ b/src/libslic3r/Thread.cpp @@ -92,6 +92,36 @@ std::string get_current_thread_name() #else // _WIN32 +#ifdef __APPLE__ + +// Appe screwed the Posix norm. +void set_thread_name(std::thread &thread, const char *thread_name) +{ +// not supported +// pthread_setname_np(thread.native_handle(), thread_name); + throw CriticalException("Not supported"); +} + +void set_thread_name(boost::thread &thread, const char *thread_name) +{ +// not supported +// pthread_setname_np(thread.native_handle(), thread_name); + throw CriticalException("Not supported"); +} + +void set_current_thread_name(const char *thread_name) +{ + pthread_setname_np(thread_name); +} + +std::string get_current_thread_name() +{ + char buf[16]; + return std::string(thread_getname_np(buf, 16) == 0 ? buf : ""); +} + +#else + // posix void set_thread_name(std::thread &thread, const char *thread_name) { @@ -114,6 +144,8 @@ std::string get_current_thread_name() return std::string(pthread_getname_np(pthread_self(), buf, 16) == 0 ? buf : ""); } +#endif + #endif // _WIN32 // Spawn (n - 1) worker threads on Intel TBB thread pool and name them by an index and a system thread ID.