diff --git a/webrtc/base/platform_thread.cc b/webrtc/base/platform_thread.cc index d043c11c80..973f7f7cf1 100644 --- a/webrtc/base/platform_thread.cc +++ b/webrtc/base/platform_thread.cc @@ -10,9 +10,12 @@ #include "webrtc/base/platform_thread.h" +#include + #include "webrtc/base/checks.h" #if defined(WEBRTC_LINUX) +#include #include #endif @@ -54,4 +57,26 @@ bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) { #endif } +void SetCurrentThreadName(const char* name) { + DCHECK(strlen(name) < 64); +#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 rtc diff --git a/webrtc/base/platform_thread.h b/webrtc/base/platform_thread.h index 96290669be..50033b3928 100644 --- a/webrtc/base/platform_thread.h +++ b/webrtc/base/platform_thread.h @@ -35,6 +35,9 @@ 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 // WEBRTC_BASE_PLATFORM_THREAD_H_ diff --git a/webrtc/base/thread.cc b/webrtc/base/thread.cc index 6aded99f1f..21109fadcd 100644 --- a/webrtc/base/thread.cc +++ b/webrtc/base/thread.cc @@ -22,6 +22,7 @@ #include "webrtc/base/common.h" #include "webrtc/base/logging.h" +#include "webrtc/base/platform_thread.h" #include "webrtc/base/stringutils.h" #include "webrtc/base/timeutils.h" @@ -350,41 +351,10 @@ void Thread::AssertBlockingIsAllowedOnCurrentThread() { #endif } -#if defined(WEBRTC_WIN) -// As seen on MSDN. -// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx -#define MSDEV_SET_THREAD_NAME 0x406D1388 -typedef struct tagTHREADNAME_INFO { - DWORD dwType; - LPCSTR szName; - DWORD dwThreadID; - DWORD dwFlags; -} THREADNAME_INFO; - -void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) { - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = szThreadName; - info.dwThreadID = dwThreadID; - info.dwFlags = 0; - - __try { - RaiseException(MSDEV_SET_THREAD_NAME, 0, sizeof(info) / sizeof(DWORD), - reinterpret_cast(&info)); - } - __except(EXCEPTION_CONTINUE_EXECUTION) { - } -} -#endif // WEBRTC_WIN - void* Thread::PreRun(void* pv) { ThreadInit* init = static_cast(pv); ThreadManager::Instance()->SetCurrentThread(init->thread); -#if defined(WEBRTC_WIN) - SetThreadName(GetCurrentThreadId(), init->thread->name_.c_str()); -#elif defined(WEBRTC_POSIX) - // TODO: See if naming exists for pthreads. -#endif + rtc::SetCurrentThreadName(init->thread->name_.c_str()); #if __has_feature(objc_arc) @autoreleasepool #elif defined(WEBRTC_MAC) diff --git a/webrtc/base/win32.cc b/webrtc/base/win32.cc index 15ed11b16b..1f32e48fc7 100644 --- a/webrtc/base/win32.cc +++ b/webrtc/base/win32.cc @@ -454,18 +454,4 @@ bool GetCurrentProcessIntegrityLevel(int* level) { return ret; } -void SetCurrentThreadName(const char* name) { - 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) { - } -} } // namespace rtc diff --git a/webrtc/base/win32.h b/webrtc/base/win32.h index a302c47a45..1fc4221aec 100644 --- a/webrtc/base/win32.h +++ b/webrtc/base/win32.h @@ -126,9 +126,6 @@ inline bool IsCurrentProcessLowIntegrity() { bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable); -// Sets the current thread name for the windows debugger. -void SetCurrentThreadName(const char* name); - } // namespace rtc #endif // WEBRTC_WIN diff --git a/webrtc/modules/audio_device/win/audio_device_core_win.cc b/webrtc/modules/audio_device/win/audio_device_core_win.cc index bcf1c1bb2a..dffd78df45 100644 --- a/webrtc/modules/audio_device/win/audio_device_core_win.cc +++ b/webrtc/modules/audio_device/win/audio_device_core_win.cc @@ -35,6 +35,7 @@ #include #include +#include "webrtc/base/platform_thread.h" #include "webrtc/modules/audio_device/audio_device_utility.h" #include "webrtc/system_wrappers/interface/sleep.h" #include "webrtc/system_wrappers/interface/trace.h" @@ -3389,7 +3390,7 @@ DWORD AudioDeviceWindowsCore::DoRenderThread() return 1; } - _SetThreadName(0, "webrtc_core_audio_render_thread"); + rtc::SetCurrentThreadName("webrtc_core_audio_render_thread"); // Use Multimedia Class Scheduler Service (MMCSS) to boost the thread priority. // @@ -3666,7 +3667,7 @@ DWORD AudioDeviceWindowsCore::InitCaptureThreadPriority() { _hMmTask = NULL; - _SetThreadName(0, "webrtc_core_audio_capture_thread"); + rtc::SetCurrentThreadName("webrtc_core_audio_capture_thread"); // Use Multimedia Class Scheduler Service (MMCSS) to boost the thread // priority. @@ -5069,30 +5070,6 @@ void AudioDeviceWindowsCore::_TraceCOMError(HRESULT hr) const WEBRTC_TRACE(kTraceError, kTraceAudioDevice, _id, "%s", WideToUTF8(buf)); } -// ---------------------------------------------------------------------------- -// _SetThreadName -// ---------------------------------------------------------------------------- - -void AudioDeviceWindowsCore::_SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) -{ - // See http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx for details on the code - // in this function. Name of article is "Setting a Thread Name (Unmanaged)". - - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = szThreadName; - info.dwThreadID = dwThreadID; - info.dwFlags = 0; - - __try - { - RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR *)&info ); - } - __except (EXCEPTION_CONTINUE_EXECUTION) - { - } -} - // ---------------------------------------------------------------------------- // WideToUTF8 // ---------------------------------------------------------------------------- diff --git a/webrtc/modules/audio_device/win/audio_device_core_win.h b/webrtc/modules/audio_device/win/audio_device_core_win.h index 4d30928c5e..7fe92ac2e9 100644 --- a/webrtc/modules/audio_device/win/audio_device_core_win.h +++ b/webrtc/modules/audio_device/win/audio_device_core_win.h @@ -235,7 +235,6 @@ private: // thread functions static DWORD WINAPI SetCaptureVolumeThread(LPVOID context); DWORD DoSetCaptureVolumeThread(); - void _SetThreadName(DWORD dwThreadID, LPCSTR szThreadName); void _Lock() { _critSect.Enter(); }; void _UnLock() { _critSect.Leave(); }; diff --git a/webrtc/modules/video_capture/windows/sink_filter_ds.cc b/webrtc/modules/video_capture/windows/sink_filter_ds.cc index f79fe1f5e6..1669d0384d 100644 --- a/webrtc/modules/video_capture/windows/sink_filter_ds.cc +++ b/webrtc/modules/video_capture/windows/sink_filter_ds.cc @@ -10,6 +10,7 @@ #include "webrtc/modules/video_capture/windows/sink_filter_ds.h" +#include "webrtc/base/platform_thread.h" #include "webrtc/modules/video_capture/windows/help_functions_ds.h" #include "webrtc/system_wrappers/interface/trace.h" @@ -328,24 +329,8 @@ CaptureInputPin::Receive ( IN IMediaSample * pIMediaSample ) HANDLE handle= GetCurrentThread(); SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST); _threadHandle = handle; - // See http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx for details on the code - // in this function. Name od article is "Setting a Thread Name (Unmanaged)". - - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = "capture_thread"; - info.dwThreadID = (DWORD)-1; - info.dwFlags = 0; - - __try - { - RaiseException( 0x406D1388, 0, sizeof(info)/sizeof(DWORD), - (DWORD_PTR*)&info ); - } - __except (EXCEPTION_CONTINUE_EXECUTION) - { - } + rtc::SetCurrentThreadName("webrtc_video_capture"); } reinterpret_cast (m_pFilter)->LockReceive(); diff --git a/webrtc/system_wrappers/source/thread_posix.cc b/webrtc/system_wrappers/source/thread_posix.cc index e326a29be7..3eb7f2ad02 100644 --- a/webrtc/system_wrappers/source/thread_posix.cc +++ b/webrtc/system_wrappers/source/thread_posix.cc @@ -17,12 +17,11 @@ #ifdef WEBRTC_LINUX #include #include -#include -#include #include #endif #include "webrtc/base/checks.h" +#include "webrtc/base/platform_thread.h" #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" #include "webrtc/system_wrappers/interface/event_wrapper.h" #include "webrtc/system_wrappers/interface/sleep.h" @@ -152,11 +151,7 @@ void ThreadPosix::Run() { if (!name_.empty()) { // Setting the thread name may fail (harmlessly) if running inside a // sandbox. Ignore failures if they happen. -#if defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID) - prctl(PR_SET_NAME, reinterpret_cast(name_.c_str())); -#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS) - pthread_setname_np(name_.substr(0, 63).c_str()); -#endif + rtc::SetCurrentThreadName(name_.substr(0, 63).c_str()); } // It's a requirement that for successful thread creation that the run diff --git a/webrtc/system_wrappers/source/thread_win.cc b/webrtc/system_wrappers/source/thread_win.cc index aa03b91809..7c6bd89547 100644 --- a/webrtc/system_wrappers/source/thread_win.cc +++ b/webrtc/system_wrappers/source/thread_win.cc @@ -15,6 +15,7 @@ #include #include "webrtc/base/checks.h" +#include "webrtc/base/platform_thread.h" #include "webrtc/system_wrappers/interface/trace.h" namespace webrtc { @@ -22,36 +23,6 @@ namespace { void CALLBACK RaiseFlag(ULONG_PTR param) { *reinterpret_cast(param) = true; } - -// TODO(tommi): This is borrowed from webrtc/base/thread.cc, but we can't -// include thread.h from here since thread.h pulls in libjingle dependencies. -// Would be good to consolidate. - -// As seen on MSDN. -// http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.71).aspx -#define MSDEV_SET_THREAD_NAME 0x406D1388 -typedef struct tagTHREADNAME_INFO { - DWORD dwType; - LPCSTR szName; - DWORD dwThreadID; - DWORD dwFlags; -} THREADNAME_INFO; - -void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName) { - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = szThreadName; - info.dwThreadID = dwThreadID; - info.dwFlags = 0; - - __try { - RaiseException(MSDEV_SET_THREAD_NAME, 0, sizeof(info) / sizeof(DWORD), - reinterpret_cast(&info)); - } - __except(EXCEPTION_CONTINUE_EXECUTION) { - } -} - } ThreadWindows::ThreadWindows(ThreadRunFunction func, void* obj, @@ -120,7 +91,7 @@ bool ThreadWindows::SetPriority(ThreadPriority priority) { void ThreadWindows::Run() { if (!name_.empty()) - SetThreadName(static_cast(-1), name_.c_str()); + rtc::SetCurrentThreadName(name_.c_str()); do { // The interface contract of Start/Stop is that for a successfull call to