Bug 1098701. Part 2: Allow disabling thread assertions. r=kats
authorMason Chang <mchang@mozilla.com>
Fri, 23 Jan 2015 11:19:53 -0800
changeset 225470 121f665885d5e5a4e05f149ef976a663ba87c551
parent 225469 ef538bd60b1c0d7a309ed00aa4c77f5fb0a43932
child 225471 1649a66f18e9860c0a1b613e7085e06818b508f3
push id28163
push userphilringnalda@gmail.com
push dateSat, 24 Jan 2015 16:27:39 +0000
treeherdermozilla-central@1cf171c1a177 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats
bugs1098701
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1098701. Part 2: Allow disabling thread assertions. r=kats
widget/VsyncDispatcher.cpp
widget/VsyncDispatcher.h
--- a/widget/VsyncDispatcher.cpp
+++ b/widget/VsyncDispatcher.cpp
@@ -2,24 +2,33 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "MainThreadUtils.h"
 #include "VsyncDispatcher.h"
 #include "VsyncSource.h"
 #include "gfxPlatform.h"
+#include "mozilla/layers/Compositor.h"
 #include "mozilla/layers/CompositorParent.h"
 
 #ifdef MOZ_ENABLE_PROFILER_SPS
 #include "GeckoProfiler.h"
 #include "ProfilerMarkers.h"
 #endif
 
 namespace mozilla {
+static bool sThreadAssertionsEnabled = true;
+
+void CompositorVsyncDispatcher::SetThreadAssertionsEnabled(bool aEnable)
+{
+  // Should only be used in test environments
+  MOZ_ASSERT(NS_IsMainThread());
+  sThreadAssertionsEnabled = aEnable;
+}
 
 CompositorVsyncDispatcher::CompositorVsyncDispatcher()
   : mCompositorObserverLock("CompositorObserverLock")
   , mDidShutdown(false)
 {
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(NS_IsMainThread());
 }
@@ -43,16 +52,26 @@ CompositorVsyncDispatcher::NotifyVsync(T
 
   MutexAutoLock lock(mCompositorObserverLock);
   if (mCompositorVsyncObserver) {
     mCompositorVsyncObserver->NotifyVsync(aVsyncTimestamp);
   }
 }
 
 void
+CompositorVsyncDispatcher::AssertOnCompositorThread()
+{
+  if (!sThreadAssertionsEnabled) {
+    return;
+  }
+
+  Compositor::AssertOnCompositorThread();
+}
+
+void
 CompositorVsyncDispatcher::ObserveVsync(bool aEnable)
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(XRE_IsParentProcess());
   if (mDidShutdown) {
     return;
   }
 
@@ -61,19 +80,21 @@ CompositorVsyncDispatcher::ObserveVsync(
   } else {
     gfxPlatform::GetPlatform()->GetHardwareVsync()->RemoveCompositorVsyncDispatcher(this);
   }
 }
 
 void
 CompositorVsyncDispatcher::SetCompositorVsyncObserver(VsyncObserver* aVsyncObserver)
 {
-  MOZ_ASSERT(layers::CompositorParent::IsInCompositorThread());
-  MutexAutoLock lock(mCompositorObserverLock);
-  mCompositorVsyncObserver = aVsyncObserver;
+  AssertOnCompositorThread();
+  { // scope lock
+    MutexAutoLock lock(mCompositorObserverLock);
+    mCompositorVsyncObserver = aVsyncObserver;
+  }
 
   bool observeVsync = aVsyncObserver != nullptr;
   nsCOMPtr<nsIRunnable> vsyncControl = NS_NewRunnableMethodWithArg<bool>(this,
                                         &CompositorVsyncDispatcher::ObserveVsync,
                                         observeVsync);
   NS_DispatchToMainThread(vsyncControl);
 }
 
--- a/widget/VsyncDispatcher.h
+++ b/widget/VsyncDispatcher.h
@@ -41,17 +41,23 @@ public:
 
   // Called on the vsync thread when a hardware vsync occurs
   void NotifyVsync(TimeStamp aVsyncTimestamp);
 
   // Compositor vsync observers must be added/removed on the compositor thread
   void SetCompositorVsyncObserver(VsyncObserver* aVsyncObserver);
   void Shutdown();
 
+  // This can be used to enable or disable thread assertions.
+  // This is useful for gtests because usually things run
+  // in only one thread in that environment
+  static void SetThreadAssertionsEnabled(bool aEnable);
+
 private:
+  void AssertOnCompositorThread();
   virtual ~CompositorVsyncDispatcher();
   void ObserveVsync(bool aEnable);
 
   Mutex mCompositorObserverLock;
   nsRefPtr<VsyncObserver> mCompositorVsyncObserver;
   bool mDidShutdown;
 };