-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathWaitTimer_stl.cpp
More file actions
303 lines (251 loc) · 7.38 KB
/
WaitTimer_stl.cpp
File metadata and controls
303 lines (251 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "pch.h"
#include "WaitTimer.h"
using Deadline = std::chrono::high_resolution_clock::time_point;
namespace OS
{
class TimerQueue;
class WaitTimerImpl
{
public:
~WaitTimerImpl();
HRESULT Initialize(_In_opt_ void* context, _In_ WaitTimerCallback* callback);
void Start(_In_ uint64_t absoluteTime);
void Cancel();
void InvokeCallback();
private:
void* m_context;
WaitTimerCallback* m_callback;
std::shared_ptr<TimerQueue> m_timerQueue;
};
struct TimerEntry
{
Deadline When;
WaitTimerImpl* Timer;
TimerEntry(Deadline d, WaitTimerImpl* t) : When{ d }, Timer{ t } {}
};
struct TimerEntryComparator
{
bool operator()(TimerEntry const& l, TimerEntry const& r) noexcept
{
return l.When > r.When;
}
};
class TimerQueue
{
public:
bool Init() noexcept;
~TimerQueue();
void Set(WaitTimerImpl* timer, Deadline deadline) noexcept;
void Remove(WaitTimerImpl const* timer) noexcept;
private:
void Worker() noexcept;
TimerEntry const& Peek() const noexcept;
TimerEntry Pop() noexcept;
DefaultUnnamedMutex m_mutex;
std::condition_variable m_cv;
std::vector<TimerEntry> m_queue; // used as a heap
std::thread m_t;
bool m_exitThread = false;
bool m_initialized = false;
};
namespace
{
std::shared_ptr<TimerQueue> g_timerQueue;
DefaultUnnamedMutex g_timerQueueMutex;
}
TimerQueue::~TimerQueue()
{
{
std::lock_guard<std::mutex> lock{ m_mutex };
m_exitThread = true;
}
m_cv.notify_all();
if (m_t.joinable())
{
m_t.join();
}
}
bool TimerQueue::Init() noexcept
{
m_exitThread = false;
try
{
m_t = std::thread([this]()
{
Worker();
});
m_initialized = true;
}
catch (...)
{
m_initialized = false;
}
return m_initialized;
}
void TimerQueue::Set(WaitTimerImpl* timer, Deadline deadline) noexcept
{
{
std::lock_guard<std::mutex> lock{ m_mutex };
for (auto& entry : m_queue)
{
if (entry.Timer == timer)
{
entry.Timer = nullptr;
}
}
m_queue.emplace_back(deadline, timer);
std::push_heap(m_queue.begin(), m_queue.end(), TimerEntryComparator{});
}
m_cv.notify_all();
}
void TimerQueue::Remove(WaitTimerImpl const* timer) noexcept
{
std::lock_guard<std::mutex> lock{ m_mutex };
// since m_queue is a heap, removing elements is non trivial, instead we
// just clean the timer pointer and the entry will be popped eventually
for (auto& entry : m_queue)
{
if (entry.Timer == timer)
{
entry.Timer = nullptr;
}
}
}
void TimerQueue::Worker() noexcept
{
std::unique_lock<std::mutex> lock{ m_mutex };
while (!m_exitThread)
{
while (!m_queue.empty())
{
Deadline next = Peek().When;
if (std::chrono::high_resolution_clock::now() < next)
{
break;
}
TimerEntry entry = Pop();
// release the lock while invoking the callback, just in case timer
// gets destroyed on this thread or re-adds itself in the callback
lock.unlock();
if (entry.Timer) // Timer is set to nullptr if the entry is removed
{
entry.Timer->InvokeCallback();
}
lock.lock();
}
if (!m_queue.empty())
{
Deadline next = Peek().When;
m_cv.wait_until(lock, next);
}
else
{
m_cv.wait(lock);
}
}
}
TimerEntry const& TimerQueue::Peek() const noexcept
{
// assume lock is held
return m_queue.front();
}
TimerEntry TimerQueue::Pop() noexcept
{
// assume lock is held
TimerEntry e = m_queue.front();
std::pop_heap(m_queue.begin(), m_queue.end(), TimerEntryComparator{});
m_queue.pop_back();
return e;
}
WaitTimerImpl::~WaitTimerImpl()
{
std::lock_guard<std::mutex> lock{ g_timerQueueMutex };
// If we are the last one referencing the global timer the
// shared use count will be two (us + the global). If it is,
// clear out the global. We let our own reference reset
// as the class destructs. This puts it outside the mutex
// lock, which we want since there is some shutdown cost
// associated with shutting the timer down.
if (g_timerQueue.use_count() == 2)
{
g_timerQueue.reset();
}
}
HRESULT WaitTimerImpl::Initialize(_In_opt_ void* context, _In_ WaitTimerCallback* callback)
{
m_context = context;
m_callback = callback;
std::lock_guard<std::mutex> lock{ g_timerQueueMutex };
if (g_timerQueue == nullptr)
{
try
{
auto queue = http_allocate_shared<TimerQueue>();
if (!queue->Init())
{
return E_FAIL;
}
g_timerQueue = std::move(queue);
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
}
m_timerQueue = g_timerQueue;
return S_OK;
}
void WaitTimerImpl::Start(_In_ uint64_t absoluteTime)
{
m_timerQueue->Set(this, Deadline(Deadline::duration(absoluteTime)));
}
void WaitTimerImpl::Cancel()
{
m_timerQueue->Remove(this);
}
void WaitTimerImpl::InvokeCallback()
{
m_callback(m_context);
}
WaitTimer::WaitTimer() noexcept
: m_impl(nullptr)
{}
WaitTimer::~WaitTimer() noexcept
{
Terminate();
}
HRESULT WaitTimer::Initialize(_In_opt_ void* context, _In_ WaitTimerCallback* callback) noexcept
{
if (m_impl.load() != nullptr || callback == nullptr)
{
ASSERT(false);
return E_UNEXPECTED;
}
std::unique_ptr<WaitTimerImpl> timer(new (std::nothrow) WaitTimerImpl);
RETURN_IF_NULL_ALLOC(timer.get());
RETURN_IF_FAILED(timer->Initialize(context, callback));
m_impl = timer.release();
return S_OK;
}
void WaitTimer::Terminate() noexcept
{
std::unique_ptr<WaitTimerImpl> timer(m_impl.exchange(nullptr));
if (timer != nullptr)
{
timer->Cancel();
}
}
void WaitTimer::Start(_In_ uint64_t absoluteTime) noexcept
{
m_impl.load()->Start(absoluteTime);
}
void WaitTimer::Cancel() noexcept
{
m_impl.load()->Cancel();
}
uint64_t WaitTimer::GetAbsoluteTime(_In_ uint32_t msFromNow) noexcept
{
Deadline d = std::chrono::high_resolution_clock::now() + std::chrono::milliseconds(msFromNow);
return d.time_since_epoch().count();
}
} // Namespace