5#include <userver/engine/deadline.hpp>
14 template <
typename LockFreeQueue>
15 explicit NoToken(LockFreeQueue& ) {}
18struct MultiToken
final {
19 template <
typename LockFreeQueue>
20 explicit MultiToken(LockFreeQueue& ) {}
27template <
typename QueueType,
typename ProducerToken,
28 typename EmplaceEnablerType>
31 std::is_same_v<EmplaceEnablerType,
typename QueueType::EmplaceEnabler>,
32 "Do not instantiate Producer on your own. Use Producer type alias "
35 using ValueType =
typename QueueType::ValueType;
38 Producer(
const Producer&) =
delete;
39 Producer(Producer&&)
noexcept =
default;
40 Producer& operator=(
const Producer&) =
delete;
41 Producer& operator=(Producer&&)
noexcept =
default;
44 if (queue_) queue_->MarkProducerIsDead();
51 [[nodiscard]]
bool Push(ValueType&& value,
52 engine::Deadline deadline
= {})
const {
54 return queue_->Push(token_, std::move(value), deadline);
61 [[nodiscard]]
bool PushNoblock(ValueType&& value)
const {
63 return queue_->PushNoblock(token_, std::move(value));
67 if (queue_) queue_->MarkProducerIsDead();
69 [[maybe_unused]] ProducerToken for_destruction = std::move(token_);
73 [[nodiscard]] std::shared_ptr<
const QueueType>
Queue()
const {
79 Producer(std::shared_ptr<QueueType> queue, EmplaceEnablerType )
80 : queue_(std::move(queue)), token_(queue_->queue_) {}
84 std::shared_ptr<QueueType> queue_;
85 mutable ProducerToken token_;
90template <
typename QueueType,
typename ConsumerToken,
91 typename EmplaceEnablerType>
94 std::is_same_v<EmplaceEnablerType,
typename QueueType::EmplaceEnabler>,
95 "Do not instantiate Consumer on your own. Use Consumer type alias "
98 using ValueType =
typename QueueType::ValueType;
101 Consumer(
const Consumer&) =
delete;
102 Consumer(Consumer&&)
noexcept =
default;
103 Consumer& operator=(
const Consumer&) =
delete;
104 Consumer& operator=(Consumer&&)
noexcept =
default;
107 if (queue_) queue_->MarkConsumerIsDead();
115 [[nodiscard]]
bool Pop(ValueType& value,
116 engine::Deadline deadline
= {})
const {
117 return queue_->Pop(token_, value, deadline);
123 [[nodiscard]]
bool PopNoblock(ValueType& value)
const {
124 return queue_->PopNoblock(token_, value);
128 [[nodiscard]] std::shared_ptr<
const QueueType>
Queue()
const {
134 Consumer(std::shared_ptr<QueueType> queue, EmplaceEnablerType )
135 : queue_(std::move(queue)), token_(queue_->queue_) {}
139 std::shared_ptr<QueueType> queue_{};
140 mutable ConsumerToken token_;