Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / expiryTimer / src / ExpiryTimerImpl.h
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 #ifndef _EXPIRY_TIMER_IMPL_H_
22 #define _EXPIRY_TIMER_IMPL_H_
23
24 #include <functional>
25 #include <map>
26 #include <mutex>
27 #include <thread>
28 #include <chrono>
29 #include <condition_variable>
30 #include <random>
31 #include <unordered_set>
32 #include <atomic>
33
34 namespace OIC
35 {
36     namespace Service
37     {
38         class TimerTask;
39
40         class ExpiryTimerImpl
41         {
42         public:
43             typedef unsigned int Id;
44             typedef std::function< void(Id) > Callback;
45
46             typedef long long DelayInMillis;
47
48         private:
49             typedef std::chrono::milliseconds Milliseconds;
50
51         private:
52             ExpiryTimerImpl();
53             ~ExpiryTimerImpl();
54
55             ExpiryTimerImpl(const ExpiryTimerImpl&) = delete;
56             ExpiryTimerImpl& operator=(const ExpiryTimerImpl&) = delete;
57
58         public:
59             static ExpiryTimerImpl* getInstance();
60
61             std::shared_ptr< TimerTask > post(DelayInMillis, Callback);
62
63             bool cancel(Id);
64             size_t cancelAll(const std::unordered_set< std::shared_ptr<TimerTask > >&);
65
66         private:
67             static Milliseconds convertToTime(Milliseconds);
68
69             std::shared_ptr< TimerTask > addTask(Milliseconds, Callback, Id);
70
71             /**
72              * @pre The lock must be acquired with m_mutex.
73              */
74             bool containsId(Id) const;
75             Id generateId();
76
77             /**
78              * @pre The lock must be acquired with m_mutex.
79              */
80             void executeExpired();
81
82             /**
83              * @pre The lock must be acquired with m_mutex.
84              */
85             Milliseconds remainingTimeForNext() const;
86
87             void run();
88
89         private:
90             std::multimap< Milliseconds, std::shared_ptr< TimerTask > > m_tasks;
91
92             std::thread m_thread;
93             std::mutex m_mutex;
94             std::condition_variable m_cond;
95             bool m_stop;
96
97             std::mt19937 m_mt;
98             std::uniform_int_distribution< Id > m_dist;
99
100         };
101
102         class TimerTask
103         {
104         public:
105             TimerTask(ExpiryTimerImpl::Id, ExpiryTimerImpl::Callback);
106
107             TimerTask(const TimerTask&) = delete;
108             TimerTask(TimerTask&&) = delete;
109
110             TimerTask& operator=(const TimerTask&) = delete;
111             TimerTask& operator=(TimerTask&&) = delete;
112
113             bool isExecuted() const;
114             ExpiryTimerImpl::Id getId() const;
115
116         private:
117             void execute();
118
119         private:
120             std::atomic< ExpiryTimerImpl::Id > m_id;
121             ExpiryTimerImpl::Callback m_callback;
122
123             friend class ExpiryTimerImpl;
124         };
125
126     }
127 }
128 #endif //_EXPIRY_TIMER_IMPL_H_