1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-26 12:18:51 -05:00

Fix a couple small system inconsistencies: properly flag server shutdown, replace lock_guard with scoped_lock, etc.

This commit is contained in:
Gordon McCann 2022-05-05 20:35:19 -04:00
parent ec8feae4c7
commit 9093f8a53b
4 changed files with 49 additions and 39 deletions

View File

@ -28,7 +28,7 @@ namespace Navigator {
void SpectrumManager::AddHistogram(const HistogramArgs& params) void SpectrumManager::AddHistogram(const HistogramArgs& params)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
if (params.y_par == "None") //Check dimensionality if (params.y_par == "None") //Check dimensionality
m_histoMap[params.name].reset(new Histogram1D(params)); m_histoMap[params.name].reset(new Histogram1D(params));
else else
@ -37,19 +37,19 @@ namespace Navigator {
void SpectrumManager::AddHistogramSummary(const HistogramArgs& params, const std::vector<std::string>& subhistos) void SpectrumManager::AddHistogramSummary(const HistogramArgs& params, const std::vector<std::string>& subhistos)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_histoMap[params.name].reset(new HistogramSummary(params, subhistos)); m_histoMap[params.name].reset(new HistogramSummary(params, subhistos));
} }
void SpectrumManager::RemoveHistogram(const std::string& name) void SpectrumManager::RemoveHistogram(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_histoMap.erase(name); m_histoMap.erase(name);
} }
void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname) void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(histoname); auto iter = m_histoMap.find(histoname);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
iter->second->AddCutToBeDrawn(cutname); iter->second->AddCutToBeDrawn(cutname);
@ -57,7 +57,7 @@ namespace Navigator {
void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname) void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(histoname); auto iter = m_histoMap.find(histoname);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
iter->second->AddCutToBeApplied(cutname); iter->second->AddCutToBeApplied(cutname);
@ -66,7 +66,7 @@ namespace Navigator {
//Use this to fill histograms. Currently can only be filled in bulk; maybe a use case for individual fills? //Use this to fill histograms. Currently can only be filled in bulk; maybe a use case for individual fills?
void SpectrumManager::UpdateHistograms() void SpectrumManager::UpdateHistograms()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
//Set state of all cuts for the event //Set state of all cuts for the event
CheckCuts(); CheckCuts();
@ -128,14 +128,14 @@ namespace Navigator {
void SpectrumManager::ClearHistograms() void SpectrumManager::ClearHistograms()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
for (auto& pair : m_histoMap) for (auto& pair : m_histoMap)
pair.second->ClearData(); pair.second->ClearData();
} }
void SpectrumManager::ClearHistogram(const std::string& name) void SpectrumManager::ClearHistogram(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
iter->second->ClearData(); iter->second->ClearData();
@ -143,7 +143,7 @@ namespace Navigator {
void SpectrumManager::DrawHistogram(const std::string& name) void SpectrumManager::DrawHistogram(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
{ {
@ -155,7 +155,7 @@ namespace Navigator {
const HistogramArgs& SpectrumManager::GetHistogramParams(const std::string& name) const HistogramArgs& SpectrumManager::GetHistogramParams(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
return iter->second->GetParameters(); return iter->second->GetParameters();
@ -166,7 +166,7 @@ namespace Navigator {
//For 2D spectra, we want to allow zooming along the z-axis (color) //For 2D spectra, we want to allow zooming along the z-axis (color)
float* SpectrumManager::GetColorScaleRange(const std::string& name) float* SpectrumManager::GetColorScaleRange(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
{ {
@ -178,7 +178,7 @@ namespace Navigator {
std::vector<double> SpectrumManager::GetBinData(const std::string& name) std::vector<double> SpectrumManager::GetBinData(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
{ {
@ -190,7 +190,7 @@ namespace Navigator {
std::vector<std::string> SpectrumManager::GetSubHistograms(const std::string& name) std::vector<std::string> SpectrumManager::GetSubHistograms(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end() && iter->second->GetType() == SpectrumType::Summary) if (iter != m_histoMap.end() && iter->second->GetType() == SpectrumType::Summary)
{ {
@ -203,7 +203,7 @@ namespace Navigator {
//Pass through for stats //Pass through for stats
StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region) StatResults SpectrumManager::AnalyzeHistogramRegion(const std::string& name, const ImPlotRect& region)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
return iter->second->AnalyzeRegion(region.X.Min, region.X.Max, region.Y.Min, region.Y.Max); return iter->second->AnalyzeRegion(region.X.Min, region.X.Max, region.Y.Min, region.Y.Max);
@ -215,7 +215,7 @@ namespace Navigator {
//in something like the Editor. //in something like the Editor.
std::vector<HistogramArgs> SpectrumManager::GetListOfHistograms() std::vector<HistogramArgs> SpectrumManager::GetListOfHistograms()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<HistogramArgs> list; std::vector<HistogramArgs> list;
list.reserve(m_histoMap.size()); list.reserve(m_histoMap.size());
for (auto& gram : m_histoMap) for (auto& gram : m_histoMap)
@ -233,7 +233,7 @@ namespace Navigator {
//Bind a NavParameter instance to the manager. If the Parameter doesn't exist, make a new one, otherwise attach to extant memory //Bind a NavParameter instance to the manager. If the Parameter doesn't exist, make a new one, otherwise attach to extant memory
void SpectrumManager::BindParameter(NavParameter& param) void SpectrumManager::BindParameter(NavParameter& param)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_paramMap.find(param.GetName()); auto iter = m_paramMap.find(param.GetName());
if (iter == m_paramMap.end()) if (iter == m_paramMap.end())
{ {
@ -247,7 +247,7 @@ namespace Navigator {
//Additionally, make a default 1D histogram for the parameter (histogram has same name as parameter) //Additionally, make a default 1D histogram for the parameter (histogram has same name as parameter)
void SpectrumManager::BindParameter(NavParameter& param, int nbins, double minVal, double maxVal) void SpectrumManager::BindParameter(NavParameter& param, int nbins, double minVal, double maxVal)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_paramMap.find(param.GetName()); auto iter = m_paramMap.find(param.GetName());
if (iter == m_paramMap.end()) if (iter == m_paramMap.end())
{ {
@ -267,7 +267,7 @@ namespace Navigator {
//Once an analysis pass is done and histograms filled, reset all parameters //Once an analysis pass is done and histograms filled, reset all parameters
void SpectrumManager::InvalidateParameters() void SpectrumManager::InvalidateParameters()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
for (auto& param : m_paramMap) for (auto& param : m_paramMap)
{ {
param.second->validFlag = false; param.second->validFlag = false;
@ -278,7 +278,7 @@ namespace Navigator {
//Similar to GetListOfHistograms, see that documentation //Similar to GetListOfHistograms, see that documentation
std::vector<std::string> SpectrumManager::GetListOfParameters() std::vector<std::string> SpectrumManager::GetListOfParameters()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<std::string> list; std::vector<std::string> list;
list.reserve(m_paramMap.size()); list.reserve(m_paramMap.size());
for (auto iter : m_paramMap) for (auto iter : m_paramMap)
@ -295,7 +295,7 @@ namespace Navigator {
void SpectrumManager::BindVariable(NavVariable& var) void SpectrumManager::BindVariable(NavVariable& var)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_varMap.find(var.GetName()); auto iter = m_varMap.find(var.GetName());
if (iter == m_varMap.end()) if (iter == m_varMap.end())
{ {
@ -306,7 +306,7 @@ namespace Navigator {
std::vector<std::string> SpectrumManager::GetListOfVariables() std::vector<std::string> SpectrumManager::GetListOfVariables()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<std::string> list; std::vector<std::string> list;
list.reserve(m_varMap.size()); list.reserve(m_varMap.size());
for (auto iter : m_varMap) for (auto iter : m_varMap)
@ -320,14 +320,14 @@ namespace Navigator {
void SpectrumManager::RemoveCut(const std::string& name) void SpectrumManager::RemoveCut(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_cutMap.erase(name); m_cutMap.erase(name);
RemoveCutFromHistograms(name); //Once a cut is gone, remove all references to it. RemoveCutFromHistograms(name); //Once a cut is gone, remove all references to it.
} }
std::vector<double> SpectrumManager::GetCutXPoints(const std::string& name) std::vector<double> SpectrumManager::GetCutXPoints(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<double> null_result; std::vector<double> null_result;
auto iter = m_cutMap.find(name); auto iter = m_cutMap.find(name);
if (iter != m_cutMap.end()) if (iter != m_cutMap.end())
@ -337,7 +337,7 @@ namespace Navigator {
std::vector<double> SpectrumManager::GetCutYPoints(const std::string& name) std::vector<double> SpectrumManager::GetCutYPoints(const std::string& name)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<double> null_result; std::vector<double> null_result;
auto iter = m_cutMap.find(name); auto iter = m_cutMap.find(name);
if (iter != m_cutMap.end()) if (iter != m_cutMap.end())
@ -347,7 +347,7 @@ namespace Navigator {
std::vector<std::string> SpectrumManager::GetCutSubHistograms(const std::string& cutname) std::vector<std::string> SpectrumManager::GetCutSubHistograms(const std::string& cutname)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_cutMap.find(cutname); auto iter = m_cutMap.find(cutname);
if (iter != m_cutMap.end() && iter->second->GetType() == CutType::CutSummaryAny || iter->second->GetType() == CutType::CutSummaryAll) if (iter != m_cutMap.end() && iter->second->GetType() == CutType::CutSummaryAny || iter->second->GetType() == CutType::CutSummaryAll)
{ {
@ -360,7 +360,7 @@ namespace Navigator {
//Similar to GetListOfHistograms, see that documentation //Similar to GetListOfHistograms, see that documentation
std::vector<CutArgs> SpectrumManager::GetListOfCuts() std::vector<CutArgs> SpectrumManager::GetListOfCuts()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
std::vector<CutArgs> list; std::vector<CutArgs> list;
list.reserve(m_cutMap.size()); list.reserve(m_cutMap.size());
for (auto& entry : m_cutMap) for (auto& entry : m_cutMap)

View File

@ -32,7 +32,7 @@ namespace Navigator {
//To clear all managed spectra. Note that Parameters are left untouched. //To clear all managed spectra. Note that Parameters are left untouched.
inline void RemoveAllSpectra() inline void RemoveAllSpectra()
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_histoMap.clear(); m_histoMap.clear();
m_cutMap.clear(); m_cutMap.clear();
} }
@ -70,17 +70,17 @@ namespace Navigator {
/*Cut Functions*/ /*Cut Functions*/
inline void AddCut(const CutArgs& params, double min, double max) inline void AddCut(const CutArgs& params, double min, double max)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_cutMap[params.name].reset(new Cut1D(params, min, max)); m_cutMap[params.name].reset(new Cut1D(params, min, max));
} }
inline void AddCut(const CutArgs& params, const std::vector<double>& xpoints, const std::vector<double>& ypoints) inline void AddCut(const CutArgs& params, const std::vector<double>& xpoints, const std::vector<double>& ypoints)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_cutMap[params.name].reset(new Cut2D(params, xpoints, ypoints)); m_cutMap[params.name].reset(new Cut2D(params, xpoints, ypoints));
} }
inline void AddCut(const CutArgs& params, const std::vector<std::string>& subhistos, double min, double max) inline void AddCut(const CutArgs& params, const std::vector<std::string>& subhistos, double min, double max)
{ {
std::lock_guard<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_cutMap[params.name].reset(new CutSummary(params, subhistos, min, max)); m_cutMap[params.name].reset(new CutSummary(params, subhistos, min, max));
} }
void RemoveCut(const std::string& name); void RemoveCut(const std::string& name);

View File

@ -75,6 +75,12 @@ namespace Navigator {
void CompassOnlineSource::FillBuffer() void CompassOnlineSource::FillBuffer()
{ {
if (!m_connection.IsOpen()) //Make sure connection is still cool
{
m_validFlag = false;
return;
}
std::vector<char> recieved = m_connection.Read(); std::vector<char> recieved = m_connection.Read();
//If we didn't finish the last buffer toss all of the stuff we used and then append the recieved data //If we didn't finish the last buffer toss all of the stuff we used and then append the recieved data
//Otherwise, copy over the recieved buffer. Note lack of vector::resize, vector::reserve. Intentional for performance. //Otherwise, copy over the recieved buffer. Note lack of vector::resize, vector::reserve. Intentional for performance.

View File

@ -20,8 +20,11 @@ namespace Navigator {
PhysicsLayer::~PhysicsLayer() PhysicsLayer::~PhysicsLayer()
{ {
DetachDataSource(); if (m_activeFlag)
DestroyPhysThread(); {
DetachDataSource();
DestroyPhysThread();
}
} }
void PhysicsLayer::OnAttach() void PhysicsLayer::OnAttach()
@ -67,8 +70,11 @@ namespace Navigator {
bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event) bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event)
{ {
DetachDataSource(); if (m_activeFlag)
DestroyPhysThread(); {
DetachDataSource();
DestroyPhysThread();
}
return true; return true;
} }
@ -101,7 +107,7 @@ namespace Navigator {
void PhysicsLayer::AttachDataSource(PhysicsStartEvent& event) void PhysicsLayer::AttachDataSource(PhysicsStartEvent& event)
{ {
std::lock_guard<std::mutex> guard(m_sourceMutex); //Shouldn't matter for this, but safety first std::scoped_lock<std::mutex> guard(m_sourceMutex); //Shouldn't matter for this, but safety first
m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType())); m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType()));
m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow()); m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow());
m_eventBuilder.ClearAll(); //Protect against stopping mid-event m_eventBuilder.ClearAll(); //Protect against stopping mid-event
@ -119,7 +125,7 @@ namespace Navigator {
void PhysicsLayer::DetachDataSource() void PhysicsLayer::DetachDataSource()
{ {
std::lock_guard<std::mutex> guard(m_sourceMutex); std::scoped_lock<std::mutex> guard(m_sourceMutex);
NAV_INFO("Detaching physics data source..."); NAV_INFO("Detaching physics data source...");
m_activeFlag = false; m_activeFlag = false;
m_source.reset(nullptr); m_source.reset(nullptr);
@ -136,10 +142,9 @@ namespace Navigator {
{ {
//Scope to encapsulate access to the data source //Scope to encapsulate access to the data source
{ {
std::lock_guard<std::mutex> guard(m_sourceMutex); std::scoped_lock<std::mutex> guard(m_sourceMutex);
if (m_source == nullptr || !m_source->IsValid()) if (m_source == nullptr || !m_source->IsValid())
{ {
m_activeFlag = false;
return; return;
} }
/* /*
@ -150,7 +155,6 @@ namespace Navigator {
if(!m_source->IsValid()) if(!m_source->IsValid())
{ {
NAV_INFO("End of data source."); NAV_INFO("End of data source.");
m_activeFlag = false;
return; return;
} }
} }