mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 08:41:16 +00:00
make FairProgOptions calls thread safe
This commit is contained in:
parent
b47cc8b29c
commit
ac69607250
|
@ -19,6 +19,7 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "FairProgOptions.h"
|
#include "FairProgOptions.h"
|
||||||
#include "FairMQEventManager.h"
|
#include "FairMQEventManager.h"
|
||||||
|
@ -164,6 +165,8 @@ class FairMQProgOptions : public FairProgOptions , public FairMQEventManager
|
||||||
template<typename T>
|
template<typename T>
|
||||||
int UpdateValue(const std::string& key, T val)
|
int UpdateValue(const std::string& key, T val)
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(fConfigMutex);
|
||||||
|
|
||||||
if (fVarMap.count(key))
|
if (fVarMap.count(key))
|
||||||
{
|
{
|
||||||
// update variable map
|
// update variable map
|
||||||
|
@ -204,6 +207,8 @@ class FairMQProgOptions : public FairProgOptions , public FairMQEventManager
|
||||||
template <typename T, typename F>
|
template <typename T, typename F>
|
||||||
void Subscribe(const std::string& key, F&& func) const
|
void Subscribe(const std::string& key, F&& func) const
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(fConfigMutex);
|
||||||
|
|
||||||
static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
|
static_assert(!std::is_same<T,const char*>::value || !std::is_same<T, char*>::value,
|
||||||
"In template member FairMQProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
|
"In template member FairMQProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ FairProgOptions::FairProgOptions() :
|
||||||
fConfigFileOptions("Configuration file options"),
|
fConfigFileOptions("Configuration file options"),
|
||||||
fSeverityMap(),
|
fSeverityMap(),
|
||||||
fVisibleOptions("Visible options"),
|
fVisibleOptions("Visible options"),
|
||||||
|
fConfigMutex(),
|
||||||
fVerbosityLevel("INFO"),
|
fVerbosityLevel("INFO"),
|
||||||
fUseConfigFile(false),
|
fUseConfigFile(false),
|
||||||
fConfigFile()
|
fConfigFile()
|
||||||
|
@ -204,29 +205,7 @@ int FairProgOptions::ParseEnvironment(const function<string(string)>& environmen
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a key, convert the variable value to string
|
int FairProgOptions::PrintHelp() const
|
||||||
string FairProgOptions::GetStringValue(const string& key)
|
|
||||||
{
|
|
||||||
string valueStr;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (fVarMap.count(key))
|
|
||||||
{
|
|
||||||
valueStr=FairMQ::ConvertVariableValue<FairMQ::ToString>().Run(fVarMap.at(key));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(exception& e)
|
|
||||||
{
|
|
||||||
LOG(ERROR) << "Exception thrown for the key '" << key << "'";
|
|
||||||
LOG(ERROR) << e.what();
|
|
||||||
}
|
|
||||||
|
|
||||||
return valueStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// //////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// Print/notify options
|
|
||||||
int FairProgOptions::PrintHelp() const
|
|
||||||
{
|
{
|
||||||
cout << fVisibleOptions << "\n";
|
cout << fVisibleOptions << "\n";
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <mutex>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -77,6 +78,8 @@ class FairProgOptions
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T GetValue(const std::string& key) const
|
T GetValue(const std::string& key) const
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(fConfigMutex);
|
||||||
|
|
||||||
T val = T();
|
T val = T();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -99,11 +102,32 @@ class FairProgOptions
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert value to string that corresponds to the key
|
// Given a key, convert the variable value to string
|
||||||
std::string GetStringValue(const std::string& key);
|
std::string GetStringValue(const std::string& key)
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(fConfigMutex);
|
||||||
|
|
||||||
|
std::string valueStr;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (fVarMap.count(key))
|
||||||
|
{
|
||||||
|
valueStr = FairMQ::ConvertVariableValue<FairMQ::ToString>().Run(fVarMap.at(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
LOG(ERROR) << "Exception thrown for the key '" << key << "'";
|
||||||
|
LOG(ERROR) << e.what();
|
||||||
|
}
|
||||||
|
|
||||||
|
return valueStr;
|
||||||
|
}
|
||||||
|
|
||||||
int Count(const std::string& key) const
|
int Count(const std::string& key) const
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(fConfigMutex);
|
||||||
|
|
||||||
return fVarMap.count(key);
|
return fVarMap.count(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,6 +186,8 @@ class FairProgOptions
|
||||||
std::map<std::string, FairMQ::severity_level> fSeverityMap;
|
std::map<std::string, FairMQ::severity_level> fSeverityMap;
|
||||||
po::options_description fVisibleOptions;
|
po::options_description fVisibleOptions;
|
||||||
|
|
||||||
|
mutable std::mutex fConfigMutex;
|
||||||
|
|
||||||
std::string fVerbosityLevel;
|
std::string fVerbosityLevel;
|
||||||
bool fUseConfigFile;
|
bool fUseConfigFile;
|
||||||
boost::filesystem::path fConfigFile;
|
boost::filesystem::path fConfigFile;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user