Introduce a new constructor to PlatformThread.

The new constructor introduces two new changes:

* Support specifying thread priority at construction time.
  - Moving forward, the SetPriority() method will be removed.
* New thread function type.
  - The new type has 'void' as a return type and a polling loop
    inside PlatformThread, is not used.

The old function type is still supported until all places have been moved over.

In this CL, the first steps towards deprecating the old mechanism are taken
by moving parts of the code that were simple to move, over to the new callback
type.

BUG=webrtc:7187

Review-Url: https://codereview.webrtc.org/2708723003
Cr-Commit-Position: refs/heads/master@{#16779}
diff --git a/webrtc/base/platform_thread.cc b/webrtc/base/platform_thread.cc
index d1bd509..525f0dd 100644
--- a/webrtc/base/platform_thread.cc
+++ b/webrtc/base/platform_thread.cc
@@ -92,14 +92,27 @@
 #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 @@
   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 @@
 // 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 @@
     // 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 @@
 }
 
 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__)