Bug 1354389. P3 - fix the callers to handle changes when dropping the monitor. r?cpearce draft
authorJW Wang <jwwang@mozilla.com>
Mon, 10 Apr 2017 13:57:13 +0800
changeset 559515 67957ff6c2ae8eaafe1836d55c8fd7c33c4267e1
parent 559514 ae703bffa62d467e7bb9b2bda2754e8c463d19c1
child 623416 a810880027d00f9c9071b4ebb8c29defb06a34b3
push id53117
push userjwwang@mozilla.com
push dateMon, 10 Apr 2017 08:16:18 +0000
reviewerscpearce
bugs1354389
milestone55.0a1
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
dom/media/MediaCache.cpp
--- 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;