mutex locking cleanup for demod add / remove chain

This commit is contained in:
Charles J. Cliffe 2016-06-02 21:48:01 -04:00
parent 6d63d570aa
commit 423c83f500
2 changed files with 12 additions and 33 deletions

View File

@ -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<DemodulatorInstance *> &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<DemodulatorInstance *>::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<DemodulatorInstance *> *DemodulatorMgr::getDemodulatorsAt(long long freq, int bandwidth) {
demods_busy.lock();
std::lock_guard < std::mutex > lock(demods_busy);
std::vector<DemodulatorInstance *> *foundDemods = new std::vector<DemodulatorInstance *>();
for (int i = 0, iMax = demods.size(); i < iMax; i++) {
@ -172,13 +161,12 @@ std::vector<DemodulatorInstance *> *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<DemodulatorInstance *>::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();
}
}

View File

@ -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<DemodulatorInstance *> *demods) {
if (!demods) {
return;
}
busy_demod.lock();
std::lock_guard < std::mutex > lock(busy_demod);
for (std::vector<DemodulatorInstance *>::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<DemodulatorInstance *>::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()) {