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.
325 lines
8.3 KiB
C
325 lines
8.3 KiB
C
4 years ago
|
/**
|
||
5 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
5 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
5 years ago
|
*
|
||
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.
|
||
5 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
/******************************************************************************
|
||
|
* File: multiqueue.h
|
||
|
* Description: General-purpose non-blocking multiqueue. It behaves as a list
|
||
|
* of bounded/unbounded queues.
|
||
|
*****************************************************************************/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_MULTIQUEUE_H
|
||
|
#define SRSRAN_MULTIQUEUE_H
|
||
5 years ago
|
|
||
4 years ago
|
#include "srsran/adt/move_callback.h"
|
||
5 years ago
|
#include <algorithm>
|
||
|
#include <condition_variable>
|
||
5 years ago
|
#include <functional>
|
||
5 years ago
|
#include <mutex>
|
||
|
#include <queue>
|
||
|
#include <vector>
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
5 years ago
|
#define MULTIQUEUE_DEFAULT_CAPACITY (8192) // Default per-queue capacity
|
||
|
|
||
5 years ago
|
template <typename myobj>
|
||
|
class multiqueue_handler
|
||
|
{
|
||
5 years ago
|
class circular_buffer
|
||
5 years ago
|
{
|
||
|
public:
|
||
5 years ago
|
circular_buffer(uint32_t cap) : buffer(cap + 1) {}
|
||
|
circular_buffer(circular_buffer&& other) noexcept
|
||
|
{
|
||
|
active = other.active;
|
||
|
other.active = false;
|
||
|
widx = other.widx;
|
||
|
ridx = other.ridx;
|
||
|
buffer = std::move(other.buffer);
|
||
|
}
|
||
5 years ago
|
|
||
|
std::condition_variable cv_full;
|
||
|
bool active = true;
|
||
5 years ago
|
|
||
|
bool empty() const { return widx == ridx; }
|
||
|
size_t size() const { return widx >= ridx ? widx - ridx : widx + (buffer.size() - ridx); }
|
||
|
bool full() const { return (ridx > 0) ? widx == ridx - 1 : widx == buffer.size() - 1; }
|
||
|
size_t capacity() const { return buffer.size() - 1; }
|
||
|
|
||
|
template <typename T>
|
||
|
void push(T&& o) noexcept
|
||
|
{
|
||
|
buffer[widx++] = std::forward<T>(o);
|
||
|
if (widx >= buffer.size()) {
|
||
|
widx = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void pop() noexcept
|
||
|
{
|
||
|
ridx++;
|
||
|
if (ridx >= buffer.size()) {
|
||
|
ridx = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
myobj& front() noexcept { return buffer[ridx]; }
|
||
|
const myobj& front() const noexcept { return buffer[ridx]; }
|
||
|
|
||
|
private:
|
||
|
std::vector<myobj> buffer;
|
||
|
size_t widx = 0, ridx = 0;
|
||
5 years ago
|
};
|
||
|
|
||
|
public:
|
||
5 years ago
|
class queue_handle
|
||
5 years ago
|
{
|
||
|
public:
|
||
5 years ago
|
queue_handle() = default;
|
||
|
queue_handle(multiqueue_handler<myobj>* parent_, int id) : parent(parent_), queue_id(id) {}
|
||
5 years ago
|
template <typename FwdRef>
|
||
|
void push(FwdRef&& value)
|
||
|
{
|
||
|
parent->push(queue_id, std::forward<FwdRef>(value));
|
||
|
}
|
||
|
bool try_push(const myobj& value) { return parent->try_push(queue_id, value); }
|
||
|
std::pair<bool, myobj> try_push(myobj&& value) { return parent->try_push(queue_id, std::move(value)); }
|
||
5 years ago
|
size_t size() { return parent->size(queue_id); }
|
||
5 years ago
|
|
||
|
private:
|
||
|
multiqueue_handler<myobj>* parent = nullptr;
|
||
|
int queue_id = -1;
|
||
|
};
|
||
|
|
||
5 years ago
|
explicit multiqueue_handler(uint32_t capacity_ = MULTIQUEUE_DEFAULT_CAPACITY) : capacity(capacity_) {}
|
||
5 years ago
|
~multiqueue_handler() { reset(); }
|
||
|
|
||
|
void reset()
|
||
5 years ago
|
{
|
||
5 years ago
|
std::unique_lock<std::mutex> lock(mutex);
|
||
5 years ago
|
running = false;
|
||
5 years ago
|
while (nof_threads_waiting > 0) {
|
||
|
uint32_t size = queues.size();
|
||
|
cv_empty.notify_one();
|
||
|
for (uint32_t i = 0; i < size; ++i) {
|
||
|
queues[i].cv_full.notify_all();
|
||
|
}
|
||
|
// wait for all threads to unblock
|
||
|
cv_exit.wait(lock);
|
||
|
}
|
||
|
queues.clear();
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Adds a new queue with fixed capacity
|
||
|
* @param capacity_ The capacity of the queue.
|
||
|
* @return The index of the newly created (or reused) queue within the vector of queues.
|
||
|
*/
|
||
|
int add_queue(uint32_t capacity_)
|
||
5 years ago
|
{
|
||
5 years ago
|
uint32_t qidx = 0;
|
||
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
if (not running) {
|
||
|
return -1;
|
||
|
}
|
||
|
for (; qidx < queues.size() and queues[qidx].active; ++qidx)
|
||
5 years ago
|
;
|
||
5 years ago
|
|
||
|
// check if there is a free queue of the required size
|
||
|
if (qidx == queues.size() || queues[qidx].capacity() != capacity_) {
|
||
5 years ago
|
// create new queue
|
||
5 years ago
|
queues.emplace_back(capacity_);
|
||
|
qidx = queues.size() - 1; // update qidx to the last element
|
||
5 years ago
|
} else {
|
||
5 years ago
|
queues[qidx].active = true;
|
||
5 years ago
|
}
|
||
|
return (int)qidx;
|
||
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
* Add queue using the default capacity of the underlying multiqueue
|
||
|
* @return The queue index
|
||
|
*/
|
||
|
int add_queue() { return add_queue(capacity); }
|
||
|
|
||
5 years ago
|
int nof_queues()
|
||
|
{
|
||
5 years ago
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
uint32_t count = 0;
|
||
|
for (uint32_t i = 0; i < queues.size(); ++i) {
|
||
|
count += queues[i].active ? 1 : 0;
|
||
|
}
|
||
|
return count;
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
template <typename FwdRef>
|
||
|
void push(int q_idx, FwdRef&& value)
|
||
5 years ago
|
{
|
||
5 years ago
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
5 years ago
|
while (is_queue_active_(q_idx) and queues[q_idx].full()) {
|
||
5 years ago
|
nof_threads_waiting++;
|
||
|
queues[q_idx].cv_full.wait(lock);
|
||
|
nof_threads_waiting--;
|
||
|
}
|
||
|
if (not is_queue_active_(q_idx)) {
|
||
|
cv_exit.notify_one();
|
||
|
return;
|
||
|
}
|
||
|
queues[q_idx].push(std::forward<FwdRef>(value));
|
||
5 years ago
|
}
|
||
5 years ago
|
cv_empty.notify_one();
|
||
|
}
|
||
|
|
||
|
bool try_push(int q_idx, const myobj& value)
|
||
|
{
|
||
5 years ago
|
{
|
||
5 years ago
|
std::lock_guard<std::mutex> lock(mutex);
|
||
5 years ago
|
if (not is_queue_active_(q_idx) or queues[q_idx].full()) {
|
||
5 years ago
|
return false;
|
||
|
}
|
||
|
queues[q_idx].push(value);
|
||
|
}
|
||
5 years ago
|
cv_empty.notify_one();
|
||
5 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
std::pair<bool, myobj> try_push(int q_idx, myobj&& value)
|
||
|
{
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
5 years ago
|
if (not is_queue_active_(q_idx) or queues[q_idx].full()) {
|
||
5 years ago
|
return {false, std::move(value)};
|
||
|
}
|
||
|
queues[q_idx].push(std::move(value));
|
||
|
}
|
||
5 years ago
|
cv_empty.notify_one();
|
||
5 years ago
|
return {true, std::move(value)};
|
||
|
}
|
||
|
|
||
|
int wait_pop(myobj* value)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
while (running) {
|
||
5 years ago
|
if (round_robin_pop_(value)) {
|
||
|
if (nof_threads_waiting > 0) {
|
||
|
lock.unlock();
|
||
|
queues[spin_idx].cv_full.notify_one();
|
||
5 years ago
|
}
|
||
5 years ago
|
return spin_idx;
|
||
5 years ago
|
}
|
||
5 years ago
|
nof_threads_waiting++;
|
||
|
cv_empty.wait(lock);
|
||
|
nof_threads_waiting--;
|
||
5 years ago
|
}
|
||
5 years ago
|
cv_exit.notify_one();
|
||
5 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
5 years ago
|
int try_pop(myobj* value)
|
||
|
{
|
||
|
std::unique_lock<std::mutex> lock(mutex);
|
||
|
if (running) {
|
||
|
if (round_robin_pop_(value)) {
|
||
|
if (nof_threads_waiting > 0) {
|
||
|
lock.unlock();
|
||
|
queues[spin_idx].cv_full.notify_one();
|
||
|
}
|
||
|
return spin_idx;
|
||
|
}
|
||
|
// didn't find any task
|
||
|
return -1;
|
||
|
}
|
||
|
cv_exit.notify_one();
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
5 years ago
|
bool empty(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
|
return queues[qidx].empty();
|
||
|
}
|
||
|
|
||
|
size_t size(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
|
return queues[qidx].size();
|
||
|
}
|
||
|
|
||
5 years ago
|
size_t max_size(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
|
return queues[qidx].capacity();
|
||
|
}
|
||
|
|
||
5 years ago
|
const myobj& front(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
5 years ago
|
return queues[qidx].front();
|
||
5 years ago
|
}
|
||
|
|
||
|
void erase_queue(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
5 years ago
|
if (is_queue_active_(qidx)) {
|
||
|
queues[qidx].active = false;
|
||
5 years ago
|
while (not queues[qidx].empty()) {
|
||
|
queues[qidx].pop();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool is_queue_active(int qidx)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lck(mutex);
|
||
5 years ago
|
return is_queue_active_(qidx);
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
queue_handle get_queue_handler() { return {this, add_queue()}; }
|
||
|
queue_handle get_queue_handler(uint32_t size) { return {this, add_queue(size)}; }
|
||
5 years ago
|
|
||
5 years ago
|
private:
|
||
5 years ago
|
bool is_queue_active_(int qidx) const { return running and queues[qidx].active; }
|
||
|
|
||
5 years ago
|
bool round_robin_pop_(myobj* value)
|
||
|
{
|
||
|
// Round-robin for all queues
|
||
5 years ago
|
for (const circular_buffer& q : queues) {
|
||
5 years ago
|
spin_idx = (spin_idx + 1) % queues.size();
|
||
|
if (is_queue_active_(spin_idx) and not queues[spin_idx].empty()) {
|
||
|
if (value) {
|
||
|
*value = std::move(queues[spin_idx].front());
|
||
|
}
|
||
|
queues[spin_idx].pop();
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
5 years ago
|
std::mutex mutex;
|
||
|
std::condition_variable cv_empty, cv_exit;
|
||
|
uint32_t spin_idx = 0;
|
||
|
bool running = true;
|
||
|
std::vector<circular_buffer> queues;
|
||
|
uint32_t capacity = 0;
|
||
|
uint32_t nof_threads_waiting = 0;
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
//! Specialization for tasks
|
||
5 years ago
|
using task_multiqueue = multiqueue_handler<move_task_t>;
|
||
|
using task_queue_handle = task_multiqueue::queue_handle;
|
||
5 years ago
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_MULTIQUEUE_H
|