diff --git a/src/demod/DemodulatorMgr.cpp b/src/demod/DemodulatorMgr.cpp index f6cc40e..f28f144 100644 --- a/src/demod/DemodulatorMgr.cpp +++ b/src/demod/DemodulatorMgr.cpp @@ -31,9 +31,7 @@ DemodulatorMgr::~DemodulatorMgr() { } DemodulatorInstance *DemodulatorMgr::newThread() { - garbageCollect(); - - demods_busy.lock(); + std::lock_guard < std::mutex > lock(demods_busy); DemodulatorInstance *newDemod = new DemodulatorInstance; std::stringstream label; @@ -41,25 +39,22 @@ DemodulatorInstance *DemodulatorMgr::newThread() { newDemod->setLabel(label.str()); demods.push_back(newDemod); - demods_busy.unlock(); return newDemod; } void DemodulatorMgr::terminateAll() { + std::lock_guard < std::mutex > lock(demods_busy); 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(); + std::lock_guard < std::mutex > lock(demods_busy); return demods; } @@ -127,8 +122,6 @@ DemodulatorInstance *DemodulatorMgr::getFirstDemodulator() { } void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) { - demods_busy.lock(); - std::vector::iterator i; i = std::find(demods.begin(), demods.end(), demod); @@ -149,14 +142,10 @@ void DemodulatorMgr::deleteThread(DemodulatorInstance *demod) { demod->terminate(); demods_deleted.push_back(demod); - - demods_busy.unlock(); - - garbageCollect(); } std::vector *DemodulatorMgr::getDemodulatorsAt(long long freq, int bandwidth) { - demods_busy.lock(); + std::lock_guard < std::mutex > lock(demods_busy); std::vector *foundDemods = new std::vector(); for (int i = 0, iMax = demods.size(); i < iMax; i++) { @@ -172,13 +161,12 @@ 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(); + std::lock_guard < std::mutex > lock(demods_busy); for (int i = 0, iMax = demods.size(); i < iMax; i++) { DemodulatorInstance *testDemod = demods[i]; @@ -189,12 +177,10 @@ 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; } @@ -213,6 +199,8 @@ void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool tempo wxGetApp().getRigThread()->setFrequency(lastActiveDemodulator->getFrequency(),true); } #endif + } else { + garbageCollect(); } if (activeVisualDemodulator) { @@ -230,8 +218,6 @@ void DemodulatorMgr::setActiveDemodulator(DemodulatorInstance *demod, bool tempo } activeDemodulator = demod; - -// garbageCollect(); } DemodulatorInstance *DemodulatorMgr::getActiveDemodulator() { @@ -246,8 +232,8 @@ DemodulatorInstance *DemodulatorMgr::getLastActiveDemodulator() { } void DemodulatorMgr::garbageCollect() { + std::lock_guard < std::mutex > lock(demods_busy); if (demods_deleted.size()) { - demods_busy.lock(); std::vector::iterator i; for (i = demods_deleted.begin(); i != demods_deleted.end(); i++) { @@ -259,11 +245,9 @@ void DemodulatorMgr::garbageCollect() { delete deleted; - demods_busy.unlock(); return; } } - demods_busy.unlock(); } } diff --git a/src/sdr/SDRPostThread.cpp b/src/sdr/SDRPostThread.cpp index a29f4a1..055501e 100644 --- a/src/sdr/SDRPostThread.cpp +++ b/src/sdr/SDRPostThread.cpp @@ -27,22 +27,20 @@ SDRPostThread::~SDRPostThread() { } void SDRPostThread::bindDemodulator(DemodulatorInstance *demod) { - busy_demod.lock(); + std::lock_guard < std::mutex > lock(busy_demod); demodulators.push_back(demod); doRefresh.store(true); - busy_demod.unlock(); } void SDRPostThread::bindDemodulators(std::vector *demods) { if (!demods) { return; } - busy_demod.lock(); + std::lock_guard < std::mutex > lock(busy_demod); 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) { @@ -50,14 +48,13 @@ void SDRPostThread::removeDemodulator(DemodulatorInstance *demod) { return; } - busy_demod.lock(); + std::lock_guard < std::mutex > lock(busy_demod); std::vector::iterator i = std::find(demodulators.begin(), demodulators.end(), demod); if (i != demodulators.end()) { demodulators.erase(i); doRefresh.store(true); } - busy_demod.unlock(); } void SDRPostThread::initPFBChannelizer() { @@ -186,7 +183,7 @@ void SDRPostThread::run() { iqDataInQueue->pop(data_in); // std::lock_guard < std::mutex > lock(data_in->m_mutex); - busy_demod.lock(); + std::lock_guard < std::mutex > lock(busy_demod); if (data_in && data_in->data.size()) { if(data_in->numChannels > 1) { @@ -211,8 +208,6 @@ void SDRPostThread::run() { if (doUpdate) { updateActiveDemodulators(); } - - busy_demod.unlock(); } if (iqVisualQueue && !iqVisualQueue->empty()) {