Bug 957707 - Merge management and IO threads in cache v2, r=michal
authorHonza Bambas <honzab.moz@firemni.cz>
Wed, 22 Jan 2014 18:54:52 +0100
changeset 181625 8624d24dbe671c042aecab9de089d19f1b8b88cc
parent 181624 196c55184482100e522f0f90ea251677e2d5dda1
child 181626 86ce91bee036fb10202ef015bd79033d2baf0005
push id462
push userraliiev@mozilla.com
push dateTue, 22 Apr 2014 00:22:30 +0000
treeherdermozilla-release@ac5db8c74ac0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs957707
milestone29.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 957707 - Merge management and IO threads in cache v2, r=michal
netwerk/cache2/CacheIOThread.cpp
netwerk/cache2/CacheIOThread.h
netwerk/cache2/CacheStorageService.cpp
netwerk/cache2/CacheStorageService.h
--- a/netwerk/cache2/CacheIOThread.cpp
+++ b/netwerk/cache2/CacheIOThread.cpp
@@ -230,16 +230,17 @@ bool CacheIOThread::EventsPending(uint32
 {
   return mLowestLevelWaiting < aLastLevel || mHasXPCOMEvents;
 }
 
 NS_IMETHODIMP CacheIOThread::OnDispatchedEvent(nsIThreadInternal *thread)
 {
   MonitorAutoLock lock(mMonitor);
   mHasXPCOMEvents = true;
+  MOZ_ASSERT(!mShutdown || (PR_GetCurrentThread() == mThread));
   lock.Notify();
   return NS_OK;
 }
 
 NS_IMETHODIMP CacheIOThread::OnProcessNextEvent(nsIThreadInternal *thread, bool mayWait, uint32_t recursionDepth)
 {
   return NS_OK;
 }
--- a/netwerk/cache2/CacheIOThread.h
+++ b/netwerk/cache2/CacheIOThread.h
@@ -26,18 +26,18 @@ public:
   CacheIOThread();
   virtual ~CacheIOThread();
 
   enum ELevel {
     OPEN_PRIORITY,
     READ_PRIORITY,
     OPEN,
     READ,
-    OPEN_TRUNCATE,
     WRITE,
+    MANAGEMENT,
     CLOSE,
     EVICT,
     LAST_LEVEL
   };
 
   nsresult Init();
   nsresult Dispatch(nsIRunnable* aRunnable, uint32_t aLevel);
   bool IsCurrentThread();
--- a/netwerk/cache2/CacheStorageService.cpp
+++ b/netwerk/cache2/CacheStorageService.cpp
@@ -94,18 +94,16 @@ CacheStorageService::CacheStorageService
 , mPurging(false)
 {
   CacheFileIOManager::Init();
 
   MOZ_ASSERT(!sSelf);
 
   sSelf = this;
   sGlobalEntryTables = new GlobalEntryTables();
-
-  NS_NewNamedThread("Cache Mngmnt", getter_AddRefs(mThread));
 }
 
 CacheStorageService::~CacheStorageService()
 {
   LOG(("CacheStorageService::~CacheStorageService"));
   sSelf = nullptr;
 
   if (mMemorySize != 0)
@@ -118,19 +116,17 @@ void CacheStorageService::Shutdown()
     return;
 
   LOG(("CacheStorageService::Shutdown - start"));
 
   mShutdown = true;
 
   nsCOMPtr<nsIRunnable> event =
     NS_NewRunnableMethod(this, &CacheStorageService::ShutdownBackground);
-
-  if (mThread)
-    mThread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
+  Dispatch(event);
 
   mozilla::MutexAutoLock lock(mLock);
   sGlobalEntryTables->Clear();
   delete sGlobalEntryTables;
   sGlobalEntryTables = nullptr;
 
   LOG(("CacheStorageService::Shutdown - done"));
 }
@@ -385,22 +381,44 @@ void CacheStorageService::DropPrivateBro
   sGlobalEntryTables->EnumerateRead(&CollectPrivateContexts, &keys);
 
   for (uint32_t i = 0; i < keys.Length(); ++i)
     DoomStorageEntries(keys[i], true, nullptr);
 }
 
 // Helper methods
 
+// static
+bool CacheStorageService::IsOnManagementThread()
+{
+  nsRefPtr<CacheStorageService> service = Self();
+  if (!service)
+    return false;
+
+  nsCOMPtr<nsIEventTarget> target = service->Thread();
+  if (!target)
+    return false;
+
+  bool currentThread;
+  nsresult rv = target->IsOnCurrentThread(&currentThread);
+  return NS_SUCCEEDED(rv) && currentThread;
+}
+
+already_AddRefed<nsIEventTarget> CacheStorageService::Thread() const
+{
+  return CacheFileIOManager::IOTarget();
+}
+
 nsresult CacheStorageService::Dispatch(nsIRunnable* aEvent)
 {
-  if (!mThread)
+  nsRefPtr<CacheIOThread> cacheIOThread = CacheFileIOManager::IOThread();
+  if (!cacheIOThread)
     return NS_ERROR_NOT_AVAILABLE;
 
-  return mThread->Dispatch(aEvent, nsIThread::DISPATCH_NORMAL);
+  return cacheIOThread->Dispatch(aEvent, CacheIOThread::MANAGEMENT);
 }
 
 // nsICacheStorageService
 
 NS_IMETHODIMP CacheStorageService::MemoryCacheStorage(nsILoadContextInfo *aLoadContextInfo,
                                                       nsICacheStorage * *_retval)
 {
   NS_ENSURE_ARG(aLoadContextInfo);
--- a/netwerk/cache2/CacheStorageService.h
+++ b/netwerk/cache2/CacheStorageService.h
@@ -50,19 +50,19 @@ public:
 
   CacheStorageService();
 
   void Shutdown();
   void DropPrivateBrowsingEntries();
 
   static CacheStorageService* Self() { return sSelf; }
   nsresult Dispatch(nsIRunnable* aEvent);
-  static bool IsOnManagementThread() { return sSelf && NS_GetCurrentThread() == sSelf->mThread; }
   static bool IsRunning() { return sSelf && !sSelf->mShutdown; }
-  nsIEventTarget* Thread() const { return mThread; }
+  static bool IsOnManagementThread();
+  already_AddRefed<nsIEventTarget> Thread() const;
   mozilla::Mutex& Lock() { return mLock; }
 
 private:
   virtual ~CacheStorageService();
   void ShutdownBackground();
 
 private:
   // The following methods may only be called on the management
@@ -184,19 +184,16 @@ private:
                            CacheEntryHandle** aResult);
 
   static CacheStorageService* sSelf;
 
   mozilla::Mutex mLock;
 
   bool mShutdown;
 
-  // The service thread
-  nsCOMPtr<nsIThread> mThread;
-
   // Accessible only on the service thread
   nsTArray<nsRefPtr<CacheEntry> > mFrecencyArray;
   nsTArray<nsRefPtr<CacheEntry> > mExpirationArray;
   mozilla::Atomic<uint32_t> mMemorySize;
   bool mPurging;
 };
 
 template<class T>