Audit our use of condition variables, fix some minor issues

This commit is contained in:
Henrik Rydgård 2024-01-29 11:09:08 +01:00
parent a93b55c215
commit ee62ffdb02
4 changed files with 32 additions and 27 deletions

View file

@ -111,12 +111,12 @@ bool LoadRemoteFileList(const Path &url, const std::string &userAgent, bool *can
} }
PathBrowser::~PathBrowser() { PathBrowser::~PathBrowser() {
std::unique_lock<std::mutex> guard(pendingLock_); {
pendingCancel_ = true; std::unique_lock<std::mutex> guard(pendingLock_);
pendingStop_ = true; pendingCancel_ = true;
pendingCond_.notify_all(); pendingStop_ = true;
guard.unlock(); pendingCond_.notify_all();
}
if (pendingThread_.joinable()) { if (pendingThread_.joinable()) {
pendingThread_.join(); pendingThread_.join();
} }

View file

@ -385,13 +385,7 @@ void VulkanRenderManager::StopThreads() {
pushCondVar_.notify_one(); pushCondVar_.notify_one();
// Once the render thread encounters the above exit task, it'll exit. // Once the render thread encounters the above exit task, it'll exit.
renderThread_.join(); renderThread_.join();
} INFO_LOG(G3D, "Vulkan submission thread joined. Frame=%d", vulkan_->GetCurFrame());
// Compiler and present thread still relies on this.
runCompileThread_ = false;
if (presentWaitThread_.joinable()) {
presentWaitThread_.join();
} }
for (int i = 0; i < vulkan_->GetInflightFrames(); i++) { for (int i = 0; i < vulkan_->GetInflightFrames(); i++) {
@ -400,12 +394,18 @@ void VulkanRenderManager::StopThreads() {
frameData.profile.timestampDescriptions.clear(); frameData.profile.timestampDescriptions.clear();
} }
INFO_LOG(G3D, "Vulkan submission thread joined. Frame=%d", vulkan_->GetCurFrame()); {
std::unique_lock<std::mutex> lock(compileMutex_);
_assert_(compileThread_.joinable()); runCompileThread_ = false; // Compiler and present thread both look at this bool.
compileCond_.notify_all(); _assert_(compileThread_.joinable());
compileCond_.notify_one();
}
compileThread_.join(); compileThread_.join();
if (presentWaitThread_.joinable()) {
presentWaitThread_.join();
}
INFO_LOG(G3D, "Vulkan compiler thread joined. Now wait for any straggling compile tasks."); INFO_LOG(G3D, "Vulkan compiler thread joined. Now wait for any straggling compile tasks.");
CreateMultiPipelinesTask::WaitForAll(); CreateMultiPipelinesTask::WaitForAll();
@ -462,7 +462,7 @@ void VulkanRenderManager::CompileThreadFunc() {
std::vector<CompileQueueEntry> toCompile; std::vector<CompileQueueEntry> toCompile;
{ {
std::unique_lock<std::mutex> lock(compileMutex_); std::unique_lock<std::mutex> lock(compileMutex_);
if (compileQueue_.empty() && runCompileThread_) { while (compileQueue_.empty() && runCompileThread_) {
compileCond_.wait(lock); compileCond_.wait(lock);
} }
toCompile = std::move(compileQueue_); toCompile = std::move(compileQueue_);
@ -786,7 +786,7 @@ VKRGraphicsPipeline *VulkanRenderManager::CreateGraphicsPipeline(VKRGraphicsPipe
VKRRenderPassStoreAction::STORE, VKRRenderPassStoreAction::DONT_CARE, VKRRenderPassStoreAction::DONT_CARE, VKRRenderPassStoreAction::STORE, VKRRenderPassStoreAction::DONT_CARE, VKRRenderPassStoreAction::DONT_CARE,
}; };
VKRRenderPass *compatibleRenderPass = queueRunner_.GetRenderPass(key); VKRRenderPass *compatibleRenderPass = queueRunner_.GetRenderPass(key);
std::lock_guard<std::mutex> lock(compileMutex_); std::unique_lock<std::mutex> lock(compileMutex_);
bool needsCompile = false; bool needsCompile = false;
for (size_t i = 0; i < (size_t)RenderPassType::TYPE_COUNT; i++) { for (size_t i = 0; i < (size_t)RenderPassType::TYPE_COUNT; i++) {
if (!(variantBitmask & (1 << i))) if (!(variantBitmask & (1 << i)))
@ -809,7 +809,7 @@ VKRGraphicsPipeline *VulkanRenderManager::CreateGraphicsPipeline(VKRGraphicsPipe
} }
pipeline->pipeline[i] = Promise<VkPipeline>::CreateEmpty(); pipeline->pipeline[i] = Promise<VkPipeline>::CreateEmpty();
compileQueue_.push_back(CompileQueueEntry(pipeline, compatibleRenderPass->Get(vulkan_, rpType, sampleCount), rpType, sampleCount)); compileQueue_.emplace_back(pipeline, compatibleRenderPass->Get(vulkan_, rpType, sampleCount), rpType, sampleCount);
needsCompile = true; needsCompile = true;
} }
if (needsCompile) if (needsCompile)
@ -1551,10 +1551,12 @@ void VulkanRenderManager::FlushSync() {
VLOG("PUSH: Frame[%d]", curFrame); VLOG("PUSH: Frame[%d]", curFrame);
VKRRenderThreadTask *task = new VKRRenderThreadTask(VKRRunType::SYNC); VKRRenderThreadTask *task = new VKRRenderThreadTask(VKRRunType::SYNC);
task->frame = curFrame; task->frame = curFrame;
std::unique_lock<std::mutex> lock(pushMutex_); {
renderThreadQueue_.push(task); std::unique_lock<std::mutex> lock(pushMutex_);
renderThreadQueue_.back()->steps = std::move(steps_); renderThreadQueue_.push(task);
pushCondVar_.notify_one(); renderThreadQueue_.back()->steps = std::move(steps_);
pushCondVar_.notify_one();
}
steps_.clear(); steps_.clear();
} }

View file

@ -59,8 +59,8 @@ ThreadManager::~ThreadManager() {
void ThreadManager::Teardown() { void ThreadManager::Teardown() {
for (TaskThreadContext *&threadCtx : global_->threads_) { for (TaskThreadContext *&threadCtx : global_->threads_) {
threadCtx->cancelled = true;
std::unique_lock<std::mutex> lock(threadCtx->mutex); std::unique_lock<std::mutex> lock(threadCtx->mutex);
threadCtx->cancelled = true;
threadCtx->cond.notify_one(); threadCtx->cond.notify_one();
} }

View file

@ -175,8 +175,11 @@ public:
}, this); }, this);
} }
~HostnameSelectScreen() { ~HostnameSelectScreen() {
resolverState_ = ResolverState::QUIT; {
resolverCond_.notify_one(); std::unique_lock<std::mutex> guard(resolverLock_);
resolverState_ = ResolverState::QUIT;
resolverCond_.notify_one();
}
resolver_.join(); resolver_.join();
} }