mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
451 lines
12 KiB
C
451 lines
12 KiB
C
4 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
4 years ago
|
*
|
||
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_CIRCULAR_BUFFER_H
|
||
|
#define SRSRAN_CIRCULAR_BUFFER_H
|
||
4 years ago
|
|
||
4 years ago
|
#include "srsran/adt/expected.h"
|
||
4 years ago
|
|
||
|
#include <array>
|
||
|
#include <cassert>
|
||
|
#include <condition_variable>
|
||
|
#include <mutex>
|
||
|
#include <thread>
|
||
|
#include <type_traits>
|
||
|
#include <vector>
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
4 years ago
|
|
||
|
namespace detail {
|
||
|
|
||
|
template <typename T, size_t N>
|
||
|
size_t get_max_size(const std::array<T, N>& a)
|
||
|
{
|
||
|
return a.max_size();
|
||
|
}
|
||
|
|
||
|
template <typename T>
|
||
|
size_t get_max_size(const std::vector<T>& a)
|
||
|
{
|
||
|
return a.capacity();
|
||
|
}
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Base common class for definition of circular buffer data structures with the following features:
|
||
|
* - no allocations while pushing/popping new elements. Just an internal index update
|
||
|
* - it provides helper methods to add/remove objects
|
||
|
* - it provides an iterator interface to iterate over added elements in the buffer
|
||
|
* - not thread-safe
|
||
|
* @tparam Container underlying container type used as buffer (e.g. std::array<T, N> or std::vector<T>)
|
||
|
*/
|
||
4 years ago
|
template <typename Container>
|
||
|
class base_circular_buffer
|
||
|
{
|
||
|
using T = typename Container::value_type;
|
||
|
|
||
|
public:
|
||
|
using value_type = T;
|
||
4 years ago
|
using difference_type = typename Container::difference_type;
|
||
4 years ago
|
|
||
|
struct iterator {
|
||
|
iterator(base_circular_buffer<Container>& parent_, size_t i) : parent(&parent_), idx(i) {}
|
||
|
iterator& operator++()
|
||
|
{
|
||
|
idx = (idx + 1) % parent->max_size();
|
||
|
return *this;
|
||
|
}
|
||
|
iterator operator++(int)
|
||
|
{
|
||
|
iterator tmp(*this);
|
||
|
++(*this);
|
||
|
return tmp;
|
||
|
}
|
||
|
iterator operator+(difference_type n)
|
||
|
{
|
||
|
iterator tmp(*this);
|
||
|
tmp += n;
|
||
|
return tmp;
|
||
|
}
|
||
|
iterator& operator+=(difference_type n)
|
||
|
{
|
||
|
idx = (idx + n) % parent->max_size();
|
||
|
return *this;
|
||
|
}
|
||
|
value_type* operator->() { return &parent->buffer[idx]; }
|
||
|
const value_type* operator->() const { return &parent->buffer[idx]; }
|
||
|
value_type& operator*() { return parent->buffer[idx]; }
|
||
|
const value_type& operator*() const { return parent->buffer[idx]; }
|
||
|
bool operator==(const iterator& it) const { return it.parent == parent and it.idx == idx; }
|
||
|
bool operator!=(const iterator& it) const { return not(*this == it); }
|
||
|
|
||
|
private:
|
||
|
base_circular_buffer<Container>* parent;
|
||
|
size_t idx;
|
||
|
};
|
||
|
|
||
|
template <typename... Args>
|
||
4 years ago
|
explicit base_circular_buffer(Args&&... args) : buffer(std::forward<Args>(args)...)
|
||
4 years ago
|
{}
|
||
|
|
||
4 years ago
|
bool try_push(T&& t)
|
||
|
{
|
||
|
if (full()) {
|
||
|
return false;
|
||
|
}
|
||
|
push(std::move(t));
|
||
|
}
|
||
4 years ago
|
void push(T&& t)
|
||
|
{
|
||
|
assert(not full());
|
||
|
size_t wpos = (rpos + count) % max_size();
|
||
|
buffer[wpos] = std::move(t);
|
||
|
count++;
|
||
|
}
|
||
4 years ago
|
bool try_push(const T& t)
|
||
|
{
|
||
|
if (full()) {
|
||
|
return false;
|
||
|
}
|
||
|
push(t);
|
||
|
}
|
||
4 years ago
|
void push(const T& t)
|
||
|
{
|
||
|
assert(not full());
|
||
|
size_t wpos = (rpos + count) % max_size();
|
||
|
buffer[wpos] = t;
|
||
|
count++;
|
||
|
}
|
||
|
void pop()
|
||
|
{
|
||
|
assert(not empty());
|
||
|
rpos = (rpos + 1) % max_size();
|
||
|
count--;
|
||
|
}
|
||
|
T& top()
|
||
|
{
|
||
|
assert(not empty());
|
||
|
return buffer[rpos];
|
||
|
}
|
||
|
const T& top() const
|
||
|
{
|
||
|
assert(not empty());
|
||
|
return buffer[rpos];
|
||
|
}
|
||
|
void clear() { count = 0; }
|
||
|
|
||
|
bool full() const { return count == max_size(); }
|
||
|
bool empty() const { return count == 0; }
|
||
|
size_t size() const { return count; }
|
||
|
size_t max_size() const { return detail::get_max_size(buffer); }
|
||
|
|
||
|
iterator begin() { return iterator(*this, rpos); }
|
||
|
iterator end() { return iterator(*this, (rpos + count) % max_size()); }
|
||
|
|
||
4 years ago
|
template <typename = std::enable_if<std::is_same<Container, std::vector<T> >::value> >
|
||
|
void set_size(size_t size)
|
||
|
{
|
||
|
buffer.resize(size);
|
||
|
}
|
||
|
|
||
4 years ago
|
private:
|
||
|
Container buffer;
|
||
|
size_t rpos = 0;
|
||
|
size_t count = 0;
|
||
|
};
|
||
|
|
||
4 years ago
|
struct noop_operator {
|
||
|
template <typename T>
|
||
|
void operator()(const T&)
|
||
|
{
|
||
|
// noop
|
||
|
}
|
||
|
};
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Base common class for definition of blocking queue data structures with the following features:
|
||
|
* - it stores pushed/popped samples in an internal circular buffer
|
||
|
* - provides blocking and non-blocking push/pop APIs
|
||
|
* - thread-safe
|
||
|
* @tparam CircBuffer underlying circular buffer data type (e.g. static_circular_buffer<T, N> or dyn_circular_buffer<T>)
|
||
|
* @tparam PushingFunc function void(const T&) called while pushing an element to the queue
|
||
|
* @tparam PoppingFunc function void(const T&) called while popping an element from the queue
|
||
|
*/
|
||
4 years ago
|
template <typename CircBuffer, typename PushingFunc, typename PoppingFunc>
|
||
4 years ago
|
class base_blocking_queue
|
||
4 years ago
|
{
|
||
|
using T = typename CircBuffer::value_type;
|
||
|
|
||
|
public:
|
||
|
template <typename... Args>
|
||
4 years ago
|
base_blocking_queue(PushingFunc push_func_, PoppingFunc pop_func_, Args&&... args) :
|
||
4 years ago
|
circ_buffer(std::forward<Args>(args)...), push_func(push_func_), pop_func(pop_func_)
|
||
4 years ago
|
{}
|
||
|
|
||
|
void stop()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
if (active) {
|
||
|
active = false;
|
||
|
if (nof_waiting == 0) {
|
||
|
return;
|
||
|
}
|
||
|
do {
|
||
|
lock.unlock();
|
||
|
cvar_empty.notify_all();
|
||
|
cvar_full.notify_all();
|
||
|
std::this_thread::yield();
|
||
|
lock.lock();
|
||
|
} while (nof_waiting > 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool try_push(const T& t) { return push_(t, false); }
|
||
4 years ago
|
srsran::error_type<T> try_push(T&& t) { return push_(std::move(t), false); }
|
||
4 years ago
|
bool push_blocking(const T& t) { return push_(t, true); }
|
||
4 years ago
|
srsran::error_type<T> push_blocking(T&& t) { return push_(std::move(t), true); }
|
||
4 years ago
|
bool try_pop(T& obj) { return pop_(obj, false); }
|
||
4 years ago
|
T pop_blocking()
|
||
4 years ago
|
{
|
||
|
T obj{};
|
||
|
pop_(obj, true);
|
||
|
return obj;
|
||
|
}
|
||
4 years ago
|
bool pop_wait_until(T& obj, const std::chrono::system_clock::time_point& until) { return pop_(obj, true, &until); }
|
||
4 years ago
|
void clear()
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
T obj;
|
||
|
while (pop_(obj, false)) {
|
||
|
}
|
||
|
}
|
||
|
|
||
|
size_t size() const
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
return circ_buffer.size();
|
||
|
}
|
||
4 years ago
|
bool empty() const
|
||
4 years ago
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
return circ_buffer.empty();
|
||
|
}
|
||
4 years ago
|
bool full() const
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
return circ_buffer.full();
|
||
|
}
|
||
4 years ago
|
size_t max_size() const
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
return circ_buffer.max_size();
|
||
|
}
|
||
|
bool is_stopped() const
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
return not active;
|
||
|
}
|
||
4 years ago
|
template <typename F>
|
||
|
bool try_call_on_front(F&& f)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
if (not circ_buffer.empty()) {
|
||
|
f(circ_buffer.top());
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
protected:
|
||
4 years ago
|
bool active = true;
|
||
|
uint8_t nof_waiting = 0;
|
||
|
mutable std::mutex mutex;
|
||
|
std::condition_variable cvar_empty, cvar_full;
|
||
4 years ago
|
PushingFunc push_func;
|
||
|
PoppingFunc pop_func;
|
||
4 years ago
|
CircBuffer circ_buffer;
|
||
|
|
||
4 years ago
|
~base_blocking_queue() { stop(); }
|
||
|
|
||
4 years ago
|
bool push_(const T& t, bool block_mode)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
if (not active) {
|
||
|
return false;
|
||
|
}
|
||
|
if (circ_buffer.full()) {
|
||
|
if (not block_mode) {
|
||
|
return false;
|
||
|
}
|
||
|
nof_waiting++;
|
||
|
while (circ_buffer.full() and active) {
|
||
|
cvar_full.wait(lock);
|
||
|
}
|
||
|
nof_waiting--;
|
||
|
if (not active) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
4 years ago
|
push_func(t);
|
||
4 years ago
|
circ_buffer.push(t);
|
||
|
lock.unlock();
|
||
|
cvar_empty.notify_one();
|
||
|
return true;
|
||
|
}
|
||
4 years ago
|
srsran::error_type<T> push_(T&& t, bool block_mode)
|
||
4 years ago
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
if (not active) {
|
||
|
return std::move(t);
|
||
|
}
|
||
|
if (circ_buffer.full()) {
|
||
|
if (not block_mode) {
|
||
|
return std::move(t);
|
||
|
}
|
||
|
nof_waiting++;
|
||
|
while (circ_buffer.full() and active) {
|
||
|
cvar_full.wait(lock);
|
||
|
}
|
||
|
nof_waiting--;
|
||
|
if (not active) {
|
||
|
return std::move(t);
|
||
|
}
|
||
|
}
|
||
4 years ago
|
push_func(t);
|
||
|
circ_buffer.push(std::move(t));
|
||
4 years ago
|
lock.unlock();
|
||
|
cvar_empty.notify_one();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
4 years ago
|
bool pop_(T& obj, bool block, const std::chrono::system_clock::time_point* until = nullptr)
|
||
4 years ago
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
if (not active) {
|
||
|
return false;
|
||
|
}
|
||
|
if (circ_buffer.empty()) {
|
||
|
if (not block) {
|
||
|
return false;
|
||
|
}
|
||
|
nof_waiting++;
|
||
4 years ago
|
if (until == nullptr) {
|
||
4 years ago
|
cvar_empty.wait(lock, [this]() { return not circ_buffer.empty() or not active; });
|
||
|
} else {
|
||
4 years ago
|
cvar_empty.wait_until(lock, *until, [this]() { return not circ_buffer.empty() or not active; });
|
||
4 years ago
|
}
|
||
|
nof_waiting--;
|
||
4 years ago
|
if (circ_buffer.empty()) {
|
||
|
// either queue got deactivated or there was a timeout
|
||
4 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
obj = std::move(circ_buffer.top());
|
||
4 years ago
|
pop_func(obj);
|
||
4 years ago
|
circ_buffer.pop();
|
||
|
lock.unlock();
|
||
|
cvar_full.notify_one();
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace detail
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Circular buffer with fixed, embedded buffer storage via a std::array<T, N>.
|
||
|
* - Single allocation at object creation for std::array. Given that the buffer size is known at compile-time, the
|
||
|
* circular iteration over the buffer may be more optimized (e.g. when N is a power of 2, % operator can be avoided)
|
||
|
* - not thread-safe
|
||
|
* @tparam T value type stored by buffer
|
||
|
* @tparam N size of the queue
|
||
|
*/
|
||
4 years ago
|
template <typename T, size_t N>
|
||
|
class static_circular_buffer : public detail::base_circular_buffer<std::array<T, N> >
|
||
|
{};
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Circular buffer with buffer storage via a std::vector<T>.
|
||
|
* - size can be defined at run-time.
|
||
|
* - not thread-safe
|
||
|
* @tparam T value type stored by buffer
|
||
|
*/
|
||
4 years ago
|
template <typename T>
|
||
|
class dyn_circular_buffer : public detail::base_circular_buffer<std::vector<T> >
|
||
|
{
|
||
|
using base_t = detail::base_circular_buffer<std::vector<T> >;
|
||
|
|
||
|
public:
|
||
|
dyn_circular_buffer() = default;
|
||
|
explicit dyn_circular_buffer(size_t size) : base_t(size) {}
|
||
|
|
||
|
void set_size(size_t size)
|
||
|
{
|
||
|
// Note: dynamic resizes not supported.
|
||
|
assert(base_t::empty());
|
||
4 years ago
|
base_t::set_size(size);
|
||
4 years ago
|
}
|
||
|
};
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Blocking queue with fixed, embedded buffer storage via a std::array<T, N>.
|
||
|
* - Blocking push/pop API via push_blocking(...) and pop_blocking(...) methods
|
||
|
* - Non-blocking push/pop API via try_push(...) and try_pop(...) methods
|
||
|
* - Only one initial allocation for the std::array<T, N>
|
||
|
* - thread-safe
|
||
|
* @tparam T value type stored by buffer
|
||
|
* @tparam N size of queue
|
||
|
* @tparam PushingCallback function void(const T&) called while pushing an element to the queue
|
||
|
* @tparam PoppingCallback function void(const T&) called while popping an element from the queue
|
||
|
*/
|
||
4 years ago
|
template <typename T,
|
||
|
size_t N,
|
||
|
typename PushingCallback = detail::noop_operator,
|
||
|
typename PoppingCallback = detail::noop_operator>
|
||
4 years ago
|
class static_blocking_queue
|
||
|
: public detail::base_blocking_queue<static_circular_buffer<T, N>, PushingCallback, PoppingCallback>
|
||
4 years ago
|
{
|
||
4 years ago
|
using base_t = detail::base_blocking_queue<static_circular_buffer<T, N>, PushingCallback, PoppingCallback>;
|
||
4 years ago
|
|
||
4 years ago
|
public:
|
||
4 years ago
|
explicit static_blocking_queue(PushingCallback push_callback = {}, PoppingCallback pop_callback = {}) :
|
||
4 years ago
|
base_t(push_callback, pop_callback)
|
||
|
{}
|
||
|
};
|
||
|
|
||
4 years ago
|
/**
|
||
|
* Blocking queue with buffer storage represented via a std::vector<T>. Features:
|
||
|
* - Blocking push/pop API via push_blocking(...) and pop_blocking(...) methods
|
||
|
* - Non-blocking push/pop API via try_push(...) and try_pop(...) methods
|
||
|
* - Size can be defined at runtime.
|
||
|
* - thread-safe
|
||
|
* @tparam T value type stored by buffer
|
||
|
* @tparam PushingCallback function void(const T&) called while pushing an element to the queue
|
||
|
* @tparam PoppingCallback function void(const T&) called while popping an element from the queue
|
||
|
*/
|
||
4 years ago
|
template <typename T,
|
||
|
typename PushingCallback = detail::noop_operator,
|
||
|
typename PoppingCallback = detail::noop_operator>
|
||
4 years ago
|
class dyn_blocking_queue : public detail::base_blocking_queue<dyn_circular_buffer<T>, PushingCallback, PoppingCallback>
|
||
4 years ago
|
{
|
||
4 years ago
|
using base_t = detail::base_blocking_queue<dyn_circular_buffer<T>, PushingCallback, PoppingCallback>;
|
||
4 years ago
|
|
||
|
public:
|
||
4 years ago
|
dyn_blocking_queue() = default;
|
||
|
explicit dyn_blocking_queue(size_t size, PushingCallback push_callback = {}, PoppingCallback pop_callback = {}) :
|
||
4 years ago
|
base_t(push_callback, pop_callback, size)
|
||
|
{}
|
||
4 years ago
|
void set_size(size_t size) { base_t::circ_buffer.set_size(size); }
|
||
|
};
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
4 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_CIRCULAR_BUFFER_H
|