1 | #include <c10/util/thread_name.h> |
---|---|
2 | |
3 | #include <algorithm> |
4 | |
5 | #ifndef __GLIBC_PREREQ |
6 | #define __GLIBC_PREREQ(x, y) 0 |
7 | #endif |
8 | |
9 | #if defined(__GLIBC__) && __GLIBC_PREREQ(2, 12) && !defined(__APPLE__) && \ |
10 | !defined(__ANDROID__) |
11 | #define C10_HAS_PTHREAD_SETNAME_NP |
12 | #endif |
13 | |
14 | #ifdef C10_HAS_PTHREAD_SETNAME_NP |
15 | #include <pthread.h> |
16 | #endif |
17 | |
18 | namespace c10 { |
19 | |
20 | void setThreadName(std::string name) { |
21 | #ifdef C10_HAS_PTHREAD_SETNAME_NP |
22 | constexpr size_t kMaxThreadName = 15; |
23 | name.resize(std::min(name.size(), kMaxThreadName)); |
24 | |
25 | pthread_setname_np(pthread_self(), name.c_str()); |
26 | #endif |
27 | } |
28 | |
29 | } // namespace c10 |
30 |