Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ETLHelpers.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <xrpl/basics/base_uint.h>
5
6#include <condition_variable>
7#include <cstddef>
8#include <cstdint>
9#include <memory>
10#include <mutex>
11#include <optional>
12#include <queue>
13#include <vector>
14
15namespace etl {
16
17// TODO: does the note make sense? lockfree queues provide the same blocking behaviour just without
18// mutex, don't they?
25template <typename T>
27 std::queue<T> queue_;
28
29 mutable std::mutex m_;
30 std::condition_variable cv_;
31 uint32_t maxSize_;
32
33public:
40 ThreadSafeQueue(uint32_t maxSize) : maxSize_(maxSize)
41 {
42 }
43
51 void
52 push(T const& elt)
53 {
54 std::unique_lock lck(m_);
55 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
56 queue_.push(elt);
57 cv_.notify_all();
58 }
59
67 void
68 push(T&& elt)
69 {
70 std::unique_lock lck(m_);
71 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
72 queue_.push(std::move(elt));
73 cv_.notify_all();
74 }
75
83 T
85 {
86 std::unique_lock lck(m_);
87 cv_.wait(lck, [this]() { return !queue_.empty(); });
88
89 T ret = std::move(queue_.front());
90 queue_.pop();
91
92 cv_.notify_all();
93 return ret;
94 }
95
101 std::optional<T>
103 {
104 std::scoped_lock const lck(m_);
105 if (queue_.empty())
106 return {};
107
108 T ret = std::move(queue_.front());
109 queue_.pop();
110
111 cv_.notify_all();
112 return ret;
113 }
114
120 std::size_t
121 size() const
122 {
123 return queue_.size();
124 }
125};
126
133std::vector<ripple::uint256>
134getMarkers(size_t numMarkers);
135
136} // namespace etl
std::optional< T > tryPop()
Attempt to pop an element.
Definition ETLHelpers.hpp:102
std::size_t size() const
Get the size of the queue.
Definition ETLHelpers.hpp:121
void push(T &&elt)
Push element onto the queue.
Definition ETLHelpers.hpp:68
T pop()
Pop element from the queue.
Definition ETLHelpers.hpp:84
void push(T const &elt)
Push element onto the queue.
Definition ETLHelpers.hpp:52
ThreadSafeQueue(uint32_t maxSize)
Create an instance of the queue.
Definition ETLHelpers.hpp:40