Index: webrtc/base/platform_thread.cc |
diff --git a/webrtc/base/platform_thread.cc b/webrtc/base/platform_thread.cc |
index d1bd509bf1ad301591b0c4be6e648218cb196de4..525f0ddf08d7e5959383c5c821dc36b823d4cc39 100644 |
--- a/webrtc/base/platform_thread.cc |
+++ b/webrtc/base/platform_thread.cc |
@@ -92,14 +92,27 @@ struct ThreadAttributes { |
#endif // defined(WEBRTC_WIN) |
} |
-PlatformThread::PlatformThread(ThreadRunFunction func, |
+PlatformThread::PlatformThread(ThreadRunFunctionDeprecated func, |
void* obj, |
const char* thread_name) |
- : run_function_(func), |
+ : run_function_deprecated_(func), |
obj_(obj), |
name_(thread_name ? thread_name : "webrtc") { |
RTC_DCHECK(func); |
RTC_DCHECK(name_.length() < 64); |
+ spawned_thread_checker_.DetachFromThread(); |
+} |
+ |
+PlatformThread::PlatformThread(ThreadRunFunction func, |
+ void* obj, |
+ const char* thread_name, |
+ ThreadPriority priority /*= kNormalPriority*/) |
+ : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) { |
+ RTC_DCHECK(func); |
+ RTC_DCHECK(!name_.empty()); |
+ // TODO(tommi): Consider lowering the limit to 15 (limit on Linux). |
+ RTC_DCHECK(name_.length() < 64); |
+ spawned_thread_checker_.DetachFromThread(); |
} |
PlatformThread::~PlatformThread() { |
@@ -180,11 +193,14 @@ void PlatformThread::Stop() { |
thread_ = nullptr; |
thread_id_ = 0; |
#else |
- RTC_CHECK_EQ(1, AtomicOps::Increment(&stop_flag_)); |
+ if (!run_function_) |
+ RTC_CHECK_EQ(1, AtomicOps::Increment(&stop_flag_)); |
RTC_CHECK_EQ(0, pthread_join(thread_, nullptr)); |
- AtomicOps::ReleaseStore(&stop_flag_, 0); |
+ if (!run_function_) |
+ AtomicOps::ReleaseStore(&stop_flag_, 0); |
thread_ = 0; |
#endif // defined(WEBRTC_WIN) |
+ spawned_thread_checker_.DetachFromThread(); |
} |
// TODO(tommi): Deprecate the loop behavior in PlatformThread. |
@@ -195,8 +211,16 @@ void PlatformThread::Stop() { |
// All implementations will need to be aware of how the thread should be stopped |
// and encouraging a busy polling loop, can be costly in terms of power and cpu. |
void PlatformThread::Run() { |
- if (!name_.empty()) |
- rtc::SetCurrentThreadName(name_.c_str()); |
+ // Attach the worker thread checker to this thread. |
+ RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
+ rtc::SetCurrentThreadName(name_.c_str()); |
+ |
+ if (run_function_) { |
+ SetPriority(priority_); |
+ run_function_(obj_); |
+ return; |
+ } |
+// TODO(tommi): Delete the below. |
#if !defined(WEBRTC_MAC) && !defined(WEBRTC_WIN) |
const struct timespec ts_null = {0}; |
#endif |
@@ -204,7 +228,7 @@ void PlatformThread::Run() { |
// The interface contract of Start/Stop is that for a successful call to |
// Start, there should be at least one call to the run function. So we |
// call the function before checking |stop_|. |
- if (!run_function_(obj_)) |
+ if (!run_function_deprecated_(obj_)) |
break; |
#if defined(WEBRTC_WIN) |
// Alertable sleep to permit RaiseFlag to run and update |stop_|. |
@@ -221,8 +245,19 @@ void PlatformThread::Run() { |
} |
bool PlatformThread::SetPriority(ThreadPriority priority) { |
- RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
- RTC_DCHECK(IsRunning()); |
+#if RTC_DCHECK_IS_ON |
+ if (run_function_) { |
+ // The non-deprecated way of how this function gets called, is that it must |
+ // be called on the worker thread itself. |
+ RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
+ } else { |
+ // In the case of deprecated use of this method, it must be called on the |
+ // same thread as the PlatformThread object is constructed on. |
+ RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
+ RTC_DCHECK(IsRunning()); |
+ } |
+#endif |
+ |
#if defined(WEBRTC_WIN) |
return SetThreadPriority(thread_, priority) != FALSE; |
#elif defined(__native_client__) |