Bug 1354389. P3 - fix the callers to handle changes when dropping the monitor. r?cpearce
No change is needed for MediaCacheStream::Read() which calls |mon.Wait()|
and has handled changes when dropping the monitor.
MozReview-Commit-ID: 40h0cHn1WFx
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -674,18 +674,16 @@ MediaCache::ReadCacheFile(
mReentrantMonitor.AssertCurrentThreadIn();
RefPtr<FileBlockCache> fileCache = mFileCache;
if (!fileCache) {
return NS_ERROR_FAILURE;
}
{
// Since the monitor might be acquired on the main thread, we need to drop
// the monitor while doing IO in order not to block the main thread.
- // TODO: fix calls of ReadCacheFile() to handle state changes happening when
- // the monitor is not held.
ReentrantMonitorAutoExit unlock(mReentrantMonitor);
return fileCache->Read(aOffset,
reinterpret_cast<uint8_t*>(aData), aLength, aBytes);
}
}
static int32_t GetMaxBlocks()
{
@@ -2331,23 +2329,26 @@ MediaCacheStream::ReadAt(int64_t aOffset
if (NS_FAILED(rv)) return rv;
return Read(aBuffer, aCount, aBytes);
}
nsresult
MediaCacheStream::ReadFromCache(char* aBuffer, int64_t aOffset, int64_t aCount)
{
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
- if (mClosed)
- return NS_ERROR_FAILURE;
// Read one block (or part of a block) at a time
uint32_t count = 0;
int64_t streamOffset = aOffset;
while (count < aCount) {
+ if (mClosed) {
+ // We need to check |mClosed| in each iteration which might be changed
+ // after calling |gMediaCache->ReadCacheFile|.
+ return NS_ERROR_FAILURE;
+ }
uint32_t streamBlock = uint32_t(streamOffset/BLOCK_SIZE);
uint32_t offsetInStreamBlock =
uint32_t(streamOffset - streamBlock*BLOCK_SIZE);
int64_t size = std::min<int64_t>(aCount - count, BLOCK_SIZE - offsetInStreamBlock);
if (mStreamLength >= 0) {
// Don't try to read beyond the end of the stream
int64_t bytesRemaining = mStreamLength - streamOffset;