Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / expiryTimer / src / ExpiryTimer.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "ExpiryTimer.h"
22 #include "ExpiryTimerImpl.h"
23
24 namespace OIC
25 {
26     namespace Service
27     {
28
29         namespace
30         {
31             constexpr size_t DEFAULT_SWEEP_SIZE{ 50 };
32         }
33
34         ExpiryTimer::ExpiryTimer() :
35                m_nextSweep { DEFAULT_SWEEP_SIZE }
36         {
37         }
38
39         ExpiryTimer::~ExpiryTimer()
40         {
41             cancelAll();
42         }
43
44         ExpiryTimer::Id ExpiryTimer::post(DelayInMilliSec milliSec, Callback cb)
45         {
46             auto task = ExpiryTimerImpl::getInstance()->post(milliSec, std::move(cb));
47             m_tasks[task->getId()] = task;
48
49             if (m_tasks.size() == m_nextSweep) sweep();
50
51             return task->getId();
52         }
53
54         bool ExpiryTimer::cancel(Id id)
55         {
56             auto it = m_tasks.find(id);
57
58             if (it == m_tasks.end()) return false;
59
60             auto task = it->second;
61             m_tasks.erase(it);
62
63             if (task->isExecuted()) return false;
64
65             return ExpiryTimerImpl::getInstance()->cancel(id);
66         }
67
68         void ExpiryTimer::cancelAll()
69         {
70             sweep();
71
72             std::unordered_set< std::shared_ptr< TimerTask > > set;
73
74             for(const auto& p : m_tasks)
75             {
76                 set.insert(p.second);
77             }
78
79             ExpiryTimerImpl::getInstance()->cancelAll(set);
80             m_tasks.clear();
81         }
82
83         size_t ExpiryTimer::getNumOfPending()
84         {
85             sweep();
86             return m_tasks.size();
87         }
88
89         size_t ExpiryTimer::getNumOfPending() const
90         {
91             size_t ret{ 0 };
92
93             for (const auto& p : m_tasks)
94             {
95                 ret += p.second->isExecuted() ? 0U : 1U;
96             }
97
98             return ret;
99         }
100
101         void ExpiryTimer::sweep()
102         {
103             for (auto it = m_tasks.begin(); it != m_tasks.end();)
104             {
105                 if (it->second->isExecuted())
106                 {
107                     it = m_tasks.erase(it);
108                 }
109                 else
110                 {
111                     ++it;
112                 }
113             }
114
115             m_nextSweep = m_tasks.size() << 1;
116         }
117
118     }
119 }