Make sure the sentinel is at the end of the current event pump cycle

If we're waiting, it's possible to not get any events, then add the sentinel, then pump again and then add another sentinel. We want to make sure we only have one sentinel and that it's at the end of the currently pumped events.

Testing:
* Verified test case in https://github.com/libsdl-org/SDL/issues/6539
* Verified test case in https://github.com/libsdl-org/SDL/issues/5350

Fixes https://github.com/libsdl-org/SDL/issues/6539

(cherry picked from commit 00b87f1ded0a4fdb6a0bab611171f37eeb0b2ebb)
main
Sam Lantinga 2023-05-20 10:27:48 -07:00
parent 1a3b713524
commit f005106d1b
1 changed files with 6 additions and 5 deletions

View File

@ -873,6 +873,11 @@ static void SDL_PumpEventsInternal(SDL_bool push_sentinel)
if (push_sentinel && SDL_EventEnabled(SDL_EVENT_POLL_SENTINEL)) {
SDL_Event sentinel;
/* Make sure we don't already have a sentinel in the queue, and add one to the end */
if (SDL_AtomicGet(&SDL_sentinel_pending) > 0) {
SDL_PeepEventsInternal(&sentinel, 1, SDL_GETEVENT, SDL_EVENT_POLL_SENTINEL, SDL_EVENT_POLL_SENTINEL, SDL_TRUE);
}
sentinel.type = SDL_EVENT_POLL_SENTINEL;
sentinel.common.timestamp = 0;
SDL_PushEvent(&sentinel);
@ -915,11 +920,7 @@ static int SDL_WaitEventTimeout_Device(SDL_VideoDevice *_this, SDL_Window *wakeu
c) Periodic processing that takes place in some platform PumpEvents() functions happens
d) Signals received in WaitEventTimeout() are turned into SDL events
*/
/* We only want a single sentinel in the queue. We could get more than one if event is NULL,
* since the SDL_PeepEvents() call below won't remove it in that case.
*/
SDL_bool add_sentinel = (SDL_AtomicGet(&SDL_sentinel_pending) == 0) ? SDL_TRUE : SDL_FALSE;
SDL_PumpEventsInternal(add_sentinel);
SDL_PumpEventsInternal(SDL_TRUE);
SDL_LockMutex(_this->wakeup_lock);
{