SDRPlusPlus/core/src/dsp/stream.h

108 lines
2.5 KiB
C
Raw Normal View History

2020-06-10 04:13:56 +02:00
#pragma once
2020-11-02 03:57:44 +01:00
#include <mutex>
2020-06-10 04:13:56 +02:00
#include <condition_variable>
2020-11-02 03:57:44 +01:00
#include <volk/volk.h>
2020-06-10 04:13:56 +02:00
2020-11-02 03:57:44 +01:00
// 1MB buffer
#define STREAM_BUFFER_SIZE 1000000
2020-06-22 16:45:57 +02:00
namespace dsp {
2020-11-02 03:57:44 +01:00
class untyped_steam {
public:
virtual int aquire() { return -1; }
virtual void write(int size) {}
virtual int read() { return -1; }
virtual void flush() {}
virtual void stopReader() {}
virtual void clearReadStop() {}
virtual void stopWriter() {}
virtual void clearWriteStop() {}
};
2020-06-10 04:13:56 +02:00
template <class T>
2020-11-02 03:57:44 +01:00
class stream : public untyped_steam {
2020-06-10 04:13:56 +02:00
public:
2020-06-10 18:52:07 +02:00
stream() {
2020-11-02 03:57:44 +01:00
data = (T*)volk_malloc(STREAM_BUFFER_SIZE * sizeof(T), volk_get_alignment());
2020-06-10 18:52:07 +02:00
}
2020-11-02 03:57:44 +01:00
int aquire() {
waitReady();
if (writerStop) {
return -1;
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
return 0;
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
void write(int size) {
2020-11-02 16:16:21 +01:00
{
std::lock_guard<std::mutex> lck(sigMtx);
contentSize = size;
dataReady = true;
}
cv.notify_one();
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
int read() {
waitData();
if (readerStop) {
return -1;
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
return contentSize;
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
void flush() {
2020-11-02 16:16:21 +01:00
{
std::lock_guard<std::mutex> lck(sigMtx);
dataReady = false;
}
cv.notify_one();
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
void stopReader() {
2020-11-02 16:16:21 +01:00
{
std::lock_guard<std::mutex> lck(sigMtx);
readerStop = true;
}
cv.notify_one();
2020-06-10 04:13:56 +02:00
}
2020-11-02 03:57:44 +01:00
void clearReadStop() {
readerStop = false;
2020-06-15 15:53:45 +02:00
}
void stopWriter() {
2020-11-02 16:16:21 +01:00
{
std::lock_guard<std::mutex> lck(sigMtx);
writerStop = true;
}
cv.notify_one();
2020-06-15 15:53:45 +02:00
}
2020-11-02 03:57:44 +01:00
void clearWriteStop() {
writerStop = false;
2020-06-15 15:53:45 +02:00
}
2020-11-02 03:57:44 +01:00
T* data;
2020-06-15 15:53:45 +02:00
2020-11-02 03:57:44 +01:00
private:
void waitReady() {
std::unique_lock<std::mutex> lck(sigMtx);
2020-11-02 16:16:21 +01:00
cv.wait(lck, [this]{ return (!dataReady || writerStop); });
2020-06-15 15:53:45 +02:00
}
2020-11-02 03:57:44 +01:00
void waitData() {
std::unique_lock<std::mutex> lck(sigMtx);
2020-11-02 16:16:21 +01:00
cv.wait(lck, [this]{ return (dataReady || readerStop); });
2020-06-15 15:53:45 +02:00
}
2020-11-02 03:57:44 +01:00
std::mutex sigMtx;
std::condition_variable cv;
bool dataReady = false;
2020-06-22 16:45:57 +02:00
2020-11-02 03:57:44 +01:00
bool readerStop = false;
bool writerStop = false;
int contentSize = 0;
2020-06-10 04:13:56 +02:00
};
2020-11-02 03:57:44 +01:00
}