From: Constantin Kaplinsky Date: Thu, 16 Feb 2006 11:50:25 +0000 (+0000) Subject: More precise computing of time to spend in select(). This fixes the X-Git-Tag: v0.0.90~384^2~345 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f56fa75f717616d972eb132d461ca12d33d7d0a;p=tigervnc.git More precise computing of time to spend in select(). This fixes the 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 --- diff --git a/x0vncserver/x0vncserver.cxx b/x0vncserver/x0vncserver.cxx index b9a44cca..7b8c6609 100644 --- a/x0vncserver/x0vncserver.cxx +++ b/x0vncserver/x0vncserver.cxx @@ -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 sockets; server.getSockets(&sockets); std::list::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) {