From 47e10eaac9fa09a76df116a1f8859f38c2549fab Mon Sep 17 00:00:00 2001
From: Albert Graef <aggraef@gmail.com>
Date: Fri, 23 Oct 2020 21:52:34 +0200
Subject: [PATCH] Backport some fixes to sys_domicrosleep from vanilla.

Massaged the routine a bit to bring it more in line with what vanilla
currently has, and backported some recent changes in rev. 1f383be3 and
rev. ff9e52c6 from vanilla which are supposed to fix some bugs in
sys_domicrosleep.
---
 pd/src/s_inter.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/pd/src/s_inter.c b/pd/src/s_inter.c
index f1e6cabbd..a712dba3a 100644
--- a/pd/src/s_inter.c
+++ b/pd/src/s_inter.c
@@ -195,6 +195,7 @@ double sys_getrealtime(void)
 }
 
 extern int sys_nosleep;
+static int fdschanged;
 
 static int sys_domicrosleep(int microsec, int pollem)
 {
@@ -203,7 +204,7 @@ static int sys_domicrosleep(int microsec, int pollem)
     t_fdpoll *fp;
     timout.tv_sec = 0;
     timout.tv_usec = (sys_nosleep ? 0 : microsec);
-    if (pollem)
+    if (pollem && sys_nfdpoll)
     {
         fd_set readset, writeset, exceptset;
         FD_ZERO(&writeset);
@@ -216,8 +217,10 @@ static int sys_domicrosleep(int microsec, int pollem)
                 Sleep(microsec/1000);
         else
 #endif
-        select(sys_maxfd+1, &readset, &writeset, &exceptset, &timout);
-        for (i = 0; i < sys_nfdpoll; i++)
+        if (select(sys_maxfd+1, &readset, &writeset, &exceptset, &timout) < 0)
+          perror("microsleep select");
+        fdschanged = 0;
+        for (i = 0; i < sys_nfdpoll && !fdschanged; i++)
             if (FD_ISSET(sys_fdpoll[i].fdp_fd, &readset))
         {
 #ifdef THREAD_LOCKING
@@ -229,18 +232,23 @@ static int sys_domicrosleep(int microsec, int pollem)
 #endif
             didsomething = 1;
         }
-        return (didsomething);
+        if (didsomething) return (1);
     }
-    else
+    if (microsec)
     {
+#ifdef THREAD_LOCKING
+        sys_unlock();
+#endif
 #ifdef MSW
-        if (sys_maxfd == 0)
-              Sleep(microsec/1000);
-        else
+        Sleep(microsec/1000);
+#else
+        usleep(microsec);
+#endif
+#ifdef THREAD_LOCKING
+        sys_lock();
 #endif
-        select(0, 0, 0, 0, &timout);
-        return (0);
     }
+    return (0);
 }
 
 void sys_microsleep(int microsec)
@@ -438,6 +446,7 @@ void sys_addpollfn(int fd, t_fdpollfn fn, void *ptr)
     fp->fdp_ptr = ptr;
     sys_nfdpoll = nfd + 1;
     if (fd >= sys_maxfd) sys_maxfd = fd + 1;
+    fdschanged = 1;
 }
 
 void sys_rmpollfn(int fd)
@@ -445,6 +454,7 @@ void sys_rmpollfn(int fd)
     int nfd = sys_nfdpoll;
     int i, size = nfd * sizeof(t_fdpoll);
     t_fdpoll *fp;
+    fdschanged = 1;
     for (i = nfd, fp = sys_fdpoll; i--; fp++)
     {
         if (fp->fdp_fd == fd)
@@ -624,6 +634,7 @@ void socketreceiver_read(t_socketreceiver *x, int fd)
 void sys_closesocket(int fd)
 {
 #ifdef HAVE_UNISTD_H
+    if (fd<0) return;
     close(fd);
 #endif
 #ifdef MSW
-- 
GitLab