From 17fb1254fc9862c991a5c4528b73a4f274dad418 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Thu, 18 Sep 2014 21:30:09 +0300 Subject: [PATCH] server/main: Better main loop timing --- src/server/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/server/main.cpp b/src/server/main.cpp index 378e9d3..e3c60ae 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -111,9 +111,14 @@ int main(int argc, char *argv[]) set_ bad_fds; size_t num_consequent_valid_selects = 0; while(!g_sigint_received){ + uint64_t current_us = get_timeofday_us(); + int64_t delay_us = next_tick_us - current_us; + if(delay_us < 0) + delay_us = 0; + struct timeval tv; tv.tv_sec = 0; - tv.tv_usec = t_per_tick; + tv.tv_usec = delay_us; fd_set rfds; FD_ZERO(&rfds); @@ -179,7 +184,6 @@ int main(int argc, char *argv[]) attempt_bad_fds.clear(); } - uint64_t current_us = get_timeofday_us(); if(current_us >= next_tick_us){ next_tick_us += t_per_tick; if(next_tick_us < current_us - 1000 * 1000){