From 2657f9c557e7e078039aa0302926edd7ed89930f Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Wed, 15 Mar 2017 12:33:15 +0900 Subject: [PATCH] Remove single-threading option For code simplicity, the single-threading option is removed. Change-Id: I0a7526cf85d992f93517d6bd10203f7a38bd0f25 Signed-off-by: Mu-Woong Lee --- include/ServiceBase.h | 3 -- src/server/ServiceBase.cpp | 89 +++++++++++++++++----------------------------- 2 files changed, 32 insertions(+), 60 deletions(-) diff --git a/include/ServiceBase.h b/include/ServiceBase.h index b70daaa..1274db0 100644 --- a/include/ServiceBase.h +++ b/include/ServiceBase.h @@ -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 __clients; - - static bool __singleThreading; }; } diff --git a/src/server/ServiceBase.cpp b/src/server/ServiceBase.cpp index 4d7da12..e769944 100644 --- a/src/server/ServiceBase.cpp +++ b/src/server/ServiceBase.cpp @@ -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("" + __methodSpecs + ""; @@ -338,8 +318,3 @@ void ServiceBase::__unwatch(unsigned int watchId) { g_dbus_connection_signal_unsubscribe(__connection, watchId); } - -void ServiceBase::setSingleThreading() -{ - __singleThreading = true; -} -- 2.7.4