context-service terminates in its own mainloop, not in the signal handler 74/115474/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 20 Feb 2017 06:07:07 +0000 (15:07 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 20 Feb 2017 06:07:07 +0000 (15:07 +0900)
Change-Id: Id77b88750434f9ecd174efb9a47ea203a5133a56
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/Main.cpp
src/ServiceLoader.cpp
src/ServiceLoader.h

index dcb7477..dba5144 100644 (file)
@@ -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;
 }
index 6cd65d1..bd33e0b 100644 (file)
@@ -25,6 +25,10 @@ using namespace ctx;
 
 std::vector<DBusService*> ServiceLoader::__services;
 
+ServiceLoader::ServiceLoader()
+{
+}
+
 bool ServiceLoader::load(GDBusConnection* conn)
 {
        //__load<AppHistoryService>(conn);
@@ -38,6 +42,7 @@ bool ServiceLoader::load(GDBusConnection* conn)
 void ServiceLoader::unload()
 {
        for (auto& svc : __services) {
+               svc->stop();
                delete svc;
        }
        __services.clear();
index b230fa7..63a5448 100644 (file)
@@ -29,6 +29,8 @@ namespace ctx {
                static void unload();
 
        private:
+               ServiceLoader();
+
                static std::vector<DBusService*> __services;
 
                template<typename ServiceType> static void __load(GDBusConnection* conn)