Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1 | #pragma once |
2 | |||||
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "logging.hpp" |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 4 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 5 | #include <boost/circular_buffer.hpp> |
6 | #include <boost/circular_buffer/space_optimized.hpp> | ||||
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 7 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 8 | #include <chrono> |
9 | #include <functional> | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 10 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 11 | namespace crow |
12 | { | ||||
James Feist | 3909dc8 | 2020-04-03 10:58:55 -0700 | [diff] [blame] | 13 | |
14 | constexpr const size_t timerQueueTimeoutSeconds = 5; | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | namespace detail |
16 | { | ||||
James Feist | f0af859 | 2020-03-27 16:28:59 -0700 | [diff] [blame] | 17 | |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 18 | constexpr const size_t maxSize = 100; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 19 | // fast timer queue for fixed tick value. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | class TimerQueue |
21 | { | ||||
22 | public: | ||||
23 | TimerQueue() | ||||
24 | { | ||||
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 25 | dq.set_capacity(maxSize); |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 26 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 27 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 28 | void cancel(size_t k) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 30 | size_t index = k - step; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 31 | if (index < dq.size()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 33 | dq[index].second = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | } |
Karthick Sundarrajan | 8ba7ae5 | 2020-12-07 14:22:46 -0800 | [diff] [blame] | 35 | while (dq.begin() != dq.end() && dq.front().second == nullptr) |
36 | { | ||||
37 | dq.pop_front(); | ||||
38 | step++; | ||||
39 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 40 | } |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 41 | |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 42 | std::optional<size_t> add(std::function<void()> f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | { |
James Feist | cb6cb49 | 2020-04-03 13:36:17 -0700 | [diff] [blame] | 44 | if (dq.size() == maxSize) |
45 | { | ||||
46 | return std::nullopt; | ||||
47 | } | ||||
48 | |||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | dq.push_back( |
50 | std::make_pair(std::chrono::steady_clock::now(), std::move(f))); | ||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 51 | size_t ret = step + dq.size() - 1; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 52 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | BMCWEB_LOG_DEBUG << "timer add inside: " << this << ' ' << ret; |
54 | return ret; | ||||
55 | } | ||||
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | void process() |
58 | { | ||||
59 | auto now = std::chrono::steady_clock::now(); | ||||
60 | while (!dq.empty()) | ||||
61 | { | ||||
62 | auto& x = dq.front(); | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 63 | // Check expiration time only for active handlers, |
64 | // remove canceled ones immediately | ||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 65 | if (x.second) |
66 | { | ||||
James Feist | f0af859 | 2020-03-27 16:28:59 -0700 | [diff] [blame] | 67 | if (now - x.first < |
68 | std::chrono::seconds(timerQueueTimeoutSeconds)) | ||||
Jan Sowinski | 2b5e08e | 2020-01-09 17:16:02 +0100 | [diff] [blame] | 69 | { |
70 | break; | ||||
71 | } | ||||
72 | |||||
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | BMCWEB_LOG_DEBUG << "timer call: " << this << ' ' << step; |
Gunnar Mills | caa3ce3 | 2020-07-08 14:46:53 -0500 | [diff] [blame] | 74 | // we know that timer handlers are very simple currently; call |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 75 | // here |
76 | x.second(); | ||||
77 | } | ||||
78 | dq.pop_front(); | ||||
79 | step++; | ||||
80 | } | ||||
81 | } | ||||
82 | |||||
83 | private: | ||||
84 | using storage_type = | ||||
85 | std::pair<std::chrono::time_point<std::chrono::steady_clock>, | ||||
86 | std::function<void()>>; | ||||
87 | |||||
88 | boost::circular_buffer_space_optimized<storage_type, | ||||
89 | std::allocator<storage_type>> | ||||
90 | dq{}; | ||||
91 | |||||
92 | // boost::circular_buffer<storage_type> dq{20}; | ||||
93 | // std::deque<storage_type> dq{}; | ||||
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 94 | size_t step{}; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 95 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 96 | } // namespace detail |
97 | } // namespace crow |