-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathasynctimerqueue.hh
More file actions
345 lines (278 loc) · 10.6 KB
/
asynctimerqueue.hh
File metadata and controls
345 lines (278 loc) · 10.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (C) 2014 Raju Kadam <rajulkadam@gmail.com>
*
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <iostream>
#include <thread>
#include <chrono>
#include <algorithm> // std::find_if, std::min_element
#include <mutex>
#include <condition_variable>
#include <unordered_map>
namespace Timer {
typedef std::function<void ()> voidFunc;
struct Event {
int id_; // Unique id for each event object
int timeout_; // Interval for execution(in millisec)
uint64_t nextRun_; // Next scheduled execution time(measured w.r.t start of loop)
bool repeat_; // Repeat event indefinitely if true
// Event handler callback
voidFunc eventHandler_;
Event(int id,
int timeout,
int nextRun,
bool repeat,
voidFunc eventHandler) :
id_(id),
timeout_(timeout),
nextRun_(nextRun),
repeat_(repeat),
eventHandler_(eventHandler) {
}
};
class AsyncTimerQueue {
public:
AsyncTimerQueue();
~AsyncTimerQueue();
template<class F, class... Args>
int create(int timeout, bool repeat, F&& f, Args&&... args);
int cancel(int id);
int cancel(int id, int timeout);
static AsyncTimerQueue & Instance();
int timerLoop();
void shutdown();
// Delete all constructors since this is singleton pattern
AsyncTimerQueue(const AsyncTimerQueue &)=delete;
AsyncTimerQueue(AsyncTimerQueue &&)=delete;
AsyncTimerQueue & operator=(const AsyncTimerQueue &)=delete;
AsyncTimerQueue & operator=(AsyncTimerQueue &&)=delete;
private:
std::mutex eventQMutex_;
std::condition_variable eventQCond_;
std::mutex emptyQMutex_;
std::condition_variable emptyQCond_;
bool stopThread_;
bool fallThrough_;
int currMin_;
int waitTime_;
int nextId_;
std::chrono::time_point<std::chrono::system_clock> startTime_;
std::unordered_map<int, std::vector<Event>> eventMap_;
};
typedef std::pair<int, std::vector<Event>> EventMapPair;
struct CompareTimeout {
bool operator()(const EventMapPair & left,
const EventMapPair & right) const {
return left.second[0].timeout_ < right.second[0].timeout_;
}
};
struct CompareNextRun {
bool operator()(const EventMapPair & left,
const EventMapPair & right) const {
return left.second[0].nextRun_ < right.second[0].nextRun_;
}
};
AsyncTimerQueue::AsyncTimerQueue() : stopThread_(false),
fallThrough_(false),
currMin_(0),
waitTime_(0),
nextId_(1),
startTime_(std::chrono::system_clock::now()) {
}
template<class F, class... Args>
int
AsyncTimerQueue::create(int timeout, bool repeat, F&& f, Args&&... args) {
int prevMinTimeout;
// Define generic funtion with any number / type of args
// and return type, make it callable without args(void func())
// using std::bind
auto func = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
std::function<void()> task = func;
// Create new event object
{
std::unique_lock<std::mutex> lock(eventQMutex_);
auto id = nextId_++;
// Save previous timeout value to ensure fall through
// incase 'this' timeout is lesser than previous timeout
prevMinTimeout = currMin_;
// Chain events with same timeout value
eventMap_[timeout].emplace_back(std::move(Event(id, timeout, timeout, repeat, task)));
// For first run trigger wait asap else find min
// timeout value and wait till it expires or event
// with lesser timeout is queued
if (!currMin_) {
currMin_ = timeout;
} else {
auto minElem = std::min_element(eventMap_.begin(),
eventMap_.end(),
CompareTimeout());
currMin_ = (*minElem).second[0].timeout_;
// The timer loop may already be waiting for longer
// timeout value. If new event with lesser timeout
// gets added to eventMap_ its necessary to notify
// the conditional variable and fall through
if (timeout < prevMinTimeout) {
fallThrough_ = true;
}
}
waitTime_ = currMin_;
// Notify timer loop thread to process event
emptyQCond_.notify_one();
return id;
}
}
int
AsyncTimerQueue::timerLoop() {
while (true) {
// Block till queue is empty
{
std::unique_lock<std::mutex> lock(emptyQMutex_);
emptyQCond_.wait(lock, [this]{ return this->stopThread_ ||
!this->eventMap_.empty(); });
}
if (stopThread_) {
std::cout << "Timer loop has been stopped\n";
return 0;
}
if (!eventMap_.empty()) {
// Block till least timeout value in eventMap_ is expired or
// new event gets added to eventMap_ with lesser value
std::unique_lock<std::mutex> lock(eventQMutex_);
// XXX Ocasionally below wait doesn't fall through
// immediately even though timer loop thread has
// already been started. Looks like notify doesn't
// reach the thread. Is this bug in STL ?
eventQCond_.wait_until(lock,
std::chrono::system_clock::now() +
std::chrono::milliseconds(waitTime_),
[this] { return this->stopThread_ ||
this->fallThrough_; });
// Enough time may not have been spent in case of
// fall through. So calculate diff and wait for
// some more time
if (fallThrough_) {
// Find time spent
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - startTime_).count();
// Find extra time which has to be spent sleeping
waitTime_ = currMin_ - diff;
if (waitTime_ > 0) {
// XXX will block thread, multiple fallThrough_
std::this_thread::sleep_for(std::chrono::milliseconds(waitTime_));
}
// Reset fallThrough_
fallThrough_ = false;
}
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - startTime_).count();
auto & eventVector = eventMap_[currMin_];
// Trigger all events in event chain
for (auto iter = eventVector.begin() ; iter != eventVector.end() ;) {
std::thread((*iter).eventHandler_).detach();
// If event has to be run once then delete
// from vector else increment nextRun_ which
// will be compared below to get next event
// to be run
if (!(*iter).repeat_) {
iter = eventVector.erase(iter);
continue;
} else {
(*iter).nextRun_ = elapsedTime + (*iter).timeout_;
}
++iter;
}
// If current bucket is empty erase it
if (eventVector.empty()) {
eventMap_.erase(currMin_);
}
// If map is empty continue and block at start of loop
if (eventMap_.empty()) {
continue;
}
// Get next event chain to be run
auto elem = std::min_element(eventMap_.begin(),
eventMap_.end(),
CompareNextRun());
waitTime_ = (*elem).second[0].nextRun_ - elapsedTime;
if (waitTime_ < 0) {
std::cout << "Event delayed : " << waitTime_ << "(msec)" << std::endl;
waitTime_ = 0;
}
// Next event chain to be run
currMin_ = (*elem).second[0].timeout_;
}
}
}
inline int
AsyncTimerQueue::cancel(int id) {
std::unique_lock<std::mutex> lock(eventQMutex_);
for (auto & eventPair : eventMap_) {
auto & eventVector = eventPair.second;
// Search for event with matching id in current pair
auto event = std::find_if(eventVector.begin(),
eventVector.end(),
[=] (Event & e) { return e.id_ == id; });
// If found erase event from vector
if (event != eventVector.end()) {
// Erase event from vector
eventVector.erase(event);
std::cout << "Event with id " << id << " deleted succesfully\n";
// Delete map entry if vector is empty
if (eventVector.empty()) {
eventMap_.erase(eventPair.first);
}
return 0;
}
}
return -1;
}
inline int
AsyncTimerQueue::cancel(int id, int timeout) {
std::unique_lock<std::mutex> lock(eventQMutex_);
auto eventPair = eventMap_.find(timeout);
if (eventPair != eventMap_.end()) {
auto eventVector = eventMap_[timeout];
auto event = std::find_if(eventVector.begin(),
eventVector.end(),
[=] (Event & e) { return e.id_ == id; });
if (event != eventVector.end()) {
eventVector.erase(event);
std::cout << "Event with id " << id << " deleted succesfully\n";
if (eventVector.empty()) {
eventMap_.erase(timeout);
}
return 0;
}
}
return -1;
}
inline AsyncTimerQueue &
AsyncTimerQueue::Instance() {
static AsyncTimerQueue asyncTimer;
return asyncTimer;
}
inline void
AsyncTimerQueue::shutdown() {
stopThread_ = true;
eventQCond_.notify_all();
emptyQCond_.notify_all();
}
AsyncTimerQueue::~AsyncTimerQueue() {
if (!stopThread_) {
shutdown();
}
}
}