Bug 497028. Make sure to have mRunningEvent still incremented when we call the runnable's destructor, since that can also do work. r=bsmedberg
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 11 Jun 2009 13:09:35 -0400
changeset 29077 007d182fca17b1bb28ca19f329e53cec1bde75a2
parent 29076 740fcf842fab6fa8ce1f68a3d670a2efeeb4bb2f
child 29078 77e96e5f783dc7bc6279a63b685d5bb8575d0261
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs497028
milestone1.9.2a1pre
Bug 497028. Make sure to have mRunningEvent still incremented when we call the runnable's destructor, since that can also do work. r=bsmedberg
xpcom/threads/nsThread.cpp
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -491,34 +491,42 @@ nsThread::ProcessNextEvent(PRBool mayWai
   if (notifyGlobalObserver) 
     sGlobalObserver->OnProcessNextEvent(this, mayWait && !ShuttingDown(),
                                         mRunningEvent);
 
   nsCOMPtr<nsIThreadObserver> obs = mObserver;
   if (obs)
     obs->OnProcessNextEvent(this, mayWait && !ShuttingDown(), mRunningEvent);
 
-  // If we are shutting down, then do not wait for new events.
-  nsCOMPtr<nsIRunnable> event; 
-  mEvents->GetEvent(mayWait && !ShuttingDown(), getter_AddRefs(event));
-
-  *result = (event.get() != nsnull);
+  ++mRunningEvent;
 
   nsresult rv = NS_OK;
 
-  if (event) {
-    LOG(("THRD(%p) running [%p]\n", this, event.get()));
-    ++mRunningEvent;
-    event->Run();
-    --mRunningEvent;
-  } else if (mayWait) {
-    NS_ASSERTION(ShuttingDown(), "This should only happen when shutting down");
-    rv = NS_ERROR_UNEXPECTED;
+  {
+    // Scope for |event| to make sure that its destructor fires while
+    // mRunningEvent has been incremented, since that destructor can
+    // also do work.
+
+    // If we are shutting down, then do not wait for new events.
+    nsCOMPtr<nsIRunnable> event;
+    mEvents->GetEvent(mayWait && !ShuttingDown(), getter_AddRefs(event));
+
+    *result = (event.get() != nsnull);
+
+    if (event) {
+      LOG(("THRD(%p) running [%p]\n", this, event.get()));
+      event->Run();
+    } else if (mayWait) {
+      NS_ASSERTION(ShuttingDown(),
+                   "This should only happen when shutting down");
+      rv = NS_ERROR_UNEXPECTED;
+    }
   }
 
+  --mRunningEvent;
   if (obs)
     obs->AfterProcessNextEvent(this, mRunningEvent);
 
   if (notifyGlobalObserver && sGlobalObserver)
     sGlobalObserver->AfterProcessNextEvent(this, mRunningEvent);
 
   return rv;
 }