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(); | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 48 | // Check expiration time only for active handlers, |
49 | // remove canceled ones immediately | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | if (x.second) |
51 | { | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 52 | if (now - x.first < std::chrono::seconds(5)) |
53 | { | ||||
54 | break; | ||||
55 | } | ||||
56 | |||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | BMCWEB_LOG_DEBUG << "timer call: " << this << ' ' << step; |
58 | // we know that timer handlers are very simple currenty; call | ||||
59 | // here | ||||
60 | x.second(); | ||||
61 | } | ||||
62 | dq.pop_front(); | ||||
63 | step++; | ||||
64 | } | ||||
65 | } | ||||
66 | |||||
67 | private: | ||||
68 | using storage_type = | ||||
69 | std::pair<std::chrono::time_point<std::chrono::steady_clock>, | ||||
70 | std::function<void()>>; | ||||
71 | |||||
72 | boost::circular_buffer_space_optimized<storage_type, | ||||
73 | std::allocator<storage_type>> | ||||
74 | dq{}; | ||||
75 | |||||
76 | // boost::circular_buffer<storage_type> dq{20}; | ||||
77 | // std::deque<storage_type> dq{}; | ||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 78 | size_t step{}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 79 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | } // namespace detail |
81 | } // namespace crow |