From d3f8472f86277e90c086cda99373c8bbb706ed9d Mon Sep 17 00:00:00 2001 From: koichirok Date: Wed, 3 Apr 2013 11:15:07 +0000 Subject: [PATCH] 2013-04-03 Koichiro KIKUCHI * windows_service.cc: Adjust sleep time so that each iteration *starts" with the same interval. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7921 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_agents/win32/ChangeLog | 5 +++++ pandora_agents/win32/windows_service.cc | 29 ++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index 95063beea4..f918f53dbc 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,8 @@ +2013-04-03 Koichiro KIKUCHI + + * windows_service.cc: Adjust sleep time so that each iteration *starts" + with the same interval. + 2013-03-12 Ramon Novoa * bin/util/pandora_revent.exe: Added to repository. Compiled version of diff --git a/pandora_agents/win32/windows_service.cc b/pandora_agents/win32/windows_service.cc index cd601eec07..7af9190b8b 100644 --- a/pandora_agents/win32/windows_service.cc +++ b/pandora_agents/win32/windows_service.cc @@ -111,13 +111,32 @@ Windows_Service::setInitFunction (void (Windows_Service::*f) ()) { */ void Windows_Service::execRunFunction () { + int sleep_time_remain; + DWORD tickbase, ticknow; + if (run_function != NULL) { - (this->*run_function) (); - if (sleep_time > 0) { - while (WaitForSingleObject (stop_event, sleep_time) != WAIT_OBJECT_0) { - (this->*run_function) (); + tickbase = GetTickCount(); + + do { + (this->*run_function) (); + + if (sleep_time > 0) + break; + + ticknow = GetTickCount(); + + // Subtract time taken by run_funtion() from sleep_time + // to *start* each iteration with the same interval + sleep_time_remain = sleep_time - (ticknow - tickbase); + + if (0 <= sleep_time_remain && sleep_time_remain <= sleep_time) { + tickbase += sleep_time; + } else { + // run_function() took more than "sleep_time", or something goes wrong. + sleep_time_remain = 0; // don't sleep + tickbase = ticknow; // use current ticks as base ticks } - } + } while (WaitForSingleObject (stop_event, sleep_time_remain) != WAIT_OBJECT_0); } }