diff --git a/src/demod/DemodulatorMgr.cpp b/src/demod/DemodulatorMgr.cpp index 1384d61..9ddaf08 100644 --- a/src/demod/DemodulatorMgr.cpp +++ b/src/demod/DemodulatorMgr.cpp @@ -50,7 +50,22 @@ void DemodulatorInstance::run() { } t_Audio = new std::thread(&AudioThread::threadMain, audioThread); + +#ifdef __APPLE__ // Already using pthreads, might as well do some custom init.. + pthread_attr_t attr; + size_t size; + + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 2048000); + pthread_attr_getstacksize(&attr, &size); + pthread_create(&t_Demod, &attr, &DemodulatorThread::pthread_helper, demodulatorThread); + pthread_attr_destroy(&attr); + + std::cout << "Initialized demodulator stack size of " << size << std::endl; + +#else t_Demod = new std::thread(&DemodulatorThread::threadMain, demodulatorThread); +#endif } DemodulatorThreadCommandQueue *DemodulatorInstance::getCommandQueue() { @@ -64,8 +79,11 @@ DemodulatorThreadParameters &DemodulatorInstance::getParams() { void DemodulatorInstance::terminate() { std::cout << "Terminating demodulator thread.." << std::endl; demodulatorThread->terminate(); +#ifdef __APPLE__ + pthread_join(t_Demod,NULL); +#else t_Demod->join(); - +#endif std::cout << "Terminating demodulator audio thread.." << std::endl; audioThread->terminate(); t_Audio->join(); diff --git a/src/demod/DemodulatorMgr.h b/src/demod/DemodulatorMgr.h index 9bcfbd2..935c2c8 100644 --- a/src/demod/DemodulatorMgr.h +++ b/src/demod/DemodulatorMgr.h @@ -12,7 +12,11 @@ public: DemodulatorThreadInputQueue* threadQueueDemod; DemodulatorThreadCommandQueue* threadQueueCommand; DemodulatorThread *demodulatorThread; +#ifdef __APPLE__ + pthread_t t_Demod; +#else std::thread *t_Demod; +#endif AudioThreadInputQueue *audioInputQueue; AudioThread *audioThread; diff --git a/src/demod/DemodulatorThread.cpp b/src/demod/DemodulatorThread.cpp index 42aa7e1..dda7d05 100644 --- a/src/demod/DemodulatorThread.cpp +++ b/src/demod/DemodulatorThread.cpp @@ -2,6 +2,10 @@ #include "CubicSDRDefs.h" #include +#ifdef __APPLE__ + #include +#endif + DemodulatorThread::DemodulatorThread(DemodulatorThreadInputQueue* pQueue) : inputQueue(pQueue), visOutQueue(NULL), terminated(false), initialized(false), audio_resampler(NULL), resample_ratio(1), audio_resample_ratio( 1), resampler(NULL), commandQueue(NULL), fir_filter(NULL), audioInputQueue(NULL) { @@ -76,7 +80,11 @@ DemodulatorThread::~DemodulatorThread() { delete workerResults; } +#ifdef __APPLE__ +void *DemodulatorThread::threadMain() { +#else void DemodulatorThread::threadMain() { +#endif if (!initialized) { initialize(); diff --git a/src/demod/DemodulatorThread.h b/src/demod/DemodulatorThread.h index 3fefd13..227182f 100644 --- a/src/demod/DemodulatorThread.h +++ b/src/demod/DemodulatorThread.h @@ -17,153 +17,172 @@ #include "DemodulatorWorkerThread.h" enum DemodulatorType { - DEMOD_TYPE_NULL, DEMOD_TYPE_AM, DEMOD_TYPE_FM, DEMOD_TYPE_LSB, DEMOD_TYPE_USB + DEMOD_TYPE_NULL, + DEMOD_TYPE_AM, + DEMOD_TYPE_FM, + DEMOD_TYPE_LSB, + DEMOD_TYPE_USB }; class DemodulatorThreadCommand { public: - enum DemodulatorThreadCommandEnum { - DEMOD_THREAD_CMD_NULL, DEMOD_THREAD_CMD_SET_BANDWIDTH, DEMOD_THREAD_CMD_SET_FREQUENCY - }; + enum DemodulatorThreadCommandEnum { + DEMOD_THREAD_CMD_NULL, + DEMOD_THREAD_CMD_SET_BANDWIDTH, + DEMOD_THREAD_CMD_SET_FREQUENCY + }; - DemodulatorThreadCommand() : - cmd(DEMOD_THREAD_CMD_NULL), int_value(0) { + DemodulatorThreadCommand() : + cmd(DEMOD_THREAD_CMD_NULL), int_value(0) { - } + } - DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) : - cmd(cmd), int_value(0) { + DemodulatorThreadCommand(DemodulatorThreadCommandEnum cmd) : + cmd(cmd), int_value(0) { - } + } - DemodulatorThreadCommandEnum cmd; - int int_value; + DemodulatorThreadCommandEnum cmd; + int int_value; }; class DemodulatorThreadIQData { public: - unsigned int frequency; - unsigned int bandwidth; - std::vector data; + unsigned int frequency; + unsigned int bandwidth; + std::vector data; - DemodulatorThreadIQData() : - frequency(0), bandwidth(0) { + DemodulatorThreadIQData() : + frequency(0), bandwidth(0) { - } + } - DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector data) : - data(data), frequency(frequency), bandwidth(bandwidth) { + DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency, + std::vector data) : + data(data), frequency(frequency), bandwidth(bandwidth) { - } + } - ~DemodulatorThreadIQData() { + ~DemodulatorThreadIQData() { - } + } }; class DemodulatorThreadAudioData { public: - unsigned int frequency; - unsigned int sampleRate; - unsigned char channels; + unsigned int frequency; + unsigned int sampleRate; + unsigned char channels; - std::vector data; + std::vector data; - DemodulatorThreadAudioData() : - sampleRate(0), frequency(0), channels(0) { + DemodulatorThreadAudioData() : + sampleRate(0), frequency(0), channels(0) { - } + } - DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, std::vector data) : - data(data), sampleRate(sampleRate), frequency(frequency), channels(1) { + DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, + std::vector data) : + data(data), sampleRate(sampleRate), frequency(frequency), channels( + 1) { - } + } - ~DemodulatorThreadAudioData() { + ~DemodulatorThreadAudioData() { - } + } }; class DemodulatorThreadParameters { public: - unsigned int frequency; - unsigned int inputRate; - unsigned int bandwidth; // set equal to disable second stage re-sampling? - unsigned int audioSampleRate; + unsigned int frequency; + unsigned int inputRate; + unsigned int bandwidth; // set equal to disable second stage re-sampling? + unsigned int audioSampleRate; - DemodulatorType demodType; + DemodulatorType demodType; - DemodulatorThreadParameters() : - frequency(0), inputRate(SRATE), bandwidth(200000), audioSampleRate(AUDIO_FREQUENCY), demodType(DEMOD_TYPE_FM) { + DemodulatorThreadParameters() : + frequency(0), inputRate(SRATE), bandwidth(200000), audioSampleRate( + AUDIO_FREQUENCY), demodType(DEMOD_TYPE_FM) { - } + } - ~DemodulatorThreadParameters() { + ~DemodulatorThreadParameters() { - } + } }; typedef ThreadQueue DemodulatorThreadInputQueue; typedef ThreadQueue DemodulatorThreadOutputQueue; typedef ThreadQueue DemodulatorThreadCommandQueue; - class DemodulatorThread { public: - DemodulatorThread(DemodulatorThreadInputQueue* pQueue); - ~DemodulatorThread(); + DemodulatorThread(DemodulatorThreadInputQueue* pQueue); + ~DemodulatorThread(); - void threadMain(); +#ifdef __APPLE__ + void *threadMain(); +#else + void threadMain(); +#endif - void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) { - visOutQueue = tQueue; - visOutQueue->set_max_num_items(1); - } + void setVisualOutputQueue(DemodulatorThreadOutputQueue *tQueue) { + visOutQueue = tQueue; + visOutQueue->set_max_num_items(1); + } - void setCommandQueue(DemodulatorThreadCommandQueue *tQueue) { - commandQueue = tQueue; - } + void setCommandQueue(DemodulatorThreadCommandQueue *tQueue) { + commandQueue = tQueue; + } - void setAudioInputQueue(AudioThreadInputQueue *tQueue) { - audioInputQueue = tQueue; - } + void setAudioInputQueue(AudioThreadInputQueue *tQueue) { + audioInputQueue = tQueue; + } - DemodulatorThreadParameters &getParams() { - return params; - } + DemodulatorThreadParameters &getParams() { + return params; + } - void initialize(); + void initialize(); - void terminate(); + void terminate(); + +#ifdef __APPLE__ + static void *pthread_helper(void *context) { + return ((DemodulatorThread *) context)->threadMain(); + } +#endif protected: - DemodulatorThreadInputQueue* inputQueue; - DemodulatorThreadOutputQueue* visOutQueue; - DemodulatorThreadCommandQueue* commandQueue; - AudioThreadInputQueue *audioInputQueue; + DemodulatorThreadInputQueue* inputQueue; + DemodulatorThreadOutputQueue* visOutQueue; + DemodulatorThreadCommandQueue* commandQueue; + AudioThreadInputQueue *audioInputQueue; - firfilt_crcf fir_filter; + firfilt_crcf fir_filter; - msresamp_crcf resampler; - float resample_ratio; + msresamp_crcf resampler; + float resample_ratio; - msresamp_crcf audio_resampler; - float audio_resample_ratio; + msresamp_crcf audio_resampler; + float audio_resample_ratio; - DemodulatorThreadParameters params; - DemodulatorThreadParameters last_params; + DemodulatorThreadParameters params; + DemodulatorThreadParameters last_params; - freqdem fdem; - nco_crcf nco_shift; - int shift_freq; + freqdem fdem; + nco_crcf nco_shift; + int shift_freq; - std::atomic terminated; - std::atomic initialized; + std::atomic terminated; + std::atomic initialized; - DemodulatorWorkerThread *workerThread; - std::thread *t_Worker; + DemodulatorWorkerThread *workerThread; + std::thread *t_Worker; - DemodulatorThreadWorkerCommandQueue *workerQueue; - DemodulatorThreadWorkerResultQueue *workerResults; + DemodulatorThreadWorkerCommandQueue *workerQueue; + DemodulatorThreadWorkerResultQueue *workerResults; };