From b54ab38a47675632f42adaf1474f4064bba79c27 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Wed, 8 Jun 2016 19:48:46 -0400 Subject: [PATCH] More minor tweaks; getBuffer refcount 1 init suggestion from @vsonnier --- src/IOThread.h | 6 +++--- src/audio/AudioThread.cpp | 35 +++++++++++++++------------------ src/demod/DemodulatorThread.cpp | 8 +++----- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/IOThread.h b/src/IOThread.h index 7ec7165..2397018 100644 --- a/src/IOThread.h +++ b/src/IOThread.h @@ -82,7 +82,6 @@ public: static void addGarbage(ReferenceCounter *ref) { std::lock_guard < std::mutex > lock(g_mutex); - std::cout << "Added garbage.." << std::endl; garbage.insert(ref); } @@ -107,7 +106,7 @@ public: for (outputBuffersI = outputBuffers.begin(); outputBuffersI != outputBuffers.end(); outputBuffersI++) { if (buf == nullptr && (*outputBuffersI)->getRefCount() <= 0) { buf = (*outputBuffersI); - buf->setRefCount(0); + buf->setRefCount(1); } else if ((*outputBuffersI)->getRefCount() <= 0) { (*outputBuffersI)->decRefCount(); } @@ -128,6 +127,7 @@ public: } buf = new BufferType(); + buf->setRefCount(1); outputBuffers.push_back(buf); return buf; @@ -142,7 +142,7 @@ public: delete ref; } else { // Something isn't done with it yet; throw it on the pile.. - std::cout << bufferId << "pushed garbage.." << std::endl; + std::cout << "'" << bufferId << "' pushed garbage.." << std::endl; ReBufferGC::addGarbage(ref); } } diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 5db31d8..bc51ef7 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -393,6 +393,15 @@ void AudioThread::run() { setSampleRate(command.int_value); } } + + // Drain any remaining inputs + while (!inputQueue->empty()) { + AudioThreadInput *ref; + inputQueue->pop(ref); + if (ref) { + ref->decRefCount(); + } + } if (deviceController[parameters.deviceId] != this) { deviceController[parameters.deviceId]->removeThread(this); @@ -408,14 +417,6 @@ void AudioThread::run() { e.printMessage(); } } - - while (!inputQueue->empty()) { // flush queue - AudioThreadInput *dummy; - inputQueue->pop(dummy); - if (dummy) { - dummy->setRefCount(0); - } - } if (threadQueueNotify != NULL) { DemodulatorThreadCommand tCmd(DemodulatorThreadCommand::DEMOD_THREAD_CMD_AUDIO_TERMINATED); @@ -439,23 +440,19 @@ void AudioThread::setActive(bool state) { AudioThreadInput *dummy; if (state && !active && inputQueue) { + deviceController[parameters.deviceId]->bindThread(this); + } else if (!state && active) { + deviceController[parameters.deviceId]->removeThread(this); + } + + // Activity state changing, clear any inputs + if(inputQueue) { while (!inputQueue->empty()) { // flush queue inputQueue->pop(dummy); if (dummy) { dummy->decRefCount(); } } - deviceController[parameters.deviceId]->bindThread(this); - } else if (!state && active) { - deviceController[parameters.deviceId]->removeThread(this); - if(inputQueue) { - while (!inputQueue->empty()) { // flush queue - inputQueue->pop(dummy); - if (dummy) { - dummy->decRefCount(); - } - } - } } active = state; } diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index bab8239..9ec0cab 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -132,13 +132,11 @@ void DemodulatorThread::run() { ati->sampleRate = cModemKit->audioSampleRate; ati->inputRate = inp->sampleRate; - ati->setRefCount(1); } else if (modemDigital != nullptr) { ati = outputBuffers.getBuffer(); ati->sampleRate = cModemKit->sampleRate; ati->inputRate = inp->sampleRate; - ati->setRefCount(1); } cModem->demodulate(cModemKit, &modemData, ati); @@ -281,14 +279,14 @@ void DemodulatorThread::run() { while (!iqInputQueue->empty()) { DemodulatorThreadPostIQData *ref; iqInputQueue->pop(ref); - if (ref) { - ref->setRefCount(0); + if (ref) { // May have other consumers; just decrement + ref->decRefCount(); } } while (!audioOutputQueue->empty()) { AudioThreadInput *ref; audioOutputQueue->pop(ref); - if (ref) { + if (ref) { // Originated here; set RefCount to 0 ref->setRefCount(0); } }