зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1365538 - Use Mutex instead of Monitor in FileBlockCache - r=cpearce
Since both FileBlockCache Monitor's are only used as locks, we should just use simple Mutex'es (smaller, faster, and tighter API). MozReview-Commit-ID: DdNNPN1kFrs --HG-- extra : rebase_source : 5216929b182d08544b3077374955dfb4ac5b7b16
This commit is contained in:
Родитель
2ed2e93dc1
Коммит
97557fcddf
|
@ -31,11 +31,11 @@ FileBlockCache::SetCacheFile(PRFileDesc* aFD)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
MonitorAutoLock lock(mFileMonitor);
|
MutexAutoLock lock(mFileMutex);
|
||||||
mFD = aFD;
|
mFD = aFD;
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
MonitorAutoLock lock(mDataMonitor);
|
MutexAutoLock lock(mDataMutex);
|
||||||
if (!mIsOpen) {
|
if (!mIsOpen) {
|
||||||
// We've been closed while waiting for the file descriptor. Bail out.
|
// We've been closed while waiting for the file descriptor. Bail out.
|
||||||
// Rely on the destructor to close the file descriptor.
|
// Rely on the destructor to close the file descriptor.
|
||||||
|
@ -55,7 +55,7 @@ FileBlockCache::Init()
|
||||||
{
|
{
|
||||||
LOG("Init()");
|
LOG("Init()");
|
||||||
|
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
nsresult rv = NS_NewNamedThread("FileBlockCache",
|
nsresult rv = NS_NewNamedThread("FileBlockCache",
|
||||||
getter_AddRefs(mThread),
|
getter_AddRefs(mThread),
|
||||||
nullptr,
|
nullptr,
|
||||||
|
@ -91,10 +91,10 @@ FileBlockCache::Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
FileBlockCache::FileBlockCache()
|
FileBlockCache::FileBlockCache()
|
||||||
: mFileMonitor("MediaCache.Writer.IO.Monitor"),
|
: mFileMutex("MediaCache.Writer.IO.Mutex"),
|
||||||
mFD(nullptr),
|
mFD(nullptr),
|
||||||
mFDCurrentPos(0),
|
mFDCurrentPos(0),
|
||||||
mDataMonitor("MediaCache.Writer.Data.Monitor"),
|
mDataMutex("MediaCache.Writer.Data.Mutex"),
|
||||||
mIsWriteScheduled(false),
|
mIsWriteScheduled(false),
|
||||||
mIsOpen(false)
|
mIsOpen(false)
|
||||||
{
|
{
|
||||||
|
@ -111,7 +111,7 @@ void FileBlockCache::Close()
|
||||||
|
|
||||||
nsCOMPtr<nsIThread> thread;
|
nsCOMPtr<nsIThread> thread;
|
||||||
{
|
{
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
if (!mIsOpen) {
|
if (!mIsOpen) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ void FileBlockCache::Close()
|
||||||
|
|
||||||
PRFileDesc* fd;
|
PRFileDesc* fd;
|
||||||
{
|
{
|
||||||
MonitorAutoLock lock(mFileMonitor);
|
MutexAutoLock lock(mFileMutex);
|
||||||
fd = mFD;
|
fd = mFD;
|
||||||
mFD = nullptr;
|
mFD = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ nsresult
|
||||||
FileBlockCache::WriteBlock(uint32_t aBlockIndex,
|
FileBlockCache::WriteBlock(uint32_t aBlockIndex,
|
||||||
Span<const uint8_t> aData1, Span<const uint8_t> aData2)
|
Span<const uint8_t> aData1, Span<const uint8_t> aData2)
|
||||||
{
|
{
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
|
|
||||||
if (!mIsOpen)
|
if (!mIsOpen)
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
@ -192,7 +192,7 @@ FileBlockCache::WriteBlock(uint32_t aBlockIndex,
|
||||||
|
|
||||||
void FileBlockCache::EnsureWriteScheduled()
|
void FileBlockCache::EnsureWriteScheduled()
|
||||||
{
|
{
|
||||||
mDataMonitor.AssertCurrentThreadOwns();
|
mDataMutex.AssertCurrentThreadOwns();
|
||||||
MOZ_ASSERT(mIsOpen);
|
MOZ_ASSERT(mIsOpen);
|
||||||
|
|
||||||
if (mIsWriteScheduled) {
|
if (mIsWriteScheduled) {
|
||||||
|
@ -209,7 +209,7 @@ void FileBlockCache::EnsureWriteScheduled()
|
||||||
|
|
||||||
nsresult FileBlockCache::Seek(int64_t aOffset)
|
nsresult FileBlockCache::Seek(int64_t aOffset)
|
||||||
{
|
{
|
||||||
mFileMonitor.AssertCurrentThreadOwns();
|
mFileMutex.AssertCurrentThreadOwns();
|
||||||
|
|
||||||
if (mFDCurrentPos != aOffset) {
|
if (mFDCurrentPos != aOffset) {
|
||||||
MOZ_ASSERT(mFD);
|
MOZ_ASSERT(mFD);
|
||||||
|
@ -229,7 +229,7 @@ nsresult FileBlockCache::ReadFromFile(int64_t aOffset,
|
||||||
int32_t& aBytesRead)
|
int32_t& aBytesRead)
|
||||||
{
|
{
|
||||||
LOG("ReadFromFile(offset=%" PRIu64 ", len=%u)", aOffset, aBytesToRead);
|
LOG("ReadFromFile(offset=%" PRIu64 ", len=%u)", aOffset, aBytesToRead);
|
||||||
mFileMonitor.AssertCurrentThreadOwns();
|
mFileMutex.AssertCurrentThreadOwns();
|
||||||
MOZ_ASSERT(mFD);
|
MOZ_ASSERT(mFD);
|
||||||
|
|
||||||
nsresult res = Seek(aOffset);
|
nsresult res = Seek(aOffset);
|
||||||
|
@ -248,7 +248,7 @@ nsresult FileBlockCache::WriteBlockToFile(int32_t aBlockIndex,
|
||||||
{
|
{
|
||||||
LOG("WriteBlockToFile(index=%u)", aBlockIndex);
|
LOG("WriteBlockToFile(index=%u)", aBlockIndex);
|
||||||
|
|
||||||
mFileMonitor.AssertCurrentThreadOwns();
|
mFileMutex.AssertCurrentThreadOwns();
|
||||||
MOZ_ASSERT(mFD);
|
MOZ_ASSERT(mFD);
|
||||||
|
|
||||||
nsresult rv = Seek(BlockIndexToOffset(aBlockIndex));
|
nsresult rv = Seek(BlockIndexToOffset(aBlockIndex));
|
||||||
|
@ -269,7 +269,7 @@ nsresult FileBlockCache::MoveBlockInFile(int32_t aSourceBlockIndex,
|
||||||
{
|
{
|
||||||
LOG("MoveBlockInFile(src=%u, dest=%u)", aSourceBlockIndex, aDestBlockIndex);
|
LOG("MoveBlockInFile(src=%u, dest=%u)", aSourceBlockIndex, aDestBlockIndex);
|
||||||
|
|
||||||
mFileMonitor.AssertCurrentThreadOwns();
|
mFileMutex.AssertCurrentThreadOwns();
|
||||||
|
|
||||||
uint8_t buf[BLOCK_SIZE];
|
uint8_t buf[BLOCK_SIZE];
|
||||||
int32_t bytesRead = 0;
|
int32_t bytesRead = 0;
|
||||||
|
@ -285,7 +285,7 @@ nsresult FileBlockCache::MoveBlockInFile(int32_t aSourceBlockIndex,
|
||||||
nsresult FileBlockCache::Run()
|
nsresult FileBlockCache::Run()
|
||||||
{
|
{
|
||||||
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
NS_ASSERTION(!mChangeIndexList.empty(), "Only dispatch when there's work to do");
|
NS_ASSERTION(!mChangeIndexList.empty(), "Only dispatch when there's work to do");
|
||||||
NS_ASSERTION(mIsWriteScheduled, "Should report write running or scheduled.");
|
NS_ASSERTION(mIsWriteScheduled, "Should report write running or scheduled.");
|
||||||
MOZ_ASSERT(mFD);
|
MOZ_ASSERT(mFD);
|
||||||
|
@ -302,14 +302,14 @@ nsresult FileBlockCache::Run()
|
||||||
// Process each pending change. We pop the index out of the change
|
// Process each pending change. We pop the index out of the change
|
||||||
// list, but leave the BlockChange in mBlockChanges until the change
|
// list, but leave the BlockChange in mBlockChanges until the change
|
||||||
// is written to file. This is so that any read which happens while
|
// is written to file. This is so that any read which happens while
|
||||||
// we drop mDataMonitor to write will refer to the data's source in
|
// we drop mDataMutex to write will refer to the data's source in
|
||||||
// memory, rather than the not-yet up to date data written to file.
|
// memory, rather than the not-yet up to date data written to file.
|
||||||
// This also ensures we will insert a new index into mChangeIndexList
|
// This also ensures we will insert a new index into mChangeIndexList
|
||||||
// when this happens.
|
// when this happens.
|
||||||
|
|
||||||
// Hold a reference to the change, in case another change
|
// Hold a reference to the change, in case another change
|
||||||
// overwrites the mBlockChanges entry for this block while we drop
|
// overwrites the mBlockChanges entry for this block while we drop
|
||||||
// mDataMonitor to take mFileMonitor.
|
// mDataMutex to take mFileMutex.
|
||||||
int32_t blockIndex = mChangeIndexList.front();
|
int32_t blockIndex = mChangeIndexList.front();
|
||||||
mChangeIndexList.pop_front();
|
mChangeIndexList.pop_front();
|
||||||
RefPtr<BlockChange> change = mBlockChanges[blockIndex];
|
RefPtr<BlockChange> change = mBlockChanges[blockIndex];
|
||||||
|
@ -317,8 +317,8 @@ nsresult FileBlockCache::Run()
|
||||||
"Change index list should only contain entries for blocks "
|
"Change index list should only contain entries for blocks "
|
||||||
"with changes");
|
"with changes");
|
||||||
{
|
{
|
||||||
MonitorAutoUnlock unlock(mDataMonitor);
|
MutexAutoUnlock unlock(mDataMutex);
|
||||||
MonitorAutoLock lock(mFileMonitor);
|
MutexAutoLock lock(mFileMutex);
|
||||||
if (change->IsWrite()) {
|
if (change->IsWrite()) {
|
||||||
WriteBlockToFile(blockIndex, change->mData.get());
|
WriteBlockToFile(blockIndex, change->mData.get());
|
||||||
} else if (change->IsMove()) {
|
} else if (change->IsMove()) {
|
||||||
|
@ -326,7 +326,7 @@ nsresult FileBlockCache::Run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If a new change has not been made to the block while we dropped
|
// If a new change has not been made to the block while we dropped
|
||||||
// mDataMonitor, clear reference to the old change. Otherwise, the old
|
// mDataMutex, clear reference to the old change. Otherwise, the old
|
||||||
// reference has been cleared already.
|
// reference has been cleared already.
|
||||||
if (mBlockChanges[blockIndex] == change) {
|
if (mBlockChanges[blockIndex] == change) {
|
||||||
mBlockChanges[blockIndex] = nullptr;
|
mBlockChanges[blockIndex] = nullptr;
|
||||||
|
@ -343,7 +343,7 @@ nsresult FileBlockCache::Read(int64_t aOffset,
|
||||||
int32_t aLength,
|
int32_t aLength,
|
||||||
int32_t* aBytes)
|
int32_t* aBytes)
|
||||||
{
|
{
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
|
|
||||||
if (!mIsOpen || (aOffset / BLOCK_SIZE) > INT32_MAX)
|
if (!mIsOpen || (aOffset / BLOCK_SIZE) > INT32_MAX)
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
@ -380,8 +380,8 @@ nsresult FileBlockCache::Read(int64_t aOffset,
|
||||||
// from file.
|
// from file.
|
||||||
nsresult res;
|
nsresult res;
|
||||||
{
|
{
|
||||||
MonitorAutoUnlock unlock(mDataMonitor);
|
MutexAutoUnlock unlock(mDataMutex);
|
||||||
MonitorAutoLock lock(mFileMonitor);
|
MutexAutoLock lock(mFileMutex);
|
||||||
res = ReadFromFile(BlockIndexToOffset(blockIndex) + start,
|
res = ReadFromFile(BlockIndexToOffset(blockIndex) + start,
|
||||||
dst,
|
dst,
|
||||||
amount,
|
amount,
|
||||||
|
@ -399,7 +399,7 @@ nsresult FileBlockCache::Read(int64_t aOffset,
|
||||||
|
|
||||||
nsresult FileBlockCache::MoveBlock(int32_t aSourceBlockIndex, int32_t aDestBlockIndex)
|
nsresult FileBlockCache::MoveBlock(int32_t aSourceBlockIndex, int32_t aDestBlockIndex)
|
||||||
{
|
{
|
||||||
MonitorAutoLock mon(mDataMonitor);
|
MutexAutoLock mon(mDataMutex);
|
||||||
|
|
||||||
if (!mIsOpen)
|
if (!mIsOpen)
|
||||||
return NS_ERROR_FAILURE;
|
return NS_ERROR_FAILURE;
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
#define FILE_BLOCK_CACHE_H_
|
#define FILE_BLOCK_CACHE_H_
|
||||||
|
|
||||||
#include "mozilla/Attributes.h"
|
#include "mozilla/Attributes.h"
|
||||||
#include "mozilla/Monitor.h"
|
|
||||||
#include "mozilla/MozPromise.h"
|
#include "mozilla/MozPromise.h"
|
||||||
|
#include "mozilla/Mutex.h"
|
||||||
#include "mozilla/UniquePtr.h"
|
#include "mozilla/UniquePtr.h"
|
||||||
#include "mozilla/AbstractThread.h"
|
#include "mozilla/AbstractThread.h"
|
||||||
#include "nsTArray.h"
|
#include "nsTArray.h"
|
||||||
|
@ -143,10 +143,10 @@ private:
|
||||||
|
|
||||||
void SetCacheFile(PRFileDesc* aFD);
|
void SetCacheFile(PRFileDesc* aFD);
|
||||||
|
|
||||||
// Monitor which controls access to mFD and mFDCurrentPos. Don't hold
|
// Mutex which controls access to mFD and mFDCurrentPos. Don't hold
|
||||||
// mDataMonitor while holding mFileMonitor! mFileMonitor must be owned
|
// mDataMutex while holding mFileMutex! mFileMutex must be owned
|
||||||
// while accessing any of the following data fields or methods.
|
// while accessing any of the following data fields or methods.
|
||||||
Monitor mFileMonitor;
|
Mutex mFileMutex;
|
||||||
// Moves a block already committed to file.
|
// Moves a block already committed to file.
|
||||||
nsresult MoveBlockInFile(int32_t aSourceBlockIndex,
|
nsresult MoveBlockInFile(int32_t aSourceBlockIndex,
|
||||||
int32_t aDestBlockIndex);
|
int32_t aDestBlockIndex);
|
||||||
|
@ -164,14 +164,14 @@ private:
|
||||||
// The current file offset in the file.
|
// The current file offset in the file.
|
||||||
int64_t mFDCurrentPos;
|
int64_t mFDCurrentPos;
|
||||||
|
|
||||||
// Monitor which controls access to all data in this class, except mFD
|
// Mutex which controls access to all data in this class, except mFD
|
||||||
// and mFDCurrentPos. Don't hold mDataMonitor while holding mFileMonitor!
|
// and mFDCurrentPos. Don't hold mDataMutex while holding mFileMutex!
|
||||||
// mDataMonitor must be owned while accessing any of the following data
|
// mDataMutex must be owned while accessing any of the following data
|
||||||
// fields or methods.
|
// fields or methods.
|
||||||
Monitor mDataMonitor;
|
Mutex mDataMutex;
|
||||||
// Ensures we either are running the event to preform IO, or an event
|
// Ensures we either are running the event to preform IO, or an event
|
||||||
// has been dispatched to preform the IO.
|
// has been dispatched to preform the IO.
|
||||||
// mDataMonitor must be owned while calling this.
|
// mDataMutex must be owned while calling this.
|
||||||
void EnsureWriteScheduled();
|
void EnsureWriteScheduled();
|
||||||
|
|
||||||
// Array of block changes to made. If mBlockChanges[offset/BLOCK_SIZE] == nullptr,
|
// Array of block changes to made. If mBlockChanges[offset/BLOCK_SIZE] == nullptr,
|
||||||
|
@ -192,8 +192,8 @@ private:
|
||||||
// True if the writer is ready to enqueue writes.
|
// True if the writer is ready to enqueue writes.
|
||||||
bool mIsOpen;
|
bool mIsOpen;
|
||||||
// True if we've got a temporary file descriptor. Note: we don't use mFD
|
// True if we've got a temporary file descriptor. Note: we don't use mFD
|
||||||
// directly as that's synchronized via mFileMonitor and we need to make
|
// directly as that's synchronized via mFileMutex and we need to make
|
||||||
// decisions about whether we can write while holding mDataMonitor.
|
// decisions about whether we can write while holding mDataMutex.
|
||||||
bool mInitialized = false;
|
bool mInitialized = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче