Bug 1287623, part 4 - Make CacheIndex::gInstance a StaticRefPtr. r=mayhemer
☠☠ backed out by d2c17679b215 ☠ ☠
authorAndrew McCreight <continuation@gmail.com>
Wed, 20 Jul 2016 15:19:00 -0700
changeset 331008 248153344e15de4cb10a5d5829a24059a861fe98
parent 331007 8aa409c9b1ce3ebd68d5e6f970da64cf91d9d011
child 331009 a48daec87ec9f637033e6ac7c1ed39d38aa177aa
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1287623
milestone50.0a1
Bug 1287623, part 4 - Make CacheIndex::gInstance a StaticRefPtr. r=mayhemer
netwerk/cache2/CacheIndex.cpp
netwerk/cache2/CacheIndex.h
--- a/netwerk/cache2/CacheIndex.cpp
+++ b/netwerk/cache2/CacheIndex.cpp
@@ -218,17 +218,17 @@ NS_IMETHODIMP FileOpenHelper::OnFileOpen
   mIndex->OnFileOpenedInternal(this, aHandle, aResult);
 
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS(FileOpenHelper, CacheFileIOListener);
 
 
-CacheIndex * CacheIndex::gInstance = nullptr;
+StaticRefPtr<CacheIndex> CacheIndex::gInstance;
 StaticMutex  CacheIndex::sLock;
 
 
 NS_IMPL_ADDREF(CacheIndex)
 NS_IMPL_RELEASE(CacheIndex)
 
 NS_INTERFACE_MAP_BEGIN(CacheIndex)
   NS_INTERFACE_MAP_ENTRY(mozilla::net::CacheFileIOListener)
@@ -284,17 +284,17 @@ CacheIndex::Init(nsIFile *aCacheDirector
     return NS_ERROR_ALREADY_INITIALIZED;
   }
 
   RefPtr<CacheIndex> idx = new CacheIndex();
 
   nsresult rv = idx->InitInternal(aCacheDirectory);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  idx.swap(gInstance);
+  gInstance = idx.forget();
   return NS_OK;
 }
 
 nsresult
 CacheIndex::InitInternal(nsIFile *aCacheDirectory)
 {
   nsresult rv;
 
@@ -311,17 +311,17 @@ CacheIndex::InitInternal(nsIFile *aCache
 // static
 nsresult
 CacheIndex::PreShutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   StaticMutexAutoLock lock(sLock);
 
-  LOG(("CacheIndex::PreShutdown() [gInstance=%p]", gInstance));
+  LOG(("CacheIndex::PreShutdown() [gInstance=%p]", gInstance.get()));
 
   nsresult rv;
   RefPtr<CacheIndex> index = gInstance;
 
   if (!index) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
@@ -405,20 +405,19 @@ CacheIndex::PreShutdownInternal()
 // static
 nsresult
 CacheIndex::Shutdown()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   StaticMutexAutoLock lock(sLock);
 
-  LOG(("CacheIndex::Shutdown() [gInstance=%p]", gInstance));
-
-  RefPtr<CacheIndex> index;
-  index.swap(gInstance);
+  LOG(("CacheIndex::Shutdown() [gInstance=%p]", gInstance.get()));
+
+  RefPtr<CacheIndex> index = gInstance.forget();
 
   if (!index) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   bool sanitize = CacheObserver::ClearCacheOnShutdown();
 
   LOG(("CacheIndex::Shutdown() - [state=%d, indexOnDiskIsValid=%d, "
--- a/netwerk/cache2/CacheIndex.h
+++ b/netwerk/cache2/CacheIndex.h
@@ -12,16 +12,17 @@
 #include "nsICacheStorageService.h"
 #include "nsICacheEntry.h"
 #include "nsILoadContextInfo.h"
 #include "nsTHashtable.h"
 #include "nsThreadUtils.h"
 #include "nsWeakReference.h"
 #include "mozilla/SHA1.h"
 #include "mozilla/StaticMutex.h"
+#include "mozilla/StaticPtr.h"
 #include "mozilla/EndianUtils.h"
 #include "mozilla/TimeStamp.h"
 
 class nsIFile;
 class nsIDirectoryEnumerator;
 class nsITimer;
 
 
@@ -929,17 +930,17 @@ private:
   void ReplaceRecordInIterators(CacheIndexRecord *aOldRecord,
                                 CacheIndexRecord *aNewRecord);
 
   // Memory reporting (private part)
   size_t SizeOfExcludingThisInternal(mozilla::MallocSizeOf mallocSizeOf) const;
 
   void ReportHashStats();
 
-  static CacheIndex *gInstance;
+  static mozilla::StaticRefPtr<CacheIndex> gInstance;
   static StaticMutex sLock;
 
   nsCOMPtr<nsIFile> mCacheDirectory;
 
   EState         mState;
   // Timestamp of time when the index was initialized. We use it to delay
   // initial update or build of index.
   TimeStamp      mStartTime;