Bug 832609 - Make memory reporter for blobs threadsafe. r=khuey

--HG--
extra : rebase_source : 4dc2387ae3867699085139545ff2b57104b1f72a
This commit is contained in:
Justin Lebar 2013-05-24 13:10:48 -04:00
parent d959fc3053
commit 46907ebf24
2 changed files with 20 additions and 1 deletions

View File

@ -22,6 +22,7 @@
#include "mozilla/GuardObjects.h"
#include "mozilla/LinkedList.h"
#include "mozilla/StandardInteger.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/DOMError.h"
#include "mozilla/dom/indexedDB/FileInfo.h"
@ -343,6 +344,10 @@ protected:
bool mStoredFile;
};
/**
* This class may be used off the main thread, and in particular, its
* constructor and destructor may not run on the same thread. Be careful!
*/
class nsDOMMemoryFile : public nsDOMFile
{
public:
@ -394,6 +399,8 @@ protected:
: mData(aMemoryBuffer)
, mLength(aLength)
{
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
if (!sDataOwners) {
sDataOwners = new mozilla::LinkedList<DataOwner>();
EnsureMemoryReporterRegistered();
@ -402,6 +409,8 @@ protected:
}
~DataOwner() {
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
remove();
if (sDataOwners->isEmpty()) {
// Free the linked list if it's empty.
@ -413,8 +422,13 @@ protected:
static void EnsureMemoryReporterRegistered();
static bool sMemoryReporterRegistered;
// sDataOwners and sMemoryReporterRegistered may only be accessed while
// holding sDataOwnerMutex! You also must hold the mutex while touching
// elements of the linked list that DataOwner inherits from.
static mozilla::StaticMutex sDataOwnerMutex;
static mozilla::StaticAutoPtr<mozilla::LinkedList<DataOwner> > sDataOwners;
static bool sMemoryReporterRegistered;
void* mData;
uint64_t mLength;
};

View File

@ -611,6 +611,9 @@ nsDOMMemoryFile::GetInternalStream(nsIInputStream **aStream)
return DataOwnerAdapter::Create(mDataOwner, mStart, mLength, aStream);
}
/* static */ StaticMutex
nsDOMMemoryFile::DataOwner::sDataOwnerMutex;
/* static */ StaticAutoPtr<LinkedList<nsDOMMemoryFile::DataOwner> >
nsDOMMemoryFile::DataOwner::sDataOwners;
@ -635,6 +638,8 @@ class nsDOMMemoryFileDataOwnerMemoryReporter MOZ_FINAL
{
typedef nsDOMMemoryFile::DataOwner DataOwner;
StaticMutexAutoLock lock(DataOwner::sDataOwnerMutex);
if (!DataOwner::sDataOwners) {
return NS_OK;
}