]> source.dussan.org Git - tigervnc.git/commitdiff
More precise computing of time to spend in select(). This fixes the
authorConstantin Kaplinsky <const@tightvnc.com>
Thu, 16 Feb 2006 11:50:25 +0000 (11:50 +0000)
committerConstantin Kaplinsky <const@tightvnc.com>
Thu, 16 Feb 2006 11:50:25 +0000 (11:50 +0000)
problem with CPU underload with very small values of PollingCycle.

git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@490 3789f03b-4d11-0410-bbf8-ca57d06f2519

x0vncserver/x0vncserver.cxx

index b9a44ccafa16597c066a1d459484bd451984bae1..7b8c66098c3d5100a1ab513950618bcc20ff5aed 100644 (file)
@@ -401,17 +401,10 @@ int main(int argc, char** argv)
     int dynPollingCycle = (int)pollingCycle;
 
     TimeMillis timeSaved, timeNow;
+    fd_set rfds;
+    struct timeval tv;
 
     while (true) {
-      fd_set rfds;
-      struct timeval tv;
-
-      // FIXME: This seems to be wrong.
-      tv.tv_sec = 0;
-      tv.tv_usec = dynPollingCycle * 1000;
-      if (tv.tv_usec > 500000) {
-        tv.tv_usec = 500000;
-      }
 
       FD_ZERO(&rfds);
       FD_SET(listener.getFd(), &rfds);
@@ -419,10 +412,32 @@ int main(int argc, char** argv)
       std::list<Socket*> sockets;
       server.getSockets(&sockets);
       std::list<Socket*>::iterator i;
+      int clients_connected = 0;
       for (i = sockets.begin(); i != sockets.end(); i++) {
         FD_SET((*i)->getFd(), &rfds);
+        clients_connected++;
       }
 
+      if (clients_connected) {
+        int poll_ms = 20;
+        if (timeNow.update()) {
+          poll_ms = timeNow.diffFrom(timeSaved);
+        }
+        int wait_ms = dynPollingCycle - poll_ms;
+        if (wait_ms < 0) {
+          wait_ms = 0;
+        } else if (wait_ms > 500) {
+          wait_ms = 500;
+        }
+        tv.tv_usec = wait_ms * 1000;
+#ifdef DEBUG
+        fprintf(stderr, "[%d]\t", wait_ms);
+#endif
+      } else {
+        tv.tv_usec = 50000;
+      }
+      tv.tv_sec = 0;
+
       int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
       if (n < 0) {
         if (errno == EINTR) {