From 5ca640d7632a5f2cdc7a3f8241d079584787d97f Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 20 Feb 2017 15:07:07 +0900 Subject: [PATCH] context-service terminates in its own mainloop, not in the signal handler Change-Id: Id77b88750434f9ecd174efb9a47ea203a5133a56 Signed-off-by: Mu-Woong Lee --- src/Main.cpp | 52 ++++++++++++++++++++++++--------------------------- src/ServiceLoader.cpp | 5 +++++ src/ServiceLoader.h | 2 ++ 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index dcb7477..dba5144 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -27,54 +27,53 @@ using namespace ctx; namespace { class Server { public: - Server(); - ~Server(); - bool start(); - void stop(); + static bool start(); + static void stop(); private: - GMainLoop* __mainloop; + Server() {} + static GMainLoop* __mainLoop; }; } -::Server::Server() : - __mainloop(NULL) +static gboolean __stopService(gpointer data) { - __mainloop = g_main_loop_new(NULL, FALSE); - IF_FAIL_VOID_TAG(__mainloop, _E, "Memory allocation failed"); + _I("Unloading services"); + ServiceLoader::unload(); + ::Server::stop(); + return G_SOURCE_REMOVE; } -::Server::~Server() -{ - if (__mainloop) - g_main_loop_unref(__mainloop); -} +GMainLoop* ::Server::__mainLoop = NULL; bool ::Server::start() { + __mainLoop = g_main_loop_new(NULL, FALSE); + IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "Memory allocation failed"); + _I(CYAN("Starting...")); + g_main_loop_run(__mainLoop); + + g_main_loop_unref(__mainLoop); - IF_FAIL_RETURN(__mainloop, false); - g_main_loop_run(__mainloop); return true; } void ::Server::stop() { _I(PURPLE("Terminating...")); - - if (g_main_loop_is_running(__mainloop)) - g_main_loop_quit(__mainloop); + g_main_loop_quit(__mainLoop); } -static ::Server __server; static AlarmInitializer __alarmInit; static void __signalHandler(int signum) { _I(YELLOW("SIGNAL-%d: '%s'"), signum, strsignal(signum)); - _I("Unloading services"); - ServiceLoader::unload(); - __server.stop(); + static bool terminated = false; + if (!terminated) { + g_idle_add(__stopService, NULL); + terminated = true; + } } static void __busAcquired(GDBusConnection* conn) @@ -88,14 +87,11 @@ static void __busAcquired(GDBusConnection* conn) } _E(RED("No service loaded.")); - // FIXME: __server.stop(); } static void __busLost(GDBusConnection* conn) { - _I("Unloading services"); - ServiceLoader::unload(); - __server.stop(); + __stopService(NULL); } int main(int argc, char* argv[]) @@ -112,7 +108,7 @@ int main(int argc, char* argv[]) DBusConnector dbusConnector(__busAcquired, __busLost); - __server.start(); + ::Server::start(); return EXIT_SUCCESS; } diff --git a/src/ServiceLoader.cpp b/src/ServiceLoader.cpp index 6cd65d1..bd33e0b 100644 --- a/src/ServiceLoader.cpp +++ b/src/ServiceLoader.cpp @@ -25,6 +25,10 @@ using namespace ctx; std::vector ServiceLoader::__services; +ServiceLoader::ServiceLoader() +{ +} + bool ServiceLoader::load(GDBusConnection* conn) { //__load(conn); @@ -38,6 +42,7 @@ bool ServiceLoader::load(GDBusConnection* conn) void ServiceLoader::unload() { for (auto& svc : __services) { + svc->stop(); delete svc; } __services.clear(); diff --git a/src/ServiceLoader.h b/src/ServiceLoader.h index b230fa7..63a5448 100644 --- a/src/ServiceLoader.h +++ b/src/ServiceLoader.h @@ -29,6 +29,8 @@ namespace ctx { static void unload(); private: + ServiceLoader(); + static std::vector __services; template static void __load(GDBusConnection* conn) -- 2.7.4