Initial IOThread prototype

This commit is contained in:
Charles J. Cliffe 2015-07-29 18:34:58 -04:00
parent 2d8c2800cb
commit 3ab8669d06
7 changed files with 136 additions and 53 deletions

View File

@ -228,6 +228,7 @@ SET (cubicsdr_sources
src/AppFrame.cpp
src/AppConfig.cpp
src/FrequencyDialog.cpp
src/IOThread.cpp
src/sdr/SDRThread.cpp
src/sdr/SDRPostThread.cpp
src/demod/DemodulatorPreThread.cpp
@ -277,6 +278,7 @@ SET (cubicsdr_headers
src/AppFrame.h
src/AppConfig.h
src/FrequencyDialog.h
src/IOThread.h
src/sdr/SDRThread.h
src/sdr/SDRPostThread.h
src/demod/DemodulatorPreThread.h

View File

@ -120,10 +120,11 @@ AppFrame::AppFrame() :
spectrumCanvas->attachWaterfallCanvas(waterfallCanvas);
vbox->Add(waterfallCanvas, 20, wxEXPAND | wxALL, 0);
/*
vbox->AddSpacer(1);
testCanvas = new UITestCanvas(this, attribList);
vbox->Add(testCanvas, 20, wxEXPAND | wxALL, 0);
// */
this->SetSizer(vbox);

View File

@ -33,55 +33,3 @@ const char filePathSeparator =
#define DEFAULT_DEMOD_TYPE 1
#define DEFAULT_DEMOD_BW 200000
#include <mutex>
#include <atomic>
#include <deque>
class ReferenceCounter {
public:
mutable std::mutex m_mutex;
void setRefCount(int rc) {
refCount.store(rc);
}
void decRefCount() {
refCount.store(refCount.load()-1);
}
int getRefCount() {
return refCount.load();
}
protected:
std::atomic_int refCount;
};
template<class BufferType = ReferenceCounter>
class ReBuffer {
public:
BufferType *getBuffer() {
BufferType* buf = NULL;
for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) {
if ((*outputBuffersI)->getRefCount() <= 0) {
return (*outputBuffersI);
}
}
buf = new BufferType();
outputBuffers.push_back(buf);
return buf;
}
void purge() {
while (!outputBuffers.empty()) {
BufferType *ref = outputBuffers.front();
outputBuffers.pop_front();
delete ref;
}
}
private:
std::deque<BufferType*> outputBuffers;
typename std::deque<BufferType*>::iterator outputBuffersI;
};

1
src/IOThread.cpp Normal file
View File

@ -0,0 +1 @@
#include "IOThread.h"

129
src/IOThread.h Normal file
View File

@ -0,0 +1,129 @@
#pragma once
#include <mutex>
#include <atomic>
#include <deque>
#include <map>
#include <string>
struct map_string_less : public std::binary_function<std::string,std::string,bool>
{
bool operator()(const std::string& a,const std::string& b) const
{
return a.compare(b) < 0;
}
};
class ReferenceCounter {
public:
mutable std::mutex m_mutex;
void setRefCount(int rc) {
refCount.store(rc);
}
void decRefCount() {
refCount.store(refCount.load()-1);
}
int getRefCount() {
return refCount.load();
}
protected:
std::atomic_int refCount;
};
template<class BufferType = ReferenceCounter>
class ReBuffer {
public:
BufferType *getBuffer() {
BufferType* buf = NULL;
for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) {
if ((*outputBuffersI)->getRefCount() <= 0) {
return (*outputBuffersI);
}
}
buf = new BufferType();
outputBuffers.push_back(buf);
return buf;
}
void purge() {
while (!outputBuffers.empty()) {
BufferType *ref = outputBuffers.front();
outputBuffers.pop_front();
delete ref;
}
}
private:
std::deque<BufferType*> outputBuffers;
typename std::deque<BufferType*>::iterator outputBuffersI;
};
class IOThread {
public:
virtual void setup() {
};
virtual void init() {
};
virtual void onBindOutput(std::string name, void* threadQueue) {
};
virtual void onBindInput(std::string name, void* threadQueue) {
};
#ifdef __APPLE__
virtual void *threadMain() {
return 0;
};
static void *pthread_helper(void *context) {
return ((IOThread *) context)->threadMain();
};
#else
virtual void threadMain() {
};
#endif
virtual void terminate() {
};
void setInputQueue(std::string qname, void *threadQueue) {
input_queues[qname] = threadQueue;
this->onBindInput(qname, threadQueue);
};
void *getInputQueue(std::string qname) {
return input_queues[qname];
};
void setOutputQueue(std::string qname, void *threadQueue) {
output_queues[qname] = threadQueue;
this->onBindOutput(qname, threadQueue);
};
void *getOutputQueue(std::string qname) {
return output_queues[qname];
};
protected:
std::map<std::string, void *, map_string_less> input_queues;
std::map<std::string, void *, map_string_less> output_queues;
};

View File

@ -15,6 +15,7 @@
#define DEMOD_TYPE_DSB 5
#define DEMOD_TYPE_RAW 6
#include "IOThread.h"
class DemodulatorThread;
class DemodulatorThreadCommand {

View File

@ -2,6 +2,7 @@
#include "CubicSDRDefs.h"
#include "ThreadQueue.h"
#include "IOThread.h"
typedef ThreadQueue<ReferenceCounter *> VisualDataQueue;