1 /*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/platform_thread_types.h"
12
13 #if defined(WEBRTC_LINUX)
14 #include <sys/prctl.h>
15 #include <sys/syscall.h>
16 #endif
17
18 #if defined(WEBRTC_WIN)
19 #include "rtc_base/arraysize.h"
20
21 // The SetThreadDescription API was brought in version 1607 of Windows 10.
22 // For compatibility with various versions of winuser and avoid clashing with
23 // a potentially defined type, we use the RTC_ prefix.
24 typedef HRESULT(WINAPI* RTC_SetThreadDescription)(HANDLE hThread,
25 PCWSTR lpThreadDescription);
26 #endif
27
28 namespace rtc {
29
CurrentThreadId()30 PlatformThreadId CurrentThreadId() {
31 #if defined(WEBRTC_WIN)
32 return GetCurrentThreadId();
33 #elif defined(WEBRTC_POSIX)
34 #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
35 return pthread_mach_thread_np(pthread_self());
36 #elif defined(WEBRTC_ANDROID)
37 return gettid();
38 #elif defined(WEBRTC_FUCHSIA)
39 return zx_thread_self();
40 #elif defined(WEBRTC_LINUX)
41 return syscall(__NR_gettid);
42 #elif defined(__EMSCRIPTEN__)
43 return static_cast<PlatformThreadId>(pthread_self());
44 #else
45 // Default implementation for nacl and solaris.
46 return reinterpret_cast<PlatformThreadId>(pthread_self());
47 #endif
48 #endif // defined(WEBRTC_POSIX)
49 }
50
CurrentThreadRef()51 PlatformThreadRef CurrentThreadRef() {
52 #if defined(WEBRTC_WIN)
53 return GetCurrentThreadId();
54 #elif defined(WEBRTC_FUCHSIA)
55 return zx_thread_self();
56 #elif defined(WEBRTC_POSIX)
57 return pthread_self();
58 #endif
59 }
60
IsThreadRefEqual(const PlatformThreadRef & a,const PlatformThreadRef & b)61 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
62 #if defined(WEBRTC_WIN) || defined(WEBRTC_FUCHSIA)
63 return a == b;
64 #elif defined(WEBRTC_POSIX)
65 return pthread_equal(a, b);
66 #endif
67 }
68
SetCurrentThreadName(const char * name)69 void SetCurrentThreadName(const char* name) {
70 #if defined(WEBRTC_WIN)
71 // The SetThreadDescription API works even if no debugger is attached.
72 // The names set with this API also show up in ETW traces. Very handy.
73 static auto set_thread_description_func =
74 reinterpret_cast<RTC_SetThreadDescription>(::GetProcAddress(
75 ::GetModuleHandleA("Kernel32.dll"), "SetThreadDescription"));
76 if (set_thread_description_func) {
77 // Convert from ASCII to UTF-16.
78 wchar_t wide_thread_name[64];
79 for (size_t i = 0; i < arraysize(wide_thread_name) - 1; ++i) {
80 wide_thread_name[i] = name[i];
81 if (wide_thread_name[i] == L'\0')
82 break;
83 }
84 // Guarantee null-termination.
85 wide_thread_name[arraysize(wide_thread_name) - 1] = L'\0';
86 set_thread_description_func(::GetCurrentThread(), wide_thread_name);
87 }
88
89 // For details see:
90 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
91 #pragma pack(push, 8)
92 struct {
93 DWORD dwType;
94 LPCSTR szName;
95 DWORD dwThreadID;
96 DWORD dwFlags;
97 } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};
98 #pragma pack(pop)
99
100 #pragma warning(push)
101 #pragma warning(disable : 6320 6322)
102 __try {
103 ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(ULONG_PTR),
104 reinterpret_cast<ULONG_PTR*>(&threadname_info));
105 } __except (EXCEPTION_EXECUTE_HANDLER) { // NOLINT
106 }
107 #pragma warning(pop)
108 #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
109 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name)); // NOLINT
110 #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
111 pthread_setname_np(name);
112 #endif
113 }
114
115 } // namespace rtc
116