Fix pre-Vista compatibility.
authorIain Patterson <me@iain.cx>
Mon, 28 Feb 2011 11:48:11 +0000 (11:48 +0000)
committerIain Patterson <me@iain.cx>
Mon, 28 Feb 2011 11:48:11 +0000 (11:48 +0000)
SleepConditionVariableCS() isn't available on Windows versions
prior to Vista.  Use a waitable timer instead.

service.cpp

index 283cbea..4f7ff03 100644 (file)
@@ -11,8 +11,8 @@ char flags[CMD_LENGTH];
 char dir[MAX_PATH];\r
 bool stopping;\r
 unsigned long throttle_delay;\r
-CRITICAL_SECTION throttle_section;\r
-CONDITION_VARIABLE throttle_condition;\r
+HANDLE throttle_timer;\r
+LARGE_INTEGER throttle_duetime;\r
 \r
 static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions;\r
 static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 };\r
@@ -209,7 +209,7 @@ void WINAPI service_main(unsigned long argc, char **argv) {
   set_service_recovery(service_name);\r
 \r
   /* Used for signalling a resume if the service pauses when throttled. */\r
-  InitializeCriticalSection(&throttle_section);\r
+  throttle_timer = CreateWaitableTimer(0, 1, 0);\r
 \r
   monitor_service();\r
 }\r
@@ -260,7 +260,8 @@ unsigned long WINAPI service_control_handler(unsigned long control, unsigned lon
 \r
     case SERVICE_CONTROL_CONTINUE:\r
       throttle = 0;\r
-      WakeConditionVariable(&throttle_condition);\r
+      ZeroMemory(&throttle_duetime, sizeof(throttle_duetime));\r
+      SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0);\r
       service_status.dwCurrentState = SERVICE_CONTINUE_PENDING;\r
       service_status.dwWaitHint = throttle_milliseconds() + NSSM_WAITHINT_MARGIN;\r
       log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service_name, 0);\r
@@ -454,12 +455,12 @@ void throttle_restart() {
   _snprintf(milliseconds, sizeof(milliseconds), "%d", ms);\r
   log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);\r
 \r
-  EnterCriticalSection(&throttle_section);\r
+  ZeroMemory(&throttle_duetime, sizeof(throttle_duetime));\r
+  throttle_duetime.QuadPart = 0 - (ms * 10000);\r
+  SetWaitableTimer(throttle_timer, &throttle_duetime, 0, 0, 0, 0);\r
 \r
   service_status.dwCurrentState = SERVICE_PAUSED;\r
   SetServiceStatus(service_handle, &service_status);\r
 \r
-  SleepConditionVariableCS(&throttle_condition, &throttle_section, ms);\r
-\r
-  LeaveCriticalSection(&throttle_section);\r
+  WaitForSingleObject(throttle_timer, INFINITE);\r
 }\r