fixes bug 332730 "lock reentrancy through nsCacheEntryDescriptor::Close (deadlock)" r=biesi sr=bzbarsky

This commit is contained in:
darin%meer.net 2006-06-27 23:13:48 +00:00
parent 4b77a427c8
commit edc7e0ce24
10 changed files with 221 additions and 99 deletions

View File

@ -45,7 +45,6 @@
#include "nsCacheMetaData.h"
#include "nsCacheRequest.h"
#include "nsThreadUtils.h"
#include "nsProxyRelease.h"
#include "nsError.h"
#include "nsICacheService.h"
#include "nsCache.h"
@ -82,16 +81,8 @@ nsCacheEntry::~nsCacheEntry()
MOZ_COUNT_DTOR(nsCacheEntry);
delete mKey;
if (IsStreamData()) return;
// proxy release of of memory cache nsISupports objects
if (!mData) return;
nsISupports *data = nsnull;
mData.swap(data); // this reference will be owned by the proxy
// release our reference before switching threads
NS_ProxyRelease(mThread, data);
if (mData)
nsCacheService::ReleaseObject_Locked(mData, mThread);
}
@ -141,9 +132,17 @@ nsCacheEntry::TouchData()
void
nsCacheEntry::SetThread()
nsCacheEntry::SetData(nsISupports * data)
{
mThread = do_GetCurrentThread();
if (mData) {
nsCacheService::ReleaseObject_Locked(mData, mThread);
mData = nsnull;
}
if (data) {
NS_ADDREF(mData = data);
mThread = do_GetCurrentThread();
}
}

View File

@ -104,15 +104,13 @@ public:
* Data accessors
*/
nsISupports *Data() { return mData; }
void SetData( nsISupports * data) { mData = data; }
void SetData( nsISupports * data);
PRUint32 DataSize() { return mDataSize; }
void SetDataSize( PRUint32 size) { mDataSize = size; }
void TouchData();
void SetThread();
/**
* Meta data accessors
*/
@ -241,7 +239,7 @@ private:
PRUint32 mDataSize; // 4
nsCacheDevice * mCacheDevice; // 4
nsCOMPtr<nsISupports> mSecurityInfo; //
nsCOMPtr<nsISupports> mData; //
nsISupports * mData; // strong ref
nsCOMPtr<nsIThread> mThread;
nsCacheMetaData mMetaData; // 4
PRCList mRequestQ; // 8

View File

