static std::atomic_uint __activeUid;
-bool ServiceBase::__singleThreading = false;
-
ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
__started(false),
__threadRunning(false),
_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()
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)
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)
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>";
{
g_dbus_connection_signal_unsubscribe(__connection, watchId);
}
-
-void ServiceBase::setSingleThreading()
-{
- __singleThreading = true;
-}