Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to call for shutting down the ResourceManager thread pool #815

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/resource/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,40 +291,40 @@ ResourceManager::GetCachedResource(std::variant<ResourceLoadError, std::shared_p
}

std::shared_ptr<std::vector<std::shared_ptr<IResource>>>
ResourceManager::LoadResourcesProcess(const ResourceFilter& filter) {
ResourceManager::LoadResourcesProcess(const ResourceFilter& filter, bool exact) {
auto loadedList = std::make_shared<std::vector<std::shared_ptr<IResource>>>();
auto fileList = GetArchiveManager()->ListFiles(filter.IncludeMasks, filter.ExcludeMasks);
loadedList->reserve(fileList->size());

for (size_t i = 0; i < fileList->size(); i++) {
auto fileName = std::string(fileList->operator[](i));
auto resource = LoadResource({ fileName, filter.Owner, filter.Parent });
auto resource = LoadResource({ fileName, filter.Owner, filter.Parent }, exact);
loadedList->push_back(resource);
}

return loadedList;
}

std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
ResourceManager::LoadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority) {
ResourceManager::LoadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority, bool exact) {
return mThreadPool->submit_task(
[this, filter]() -> std::shared_ptr<std::vector<std::shared_ptr<IResource>>> {
return LoadResourcesProcess(filter);
[this, filter, exact]() -> std::shared_ptr<std::vector<std::shared_ptr<IResource>>> {
return LoadResourcesProcess(filter, exact);
},
priority);
}

std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
ResourceManager::LoadResourcesAsync(const std::string& searchMask, BS::priority_t priority) {
return LoadResourcesAsync({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive }, priority);
ResourceManager::LoadResourcesAsync(const std::string& searchMask, BS::priority_t priority, bool exact) {
return LoadResourcesAsync({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive }, priority, exact);
}

std::shared_ptr<std::vector<std::shared_ptr<IResource>>> ResourceManager::LoadResources(const std::string& searchMask) {
return LoadResources({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive });
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> ResourceManager::LoadResources(const std::string& searchMask, bool exact) {
return LoadResources({ { searchMask }, {}, mDefaultCacheOwner, mDefaultCacheArchive }, exact);
}

std::shared_ptr<std::vector<std::shared_ptr<IResource>>> ResourceManager::LoadResources(const ResourceFilter& filter) {
return LoadResourcesAsync(filter, BS::pr::highest).get();
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> ResourceManager::LoadResources(const ResourceFilter& filter, bool exact) {
return LoadResourcesAsync(filter, BS::pr::highest, exact).get();
}

void ResourceManager::DirtyResources(const ResourceFilter& filter) {
Expand Down Expand Up @@ -411,4 +411,14 @@ void ResourceManager::SetAltAssetsEnabled(bool isEnabled) {
mAltAssetsEnabled = isEnabled;
}

void ResourceManager::ShutDownThreadPool() {
mThreadPool->pause();
mThreadPool->wait_for(std::chrono::duration<double>(2));
Copy link
Collaborator

Choose a reason for hiding this comment

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

i see you mentioned

I have a wait time of 2 seconds here between pausing and purging to prevent deadlocks

that feels a little bit magic to me, are we just assuming we'll never have a pool where 2 seconds isn't enough time? have you looked into alternative ways to mitigate deadlock that don't rely on a hardcoded delay?

Copy link
Collaborator

@briaguya-ai briaguya-ai Feb 21, 2025

Choose a reason for hiding this comment

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

@Kenix3's suggestion is to just pass the amount of time to wait as a function param

mThreadPool->purge();
}

void ResourceManager::ThreadPoolWait(std::chrono::duration<double> interval) {
mThreadPool->wait_for(interval);
}

} // namespace Ship
12 changes: 7 additions & 5 deletions src/resource/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class ResourceManager {
size_t UnloadResource(const ResourceIdentifier& identifier);
size_t UnloadResource(const std::string& filePath);

std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const std::string& searchMask);
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const ResourceFilter& filter);
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const std::string& searchMask, bool exact = false);
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResources(const ResourceFilter& filter, bool exact = false);
std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
LoadResourcesAsync(const std::string& searchMask, BS::priority_t priority = BS::pr::normal);
LoadResourcesAsync(const std::string& searchMask, BS::priority_t priority = BS::pr::normal, bool exact = false);
std::shared_future<std::shared_ptr<std::vector<std::shared_ptr<IResource>>>>
LoadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority = BS::pr::normal);
LoadResourcesAsync(const ResourceFilter& filter, BS::priority_t priority = BS::pr::normal, bool exact = false);

void DirtyResources(const std::string& searchMask);
void DirtyResources(const ResourceFilter& filter);
Expand All @@ -101,9 +101,11 @@ class ResourceManager {
bool OtrSignatureCheck(const char* fileName);
bool IsAltAssetsEnabled();
void SetAltAssetsEnabled(bool isEnabled);
void ShutDownThreadPool();
void ThreadPoolWait(std::chrono::duration<double> interval);

protected:
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResourcesProcess(const ResourceFilter& filter);
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadResourcesProcess(const ResourceFilter& filter, bool exact = false);
void UnloadResourcesProcess(const ResourceFilter& filter);
std::variant<ResourceLoadError, std::shared_ptr<IResource>> CheckCache(const ResourceIdentifier& identifier,
bool loadExact = false);
Expand Down
Loading