diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 6dccc484be..317c456488 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -217,6 +217,7 @@ rtc_source_set("rtc_base_approved_generic") { "platform_file.h", "platform_thread.cc", "platform_thread.h", + "platform_thread_types.cc", "platform_thread_types.h", "ptr_util.h", "race_checker.cc", diff --git a/rtc_base/criticalsection.cc b/rtc_base/criticalsection.cc index d92b478b9b..f9168d89e8 100644 --- a/rtc_base/criticalsection.cc +++ b/rtc_base/criticalsection.cc @@ -11,7 +11,7 @@ #include "rtc_base/criticalsection.h" #include "rtc_base/checks.h" -#include "rtc_base/platform_thread.h" +#include "rtc_base/platform_thread_types.h" // TODO(tommi): Split this file up to per-platform implementation files. diff --git a/rtc_base/platform_thread.cc b/rtc_base/platform_thread.cc index 9495ef43a3..25742c166f 100644 --- a/rtc_base/platform_thread.cc +++ b/rtc_base/platform_thread.cc @@ -25,66 +25,6 @@ #endif namespace rtc { - -PlatformThreadId CurrentThreadId() { - PlatformThreadId ret; -#if defined(WEBRTC_WIN) - ret = GetCurrentThreadId(); -#elif defined(WEBRTC_POSIX) -#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) - ret = pthread_mach_thread_np(pthread_self()); -#elif defined(WEBRTC_ANDROID) - ret = gettid(); -#elif defined(WEBRTC_FUCHSIA) - ret = zx_thread_self(); -#elif defined(WEBRTC_LINUX) - ret = syscall(__NR_gettid); -#else - // Default implementation for nacl and solaris. - ret = reinterpret_cast(pthread_self()); -#endif -#endif // defined(WEBRTC_POSIX) - RTC_DCHECK(ret); - return ret; -} - -PlatformThreadRef CurrentThreadRef() { -#if defined(WEBRTC_WIN) - return GetCurrentThreadId(); -#elif defined(WEBRTC_POSIX) - return pthread_self(); -#endif -} - -bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { -#if defined(WEBRTC_WIN) - return a == b; -#elif defined(WEBRTC_POSIX) - return pthread_equal(a, b); -#endif -} - -void SetCurrentThreadName(const char* name) { -#if defined(WEBRTC_WIN) - struct { - DWORD dwType; - LPCSTR szName; - DWORD dwThreadID; - DWORD dwFlags; - } threadname_info = {0x1000, name, static_cast(-1), 0}; - - __try { - ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD), - reinterpret_cast(&threadname_info)); - } __except (EXCEPTION_EXECUTE_HANDLER) { - } -#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) - prctl(PR_SET_NAME, reinterpret_cast(name)); -#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) - pthread_setname_np(name); -#endif -} - namespace { #if defined(WEBRTC_WIN) void CALLBACK RaiseFlag(ULONG_PTR param) { diff --git a/rtc_base/platform_thread.h b/rtc_base/platform_thread.h index 25fa036908..33921c209d 100644 --- a/rtc_base/platform_thread.h +++ b/rtc_base/platform_thread.h @@ -20,15 +20,6 @@ namespace rtc { -PlatformThreadId CurrentThreadId(); -PlatformThreadRef CurrentThreadRef(); - -// Compares two thread identifiers for equality. -bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); - -// Sets the current thread name. -void SetCurrentThreadName(const char* name); - // Callback function that the spawned thread will enter once spawned. // A return value of false is interpreted as that the function has no // more work to do and that the thread can be released. diff --git a/rtc_base/platform_thread_types.cc b/rtc_base/platform_thread_types.cc new file mode 100644 index 0000000000..feb0991211 --- /dev/null +++ b/rtc_base/platform_thread_types.cc @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "rtc_base/platform_thread_types.h" + +#if defined(WEBRTC_LINUX) +#include +#include +#endif + +#if defined(WEBRTC_FUCHSIA) +#include +#endif + +namespace rtc { + +PlatformThreadId CurrentThreadId() { +#if defined(WEBRTC_WIN) + return GetCurrentThreadId(); +#elif defined(WEBRTC_POSIX) +#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) + return pthread_mach_thread_np(pthread_self()); +#elif defined(WEBRTC_ANDROID) + return gettid(); +#elif defined(WEBRTC_FUCHSIA) + return zx_thread_self(); +#elif defined(WEBRTC_LINUX) + return syscall(__NR_gettid); +#else + // Default implementation for nacl and solaris. + return reinterpret_cast(pthread_self()); +#endif +#endif // defined(WEBRTC_POSIX) +} + +PlatformThreadRef CurrentThreadRef() { +#if defined(WEBRTC_WIN) + return GetCurrentThreadId(); +#elif defined(WEBRTC_POSIX) + return pthread_self(); +#endif +} + +bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { +#if defined(WEBRTC_WIN) + return a == b; +#elif defined(WEBRTC_POSIX) + return pthread_equal(a, b); +#endif +} + +void SetCurrentThreadName(const char* name) { +#if defined(WEBRTC_WIN) + struct { + DWORD dwType; + LPCSTR szName; + DWORD dwThreadID; + DWORD dwFlags; + } threadname_info = {0x1000, name, static_cast(-1), 0}; + + __try { + ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD), + reinterpret_cast(&threadname_info)); + } __except (EXCEPTION_EXECUTE_HANDLER) { // NOLINT + } +#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) + prctl(PR_SET_NAME, reinterpret_cast(name)); // NOLINT +#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) + pthread_setname_np(name); +#endif +} + +} // namespace rtc diff --git a/rtc_base/platform_thread_types.h b/rtc_base/platform_thread_types.h index 10c5de8397..9470e4526a 100644 --- a/rtc_base/platform_thread_types.h +++ b/rtc_base/platform_thread_types.h @@ -32,6 +32,21 @@ typedef pthread_t PlatformThreadRef; typedef pid_t PlatformThreadId; typedef pthread_t PlatformThreadRef; #endif + +// Retrieve the ID of the current thread. +PlatformThreadId CurrentThreadId(); + +// Retrieves a reference to the current thread. On Windows, this is the same +// as CurrentThreadId. On other platforms it's the pthread_t returned by +// pthread_self(). +PlatformThreadRef CurrentThreadRef(); + +// Compares two thread identifiers for equality. +bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b); + +// Sets the current thread name. +void SetCurrentThreadName(const char* name); + } // namespace rtc #endif // RTC_BASE_PLATFORM_THREAD_TYPES_H_