-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_helper.h
68 lines (63 loc) · 2.14 KB
/
thread_helper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
// clang-format off
#include <string>
#if defined(WIN32)
#include <windows.h>
#include <processthreadsapi.h>
#include "coding_helper.h"
#elif defined(__LINUX__)
#include <pthread.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#elif defined(__ANDROID__)
#include <pthread.h>
#include <sys/prctl.h>
#include <unistd.h>
#if defined(__MACH__) || defined(__APPLE__)
#include <pthread.h>
#include <pthread_spis.h>
#include <unistd.h>
#endif
#endif
namespace helper {
#if defined(WIN32)
typedef DWORD PlatformThreadId;
typedef DWORD PlatformThreadRef;
#elif defined(__LINUX__) || defined(__ANDROID__) || defined(__MACH__) || defined(__APPLE__)
typedef pid_t PlatformThreadId;
typedef pthread_t PlatformThreadRef;
#endif
static inline PlatformThreadId CurrentThreadId() {
#if defined(WIN32)
return GetCurrentThreadId();
#elif defined(__MACH__) || defined(__APPLE__)
return pthread_mach_thread_np(pthread_self());
#elif defined(__ANDROID__)
return gettid();
#elif defined(__LINUX__)
return syscall(__NR_gettid);
#else
// Default implementation for nacl and solaris.
return reinterpret_cast<PlatformThreadId>(pthread_self());
#endif
}
static inline void SetCurrentThreadName(const std::string& name) {
#if defined(WIN32)
typedef HRESULT(WINAPI* RTC_SetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
static auto set_thread_description_func = reinterpret_cast<RTC_SetThreadDescription>
(::GetProcAddress(::GetModuleHandleA("Kernel32.dll"), "SetThreadDescription"));
if (set_thread_description_func) {
// The SetThreadDescription API works even if no debugger is attached.
// The names set with this API also show up in ETW traces. Very handy.
std::wstring wide_thread_name = helper::Utf82Unicode(name);
set_thread_description_func(::GetCurrentThread(), wide_thread_name.c_str());
}
#elif defined(__LINUX__) || defined(__ANDROID__)
prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name.c_str())); // NOLINT
#elif defined(__MACH__) || defined(__APPLE__)
pthread_setname_np(name.c_str());
#endif
}
} // end namespace helper
// clang-format on