SDL: Revert "We only need to add the sentinel at the top of SDL_WaitEventTimeout()"

From 289c3fbb64b0615f8d627cad0a582c5f1711f93d Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 7 Jan 2022 17:07:22 -0800
Subject: [PATCH] Revert "We only need to add the sentinel at the top of
 SDL_WaitEventTimeout()"

This reverts commit c477768e6f926c38d4cdc236cab8376faf9f6789.

We want to add the sentinel anytime we pump inside SDL_WaitEventTimeout() to avoid pumping again the next time through, as a performance optimization.
---
 src/events/SDL_events.c | 30 ++++++++++++++++++------------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index 7bd982a7a84..ff079c50ea9 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -809,7 +809,7 @@ SDL_FlushEvents(Uint32 minType, Uint32 maxType)
 
 /* Run the system dependent event loops */
 void
-SDL_PumpEvents()
+SDL_PumpEventsInternal(SDL_bool push_sentinel)
 {
     SDL_VideoDevice *_this = SDL_GetVideoDevice();
 
@@ -836,6 +836,20 @@ SDL_PumpEvents()
 #endif
 
     SDL_SendPendingSignalEvents();  /* in case we had a signal handler fire, etc. */
+
+    if (push_sentinel && SDL_GetEventState(SDL_POLLSENTINEL) == SDL_ENABLE) {
+        SDL_Event sentinel;
+
+        SDL_zero(sentinel);
+        sentinel.type = SDL_POLLSENTINEL;
+        SDL_PushEvent(&sentinel);
+    }
+}
+
+void
+SDL_PumpEvents()
+{
+    SDL_PumpEventsInternal(SDL_FALSE);
 }
 
 /* Public functions */
@@ -877,7 +891,7 @@ SDL_WaitEventTimeout_Device(_THIS, SDL_Window *wakeup_window, SDL_Event * event,
            c) Periodic processing that takes place in some platform PumpEvents() functions happens
            d) Signals received in WaitEventTimeout() are turned into SDL events
         */
-        SDL_PumpEvents();
+        SDL_PumpEventsInternal(SDL_TRUE);
 
         if (!_this->wakeup_lock || SDL_LockMutex(_this->wakeup_lock) == 0) {
             int status = SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
@@ -976,15 +990,7 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
 
     /* If there isn't a poll sentinel event pending, pump events and add one */
     if (SDL_AtomicGet(&SDL_sentinel_pending) == 0) {
-        SDL_PumpEvents();
-
-        if (SDL_GetEventState(SDL_POLLSENTINEL) == SDL_ENABLE) {
-            SDL_Event sentinel;
-
-            SDL_zero(sentinel);
-            sentinel.type = SDL_POLLSENTINEL;
-            SDL_PushEvent(&sentinel);
-        }
+        SDL_PumpEventsInternal(SDL_TRUE);
     }
 
     /* First check for existing events */
@@ -1031,7 +1037,7 @@ SDL_WaitEventTimeout(SDL_Event * event, int timeout)
     }
 
     for (;;) {
-        SDL_PumpEvents();
+        SDL_PumpEventsInternal(SDL_TRUE);
         switch (SDL_PeepEvents(event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT)) {
         case -1:
             return 0;