Skip to content

Commit

Permalink
Audit our use of condition variables, fix some minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Jan 29, 2024
1 parent a93b55c commit ee62ffd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
12 changes: 6 additions & 6 deletions Common/File/PathBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ bool LoadRemoteFileList(const Path &url, const std::string &userAgent, bool *can
}

PathBrowser::~PathBrowser() {
std::unique_lock<std::mutex> guard(pendingLock_);
pendingCancel_ = true;
pendingStop_ = true;
pendingCond_.notify_all();
guard.unlock();

{
std::unique_lock<std::mutex> guard(pendingLock_);
pendingCancel_ = true;
pendingStop_ = true;
pendingCond_.notify_all();
}
if (pendingThread_.joinable()) {
pendingThread_.join();
}
Expand Down
38 changes: 20 additions & 18 deletions Common/GPU/Vulkan/VulkanRenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,7 @@ void VulkanRenderManager::StopThreads() {
pushCondVar_.notify_one();
// Once the render thread encounters the above exit task, it'll exit.
renderThread_.join();
}

// Compiler and present thread still relies on this.
runCompileThread_ = false;

if (presentWaitThread_.joinable()) {
presentWaitThread_.join();
INFO_LOG(G3D, "Vulkan submission thread joined. Frame=%d", vulkan_->GetCurFrame());
}

for (int i = 0; i < vulkan_->GetInflightFrames(); i++) {
Expand All @@ -400,12 +394,18 @@ void VulkanRenderManager::StopThreads() {
frameData.profile.timestampDescriptions.clear();
}

INFO_LOG(G3D, "Vulkan submission thread joined. Frame=%d", vulkan_->GetCurFrame());

_assert_(compileThread_.joinable());
compileCond_.notify_all();
{
std::unique_lock<std::mutex> lock(compileMutex_);
runCompileThread_ = false; // Compiler and present thread both look at this bool.
_assert_(compileThread_.joinable());
compileCond_.notify_one();
}
compileThread_.join();

if (presentWaitThread_.joinable()) {
presentWaitThread_.join();
}

INFO_LOG(G3D, "Vulkan compiler thread joined. Now wait for any straggling compile tasks.");
CreateMultiPipelinesTask::WaitForAll();

Expand Down Expand Up @@ -462,7 +462,7 @@ void VulkanRenderManager::CompileThreadFunc() {
std::vector<CompileQueueEntry> toCompile;
{
std::unique_lock<std::mutex> lock(compileMutex_);
if (compileQueue_.empty() && runCompileThread_) {
while (compileQueue_.empty() && runCompileThread_) {
compileCond_.wait(lock);
}
toCompile = std::move(compileQueue_);
Expand Down Expand Up @@ -786,7 +786,7 @@ VKRGraphicsPipeline *VulkanRenderManager::CreateGraphicsPipeline(VKRGraphicsPipe
VKRRenderPassStoreAction::STORE, VKRRenderPassStoreAction::DONT_CARE, VKRRenderPassStoreAction::DONT_CARE,
};
VKRRenderPass *compatibleRenderPass = queueRunner_.GetRenderPass(key);
std::lock_guard<std::mutex> lock(compileMutex_);
std::unique_lock<std::mutex> lock(compileMutex_);
bool needsCompile = false;
for (size_t i = 0; i < (size_t)RenderPassType::TYPE_COUNT; i++) {
if (!(variantBitmask & (1 << i)))
Expand All @@ -809,7 +809,7 @@ VKRGraphicsPipeline *VulkanRenderManager::CreateGraphicsPipeline(VKRGraphicsPipe
}

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;
}
if (needsCompile)
Expand Down Expand Up @@ -1551,10 +1551,12 @@ void VulkanRenderManager::FlushSync() {
VLOG("PUSH: Frame[%d]", curFrame);
VKRRenderThreadTask *task = new VKRRenderThreadTask(VKRRunType::SYNC);
task->frame = curFrame;
std::unique_lock<std::mutex> lock(pushMutex_);
renderThreadQueue_.push(task);
renderThreadQueue_.back()->steps = std::move(steps_);
pushCondVar_.notify_one();
{
std::unique_lock<std::mutex> lock(pushMutex_);
renderThreadQueue_.push(task);
renderThreadQueue_.back()->steps = std::move(steps_);
pushCondVar_.notify_one();
}
steps_.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion Common/Thread/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ ThreadManager::~ThreadManager() {

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

Expand Down
7 changes: 5 additions & 2 deletions UI/GameSettingsScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,11 @@ class HostnameSelectScreen : public PopupScreen {
}, this);
}
~HostnameSelectScreen() {
resolverState_ = ResolverState::QUIT;
resolverCond_.notify_one();
{
std::unique_lock<std::mutex> guard(resolverLock_);
resolverState_ = ResolverState::QUIT;
resolverCond_.notify_one();
}
resolver_.join();
}

Expand Down

0 comments on commit ee62ffd

Please sign in to comment.