diff options
| author | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 14:31:09 -0300 |
|---|---|---|
| committer | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 14:31:09 -0300 |
| commit | 2aed56c6e50c4fa577c50e5b10976ba9f9a7379f (patch) | |
| tree | 17d97c52f6d32794c0a98f9fb75edd66beba3713 /runtime/shims/gameinput.cpp | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
RT-176: IGameInputDispatcher::Dispatch honours its quota
Dispatch ran the whole queue whatever quota it was given. It now runs
queued events oldest first, at least one per call (a quota of 0 runs
exactly one, as the GDK documents), takes no further event once the
quota has passed, and returns whether events remain. While events
remain the wait handle is signalled again, so a title that waits on it
before each call comes back for the rest.
The automatic path (no dispatcher, poll thread) still delivers the
whole queue. gameinput_test checks Dispatch(0), a 1 ms quota against a
5 ms callback, a large quota draining the queue, and an empty queue.
Diffstat (limited to 'runtime/shims/gameinput.cpp')
| -rw-r--r-- | runtime/shims/gameinput.cpp | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index 35eac4c4e..61999a3ad 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp | |||
| @@ -32,6 +32,7 @@ | |||
| 32 | 32 | ||
| 33 | #include <algorithm> | 33 | #include <algorithm> |
| 34 | #include <atomic> | 34 | #include <atomic> |
| 35 | #include <chrono> | ||
| 35 | #include <cmath> | 36 | #include <cmath> |
| 36 | #include <cstdio> | 37 | #include <cstdio> |
| 37 | #include <cstdlib> | 38 | #include <cstdlib> |
| @@ -423,6 +424,28 @@ void call(const Callback& c, const Event& e) { | |||
| 423 | } | 424 | } |
| 424 | } | 425 | } |
| 425 | 426 | ||
| 427 | // Runs one queued event's callbacks on the calling thread, without holding the state lock. | ||
| 428 | void deliver_event(const Event& e) { | ||
| 429 | State& s = g(); | ||
| 430 | std::vector<Callback> targets; | ||
| 431 | { | ||
| 432 | std::lock_guard lock(s.mu); | ||
| 433 | for (const Callback& c : s.callbacks) | ||
| 434 | if (matches(c, e)) targets.push_back(c); | ||
| 435 | } | ||
| 436 | for (const Callback& c : targets) { | ||
| 437 | bool still = false; | ||
| 438 | { | ||
| 439 | std::lock_guard lock(s.mu); | ||
| 440 | for (const Callback& now : s.callbacks) | ||
| 441 | if (now.token == c.token) still = now.active; | ||
| 442 | } | ||
| 443 | if (still) call(c, e); | ||
| 444 | } | ||
| 445 | if (e.device) e.device->Release(); | ||
| 446 | if (e.reading) e.reading->Release(); | ||
| 447 | } | ||
| 448 | |||
| 426 | // Runs queued callbacks on the calling thread, without holding the state lock. Returns whether more events arrived | 449 | // Runs queued callbacks on the calling thread, without holding the state lock. Returns whether more events arrived |
| 427 | // meanwhile. | 450 | // meanwhile. |
| 428 | bool deliver() { | 451 | bool deliver() { |
| @@ -433,30 +456,41 @@ bool deliver() { | |||
| 433 | std::lock_guard lock(s.mu); | 456 | std::lock_guard lock(s.mu); |
| 434 | events.swap(s.queue); | 457 | events.swap(s.queue); |
| 435 | } | 458 | } |
| 436 | const DWORD self = GetCurrentThreadId(); | 459 | s.delivering_thread = GetCurrentThreadId(); |
| 437 | s.delivering_thread = self; | 460 | for (const Event& e : events) deliver_event(e); |
| 438 | for (const Event& e : events) { | 461 | s.delivering_thread = 0; |
| 439 | std::vector<Callback> targets; | 462 | std::lock_guard lock(s.mu); |
| 463 | return !s.queue.empty(); | ||
| 464 | } | ||
| 465 | |||
| 466 | // IGameInputDispatcher::Dispatch: runs queued events oldest first, at least one even with a quota of 0, and takes no | ||
| 467 | // further event once quota_us microseconds have passed (an event already started runs to the end, so the quota is a | ||
| 468 | // target, as on Windows). While events remain the wait handle is signalled again, so a title that waits on it before | ||
| 469 | // each call comes back for the rest. Returns whether events remain. | ||
| 470 | bool deliver_within(uint64_t quota_us) { | ||
| 471 | State& s = g(); | ||
| 472 | std::lock_guard dl(s.deliver_mu); | ||
| 473 | const auto start = std::chrono::steady_clock::now(); | ||
| 474 | s.delivering_thread = GetCurrentThreadId(); | ||
| 475 | for (bool first = true;; first = false) { | ||
| 476 | if (!first) { | ||
| 477 | 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; | ||
| 479 | } | ||
| 480 | Event e; | ||
| 440 | { | 481 | { |
| 441 | std::lock_guard lock(s.mu); | 482 | std::lock_guard lock(s.mu); |
| 442 | for (const Callback& c : s.callbacks) | 483 | if (s.queue.empty()) break; |
| 443 | if (matches(c, e)) targets.push_back(c); | 484 | e = s.queue.front(); |
| 485 | s.queue.erase(s.queue.begin()); | ||
| 444 | } | 486 | } |
| 445 | for (const Callback& c : targets) { | 487 | deliver_event(e); |
| 446 | bool still = false; | ||
| 447 | { | ||
| 448 | std::lock_guard lock(s.mu); | ||
| 449 | for (const Callback& now : s.callbacks) | ||
| 450 | if (now.token == c.token) still = now.active; | ||
| 451 | } | ||
| 452 | if (still) call(c, e); | ||
| 453 | } | ||
| 454 | if (e.device) e.device->Release(); | ||
| 455 | if (e.reading) e.reading->Release(); | ||
| 456 | } | 488 | } |
| 457 | s.delivering_thread = 0; | 489 | s.delivering_thread = 0; |
| 458 | std::lock_guard lock(s.mu); | 490 | std::lock_guard lock(s.mu); |
| 459 | return !s.queue.empty(); | 491 | const bool remaining = !s.queue.empty(); |
| 492 | if (remaining && s.wait_event) SetEvent(s.wait_event); | ||
| 493 | return remaining; | ||
| 460 | } | 494 | } |
| 461 | 495 | ||
| 462 | // ---- SDL backend (poll thread only) ----------------------------------------------------------------------------- | 496 | // ---- SDL backend (poll thread only) ----------------------------------------------------------------------------- |
| @@ -1153,8 +1187,8 @@ public: | |||
| 1153 | } | 1187 | } |
| 1154 | return r; | 1188 | return r; |
| 1155 | } | 1189 | } |
| 1156 | // Runs the queued callbacks on the caller's thread. The quota is not enforced: the queue is short. | 1190 | // Runs the queued callbacks on the caller's thread within the quota (see deliver_within). |
| 1157 | bool STDMETHODCALLTYPE Dispatch(uint64_t) override { return deliver(); } | 1191 | bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); } |
| 1158 | HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { | 1192 | HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { |
| 1159 | if (!handle) return E_POINTER; | 1193 | if (!handle) return E_POINTER; |
| 1160 | *handle = nullptr; | 1194 | *handle = nullptr; |