using namespace ctx;
+bool ServiceBase::__singleThreading = false;
+
ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
__serviceName(serviceName),
__mainContext(NULL),
{
_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)
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>";
{
g_dbus_connection_signal_unsubscribe(__connection, watchId);
}
+
+void ServiceBase::setSingleThreading()
+{
+ __singleThreading = true;
+}