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.
231 lines
5.3 KiB
C
231 lines
5.3 KiB
C
4 years ago
|
/**
|
||
8 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
8 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
8 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.
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
7 years ago
|
/******************************************************************************
|
||
|
* File: block_queue.h
|
||
|
* Description: General-purpose blocking queue. It can behave as a bounded or
|
||
|
* unbounded blocking queue and allows blocking and non-blocking
|
||
|
* operations in both push and pop
|
||
|
*****************************************************************************/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_BLOCK_QUEUE_H
|
||
|
#define SRSRAN_BLOCK_QUEUE_H
|
||
8 years ago
|
|
||
4 years ago
|
#include "srsran/adt/expected.h"
|
||
8 years ago
|
#include <memory>
|
||
|
#include <pthread.h>
|
||
5 years ago
|
#include <queue>
|
||
7 years ago
|
#include <stdint.h>
|
||
5 years ago
|
#include <stdio.h>
|
||
7 years ago
|
#include <strings.h>
|
||
5 years ago
|
#include <unistd.h>
|
||
|
#include <utility>
|
||
7 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
8 years ago
|
|
||
5 years ago
|
template <typename myobj>
|
||
|
class block_queue
|
||
|
{
|
||
8 years ago
|
|
||
|
public:
|
||
7 years ago
|
// Callback functions for mutexed operations inside pop/push methods
|
||
5 years ago
|
class call_mutexed_itf
|
||
|
{
|
||
7 years ago
|
public:
|
||
6 years ago
|
virtual void popping(const myobj& obj) = 0;
|
||
|
virtual void pushing(const myobj& obj) = 0;
|
||
7 years ago
|
};
|
||
|
|
||
6 years ago
|
explicit block_queue<myobj>(int capacity_ = -1)
|
||
|
{
|
||
8 years ago
|
pthread_mutex_init(&mutex, NULL);
|
||
7 years ago
|
pthread_cond_init(&cv_empty, NULL);
|
||
|
pthread_cond_init(&cv_full, NULL);
|
||
6 years ago
|
capacity = capacity_;
|
||
7 years ago
|
mutexed_callback = NULL;
|
||
5 years ago
|
enable = true;
|
||
|
num_threads = 0;
|
||
7 years ago
|
}
|
||
5 years ago
|
~block_queue()
|
||
|
{
|
||
7 years ago
|
// Unlock threads waiting at push or pop
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
enable = false;
|
||
|
pthread_cond_signal(&cv_full);
|
||
|
pthread_cond_signal(&cv_empty);
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
|
||
|
// Wait threads blocked in push/pop to exit
|
||
5 years ago
|
while (num_threads > 0) {
|
||
7 years ago
|
usleep(100);
|
||
|
}
|
||
5 years ago
|
|
||
7 years ago
|
// Wait them to exit and destroy cv and mutex
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
pthread_cond_destroy(&cv_full);
|
||
|
pthread_cond_destroy(&cv_empty);
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
pthread_mutex_destroy(&mutex);
|
||
7 years ago
|
}
|
||
5 years ago
|
void set_mutexed_itf(call_mutexed_itf* itf) { mutexed_callback = itf; }
|
||
|
void resize(int new_capacity) { capacity = new_capacity; }
|
||
7 years ago
|
|
||
5 years ago
|
void push(const myobj& value) { push_(value, true); }
|
||
7 years ago
|
|
||
6 years ago
|
void push(myobj&& value) { push_(std::move(value), true); }
|
||
|
|
||
5 years ago
|
bool try_push(const myobj& value) { return push_(value, false); }
|
||
8 years ago
|
|
||
4 years ago
|
srsran::error_type<myobj> try_push(myobj&& value) { return push_(std::move(value), false); }
|
||
6 years ago
|
|
||
5 years ago
|
bool try_pop(myobj* value) { return pop_(value, false); }
|
||
8 years ago
|
|
||
5 years ago
|
myobj wait_pop()
|
||
|
{ // blocking pop
|
||
6 years ago
|
myobj value = myobj();
|
||
7 years ago
|
pop_(&value, true);
|
||
8 years ago
|
return value;
|
||
|
}
|
||
|
|
||
5 years ago
|
bool empty()
|
||
|
{ // queue is empty?
|
||
8 years ago
|
pthread_mutex_lock(&mutex);
|
||
|
bool ret = q.empty();
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
5 years ago
|
bool full()
|
||
|
{ // queue is full?
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
bool ret = not check_queue_space_unlocked(false);
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
5 years ago
|
void clear()
|
||
|
{ // remove all items
|
||
|
myobj* item = NULL;
|
||
|
while (try_pop(item))
|
||
|
;
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
const myobj& front() const { return q.front(); }
|
||
7 years ago
|
|
||
5 years ago
|
size_t size() { return q.size(); }
|
||
7 years ago
|
|
||
8 years ago
|
private:
|
||
5 years ago
|
bool pop_(myobj* value, bool block)
|
||
|
{
|
||
7 years ago
|
if (!enable) {
|
||
|
return false;
|
||
|
}
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
num_threads++;
|
||
|
bool ret = false;
|
||
|
if (q.empty() && !block) {
|
||
|
goto exit;
|
||
|
}
|
||
|
while (q.empty() && enable) {
|
||
|
pthread_cond_wait(&cv_empty, &mutex);
|
||
|
}
|
||
|
if (!enable) {
|
||
|
goto exit;
|
||
|
}
|
||
|
if (value) {
|
||
6 years ago
|
*value = std::move(q.front());
|
||
7 years ago
|
}
|
||
|
if (mutexed_callback) {
|
||
5 years ago
|
mutexed_callback->popping(*value); // TODO: Value might be null!
|
||
7 years ago
|
}
|
||
6 years ago
|
q.pop();
|
||
|
ret = true;
|
||
7 years ago
|
pthread_cond_signal(&cv_full);
|
||
|
exit:
|
||
|
num_threads--;
|
||
|
pthread_mutex_unlock(&mutex);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool check_queue_space_unlocked(bool block)
|
||
6 years ago
|
{
|
||
7 years ago
|
num_threads++;
|
||
|
if (capacity > 0) {
|
||
|
if (block) {
|
||
5 years ago
|
while (q.size() >= (uint32_t)capacity && enable) {
|
||
7 years ago
|
pthread_cond_wait(&cv_full, &mutex);
|
||
|
}
|
||
|
if (!enable) {
|
||
6 years ago
|
num_threads--;
|
||
6 years ago
|
return false;
|
||
7 years ago
|
}
|
||
5 years ago
|
} else if (q.size() >= (uint32_t)capacity) {
|
||
6 years ago
|
num_threads--;
|
||
6 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
num_threads--;
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
4 years ago
|
srsran::error_type<myobj> push_(myobj&& value, bool block)
|
||
6 years ago
|
{
|
||
|
if (!enable) {
|
||
5 years ago
|
return std::move(value);
|
||
6 years ago
|
}
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
bool ret = check_queue_space_unlocked(block);
|
||
|
if (ret) {
|
||
|
if (mutexed_callback) {
|
||
|
mutexed_callback->pushing(value);
|
||
|
}
|
||
|
q.push(std::move(value));
|
||
5 years ago
|
pthread_mutex_unlock(&mutex);
|
||
6 years ago
|
pthread_cond_signal(&cv_empty);
|
||
5 years ago
|
return {};
|
||
6 years ago
|
}
|
||
|
pthread_mutex_unlock(&mutex);
|
||
5 years ago
|
return std::move(value);
|
||
6 years ago
|
}
|
||
|
|
||
|
bool push_(const myobj& value, bool block)
|
||
|
{
|
||
|
if (!enable) {
|
||
|
return false;
|
||
|
}
|
||
|
pthread_mutex_lock(&mutex);
|
||
|
bool ret = check_queue_space_unlocked(block);
|
||
|
if (ret) {
|
||
|
if (mutexed_callback) {
|
||
|
mutexed_callback->pushing(value);
|
||
|
}
|
||
|
q.push(value);
|
||
|
pthread_cond_signal(&cv_empty);
|
||
|
}
|
||
7 years ago
|
pthread_mutex_unlock(&mutex);
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
5 years ago
|
std::queue<myobj> q;
|
||
|
pthread_mutex_t mutex;
|
||
|
pthread_cond_t cv_empty;
|
||
|
pthread_cond_t cv_full;
|
||
|
call_mutexed_itf* mutexed_callback;
|
||
|
int capacity;
|
||
|
bool enable;
|
||
|
uint32_t num_threads;
|
||
8 years ago
|
};
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
8 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_BLOCK_QUEUE_H
|