Change-Id: I2033801f35699fc07ce24fb8c9a424a926a3f39a
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
#ifndef __CONTEXT_SERVICE_BASE_H__
#define __CONTEXT_SERVICE_BASE_H__
#ifndef __CONTEXT_SERVICE_BASE_H__
#define __CONTEXT_SERVICE_BASE_H__
#include <string>
#include <map>
#include <ContextTypes.h>
#include <string>
#include <map>
#include <ContextTypes.h>
unsigned int __watch(const std::string& busName, ClientBase* client);
void __unwatch(unsigned int watchId);
unsigned int __watch(const std::string& busName, ClientBase* client);
void __unwatch(unsigned int watchId);
+ bool __started;
+ std::atomic_bool __threadRunning;
const char* __serviceName;
GMainContext* __mainContext;
const char* __serviceName;
GMainContext* __mainContext;
bool ServiceBase::__singleThreading = false;
ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
bool ServiceBase::__singleThreading = false;
ServiceBase::ServiceBase(GDBusConnection* conn, const char* serviceName, const char* methodSpecs) :
+ __started(false),
+ __threadRunning(false),
__serviceName(serviceName),
__mainContext(NULL),
__mainLoop(NULL),
__serviceName(serviceName),
__mainContext(NULL),
__mainLoop(NULL),
bool ServiceBase::start()
{
bool ServiceBase::start()
{
- IF_FAIL_RETURN(!__running, true);
- __running = true;
+ IF_FAIL_RETURN(!__started, true);
_I("Preparing '%s'", __serviceName);
if (__singleThreading) {
_I(CYAN("'%s' runs on the global main loop"), __serviceName);
_I("Preparing '%s'", __serviceName);
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");
} else {
__gthread = g_thread_new(__serviceName, __threadFunc, this);
IF_FAIL_RETURN_TAG(__gthread, false, _E, "Thread creation failed");
}
void ServiceBase::stop()
{
}
void ServiceBase::stop()
{
- IF_FAIL_VOID(__running);
- __running = false;
+ IF_FAIL_VOID(__started);
+ __started = false;
if (__singleThreading) {
__release();
} else {
if (__singleThreading) {
__release();
} else {
- if (__mainLoop && g_main_loop_is_running(__mainLoop)) {
+ if (__threadRunning.load()) {
GSource *gSrc = g_idle_source_new();
if (gSrc) {
// Tries to stop the main loop within its thread.
GSource *gSrc = g_idle_source_new();
if (gSrc) {
// Tries to stop the main loop within its thread.
void ServiceBase::notifyUserNew()
{
void ServiceBase::notifyUserNew()
{
- GSource* gSrc = g_idle_source_new();
- IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
+ IF_FAIL_VOID(__started);
- g_source_set_callback(gSrc, __onUserActivated, this, NULL);
- g_source_attach(gSrc, __mainContext);
+ if (__threadRunning.load()) {
+ 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);
+ }
}
void ServiceBase::notifyUserRemoved()
{
}
void ServiceBase::notifyUserRemoved()
{
- GSource* gSrc = g_idle_source_new();
- IF_FAIL_VOID_TAG(gSrc, _E, "Memory allocation failed");
+ IF_FAIL_VOID(__started);
+
+ if (__threadRunning.load()) {
+ 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);
+ g_source_set_callback(gSrc, __onUserDeactivated, this, NULL);
+ g_source_attach(gSrc, __mainContext);
+ } else {
+ __onUserDeactivated(this);
+ }
}
gboolean ServiceBase::__onUserActivated(gpointer data)
}
gboolean ServiceBase::__onUserActivated(gpointer data)
void ServiceBase::__run()
{
if (!__init()) {
void ServiceBase::__run()
{
if (!__init()) {
+ _E("Starting '%s' failed", __serviceName);
+ __threadRunning.store(true);
+
_I(CYAN("Starting '%s'"), __serviceName);
g_main_loop_run(__mainLoop);
_I(CYAN("Starting '%s'"), __serviceName);
g_main_loop_run(__mainLoop);
+ __threadRunning.store(false);
+