Bug 1287623, part 5 - Convert CacheFileIOManager::gInstance to StaticRefPtr. r=mayhemer
authorAndrew McCreight <continuation@gmail.com>
Thu, 21 Jul 2016 09:54:17 -0700
changeset 346217 2f91720817ab2f49b2e61460d92a28220868d73e
parent 346216 c3676ef8ac272183a1f2e551f1966700a0790f3b
child 346218 6951eb08d08305ba5ffc3b8d956d8689f7b97cc3
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1287623
milestone50.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 1287623, part 5 - Convert CacheFileIOManager::gInstance to StaticRefPtr. r=mayhemer
netwerk/cache2/CacheFileIOManager.cpp
netwerk/cache2/CacheFileIOManager.h
--- a/netwerk/cache2/CacheFileIOManager.cpp
+++ b/netwerk/cache2/CacheFileIOManager.cpp
@@ -1087,17 +1087,17 @@ public:
     case SHUTDOWN:
       ioMan->ShutdownMetadataWriteSchedulingInternal();
       break;
     }
     return NS_OK;
   }
 };
 
-CacheFileIOManager * CacheFileIOManager::gInstance = nullptr;
+StaticRefPtr<CacheFileIOManager> CacheFileIOManager::gInstance;
 
 NS_IMPL_ISUPPORTS(CacheFileIOManager, nsITimerCallback)
 
 CacheFileIOManager::CacheFileIOManager()
   : mShuttingDown(false)
   , mTreeCreated(false)
   , mOverLimitEvicting(false)
   , mRemovingTrashDirs(false)
@@ -1125,17 +1125,17 @@ CacheFileIOManager::Init()
     return NS_ERROR_ALREADY_INITIALIZED;
   }
 
   RefPtr<CacheFileIOManager> ioMan = new CacheFileIOManager();
 
   nsresult rv = ioMan->InitInternal();
   NS_ENSURE_SUCCESS(rv, rv);
 
-  ioMan.swap(gInstance);
+  gInstance = ioMan.forget();
   return NS_OK;
 }
 
 nsresult
 CacheFileIOManager::InitInternal()
 {
   nsresult rv;
 
@@ -1149,17 +1149,17 @@ CacheFileIOManager::InitInternal()
 
   return NS_OK;
 }
 
 // static
 nsresult
 CacheFileIOManager::Shutdown()
 {
-  LOG(("CacheFileIOManager::Shutdown() [gInstance=%p]", gInstance));
+  LOG(("CacheFileIOManager::Shutdown() [gInstance=%p]", gInstance.get()));
 
   MOZ_ASSERT(NS_IsMainThread());
 
   if (!gInstance) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_SHUTDOWN_V2> shutdownTimer;
@@ -1180,18 +1180,17 @@ CacheFileIOManager::Shutdown()
 
   CacheIndex::Shutdown();
 
   if (CacheObserver::ClearCacheOnShutdown()) {
     Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE2_SHUTDOWN_CLEAR_PRIVATE> totalTimer;
     gInstance->SyncRemoveAllCacheFiles();
   }
 
-  RefPtr<CacheFileIOManager> ioMan;
-  ioMan.swap(gInstance);
+  gInstance = nullptr;
 
   return NS_OK;
 }
 
 nsresult
 CacheFileIOManager::ShutdownInternal()
 {
   LOG(("CacheFileIOManager::ShutdownInternal() [this=%p]", this));
@@ -1254,17 +1253,17 @@ CacheFileIOManager::ShutdownInternal()
 
   return NS_OK;
 }
 
 // static
 nsresult
 CacheFileIOManager::OnProfile()
 {
-  LOG(("CacheFileIOManager::OnProfile() [gInstance=%p]", gInstance));
+  LOG(("CacheFileIOManager::OnProfile() [gInstance=%p]", gInstance.get()));
 
   RefPtr<CacheFileIOManager> ioMan = gInstance;
   if (!ioMan) {
     // CacheFileIOManager::Init() failed, probably could not create the IO
     // thread, just go with it...
     return NS_ERROR_NOT_INITIALIZED;
   }
 
@@ -3059,17 +3058,17 @@ CacheFileIOManager::CacheIndexStateChang
   // CacheFileIOManager lives longer than CacheIndex so gInstance must be
   // non-null here.
   MOZ_ASSERT(gInstance);
 
   // We have to re-distatch even if we are on IO thread to prevent reentering
   // the lock in CacheIndex
   nsCOMPtr<nsIRunnable> ev;
   ev = NewRunnableMethod(
-    gInstance, &CacheFileIOManager::CacheIndexStateChangedInternal);
+    gInstance.get(), &CacheFileIOManager::CacheIndexStateChangedInternal);
 
   nsCOMPtr<nsIEventTarget> ioTarget = IOTarget();
   MOZ_ASSERT(ioTarget);
 
   rv = ioTarget->Dispatch(ev, nsIEventTarget::DISPATCH_NORMAL);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
--- a/netwerk/cache2/CacheFileIOManager.h
+++ b/netwerk/cache2/CacheFileIOManager.h
@@ -7,16 +7,17 @@
 
 #include "CacheIOThread.h"
 #include "CacheStorageService.h"
 #include "nsIEventTarget.h"
 #include "nsITimer.h"
 #include "nsCOMPtr.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/SHA1.h"
+#include "mozilla/StaticPtr.h"
 #include "mozilla/TimeStamp.h"
 #include "nsTArray.h"
 #include "nsString.h"
 #include "nsTHashtable.h"
 #include "prio.h"
 
 //#define DEBUG_HANDLES 1
 
@@ -436,17 +437,18 @@ private:
   // It is called in EvictIfOverLimitInternal() just before we decide whether to
   // start overlimit eviction or not and also in OverLimitEvictionInternal()
   // before we start an eviction loop.
   nsresult UpdateSmartCacheSize(int64_t aFreeSpace);
 
   // Memory reporting (private part)
   size_t SizeOfExcludingThisInternal(mozilla::MallocSizeOf mallocSizeOf) const;
 
-  static CacheFileIOManager           *gInstance;
+  static StaticRefPtr<CacheFileIOManager> gInstance;
+
   TimeStamp                            mStartTime;
   // Set true on the IO thread, CLOSE level as part of the internal shutdown
   // procedure.
   bool                                 mShuttingDown;
   RefPtr<CacheIOThread>                mIOThread;
   nsCOMPtr<nsIFile>                    mCacheDirectory;
 #if defined(MOZ_WIDGET_ANDROID)
   // On Android we add the active profile directory name between the path