Skip to content

Commit

Permalink
UWP crashfix - serialize accesses to each loader
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Apr 15, 2022
1 parent a31608e commit 93c39f7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 8 additions & 4 deletions UWP/StorageFileLoader.cpp
Expand Up @@ -43,7 +43,6 @@ StorageFileLoader::~StorageFileLoader() {
void StorageFileLoader::threadfunc() {
SetCurrentThreadName("StorageFileLoader");


{
std::unique_lock<std::mutex> lock(initMutex);
_assert_(!active_);
Expand Down Expand Up @@ -85,10 +84,10 @@ void StorageFileLoader::threadfunc() {
if (operationRequested_) {
switch (operation_.type) {
case OpType::READ_AT: {
Streams::Buffer ^buf = ref new Streams::Buffer(operation_.size);
Streams::Buffer ^buf = ref new Streams::Buffer((unsigned int)operation_.size);
operationFailed_ = false;
stream_->Seek(operation_.offset);
auto task = create_task(stream_->ReadAsync(buf, operation_.size, Streams::InputStreamOptions::None));
auto task = create_task(stream_->ReadAsync(buf, (unsigned int)operation_.size, Streams::InputStreamOptions::None));
Streams::IBuffer ^output = nullptr;
try {
task.wait();
Expand All @@ -98,8 +97,8 @@ void StorageFileLoader::threadfunc() {
const char *what = e.what();
INFO_LOG(SYSTEM, "%s", what);
}
operationRequested_ = false;
std::unique_lock<std::mutex> lock(mutexResponse_);
operationRequested_ = false;
response_.buffer = output;
responseAvailable_ = true;
condResponse_.notify_one();
Expand Down Expand Up @@ -142,7 +141,11 @@ void StorageFileLoader::EnsureOpen() {
}

size_t StorageFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data, Flags flags) {
// We can't handle multiple of these at a time, so serialize the easy way.
std::unique_lock<std::mutex> lock(operationMutex_);

EnsureOpen();

_assert_(!operationRequested_);
_assert_(!responseAvailable_)

Expand All @@ -166,6 +169,7 @@ size_t StorageFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, vo
if (operationFailed_) {
return 0;
}

DataReader ^rd = DataReader::FromBuffer(response_.buffer);
size_t len = response_.buffer->Length;
Platform::Array<uint8_t> ^bytearray = ref new Platform::Array<uint8_t>((unsigned int)len);
Expand Down
2 changes: 2 additions & 0 deletions UWP/StorageFileLoader.h
Expand Up @@ -56,6 +56,8 @@ class StorageFileLoader : public FileLoader {
Windows::Storage::Streams::IRandomAccessStreamWithContentType ^stream_;
Path path_;

std::mutex operationMutex_;

bool operationRequested_ = false;
Operation operation_{ OpType::NONE, 0, 0 };
std::condition_variable cond_;
Expand Down

1 comment on commit 93c39f7

@hrydgard
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the idea, thanks for linking!

Please sign in to comment.