|
| 1 | +#ifndef TIMERQUEUE_H |
| 2 | +#define TIMERQUEUE_H |
| 3 | + |
| 4 | +#include <mutex> |
| 5 | +#include <condition_variable> |
| 6 | +#include <thread> |
| 7 | +#include <queue> |
| 8 | +#include <chrono> |
| 9 | +#include <assert.h> |
| 10 | + |
| 11 | +namespace BT { |
| 12 | + |
| 13 | +// http://www.crazygaze.com/blog/2016/03/24/portable-c-timer-queue/ |
| 14 | + |
| 15 | +namespace details { |
| 16 | + |
| 17 | +class Semaphore { |
| 18 | +public: |
| 19 | + Semaphore(unsigned int count = 0) : m_count(count) {} |
| 20 | + |
| 21 | + void notify() { |
| 22 | + std::unique_lock<std::mutex> lock(m_mtx); |
| 23 | + m_count++; |
| 24 | + m_cv.notify_one(); |
| 25 | + } |
| 26 | + |
| 27 | + void wait() { |
| 28 | + std::unique_lock<std::mutex> lock(m_mtx); |
| 29 | + m_cv.wait(lock, [this]() { return m_count > 0; }); |
| 30 | + m_count--; |
| 31 | + } |
| 32 | + |
| 33 | + template <class Clock, class Duration> |
| 34 | + bool waitUntil(const std::chrono::time_point<Clock, Duration>& point) { |
| 35 | + std::unique_lock<std::mutex> lock(m_mtx); |
| 36 | + if (!m_cv.wait_until(lock, point, [this]() { return m_count > 0; })) |
| 37 | + return false; |
| 38 | + m_count--; |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | +private: |
| 43 | + std::mutex m_mtx; |
| 44 | + std::condition_variable m_cv; |
| 45 | + unsigned int m_count; |
| 46 | +}; |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +// Timer Queue |
| 53 | +// |
| 54 | +// Allows execution of handlers at a specified time in the future |
| 55 | +// Guarantees: |
| 56 | +// - All handlers are executed ONCE, even if canceled (aborted parameter will |
| 57 | +//be set to true) |
| 58 | +// - If TimerQueue is destroyed, it will cancel all handlers. |
| 59 | +// - Handlers are ALWAYS executed in the Timer Queue worker thread. |
| 60 | +// - Handlers execution order is NOT guaranteed |
| 61 | +// |
| 62 | +class TimerQueue { |
| 63 | +public: |
| 64 | + TimerQueue() { |
| 65 | + m_th = std::thread([this] { run(); }); |
| 66 | + } |
| 67 | + |
| 68 | + ~TimerQueue() { |
| 69 | + cancelAll(); |
| 70 | + // Abusing the timer queue to trigger the shutdown. |
| 71 | + add( std::chrono::milliseconds(0), [this](bool) { m_finish = true; }); |
| 72 | + m_th.join(); |
| 73 | + } |
| 74 | + |
| 75 | + //! Adds a new timer |
| 76 | + // \return |
| 77 | + // Returns the ID of the new timer. You can use this ID to cancel the |
| 78 | + // timer |
| 79 | + uint64_t add(std::chrono::milliseconds milliseconds, |
| 80 | + std::function<void(bool)> handler) |
| 81 | + { |
| 82 | + WorkItem item; |
| 83 | + item.end = Clock::now() + milliseconds; |
| 84 | + item.handler = std::move(handler); |
| 85 | + |
| 86 | + std::unique_lock<std::mutex> lk(m_mtx); |
| 87 | + uint64_t id = ++m_idcounter; |
| 88 | + item.id = id; |
| 89 | + m_items.push(std::move(item)); |
| 90 | + lk.unlock(); |
| 91 | + |
| 92 | + // Something changed, so wake up timer thread |
| 93 | + m_checkWork.notify(); |
| 94 | + return id; |
| 95 | + } |
| 96 | + |
| 97 | + //! Cancels the specified timer |
| 98 | + // \return |
| 99 | + // 1 if the timer was cancelled. |
| 100 | + // 0 if you were too late to cancel (or the timer ID was never valid to |
| 101 | + // start with) |
| 102 | + size_t cancel(uint64_t id) { |
| 103 | + // Instead of removing the item from the container (thus breaking the |
| 104 | + // heap integrity), we set the item as having no handler, and put |
| 105 | + // that handler on a new item at the top for immediate execution |
| 106 | + // The timer thread will then ignore the original item, since it has no |
| 107 | + // handler. |
| 108 | + std::unique_lock<std::mutex> lk(m_mtx); |
| 109 | + for (auto&& item : m_items.getContainer()) { |
| 110 | + if (item.id == id && item.handler) { |
| 111 | + WorkItem newItem; |
| 112 | + // Zero time, so it stays at the top for immediate execution |
| 113 | + newItem.end = Clock::time_point(); |
| 114 | + newItem.id = 0; // Means it is a canceled item |
| 115 | + // Move the handler from item to newitem. |
| 116 | + // Also, we need to manually set the handler to nullptr, since |
| 117 | + // the standard does not guarantee moving an std::function will |
| 118 | + // empty it. Some STL implementation will empty it, others will |
| 119 | + // not. |
| 120 | + newItem.handler = std::move(item.handler); |
| 121 | + item.handler = nullptr; |
| 122 | + m_items.push(std::move(newItem)); |
| 123 | + |
| 124 | + lk.unlock(); |
| 125 | + // Something changed, so wake up timer thread |
| 126 | + m_checkWork.notify(); |
| 127 | + return 1; |
| 128 | + } |
| 129 | + } |
| 130 | + return 0; |
| 131 | + } |
| 132 | + |
| 133 | + //! Cancels all timers |
| 134 | + // \return |
| 135 | + // The number of timers cancelled |
| 136 | + size_t cancelAll() { |
| 137 | + // Setting all "end" to 0 (for immediate execution) is ok, |
| 138 | + // since it maintains the heap integrity |
| 139 | + std::unique_lock<std::mutex> lk(m_mtx); |
| 140 | + for (auto&& item : m_items.getContainer()) { |
| 141 | + if (item.id) { |
| 142 | + item.end = Clock::time_point(); |
| 143 | + item.id = 0; |
| 144 | + } |
| 145 | + } |
| 146 | + auto ret = m_items.size(); |
| 147 | + |
| 148 | + lk.unlock(); |
| 149 | + m_checkWork.notify(); |
| 150 | + return ret; |
| 151 | + } |
| 152 | + |
| 153 | +private: |
| 154 | + using Clock = std::chrono::steady_clock; |
| 155 | + TimerQueue(const TimerQueue&) = delete; |
| 156 | + TimerQueue& operator=(const TimerQueue&) = delete; |
| 157 | + |
| 158 | + void run() |
| 159 | + { |
| 160 | + while (!m_finish) { |
| 161 | + auto end = calcWaitTime(); |
| 162 | + if (end.first) { |
| 163 | + // Timers found, so wait until it expires (or something else |
| 164 | + // changes) |
| 165 | + m_checkWork.waitUntil(end.second); |
| 166 | + } else { |
| 167 | + // No timers exist, so wait forever until something changes |
| 168 | + m_checkWork.wait(); |
| 169 | + } |
| 170 | + |
| 171 | + // Check and execute as much work as possible, such as, all expired |
| 172 | + // timers |
| 173 | + checkWork(); |
| 174 | + } |
| 175 | + |
| 176 | + // If we are shutting down, we should not have any items left, |
| 177 | + // since the shutdown cancels all items |
| 178 | + assert(m_items.size() == 0); |
| 179 | + } |
| 180 | + |
| 181 | + std::pair<bool, Clock::time_point> calcWaitTime() |
| 182 | + { |
| 183 | + std::lock_guard<std::mutex> lk(m_mtx); |
| 184 | + while (m_items.size()) |
| 185 | + { |
| 186 | + if (m_items.top().handler) |
| 187 | + { |
| 188 | + // Item present, so return the new wait time |
| 189 | + return std::make_pair(true, m_items.top().end); |
| 190 | + } |
| 191 | + else{ |
| 192 | + // Discard empty handlers (they were cancelled) |
| 193 | + m_items.pop(); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + // No items found, so return no wait time (causes the thread to wait |
| 198 | + // indefinitely) |
| 199 | + return std::make_pair(false, Clock::time_point()); |
| 200 | + } |
| 201 | + |
| 202 | + void checkWork() { |
| 203 | + std::unique_lock<std::mutex> lk(m_mtx); |
| 204 | + while (m_items.size() && m_items.top().end <= Clock::now()) { |
| 205 | + WorkItem item(std::move(m_items.top())); |
| 206 | + m_items.pop(); |
| 207 | + |
| 208 | + lk.unlock(); |
| 209 | + if (item.handler) |
| 210 | + item.handler(item.id == 0); |
| 211 | + lk.lock(); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + details::Semaphore m_checkWork; |
| 216 | + std::thread m_th; |
| 217 | + bool m_finish = false; |
| 218 | + uint64_t m_idcounter = 0; |
| 219 | + |
| 220 | + struct WorkItem { |
| 221 | + Clock::time_point end; |
| 222 | + uint64_t id; // id==0 means it was cancelled |
| 223 | + std::function<void(bool)> handler; |
| 224 | + bool operator>(const WorkItem& other) const { |
| 225 | + return end > other.end; |
| 226 | + } |
| 227 | + }; |
| 228 | + |
| 229 | + std::mutex m_mtx; |
| 230 | + // Inheriting from priority_queue, so we can access the internal container |
| 231 | + class Queue : public std::priority_queue<WorkItem, std::vector<WorkItem>, |
| 232 | + std::greater<WorkItem>> { |
| 233 | + public: |
| 234 | + std::vector<WorkItem>& getContainer() { |
| 235 | + return this->c; |
| 236 | + } |
| 237 | + } m_items; |
| 238 | +}; |
| 239 | + |
| 240 | +} |
| 241 | + |
| 242 | +#endif // TIMERQUEUE_H |
0 commit comments