Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Repeat.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include "util/Assert.hpp"
23
24#include <boost/asio/io_context.hpp>
25#include <boost/asio/post.hpp>
26#include <boost/asio/steady_timer.hpp>
27
28#include <atomic>
29#include <chrono>
30#include <concepts>
31#include <memory>
32#include <semaphore>
33#include <utility>
34
35namespace util {
36
41class Repeat {
42 struct Control {
43 boost::asio::steady_timer timer;
44 std::atomic_bool stopping{true};
45 std::binary_semaphore semaphore{0};
46
47 Control(auto& ctx) : timer(ctx)
48 {
49 }
50 };
51
52 std::shared_ptr<Control> control_;
53
54public:
61 Repeat(auto& ctx) : control_(std::make_unique<Control>(ctx))
62 {
63 }
64
65 Repeat(Repeat const&) = delete;
66 Repeat&
67 operator=(Repeat const&) = delete;
68 Repeat(Repeat&&) = default;
69 Repeat&
70 operator=(Repeat&&) = default;
71
76 void
77 stop();
78
87 template <std::invocable Action>
88 void
89 start(std::chrono::steady_clock::duration interval, Action&& action)
90 {
91 ASSERT(control_->stopping, "Should be stopped before starting");
92 control_->stopping = false;
93 startImpl(control_, interval, std::forward<Action>(action));
94 }
95
96private:
97 template <std::invocable Action>
98 static void
99 startImpl(std::shared_ptr<Control> control, std::chrono::steady_clock::duration interval, Action&& action)
100 {
101 control->timer.expires_after(interval);
102 control->timer.async_wait([control, interval, action = std::forward<Action>(action)](auto const& ec) mutable {
103 if (ec or control->stopping) {
104 control->semaphore.release();
105 return;
106 }
107 action();
108
109 startImpl(std::move(control), interval, std::forward<Action>(action));
110 });
111 }
112};
113
114} // namespace util
A class to repeat some action at a regular interval.
Definition Repeat.hpp:41
void stop()
Stop repeating.
Definition Repeat.cpp:25
void start(std::chrono::steady_clock::duration interval, Action &&action)
Start asynchronously repeating.
Definition Repeat.hpp:89
Repeat(auto &ctx)
Construct a new Repeat object.
Definition Repeat.hpp:61
This namespace contains various utilities.
Definition AccountUtils.hpp:30