SDL: Make sure the sentinel is at the end of the current event pump cycle (f0051)

From f005106d1ba7a95d362e76159b16a5f33c624886 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 20 May 2023 10:27:48 -0700
Subject: [PATCH] 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)
---
 src/events/SDL_events.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 314983107f8b..f9b8dca3e1a7 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -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);
         {