2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/EventFd.h"
|
|
|
|
|
|
|
|
#if !TD_EVENTFD_UNSUPPORTED
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2022-07-14 14:27:06 +02:00
|
|
|
#include "td/utils/port/Mutex.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
// interface like in PollableQueue
|
2018-07-22 23:08:47 +02:00
|
|
|
template <class T>
|
2018-12-31 20:04:05 +01:00
|
|
|
class MpscPollableQueue {
|
|
|
|
public:
|
2018-07-22 23:08:47 +02:00
|
|
|
using ValueType = T;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int reader_wait_nonblock() {
|
|
|
|
auto ready = reader_vector_.size() - reader_pos_;
|
|
|
|
if (ready != 0) {
|
|
|
|
return narrow_cast<int>(ready);
|
|
|
|
}
|
|
|
|
|
2019-07-04 14:07:14 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
auto guard = lock_.lock();
|
|
|
|
if (writer_vector_.empty()) {
|
|
|
|
if (i == 1) {
|
|
|
|
wait_event_fd_ = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reader_vector_.clear();
|
|
|
|
reader_pos_ = 0;
|
|
|
|
std::swap(writer_vector_, reader_vector_);
|
|
|
|
return narrow_cast<int>(reader_vector_.size());
|
|
|
|
}
|
|
|
|
event_fd_.acquire();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-04 15:16:59 +02:00
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-22 23:08:47 +02:00
|
|
|
ValueType reader_get_unsafe() {
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::move(reader_vector_[reader_pos_++]);
|
|
|
|
}
|
|
|
|
void reader_flush() {
|
|
|
|
//nop
|
|
|
|
}
|
2018-07-22 23:08:47 +02:00
|
|
|
void writer_put(ValueType value) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto guard = lock_.lock();
|
|
|
|
writer_vector_.push_back(std::move(value));
|
|
|
|
if (wait_event_fd_) {
|
|
|
|
wait_event_fd_ = false;
|
2019-07-03 21:21:38 +02:00
|
|
|
guard.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
event_fd_.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EventFd &reader_get_event_fd() {
|
|
|
|
return event_fd_;
|
|
|
|
}
|
|
|
|
void writer_flush() {
|
|
|
|
//nop
|
|
|
|
}
|
|
|
|
|
2022-11-07 12:59:18 +01:00
|
|
|
bool is_empty() {
|
|
|
|
auto guard = lock_.lock();
|
|
|
|
return writer_vector_.empty() && reader_vector_.empty();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void init() {
|
|
|
|
event_fd_.init();
|
|
|
|
}
|
|
|
|
void destroy() {
|
|
|
|
if (!event_fd_.empty()) {
|
|
|
|
event_fd_.close();
|
|
|
|
wait_event_fd_ = false;
|
|
|
|
writer_vector_.clear();
|
|
|
|
reader_vector_.clear();
|
|
|
|
reader_pos_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 20:22:25 +02:00
|
|
|
// Just an example of usage
|
2018-12-31 20:04:05 +01:00
|
|
|
int reader_wait() {
|
|
|
|
int res;
|
|
|
|
while ((res = reader_wait_nonblock()) == 0) {
|
2018-09-13 20:22:25 +02:00
|
|
|
reader_get_event_fd().wait(1000);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-07-14 14:27:06 +02:00
|
|
|
Mutex lock_;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool wait_event_fd_{false};
|
|
|
|
EventFd event_fd_;
|
2018-07-22 23:08:47 +02:00
|
|
|
std::vector<ValueType> writer_vector_;
|
|
|
|
std::vector<ValueType> reader_vector_;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t reader_pos_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
// dummy implementation which shouldn't be used
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class MpscPollableQueue {
|
|
|
|
public:
|
|
|
|
using ValueType = T;
|
|
|
|
|
|
|
|
void init() {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class PutValueType>
|
|
|
|
void writer_put(PutValueType &&value) {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void writer_flush() {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
int reader_wait_nonblock() {
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueType reader_get_unsafe() {
|
|
|
|
UNREACHABLE();
|
|
|
|
return ValueType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void reader_flush() {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
|
|
|
MpscPollableQueue() = default;
|
|
|
|
MpscPollableQueue(const MpscPollableQueue &) = delete;
|
|
|
|
MpscPollableQueue &operator=(const MpscPollableQueue &) = delete;
|
|
|
|
MpscPollableQueue(MpscPollableQueue &&) = delete;
|
|
|
|
MpscPollableQueue &operator=(MpscPollableQueue &&) = delete;
|
|
|
|
~MpscPollableQueue() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|
|
|
|
|
|
|
|
#endif
|