Skip to content

Commit

Permalink
Merge pull request #24789 from bluebear94/mf/set-thread-name
Browse files Browse the repository at this point in the history
Set thread name on Linux for debugging
  • Loading branch information
RomanPudashkin committed Sep 20, 2024
2 parents c40e1cb + 6faf918 commit 740034c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/framework/global/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,32 @@

#include "runtime.h"

#include "log.h"

#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
#include <pthread.h>
#endif
#if defined(Q_OS_FREEBSD)
#include <pthread_np.h> // 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()
Expand Down

0 comments on commit 740034c

Please sign in to comment.