Bug 1274585 - Push HTTP cache index build when asked for disk cache size, r=michal
authorHonza Bambas <honzab.moz@firemni.cz>
Mon, 30 May 2016 04:48:00 +0200
changeset 299584 6750817f09e1329b156b6572663fecb65fafcb97
parent 299583 9143e6d98cdf0ec9825d76c048e4659e86b390d2
child 299585 f962268dd6dcba2e1d0173e79fb6986d30636a87
push id77636
push usercbook@mozilla.com
push dateTue, 31 May 2016 03:51:09 +0000
treeherdermozilla-inbound@f962268dd6dc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1274585
milestone49.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 1274585 - Push HTTP cache index build when asked for disk cache size, r=michal
netwerk/cache2/CacheIndex.cpp
netwerk/cache2/CacheIndex.h
--- a/netwerk/cache2/CacheIndex.cpp
+++ b/netwerk/cache2/CacheIndex.cpp
@@ -1364,16 +1364,30 @@ CacheIndex::AsyncGetDiskConsumption(nsIC
     observer->OnDiskConsumption(index->mIndexStats.Size() << 10);
     return NS_OK;
   }
 
   LOG(("CacheIndex::AsyncGetDiskConsumption - remembering callback"));
   // Will be called when the index get to the READY state.
   index->mDiskConsumptionObservers.AppendElement(observer);
 
+  // Move forward with index re/building if it is pending
+  RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
+  if (ioThread) {
+    ioThread->Dispatch(NS_NewRunnableFunction([]() -> void {
+      StaticMutexAutoLock lock(sLock);
+
+      RefPtr<CacheIndex> index = gInstance;
+      if (index && index->mUpdateTimer) {
+        index->mUpdateTimer->Cancel();
+        index->DelayedUpdateLocked();
+      }
+    }), CacheIOThread::INDEX);
+  }
+
   return NS_OK;
 }
 
 // static
 nsresult
 CacheIndex::GetIterator(nsILoadContextInfo *aInfo, bool aAddNew,
                         CacheIndexIterator **_retval)
 {
@@ -2495,54 +2509,65 @@ CacheIndex::FinishRead(bool aSucceeded)
 }
 
 // static
 void
 CacheIndex::DelayedUpdate(nsITimer *aTimer, void *aClosure)
 {
   LOG(("CacheIndex::DelayedUpdate()"));
 
-  nsresult rv;
   StaticMutexAutoLock lock(sLock);
-
   RefPtr<CacheIndex> index = gInstance;
 
   if (!index) {
     return;
   }
 
-  index->mUpdateTimer = nullptr;
-
-  if (!index->IsIndexUsable()) {
+  index->DelayedUpdateLocked();
+}
+
+// static
+void
+CacheIndex::DelayedUpdateLocked()
+{
+  LOG(("CacheIndex::DelayedUpdateLocked()"));
+
+  sLock.AssertCurrentThreadOwns();
+
+  nsresult rv;
+
+  mUpdateTimer = nullptr;
+
+  if (!IsIndexUsable()) {
     return;
   }
 
-  if (index->mState == READY && index->mShuttingDown) {
+  if (mState == READY && mShuttingDown) {
     return;
   }
 
   // mUpdateEventPending must be false here since StartUpdatingIndex() won't
   // schedule timer if it is true.
-  MOZ_ASSERT(!index->mUpdateEventPending);
-  if (index->mState != BUILDING && index->mState != UPDATING) {
-    LOG(("CacheIndex::DelayedUpdate() - Update was canceled"));
+  MOZ_ASSERT(!mUpdateEventPending);
+  if (mState != BUILDING && mState != UPDATING) {
+    LOG(("CacheIndex::DelayedUpdateLocked() - Update was canceled"));
     return;
   }
 
   // We need to redispatch to run with lower priority
   RefPtr<CacheIOThread> ioThread = CacheFileIOManager::IOThread();
   MOZ_ASSERT(ioThread);
 
-  index->mUpdateEventPending = true;
-  rv = ioThread->Dispatch(index, CacheIOThread::INDEX);
+  mUpdateEventPending = true;
+  rv = ioThread->Dispatch(this, CacheIOThread::INDEX);
   if (NS_FAILED(rv)) {
-    index->mUpdateEventPending = false;
-    NS_WARNING("CacheIndex::DelayedUpdate() - Can't dispatch event");
+    mUpdateEventPending = false;
+    NS_WARNING("CacheIndex::DelayedUpdateLocked() - Can't dispatch event");
     LOG(("CacheIndex::DelayedUpdate() - Can't dispatch event" ));
-    index->FinishUpdate(false);
+    FinishUpdate(false);
   }
 }
 
 nsresult
 CacheIndex::ScheduleUpdateTimer(uint32_t aDelay)
 {
   LOG(("CacheIndex::ScheduleUpdateTimer() [delay=%u]", aDelay));
 
--- a/netwerk/cache2/CacheIndex.h
+++ b/netwerk/cache2/CacheIndex.h
@@ -830,16 +830,17 @@ private:
   // to make sure mIndexStats contains correct information.
   void EnsureCorrectStats();
   // Finalizes reading process.
   void FinishRead(bool aSucceeded);
 
   // Following methods perform updating and building of the index.
   // Timer callback that starts update or build process.
   static void DelayedUpdate(nsITimer *aTimer, void *aClosure);
+  void DelayedUpdateLocked();
   // Posts timer event that start update or build process.
   nsresult ScheduleUpdateTimer(uint32_t aDelay);
   nsresult SetupDirectoryEnumerator();
   void InitEntryFromDiskData(CacheIndexEntry *aEntry,
                              CacheFileMetadata *aMetaData,
                              int64_t aFileSize);
   // Returns true when either a timer is scheduled or event is posted.
   bool IsUpdatePending();