Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ETLHelpers.hpp
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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
21#pragma once
22
23#include <xrpl/basics/base_uint.h>
24
25#include <condition_variable>
26#include <cstddef>
27#include <cstdint>
28#include <memory>
29#include <mutex>
30#include <optional>
31#include <queue>
32#include <vector>
33
34namespace etl {
35
36// TODO: does the note make sense? lockfree queues provide the same blocking behaviour just without mutex, don't they?
43template <typename T>
45 std::queue<T> queue_;
46
47 mutable std::mutex m_;
48 std::condition_variable cv_;
49 uint32_t maxSize_;
50
51public:
58 ThreadSafeQueue(uint32_t maxSize) : maxSize_(maxSize)
59 {
60 }
61
69 void
70 push(T const& elt)
71 {
72 std::unique_lock lck(m_);
73 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
74 queue_.push(elt);
75 cv_.notify_all();
76 }
77
85 void
86 push(T&& elt)
87 {
88 std::unique_lock lck(m_);
89 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
90 queue_.push(std::move(elt));
91 cv_.notify_all();
92 }
93
101 T
103 {
104 std::unique_lock lck(m_);
105 cv_.wait(lck, [this]() { return !queue_.empty(); });
106
107 T ret = std::move(queue_.front());
108 queue_.pop();
109
110 cv_.notify_all();
111 return ret;
112 }
113
119 std::optional<T>
121 {
122 std::scoped_lock const lck(m_);
123 if (queue_.empty())
124 return {};
125
126 T ret = std::move(queue_.front());
127 queue_.pop();
128
129 cv_.notify_all();
130 return ret;
131 }
132
138 std::size_t
139 size() const
140 {
141 return queue_.size();
142 }
143};
144
151std::vector<ripple::uint256>
152getMarkers(size_t numMarkers);
153
154} // namespace etl
Generic thread-safe queue with a max capacity.
Definition ETLHelpers.hpp:44
std::optional< T > tryPop()
Attempt to pop an element.
Definition ETLHelpers.hpp:120
std::size_t size() const
Get the size of the queue.
Definition ETLHelpers.hpp:139
void push(T &&elt)
Push element onto the queue.
Definition ETLHelpers.hpp:86
T pop()
Pop element from the queue.
Definition ETLHelpers.hpp:102
void push(T const &elt)
Push element onto the queue.
Definition ETLHelpers.hpp:70
ThreadSafeQueue(uint32_t maxSize)
Create an instance of the queue.
Definition ETLHelpers.hpp:58
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:36
std::vector< ripple::uint256 > getMarkers(size_t numMarkers)
Parititions the uint256 keyspace into numMarkers partitions, each of equal size.
Definition ETLHelpers.cpp:31