From 24cd93d1ae81f89b32c9a13bde2a8108aa4fce9e Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Thu, 11 Dec 2014 23:35:06 -0500 Subject: [PATCH] Split DemodulatorMgr and DemodulatorInstance --- CMakeLists.txt | 2 + src/demod/DemodulatorInstance.cpp | 87 +++++++++++++++++++++++++++++++ src/demod/DemodulatorInstance.h | 49 +++++++++++++++++ src/demod/DemodulatorMgr.cpp | 85 ------------------------------ src/demod/DemodulatorMgr.h | 42 +-------------- 5 files changed, 139 insertions(+), 126 deletions(-) create mode 100644 src/demod/DemodulatorInstance.cpp create mode 100644 src/demod/DemodulatorInstance.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fffc75d..e86417a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -127,6 +127,7 @@ SET (cubicsdr_sources src/sdr/SDRPostThread.cpp src/demod/DemodulatorThread.cpp src/demod/DemodulatorWorkerThread.cpp + src/demod/DemodulatorInstance.cpp src/demod/DemodulatorMgr.cpp src/audio/AudioThread.cpp src/util/Gradient.cpp @@ -152,6 +153,7 @@ SET (cubicsdr_headers src/sdr/SDRPostThread.h src/demod/DemodulatorThread.h src/demod/DemodulatorWorkerThread.h + src/demod/DemodulatorInstance.h src/demod/DemodulatorMgr.h src/demod/DemodDefs.h src/audio/AudioThread.h diff --git a/src/demod/DemodulatorInstance.cpp b/src/demod/DemodulatorInstance.cpp new file mode 100644 index 0000000..0daf081 --- /dev/null +++ b/src/demod/DemodulatorInstance.cpp @@ -0,0 +1,87 @@ +#include "DemodulatorInstance.h" + +DemodulatorInstance::DemodulatorInstance() : + t_Demod(NULL), t_Audio(NULL), threadQueueDemod(NULL), demodulatorThread(NULL), terminated(false), audioTerminated(false), demodTerminated( + false) { + + label = new std::string("Unnamed"); + threadQueueDemod = new DemodulatorThreadInputQueue; + threadQueueCommand = new DemodulatorThreadCommandQueue; + threadQueueNotify = new DemodulatorThreadCommandQueue; + demodulatorThread = new DemodulatorThread(threadQueueDemod, threadQueueNotify); + demodulatorThread->setCommandQueue(threadQueueCommand); + audioInputQueue = new AudioThreadInputQueue; + audioThread = new AudioThread(audioInputQueue, threadQueueNotify); + demodulatorThread->setAudioInputQueue(audioInputQueue); +} + +DemodulatorInstance::~DemodulatorInstance() { + delete audioThread; + delete demodulatorThread; + + delete audioInputQueue; + delete threadQueueDemod; +} + +void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) { + demodulatorThread->setVisualOutputQueue(tQueue); +} + +void DemodulatorInstance::run() { + t_Audio = new std::thread(&AudioThread::threadMain, audioThread); + +#ifdef __APPLE__ // Already using pthreads, might as well do some custom init.. + pthread_attr_t attr; + size_t size; + + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 2048000); + pthread_attr_getstacksize(&attr, &size); + pthread_create(&t_Demod, &attr, &DemodulatorThread::pthread_helper, demodulatorThread); + pthread_attr_destroy(&attr); + + std::cout << "Initialized demodulator stack size of " << size << std::endl; + +#else + t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread); +#endif +} + +void DemodulatorInstance::updateLabel(int freq) { + std::stringstream newLabel; + newLabel.precision(3); + newLabel << std::fixed << ((float) freq / 1000000.0); + setLabel(newLabel.str()); +} + +DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() { + return threadQueueCommand; +} + +DemodulatorThreadParameters &DemodulatorInstance::getParams() { + return demodulatorThread->getParams(); +} + +void DemodulatorInstance::terminate() { + std::cout << "Terminating demodulator thread.." << std::endl; + demodulatorThread->terminate(); +//#ifdef __APPLE__ +// pthread_join(t_Demod,NULL); +//#else +//#endif + std::cout << "Terminating demodulator audio thread.." << std::endl; + audioThread->terminate(); +} + +std::string DemodulatorInstance::getLabel() { + return *(label.load()); +} + +void DemodulatorInstance::setLabel(std::string labelStr) { + std::string *newLabel = new std::string; + newLabel->append(labelStr); + std::string *oldLabel; + oldLabel = label; + label = newLabel; + delete oldLabel; +} diff --git a/src/demod/DemodulatorInstance.h b/src/demod/DemodulatorInstance.h new file mode 100644 index 0000000..b636937 --- /dev/null +++ b/src/demod/DemodulatorInstance.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include +#include + +#include "DemodulatorThread.h" + +class DemodulatorInstance { +public: + + DemodulatorThreadInputQueue* threadQueueDemod; + DemodulatorThreadCommandQueue* threadQueueCommand; + DemodulatorThreadCommandQueue* threadQueueNotify; + DemodulatorThread *demodulatorThread; +#ifdef __APPLE__ + pthread_t t_Demod; +#else + std::thread *t_Demod; +#endif + + AudioThreadInputQueue *audioInputQueue; + AudioThread *audioThread; + std::thread *t_Audio; + + DemodulatorInstance(); + ~DemodulatorInstance(); + + void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue); + + DemodulatorThreadCommandQueue *getCommandQueue(); + DemodulatorThreadParameters &getParams(); + + void run(); + void terminate(); + std::string getLabel(); + void setLabel(std::string labelStr); + + bool isTerminated(); + void updateLabel(int freq); + +private: + std::atomic label; + bool terminated; + bool demodTerminated; + bool audioTerminated; +}; + + diff --git a/src/demod/DemodulatorMgr.cpp b/src/demod/DemodulatorMgr.cpp index d4c5a82..4a25ed3 100644 --- a/src/demod/DemodulatorMgr.cpp +++ b/src/demod/DemodulatorMgr.cpp @@ -5,91 +5,6 @@ #include #include -DemodulatorInstance::DemodulatorInstance() : - t_Demod(NULL), t_Audio(NULL), threadQueueDemod(NULL), demodulatorThread(NULL), terminated(false), audioTerminated(false), demodTerminated( - false) { - - label = new std::string("Unnamed"); - threadQueueDemod = new DemodulatorThreadInputQueue; - threadQueueCommand = new DemodulatorThreadCommandQueue; - threadQueueNotify = new DemodulatorThreadCommandQueue; - demodulatorThread = new DemodulatorThread(threadQueueDemod, threadQueueNotify); - demodulatorThread->setCommandQueue(threadQueueCommand); - audioInputQueue = new AudioThreadInputQueue; - audioThread = new AudioThread(audioInputQueue, threadQueueNotify); - demodulatorThread->setAudioInputQueue(audioInputQueue); -} - -DemodulatorInstance::~DemodulatorInstance() { - delete audioThread; - delete demodulatorThread; - - delete audioInputQueue; - delete threadQueueDemod; -} - -void DemodulatorInstance::setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) { - demodulatorThread->setVisualOutputQueue(tQueue); -} - -void DemodulatorInstance::run() { - t_Audio = new std::thread(&AudioThread::threadMain, audioThread); - -#ifdef __APPLE__ // Already using pthreads, might as well do some custom init.. - pthread_attr_t attr; - size_t size; - - pthread_attr_init(&attr); - pthread_attr_setstacksize(&attr, 2048000); - pthread_attr_getstacksize(&attr, &size); - pthread_create(&t_Demod, &attr, &DemodulatorThread::pthread_helper, demodulatorThread); - pthread_attr_destroy(&attr); - - std::cout << "Initialized demodulator stack size of " << size << std::endl; - -#else - t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread); -#endif -} - -void DemodulatorInstance::updateLabel(int freq) { - std::stringstream newLabel; - newLabel.precision(3); - newLabel << std::fixed << ((float) freq / 1000000.0); - setLabel(newLabel.str()); -} - -DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() { - return threadQueueCommand; -} - -DemodulatorThreadParameters &DemodulatorInstance::getParams() { - return demodulatorThread->getParams(); -} - -void DemodulatorInstance::terminate() { - std::cout << "Terminating demodulator thread.." << std::endl; - demodulatorThread->terminate(); -//#ifdef __APPLE__ -// pthread_join(t_Demod,NULL); -//#else -//#endif - std::cout << "Terminating demodulator audio thread.." << std::endl; - audioThread->terminate(); -} - -std::string DemodulatorInstance::getLabel() { - return *(label.load()); -} - -void DemodulatorInstance::setLabel(std::string labelStr) { - std::string *newLabel = new std::string; - newLabel->append(labelStr); - std::string *oldLabel; - oldLabel = label; - label = newLabel; - delete oldLabel; -} DemodulatorMgr::DemodulatorMgr() : activeDemodulator(NULL), lastActiveDemodulator(NULL), activeVisualDemodulator(NULL) { diff --git a/src/demod/DemodulatorMgr.h b/src/demod/DemodulatorMgr.h index a936ddd..2bbb072 100644 --- a/src/demod/DemodulatorMgr.h +++ b/src/demod/DemodulatorMgr.h @@ -4,47 +4,7 @@ #include #include -#include "DemodulatorThread.h" - -class DemodulatorInstance { -public: - - DemodulatorThreadInputQueue* threadQueueDemod; - DemodulatorThreadCommandQueue* threadQueueCommand; - DemodulatorThreadCommandQueue* threadQueueNotify; - DemodulatorThread *demodulatorThread; -#ifdef __APPLE__ - pthread_t t_Demod; -#else - std::thread *t_Demod; -#endif - - AudioThreadInputQueue *audioInputQueue; - AudioThread *audioThread; - std::thread *t_Audio; - - DemodulatorInstance(); - ~DemodulatorInstance(); - - void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue); - - DemodulatorThreadCommandQueue *getCommandQueue(); - DemodulatorThreadParameters &getParams(); - - void run(); - void terminate(); - std::string getLabel(); - void setLabel(std::string labelStr); - - bool isTerminated(); - void updateLabel(int freq); - -private: - std::atomic label; - bool terminated; - bool demodTerminated; - bool audioTerminated; -}; +#include "DemodulatorInstance.h" class DemodulatorMgr { public: