2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-09 00:27:42 +00:00

Add ResourceLock for detecting parallel resource collisions

This commit is contained in:
Jack Andersen
2016-08-11 16:33:03 -10:00
parent 48a4a51853
commit 719c62f09f
5 changed files with 60 additions and 1 deletions

View File

@@ -1,4 +1,7 @@
#include "hecl/hecl.hpp"
#include <thread>
#include <mutex>
#include <unordered_map>
#ifdef WIN32
#include <windows.h>
@@ -84,6 +87,38 @@ void SanitizePath(std::wstring& path)
});
}
static std::mutex PathsMutex;
static std::unordered_map<std::thread::id, ProjectPath> PathsInProgress;
bool ResourceLock::InProgress(const ProjectPath& path)
{
std::unique_lock<std::mutex> lk(PathsMutex);
for (const auto& p : PathsInProgress)
if (p.second == path)
return true;
return false;
}
bool ResourceLock::SetThreadRes(const ProjectPath& path)
{
std::unique_lock<std::mutex> lk(PathsMutex);
if (PathsInProgress.find(std::this_thread::get_id()) != PathsInProgress.cend())
LogModule.report(logvisor::Fatal, "multiple resource locks on thread");
for (const auto& p : PathsInProgress)
if (p.second == path)
return false;
PathsInProgress[std::this_thread::get_id()] = path;
return true;
}
void ResourceLock::ClearThreadRes()
{
std::unique_lock<std::mutex> lk(PathsMutex);
PathsInProgress.erase(std::this_thread::get_id());
}
bool IsPathPNG(const hecl::ProjectPath& path)
{
FILE* fp = hecl::Fopen(path.getAbsolutePath().c_str(), _S("rb"));