From 703501f32d8238f28c28e102c4bb7073f5bb1673 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 22 Dec 2014 23:27:52 -0500 Subject: [PATCH] OSX fixes --- src/audio/AudioThread.cpp | 2 +- src/demod/DemodDefs.h | 32 +++++++++++++-------------- src/demod/DemodulatorPreThread.cpp | 19 +++++++++------- src/sdr/SDRPostThread.cpp | 35 ++++++++++++------------------ 4 files changed, 41 insertions(+), 47 deletions(-) diff --git a/src/audio/AudioThread.cpp b/src/audio/AudioThread.cpp index 1e95c63..9095186 100644 --- a/src/audio/AudioThread.cpp +++ b/src/audio/AudioThread.cpp @@ -94,7 +94,7 @@ static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBu } } else { for (int i = 0, iMax = src->currentInput.channels * nBufferFrames; i < iMax; i++) { - if (srcmix->audio_queue_ptr >= srcmix->currentInput.data.size()) { + if (srcmix->audio_queue_ptr >= srcmix->currentInput.data->size()) { if (srcmix->currentInput.data) { delete srcmix->currentInput.data; } diff --git a/src/demod/DemodDefs.h b/src/demod/DemodDefs.h index a9129d2..88a7d25 100644 --- a/src/demod/DemodDefs.h +++ b/src/demod/DemodDefs.h @@ -59,11 +59,10 @@ class DemodulatorThreadIQData { public: unsigned int frequency; unsigned int bandwidth; - std::vector *data; - std::atomic *refCount; + std::vector data; DemodulatorThreadIQData() : - frequency(0), bandwidth(0), data(NULL), refCount(NULL) { + frequency(0), bandwidth(0), refCount(0) { } @@ -71,28 +70,27 @@ public: frequency = o.frequency; bandwidth = o.bandwidth; data = o.data; - refCount = o.refCount; + refCount.store(o.refCount.load()); } - void setRefCount(std::atomic *rc) { - refCount = rc; + void setRefCount(int rc) { + refCount.store(rc); } - void cleanup() { - if (refCount) { - refCount->store(refCount->load()-1); - if (refCount->load() == 0) { - delete data; - data = NULL; - delete refCount; - refCount = NULL; - } - } + void decRefCount() { + refCount.store(refCount.load()-1); + } + + int getRefCount() { + return refCount.load(); } ~DemodulatorThreadIQData() { } +private: + std::atomic refCount; + }; class DemodulatorThreadPostIQData { @@ -146,7 +144,7 @@ public: } }; -typedef ThreadQueue DemodulatorThreadInputQueue; +typedef ThreadQueue DemodulatorThreadInputQueue; typedef ThreadQueue DemodulatorThreadPostInputQueue; typedef ThreadQueue DemodulatorThreadCommandQueue; typedef ThreadQueue DemodulatorThreadControlCommandQueue; diff --git a/src/demod/DemodulatorPreThread.cpp b/src/demod/DemodulatorPreThread.cpp index eb71a6b..e0635e8 100644 --- a/src/demod/DemodulatorPreThread.cpp +++ b/src/demod/DemodulatorPreThread.cpp @@ -100,7 +100,7 @@ void DemodulatorPreThread::threadMain() { std::cout << "Demodulator preprocessor thread started.." << std::endl; while (!terminated) { - DemodulatorThreadIQData inp; + DemodulatorThreadIQData *inp; inputQueue->pop(inp); bool bandwidthChanged = false; @@ -144,9 +144,9 @@ void DemodulatorPreThread::threadMain() { } // Requested frequency is not center, shift it into the center! - if (inp.frequency != params.frequency) { - if ((params.frequency - inp.frequency) != shift_freq) { - shift_freq = params.frequency - inp.frequency; + if (inp->frequency != params.frequency) { + if ((params.frequency - inp->frequency) != shift_freq) { + shift_freq = params.frequency - inp->frequency; if (abs(shift_freq) <= (int) ((float) (SRATE / 2) * 1.5)) { nco_crcf_set_frequency(nco_shift, (2.0 * M_PI) * (((float) abs(shift_freq)) / ((float) SRATE))); } @@ -157,8 +157,8 @@ void DemodulatorPreThread::threadMain() { continue; } - std::vector *data = inp.data; - if (data && data->size()) { + std::vector *data = &inp->data; + if (data->size()) { int bufSize = data->size() / 2; liquid_float_complex in_buf_data[bufSize]; @@ -197,7 +197,10 @@ void DemodulatorPreThread::threadMain() { resamp.resampler = resampler; postInputQueue->push(resamp); - inp.cleanup(); + inp->decRefCount(); + if (inp->getRefCount()<=0) { + delete inp; + } } if (!workerResults->empty()) { @@ -236,7 +239,7 @@ void DemodulatorPreThread::threadMain() { void DemodulatorPreThread::terminate() { terminated = true; - DemodulatorThreadIQData inp; // push dummy to nudge queue + DemodulatorThreadIQData *inp = new DemodulatorThreadIQData; // push dummy to nudge queue inputQueue->push(inp); workerThread->terminate(); } diff --git a/src/sdr/SDRPostThread.cpp b/src/sdr/SDRPostThread.cpp index 3b74ae3..bee87f9 100644 --- a/src/sdr/SDRPostThread.cpp +++ b/src/sdr/SDRPostThread.cpp @@ -39,7 +39,7 @@ void SDRPostThread::threadMain() { #ifdef __APPLE__ pthread_t tID = pthread_self(); // ID of this thread int priority = sched_get_priority_max( SCHED_FIFO) - 1; - sched_param prio = {priority}; // scheduling priority of thread + sched_param prio = { priority }; // scheduling priority of thread pthread_setschedparam(tID, SCHED_FIFO, &prio); #endif @@ -82,7 +82,6 @@ void SDRPostThread::threadMain() { iqVisualQueue.load()->push(visualDataOut); } - if (demodulators_add.size()) { while (!demodulators_add.empty()) { demodulators.push_back(demodulators_add.back()); @@ -104,7 +103,6 @@ void SDRPostThread::threadMain() { int activeDemods = 0; bool pushedData = false; - std::atomic *c = new std::atomic; if (demodulators.size()) { @@ -119,20 +117,14 @@ void SDRPostThread::threadMain() { activeDemods++; } - c->store(activeDemods); - bool demodActive = false; if (demodulators.size()) { - DemodulatorThreadIQData dummyDataOut; - dummyDataOut.frequency = data_in.frequency; - dummyDataOut.bandwidth = data_in.bandwidth; - dummyDataOut.data = NULL; - DemodulatorThreadIQData demodDataOut; - demodDataOut.frequency = data_in.frequency; - demodDataOut.bandwidth = data_in.bandwidth; - demodDataOut.setRefCount(c); - demodDataOut.data = data_in.data; + DemodulatorThreadIQData *demodDataOut = new DemodulatorThreadIQData; + demodDataOut->frequency = data_in.frequency; + demodDataOut->bandwidth = data_in.bandwidth; + demodDataOut->setRefCount(activeDemods); + demodDataOut->data.assign(dataOut.data->begin(), dataOut.data->begin() + dataOut.data->size()); std::vector::iterator i; for (i = demodulators.begin(); i != demodulators.end(); i++) { @@ -143,6 +135,9 @@ void SDRPostThread::threadMain() { && abs(data_in.frequency - demod->getParams().frequency) > (int) ((float) ((float) SRATE / 2.0))) { if (demod->isActive()) { demod->setActive(false); + DemodulatorThreadIQData *dummyDataOut = new DemodulatorThreadIQData; + dummyDataOut->frequency = data_in.frequency; + dummyDataOut->bandwidth = data_in.bandwidth; demodQueue->push(dummyDataOut); } } else if (!demod->isActive()) { @@ -156,16 +151,14 @@ void SDRPostThread::threadMain() { demodQueue->push(demodDataOut); pushedData = true; } + + if (!pushedData) { + delete demodDataOut; + } } } - - if (!pushedData) { - delete dataOut.data; - delete c; - } + delete dataOut.data; } - - } std::cout << "SDR post-processing thread done." << std::endl; }