Move SetCurrentThreadName to platform_thread.* in rtc_base_approved,
update all webrtc and libjingle code to use the same function and remove
extra implementations.
BUG=
R=andresp@webrtc.org
Review URL: https://webrtc-codereview.appspot.com/55439004
Cr-Commit-Position: refs/heads/master@{#9205}
diff --git a/webrtc/base/platform_thread.cc b/webrtc/base/platform_thread.cc
index d043c11..973f7f7 100644
--- a/webrtc/base/platform_thread.cc
+++ b/webrtc/base/platform_thread.cc
@@ -10,9 +10,12 @@
#include "webrtc/base/platform_thread.h"
+#include <string.h>
+
#include "webrtc/base/checks.h"
#if defined(WEBRTC_LINUX)
+#include <sys/prctl.h>
#include <sys/syscall.h>
#endif
@@ -54,4 +57,26 @@
#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<DWORD>(-1), 0};
+
+ __try {
+ ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD),
+ reinterpret_cast<ULONG_PTR*>(&threadname_info));
+ } __except (EXCEPTION_EXECUTE_HANDLER) {
+ }
+#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
+ prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(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 9629066..50033b3 100644
--- a/webrtc/base/platform_thread.h
+++ b/webrtc/base/platform_thread.h
@@ -35,6 +35,9 @@
// 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 6aded99..21109fa 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 @@
#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<ULONG_PTR*>(&info));
- }
- __except(EXCEPTION_CONTINUE_EXECUTION) {
- }
-}
-#endif // WEBRTC_WIN
-
void* Thread::PreRun(void* pv) {
ThreadInit* init = static_cast<ThreadInit*>(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 15ed11b..1f32e48 100644
--- a/webrtc/base/win32.cc
+++ b/webrtc/base/win32.cc
@@ -454,18 +454,4 @@
return ret;
}
-void SetCurrentThreadName(const char* name) {
- struct {
- DWORD dwType;
- LPCSTR szName;
- DWORD dwThreadID;
- DWORD dwFlags;
- } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};
-
- __try {
- ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD),
- reinterpret_cast<ULONG_PTR*>(&threadname_info));
- } __except (EXCEPTION_EXECUTE_HANDLER) {
- }
-}
} // namespace rtc
diff --git a/webrtc/base/win32.h b/webrtc/base/win32.h
index a302c47..1fc4221 100644
--- a/webrtc/base/win32.h
+++ b/webrtc/base/win32.h
@@ -126,9 +126,6 @@
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 bcf1c1b..dffd78d 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 <strsafe.h>
#include <uuids.h>
+#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 @@
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 @@
{
_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.
@@ -5070,30 +5071,6 @@
}
// ----------------------------------------------------------------------------
-// _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 4d30928..7fe92ac 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 @@
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 f79fe1f..1669d03 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 @@
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 <CaptureSinkFilter *>(m_pFilter)->LockReceive();
diff --git a/webrtc/system_wrappers/source/thread_posix.cc b/webrtc/system_wrappers/source/thread_posix.cc
index e326a29..3eb7f2a 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 <linux/unistd.h>
#include <sched.h>
-#include <sys/prctl.h>
-#include <sys/syscall.h>
#include <sys/types.h>
#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 @@
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<unsigned long>(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 aa03b91..7c6bd89 100644
--- a/webrtc/system_wrappers/source/thread_win.cc
+++ b/webrtc/system_wrappers/source/thread_win.cc
@@ -15,6 +15,7 @@
#include <windows.h>
#include "webrtc/base/checks.h"
+#include "webrtc/base/platform_thread.h"
#include "webrtc/system_wrappers/interface/trace.h"
namespace webrtc {
@@ -22,36 +23,6 @@
void CALLBACK RaiseFlag(ULONG_PTR param) {
*reinterpret_cast<bool*>(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<ULONG_PTR*>(&info));
- }
- __except(EXCEPTION_CONTINUE_EXECUTION) {
- }
-}
-
}
ThreadWindows::ThreadWindows(ThreadRunFunction func, void* obj,
@@ -120,7 +91,7 @@
void ThreadWindows::Run() {
if (!name_.empty())
- SetThreadName(static_cast<DWORD>(-1), name_.c_str());
+ rtc::SetCurrentThreadName(name_.c_str());
do {
// The interface contract of Start/Stop is that for a successfull call to