CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
timer_queue.h
1
2#ifndef _aspose_system_threading_timer_queue_h
3#define _aspose_system_threading_timer_queue_h
4
5#include <system/threading/timer.h>
6#include <system/threading/thread_pool.h>
7#include <functional>
8#include <algorithm>
9#include <unordered_map>
10#include <chrono>
11#include <thread>
12#include <mutex>
13#include <condition_variable>
14
15namespace System { namespace Threading {
16
21class ASPOSECPP_SHARED_CLASS TimerQueue
22{
23public:
25 static ASPOSECPP_SHARED_API TimerQueue& GetInstance();
26
28 TimerQueue(const TimerQueue&) = delete;
30 void operator = (const TimerQueue&) = delete;
31
35 bool Add(Timer* timer)
36 {
37 std::lock_guard<std::mutex> __lock(m_mutex);
38 m_timer_queue[timer] = true;
39
40 m_cv.notify_one();
41 return true;
42 }
46 bool Delete(Timer* timer)
47 {
48 std::lock_guard<std::mutex> __lock(m_mutex);
49 m_timer_queue.erase(timer);
50
51 m_cv.notify_one();
52 return true;
53 }
55 static ASPOSECPP_SHARED_API void JoinWorkerThread();
56
57private:
58#ifdef _MSC_VER
59#pragma warning(push)
60#pragma warning(disable: 4355)
61#endif
63 TimerQueue() : is_worker_terminates(false), m_worker_thread(std::bind(&TimerQueue::Worker, this)) {}
64#ifdef _MSC_VER
65#pragma warning(pop)
66#endif
71 static bool& GetInitialized();
73 void Join();
74
76 typedef std::unordered_map<Timer*, bool> Queue;
78 Queue m_timer_queue;
80 bool is_worker_terminates;
82 std::mutex m_mutex;
84 std::condition_variable m_cv;
86 std::thread m_worker_thread;
87
90 void Fire(const Timer* t)
91 {
92 ThreadPool::QueueUserWorkItem(t->m_callback, t->m_state);
93 };
94
96 void Worker()
97 {
98 using namespace std::chrono;
99
100 system_clock::time_point time_point;
101 std::unique_lock<std::mutex> m_lock(m_mutex);
102 Queue::iterator min;
103 milliseconds fire_interval, duration;
104 for (;;)
105 {
106 while (m_timer_queue.empty()) //Stuck here until something appears in queue
107 {
108 if (is_worker_terminates)
109 {
110 return;
111 }
112 m_cv.wait(m_lock);
113 }
114
115 min = std::min_element(m_timer_queue.begin(), m_timer_queue.end(),
116 [](const Queue::value_type &t1, const Queue::value_type &t2) {
117 return std::get<0>(t1)->m_dueTime < std::get<0>(t2)->m_dueTime;
118 }
119 );
120
121 if (m_timer_queue.size() == 1) // The first added timer starts waiting loop and should not be handled as "just added"
122 {
123 min->second = false;
124 }
125
126 fire_interval = milliseconds(min->first->m_dueTime);
127 time_point = system_clock::now() + fire_interval;
128
129 m_cv.wait_until(m_lock, time_point);
130
131 duration = duration_cast<milliseconds>(system_clock::now() - time_point) + fire_interval;
132
133 for (auto i = m_timer_queue.begin(); i != m_timer_queue.end(); )
134 {
135 if ((i->first->m_dueTime <= duration.count() && !i->second) || // Fire expired timers and reset their timeout
136 i->first->m_dueTime == 0) // Also fire timers with immediate start
137 {
138 Fire(i->first);
139
140 if(i->first->m_period > 0)
141 {
142 i->first->m_dueTime = i->first->m_period;
143 i->second = false;
144 ++i;
145 }
146 else // One shot timer
147 {
148 i = m_timer_queue.erase(i);
149 }
150
151 }
152 else // Update timeout for the rest
153 {
154 if (i->second) // Just added, no need to update
155 {
156 i->second = false;
157 }
158 else
159 {
160 i->first->m_dueTime -= duration.count();
161 }
162
163 ++i;
164 }
165
166 }
167 }
168 }
169};
170
171}} // System::Threading
172#endif
static bool QueueUserWorkItem(WaitCallback callback)
Puts work item into queue which is present with callback with no parameter.
Timer class that executes job item in separate thread after delay. Objects of this class should only ...
Definition: timer.h:51
Queue that handles Timer objects. This is just an implementation. Timer objects register there by the...
Definition: timer_queue.h:22
static void JoinWorkerThread()
Joins worker thread. Waits infinitely if required.
static TimerQueue & GetInstance()
Implementation singleton.
bool Delete(Timer *timer)
Deletes timer from queue.
Definition: timer_queue.h:46
bool Add(Timer *timer)
Registers timer in queue.
Definition: timer_queue.h:35
TimerQueue(const TimerQueue &)=delete
No copying.
Definition: db_command.h:9