From eb46ba2646586047bcf99dcdf9952f7a19e2b572 Mon Sep 17 00:00:00 2001 From: nanavuletic Date: Thu, 24 Feb 2022 17:27:55 +0100 Subject: [PATCH] Even better waits --- core/utilities/Buffer.h | 11 ++++++----- core/utilities/CommandBuffer.cpp | 15 +++++++-------- core/utilities/CommandBuffer.h | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/utilities/Buffer.h b/core/utilities/Buffer.h index a3c22a51..38656223 100644 --- a/core/utilities/Buffer.h +++ b/core/utilities/Buffer.h @@ -62,7 +62,10 @@ template Buffer::~Buffer() template void Buffer::stop() { - m_exitCondition = true; + { + std::lock_guard lg{m_pushLock}; + m_exitCondition = true; + } m_condition.notify_one(); } @@ -92,7 +95,7 @@ template T Buffer::pop() if (m_popQueue.empty()) { - LOG(ERROR) << "Poping from empty buffer"; + LOG(ERROR) << "Popping from empty buffer"; return {}; } @@ -106,10 +109,8 @@ template void Buffer::swapBuffers() { std::unique_lock pushGuard(m_pushLock); - if (m_pushQueue.empty() && !m_exitCondition) - { + if (m_pushQueue.empty()) m_condition.wait(pushGuard, [&] { return !m_pushQueue.empty() || m_exitCondition; }); - } std::lock_guard popGuard{m_popLock}; std::swap(m_pushQueue, m_popQueue); diff --git a/core/utilities/CommandBuffer.cpp b/core/utilities/CommandBuffer.cpp index 1f83b045..9808bb36 100644 --- a/core/utilities/CommandBuffer.cpp +++ b/core/utilities/CommandBuffer.cpp @@ -16,8 +16,6 @@ #include "CommandBuffer.h" -#include -#include #include #include #include @@ -36,7 +34,7 @@ CommandBuffer::~CommandBuffer() stop(); } -void CommandBuffer::pushCommand(std::shared_ptr command) +void CommandBuffer::pushCommand(const std::shared_ptr& command) { std::unique_lock unique_lock(m_lock); @@ -47,7 +45,10 @@ void CommandBuffer::pushCommand(std::shared_ptr command) void CommandBuffer::stop() { - m_isRunning = false; + { + std::lock_guard lg{m_lock}; + m_isRunning = false; + } notify(); if (m_worker && m_worker->joinable()) @@ -79,10 +80,8 @@ void CommandBuffer::switchBuffers() { std::unique_lock unique_lock(m_lock); - if (m_pushCommandQueue.empty() && m_isRunning) - { - m_condition.wait(unique_lock); - } + if (m_pushCommandQueue.empty()) + m_condition.wait(unique_lock, [&] { return !m_pushCommandQueue.empty() || !m_isRunning; }); std::swap(m_pushCommandQueue, m_popCommandQueue); } diff --git a/core/utilities/CommandBuffer.h b/core/utilities/CommandBuffer.h index bc7ba7aa..a2f8ef52 100644 --- a/core/utilities/CommandBuffer.h +++ b/core/utilities/CommandBuffer.h @@ -35,7 +35,7 @@ class CommandBuffer CommandBuffer(); virtual ~CommandBuffer(); - void pushCommand(std::shared_ptr command); + void pushCommand(const std::shared_ptr& command); void stop();