X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=60a8db0b32cf43d9ca3cda01f6381754ad60e029;hb=b900e3b0859e71d5089c14314c0060082fc230de;hp=283cbea9792f96b2bae524f64cd1d480a4b38fb3;hpb=8d855fdbce4ba0b8cfeda551f137434731385c23;p=nssm.git diff --git a/service.cpp b/service.cpp index 283cbea..60a8db0 100644 --- a/service.cpp +++ b/service.cpp @@ -11,8 +11,8 @@ char flags[CMD_LENGTH]; char dir[MAX_PATH]; bool stopping; unsigned long throttle_delay; -CRITICAL_SECTION throttle_section; -CONDITION_VARIABLE throttle_condition; +HANDLE throttle_timer; +LARGE_INTEGER throttle_duetime; static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions; static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 }; @@ -209,7 +209,10 @@ void WINAPI service_main(unsigned long argc, char **argv) { set_service_recovery(service_name); /* Used for signalling a resume if the service pauses when throttled. */ - InitializeCriticalSection(&throttle_section); + throttle_timer = CreateWaitableTimer(0, 1, 0); + if (! throttle_timer) { + log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_CREATEWAITABLETIMER_FAILED, service_name, error_string(GetLastError()), 0); + } monitor_service(); } @@ -259,8 +262,10 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon return NO_ERROR; case SERVICE_CONTROL_CONTINUE: + if (! throttle_timer) return ERROR_CALL_NOT_IMPLEMENTED; throttle = 0; - WakeConditionVariable(&throttle_condition); + ZeroMemory(&throttle_duetime, sizeof(throttle_duetime)); + SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0); service_status.dwCurrentState = SERVICE_CONTINUE_PENDING; service_status.dwWaitHint = throttle_milliseconds() + NSSM_WAITHINT_MARGIN; log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service_name, 0); @@ -454,12 +459,15 @@ void throttle_restart() { _snprintf(milliseconds, sizeof(milliseconds), "%d", ms); log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0); - EnterCriticalSection(&throttle_section); + if (throttle_timer) { + ZeroMemory(&throttle_duetime, sizeof(throttle_duetime)); + throttle_duetime.QuadPart = 0 - (ms * 10000LL); + SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0); + } service_status.dwCurrentState = SERVICE_PAUSED; SetServiceStatus(service_handle, &service_status); - SleepConditionVariableCS(&throttle_condition, &throttle_section, ms); - - LeaveCriticalSection(&throttle_section); + if (throttle_timer) WaitForSingleObject(throttle_timer, INFINITE); + else Sleep(ms); }