Chromium Code Reviews| Index: webrtc/base/platform_thread.cc |
| diff --git a/webrtc/base/platform_thread.cc b/webrtc/base/platform_thread.cc |
| index d1bd509bf1ad301591b0c4be6e648218cb196de4..8755f53727eb44cded48da679fe4f9eb1b5db693 100644 |
| --- a/webrtc/base/platform_thread.cc |
| +++ b/webrtc/base/platform_thread.cc |
| @@ -92,14 +92,26 @@ 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); |
|
the sun
2017/02/22 13:42:57
Where's the limit of 64 from? To my knowledge, the
tommi
2017/02/22 15:04:00
15 sounds good to me. I think we break that though
the sun
2017/02/22 16:31:30
sgtm
|
| + worker_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()); |
| + RTC_DCHECK(name_.length() < 64); |
| + worker_thread_checker_.DetachFromThread(); |
| } |
| PlatformThread::~PlatformThread() { |
| @@ -180,11 +192,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) |
| + worker_thread_checker_.DetachFromThread(); |
| } |
| // TODO(tommi): Deprecate the loop behavior in PlatformThread. |
| @@ -195,8 +210,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(worker_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 +227,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 +244,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(worker_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__) |