Fix startup config access issues, init race

This commit is contained in:
Charles J. Cliffe 2015-07-08 18:54:52 -04:00
parent b762d4d118
commit f86950b334
5 changed files with 56 additions and 25 deletions

View File

@ -10,54 +10,65 @@ DeviceConfig::DeviceConfig(std::string deviceId) : ppm(0) {
} }
void DeviceConfig::setPPM(int ppm) { void DeviceConfig::setPPM(int ppm) {
this->ppm = ppm; this->ppm.store(ppm);
} }
int DeviceConfig::getPPM() { int DeviceConfig::getPPM() {
return ppm; return ppm.load();
} }
void DeviceConfig::setDirectSampling(int mode) { void DeviceConfig::setDirectSampling(int mode) {
directSampling = mode; directSampling.store(mode);
} }
int DeviceConfig::getDirectSampling() { int DeviceConfig::getDirectSampling() {
return directSampling; return directSampling.load();
} }
void DeviceConfig::setOffset(long long offset) { void DeviceConfig::setOffset(long long offset) {
this->offset = offset; this->offset.store(offset);
} }
long long DeviceConfig::getOffset() { long long DeviceConfig::getOffset() {
return offset; return offset.load();
} }
void DeviceConfig::setIQSwap(bool iqSwap) { void DeviceConfig::setIQSwap(bool iqSwap) {
this->iqSwap = iqSwap; this->iqSwap.store(iqSwap);
} }
bool DeviceConfig::getIQSwap() { bool DeviceConfig::getIQSwap() {
return iqSwap; return iqSwap.load();
} }
void DeviceConfig::setDeviceId(std::string deviceId) { void DeviceConfig::setDeviceId(std::string deviceId) {
busy_lock.lock();
this->deviceId = deviceId; this->deviceId = deviceId;
busy_lock.unlock();
} }
std::string DeviceConfig::getDeviceId() { std::string DeviceConfig::getDeviceId() {
return deviceId; std::string tmp;
busy_lock.lock();
tmp = deviceId;
busy_lock.unlock();
return tmp;
} }
void DeviceConfig::save(DataNode *node) { void DeviceConfig::save(DataNode *node) {
busy_lock.lock();
*node->newChild("id") = deviceId; *node->newChild("id") = deviceId;
*node->newChild("ppm") = (int)ppm; *node->newChild("ppm") = (int)ppm;
*node->newChild("iq_swap") = iqSwap; *node->newChild("iq_swap") = iqSwap;
*node->newChild("direct_sampling") = directSampling; *node->newChild("direct_sampling") = directSampling;
*node->newChild("offset") = offset; *node->newChild("offset") = offset;
busy_lock.unlock();
} }
void DeviceConfig::load(DataNode *node) { void DeviceConfig::load(DataNode *node) {
busy_lock.lock();
if (node->hasAnother("ppm")) { if (node->hasAnother("ppm")) {
DataNode *ppm_node = node->getNext("ppm"); DataNode *ppm_node = node->getNext("ppm");
int ppmValue = 0; int ppmValue = 0;
@ -96,8 +107,9 @@ void DeviceConfig::load(DataNode *node) {
long long offsetValue = 0; long long offsetValue = 0;
offset_node->element()->get(offsetValue); offset_node->element()->get(offsetValue);
setOffset(offsetValue); setOffset(offsetValue);
std::cout << "Loaded offset for device '" << deviceId << "' at " << offsetValue << "ppm" << std::endl; std::cout << "Loaded offset for device '" << deviceId << "' at " << offsetValue << "Hz" << std::endl;
} }
busy_lock.unlock();
} }

View File

@ -3,10 +3,11 @@
#include <wx/stdpaths.h> #include <wx/stdpaths.h>
#include <wx/dir.h> #include <wx/dir.h>
#include <wx/filename.h> #include <wx/filename.h>
#include <atomic>
#include <mutex>
#include "DataTree.h" #include "DataTree.h"
class DeviceConfig { class DeviceConfig {
public: public:
DeviceConfig(); DeviceConfig();
@ -32,9 +33,11 @@ public:
private: private:
std::string deviceId; std::string deviceId;
int ppm, directSampling; std::mutex busy_lock;
bool iqSwap;
long long offset; std::atomic<int> ppm, directSampling;
std::atomic<bool> iqSwap;
std::atomic<long long> offset;
}; };
class AppConfig { class AppConfig {

View File

@ -319,8 +319,18 @@ AppFrame::AppFrame() :
wxAcceleratorTable accel(3, entries); wxAcceleratorTable accel(3, entries);
SetAcceleratorTable(accel); SetAcceleratorTable(accel);
SDRDeviceInfo *dev = (*wxGetApp().getDevices())[wxGetApp().getDevice()]; // static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
DeviceConfig *devConfig = wxGetApp().getConfig()->getDevice(dev->getDeviceId()); // wxLogStatus("Double-buffered display %s supported", wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not");
// ShowFullScreen(true);
}
AppFrame::~AppFrame() {
}
void AppFrame::initDeviceParams(std::string deviceId) {
DeviceConfig *devConfig = wxGetApp().getConfig()->getDevice(deviceId);
int dsMode = devConfig->getDirectSampling(); int dsMode = devConfig->getDirectSampling();
@ -331,16 +341,8 @@ AppFrame::AppFrame() :
if (devConfig->getIQSwap()) { if (devConfig->getIQSwap()) {
iqSwapMenuItem->Check(); iqSwapMenuItem->Check();
} }
// static const int attribs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, 0 };
// wxLogStatus("Double-buffered display %s supported", wxGLCanvas::IsDisplaySupported(attribs) ? "is" : "not");
// ShowFullScreen(true);
} }
AppFrame::~AppFrame() {
}
void AppFrame::OnMenu(wxCommandEvent& event) { void AppFrame::OnMenu(wxCommandEvent& event) {
if (event.GetId() >= wxID_RT_AUDIO_DEVICE && event.GetId() < wxID_RT_AUDIO_DEVICE + devices.size()) { if (event.GetId() >= wxID_RT_AUDIO_DEVICE && event.GetId() < wxID_RT_AUDIO_DEVICE + devices.size()) {

View File

@ -58,6 +58,7 @@ public:
~AppFrame(); ~AppFrame();
void OnThread(wxCommandEvent& event); void OnThread(wxCommandEvent& event);
void OnEventInput(wxThreadEvent& event); void OnEventInput(wxThreadEvent& event);
void initDeviceParams(std::string deviceId);
void saveSession(std::string fileName); void saveSession(std::string fileName);
bool loadSession(std::string fileName); bool loadSession(std::string fileName);

View File

@ -67,6 +67,7 @@ bool CubicSDR::OnInit() {
std::vector<SDRDeviceInfo *>::iterator devs_i; std::vector<SDRDeviceInfo *>::iterator devs_i;
SDRThread::enumerate_rtl(&devs); SDRThread::enumerate_rtl(&devs);
SDRDeviceInfo *dev = NULL;
if (devs.size() > 1) { if (devs.size() > 1) {
wxArrayString choices; wxArrayString choices;
@ -78,6 +79,9 @@ bool CubicSDR::OnInit() {
devName.append(" ["); devName.append(" [");
devName.append((*devs_i)->getSerial()); devName.append((*devs_i)->getSerial());
devName.append("]"); devName.append("]");
if (!dev) {
dev = (*devs_i);
}
} else { } else {
devName.append(" (In Use?)"); devName.append(" (In Use?)");
} }
@ -85,15 +89,24 @@ bool CubicSDR::OnInit() {
} }
int devId = wxGetSingleChoiceIndex(wxT("Devices"), wxT("Choose Input Device"), choices); int devId = wxGetSingleChoiceIndex(wxT("Devices"), wxT("Choose Input Device"), choices);
dev = devs[devId];
std::cout << "Chosen: " << devId << std::endl;
sdrThread->setDeviceId(devId); sdrThread->setDeviceId(devId);
} else if (devs.size() == 1) {
dev = devs[0];
} }
t_PostSDR = new std::thread(&SDRPostThread::threadMain, sdrPostThread); t_PostSDR = new std::thread(&SDRPostThread::threadMain, sdrPostThread);
t_SDR = new std::thread(&SDRThread::threadMain, sdrThread); t_SDR = new std::thread(&SDRThread::threadMain, sdrThread);
appframe = new AppFrame(); appframe = new AppFrame();
if (dev != NULL) {
appframe->initDeviceParams(dev->getDeviceId());
DeviceConfig *devConfig = wxGetApp().getConfig()->getDevice(dev->getDeviceId());
ppm = devConfig->getPPM();
offset = devConfig->getOffset();
directSamplingMode = devConfig->getDirectSampling();
}
#ifdef __APPLE__ #ifdef __APPLE__
int main_policy; int main_policy;