@ -46,7 +46,6 @@
#include "nsReadableUtils.h"
#include "nsIOutputStream.h"
#include "nsCRT.h"
#include "nsAutoLock.h"
NS_IMPL_THREADSAFE_ISUPPORTS2(nsCacheEntryDescriptor,
nsICacheEntryDescriptor,
@ -81,7 +80,8 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetClientID(char ** result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return ClientIDFromCacheKey(*(mCacheEntry->Key()), result);
@ -92,7 +92,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetDeviceID(char ** result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = nsCRT::strdup(mCacheEntry->GetDeviceID());
@ -103,7 +103,7 @@ nsCacheEntryDescriptor::GetDeviceID(char ** result)
NS_IMETHODIMP
nsCacheEntryDescriptor::GetKey(nsACString &result)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return ClientKeyFromCacheKey(*(mCacheEntry->Key()), result);
@ -114,7 +114,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetFetchCount(PRInt32 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->FetchCount();
@ -126,7 +126,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetLastFetched(PRUint32 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->LastFetched();
@ -138,7 +138,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetLastModified(PRUint32 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->LastModified();
@ -150,7 +150,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetExpirationTime(PRUint32 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->ExpirationTime();
@ -161,7 +161,7 @@ nsCacheEntryDescriptor::GetExpirationTime(PRUint32 *result)
NS_IMETHODIMP
nsCacheEntryDescriptor::SetExpirationTime(PRUint32 expirationTime)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
mCacheEntry->SetExpirationTime(expirationTime);
@ -173,7 +173,7 @@ nsCacheEntryDescriptor::SetExpirationTime(PRUint32 expirationTime)
NS_IMETHODIMP nsCacheEntryDescriptor::IsStreamBased(PRBool *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->IsStreamData();
@ -184,7 +184,7 @@ NS_IMETHODIMP nsCacheEntryDescriptor::IsStreamBased(PRBool *result)
NS_IMETHODIMP nsCacheEntryDescriptor::GetDataSize(PRUint32 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
*result = mCacheEntry->DataSize();
@ -195,7 +195,7 @@ NS_IMETHODIMP nsCacheEntryDescriptor::GetDataSize(PRUint32 *result)
nsresult
nsCacheEntryDescriptor::RequestDataSizeChange(PRInt32 deltaSize)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
nsresult rv;
@ -213,7 +213,7 @@ nsCacheEntryDescriptor::RequestDataSizeChange(PRInt32 deltaSize)
NS_IMETHODIMP
nsCacheEntryDescriptor::SetDataSize(PRUint32 dataSize)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
// XXX review for signed/unsigned math errors
@ -241,7 +241,7 @@ nsCacheEntryDescriptor::OpenInputStream(PRUint32 offset, nsIInputStream ** resul
NS_ENSURE_ARG_POINTER(result);
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
if (!mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_NOT_STREAM;
@ -264,7 +264,7 @@ nsCacheEntryDescriptor::OpenOutputStream(PRUint32 offset, nsIOutputStream ** res
NS_ENSURE_ARG_POINTER(result);
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
if (!mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_NOT_STREAM;
@ -286,7 +286,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetCacheElement(nsISupports ** result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
if (mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_STREAM;
@ -298,7 +298,7 @@ nsCacheEntryDescriptor::GetCacheElement(nsISupports ** result)
NS_IMETHODIMP
nsCacheEntryDescriptor::SetCacheElement(nsISupports * cacheElement)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
if (mCacheEntry->IsStreamData()) return NS_ERROR_CACHE_DATA_IS_STREAM;
@ -319,7 +319,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetStoragePolicy(nsCacheStoragePolicy *result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return mCacheEntry->StoragePolicy();
@ -329,7 +329,7 @@ nsCacheEntryDescriptor::GetStoragePolicy(nsCacheStoragePolicy *result)
NS_IMETHODIMP
nsCacheEntryDescriptor::SetStoragePolicy(nsCacheStoragePolicy policy)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
// XXX validate policy against session?
@ -347,7 +347,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetFile(nsIFile ** result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return nsCacheService::GetFileForEntry(mCacheEntry, result);
@ -358,7 +358,7 @@ NS_IMETHODIMP
nsCacheEntryDescriptor::GetSecurityInfo(nsISupports ** result)
{
NS_ENSURE_ARG_POINTER(result);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return mCacheEntry->GetSecurityInfo(result);
@ -368,7 +368,7 @@ nsCacheEntryDescriptor::GetSecurityInfo(nsISupports ** result)
NS_IMETHODIMP
nsCacheEntryDescriptor::SetSecurityInfo(nsISupports * securityInfo)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
mCacheEntry->SetSecurityInfo(securityInfo);
@ -380,7 +380,7 @@ nsCacheEntryDescriptor::SetSecurityInfo(nsISupports * securityInfo)
NS_IMETHODIMP
nsCacheEntryDescriptor::Doom()
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return nsCacheService::DoomEntry(mCacheEntry);
@ -390,7 +390,7 @@ nsCacheEntryDescriptor::Doom()
NS_IMETHODIMP
nsCacheEntryDescriptor::DoomAndFailPendingRequests(nsresult status)
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
return NS_ERROR_NOT_IMPLEMENTED;
@ -400,7 +400,7 @@ nsCacheEntryDescriptor::DoomAndFailPendingRequests(nsresult status)
NS_IMETHODIMP
nsCacheEntryDescriptor::MarkValid()
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
nsresult rv = nsCacheService::ValidateEntry(mCacheEntry);
@ -411,7 +411,7 @@ nsCacheEntryDescriptor::MarkValid()
NS_IMETHODIMP
nsCacheEntryDescriptor::Close()
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
// XXX perhaps closing descriptors should clear/sever transports
@ -430,7 +430,7 @@ nsCacheEntryDescriptor::GetMetaDataElement(const char *key, char **result)
NS_ENSURE_ARG_POINTER(key);
*result = nsnull;
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
NS_ENSURE_TRUE(mCacheEntry, NS_ERROR_NOT_AVAILABLE);
const char *value;
@ -450,7 +450,7 @@ nsCacheEntryDescriptor::SetMetaDataElement(const char *key, const char *value)
{
NS_ENSURE_ARG_POINTER(key);
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
NS_ENSURE_TRUE(mCacheEntry, NS_ERROR_NOT_AVAILABLE);
// XXX allow null value, for clearing key?
@ -465,7 +465,7 @@ nsCacheEntryDescriptor::SetMetaDataElement(const char *key, const char *value)
NS_IMETHODIMP
nsCacheEntryDescriptor::VisitMetaData(nsICacheMetaDataVisitor * visitor)
{
nsAutoLock lock(nsCacheService::ServiceLock()); // XXX check callers, we're calling out of module
nsCacheServiceAutoLock lock; // XXX check callers, we're calling out of module
NS_ENSURE_ARG_POINTER(visitor);
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;
@ -484,7 +484,7 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(nsCacheEntryDescriptor::nsInputStreamWrapper,
nsresult nsCacheEntryDescriptor::
nsInputStreamWrapper::LazyInit()
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
nsCacheAccessMode mode;
nsresult rv = mDescriptor->GetAccessGranted(&mode);
@ -561,7 +561,7 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(nsCacheEntryDescriptor::nsOutputStreamWrapper,
nsresult nsCacheEntryDescriptor::
nsOutputStreamWrapper::LazyInit()
{
nsAutoLock lock(nsCacheService::ServiceLock());
nsCacheServiceAutoLock lock;
nsCacheAccessMode mode;
nsresult rv = mDescriptor->GetAccessGranted(&mode);

View File

@ -46,6 +46,7 @@
#include "nsICache.h"
#include "nsICacheListener.h"
#include "nsCacheSession.h"
#include "nsCacheService.h"
class nsCacheRequest : public PRCList
@ -73,6 +74,7 @@ private:
if (session->WillDoomEntriesIfExpired()) MarkDoomEntriesIfExpired();
if (blockingMode == nsICache::BLOCKING) MarkBlockingMode();
MarkWaitingForValidation();
NS_IF_ADDREF(mListener);
}
~nsCacheRequest()
@ -82,6 +84,9 @@ private:
if (mLock) PR_DestroyLock(mLock);
if (mCondVar) PR_DestroyCondVar(mCondVar);
NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "request still on a list");
if (mListener)
nsCacheService::ReleaseObject_Locked(mListener, mThread);
}
/**
@ -186,7 +191,7 @@ private:
*/
nsCString * mKey;
PRUint32 mInfo;
nsCOMPtr<nsICacheListener> mListener;
nsICacheListener * mListener; // strong ref
nsCOMPtr<nsIThread> mThread;
PRLock * mLock;
PRCondVar * mCondVar;

View File

@ -55,7 +55,6 @@
#include "nsDiskCacheDevice.h"
#endif
#include "nsAutoLock.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
@ -64,6 +63,7 @@
#include "nsDirectoryServiceDefs.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsThreadUtils.h"
#include "nsProxyRelease.h"
#include "nsVoidArray.h"
#include "nsDeleteDir.h"
#include <math.h> // for log()
@ -466,7 +466,7 @@ nsCacheService * nsCacheService::gService = nsnull;
NS_IMPL_THREADSAFE_ISUPPORTS1(nsCacheService, nsICacheService)
nsCacheService::nsCacheService()
: mCacheServiceLock(nsnull),
: mLock(nsnull),
mInitialized(PR_FALSE),
mEnableMemoryDevice(PR_TRUE),
mEnableDiskDevice(PR_TRUE),
@ -488,7 +488,11 @@ nsCacheService::nsCacheService()
PR_INIT_CLIST(&mDoomedEntries);
// allocate service lock
mCacheServiceLock = PR_NewLock();
mLock = PR_NewLock();
#if defined(DEBUG)
mLockedThread = nsnull;
#endif
}
nsCacheService::~nsCacheService()
@ -496,7 +500,7 @@ nsCacheService::~nsCacheService()
if (mInitialized) // Shutdown hasn't been called yet.
(void) Shutdown();
PR_DestroyLock(mCacheServiceLock);
PR_DestroyLock(mLock);
gService = nsnull;
}
@ -508,7 +512,7 @@ nsCacheService::Init()
if (mInitialized)
return NS_ERROR_ALREADY_INITIALIZED;
if (mCacheServiceLock == nsnull)
if (mLock == nsnull)
return NS_ERROR_OUT_OF_MEMORY;
CACHE_LOG_INIT();
@ -534,7 +538,7 @@ nsCacheService::Init()
void
nsCacheService::Shutdown()
{
nsAutoLock lock(mCacheServiceLock);
nsCacheServiceAutoLock lock;
NS_ASSERTION(mInitialized,
"can't shutdown nsCacheService unless it has been initialized.");
@ -640,7 +644,7 @@ nsCacheService::EvictEntriesForClient(const char * clientID,
}
}
nsAutoLock lock(mCacheServiceLock);
nsCacheServiceAutoLock lock;
nsresult rv = NS_OK;
#ifdef NECKO_DISK_CACHE
@ -677,7 +681,7 @@ nsCacheService::IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
PRBool * result)
{
if (gService == nsnull) return NS_ERROR_NOT_AVAILABLE;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
*result = gService->IsStorageEnabledForPolicy_Locked(storagePolicy);
return NS_OK;
@ -707,7 +711,7 @@ NS_IMETHODIMP nsCacheService::VisitEntries(nsICacheVisitor *visitor)
{
NS_ENSURE_ARG_POINTER(visitor);
nsAutoLock lock(mCacheServiceLock);
nsCacheServiceAutoLock lock;
if (!(mEnableDiskDevice || mEnableMemoryDevice))
return NS_ERROR_NOT_AVAILABLE;
@ -839,25 +843,63 @@ nsCacheService::CreateRequest(nsCacheSession * session,
}
class nsCacheListenerEvent : public nsRunnable
{
public:
nsCacheListenerEvent(nsICacheListener *listener,
nsICacheEntryDescriptor *descriptor,
nsCacheAccessMode accessGranted,
nsresult status)
: mListener(listener) // transfers reference
, mDescriptor(descriptor) // transfers reference (may be null)
, mAccessGranted(accessGranted)
, mStatus(status)
{}
NS_IMETHOD Run()
{
mListener->OnCacheEntryAvailable(mDescriptor, mAccessGranted, mStatus);
NS_RELEASE(mListener);
NS_IF_RELEASE(mDescriptor);
return NS_OK;
}
private:
// We explicitly leak mListener or mDescriptor if Run is not called
// because otherwise we cannot guarantee that they are destroyed on
// the right thread.
nsICacheListener *mListener;
nsICacheEntryDescriptor *mDescriptor;
nsCacheAccessMode mAccessGranted;
nsresult mStatus;
};
nsresult
nsCacheService::NotifyListener(nsCacheRequest * request,
nsICacheEntryDescriptor * descriptor,
nsCacheAccessMode accessGranted,
nsresult error)
nsresult status)
{
nsresult rv;
nsCOMPtr<nsICacheListener> listenerProxy;
NS_ASSERTION(request->mThread, "no thread set in async request!");
rv = NS_GetProxyForObject(request->mThread,
NS_GET_IID(nsICacheListener),
request->mListener,
NS_PROXY_ASYNC | NS_PROXY_ALWAYS,
getter_AddRefs(listenerProxy));
if (NS_FAILED(rv)) return rv;
// Swap ownership, and release listener on target thread...
nsICacheListener *listener = request->mListener;
request->mListener = nsnull;
return listenerProxy->OnCacheEntryAvailable(descriptor, accessGranted, error);
nsCOMPtr<nsIRunnable> ev =
new nsCacheListenerEvent(listener, descriptor,
accessGranted, status);
if (!ev) {
// Better to leak listener and descriptor if we fail because we don't
// want to destroy them inside the cache service lock or on potentially
// the wrong thread.
return NS_ERROR_OUT_OF_MEMORY;
}
return request->mThread->Dispatch(ev, NS_DISPATCH_NORMAL);
}
@ -866,7 +908,7 @@ nsCacheService::ProcessRequest(nsCacheRequest * request,
PRBool calledFromOpenCacheEntry,
nsICacheEntryDescriptor ** result)
{
// !!! must be called with mCacheServiceLock held !!!
// !!! must be called with mLock held !!!
nsresult rv;
nsCacheEntry * entry = nsnull;
nsCacheAccessMode accessGranted = nsICache::ACCESS_NONE;
@ -886,9 +928,10 @@ nsCacheService::ProcessRequest(nsCacheRequest * request,
return rv;
if (request->IsBlocking()) {
PR_Unlock(mCacheServiceLock);
// XXX this is probably wrong...
Unlock();
rv = request->WaitForValidation();
PR_Lock(mCacheServiceLock);
Lock();
}
PR_REMOVE_AND_INIT_LINK(request);
@ -904,10 +947,10 @@ nsCacheService::ProcessRequest(nsCacheRequest * request,
// loop back around to look for another entry
}
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
nsICacheEntryDescriptor *descriptor = nsnull;
if (NS_SUCCEEDED(rv))
rv = entry->CreateDescriptor(request, accessGranted, getter_AddRefs(descriptor));
rv = entry->CreateDescriptor(request, accessGranted, &descriptor);
if (request->mListener) { // Asynchronous
@ -920,7 +963,7 @@ nsCacheService::ProcessRequest(nsCacheRequest * request,
rv = rv2; // trigger delete request
}
} else { // Synchronous
NS_IF_ADDREF(*result = descriptor);
*result = descriptor;
}
return rv;
}
@ -940,7 +983,7 @@ nsCacheService::OpenCacheEntry(nsCacheSession * session,
nsCacheRequest * request = nsnull;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
nsresult rv = gService->CreateRequest(session,
key,
accessRequested,
@ -1158,7 +1201,7 @@ void
nsCacheService::OnProfileShutdown(PRBool cleanse)
{
if (!gService) return;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
gService->DoomActiveEntries();
gService->ClearDoomList();
@ -1186,7 +1229,7 @@ nsCacheService::OnProfileChanged()
{
if (!gService) return;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
gService->mEnableDiskDevice = gService->mObserver->DiskCacheEnabled();
gService->mEnableMemoryDevice = gService->mObserver->MemoryCacheEnabled();
@ -1224,7 +1267,7 @@ void
nsCacheService::SetDiskCacheEnabled(PRBool enabled)
{
if (!gService) return;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
gService->mEnableDiskDevice = enabled;
}
@ -1233,7 +1276,7 @@ void
nsCacheService::SetDiskCacheCapacity(PRInt32 capacity)
{
if (!gService) return;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
#ifdef NECKO_DISK_CACHE
if (gService->mDiskDevice) {
@ -1249,7 +1292,7 @@ void
nsCacheService::SetMemoryCache()
{
if (!gService) return;
nsAutoLock lock(gService->mCacheServiceLock);
nsCacheServiceAutoLock lock;
gService->mEnableMemoryDevice = gService->mObserver->MemoryCacheEnabled();
@ -1338,19 +1381,53 @@ nsCacheService::OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize)
return device->OnDataSizeChange(entry, deltaSize);
}
PRLock *
nsCacheService::ServiceLock()
void
nsCacheService::Lock()
{
NS_ASSERTION(gService, "nsCacheService::gService is null.");
return gService->mCacheServiceLock;
PR_Lock(gService->mLock);
#if defined(DEBUG)
gService->mLockedThread = PR_GetCurrentThread();
#endif
}
void
nsCacheService::Unlock()
{
NS_ASSERTION(gService->mLockedThread == PR_GetCurrentThread(), "oops");
nsTArray<nsISupports*> doomed;
doomed.SwapElements(gService->mDoomedObjects);
#if defined(DEBUG)
gService->mLockedThread = nsnull;
#endif
PR_Unlock(gService->mLock);
for (PRUint32 i = 0; i < doomed.Length(); ++i) {
printf(">>>>>> releasing [%p]\n", (void*) doomed[i]);
doomed[i]->Release();
}
}
void
nsCacheService::ReleaseObject_Locked(nsISupports * obj,
nsIEventTarget * target)
{
NS_ASSERTION(gService->mLockedThread == PR_GetCurrentThread(), "oops");
PRBool isCur;
if (!target || NS_SUCCEEDED(target->IsOnCurrentThread(&isCur)) && isCur) {
gService->mDoomedObjects.AppendElement(obj);
} else {
NS_ProxyRelease(target, obj);
}
}
nsresult
nsCacheService::SetCacheElement(nsCacheEntry * entry, nsISupports * element)
{
entry->SetThread();
entry->SetData(element);
entry->TouchData();
return NS_OK;
@ -1489,10 +1566,10 @@ nsCacheService::ProcessPendingRequests(nsCacheEntry * entry)
// XXX if (newWriter) NS_ASSERTION( accessGranted == request->AccessRequested(), "why not?");
// entry->CreateDescriptor dequeues request, and queues descriptor
nsCOMPtr<nsICacheEntryDescriptor> descriptor;
nsICacheEntryDescriptor *descriptor = nsnull;
rv = entry->CreateDescriptor(request,
accessGranted,
getter_AddRefs(descriptor));
&descriptor);
// post call to listener to report error or descriptor
rv = NotifyListener(request, descriptor, accessGranted, rv);

View File

@ -49,15 +49,18 @@
#include "nsCacheDevice.h"
#include "nsCacheEntry.h"
#include "nspr.h"
#include "prlock.h"
#include "prthread.h"
#include "nsIObserver.h"
#include "nsString.h"
#include "nsProxiedService.h"
#include "nsTArray.h"
class nsCacheRequest;
class nsCacheProfilePrefObserver;
class nsDiskCacheDevice;
class nsMemoryCacheDevice;
class nsCacheServiceAutoLock;
/******************************************************************************
@ -114,8 +117,6 @@ public:
static nsresult OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
static PRLock * ServiceLock();
static nsresult SetCacheElement(nsCacheEntry * entry, nsISupports * element);
static nsresult ValidateEntry(nsCacheEntry * entry);
@ -132,6 +133,14 @@ public:
static PRBool IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy policy);
// This method may be called to release an object while the cache service
// lock is being held. If a non-null target is specified and the target
// does not correspond to the current thread, then the release will be
// proxied to the specified target. Otherwise, the object will be added to
// the list of objects to be released when the cache service is unlocked.
static void ReleaseObject_Locked(nsISupports * object,
nsIEventTarget * target = nsnull);
/**
* Methods called by nsCacheProfilePrefObserver
*/
@ -146,11 +155,15 @@ public:
nsresult Init();
void Shutdown();
private:
friend class nsCacheServiceAutoLock;
/**
* Internal Methods
*/
static void Lock();
static void Unlock();
nsresult CreateDiskDevice();
nsresult CreateMemoryDevice();
@ -166,6 +179,9 @@ private:
nsresult EvictEntriesForClient(const char * clientID,
nsCacheStoragePolicy storagePolicy);
// Notifies request listener asynchronously on the request's thread, and
// releases the descriptor on the request's thread. If this method fails,
// the descriptor is not released.
nsresult NotifyListener(nsCacheRequest * request,
nsICacheEntryDescriptor * descriptor,
nsCacheAccessMode accessGranted,
@ -212,7 +228,13 @@ private:
nsCacheProfilePrefObserver * mObserver;
PRLock * mCacheServiceLock;
PRLock * mLock;
#if defined(DEBUG)
PRThread * mLockedThread; // The thread holding mLock
#endif
nsTArray<nsISupports*> mDoomedObjects;
PRBool mInitialized;
@ -239,5 +261,20 @@ private:
PRUint32 mDeactivatedUnboundEntries;
};
/******************************************************************************
* nsCacheServiceAutoLock
******************************************************************************/
// Instantiate this class to acquire the cache service lock for a particular
// execution scope.
class nsCacheServiceAutoLock {
public:
nsCacheServiceAutoLock() {
nsCacheService::Lock();
}
~nsCacheServiceAutoLock() {
nsCacheService::Unlock();
}
};
#endif // _nsCacheService_h_

View File

@ -76,7 +76,7 @@ public:
public:
nsCacheEntry* mCacheEntry; // back pointer to parent nsCacheEntry
nsDiskCacheRecord mRecord;
nsDiskCacheStreamIO* mStreamIO;
nsDiskCacheStreamIO* mStreamIO; // strong reference
PRBool mDoomed; // record is not stored in cache map
PRUint8 mGeneration; // possibly just reservation
};

View File

@ -44,8 +44,6 @@
#include "nsDiskCacheStreams.h"
#include "nsCacheService.h"
#include "nsAutoLock.h"
// Assumptions:
@ -443,7 +441,7 @@ nsDiskCacheStreamIO::ClearBinding()
nsresult
nsDiskCacheStreamIO::CloseOutputStream(nsDiskCacheOutputStream * outputStream)
{
nsAutoLock lock(nsCacheService::ServiceLock()); // grab service lock
nsCacheServiceAutoLock lock; // grab service lock
nsresult rv;
if (outputStream != mOutStream) {
@ -560,7 +558,7 @@ nsDiskCacheStreamIO::Write( const char * buffer,
PRUint32 * bytesWritten)
{
nsresult rv = NS_OK;
nsAutoLock lock(nsCacheService::ServiceLock()); // grab service lock
nsCacheServiceAutoLock lock; // grab service lock
if (!mBinding) return NS_ERROR_NOT_AVAILABLE;
if (mInStreamCount) {

View File

@ -99,7 +99,7 @@ private:
nsresult Flush();
nsDiskCacheBinding * mBinding;
nsDiskCacheBinding * mBinding; // not an owning reference
nsDiskCacheDevice * mDevice;
nsDiskCacheOutputStream * mOutStream; // not an owning reference
PRInt32 mInStreamCount;

View File

@ -500,6 +500,14 @@ class nsTArray : public nsTArray_base {
return RemoveElement(item, nsDefaultComparator<elem_type, Item>());
}
// This method causes the elements contained in this array and the given
// array to be swapped.
void SwapElements(self_type& other) {
Header *h = other.mHdr;
other.mHdr = mHdr;
mHdr = h;
}
//
// Allocation
//