aboutsummaryrefslogtreecommitdiffstats
path: root/src/Win32Main.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/Win32Main.cxx (renamed from src/main_win32.c)35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/main_win32.c b/src/Win32Main.cxx
index aac7ad886..0cd5e445b 100644
--- a/src/main_win32.c
+++ b/src/Win32Main.cxx
@@ -18,12 +18,15 @@
*/
#include "config.h"
-#include "main.h"
+#include "Main.hxx"
#ifdef WIN32
#include "mpd_error.h"
-#include "event_pipe.h"
+#include "GlobalEvents.hxx"
+
+#include <cstdlib>
+#include <atomic>
#include <glib.h>
@@ -32,7 +35,7 @@
static int service_argc;
static char **service_argv;
static char service_name[] = "";
-static BOOL ignore_console_events;
+static std::atomic_bool running;
static SERVICE_STATUS_HANDLE service_handle;
static void WINAPI
@@ -68,7 +71,7 @@ service_dispatcher(G_GNUC_UNUSED DWORD control, G_GNUC_UNUSED DWORD event_type,
switch (control) {
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
- event_pipe_emit(PIPE_EVENT_SHUTDOWN);
+ GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
return NO_ERROR;
default:
return NO_ERROR;
@@ -103,8 +106,22 @@ console_handler(DWORD event)
switch (event) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
- if (!ignore_console_events)
- event_pipe_emit(PIPE_EVENT_SHUTDOWN);
+ if (running.load()) {
+ // Recent msdn docs that process is terminated
+ // if this function returns TRUE.
+ // We initiate correct shutdown sequence (if possible).
+ // Once main() returns CRT will terminate our process
+ // regardless our thread is still active.
+ // If this did not happen within 3 seconds
+ // let's shutdown anyway.
+ GlobalEvents::Emit(GlobalEvents::SHUTDOWN);
+ // Under debugger it's better to wait indefinitely
+ // to allow debugging of shutdown code.
+ Sleep(IsDebuggerPresent() ? INFINITE : 3000);
+ }
+ // If we're not running main loop there is no chance for
+ // clean shutdown.
+ std::exit(EXIT_FAILURE);
return TRUE;
default:
return FALSE;
@@ -125,8 +142,8 @@ int win32_main(int argc, char *argv[])
error_code = GetLastError();
if (error_code == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
/* running as console app */
+ running.store(false);
SetConsoleTitle("Music Player Daemon");
- ignore_console_events = TRUE;
SetConsoleCtrlHandler(console_handler, TRUE);
return mpd_main(argc, argv);
}
@@ -140,7 +157,7 @@ void win32_app_started()
if (service_handle != 0)
service_notify_status(SERVICE_RUNNING);
else
- ignore_console_events = FALSE;
+ running.store(true);
}
void win32_app_stopping()
@@ -148,7 +165,7 @@ void win32_app_stopping()
if (service_handle != 0)
service_notify_status(SERVICE_STOP_PENDING);
else
- ignore_console_events = TRUE;
+ running.store(false);
}
#endif