diff options
| author | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 16:32:10 -0300 |
|---|---|---|
| committer | marquezini <felipemarquesini10@gmail.com> | 2026-11-22 16:32:10 -0300 |
| commit | e7c83353c7aabdffa873f963f20d9ab6a1b938a9 (patch) | |
| tree | b8e4f2980b95424c814b6cee814231f4e1be3e61 /runtime/test/gameinput_test.cpp | |
| parent | 2aed56c6e50c4fa577c50e5b10976ba9f9a7379f (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/test/gameinput_test.cpp')
| -rw-r--r-- | runtime/test/gameinput_test.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/runtime/test/gameinput_test.cpp b/runtime/test/gameinput_test.cpp index d9b5d57e2..5156cbe82 100644 --- a/runtime/test/gameinput_test.cpp +++ b/runtime/test/gameinput_test.cpp | |||
| @@ -131,10 +131,17 @@ void CALLBACK on_device(GameInputCallbackToken token, void*, IGameInputDevice* d | |||
| 131 | g_device_events.push_back({device, current, previous, token}); | 131 | g_device_events.push_back({device, current, previous, token}); |
| 132 | } | 132 | } |
| 133 | DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota checks | 133 | DWORD g_reading_sleep_ms = 0; // makes on_reading slow, for the Dispatch quota checks |
| 134 | SDL_Joystick* g_requeue_pad = nullptr; // set: on_reading queues one more reading from inside the callback | ||
| 135 | bool g_requeue_down = false; | ||
| 134 | 136 | ||
| 135 | void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { | 137 | void CALLBACK on_reading(GameInputCallbackToken, void*, IGameInputReading* reading, bool) { |
| 136 | if (reading) ++g_reading_events; | 138 | if (reading) ++g_reading_events; |
| 137 | if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); | 139 | if (g_reading_sleep_ms) Sleep(g_reading_sleep_ms); |
| 140 | if (g_requeue_pad) { | ||
| 141 | g_requeue_down = !g_requeue_down; | ||
| 142 | SDL_SetJoystickVirtualButton(g_requeue_pad, SDL_GAMEPAD_BUTTON_WEST, g_requeue_down); | ||
| 143 | rt::gameinput::poll(); | ||
| 144 | } | ||
| 138 | } | 145 | } |
| 139 | void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) { | 146 | void CALLBACK on_guide(GameInputCallbackToken, void*, IGameInputDevice*, uint64_t, bool pressed) { |
| 140 | g_guide_events.push_back(pressed); | 147 | g_guide_events.push_back(pressed); |
| @@ -998,6 +1005,8 @@ int main() { | |||
| 998 | if (wait) WaitForSingleObject(wait, 0); // consume the signal from queueing | 1005 | if (wait) WaitForSingleObject(wait, 0); // consume the signal from queueing |
| 999 | check(dispatcher->Dispatch(0) && g_reading_events == before_quota + 1, "Dispatch(0) runs exactly one event and reports more"); | 1006 | check(dispatcher->Dispatch(0) && g_reading_events == before_quota + 1, "Dispatch(0) runs exactly one event and reports more"); |
| 1000 | check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled again while events remain"); | 1007 | check(wait && WaitForSingleObject(wait, 0) == WAIT_OBJECT_0, "the wait handle is signalled again while events remain"); |
| 1008 | // The callback sleeps 5 ms, so at least 5 ms have passed after the first event however loaded the machine | ||
| 1009 | // is; a slower run only makes the 1 ms quota more clearly spent. | ||
| 1001 | g_reading_sleep_ms = 5; | 1010 | g_reading_sleep_ms = 5; |
| 1002 | before_quota = g_reading_events; | 1011 | before_quota = g_reading_events; |
| 1003 | check(dispatcher->Dispatch(1000) && g_reading_events == before_quota + 1, | 1012 | check(dispatcher->Dispatch(1000) && g_reading_events == before_quota + 1, |
| @@ -1007,6 +1016,26 @@ int main() { | |||
| 1007 | check(!dispatcher->Dispatch(1'000'000) && g_reading_events >= before_quota + 2, | 1016 | check(!dispatcher->Dispatch(1'000'000) && g_reading_events >= before_quota + 2, |
| 1008 | "a large quota drains the queue and returns false"); | 1017 | "a large quota drains the queue and returns false"); |
| 1009 | check(!dispatcher->Dispatch(0), "Dispatch on an empty queue returns false"); | 1018 | check(!dispatcher->Dispatch(0), "Dispatch on an empty queue returns false"); |
| 1019 | |||
| 1020 | // Events queued during a call wait for the next one: each callback here queues another reading, so a | ||
| 1021 | // Dispatch that kept draining would never return. With two events queued, each call runs exactly the batch | ||
| 1022 | // it started with and reports the readings queued meanwhile. | ||
| 1023 | queue_changes(2); | ||
| 1024 | g_requeue_pad = xbox.js; | ||
| 1025 | before_quota = g_reading_events; | ||
| 1026 | check(dispatcher->Dispatch(UINT64_MAX) && g_reading_events == before_quota + 2, | ||
| 1027 | "Dispatch runs only the events queued when it started"); | ||
| 1028 | before_quota = g_reading_events; | ||
| 1029 | check(dispatcher->Dispatch(UINT64_MAX) && g_reading_events == before_quota + 2, | ||
| 1030 | "the events queued during the call run on the next one"); | ||
| 1031 | g_requeue_pad = nullptr; | ||
| 1032 | while (dispatcher->Dispatch(UINT64_MAX)) {} | ||
| 1033 | if (g_requeue_down) { | ||
| 1034 | g_requeue_down = false; | ||
| 1035 | SDL_SetJoystickVirtualButton(xbox.js, SDL_GAMEPAD_BUTTON_WEST, false); | ||
| 1036 | rt::gameinput::poll(); | ||
| 1037 | while (dispatcher->Dispatch(UINT64_MAX)) {} | ||
| 1038 | } | ||
| 1010 | } | 1039 | } |
| 1011 | if (wait) CloseHandle(wait); | 1040 | if (wait) CloseHandle(wait); |
| 1012 | if (dispatcher) dispatcher->Release(); | 1041 | if (dispatcher) dispatcher->Release(); |