Add thread check to ModuleProcessThread::DeRegisterModule and remove all unnecessary locking that was there due to one implementation calling from a different thread.

The only thing that was holding us back was the indeterministic teardown of voe::Channel(), but it turned out that fixing it wasn't that hard :)

BUG=webrtc:4508

Review-Url: https://codereview.webrtc.org/2755273004
Cr-Commit-Position: refs/heads/master@{#17315}
diff --git a/webrtc/modules/utility/source/process_thread_impl.cc b/webrtc/modules/utility/source/process_thread_impl.cc
index 6252931..7ea2327 100644
--- a/webrtc/modules/utility/source/process_thread_impl.cc
+++ b/webrtc/modules/utility/source/process_thread_impl.cc
@@ -67,15 +67,8 @@
 
   RTC_DCHECK(!stop_);
 
-  {
-    // TODO(tommi): Since DeRegisterModule is currently being called from
-    // different threads in some cases (ChannelOwner), we need to lock access to
-    // the modules_ collection even on the controller thread.
-    // Once we've cleaned up those places, we can remove this lock.
-    rtc::CritScope lock(&lock_);
-    for (ModuleCallback& m : modules_)
-      m.module->ProcessThreadAttached(this);
-  }
+  for (ModuleCallback& m : modules_)
+    m.module->ProcessThreadAttached(this);
 
   thread_.reset(
       new rtc::PlatformThread(&ProcessThreadImpl::Run, this, thread_name_));
@@ -97,13 +90,6 @@
   thread_->Stop();
   stop_ = false;
 
-  // TODO(tommi): Since DeRegisterModule is currently being called from
-  // different threads in some cases (ChannelOwner), we need to lock access to
-  // the modules_ collection even on the controller thread.
-  // Since DeRegisterModule also checks thread_, we also need to hold the
-  // lock for the .reset() operation.
-  // Once we've cleaned up those places, we can remove this lock.
-  rtc::CritScope lock(&lock_);
   thread_.reset();
   for (ModuleCallback& m : modules_)
     m.module->ProcessThreadAttached(nullptr);
@@ -162,8 +148,7 @@
 }
 
 void ProcessThreadImpl::DeRegisterModule(Module* module) {
-  // Allowed to be called on any thread.
-  // TODO(tommi): Disallow this ^^^
+  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   RTC_DCHECK(module);
 
   {
@@ -171,18 +156,10 @@
     modules_.remove_if([&module](const ModuleCallback& m) {
         return m.module == module;
       });
-
-    // TODO(tommi): we currently need to hold the lock while calling out to
-    // ProcessThreadAttached.  This is to make sure that the thread hasn't been
-    // destroyed while we attach the module.  Once we can make sure
-    // DeRegisterModule isn't being called on arbitrary threads, we can move the
-    // |if (thread_.get())| check and ProcessThreadAttached() call outside the
-    // lock scope.
-
-    // Notify the module that it's been detached.
-    if (thread_.get())
-      module->ProcessThreadAttached(nullptr);
   }
+
+  // Notify the module that it's been detached.
+  module->ProcessThreadAttached(nullptr);
 }
 
 // static