Skip to content

Commit

Permalink
Platform: Core: reduce latency on handling messages produced on the e…
Browse files Browse the repository at this point in the history
…mulator thread
  • Loading branch information
fleroviux committed Jan 18, 2024
1 parent 3f7a202 commit d9e87f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/platform/core/include/platform/emulator_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 24 additions & 16 deletions src/platform/core/src/emulator_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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

0 comments on commit d9e87f9

Please sign in to comment.