Add destroyTimer function to ExpiryTimer
authorChaJiwon <jw_wonny.cha@samsung.com>
Fri, 10 Jul 2015 05:22:37 +0000 (14:22 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 14 Jul 2015 01:38:20 +0000 (01:38 +0000)
User call destroyTimer function, ExpiryTimer is destroyed.

Change-Id: I7cf625adbb51c3ec2ade4f6b223c8a3876ce842f
Signed-off-by: ChaJiwon <jw_wonny.cha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1603
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: kwon doil <doil.kwon@samsung.com>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/common/expiryTimer/include/ExpiryTimer.h
service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer.cpp
service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.cpp
service/resource-manipulation/modules/common/expiryTimer/src/ExpiryTimer_Impl.h

index df6ecbee68880f1a15c6168051d2216b1681e638..dd44907c5d3bf1623cdd84844ec73a2316e8ebad 100644 (file)
@@ -34,11 +34,14 @@ public:
 
 public:
     ExpiryTimer();
+
+private:
     ~ExpiryTimer();
 
 public:
     TimerID postTimer(DelayMilliSec sec, TimerCB);
     bool cancelTimer(TimerID timerID);
+    void destroyTimer();
 
 private:
     std::list<TimerID> mTimerIDList;
index 97c1e4f762ec43abc89d548ba5d977ace1ec84ee..22bc1db9afe2ef81e2e3aaaff598bf5f4a420c2b 100644 (file)
@@ -29,13 +29,19 @@ ExpiryTimer::ExpiryTimer()
 }
 
 ExpiryTimer::~ExpiryTimer()
+{
+}
+
+void ExpiryTimer::destroyTimer()
 {
     for(auto it : mTimerIDList)
     {
         timerPtr->cancelTimer(it);
     }
-    timerPtr->destroy();
+    timerPtr->destroyInstance();
     mTimerIDList.clear();
+
+    this->~ExpiryTimer();
 }
 
 ExpiryTimer::TimerID ExpiryTimer::postTimer(DelayMilliSec sec, TimerCB cb)
index 053243b0827d0c2df71ec3b5f4919028b23b5d80..5c8f35258bd40e3d8d318817fe77ade4daf6e348 100644 (file)
@@ -23,6 +23,7 @@
 #include <unistd.h>
 #include <algorithm>
 #include <cstdlib>
+#include <random>
 #include <utility>
 
 ExpiryTimer_Impl* ExpiryTimer_Impl::s_instance = nullptr;
@@ -36,12 +37,11 @@ ExpiryTimer_Impl::ExpiryTimer_Impl()
 ExpiryTimer_Impl::~ExpiryTimer_Impl()
 {
     check.detach();
-    delete mflag;
+    mflag = new std::once_flag;
     s_instance = nullptr;
-    delete s_instance;
 }
 
-void ExpiryTimer_Impl::destroy()
+void ExpiryTimer_Impl::destroyInstance()
 {
     if(mTimerCBList.empty())
     {
@@ -55,6 +55,7 @@ void ExpiryTimer_Impl::destroy()
         }
     }
 }
+
 ExpiryTimer_Impl* ExpiryTimer_Impl::getInstance()
 {
     std::call_once((*mflag), [](){ s_instance = new ExpiryTimer_Impl(); });
@@ -75,11 +76,11 @@ ExpiryTimer_Impl::Id ExpiryTimer_Impl::postTimer(DelayMilliSec msec, TimerCb cb)
 bool ExpiryTimer_Impl::cancelTimer(Id timerID)
 {
     bool ret = false;
+    std::lock_guard<std::mutex> lockf(m_mutex);
     for(auto it: mTimerCBList)
     {
         if(it.second.m_id == timerID)
         {
-            std::lock_guard<std::mutex> lockf(m_mutex);
             if(mTimerCBList.erase(it.first)!=0)
                 ret = true;
             else
@@ -107,17 +108,20 @@ ExpiryTimer_Impl::ExpiredTime ExpiryTimer_Impl::countExpireTime(milliDelayTime m
 
 ExpiryTimer_Impl::Id ExpiryTimer_Impl::generateID()
 {
-    srand(time(NULL));
-    Id retID = rand();
+    std::srand((unsigned)std::time(NULL));
+    Id retID = std::rand();
 
-    std::lock_guard<std::mutex> lockf(id_mutex);
-    for(std::multimap<ExpiredTime, TimerCBInfo>::iterator it=mTimerCBList.begin(); it!=mTimerCBList.end(); ++it)
+    for(std::multimap<ExpiredTime, TimerCBInfo>::iterator it=mTimerCBList.begin(); it!=mTimerCBList.end(); )
      {
        if((*it).second.m_id == retID || retID == 0)
         {
-            retID = rand();
+            retID = std::rand();
             it = mTimerCBList.begin();
         }
+       else
+       {
+           ++it;
+       }
      }
     return retID;
 }
@@ -154,6 +158,7 @@ void ExpiryTimer_Impl::doChecker()
 
 void ExpiryTimer_Impl::doExecutor(ExpiredTime expireTime)
 {
+    std::lock_guard<std::mutex> lockf(m_mutex);
     for(auto it: mTimerCBList)
     {
         if(it.first <= expireTime)
@@ -166,7 +171,7 @@ void ExpiryTimer_Impl::doExecutor(ExpiredTime expireTime)
     }
 }
 
-// ExecuterThread Class
+// ExecutorThread Class
 ExpiryTimer_Impl::ExecutorThread::ExecutorThread(TimerCBInfo cbInfo)
 {
     execute = std::thread(&ExpiryTimer_Impl::ExecutorThread::executorFunc, this, cbInfo);
index 1d54f9306c78d6041565d97469213a97e1bb1cfe..7c785ee3871c1fcc7a1c044e1d915a600bd47757 100644 (file)
@@ -58,7 +58,7 @@ private:
 
 public:
     static ExpiryTimer_Impl* getInstance();
-    void destroy();
+    void destroyInstance();
 
     Id postTimer(DelayMilliSec, TimerCb);
     bool cancelTimer(Id);
@@ -82,7 +82,6 @@ private:
 
    std::thread check;
    std::mutex m_mutex;
-   std::mutex id_mutex;
    std::mutex cond_mutex;
    std::condition_variable m_cond;