2013-04-04 Koichiro KIKUCHI <koichiro@rworks.jp>

* windows_service.cc,
	  pandora_windows_service.cc,
	  windows_service.h: Set iteration base ticks after sleeping startup_delay
	 to exclude time taken by startup_delay from sleep time subtraction.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7928 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
koichirok 2013-04-04 08:06:38 +00:00
parent 7b5e63b850
commit a1b1afde7e
4 changed files with 35 additions and 10 deletions

View File

@ -1,3 +1,10 @@
2013-04-04 Koichiro KIKUCHI <koichiro@rworks.jp>
* windows_service.cc,
pandora_windows_service.cc,
windows_service.h: Set iteration base ticks after sleeping startup_delay
to exclude time taken by startup_delay from sleep time subtraction.
2013-04-03 Koichiro KIKUCHI <koichiro@rworks.jp>
* windows_service.cc: Fixed typo of my previous commit.

View File

@ -1629,8 +1629,6 @@ void
Pandora_Windows_Service::pandora_run_broker (string config) {
Pandora_Agent_Conf *conf = NULL;
string server_addr;
int startup_delay = 0;
static unsigned char delayed = 0;
unsigned char data_flag = 0;
unsigned char intensive_match;
@ -1725,7 +1723,7 @@ Pandora_Windows_Service::pandora_run () {
string server_addr, conf_file, *all_conf;
int startup_delay = 0;
int i, num;
static unsigned char delayed = 0;
static bool startup = true;
unsigned char data_flag = 0;
unsigned char intensive_match;
@ -1733,13 +1731,17 @@ Pandora_Windows_Service::pandora_run () {
conf = this->getConf ();
/* process only once at startup */
if (startup) {
startup = false;
/* Sleep if a startup delay was specified */
startup_delay = atoi (conf->getValue ("startup_delay").c_str ()) * 1000;
if (startup_delay > 0 && delayed == 0) {
delayed = 1;
if (startup_delay > 0) {
pandoraLog ("Delaying startup %d miliseconds", startup_delay);
Sleep (startup_delay);
}
setIterationBaseTicks(GetTickCount());
}
/* Set the run time */
this->run_time = time (NULL);

View File

@ -112,10 +112,11 @@ Windows_Service::setInitFunction (void (Windows_Service::*f) ()) {
void
Windows_Service::execRunFunction () {
int sleep_time_remain;
DWORD tickbase, ticknow;
DWORD ticknow;
if (run_function != NULL) {
tickbase = GetTickCount();
// init here
setIterationBaseTicks(GetTickCount());
do {
(this->*run_function) ();
@ -127,14 +128,14 @@ Windows_Service::execRunFunction () {
// Subtract time taken by run_funtion() from sleep_time
// to *start* each iteration with the same interval
sleep_time_remain = sleep_time - (ticknow - tickbase);
sleep_time_remain = sleep_time - (ticknow - iter_base_ticks);
if (0 <= sleep_time_remain && sleep_time_remain <= sleep_time) {
tickbase += sleep_time;
setIterationBaseTicks(iter_base_ticks + 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
setIterationBaseTicks(ticknow); // use current ticks as base ticks
}
} while (WaitForSingleObject (stop_event, sleep_time_remain) != WAIT_OBJECT_0);
}
@ -175,6 +176,19 @@ Windows_Service::setSleepTime (unsigned int s) {
current_service->sleep_time = sleep_time;
}
/**
* Set the base tick count.
*
* This ticks used to start each iteration with the same
* interval (this->sleep_time)
* @param ticks base tick count..
*/
void
Windows_Service::setIterationBaseTicks(DWORD ticks) {
iter_base_ticks = ticks;
current_service->iter_base_ticks = ticks;
}
/**
* Install the service in the Windows services system.
*

View File

@ -42,6 +42,7 @@ protected:
private:
HANDLE stop_event;
int sleep_time;
DWORD iter_base_ticks;
SC_HANDLE sc_service;
void (Windows_Service::*run_function) ();
@ -62,6 +63,7 @@ public:
void setInitFunction (void (Windows_Service::*f) ());
LPSTR getServiceName ();
void setSleepTime (unsigned int s);
void setIterationBaseTicks(DWORD ticks);
void execRunFunction ();
void execInitFunction ();