From 514717c867d38ea0393ff395b01c25c218a47f5c Mon Sep 17 00:00:00 2001 From: ChaJiwon Date: Thu, 9 Jul 2015 17:08:15 +0900 Subject: [PATCH] Add destroy function into primitive service timer When ExpiryTimer's destructor called, ExpiryTimer requests destroy to ExpiryTimer_impl. ExpiryTimer_impl makes a decision for destroying instance or not. Change-Id: Ib93dec7ff76ef96923bf34f5e645a8d0c2b5dfdc Signed-off-by: ChaJiwon Reviewed-on: https://gerrit.iotivity.org/gerrit/1593 Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../modules/common/expiryTimer/src/ExpiryTimer.cpp | 3 ++- .../common/expiryTimer/src/ExpiryTimer_Impl.cpp | 30 ++++++++++++++++++---- .../common/expiryTimer/src/ExpiryTimer_Impl.h | 14 +++++----- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer.cpp b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer.cpp index e4279b9..97c1e4f 100644 --- a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer.cpp +++ b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer.cpp @@ -34,10 +34,11 @@ ExpiryTimer::~ExpiryTimer() { timerPtr->cancelTimer(it); } + timerPtr->destroy(); mTimerIDList.clear(); } -ExpiryTimer::TimerID ExpiryTimer::postTimer(long long sec, TimerCB cb) +ExpiryTimer::TimerID ExpiryTimer::postTimer(DelayMilliSec sec, TimerCB cb) { TimerID retID = 0; diff --git a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.cpp b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.cpp index 99d9a58..053243b 100644 --- a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.cpp +++ b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.cpp @@ -25,8 +25,8 @@ #include #include -ExpiryTimer_Impl* ExpiryTimer_Impl::s_instance; -std::once_flag ExpiryTimer_Impl::mflag; +ExpiryTimer_Impl* ExpiryTimer_Impl::s_instance = nullptr; +std::once_flag* ExpiryTimer_Impl::mflag = new std::once_flag; ExpiryTimer_Impl::ExpiryTimer_Impl() { @@ -35,11 +35,29 @@ ExpiryTimer_Impl::ExpiryTimer_Impl() ExpiryTimer_Impl::~ExpiryTimer_Impl() { + check.detach(); + delete mflag; + s_instance = nullptr; + delete s_instance; } +void ExpiryTimer_Impl::destroy() +{ + if(mTimerCBList.empty()) + { + try + { + s_instance->~ExpiryTimer_Impl(); + } + catch(std::exception &e) + { + std::cout << e.what(); + } + } +} ExpiryTimer_Impl* ExpiryTimer_Impl::getInstance() { - std::call_once(mflag, [](){ s_instance = new ExpiryTimer_Impl; }); + std::call_once((*mflag), [](){ s_instance = new ExpiryTimer_Impl(); }); return s_instance; } @@ -47,6 +65,7 @@ ExpiryTimer_Impl::Id ExpiryTimer_Impl::postTimer(DelayMilliSec msec, TimerCb cb) { Id retID; retID = generateID(); + milliSeconds delay(msec); insertTimerCBInfo(countExpireTime(delay), cb, retID); @@ -55,12 +74,12 @@ ExpiryTimer_Impl::Id ExpiryTimer_Impl::postTimer(DelayMilliSec msec, TimerCb cb) bool ExpiryTimer_Impl::cancelTimer(Id timerID) { - std::lock_guard lockf(m_mutex); bool ret = false; for(auto it: mTimerCBList) { if(it.second.m_id == timerID) { + std::lock_guard lockf(m_mutex); if(mTimerCBList.erase(it.first)!=0) ret = true; else @@ -72,8 +91,8 @@ bool ExpiryTimer_Impl::cancelTimer(Id timerID) void ExpiryTimer_Impl::insertTimerCBInfo(ExpiredTime msec, TimerCb cb, Id timerID) { - std::lock_guard lockf(m_mutex); TimerCBInfo newInfo = {timerID, cb}; + std::lock_guard lockf(m_mutex); mTimerCBList.insert(std::multimap::value_type(msec, newInfo)); m_cond.notify_all(); } @@ -91,6 +110,7 @@ ExpiryTimer_Impl::Id ExpiryTimer_Impl::generateID() srand(time(NULL)); Id retID = rand(); + std::lock_guard lockf(id_mutex); for(std::multimap::iterator it=mTimerCBList.begin(); it!=mTimerCBList.end(); ++it) { if((*it).second.m_id == retID || retID == 0) diff --git a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.h b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.h index a5b8a0c..1d54f93 100644 --- a/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.h +++ b/service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.h @@ -21,8 +21,8 @@ #ifndef _EXPIRY_TIMER_Impl_H_ #define _EXPIRY_TIMER_Impl_H_ -// CHECKER_WAIT_TIME : checker thread waits new request for 10000 seconds -#define CHECKER_WAIT_TIME 10000 +// CHECKER_WAIT_TIME : checker thread waits new request for 10 seconds +#define CHECKER_WAIT_TIME 10 #include #include @@ -50,14 +50,15 @@ private: TimerCb m_cb; }; -public: - ~ExpiryTimer_Impl(); - private: ExpiryTimer_Impl(); + ExpiryTimer_Impl(const ExpiryTimer_Impl&); + ExpiryTimer_Impl& operator=(const ExpiryTimer_Impl&); + ~ExpiryTimer_Impl(); public: static ExpiryTimer_Impl* getInstance(); + void destroy(); Id postTimer(DelayMilliSec, TimerCb); bool cancelTimer(Id); @@ -75,12 +76,13 @@ private: private: static ExpiryTimer_Impl* s_instance; - static std::once_flag mflag; + static std::once_flag* mflag; std::multimap mTimerCBList; std::thread check; std::mutex m_mutex; + std::mutex id_mutex; std::mutex cond_mutex; std::condition_variable m_cond; -- 2.7.4