Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 3 | #include <boost/circular_buffer.hpp> |
| 4 | #include <boost/circular_buffer/space_optimized.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 5 | #include <chrono> |
| 6 | #include <functional> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 7 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 8 | #include "logging.h" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 9 | |
| 10 | namespace crow |
| 11 | { |
| 12 | namespace detail |
| 13 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 14 | // fast timer queue for fixed tick value. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | class TimerQueue |
| 16 | { |
| 17 | public: |
| 18 | TimerQueue() |
| 19 | { |
| 20 | dq.set_capacity(100); |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 21 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 22 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 23 | void cancel(size_t k) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 25 | size_t index = k - step; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 26 | if (index < dq.size()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 28 | dq[index].second = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 30 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 31 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 32 | size_t add(std::function<void()> f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | { |
| 34 | dq.push_back( |
| 35 | std::make_pair(std::chrono::steady_clock::now(), std::move(f))); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 36 | size_t ret = step + dq.size() - 1; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 37 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | BMCWEB_LOG_DEBUG << "timer add inside: " << this << ' ' << ret; |
| 39 | return ret; |
| 40 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 41 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | void process() |
| 43 | { |
| 44 | auto now = std::chrono::steady_clock::now(); |
| 45 | while (!dq.empty()) |
| 46 | { |
| 47 | auto& x = dq.front(); |
| 48 | if (now - x.first < std::chrono::seconds(5)) |
| 49 | { |
| 50 | break; |
| 51 | } |
| 52 | if (x.second) |
| 53 | { |
| 54 | BMCWEB_LOG_DEBUG << "timer call: " << this << ' ' << step; |
| 55 | // we know that timer handlers are very simple currenty; call |
| 56 | // here |
| 57 | x.second(); |
| 58 | } |
| 59 | dq.pop_front(); |
| 60 | step++; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | using storage_type = |
| 66 | std::pair<std::chrono::time_point<std::chrono::steady_clock>, |
| 67 | std::function<void()>>; |
| 68 | |
| 69 | boost::circular_buffer_space_optimized<storage_type, |
| 70 | std::allocator<storage_type>> |
| 71 | dq{}; |
| 72 | |
| 73 | // boost::circular_buffer<storage_type> dq{20}; |
| 74 | // std::deque<storage_type> dq{}; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 75 | size_t step{}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 76 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 77 | } // namespace detail |
| 78 | } // namespace crow |