Remove single-threading option 65/118965/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Wed, 15 Mar 2017 03:33:15 +0000 (12:33 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Wed, 15 Mar 2017 03:33:15 +0000 (12:33 +0900)
For code simplicity, the single-threading option is removed.

Change-Id: I0a7526cf85d992f93517d6bd10203f7a38bd0f25
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/ServiceBase.h
src/server/ServiceBase.cpp

index b70daaa..1274db0 100644 (file)
@@ -38,7 +38,6 @@ namespace ctx {
                void notifyUserNew();
                void notifyUserRemoved();
                static void setActiveUser(uid_t uid);
-               static void setSingleThreading();
                // ---
 
                // If the GVariant 'param' is floating, it is consumed.
@@ -114,8 +113,6 @@ namespace ctx {
                };
 
                std::map<std::string, ClientInfo> __clients;
-
-               static bool __singleThreading;
        };
 
 }
index 4d7da12..e769944 100644 (file)
@@ -24,8 +24,6 @@ using namespace ctx;
 
 static std::atomic_uint __activeUid;
 
-bool ServiceBase::__singleThreading = false;
-
 ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
        __started(false),
        __threadRunning(false),
@@ -59,16 +57,11 @@ bool ServiceBase::start()
 
        _I("Preparing '%s'", __serviceName);
 
-       if (__singleThreading) {
-               _I(CYAN("'%s' runs on the global main loop"), __serviceName);
-               __started = __init();
-       } else {
-               __gthread = g_thread_new(__serviceName, __threadFunc, this);
-               IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
-               __started = true;
-       }
+       __gthread = g_thread_new(__serviceName, __threadFunc, this);
+       IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
 
-       return __started;
+       __started = true;
+       return true;
 }
 
 void ServiceBase::stop()
@@ -76,26 +69,21 @@ void ServiceBase::stop()
        IF_FAIL_VOID(__started);
        __started = false;
 
-       if (__singleThreading) {
-               __release();
+       IF_FAIL_VOID(__threadRunning.load());
+
+       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 {
-               if (__threadRunning.load()) {
-                       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);
-               }
+               __stopMainLoop(this);
        }
+
+       _I("Joining the thread of '%s'", __serviceName);
+       g_thread_join(__gthread);
+       __gthread = NULL;
 }
 
 gboolean ServiceBase::__stopMainLoop(gpointer data)
@@ -136,31 +124,25 @@ void ServiceBase::onUserDeactivated()
 void ServiceBase::notifyUserNew()
 {
        IF_FAIL_VOID(__started);
+       IF_FAIL_VOID(__threadRunning.load());
 
-       if (__threadRunning.load()) {
-               GSource* gSrc = g_idle_source_new();
-               IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
+       GSource* gSrc = g_idle_source_new();
+       IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
 
-               g_source_set_callback(gSrc, __onUserActivated, this, NULL);
-               g_source_attach(gSrc, __mainContext);
-       } else {
-               __onUserActivated(this);
-       }
+       g_source_set_callback(gSrc, __onUserActivated, this, NULL);
+       g_source_attach(gSrc, __mainContext);
 }
 
 void ServiceBase::notifyUserRemoved()
 {
        IF_FAIL_VOID(__started);
+       IF_FAIL_VOID(__threadRunning.load());
 
-       if (__threadRunning.load()) {
-               GSource* gSrc = g_idle_source_new();
-               IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
+       GSource* gSrc = g_idle_source_new();
+       IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
 
-               g_source_set_callback(gSrc, __onUserDeactivated, this, NULL);
-               g_source_attach(gSrc, __mainContext);
-       } else {
-               __onUserDeactivated(this);
-       }
+       g_source_set_callback(gSrc, __onUserDeactivated, this, NULL);
+       g_source_attach(gSrc, __mainContext);
 }
 
 gboolean ServiceBase::__onUserActivated(gpointer data)
@@ -211,15 +193,13 @@ bool ServiceBase::__init()
        vtable.get_property = NULL;
        vtable.set_property = NULL;
 
-       if (!__singleThreading) {
-               __mainContext = g_main_context_new();
-               IF_FAIL_RETURN_TAG(__mainContext, false, _E, "MainContext creation failed");
+       __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");
-       }
+       __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>";
@@ -338,8 +318,3 @@ void ServiceBase::__unwatch(unsigned int watchId)
 {
        g_dbus_connection_signal_unsubscribe(__connection, watchId);
 }
-
-void ServiceBase::setSingleThreading()
-{
-       __singleThreading = true;
-}