Bug 829419 - Don't allow processing pending events while shutting down cache IO thread, r=jduell

This commit is contained in:
Michal Novotny 2013-04-16 17:40:08 +02:00
parent 62f493f674
commit 4f34c06608
5 changed files with 134 additions and 16 deletions

View File

@ -28,6 +28,7 @@ CPPSRCS = \
nsCacheMetaData.cpp \
nsCacheService.cpp \
nsCacheSession.cpp \
nsCacheUtils.cpp \
nsMemoryCacheDevice.cpp \
nsDiskCacheBinding.cpp \
nsDiskCacheBlockFile.cpp \

View File

@ -20,6 +20,7 @@
#include "nsICacheVisitor.h"
#include "nsDiskCacheDevice.h"
#include "nsDiskCacheDeviceSQL.h"
#include "nsCacheUtils.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
@ -1254,7 +1255,7 @@ nsCacheService::Shutdown()
}
if (cacheIOThread)
cacheIOThread->Shutdown();
nsShutdownThread::BlockingShutdown(cacheIOThread);
if (shouldSanitize) {
nsresult rv = parentDir->AppendNative(NS_LITERAL_CSTRING("Cache"));

85
netwerk/cache/nsCacheUtils.cpp vendored Normal file
View File

@ -0,0 +1,85 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCache.h"
#include "nsCacheUtils.h"
#include "nsThreadUtils.h"
using namespace mozilla;
class nsDestroyThreadEvent : public nsRunnable {
public:
nsDestroyThreadEvent(nsIThread *thread)
: mThread(thread)
{}
NS_IMETHOD Run()
{
mThread->Shutdown();
return NS_OK;
}
private:
nsCOMPtr<nsIThread> mThread;
};
nsShutdownThread::nsShutdownThread(nsIThread *aThread)
: mLock("nsShutdownThread.mLock")
, mCondVar(mLock, "nsShutdownThread.mCondVar")
, mThread(aThread)
{
}
nsShutdownThread::~nsShutdownThread()
{
}
nsresult
nsShutdownThread::Shutdown(nsIThread *aThread)
{
nsresult rv;
nsRefPtr<nsDestroyThreadEvent> ev = new nsDestroyThreadEvent(aThread);
rv = NS_DispatchToMainThread(ev);
if (NS_FAILED(rv)) {
NS_WARNING("Dispatching event in nsShutdownThread::Shutdown failed!");
}
return rv;
}
nsresult
nsShutdownThread::BlockingShutdown(nsIThread *aThread)
{
nsresult rv;
nsRefPtr<nsShutdownThread> st = new nsShutdownThread(aThread);
nsCOMPtr<nsIThread> workerThread;
rv = NS_NewNamedThread("thread shutdown", getter_AddRefs(workerThread));
if (NS_FAILED(rv)) {
NS_WARNING("Can't create nsShutDownThread worker thread!");
return rv;
}
{
MutexAutoLock lock(st->mLock);
rv = aThread->Dispatch(st, NS_DISPATCH_NORMAL);
if (NS_FAILED(rv)) {
NS_WARNING(
"Dispatching event in nsShutdownThread::BlockingShutdown failed!");
} else {
st->mCondVar.Wait();
}
}
return Shutdown(workerThread);
}
NS_IMETHODIMP
nsShutdownThread::Run()
{
MutexAutoLock lock(mLock);
mThread->Shutdown();
mCondVar.Notify();
return NS_OK;
}

44
netwerk/cache/nsCacheUtils.h vendored Normal file
View File

@ -0,0 +1,44 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef _nsCacheUtils_h_
#define _nsCacheUtils_h_
#include "nsThreadUtils.h"
#include "nsCOMPtr.h"
#include "mozilla/Mutex.h"
#include "mozilla/CondVar.h"
class nsIThread;
/**
* A class with utility methods for shutting down nsIThreads easily.
*/
class nsShutdownThread : public nsRunnable {
public:
nsShutdownThread(nsIThread *aThread);
~nsShutdownThread();
NS_IMETHOD Run();
/**
* Shutdown ensures that aThread->Shutdown() is called on a main thread
*/
static nsresult Shutdown(nsIThread *aThread);
/**
* BlockingShutdown ensures that by the time it returns, aThread->Shutdown() has
* been called and no pending events have been processed on the current thread.
*/
static nsresult BlockingShutdown(nsIThread *aThread);
private:
mozilla::Mutex mLock;
mozilla::CondVar mCondVar;
nsCOMPtr<nsIThread> mThread;
};
#endif

View File

@ -13,6 +13,7 @@
#include "nsAutoPtr.h"
#include "nsThreadUtils.h"
#include "nsISupportsPriority.h"
#include "nsCacheUtils.h"
#include <time.h>
using namespace mozilla;
@ -28,20 +29,6 @@ public:
}
};
class nsDestroyThreadEvent : public nsRunnable {
public:
nsDestroyThreadEvent(nsIThread *thread)
: mThread(thread)
{}
NS_IMETHOD Run()
{
mThread->Shutdown();
return NS_OK;
}
private:
nsCOMPtr<nsIThread> mThread;
};
nsDeleteDir * nsDeleteDir::gInstance = nullptr;
@ -155,7 +142,7 @@ nsDeleteDir::DestroyThread()
// more work to do, so don't delete thread.
return;
NS_DispatchToMainThread(new nsDestroyThreadEvent(mThread));
nsShutdownThread::Shutdown(mThread);
mThread = nullptr;
}