Add two parameterized constructors of Timer for specifying the main context 98/123498/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 6 Apr 2017 01:54:12 +0000 (10:54 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Thu, 6 Apr 2017 01:54:12 +0000 (10:54 +0900)
Normally, a Timer object will be used within a main context,
thus it is OK to create a Timer without specifying the main context.
However, in the case that a service creates more threads,
this parameterized constructors would be convenient.

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

index 1274db0..b9532ff 100644 (file)
@@ -43,6 +43,8 @@ namespace ctx {
                // If the GVariant 'param' is floating, it is consumed.
                void publish(const std::string& busName, const std::string& signalName, GVariant* param);
 
+               GMainContext* getMainContext();
+
                GDBusConnection* getConnection();
 
                static uid_t getActiveUser();
index 3cb867f..3e0d019 100644 (file)
 
 namespace ctx {
 
+       class ServiceBase;
        class ITimerListener;
 
        /* All timers expire in the context where the Timer object is created. */
        class EXPORT_API Timer {
        public:
                Timer();
+               Timer(ServiceBase* hostService);
+               Timer(GMainContext* mainContext);
                ~Timer();
 
                /* This is a helper function that chooses and executes one of the below three functions. */
index 4a24920..2a8585d 100644 (file)
@@ -46,6 +46,11 @@ ServiceBase::~ServiceBase()
 {
 }
 
+GMainContext* ServiceBase::getMainContext()
+{
+       return __mainContext;
+}
+
 GDBusConnection* ServiceBase::getConnection()
 {
        return __connection;
index 90217e7..7256509 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <atomic>
 #include <alarm.h>
+#include <ServiceBase.h>
 #include <ITimerListener.h>
 #include <Timer.h>
 
@@ -48,6 +49,18 @@ Timer::Timer() :
 {
 }
 
+Timer::Timer(GMainContext* mainContext) :
+       __context(mainContext)
+{
+       if (__context)
+               g_main_context_ref(__context);
+}
+
+Timer::Timer(ServiceBase* hostService) :
+       Timer(hostService->getMainContext())
+{
+}
+
 Timer::~Timer()
 {
        cancelAll();