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 "etlng/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 etlng::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
50class TaskQueue {
51 struct Data {
52 std::uint32_t expectedSequence;
53 std::priority_queue<model::LedgerData, std::vector<model::LedgerData>, ReverseOrderComparator> forwardLoadQueue;
54
55 Data(std::uint32_t seq) : expectedSequence(seq)
56 {
57 }
58 };
59
60 std::size_t limit_;
61 std::uint32_t increment_;
63
64 std::condition_variable cv_;
65 std::atomic_bool stopping_ = false;
66
67public:
68 struct Settings {
69 std::uint32_t startSeq = 0u; // sequence to start from (for dequeue)
70 std::uint32_t increment = 1u; // increment sequence by this value once dequeue was successful
71 std::optional<std::size_t> limit = std::nullopt;
72 };
73
79 explicit TaskQueue(Settings settings)
80 : limit_(settings.limit.value_or(0uz)), increment_(settings.increment), data_(settings.startSeq)
81 {
82 }
83
85 {
86 ASSERT(stopping_, "stop() must be called before destroying the TaskQueue");
87 }
88
96 [[nodiscard]] bool
98 {
99 auto lock = data_.lock();
100
101 if (limit_ == 0uz or lock->forwardLoadQueue.size() < limit_) {
102 lock->forwardLoadQueue.push(std::move(item));
103 cv_.notify_all();
104
105 return true;
106 }
107
108 return false;
109 }
110
116 [[nodiscard]] std::optional<model::LedgerData>
118 {
119 auto lock = data_.lock();
120 std::optional<model::LedgerData> out;
121
122 if (not lock->forwardLoadQueue.empty() && lock->forwardLoadQueue.top().seq == lock->expectedSequence) {
123 out.emplace(lock->forwardLoadQueue.top());
124 lock->forwardLoadQueue.pop();
125 lock->expectedSequence += increment_;
126 }
127
128 return out;
129 }
130
137 [[nodiscard]] bool
139 {
140 return data_.lock()->forwardLoadQueue.empty();
141 }
142
147 void
149 {
150 if (stopping_)
151 return;
152
153 auto lock = data_.lock<std::unique_lock>();
154 cv_.wait(lock, [&] { return stopping_ or not lock->forwardLoadQueue.empty(); });
155 }
156
161 void
163 {
164 // unblock all waiters
165 stopping_ = true;
166 cv_.notify_all();
167 }
168};
169
170} // namespace etlng::impl
A wrapper for std::priority_queue that serialises operations using a mutex.
Definition TaskQueue.hpp:50
bool enqueue(model::LedgerData item)
Enqueue a new item onto the queue if space is available.
Definition TaskQueue.hpp:97
bool empty()
Check if the queue is empty.
Definition TaskQueue.hpp:138
std::optional< model::LedgerData > dequeue()
Dequeue the next available item out of the queue.
Definition TaskQueue.hpp:117
void awaitTask()
Awaits for the queue to become non-empty.
Definition TaskQueue.hpp:148
TaskQueue(Settings settings)
Construct a new priority queue.
Definition TaskQueue.hpp:79
void stop()
Notify the queue that it's no longer needed.
Definition TaskQueue.hpp:162
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:101
Lock< ProtectedDataType const, LockType, MutexType > lock() const
Lock the mutex and get a lock object allowing access to the protected data.
Definition Mutex.hpp:139
Definition TaskQueue.hpp:38
Definition TaskQueue.hpp:68
Represents an entire ledger diff worth of transactions and objects.
Definition Models.hpp:143