make FairProgOptions calls thread safe

This commit is contained in:
Alexey Rybalchenko 2017-06-26 12:29:19 +02:00 committed by Mohammad Al-Turany
parent b47cc8b29c
commit ac69607250
3 changed files with 35 additions and 25 deletions

View File

@ -19,6 +19,7 @@
#include <unordered_map>
#include <map>
#include <set>
#include <mutex>
#include "FairProgOptions.h"
#include "FairMQEventManager.h"
@ -164,6 +165,8 @@ class FairMQProgOptions : public FairProgOptions , public FairMQEventManager
template<typename T>
int UpdateValue(const std::string& key, T val)
{
std::unique_lock<std::mutex> lock(fConfigMutex);
if (fVarMap.count(key))
{
// update variable map
@ -204,6 +207,8 @@ class FairMQProgOptions : public FairProgOptions , public FairMQEventManager
template <typename T, typename F>
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,
"In template member FairMQProgOptions::Subscribe<T>(key,Lambda) the types const char* or char* for the calback signatures are not supported.");

View File

@ -28,6 +28,7 @@ FairProgOptions::FairProgOptions() :
fConfigFileOptions("Configuration file options"),
fSeverityMap(),
fVisibleOptions("Visible options"),
fConfigMutex(),
fVerbosityLevel("INFO"),
fUseConfigFile(false),
fConfigFile()
@ -204,29 +205,7 @@ int FairProgOptions::ParseEnvironment(const function<string(string)>& environmen
return 0;
}
// Given a key, convert the variable value to string
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
int FairProgOptions::PrintHelp() const
{
cout << fVisibleOptions << "\n";
return 0;

View File

@ -24,6 +24,7 @@
#include <iostream>
#include <fstream>
#include <iterator>
#include <mutex>
#include <tuple>
/*
@ -77,6 +78,8 @@ class FairProgOptions
template<typename T>
T GetValue(const std::string& key) const
{
std::unique_lock<std::mutex> lock(fConfigMutex);
T val = T();
try
{
@ -99,11 +102,32 @@ class FairProgOptions
return val;
}
// convert value to string that corresponds to the key
std::string GetStringValue(const std::string& key);
// Given a key, convert the variable value to string
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
{
std::unique_lock<std::mutex> lock(fConfigMutex);
return fVarMap.count(key);
}
@ -162,6 +186,8 @@ class FairProgOptions
std::map<std::string, FairMQ::severity_level> fSeverityMap;
po::options_description fVisibleOptions;
mutable std::mutex fConfigMutex;
std::string fVerbosityLevel;
bool fUseConfigFile;
boost::filesystem::path fConfigFile;