Add a runtime switch to turn on the single threading mode 48/115548/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 20 Feb 2017 09:26:20 +0000 (18:26 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 20 Feb 2017 09:26:20 +0000 (18:26 +0900)
Change-Id: I3981d4136feb42a7054e1a8bd8b984b00492076d
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/ServiceBase.h
src/server/ServiceBase.cpp

index 9a0bae5..cb12256 100644 (file)
@@ -42,6 +42,8 @@ namespace ctx {
 
                uid_t getActiveUser();
 
+               static void setSingleThreading();
+
        protected:
                ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs);
 
@@ -104,6 +106,8 @@ namespace ctx {
                };
 
                std::map<std::string, ClientInfo> __clients;
+
+               static bool __singleThreading;
        };
 
 }
index b79c986..122c200 100644 (file)
@@ -22,6 +22,8 @@
 
 using namespace ctx;
 
+bool ServiceBase::__singleThreading = false;
+
 ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
        __serviceName(serviceName),
        __mainContext(NULL),
@@ -53,39 +55,39 @@ bool ServiceBase::start()
 {
        _I("Preparing '%s'", __serviceName);
 
-#ifndef SINGLE_THREADING
-       __gthread = g_thread_new(__serviceName, __threadFunc, this);
-       IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
-#else
-       _I(CYAN("'%s' runs on the default main loop"), __serviceName);
-       IF_FAIL_RETURN(__init(), false);
-#endif
+       if (__singleThreading) {
+               _I(CYAN("'%s' runs on the global main loop"), __serviceName);
+               IF_FAIL_RETURN(__init(), false);
+       } else {
+               __gthread = g_thread_new(__serviceName, __threadFunc, this);
+               IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
+       }
 
        return true;
 }
 
 void ServiceBase::stop()
 {
-#ifndef SINGLE_THREADING
-       if (__mainLoop && g_main_loop_is_running(__mainLoop)) {
-               GSource *gSrc = g_idle_source_new();
-               if (gSrc) {
-                       // Tries to stop the main loop within its thread.
-                       // In this way, already scheduled idle tasks are not discarded.
-                       g_source_set_callback(gSrc, __stopMainLoop, this, NULL);
-                       g_source_attach(gSrc, __mainContext);
-               } else {
-                       __stopMainLoop(this);
+       if (__singleThreading) {
+               __release();
+       } else {
+               if (__mainLoop && g_main_loop_is_running(__mainLoop)) {
+                       GSource *gSrc = g_idle_source_new();
+                       if (gSrc) {
+                               // Tries to stop the main loop within its thread.
+                               // In this way, already scheduled idle tasks are not discarded.
+                               g_source_set_callback(gSrc, __stopMainLoop, this, NULL);
+                               g_source_attach(gSrc, __mainContext);
+                       } else {
+                               __stopMainLoop(this);
+                       }
                }
-       }
 
-       if (__gthread) {
-               _I("Joining the thread of '%s'", __serviceName);
-               g_thread_join(__gthread);
+               if (__gthread) {
+                       _I("Joining the thread of '%s'", __serviceName);
+                       g_thread_join(__gthread);
+               }
        }
-#else
-       __release();
-#endif
 }
 
 gboolean ServiceBase::__stopMainLoop(gpointer data)
@@ -147,15 +149,15 @@ bool ServiceBase::__init()
        vtable.get_property = NULL;
        vtable.set_property = NULL;
 
-#ifndef SINGLE_THREADING
-       __mainContext = g_main_context_new();
-       IF_FAIL_RETURN_TAG(__mainContext, false, _E, "MainContext creation failed");
+       if (!__singleThreading) {
+               __mainContext = g_main_context_new();
+               IF_FAIL_RETURN_TAG(__mainContext, false, _E, "MainContext creation failed");
 
-       g_main_context_push_thread_default(__mainContext);
+               g_main_context_push_thread_default(__mainContext);
 
-       __mainLoop = g_main_loop_new(__mainContext, FALSE);
-       IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "MainLoop creation failed");
-#endif
+               __mainLoop = g_main_loop_new(__mainContext, FALSE);
+               IF_FAIL_RETURN_TAG(__mainLoop, false, _E, "MainLoop creation failed");
+       }
 
        std::string introspection("<node><interface name='");
        introspection += __interface + "'>" + __methodSpecs + "</interface></node>";
@@ -293,3 +295,8 @@ void ServiceBase::__unwatch(unsigned int watchId)
 {
        g_dbus_connection_signal_unsubscribe(__connection, watchId);
 }
+
+void ServiceBase::setSingleThreading()
+{
+       __singleThreading = true;
+}