Increase demodulator stack size on OSX

Using pthreads as a drop-in, should work since underlying is already
pthreads?
This commit is contained in:
Charles J. Cliffe 2014-12-01 18:59:07 -05:00
parent 86ac0c59a8
commit e668fa2b5d
4 changed files with 134 additions and 85 deletions

View File

@ -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();

View File

@ -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;

View File

@ -2,6 +2,10 @@
#include "CubicSDRDefs.h"
#include <vector>
#ifdef __APPLE__
#include <pthread.h>
#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();

View File

@ -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<signed char> data;
unsigned int frequency;
unsigned int bandwidth;
std::vector<signed char> data;
DemodulatorThreadIQData() :
frequency(0), bandwidth(0) {
DemodulatorThreadIQData() :
frequency(0), bandwidth(0) {
}
}
DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency, std::vector<signed char> data) :
data(data), frequency(frequency), bandwidth(bandwidth) {
DemodulatorThreadIQData(unsigned int bandwidth, unsigned int frequency,
std::vector<signed char> 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<float> data;
std::vector<float> data;
DemodulatorThreadAudioData() :
sampleRate(0), frequency(0), channels(0) {
DemodulatorThreadAudioData() :
sampleRate(0), frequency(0), channels(0) {
}
}
DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate, std::vector<float> data) :
data(data), sampleRate(sampleRate), frequency(frequency), channels(1) {
DemodulatorThreadAudioData(unsigned int frequency, unsigned int sampleRate,
std::vector<float> 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<DemodulatorThreadIQData> DemodulatorThreadInputQueue;
typedef ThreadQueue<AudioThreadInput> DemodulatorThreadOutputQueue;
typedef ThreadQueue<DemodulatorThreadCommand> 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<bool> terminated;
std::atomic<bool> initialized;
std::atomic<bool> terminated;
std::atomic<bool> initialized;
DemodulatorWorkerThread *workerThread;
std::thread *t_Worker;
DemodulatorWorkerThread *workerThread;
std::thread *t_Worker;
DemodulatorThreadWorkerCommandQueue *workerQueue;
DemodulatorThreadWorkerResultQueue *workerResults;
DemodulatorThreadWorkerCommandQueue *workerQueue;
DemodulatorThreadWorkerResultQueue *workerResults;
};