From: dv1tas Date: Fri, 14 Jun 2013 08:54:49 +0000 (+0000) Subject: Fix jitter in computing one-second interval X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea15ac2e17b572bfaba79d4a8e7136f6cba95c63;p=people%2Fms%2Fmstpd.git Fix jitter in computing one-second interval Signed-off-by: Jorge Gastón Signed-off-by: Vitalii Demianets git-svn-id: svn://svn.code.sf.net/p/mstpd/code/trunk@45 fbe50366-0c72-4402-a84b-5d246361dba7 --- diff --git a/epoll_loop.c b/epoll_loop.c index d5b3b01..e8c0556 100644 --- a/epoll_loop.c +++ b/epoll_loop.c @@ -94,7 +94,6 @@ static inline int time_diff(struct timeval *second, struct timeval *first) static inline void run_timeouts(void) { bridge_one_second(); - gettimeofday(&nexttimeout, NULL); ++(nexttimeout.tv_sec); } @@ -116,6 +115,22 @@ int epoll_main_loop(void) if(timeout < 0 || timeout > 1000) { run_timeouts(); + /* + * Check if system time has changed. + * NOTE: we can not differentiate reliably if system + * time has changed or we have spent too much time + * inside event handlers and run_timeouts(). + * Fix: use clock_gettime(CLOCK_MONOTONIC, ) instead of + * gettimeofday, if it is available. + * If it is not available on given system - + * the following is the best we can do. + */ + if(timeout < -4000 || timeout > 1000) + { + /* Most probably, system time has changed */ + nexttimeout.tv_usec = tv.tv_usec; + nexttimeout.tv_sec = tv.tv_sec + 1; + } timeout = 0; }