Skip to content

Commit

Permalink
Merge branch 'master' into TemporaryHackishBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaMoo committed Dec 10, 2017
2 parents 2ed534f + 27227f8 commit 8ac5fbc
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 66 deletions.
10 changes: 9 additions & 1 deletion Core/FileLoaders/CachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// Takes ownership of backend.
CachingFileLoader::CachingFileLoader(FileLoader *backend)
: filesize_(0), backend_(backend), exists_(-1), isDirectory_(-1), aheadThread_(false) {
: backend_(backend) {
}

void CachingFileLoader::Prepare() {
Expand Down Expand Up @@ -288,3 +288,11 @@ void CachingFileLoader::StartReadAhead(s64 pos) {
});
th.detach();
}

bool CachingFileLoader::IsRemote() {
return backend_->IsRemote();
}

void CachingFileLoader::Cancel() {
backend_->Cancel();
}
11 changes: 7 additions & 4 deletions Core/FileLoaders/CachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CachingFileLoader : public FileLoader {
CachingFileLoader(FileLoader *backend);
~CachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
Expand All @@ -39,6 +40,8 @@ class CachingFileLoader : public FileLoader {
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

private:
void Prepare();
void InitCache();
Expand All @@ -57,10 +60,10 @@ class CachingFileLoader : public FileLoader {
BLOCK_READAHEAD = 4,
};

s64 filesize_;
s64 filesize_ = 0;
FileLoader *backend_;
int exists_;
int isDirectory_;
int exists_ = -1;
int isDirectory_ = -1;
u64 generation_;
u64 oldestGeneration_;
size_t cacheSize_;
Expand All @@ -77,6 +80,6 @@ class CachingFileLoader : public FileLoader {

std::map<s64, BlockInfo> blocks_;
std::recursive_mutex blocksMutex_;
bool aheadThread_;
bool aheadThread_ = false;
std::once_flag preparedFlag_;
};
27 changes: 16 additions & 11 deletions Core/FileLoaders/DiskCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ std::mutex DiskCachingFileLoader::cachesMutex_;

// Takes ownership of backend.
DiskCachingFileLoader::DiskCachingFileLoader(FileLoader *backend)
: prepared_(false), filesize_(0), backend_(backend), cache_(nullptr) {
: backend_(backend) {
}

void DiskCachingFileLoader::Prepare() {
if (prepared_) {
return;
}
prepared_ = true;

filesize_ = backend_->FileSize();
if (filesize_ > 0) {
InitCache();
}
std::call_once(preparedFlag_, [this]() {
filesize_ = backend_->FileSize();
if (filesize_ > 0) {
InitCache();
}
});
}

DiskCachingFileLoader::~DiskCachingFileLoader() {
Expand Down Expand Up @@ -118,6 +115,14 @@ size_t DiskCachingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data,
return readSize;
}

bool DiskCachingFileLoader::IsRemote() {
return backend_->IsRemote();
}

void DiskCachingFileLoader::Cancel() {
backend_->Cancel();
}

std::vector<std::string> DiskCachingFileLoader::GetCachedPathsInUse() {
std::lock_guard<std::mutex> guard(cachesMutex_);

Expand Down Expand Up @@ -156,7 +161,7 @@ void DiskCachingFileLoader::ShutdownCache() {
}

DiskCachingFileLoaderCache::DiskCachingFileLoaderCache(const std::string &path, u64 filesize)
: refCount_(0), filesize_(filesize), origPath_(path), f_(nullptr), fd_(0) {
: filesize_(filesize), origPath_(path) {
InitCache(path);
}

Expand Down
15 changes: 9 additions & 6 deletions Core/FileLoaders/DiskCachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class DiskCachingFileLoader : public FileLoader {
DiskCachingFileLoader(FileLoader *backend);
~DiskCachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
Expand All @@ -43,17 +44,19 @@ class DiskCachingFileLoader : public FileLoader {
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

static std::vector<std::string> GetCachedPathsInUse();

private:
void Prepare();
void InitCache();
void ShutdownCache();

bool prepared_;
s64 filesize_;
std::once_flag preparedFlag_;
s64 filesize_ = 0;
FileLoader *backend_;
DiskCachingFileLoaderCache *cache_;
DiskCachingFileLoaderCache *cache_ = nullptr;

// We don't support concurrent disk cache access (we use memory cached indexes.)
// So we have to ensure there's only one of these per.
Expand Down Expand Up @@ -139,7 +142,7 @@ class DiskCachingFileLoaderCache {
INVALID_INDEX = 0xFFFFFFFF,
};

int refCount_;
int refCount_ = 0;
s64 filesize_;
u32 blockSize_;
u16 generation_;
Expand Down Expand Up @@ -176,8 +179,8 @@ class DiskCachingFileLoaderCache {
std::vector<BlockInfo> index_;
std::vector<u32> blockIndexLookup_;

FILE *f_;
int fd_;
FILE *f_ = nullptr;
int fd_ = 0;

static std::string cacheDir_;
};
17 changes: 16 additions & 1 deletion Core/FileLoaders/HTTPFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "Core/FileLoaders/HTTPFileLoader.h"

HTTPFileLoader::HTTPFileLoader(const std::string &filename)
: filesize_(0), filepos_(0), url_(filename), filename_(filename), connected_(false) {
: url_(filename), filename_(filename) {
}

void HTTPFileLoader::Prepare() {
Expand All @@ -33,6 +33,10 @@ void HTTPFileLoader::Prepare() {
}

Connect();
if (!connected_) {
return;
}

int err = client_.SendRequest("HEAD", url_.Resource().c_str());
if (err < 0) {
Disconnect();
Expand Down Expand Up @@ -123,6 +127,9 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
}

Connect();
if (!connected_) {
return 0;
}

char requestHeaders[4096];
// Note that the Range header is *inclusive*.
Expand Down Expand Up @@ -186,3 +193,11 @@ size_t HTTPFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags f
filepos_ = absolutePos + readBytes;
return readBytes;
}

void HTTPFileLoader::Connect() {
if (!connected_) {
cancelConnect_ = false;
// Latency is important here, so reduce the timeout.
connected_ = client_.Connect(3, 10.0, &cancelConnect_);
}
}
20 changes: 12 additions & 8 deletions Core/FileLoaders/HTTPFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class HTTPFileLoader : public FileLoader {
HTTPFileLoader(const std::string &filename);
virtual ~HTTPFileLoader() override;

bool IsRemote() override {
return true;
}
virtual bool Exists() override;
virtual bool ExistsFast() override;
virtual bool IsDirectory() override;
Expand All @@ -41,14 +44,14 @@ class HTTPFileLoader : public FileLoader {
}
virtual size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override {
cancelConnect_ = true;
}

private:
void Prepare();

void Connect() {
if (!connected_) {
connected_ = client_.Connect();
}
}
void Connect();

void Disconnect() {
if (connected_) {
Expand All @@ -57,12 +60,13 @@ class HTTPFileLoader : public FileLoader {
connected_ = false;
}

s64 filesize_;
s64 filepos_;
s64 filesize_ = 0;
s64 filepos_ = 0;
Url url_;
http::Client client_;
std::string filename_;
bool connected_;
bool connected_ = false;
bool cancelConnect_ = false;

std::once_flag preparedFlag_;
std::mutex readAtMutex_;
Expand Down
24 changes: 17 additions & 7 deletions Core/FileLoaders/RamCachingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

// Takes ownership of backend.
RamCachingFileLoader::RamCachingFileLoader(FileLoader *backend)
: filesize_(0), backend_(backend), exists_(-1), isDirectory_(-1), aheadThread_(false) {
: backend_(backend) {
filesize_ = backend->FileSize();
if (filesize_ > 0) {
InitCache();
Expand Down Expand Up @@ -107,11 +107,7 @@ void RamCachingFileLoader::InitCache() {
}

void RamCachingFileLoader::ShutdownCache() {
{
std::lock_guard<std::mutex> guard(blocksMutex_);
// Try to have the thread stop.
aheadRemaining_ = 0;
}
Cancel();

// We can't delete while the thread is running, so have to wait.
// This should only happen from the menu.
Expand All @@ -127,6 +123,15 @@ void RamCachingFileLoader::ShutdownCache() {
}
}

void RamCachingFileLoader::Cancel() {
if (aheadThread_) {
std::lock_guard<std::mutex> guard(blocksMutex_);
aheadCancel_ = true;
}

backend_->Cancel();
}

size_t RamCachingFileLoader::ReadFromCache(s64 pos, size_t bytes, void *data) {
s64 cacheStartPos = pos >> BLOCK_SHIFT;
s64 cacheEndPos = (pos + bytes - 1) >> BLOCK_SHIFT;
Expand Down Expand Up @@ -220,10 +225,11 @@ void RamCachingFileLoader::StartReadAhead(s64 pos) {
}

aheadThread_ = true;
aheadCancel_ = false;
std::thread th([this] {
setCurrentThreadName("FileLoaderReadAhead");

while (aheadRemaining_ != 0) {
while (aheadRemaining_ != 0 && !aheadCancel_) {
// Where should we look?
const u32 cacheStartPos = NextAheadBlock();
if (cacheStartPos == 0xFFFFFFFF) {
Expand Down Expand Up @@ -264,3 +270,7 @@ u32 RamCachingFileLoader::NextAheadBlock() {

return 0xFFFFFFFF;
}

bool RamCachingFileLoader::IsRemote() {
return backend_->IsRemote();
}
14 changes: 9 additions & 5 deletions Core/FileLoaders/RamCachingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class RamCachingFileLoader : public FileLoader {
RamCachingFileLoader(FileLoader *backend);
~RamCachingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
Expand All @@ -39,6 +40,8 @@ class RamCachingFileLoader : public FileLoader {
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

private:
void InitCache();
void ShutdownCache();
Expand All @@ -55,15 +58,16 @@ class RamCachingFileLoader : public FileLoader {
BLOCK_READAHEAD = 4,
};

s64 filesize_;
s64 filesize_ = 0;
FileLoader *backend_;
u8 *cache_;
int exists_;
int isDirectory_;
u8 *cache_ = nullptr;
int exists_ = -1;
int isDirectory_ = -1;

std::vector<u8> blocks_;
std::mutex blocksMutex_;
u32 aheadRemaining_;
s64 aheadPos_;
bool aheadThread_;
bool aheadThread_ = false;
bool aheadCancel_ = false;
};
8 changes: 8 additions & 0 deletions Core/FileLoaders/RetryingFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ size_t RetryingFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Fla

return readSize;
}

bool RetryingFileLoader::IsRemote() {
return backend_->IsRemote();
}

void RetryingFileLoader::Cancel() {
backend_->Cancel();
}
3 changes: 3 additions & 0 deletions Core/FileLoaders/RetryingFileLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RetryingFileLoader : public FileLoader {
RetryingFileLoader(FileLoader *backend);
~RetryingFileLoader() override;

bool IsRemote() override;
bool Exists() override;
bool ExistsFast() override;
bool IsDirectory() override;
Expand All @@ -36,6 +37,8 @@ class RetryingFileLoader : public FileLoader {
}
size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) override;

void Cancel() override;

private:
enum {
MAX_RETRIES = 3,
Expand Down
7 changes: 7 additions & 0 deletions Core/Loaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class FileLoader {

virtual ~FileLoader() {}

virtual bool IsRemote() {
return false;
}
virtual bool Exists() = 0;
virtual bool ExistsFast() {
return Exists();
Expand All @@ -86,6 +89,10 @@ class FileLoader {
virtual size_t ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags = Flags::NONE) {
return ReadAt(absolutePos, 1, bytes, data, flags);
}

// Cancel any operations that might block, if possible.
virtual void Cancel() {
}
};

inline u32 operator & (const FileLoader::Flags &a, const FileLoader::Flags &b) {
Expand Down
Loading

0 comments on commit 8ac5fbc

Please sign in to comment.