Skip to content

Commit

Permalink
Even better waits
Browse files Browse the repository at this point in the history
  • Loading branch information
nanavuletic committed Feb 24, 2022
1 parent ded5265 commit eb46ba2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
11 changes: 6 additions & 5 deletions core/utilities/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ template <class T> Buffer<T>::~Buffer()

template <class T> void Buffer<T>::stop()
{
m_exitCondition = true;
{
std::lock_guard<std::mutex> lg{m_pushLock};
m_exitCondition = true;
}
m_condition.notify_one();
}

Expand Down Expand Up @@ -92,7 +95,7 @@ template <class T> T Buffer<T>::pop()

if (m_popQueue.empty())
{
LOG(ERROR) << "Poping from empty buffer";
LOG(ERROR) << "Popping from empty buffer";
return {};
}

Expand All @@ -106,10 +109,8 @@ template <class T> void Buffer<T>::swapBuffers()
{
std::unique_lock<std::mutex> 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<std::mutex> popGuard{m_popLock};
std::swap(m_pushQueue, m_popQueue);
Expand Down
15 changes: 7 additions & 8 deletions core/utilities/CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include "CommandBuffer.h"

#include <atomic>
#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
Expand All @@ -36,7 +34,7 @@ CommandBuffer::~CommandBuffer()
stop();
}

void CommandBuffer::pushCommand(std::shared_ptr<Command> command)
void CommandBuffer::pushCommand(const std::shared_ptr<Command>& command)
{
std::unique_lock<std::mutex> unique_lock(m_lock);

Expand All @@ -47,7 +45,10 @@ void CommandBuffer::pushCommand(std::shared_ptr<Command> command)

void CommandBuffer::stop()
{
m_isRunning = false;
{
std::lock_guard<std::mutex> lg{m_lock};
m_isRunning = false;
}
notify();

if (m_worker && m_worker->joinable())
Expand Down Expand Up @@ -79,10 +80,8 @@ void CommandBuffer::switchBuffers()
{
std::unique_lock<std::mutex> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion core/utilities/CommandBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CommandBuffer
CommandBuffer();
virtual ~CommandBuffer();

void pushCommand(std::shared_ptr<Command> command);
void pushCommand(const std::shared_ptr<Command>& command);

void stop();

Expand Down

0 comments on commit eb46ba2

Please sign in to comment.