lib/base: Use standard mutex primitives in event queue buffer

This commit is contained in:
Povilas Kanapickas 2021-11-03 02:58:28 +02:00
parent cb69469871
commit 27de30e761
2 changed files with 9 additions and 12 deletions

View File

@ -28,21 +28,17 @@ class EventQueueTimer { };
SimpleEventQueueBuffer::SimpleEventQueueBuffer()
{
m_queueMutex = ARCH->newMutex();
m_queueReadyCond = ARCH->newCondVar();
m_queueReady = false;
}
SimpleEventQueueBuffer::~SimpleEventQueueBuffer()
{
ARCH->closeCondVar(m_queueReadyCond);
ARCH->closeMutex(m_queueMutex);
}
void
SimpleEventQueueBuffer::waitForEvent(double timeout)
{
ArchMutexLock lock(m_queueMutex);
std::unique_lock<std::mutex> lock(queue_mutex_);
Stopwatch timer(true);
while (!m_queueReady) {
double timeLeft = timeout;
@ -52,14 +48,14 @@ SimpleEventQueueBuffer::waitForEvent(double timeout)
return;
}
}
ARCH->waitCondVar(m_queueReadyCond, lock, timeLeft);
ARCH->wait_cond_var(queue_ready_cv_, lock, timeLeft);
}
}
IEventQueueBuffer::Type
SimpleEventQueueBuffer::getEvent(Event&, UInt32& dataID)
{
ArchMutexLock lock(m_queueMutex);
std::lock_guard<std::mutex> lock(queue_mutex_);
if (!m_queueReady) {
return kNone;
}
@ -72,11 +68,11 @@ SimpleEventQueueBuffer::getEvent(Event&, UInt32& dataID)
bool
SimpleEventQueueBuffer::addEvent(UInt32 dataID)
{
ArchMutexLock lock(m_queueMutex);
std::lock_guard<std::mutex> lock(queue_mutex_);
m_queue.push_front(dataID);
if (!m_queueReady) {
m_queueReady = true;
ARCH->broadcastCondVar(m_queueReadyCond);
queue_ready_cv_.notify_all();
}
return true;
}
@ -84,7 +80,7 @@ SimpleEventQueueBuffer::addEvent(UInt32 dataID)
bool
SimpleEventQueueBuffer::isEmpty() const
{
ArchMutexLock lock(m_queueMutex);
std::lock_guard<std::mutex> lock(queue_mutex_);
return !m_queueReady;
}

View File

@ -21,6 +21,7 @@
#include "base/IEventQueueBuffer.h"
#include "arch/IArchMultithread.h"
#include "common/stddeque.h"
#include <condition_variable>
//! In-memory event queue buffer
/*!
@ -44,8 +45,8 @@ public:
private:
typedef std::deque<UInt32> EventDeque;
ArchMutex m_queueMutex;
ArchCond m_queueReadyCond;
mutable std::mutex queue_mutex_;
std::condition_variable queue_ready_cv_;
bool m_queueReady;
EventDeque m_queue;
};