diff --git a/src/platform/core/include/platform/emulator_thread.hpp b/src/platform/core/include/platform/emulator_thread.hpp index 37819e9f..90a9017a 100644 --- a/src/platform/core/include/platform/emulator_thread.hpp +++ b/src/platform/core/include/platform/emulator_thread.hpp @@ -54,6 +54,7 @@ struct EmulatorThread { void PushMessage(const Message& message); void ProcessMessages(); + void ProcessMessage(const Message& message); static constexpr int k_input_subsample_count = 4; static constexpr int k_cycles_per_second = 16777216; diff --git a/src/platform/core/src/emulator_thread.cpp b/src/platform/core/src/emulator_thread.cpp index 497872f6..1a4bdd66 100644 --- a/src/platform/core/src/emulator_thread.cpp +++ b/src/platform/core/src/emulator_thread.cpp @@ -105,8 +105,15 @@ void EmulatorThread::PushMessage(const Message& message) { return; } - std::lock_guard lock_guard{msg_queue_mutex}; - msg_queue.push(message); // @todo: maybe use emplace. + if(std::this_thread::get_id() == thread.get_id()) { + // Messages sent on the emulator thread (i.e. from a callback) do + // not need to be pushed to the message queue. + // Process them right away instead to reduce latency. + ProcessMessage(message); + } else { + std::lock_guard lock_guard{msg_queue_mutex}; + msg_queue.push(message); // @todo: maybe use emplace. + } } void EmulatorThread::ProcessMessages() { @@ -115,22 +122,23 @@ void EmulatorThread::ProcessMessages() { std::lock_guard lock_guard{msg_queue_mutex}; while(!msg_queue.empty()) { - const Message& message = msg_queue.front(); - - switch(message.type) { - case MessageType::Reset: { - core->Reset(); - break; - } - case MessageType::SetKeyStatus: { - core->SetKeyStatus(message.set_key_status.key, message.set_key_status.pressed); - break; - } - default: Assert(false, "unhandled message type: {}", (int)message.type); - } - + ProcessMessage(msg_queue.front()); msg_queue.pop(); } } +void EmulatorThread::ProcessMessage(const Message& message) { + switch(message.type) { + case MessageType::Reset: { + core->Reset(); + break; + } + case MessageType::SetKeyStatus: { + core->SetKeyStatus(message.set_key_status.key, message.set_key_status.pressed); + break; + } + default: Assert(false, "unhandled message type: {}", (int)message.type); + } +} + } // namespace nba