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
37// mutex, don't they?
44template <typename T>
46 std::queue<T> queue_;
47
48 mutable std::mutex m_;
49 std::condition_variable cv_;
50 uint32_t maxSize_;
51
52public:
59 ThreadSafeQueue(uint32_t maxSize) : maxSize_(maxSize)
60 {
61 }
62
70 void
71 push(T const& elt)
72 {
73 std::unique_lock lck(m_);
74 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
75 queue_.push(elt);
76 cv_.notify_all();
77 }
78
86 void
87 push(T&& elt)
88 {
89 std::unique_lock lck(m_);
90 cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
91 queue_.push(std::move(elt));
92 cv_.notify_all();
93 }
94
102 T
104 {
105 std::unique_lock lck(m_);
106 cv_.wait(lck, [this]() { return !queue_.empty(); });
107
108 T ret = std::move(queue_.front());
109 queue_.pop();
110
111 cv_.notify_all();
112 return ret;
113 }
114
120 std::optional<T>
122 {
123 std::scoped_lock const lck(m_);
124 if (queue_.empty())
125 return {};
126
127 T ret = std::move(queue_.front());
128 queue_.pop();
129
130 cv_.notify_all();
131 return ret;
132 }
133
139 std::size_t
140 size() const
141 {
142 return queue_.size();
143 }
144};
145
152std::vector<ripple::uint256>
153getMarkers(size_t numMarkers);
154
155} // namespace etl
std::optional< T > tryPop()
Attempt to pop an element.
Definition ETLHelpers.hpp:121
std::size_t size() const
Get the size of the queue.
Definition ETLHelpers.hpp:140
void push(T &&elt)
Push element onto the queue.
Definition ETLHelpers.hpp:87
T pop()
Pop element from the queue.
Definition ETLHelpers.hpp:103
void push(T const &elt)
Push element onto the queue.
Definition ETLHelpers.hpp:71
ThreadSafeQueue(uint32_t maxSize)
Create an instance of the queue.
Definition ETLHelpers.hpp:59