Skip to content

Commit

Permalink
fixed: Don't do GetLength() off-thread is it may be (b)locked by a pe…
Browse files Browse the repository at this point in the history
…nding read() (fixes #16046)
  • Loading branch information
arnova committed Jun 23, 2015
1 parent 5737b0c commit 593f4aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions xbmc/filesystem/FileCache.cpp
Expand Up @@ -104,6 +104,7 @@ CFileCache::CFileCache(bool useDoubleCache)
, m_writeRate(0)
, m_writeRateActual(0)
, m_cacheFull(false)
, m_fileSize(0)
{
m_bDeleteCache = true;
m_nSeekResult = 0;
Expand Down Expand Up @@ -206,6 +207,7 @@ bool CFileCache::Open(const CURL& url)
// check if source can seek
m_seekPossible = m_source.IoControl(IOCTRL_SEEK_POSSIBLE, NULL);
m_chunkSize = CFile::GetChunkSize(m_source.GetChunkSize(), READ_CACHE_CHUNK_SIZE);
m_fileSize = m_source.GetLength();

m_readPos = 0;
m_writePos = 0;
Expand Down Expand Up @@ -242,12 +244,15 @@ void CFileCache::Process()

while (!m_bStop)
{
// Update filesize
m_fileSize = m_source.GetLength();

// check for seek events
if (m_seekEvent.WaitMSec(0))
{
m_seekEvent.Reset();
int64_t cacheMaxPos = m_pCache->CachedDataEndPosIfSeekTo(m_seekPos);
cacheReachEOF = (cacheMaxPos == m_source.GetLength());
cacheReachEOF = (cacheMaxPos == m_fileSize);
bool sourceSeekFailed = false;
if (!cacheReachEOF)
{
Expand Down Expand Up @@ -444,7 +449,7 @@ int64_t CFileCache::Seek(int64_t iFilePosition, int iWhence)
int64_t iCurPos = m_readPos;
int64_t iTarget = iFilePosition;
if (iWhence == SEEK_END)
iTarget = GetLength() + iTarget;
iTarget = m_fileSize + iTarget;
else if (iWhence == SEEK_CUR)
iTarget = iCurPos + iTarget;
else if (iWhence != SEEK_SET)
Expand All @@ -459,7 +464,7 @@ int64_t CFileCache::Seek(int64_t iFilePosition, int iWhence)
return m_nSeekResult;

/* never request closer to end than 2k, speeds up tag reading */
m_seekPos = std::min(iTarget, std::max((int64_t)0, m_source.GetLength() - m_chunkSize));
m_seekPos = std::min(iTarget, std::max((int64_t)0, m_fileSize - m_chunkSize));

m_seekEvent.Set();
if (!m_seekEnded.Wait())
Expand Down Expand Up @@ -506,7 +511,7 @@ int64_t CFileCache::GetPosition()

int64_t CFileCache::GetLength()
{
return m_source.GetLength();
return m_fileSize;
}

void CFileCache::StopThread(bool bWait /*= true*/)
Expand Down
2 changes: 2 additions & 0 deletions xbmc/filesystem/FileCache.h
Expand Up @@ -24,6 +24,7 @@
#include "threads/CriticalSection.h"
#include "File.h"
#include "threads/Thread.h"
#include <atomic>

namespace XFILE
{
Expand Down Expand Up @@ -77,6 +78,7 @@ namespace XFILE
unsigned m_writeRate;
unsigned m_writeRateActual;
bool m_cacheFull;
std::atomic<int64_t> m_fileSize;
CCriticalSection m_sync;
};

Expand Down

0 comments on commit 593f4aa

Please sign in to comment.