lib/base: Use standard mutex primitives in EventQueue

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

View File

@ -18,8 +18,6 @@
#include "base/EventQueue.h"
#include "mt/Mutex.h"
#include "mt/Lock.h"
#include "arch/Arch.h"
#include "base/SimpleEventQueueBuffer.h"
#include "base/Stopwatch.h"
@ -86,9 +84,7 @@ EventQueue::EventQueue() :
m_typesForIPrimaryScreen(NULL),
m_typesForIScreen(NULL),
m_typesForClipboard(NULL),
m_typesForFile(NULL),
m_readyMutex(new Mutex),
m_readyCondVar(new CondVar<bool>(m_readyMutex, false))
m_typesForFile(NULL)
{
ARCH->setSignalHandler(Arch::kINTERRUPT, &interrupt, this);
ARCH->setSignalHandler(Arch::kTERMINATE, &interrupt, this);
@ -98,9 +94,6 @@ EventQueue::EventQueue() :
EventQueue::~EventQueue()
{
delete m_buffer;
delete m_readyCondVar;
delete m_readyMutex;
ARCH->setSignalHandler(Arch::kINTERRUPT, NULL, NULL);
ARCH->setSignalHandler(Arch::kTERMINATE, NULL, NULL);
}
@ -110,9 +103,9 @@ EventQueue::loop()
{
m_buffer->init();
{
Lock lock(m_readyMutex);
*m_readyCondVar = true;
m_readyCondVar->signal();
std::unique_lock<std::mutex> lock(ready_mutex_);
is_ready_ = true;
ready_cv_.notify_one();
}
LOG((CLOG_DEBUG "event queue is ready"));
while (!m_pending.empty()) {
@ -303,7 +296,7 @@ EventQueue::addEvent(const Event& event)
dispatchEvent(event);
Event::deleteData(event);
}
else if (!(*m_readyCondVar)) {
else if (!is_ready_) {
m_pending.push(event);
}
else {
@ -566,14 +559,11 @@ EventQueue::getSystemTarget()
void
EventQueue::waitForReady() const
{
double timeout = ARCH->time() + 10;
Lock lock(m_readyMutex);
std::unique_lock<std::mutex> lock(ready_mutex_);
while (!m_readyCondVar->wait()) {
if (ARCH->time() > timeout) {
if (!ready_cv_.wait_for(lock, std::chrono::seconds{10}, [this](){ return is_ready_; })) {
throw std::runtime_error("event queue is not ready within 5 sec");
}
}
}
//

View File

@ -28,6 +28,7 @@
#include "common/stdset.h"
#include "base/NonBlockingStream.h"
#include <condition_variable>
#include <mutex>
#include <queue>
@ -180,8 +181,9 @@ private:
IScreenEvents* m_typesForIScreen;
ClipboardEvents* m_typesForClipboard;
FileEvents* m_typesForFile;
Mutex* m_readyMutex;
CondVar<bool>* m_readyCondVar;
mutable std::mutex ready_mutex_;
mutable std::condition_variable ready_cv_;
bool is_ready_ = false;
std::queue<Event> m_pending;
NonBlockingStream m_parentStream;
};