Bug 1378970 - Part 2: Label CollectorRunner::mTimer using SystemGroup. r=billm
authorBevis Tseng <btseng@mozilla.com>
Tue, 18 Jul 2017 16:49:43 +0800
changeset 418643 bed655e34ed939af69706116bbdb1d97b1f87c77
parent 418642 7d8a372d113f3985735a1c3f35ec791b43f12264
child 418644 19b982efa54dc1176af09c19304c7622e51e47e8
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1378970
milestone56.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 1378970 - Part 2: Label CollectorRunner::mTimer using SystemGroup. r=billm MozReview-Commit-ID: LfCjWyKk4hR
dom/base/nsJSEnvironment.cpp
--- a/dom/base/nsJSEnvironment.cpp
+++ b/dom/base/nsJSEnvironment.cpp
@@ -280,34 +280,22 @@ public:
 
   void SetDeadline(mozilla::TimeStamp aDeadline) override
   {
     mDeadline = aDeadline;
   };
 
   void SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) override
   {
-    if (mTimerActive) {
-      return;
-    }
-
-    mTarget = aTarget;
-    if (!mTimer) {
-      mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
-    } else {
-      mTimer->Cancel();
-    }
-
-    if (mTimer) {
-      mTimer->SetTarget(mTarget);
-      mTimer->InitWithNamedFuncCallback(TimedOut, this, aDelay,
-                                        nsITimer::TYPE_ONE_SHOT,
-                                        "CollectorRunner");
-      mTimerActive = true;
-    }
+    MOZ_ASSERT(NS_IsMainThread());
+    // aTarget is always the main thread event target provided from
+    // NS_IdleDispatchToCurrentThread(). We ignore aTarget here to ensure that
+    // CollectorRunner a(ways run specifically on SystemGroup::EventTargetFor(
+    // TaskCategory::GarbageCollection) of the main thread.
+    SetTimerInternal(aDelay);
   }
 
   nsresult Cancel() override
   {
     CancelTimer();
     mTimer = nullptr;
     mScheduleTimer = nullptr;
     mCallback = nullptr;
@@ -334,23 +322,23 @@ public:
 
     mDeadline = TimeStamp();
     TimeStamp now = TimeStamp::Now();
     TimeStamp hint = nsRefreshDriver::GetIdleDeadlineHint(now);
     if (hint != now) {
       // RefreshDriver is ticking, let it schedule the idle dispatch.
       nsRefreshDriver::DispatchIdleRunnableAfterTick(this, mDelay);
       // Ensure we get called at some point, even if RefreshDriver is stopped.
-      SetTimer(mDelay, mTarget);
+      SetTimerInternal(mDelay);
     } else {
       // RefreshDriver doesn't seem to be running.
       if (aAllowIdleDispatch) {
         nsCOMPtr<nsIRunnable> runnable = this;
-        NS_IdleDispatchToCurrentThread(runnable.forget(), mDelay);
-        SetTimer(mDelay, mTarget);
+        SetTimerInternal(mDelay);
+        NS_IdleDispatchToCurrentThread(runnable.forget());
       } else {
         if (!mScheduleTimer) {
           mScheduleTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
           if (!mScheduleTimer) {
             return;
           }
           mScheduleTimer->SetTarget(SystemGroup::EventTargetFor(TaskCategory::GarbageCollection));
         } else {
@@ -388,19 +376,39 @@ private:
       mTimer->Cancel();
     }
     if (mScheduleTimer) {
       mScheduleTimer->Cancel();
     }
     mTimerActive = false;
   }
 
+  void SetTimerInternal(uint32_t aDelay)
+  {
+    if (mTimerActive) {
+      return;
+    }
+
+    if (!mTimer) {
+      mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+    } else {
+      mTimer->Cancel();
+    }
+
+    if (mTimer) {
+      mTimer->SetTarget(SystemGroup::EventTargetFor(TaskCategory::GarbageCollection));
+      mTimer->InitWithNamedFuncCallback(TimedOut, this, aDelay,
+                                        nsITimer::TYPE_ONE_SHOT,
+                                        "CollectorRunner");
+      mTimerActive = true;
+    }
+  }
+
   nsCOMPtr<nsITimer> mTimer;
   nsCOMPtr<nsITimer> mScheduleTimer;
-  nsCOMPtr<nsIEventTarget> mTarget;
   CollectorRunnerCallback mCallback;
   uint32_t mDelay;
   TimeStamp mDeadline;
   TimeDuration mBudget;
   bool mRepeating;
   bool mTimerActive;
   void* mData;
 };