Bug 998880: don't busy-wait in nsThread waiting for shutdown or DISPATCH_SYNC r=bsmedberg
authorRandell Jesup <rjesup@jesup.org>
Tue, 22 Apr 2014 15:34:24 -0400
changeset 179965 2e2566a604f18eb8ab3a6d6d929c6af2bfffc3df
parent 179964 d4df499022c99c391dc2d1c2a0289d492357170e
child 179966 98f12f724867ef8e7deb34765c02d5cb6f78e103
child 180071 02515cf4fcfd1512e5f5e747062c7023e0d37a44
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersbsmedberg
bugs998880
milestone31.0a1
Bug 998880: don't busy-wait in nsThread waiting for shutdown or DISPATCH_SYNC r=bsmedberg
xpcom/threads/nsThread.cpp
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -458,18 +458,19 @@ nsThread::DispatchInternal(nsIRunnable *
         new nsThreadSyncDispatch(thread, event);
     if (!wrapper)
       return NS_ERROR_OUT_OF_MEMORY;
     nsresult rv = PutEvent(wrapper, target);
     // Don't wait for the event to finish if we didn't dispatch it...
     if (NS_FAILED(rv))
       return rv;
 
+    // Allows waiting; ensure no locks are held that would deadlock us!
     while (wrapper->IsPending())
-      NS_ProcessNextEvent(thread);
+      NS_ProcessNextEvent(thread, true);
     return wrapper->Result();
   }
 
   NS_ASSERTION(flags == NS_DISPATCH_NORMAL, "unexpected dispatch flags");
   return PutEvent(event, target);
 }
 
 //-----------------------------------------------------------------------------
@@ -534,18 +535,19 @@ nsThread::Shutdown()
   // XXXroc What if posting the event fails due to OOM?
   PutEvent(event, nullptr);
 
   // We could still end up with other events being added after the shutdown
   // task, but that's okay because we process pending events in ThreadFunc
   // after setting mShutdownContext just before exiting.
   
   // Process events on the current thread until we receive a shutdown ACK.
+  // Allows waiting; ensure no locks are held that would deadlock us!
   while (!context.shutdownAck)
-    NS_ProcessNextEvent(context.joiningThread);
+    NS_ProcessNextEvent(context.joiningThread, true);
 
   // Now, it should be safe to join without fear of dead-locking.
 
   PR_JoinThread(mThread);
   mThread = nullptr;
 
   // We hold strong references to our event observers, and once the thread is
   // shut down the observers can't easily unregister themselves. Do it here