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:
@@ -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"));
|
||||
|
||||
Reference in New Issue
Block a user