--- a/dom/media/FileBlockCache.cpp
+++ b/dom/media/FileBlockCache.cpp
@@ -26,21 +26,21 @@ FileBlockCache::SetCacheFile(PRFileDesc*
LOG("SetFD(aFD=%p) mIsOpen=%d", aFD, mIsOpen);
if (!aFD) {
// Failed to get a temporary file. Shutdown.
Close();
return;
}
{
- MonitorAutoLock lock(mFileMonitor);
+ MutexAutoLock lock(mFileMutex);
mFD = aFD;
}
{
- MonitorAutoLock lock(mDataMonitor);
+ MutexAutoLock lock(mDataMutex);
if (!mIsOpen) {
// We've been closed while waiting for the file descriptor. Bail out.
// Rely on the destructor to close the file descriptor.
return;
}
mInitialized = true;
if (mIsWriteScheduled) {
// A write was scheduled while waiting for FD. We need to run/dispatch a
@@ -50,17 +50,17 @@ FileBlockCache::SetCacheFile(PRFileDesc*
}
}
nsresult
FileBlockCache::Init()
{
LOG("Init()");
- MonitorAutoLock mon(mDataMonitor);
+ MutexAutoLock mon(mDataMutex);
nsresult rv = NS_NewNamedThread("FileBlockCache",
getter_AddRefs(mThread),
nullptr,
SharedThreadPool::kStackSize);
if (NS_FAILED(rv)) {
return rv;
}
mIsOpen = true;
@@ -86,50 +86,50 @@ FileBlockCache::Init()
if (NS_FAILED(rv)) {
Close();
}
return rv;
}
FileBlockCache::FileBlockCache()
- : mFileMonitor("MediaCache.Writer.IO.Monitor"),
+ : mFileMutex("MediaCache.Writer.IO.Mutex"),
mFD(nullptr),
mFDCurrentPos(0),
- mDataMonitor("MediaCache.Writer.Data.Monitor"),
+ mDataMutex("MediaCache.Writer.Data.Mutex"),
mIsWriteScheduled(false),
mIsOpen(false)
{
}
FileBlockCache::~FileBlockCache()
{
NS_ASSERTION(!mIsOpen, "Should Close() FileBlockCache before destroying");
}
void FileBlockCache::Close()
{
LOG("Close()");
nsCOMPtr<nsIThread> thread;
{
- MonitorAutoLock mon(mDataMonitor);
+ MutexAutoLock mon(mDataMutex);
if (!mIsOpen) {
return;
}
mIsOpen = false;
if (!mThread) {
return;
}
thread.swap(mThread);
}
PRFileDesc* fd;
{
- MonitorAutoLock lock(mFileMonitor);
+ MutexAutoLock lock(mFileMutex);
fd = mFD;
mFD = nullptr;
}
// Let the thread close the FD, and then trigger its own shutdown.
// Note that mThread is now empty, so no other task will be posted there.
// Also mThread and mFD are empty and therefore can be reused immediately.
nsresult rv = thread->Dispatch(NS_NewRunnableFunction([thread, fd] {
@@ -159,17 +159,17 @@ ContainerContains(const Container& aCont
return std::find(aContainer.begin(), aContainer.end(), value)
!= aContainer.end();
}
nsresult
FileBlockCache::WriteBlock(uint32_t aBlockIndex,
Span<const uint8_t> aData1, Span<const uint8_t> aData2)
{
- MonitorAutoLock mon(mDataMonitor);
+ MutexAutoLock mon(mDataMutex);
if (!mIsOpen)
return NS_ERROR_FAILURE;
// Check if we've already got a pending write scheduled for this block.
mBlockChanges.EnsureLengthAtLeast(aBlockIndex + 1);
bool blockAlreadyHadPendingChange = mBlockChanges[aBlockIndex] != nullptr;
mBlockChanges[aBlockIndex] = new BlockChange(aData1, aData2);
@@ -187,34 +187,34 @@ FileBlockCache::WriteBlock(uint32_t aBlo
EnsureWriteScheduled();
return NS_OK;
}
void FileBlockCache::EnsureWriteScheduled()
{
- mDataMonitor.AssertCurrentThreadOwns();
+ mDataMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mIsOpen);
if (mIsWriteScheduled) {
return;
}
mIsWriteScheduled = true;
if (!mInitialized) {
// We're still waiting on a file descriptor. When it arrives,
// the write will be scheduled.
return;
}
mThread->Dispatch(this, NS_DISPATCH_NORMAL);
}
nsresult FileBlockCache::Seek(int64_t aOffset)
{
- mFileMonitor.AssertCurrentThreadOwns();
+ mFileMutex.AssertCurrentThreadOwns();
if (mFDCurrentPos != aOffset) {
MOZ_ASSERT(mFD);
int64_t result = PR_Seek64(mFD, aOffset, PR_SEEK_SET);
if (result != aOffset) {
NS_WARNING("Failed to seek media cache file");
return NS_ERROR_FAILURE;
}
@@ -224,17 +224,17 @@ nsresult FileBlockCache::Seek(int64_t aO
}
nsresult FileBlockCache::ReadFromFile(int64_t aOffset,
uint8_t* aDest,
int32_t aBytesToRead,
int32_t& aBytesRead)
{
LOG("ReadFromFile(offset=%" PRIu64 ", len=%u)", aOffset, aBytesToRead);
- mFileMonitor.AssertCurrentThreadOwns();
+ mFileMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mFD);
nsresult res = Seek(aOffset);
if (NS_FAILED(res)) return res;
aBytesRead = PR_Read(mFD, aDest, aBytesToRead);
if (aBytesRead <= 0)
return NS_ERROR_FAILURE;
@@ -243,17 +243,17 @@ nsresult FileBlockCache::ReadFromFile(in
return NS_OK;
}
nsresult FileBlockCache::WriteBlockToFile(int32_t aBlockIndex,
const uint8_t* aBlockData)
{
LOG("WriteBlockToFile(index=%u)", aBlockIndex);
- mFileMonitor.AssertCurrentThreadOwns();
+ mFileMutex.AssertCurrentThreadOwns();
MOZ_ASSERT(mFD);
nsresult rv = Seek(BlockIndexToOffset(aBlockIndex));
if (NS_FAILED(rv)) return rv;
int32_t amount = PR_Write(mFD, aBlockData, BLOCK_SIZE);
if (amount < BLOCK_SIZE) {
NS_WARNING("Failed to write media cache block!");
@@ -264,91 +264,91 @@ nsresult FileBlockCache::WriteBlockToFil
return NS_OK;
}
nsresult FileBlockCache::MoveBlockInFile(int32_t aSourceBlockIndex,
int32_t aDestBlockIndex)
{
LOG("MoveBlockInFile(src=%u, dest=%u)", aSourceBlockIndex, aDestBlockIndex);
- mFileMonitor.AssertCurrentThreadOwns();
+ mFileMutex.AssertCurrentThreadOwns();
uint8_t buf[BLOCK_SIZE];
int32_t bytesRead = 0;
if (NS_FAILED(ReadFromFile(BlockIndexToOffset(aSourceBlockIndex),
buf,
BLOCK_SIZE,
bytesRead))) {
return NS_ERROR_FAILURE;
}
return WriteBlockToFile(aDestBlockIndex, buf);
}
nsresult FileBlockCache::Run()
{
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(mIsWriteScheduled, "Should report write running or scheduled.");
MOZ_ASSERT(mFD);
LOG("Run() mFD=%p mIsOpen=%d", mFD, mIsOpen);
while (!mChangeIndexList.empty()) {
if (!mIsOpen) {
// We've been closed, abort, discarding unwritten changes.
mIsWriteScheduled = false;
return NS_ERROR_FAILURE;
}
// Process each pending change. We pop the index out of 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
- // 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.
// This also ensures we will insert a new index into mChangeIndexList
// when this happens.
// Hold a reference to the change, in case another change
// overwrites the mBlockChanges entry for this block while we drop
- // mDataMonitor to take mFileMonitor.
+ // mDataMutex to take mFileMutex.
int32_t blockIndex = mChangeIndexList.front();
mChangeIndexList.pop_front();
RefPtr<BlockChange> change = mBlockChanges[blockIndex];
MOZ_ASSERT(change,
"Change index list should only contain entries for blocks "
"with changes");
{
- MonitorAutoUnlock unlock(mDataMonitor);
- MonitorAutoLock lock(mFileMonitor);
+ MutexAutoUnlock unlock(mDataMutex);
+ MutexAutoLock lock(mFileMutex);
if (change->IsWrite()) {
WriteBlockToFile(blockIndex, change->mData.get());
} else if (change->IsMove()) {
MoveBlockInFile(change->mSourceBlockIndex, blockIndex);
}
}
// 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.
if (mBlockChanges[blockIndex] == change) {
mBlockChanges[blockIndex] = nullptr;
}
}
mIsWriteScheduled = false;
return NS_OK;
}
nsresult FileBlockCache::Read(int64_t aOffset,
uint8_t* aData,
int32_t aLength,
int32_t* aBytes)
{
- MonitorAutoLock mon(mDataMonitor);
+ MutexAutoLock mon(mDataMutex);
if (!mIsOpen || (aOffset / BLOCK_SIZE) > INT32_MAX)
return NS_ERROR_FAILURE;
int32_t bytesToRead = aLength;
int64_t offset = aOffset;
uint8_t* dst = aData;
while (bytesToRead > 0) {
@@ -375,18 +375,18 @@ nsresult FileBlockCache::Read(int64_t aO
// which happened *after* this move was recorded.
blockIndex = mBlockChanges[blockIndex]->mSourceBlockIndex;
}
// Block has been written to file, either as the source block of a move,
// or as a stable (all changes made) block. Read the data directly
// from file.
nsresult res;
{
- MonitorAutoUnlock unlock(mDataMonitor);
- MonitorAutoLock lock(mFileMonitor);
+ MutexAutoUnlock unlock(mDataMutex);
+ MutexAutoLock lock(mFileMutex);
res = ReadFromFile(BlockIndexToOffset(blockIndex) + start,
dst,
amount,
bytesRead);
}
NS_ENSURE_SUCCESS(res,res);
}
dst += bytesRead;
@@ -394,17 +394,17 @@ nsresult FileBlockCache::Read(int64_t aO
bytesToRead -= bytesRead;
}
*aBytes = aLength - bytesToRead;
return NS_OK;
}
nsresult FileBlockCache::MoveBlock(int32_t aSourceBlockIndex, int32_t aDestBlockIndex)
{
- MonitorAutoLock mon(mDataMonitor);
+ MutexAutoLock mon(mDataMutex);
if (!mIsOpen)
return NS_ERROR_FAILURE;
mBlockChanges.EnsureLengthAtLeast(std::max(aSourceBlockIndex, aDestBlockIndex) + 1);
// The source block's contents may be the destination of another pending
// move, which in turn can be the destination of another pending move,
--- a/dom/media/FileBlockCache.h
+++ b/dom/media/FileBlockCache.h
@@ -3,18 +3,18 @@
/* 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 FILE_BLOCK_CACHE_H_
#define FILE_BLOCK_CACHE_H_
#include "mozilla/Attributes.h"
-#include "mozilla/Monitor.h"
#include "mozilla/MozPromise.h"
+#include "mozilla/Mutex.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/AbstractThread.h"
#include "nsTArray.h"
#include "MediaCache.h"
#include "nsDeque.h"
#include "nsThreadUtils.h"
#include <deque>
@@ -138,20 +138,20 @@ public:
private:
int64_t BlockIndexToOffset(int32_t aBlockIndex) {
return static_cast<int64_t>(aBlockIndex) * BLOCK_SIZE;
}
void SetCacheFile(PRFileDesc* aFD);
- // Monitor which controls access to mFD and mFDCurrentPos. Don't hold
- // mDataMonitor while holding mFileMonitor! mFileMonitor must be owned
+ // Mutex which controls access to mFD and mFDCurrentPos. Don't hold
+ // mDataMutex while holding mFileMutex! mFileMutex must be owned
// while accessing any of the following data fields or methods.
- Monitor mFileMonitor;
+ Mutex mFileMutex;
// Moves a block already committed to file.
nsresult MoveBlockInFile(int32_t aSourceBlockIndex,
int32_t aDestBlockIndex);
// Seeks file pointer.
nsresult Seek(int64_t aOffset);
// Reads data from file offset.
nsresult ReadFromFile(int64_t aOffset,
uint8_t* aDest,
@@ -159,24 +159,24 @@ private:
int32_t& aBytesRead);
nsresult WriteBlockToFile(int32_t aBlockIndex, const uint8_t* aBlockData);
// File descriptor we're writing to. This is created externally, but
// shutdown by us.
PRFileDesc* mFD;
// The current file offset in the file.
int64_t mFDCurrentPos;
- // Monitor which controls access to all data in this class, except mFD
- // and mFDCurrentPos. Don't hold mDataMonitor while holding mFileMonitor!
- // mDataMonitor must be owned while accessing any of the following data
+ // Mutex which controls access to all data in this class, except mFD
+ // and mFDCurrentPos. Don't hold mDataMutex while holding mFileMutex!
+ // mDataMutex must be owned while accessing any of the following data
// fields or methods.
- Monitor mDataMonitor;
+ Mutex mDataMutex;
// Ensures we either are running the event to preform IO, or an event
// has been dispatched to preform the IO.
- // mDataMonitor must be owned while calling this.
+ // mDataMutex must be owned while calling this.
void EnsureWriteScheduled();
// Array of block changes to made. If mBlockChanges[offset/BLOCK_SIZE] == nullptr,
// then the block has no pending changes to be written, but if
// mBlockChanges[offset/BLOCK_SIZE] != nullptr, then either there's a block
// cached in memory waiting to be written, or this block is the target of a
// block move.
nsTArray< RefPtr<BlockChange> > mBlockChanges;
@@ -187,16 +187,16 @@ private:
// Queue of pending block indexes that need to be written or moved.
std::deque<int32_t> mChangeIndexList;
// True if we've dispatched an event to commit all pending block changes
// to file on mThread.
bool mIsWriteScheduled;
// True if the writer is ready to enqueue writes.
bool mIsOpen;
// 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
- // decisions about whether we can write while holding mDataMonitor.
+ // directly as that's synchronized via mFileMutex and we need to make
+ // decisions about whether we can write while holding mDataMutex.
bool mInitialized = false;
};
} // End namespace mozilla.
#endif /* FILE_BLOCK_CACHE_H_ */