use mutexes in eventmanager like a normal human

This commit is contained in:
vaxerski 2022-05-25 17:26:26 +02:00
parent edac77abeb
commit b2769bab68
2 changed files with 10 additions and 26 deletions

View file

@ -48,7 +48,6 @@ void CEventManager::startThread() {
fcntl(SOCKET, F_SETFL, flags | O_NONBLOCK); fcntl(SOCKET, F_SETFL, flags | O_NONBLOCK);
while (1) { while (1) {
m_bCanWriteEventQueue = true;
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
@ -78,15 +77,11 @@ void CEventManager::startThread() {
// valid FDs, check the queue // valid FDs, check the queue
// don't do anything if main thread is writing to the eventqueue // don't do anything if main thread is writing to the eventqueue
while (!m_bCanReadEventQueue) { eventQueueMutex.lock();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// if we got here, we'll be reading the queue, let's disallow writing
m_bCanWriteEventQueue = false;
if (m_dQueuedEvents.empty()){ // if queue empty, sleep and ignore if (m_dQueuedEvents.empty()){ // if queue empty, sleep and ignore
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
eventQueueMutex.unlock();
continue; continue;
} }
@ -100,7 +95,7 @@ void CEventManager::startThread() {
m_dQueuedEvents.clear(); m_dQueuedEvents.clear();
m_bCanWriteEventQueue = true; eventQueueMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
@ -110,20 +105,9 @@ void CEventManager::startThread() {
} }
void CEventManager::postEvent(const SHyprIPCEvent event) { void CEventManager::postEvent(const SHyprIPCEvent event) {
m_bCanReadEventQueue = false;
if (!m_bCanWriteEventQueue) {
// if we can't write rn, make a thread to write whenever possible, don't block calling events.
std::thread([&](const SHyprIPCEvent ev) { std::thread([&](const SHyprIPCEvent ev) {
while(!m_bCanWriteEventQueue) { eventQueueMutex.lock();
std::this_thread::sleep_for(std::chrono::microseconds(200));
}
m_dQueuedEvents.push_back(ev); m_dQueuedEvents.push_back(ev);
m_bCanReadEventQueue = true; eventQueueMutex.unlock();
}, event).detach(); }, event).detach();
} else {
m_dQueuedEvents.push_back(event);
m_bCanReadEventQueue = true;
}
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <deque> #include <deque>
#include <fstream> #include <fstream>
#include <mutex>
#include "../defines.hpp" #include "../defines.hpp"
#include "../helpers/MiscFunctions.hpp" #include "../helpers/MiscFunctions.hpp"
@ -20,8 +21,7 @@ public:
private: private:
bool m_bCanReadEventQueue = true; std::mutex eventQueueMutex;
bool m_bCanWriteEventQueue = true;
std::deque<SHyprIPCEvent> m_dQueuedEvents; std::deque<SHyprIPCEvent> m_dQueuedEvents;
std::deque<int> m_dAcceptedSocketFDs; std::deque<int> m_dAcceptedSocketFDs;