Bug 982146 - Clean up SyncRunnable wait code, r=bsmedberg.
authorBen Turner <bent.mozilla@gmail.com>
Tue, 11 Mar 2014 09:42:58 -0700
changeset 173063 ed30fc4d3e178ad72fe63bf112956d96be876f6a
parent 173062 b79d5726ac06cef1508ac652e8bc58475cfb0840
child 173064 9948eaf63a9546241106554d2d02351bef3b5879
push id26387
push userkwierso@gmail.com
push dateWed, 12 Mar 2014 01:07:12 +0000
treeherdermozilla-central@dfd483bbeea6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs982146
milestone30.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 982146 - Clean up SyncRunnable wait code, r=bsmedberg.
xpcom/threads/SyncRunnable.h
--- a/xpcom/threads/SyncRunnable.h
+++ b/xpcom/threads/SyncRunnable.h
@@ -28,16 +28,17 @@ namespace mozilla {
  *
  */
 class SyncRunnable : public nsRunnable
 {
 public:
   SyncRunnable(nsIRunnable* r)
     : mRunnable(r)
     , mMonitor("SyncRunnable")
+    , mDone(false)
   { }
 
   void DispatchToThread(nsIEventTarget* thread,
                         bool forceDispatch = false)
   {
     nsresult rv;
     bool on;
 
@@ -45,39 +46,48 @@ public:
       rv = thread->IsOnCurrentThread(&on);
       MOZ_ASSERT(NS_SUCCEEDED(rv));
       if (NS_SUCCEEDED(rv) && on) {
         mRunnable->Run();
         return;
       }
     }
 
-    mozilla::MonitorAutoLock lock(mMonitor);
     rv = thread->Dispatch(this, NS_DISPATCH_NORMAL);
     if (NS_SUCCEEDED(rv)) {
-      lock.Wait();
+      mozilla::MonitorAutoLock lock(mMonitor);
+      while (!mDone) {
+        lock.Wait();
+      }
     }
   }
 
   static void DispatchToThread(nsIEventTarget* thread,
                                nsIRunnable* r,
                                bool forceDispatch = false)
   {
     nsRefPtr<SyncRunnable> s(new SyncRunnable(r));
     s->DispatchToThread(thread, forceDispatch);
   }
 
 protected:
   NS_IMETHODIMP Run()
   {
     mRunnable->Run();
-    mozilla::MonitorAutoLock(mMonitor).Notify();
+
+    mozilla::MonitorAutoLock lock(mMonitor);
+    MOZ_ASSERT(!mDone);
+
+    mDone = true;
+    mMonitor.Notify();
+
     return NS_OK;
   }
 
 private:
   nsCOMPtr<nsIRunnable> mRunnable;
   mozilla::Monitor mMonitor;
+  bool mDone;
 };
 
 } // namespace mozilla
 
 #endif // mozilla_SyncRunnable_h