From 710d4ddf4df479bf2c87b8fbc31524aec6c3cd80 Mon Sep 17 00:00:00 2001 From: bones_was_here Date: Mon, 8 Apr 2024 10:41:52 +1000 Subject: [PATCH] sys: [Windows] fix spinning instead of sleeping For 231beb2c234914f942166eb152c44e55b5b01f86 I tested incorrectly, using Task Manager inside the VM which reports low CPU load even when spinning. DP's `status` command showed the correct result. Changes the default path from select() to: usleep() on POSIX, Sleep() on Windows. Signed-off-by: bones_was_here --- sys_shared.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/sys_shared.c b/sys_shared.c index 745a839e..d06c8849 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -508,7 +508,7 @@ double Sys_Sleep(double time) else if(sys_supportsdlgetticks && sys_usesdldelay.integer) Sys_SDL_Delay(microseconds / 1000); #if HAVE_SELECT - else + else if (cls.state == ca_dedicated && sv_checkforpacketsduringsleep.integer) { struct timeval tv; lhnetsocket_t *s; @@ -516,30 +516,27 @@ double Sys_Sleep(double time) int lastfd = -1; FD_ZERO(&fdreadset); - if (cls.state == ca_dedicated && sv_checkforpacketsduringsleep.integer) + List_For_Each_Entry(s, &lhnet_socketlist.list, lhnetsocket_t, list) { - List_For_Each_Entry(s, &lhnet_socketlist.list, lhnetsocket_t, list) + if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6) { - if (s->address.addresstype == LHNETADDRESSTYPE_INET4 || s->address.addresstype == LHNETADDRESSTYPE_INET6) - { - if (lastfd < s->inetsocket) - lastfd = s->inetsocket; + if (lastfd < s->inetsocket) + lastfd = s->inetsocket; #if defined(WIN32) && !defined(_MSC_VER) - FD_SET((int)s->inetsocket, &fdreadset); + FD_SET((int)s->inetsocket, &fdreadset); #else - FD_SET((unsigned int)s->inetsocket, &fdreadset); + FD_SET((unsigned int)s->inetsocket, &fdreadset); #endif - } } } tv.tv_sec = microseconds / 1000000; tv.tv_usec = microseconds % 1000000; // on Win32, select() cannot be used with all three FD list args being NULL according to MSDN - // (so much for POSIX...) - // bones_was_here: but a zeroed fd_set seems to be tolerated (tested on Win 7) + // (so much for POSIX...), not with an empty fd_set either. select(lastfd + 1, &fdreadset, NULL, NULL, &tv); } -#elif HAVE_USLEEP +#endif +#if HAVE_USLEEP else usleep(microseconds); #elif HAVE_Sleep -- 2.39.2