Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
TaskQueue.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, 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 "etl/Models.hpp"
23#include "util/Assert.hpp"
24#include "util/Mutex.hpp"
25
26#include <atomic>
27#include <condition_variable>
28#include <cstddef>
29#include <cstdint>
30#include <mutex>
31#include <optional>
32#include <queue>
33#include <utility>
34#include <vector>
35
36namespace etl::impl {
37
39 [[nodiscard]] bool
40 operator()(model::LedgerData const& lhs, model::LedgerData const& rhs) const noexcept
41 {
42 return lhs.seq > rhs.seq;
43 }
44};
45
51class TaskQueue {
52 struct Data {
53 std::uint32_t expectedSequence;
54 std::priority_queue<
56 std::vector<model::LedgerData>,
58 forwardLoadQueue;
59
60 Data(std::uint32_t seq) : expectedSequence(seq)
61 {
62 }
63 };
64
65 std::size_t limit_;
66 std::uint32_t increment_;
68
69 std::condition_variable cv_;
70 std::atomic_bool stopping_ = false;
71
72public:
73 struct Settings {
74 std::uint32_t startSeq = 0u; // sequence to start from (for dequeue)
75 std::uint32_t increment =
76 1u; // increment sequence by this value once dequeue was successful
77 std::optional<std::size_t> limit = std::nullopt;
78 };
79
86 explicit TaskQueue(Settings settings)
87 : limit_(settings.limit.value_or(0uz))
88 , increment_(settings.increment)
89 , data_(settings.startSeq)
90 {
91 }
92
94 {
95 ASSERT(stopping_, "stop() must be called before destroying the TaskQueue");
96 }
97
105 [[nodiscard]] bool
107 {
108 auto lock = data_.lock();
109
110 if (limit_ == 0uz or lock->forwardLoadQueue.size() < limit_) {
111 lock->forwardLoadQueue.push(std::move(item));
112 cv_.notify_all();
113
114 return true;
115 }
116
117 return false;
118 }
119
125 [[nodiscard]] std::optional<model::LedgerData>
127 {
128 auto lock = data_.lock();
129 std::optional<model::LedgerData> out;
130
131 if (not lock->forwardLoadQueue.empty() &&
132 lock->forwardLoadQueue.top().seq == lock->expectedSequence) {
133 out.emplace(lock->forwardLoadQueue.top());
134 lock->forwardLoadQueue.pop();
135 lock->expectedSequence += increment_;
136 }
137
138 return out;
139 }
140
147 [[nodiscard]] bool
149 {
150 return data_.lock()->forwardLoadQueue.empty();
151 }
152
157 void
159 {
160 if (stopping_)
161 return;
162
163 auto lock = data_.lock<std::unique_lock>();
164 cv_.wait(lock, [&] { return stopping_ or not lock->forwardLoadQueue.empty(); });
165 }
166
171 void
173 {
174 // unblock all waiters
175 stopping_ = true;
176 cv_.notify_all();
177 }
178};
179
180} // namespace etl::impl
A wrapper for std::priority_queue that serialises operations using a mutex.
Definition TaskQueue.hpp:51
void stop()
Notify the queue that it's no longer needed.
Definition TaskQueue.hpp:172
TaskQueue(Settings settings)
Construct a new priority queue.
Definition TaskQueue.hpp:86
bool empty()
Check if the queue is empty.
Definition TaskQueue.hpp:148
void awaitTask()
Awaits for the queue to become non-empty.
Definition TaskQueue.hpp:158
std::optional< model::LedgerData > dequeue()
Dequeue the next available item out of the queue.
Definition TaskQueue.hpp:126
bool enqueue(model::LedgerData item)
Enqueue a new item onto the queue if space is available.
Definition TaskQueue.hpp:106
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:101
Definition TaskQueue.hpp:38
Definition TaskQueue.hpp:73
Represents an entire ledger diff worth of transactions and objects.
Definition Models.hpp:143