Skip to content

Commit

Permalink
Use a separate mutex for the TerminalEvent queue
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Oct 20, 2024
1 parent f75afe0 commit e492584
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions source/tvterm-core/termctrl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct TerminalController::TerminalEventLoop
bool terminated {false};

// Used for sending events from the main thread to the TerminalEventLoop's
// threads.
std::queue<TerminalEvent> eventQueue;
// threads. Has its own mutex to avoid blocking the main thread for long.
Mutex<std::queue<TerminalEvent>> eventQueue;

// Used for waking up the WriterLoop a short time after data was received
// by the ReaderLoop thread.
Expand Down Expand Up @@ -138,10 +138,9 @@ TerminalController::~TerminalController()

void TerminalController::sendEvent(const TerminalEvent &event) noexcept
{
{
std::lock_guard<std::mutex> lock(eventLoop.mutex);
eventLoop.eventQueue.push(event);
}
eventLoop.eventQueue.lock([&] (auto &eventQueue) {
eventQueue.push(event);
});
eventLoop.condVar.notify_one();
}

Expand Down Expand Up @@ -222,10 +221,21 @@ void TerminalController::TerminalEventLoop::runReaderLoop() noexcept
void TerminalController::TerminalEventLoop::processEvents() noexcept
// Pre: 'this->mutex' is locked.
{
while (!eventQueue.empty())
while (true)
{
TerminalEvent event = eventQueue.front();
eventQueue.pop();
bool hasEvent;
TerminalEvent event;

eventQueue.lock([&] (auto &eventQueue) {
if ((hasEvent = !eventQueue.empty()))
{
event = eventQueue.front();
eventQueue.pop();
}
});

if (!hasEvent)
break;

switch (event.type)
{
Expand Down

0 comments on commit e492584

Please sign in to comment.