SDL: Only return from SDL_PollEvent() if the last sentinel is consumed

From 8ff2166855067feaac81512be5d32c73841b6533 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 6 Jan 2022 12:56:07 -0800
Subject: [PATCH] Only return from SDL_PollEvent() if the last sentinel is
 consumed

---
 src/events/SDL_events.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 84d0defae9a..65415d0f355 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -984,6 +984,7 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
     }
 
     /* First check for existing events */
+retry:
     switch (SDL_PeepEventsInternal(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT, include_sentinel)) {
     case -1:
         return 0;
@@ -995,7 +996,11 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
         break;
     default:
         if (event && event->type == SDL_POLLSENTINEL) {
-            /* Reached the end of a poll cycle, and no timeout */
+            /* Reached the end of a poll cycle, and not willing to wait */
+            if (SDL_AtomicGet(&SDL_sentinel_pending) > 0) {
+                /* We have another sentinel pending, skip this and keep going */
+                goto retry;
+            }
             return 0;
         }
         /* Has existing events */