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.h"
22 #include "ExpiryTimerImpl.h"
31 constexpr size_t DEFAULT_SWEEP_SIZE{ 50 };
34 ExpiryTimer::ExpiryTimer() :
35 m_nextSweep { DEFAULT_SWEEP_SIZE }
39 ExpiryTimer::~ExpiryTimer()
44 ExpiryTimer::Id ExpiryTimer::post(DelayInMilliSec milliSec, Callback cb)
46 auto task = ExpiryTimerImpl::getInstance()->post(milliSec, std::move(cb));
47 m_tasks[task->getId()] = task;
49 if (m_tasks.size() == m_nextSweep) sweep();
54 bool ExpiryTimer::cancel(Id id)
56 auto it = m_tasks.find(id);
58 if (it == m_tasks.end()) return false;
60 auto task = it->second;
63 if (task->isExecuted()) return false;
65 return ExpiryTimerImpl::getInstance()->cancel(id);
68 void ExpiryTimer::cancelAll()
72 std::unordered_set< std::shared_ptr< TimerTask > > set;
74 for(const auto& p : m_tasks)
79 ExpiryTimerImpl::getInstance()->cancelAll(set);
83 size_t ExpiryTimer::getNumOfPending()
86 return m_tasks.size();
89 size_t ExpiryTimer::getNumOfPending() const
93 for (const auto& p : m_tasks)
95 ret += p.second->isExecuted() ? 0U : 1U;
101 void ExpiryTimer::sweep()
103 for (auto it = m_tasks.begin(); it != m_tasks.end();)
105 if (it->second->isExecuted())
107 it = m_tasks.erase(it);
115 m_nextSweep = m_tasks.size() << 1;