From c1863d9319e8a9aa80711293c87a997095e0052a Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Tue, 31 May 2016 19:58:37 -0400 Subject: [PATCH] Basic cleanup / mutex additions / bulk demod add --- src/AppFrame.cpp | 10 ++++----- src/CubicSDR.cpp | 7 ++++++ src/CubicSDR.h | 1 + src/demod/DemodulatorMgr.cpp | 43 ++++++++++++++++++++++++++++++------ src/demod/DemodulatorMgr.h | 2 ++ src/sdr/SDRPostThread.cpp | 12 ++++++++++ src/sdr/SDRPostThread.h | 1 + src/util/DataTree.cpp | 2 +- 8 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/AppFrame.cpp b/src/AppFrame.cpp index 84200e7..3d546a0 100644 --- a/src/AppFrame.cpp +++ b/src/AppFrame.cpp @@ -1627,6 +1627,7 @@ bool AppFrame::loadSession(std::string fileName) { int numDemodulators = 0; DemodulatorInstance *loadedDemod = NULL; DemodulatorInstance *newDemod = NULL; + std::vector demodsLoaded; while (demodulators->hasAnother("demodulator")) { DataNode *demod = demodulators->getNext("demodulator"); @@ -1727,8 +1728,9 @@ bool AppFrame::loadSession(std::string fileName) { } newDemod->run(); - newDemod->setActive(false); - wxGetApp().bindDemodulator(newDemod); + newDemod->setActive(true); + demodsLoaded.push_back(newDemod); +// wxGetApp().bindDemodulator(newDemod); std::cout << "\tAdded demodulator at frequency " << freq << " type " << type << std::endl; std::cout << "\t\tBandwidth: " << bandwidth << std::endl; @@ -1740,9 +1742,7 @@ bool AppFrame::loadSession(std::string fileName) { DemodulatorInstance *focusDemod = loadedDemod?loadedDemod:newDemod; if (focusDemod) { - focusDemod->setActive(true); - focusDemod->setFollow(true); - focusDemod->setTracking(true); + wxGetApp().bindDemodulators(&demodsLoaded); wxGetApp().getDemodMgr().setActiveDemodulator(focusDemod, false); } } catch (DataInvalidChildException &e) { diff --git a/src/CubicSDR.cpp b/src/CubicSDR.cpp index 51e6014..4e6ea4a 100644 --- a/src/CubicSDR.cpp +++ b/src/CubicSDR.cpp @@ -632,6 +632,13 @@ void CubicSDR::bindDemodulator(DemodulatorInstance *demod) { sdrPostThread->bindDemodulator(demod); } +void CubicSDR::bindDemodulators(std::vector *demods) { + if (!demods) { + return; + } + sdrPostThread->bindDemodulators(demods); +} + long long CubicSDR::getSampleRate() { return sampleRate; } diff --git a/src/CubicSDR.h b/src/CubicSDR.h index 58a057f..1641669 100644 --- a/src/CubicSDR.h +++ b/src/CubicSDR.h @@ -115,6 +115,7 @@ public: SDRThread *getSDRThread(); void bindDemodulator(DemodulatorInstance *demod); + void bindDemodulators(std::vector *demods); void removeDemodulator(DemodulatorInstance *demod); void setFrequencySnap(int snap); diff --git a/src/demod/DemodulatorMgr.cpp b/src/demod/DemodulatorMgr.cpp index 8f80829..76d3023 100644 --- a/src/demod/DemodulatorMgr.cpp +++ b/src/demod/DemodulatorMgr.cpp @@ -13,9 +13,17 @@ bool demodFreqCompare (DemodulatorInstance *i, DemodulatorInstance *j) { return (i->getFrequency()getFrequency()); } bool inactiveCompare (DemodulatorInstance *i, DemodulatorInstance *j) { return (i->isActive()isActive()); } -DemodulatorMgr::DemodulatorMgr() : - activeDemodulator(NULL), lastActiveDemodulator(NULL), activeVisualDemodulator(NULL), lastBandwidth(DEFAULT_DEMOD_BW), lastDemodType( - DEFAULT_DEMOD_TYPE), lastSquelchEnabled(false), lastSquelch(-100), lastGain(1.0), lastMuted(false), lastDeltaLock(false) { +DemodulatorMgr::DemodulatorMgr() { + activeDemodulator = NULL; + lastActiveDemodulator = NULL; + activeVisualDemodulator = NULL; + lastBandwidth = DEFAULT_DEMOD_BW; + lastDemodType = DEFAULT_DEMOD_TYPE; + lastSquelchEnabled = false; + lastSquelch = -100; + lastGain = 1.0; + lastMuted = false; + lastDeltaLock = false; } DemodulatorMgr::~DemodulatorMgr() { @@ -23,26 +31,35 @@ DemodulatorMgr::~DemodulatorMgr() { } DemodulatorInstance *DemodulatorMgr::newThread() { + garbageCollect(); + + demods_busy.lock(); DemodulatorInstance *newDemod = new DemodulatorInstance; - demods.push_back(newDemod); std::stringstream label; label << demods.size(); newDemod->setLabel(label.str()); - + + demods.push_back(newDemod); + demods_busy.unlock(); + return newDemod; } void DemodulatorMgr::terminateAll() { while (demods.size()) { + demods_busy.lock(); DemodulatorInstance *d = demods.back(); demods.pop_back(); + demods_busy.unlock(); wxGetApp().removeDemodulator(d); deleteThread(d); } } std::vector &DemodulatorMgr::getDemodulators() { + demods_busy.lock(); + demods_busy.unlock(); return demods; } @@ -110,6 +127,8 @@ DemodulatorInstance *DemodulatorMgr::getFirstDemodulator() { } void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) { + demods_busy.lock(); + std::vector::iterator i; i = std::find(demods.begin(), demods.end(), demod); @@ -131,10 +150,13 @@ void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) { demods_deleted.push_back(demod); + demods_busy.unlock(); + garbageCollect(); } std::vector *DemodulatorMgr::getDemodulatorsAt(long long freq, int bandwidth) { + demods_busy.lock(); std::vector *foundDemods = new std::vector(); for (int i = 0, iMax = demods.size(); i < iMax; i++) { @@ -150,12 +172,13 @@ std::vector *DemodulatorMgr::getDemodulatorsAt(long long foundDemods->push_back(testDemod); } } + demods_busy.unlock(); return foundDemods; } bool DemodulatorMgr::anyDemodulatorsAt(long long freq, int bandwidth) { - + demods_busy.lock(); for (int i = 0, iMax = demods.size(); i < iMax; i++) { DemodulatorInstance *testDemod = demods[i]; @@ -166,10 +189,12 @@ bool DemodulatorMgr::anyDemodulatorsAt(long long freq, int bandwidth) { long long halfBuffer = bandwidth / 2; if ((freq <= (freqTest + ((testDemod->getDemodulatorType() != "LSB")?halfBandwidthTest:0) + halfBuffer)) && (freq >= (freqTest - ((testDemod->getDemodulatorType() != "USB")?halfBandwidthTest:0) - halfBuffer))) { + demods_busy.unlock(); return true; } } + demods_busy.unlock(); return false; } @@ -206,7 +231,7 @@ void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool tempo activeDemodulator = demod; - garbageCollect(); +// garbageCollect(); } DemodulatorInstance *DemodulatorMgr::getActiveDemodulator() { @@ -222,6 +247,7 @@ DemodulatorInstance *DemodulatorMgr::getLastActiveDemodulator() { void DemodulatorMgr::garbageCollect() { if (demods_deleted.size()) { + demods_busy.lock(); std::vector::iterator i; for (i = demods_deleted.begin(); i != demods_deleted.end(); i++) { @@ -232,9 +258,12 @@ void DemodulatorMgr::garbageCollect() { std::cout << "Garbage collected demodulator instance " << deleted->getLabel() << std::endl; delete deleted; + + demods_busy.unlock(); return; } } + demods_busy.unlock(); } } diff --git a/src/demod/DemodulatorMgr.h b/src/demod/DemodulatorMgr.h index 8d72711..8218045 100644 --- a/src/demod/DemodulatorMgr.h +++ b/src/demod/DemodulatorMgr.h @@ -72,5 +72,7 @@ private: bool lastMuted; bool lastDeltaLock; + std::mutex demods_busy; + std::map lastModemSettings; }; diff --git a/src/sdr/SDRPostThread.cpp b/src/sdr/SDRPostThread.cpp index fe6f62c..32e72b0 100644 --- a/src/sdr/SDRPostThread.cpp +++ b/src/sdr/SDRPostThread.cpp @@ -33,6 +33,18 @@ void SDRPostThread::bindDemodulator(DemodulatorInstance *demod) { busy_demod.unlock(); } +void SDRPostThread::bindDemodulators(std::vector *demods) { + if (!demods) { + return; + } + busy_demod.lock(); + for (std::vector::iterator di = demods->begin(); di != demods->end(); di++) { + demodulators.push_back(*di); + doRefresh.store(true); + } + busy_demod.unlock(); +} + void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) { if (!demod) { return; diff --git a/src/sdr/SDRPostThread.h b/src/sdr/SDRPostThread.h index 48c7540..f6634e7 100644 --- a/src/sdr/SDRPostThread.h +++ b/src/sdr/SDRPostThread.h @@ -13,6 +13,7 @@ public: ~SDRPostThread(); void bindDemodulator(DemodulatorInstance *demod); + void bindDemodulators(std::vector *demods); void removeDemodulator(DemodulatorInstance *demod); void run(); diff --git a/src/util/DataTree.cpp b/src/util/DataTree.cpp index 968d371..0453b07 100755 --- a/src/util/DataTree.cpp +++ b/src/util/DataTree.cpp @@ -1554,7 +1554,7 @@ bool DataTree::SaveToFileXML(const std::string& filename) { */ bool DataTree::SaveToFile(const std::string& filename, bool compress, int /* compress_level */) { - long dataSize, compressedSize, headerSize; + long dataSize, compressedSize = 0, headerSize; char *serialized = nullptr, *hdr_serialized = nullptr, *compressed = nullptr; DataTree dtHeader;