diff --git a/src/framework/global/runtime.cpp b/src/framework/global/runtime.cpp index c0acb42ac191e..625576def3c08 100644 --- a/src/framework/global/runtime.cpp +++ b/src/framework/global/runtime.cpp @@ -22,11 +22,32 @@ #include "runtime.h" +#include "log.h" + +#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS) +#include +#endif +#if defined(Q_OS_FREEBSD) +#include // Needed for pthread_setname_np on FreeBSD +#endif + static thread_local std::string s_threadName; void muse::runtime::setThreadName(const std::string& name) { s_threadName = name; +#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) + // Set thread name through pthreads to aid debuggers that display such names. + // Thread names are limited to 16 bytes on Linux, including the + // terminating null. + DO_ASSERT(name.length() <= 15); + std::string truncated_name = name.length() > 15 ? name.substr(0, 15) : name; + if (pthread_setname_np(pthread_self(), truncated_name.c_str()) > 0) { + LOGW() << "Couldn't set thread name through pthreads"; + } +#elif defined(Q_OS_MACOS) + pthread_setname_np(name.c_str()); +#endif } const std::string& muse::runtime::threadName()