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 #if defined(WEBRTC_FUCHSIA)
29 #include <string.h>
30 #include <zircon/syscalls.h>
31
32 #include "rtc_base/checks.h"
33 #endif
34
35 namespace rtc {
36
CurrentThreadId()37 PlatformThreadId CurrentThreadId() {
38 #if defined(WEBRTC_WIN)
39 return GetCurrentThreadId();
40 #elif defined(WEBRTC_POSIX)
41 #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
42 return pthread_mach_thread_np(pthread_self());
43 #elif defined(WEBRTC_ANDROID)
44 return gettid();
45 #elif defined(WEBRTC_FUCHSIA)
46 return zx_thread_self();
47 #elif defined(WEBRTC_LINUX)
48 return syscall(__NR_gettid);
49 #elif defined(__EMSCRIPTEN__)
50 return static_cast<PlatformThreadId>(pthread_self());
51 #else
52 // Default implementation for nacl and solaris.
53 return reinterpret_cast<PlatformThreadId>(pthread_self());
54 #endif
55 #endif // defined(WEBRTC_POSIX)
56 }
57
CurrentThreadRef()58 PlatformThreadRef CurrentThreadRef() {
59 #if defined(WEBRTC_WIN)
60 return GetCurrentThreadId();
61 #elif defined(WEBRTC_FUCHSIA)
62 return zx_thread_self();
63 #elif defined(WEBRTC_POSIX)
64 return pthread_self();
65 #endif
66 }
67
IsThreadRefEqual(const PlatformThreadRef & a,const PlatformThreadRef & b)68 bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
69 #if defined(WEBRTC_WIN) || defined(WEBRTC_FUCHSIA)
70 return a == b;
71 #elif defined(WEBRTC_POSIX)
72 return pthread_equal(a, b);
73 #endif
74 }
75
SetCurrentThreadName(const char * name)76 void SetCurrentThreadName(const char* name) {
77 #if defined(WEBRTC_WIN)
78 // The SetThreadDescription API works even if no debugger is attached.
79 // The names set with this API also show up in ETW traces. Very handy.
80 static auto set_thread_description_func =
81 reinterpret_cast<RTC_SetThreadDescription>(::GetProcAddress(
82 ::GetModuleHandleA("Kernel32.dll"), "SetThreadDescription"));
83 if (set_thread_description_func) {
84 // Convert from ASCII to UTF-16.
85 wchar_t wide_thread_name[64];
86 for (size_t i = 0; i < arraysize(wide_thread_name) - 1; ++i) {
87 wide_thread_name[i] = name[i];
88 if (wide_thread_name[i] == L'\0')
89 break;
90 }
91 // Guarantee null-termination.
92 wide_thread_name[arraysize(wide_thread_name) - 1] = L'\0';
93 set_thread_description_func(::GetCurrentThread(), wide_thread_name);
94 }
95
96 // For details see:
97 // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
98 #pragma pack(push, 8)
99 struct {
100 DWORD dwType;
101 LPCSTR szName;
102 DWORD dwThreadID;
103 DWORD dwFlags;
104 } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};
105 #pragma pack(pop)
106
107 #pragma warning(push)
108 #pragma warning(disable : 6320 6322)
109 __try {
110 ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(ULONG_PTR),
111 reinterpret_cast<ULONG_PTR*>(&threadname_info));
112 } __except (EXCEPTION_EXECUTE_HANDLER) { // NOLINT
113 }
114 #pragma warning(pop)
115 #elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
116 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name)); // NOLINT
117 #elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
118 pthread_setname_np(name);
119 #elif defined(WEBRTC_FUCHSIA)
120 zx_status_t status = zx_object_set_property(zx_thread_self(), ZX_PROP_NAME,
121 name, strlen(name));
122 RTC_DCHECK_EQ(status, ZX_OK);
123 #endif
124 }
125
126 } // namespace rtc
127