1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "ExpiryTimer_Impl.h"
28 ExpiryTimer_Impl* ExpiryTimer_Impl::s_instance;
29 std::once_flag ExpiryTimer_Impl::mflag;
31 ExpiryTimer_Impl::ExpiryTimer_Impl()
36 ExpiryTimer_Impl::~ExpiryTimer_Impl()
40 ExpiryTimer_Impl* ExpiryTimer_Impl::getInstance()
42 std::call_once(mflag, [](){ s_instance = new ExpiryTimer_Impl; });
46 ExpiryTimer_Impl::Id ExpiryTimer_Impl::postTimer(DelayMilliSec msec, TimerCb cb)
50 milliSeconds delay(msec);
51 insertTimerCBInfo(countExpireTime(delay), cb, retID);
56 bool ExpiryTimer_Impl::cancelTimer(Id timerID)
58 std::lock_guard<std::mutex> lockf(m_mutex);
60 for(auto it: mTimerCBList)
62 if(it.second.m_id == timerID)
64 if(mTimerCBList.erase(it.first)!=0)
73 void ExpiryTimer_Impl::insertTimerCBInfo(ExpiredTime msec, TimerCb cb, Id timerID)
75 std::lock_guard<std::mutex> lockf(m_mutex);
76 TimerCBInfo newInfo = {timerID, cb};
77 mTimerCBList.insert(std::multimap<ExpiredTime, TimerCBInfo>::value_type(msec, newInfo));
81 ExpiryTimer_Impl::ExpiredTime ExpiryTimer_Impl::countExpireTime(milliDelayTime msec)
83 auto now = std::chrono::system_clock::now();
84 milliSeconds ret = std::chrono::duration_cast<milliSeconds>(now.time_since_epoch()) + msec;
89 ExpiryTimer_Impl::Id ExpiryTimer_Impl::generateID()
94 for(std::multimap<ExpiredTime, TimerCBInfo>::iterator it=mTimerCBList.begin(); it!=mTimerCBList.end(); ++it)
96 if((*it).second.m_id == retID || retID == 0)
99 it = mTimerCBList.begin();
105 void ExpiryTimer_Impl::createChecker()
107 check = std::thread(&ExpiryTimer_Impl::doChecker, this);
110 void ExpiryTimer_Impl::doChecker()
114 std::unique_lock<std::mutex> ul(cond_mutex);
116 if(mTimerCBList.empty())
118 m_cond.wait_for(ul, std::chrono::seconds(CHECKER_WAIT_TIME));
122 ExpiredTime expireTime;
123 expireTime = mTimerCBList.begin()->first;
125 auto now = std::chrono::system_clock::now();
126 milliSeconds waitTime = expireTime - std::chrono::duration_cast<milliSeconds>(now.time_since_epoch());
127 m_cond.wait_for(ul, waitTime);
129 auto callTime = std::chrono::system_clock::now();
130 doExecutor(std::chrono::duration_cast<milliSeconds>(callTime.time_since_epoch()));
135 void ExpiryTimer_Impl::doExecutor(ExpiredTime expireTime)
137 for(auto it: mTimerCBList)
139 if(it.first <= expireTime)
141 new ExecutorThread(it.second);
142 mTimerCBList.erase(mTimerCBList.begin());
149 // ExecuterThread Class
150 ExpiryTimer_Impl::ExecutorThread::ExecutorThread(TimerCBInfo cbInfo)
152 execute = std::thread(&ExpiryTimer_Impl::ExecutorThread::executorFunc, this, cbInfo);
155 ExpiryTimer_Impl::ExecutorThread::~ExecutorThread()
160 void ExpiryTimer_Impl::ExecutorThread::executorFunc(TimerCBInfo cbInfo)
162 cbInfo.m_cb(cbInfo.m_id);