upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authormarquezini <felipemarquesini10@gmail.com>2026-11-22 16:32:10 -0300
committermarquezini <felipemarquesini10@gmail.com>2026-11-22 16:32:10 -0300
commite7c83353c7aabdffa873f963f20d9ab6a1b938a9 (patch)
treeb8e4f2980b95424c814b6cee814231f4e1be3e61 /runtime/shims/gameinput.cpp
parent2aed56c6e50c4fa577c50e5b10976ba9f9a7379f (diff)
RT-176: Dispatch runs only the batch queued when it starts
Review follow-up. Dispatch took events one at a time from the live queue, so readings queued during the call (by the poll thread, or by a callback) were delivered in the same call: with an unlimited quota a busy queue could keep the title inside Dispatch, and each erase at the front made a drain O(n^2) under deliver_mu. Dispatch now swaps out the batch present at entry and walks it in order; events it leaves (quota spent) go back to the front of the queue, ahead of newer ones. gameinput_test adds a callback that queues a reading from inside the call: each Dispatch(UINT64_MAX) runs exactly its starting batch and returns true (the previous version never returned). The docs say so, and the dispatcher row's open column reads a dash instead of a blank.
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp29
1 files changed, 15 insertions, 14 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 61999a3ad..2acf5e381 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -463,31 +463,32 @@ bool deliver() {
463 return !s.queue.empty(); 463 return !s.queue.empty();
464} 464}
465 465
466// IGameInputDispatcher::Dispatch: runs queued events oldest first, at least one even with a quota of 0, and takes no 466// IGameInputDispatcher::Dispatch: runs the events queued when the call starts, oldest first: at least one even with a
467// further event once quota_us microseconds have passed (an event already started runs to the end, so the quota is a 467// quota of 0, and no further event once quota_us microseconds have passed (an event already started runs to the end,
468// target, as on Windows). While events remain the wait handle is signalled again, so a title that waits on it before 468// so the quota is a target, as on Windows). Events queued during the call wait for the next one, so a busy poll thread
469// each call comes back for the rest. Returns whether events remain. 469// cannot keep the title inside Dispatch even with an unlimited quota. Events left over go back to the front of the
470// queue, ahead of newer ones, and the wait handle is signalled again while any remain. Returns whether events remain.
470bool deliver_within(uint64_t quota_us) { 471bool deliver_within(uint64_t quota_us) {
471 State& s = g(); 472 State& s = g();
472 std::lock_guard dl(s.deliver_mu); 473 std::lock_guard dl(s.deliver_mu);
473 const auto start = std::chrono::steady_clock::now(); 474 const auto start = std::chrono::steady_clock::now();
475 std::vector<Event> batch;
476 {
477 std::lock_guard lock(s.mu);
478 batch.swap(s.queue);
479 }
474 s.delivering_thread = GetCurrentThreadId(); 480 s.delivering_thread = GetCurrentThreadId();
475 for (bool first = true;; first = false) { 481 size_t next = 0;
476 if (!first) { 482 while (next < batch.size()) {
483 if (next > 0) {
477 const auto spent = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start); 484 const auto spent = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start);
478 if (static_cast<uint64_t>(spent.count()) >= quota_us) break; 485 if (static_cast<uint64_t>(spent.count()) >= quota_us) break;
479 } 486 }
480 Event e; 487 deliver_event(batch[next++]);
481 {
482 std::lock_guard lock(s.mu);
483 if (s.queue.empty()) break;
484 e = s.queue.front();
485 s.queue.erase(s.queue.begin());
486 }
487 deliver_event(e);
488 } 488 }
489 s.delivering_thread = 0; 489 s.delivering_thread = 0;
490 std::lock_guard lock(s.mu); 490 std::lock_guard lock(s.mu);
491 if (next < batch.size()) s.queue.insert(s.queue.begin(), batch.begin() + static_cast<std::ptrdiff_t>(next), batch.end());
491 const bool remaining = !s.queue.empty(); 492 const bool remaining = !s.queue.empty();
492 if (remaining && s.wait_event) SetEvent(s.wait_event); 493 if (remaining && s.wait_event) SetEvent(s.wait_event);
493 return remaining; 494 return remaining;