Files
pytorch/c10/util/thread_name.cpp
Will Constable 0344f95c2e Add missing #include <array> to thread_name.cpp (#128664)
I got local compile errors (using clang 14.0.6) due to this missing include after pulling the
latest pytorch main.  It's totally puzzling why CI appears to pass
without this fix.  Hopefully someone else will have an idea if we are
missing some CI coverage or if I am using a strange build setup locally.

The PR introducing the compile errors was https://github.com/pytorch/pytorch/pull/128448.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/128664
Approved by: https://github.com/fduwjj, https://github.com/malfet, https://github.com/d4l3k
2024-06-14 07:49:09 +00:00

47 lines
981 B
C++

#include <c10/util/thread_name.h>
#include <algorithm>
#include <array>
#ifndef __GLIBC_PREREQ
#define __GLIBC_PREREQ(x, y) 0
#endif
#if defined(__GLIBC__) && __GLIBC_PREREQ(2, 12) && !defined(__APPLE__) && \
!defined(__ANDROID__)
#define C10_HAS_PTHREAD_SETNAME_NP
#endif
#ifdef C10_HAS_PTHREAD_SETNAME_NP
#include <pthread.h>
#endif
namespace c10 {
#ifdef C10_HAS_PTHREAD_SETNAME_NP
namespace {
// pthreads has a limit of 16 characters including the null termination byte.
constexpr size_t kMaxThreadName = 15;
} // namespace
#endif
void setThreadName(std::string name) {
#ifdef C10_HAS_PTHREAD_SETNAME_NP
name.resize(std::min(name.size(), kMaxThreadName));
pthread_setname_np(pthread_self(), name.c_str());
#endif
}
std::string getThreadName() {
#ifdef C10_HAS_PTHREAD_SETNAME_NP
std::array<char, kMaxThreadName + 1> name{};
pthread_getname_np(pthread_self(), name.data(), name.size());
return name.data();
#else
return "";
#endif
}
} // namespace c10