Skip to content

Commit

Permalink
Fix potential race condition for writing GenericParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jun 12, 2024
1 parent b95f710 commit 8850b63
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
14 changes: 10 additions & 4 deletions include/podio/GenericParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class GenericParameters {

/// Get all the available values for a given type
template <typename T, typename = EnableIfValidGenericDataType<T>>
std::vector<std::vector<T>> getValues() const;
std::tuple<std::vector<std::string>, std::vector<std::vector<T>>> getKeysAndValues() const;

/// erase all elements
void clear() {
Expand Down Expand Up @@ -263,18 +263,24 @@ std::vector<std::string> GenericParameters::getKeys() const {
}

template <typename T, typename>
std::vector<std::vector<T>> GenericParameters::getValues() const {
std::tuple<std::vector<std::string>, std::vector<std::vector<T>>> GenericParameters::getKeysAndValues() const {
std::vector<std::vector<T>> values;
std::vector<std::string> keys;
auto& mtx = getMutex<T>();
const auto& map = getMap<T>();
{
// Lock to avoid concurrent changes to the map while we get the stored
// values
std::lock_guard lock{mtx};
values.reserve(map.size());
std::transform(map.begin(), map.end(), std::back_inserter(values), [](const auto& pair) { return pair.second; });
keys.reserve(map.size());

for (const auto& [k, v] : map) {
keys.emplace_back(k);
values.emplace_back(v);
}
}
return values;
return {keys, values};
}

template <typename T, template <typename...> typename VecLike>
Expand Down
3 changes: 2 additions & 1 deletion include/podio/utilities/RootHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ namespace root_utils {
ParamStorage(ParamStorage&&) = default;
ParamStorage& operator=(ParamStorage&&) = default;

ParamStorage(const std::vector<std::string>& ks, const std::vector<std::vector<T>>& vs) : keys(ks), values(vs) {
ParamStorage(std::tuple<std::vector<std::string>, std::vector<std::vector<T>>> keysValues) :
keys(std::move(std::get<0>(keysValues))), values(std::move(std::get<1>(keysValues))) {
}

/// Get a pointer to the stored keys for binding it to a TBranch
Expand Down
3 changes: 1 addition & 2 deletions src/RNTupleWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ void RNTupleWriter::fillParams(const GenericParameters& params, CategoryInfo& ca
auto& paramStorage = getParamStorage<T>(catInfo);
auto& keys = paramStorage.keys;
auto& values = paramStorage.values;
keys = params.getKeys<T>();
values = params.getValues<T>();
std::tie(keys, values) = params.getKeysAndValues<T>();
#if ROOT_VERSION_CODE >= ROOT_VERSION(6, 31, 0)
entry->BindRawPtr(root_utils::getGPKeyName<T>(), &keys);
entry->BindRawPtr(root_utils::getGPValueName<T>(), &values);
Expand Down
8 changes: 4 additions & 4 deletions src/ROOTWriter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ ROOTWriter::checkConsistency(const std::vector<std::string>& collsToWrite, const
}

void ROOTWriter::fillParams(CategoryInfo& catInfo, const GenericParameters& params) {
catInfo.intParams = {params.getKeys<int>(), params.getValues<int>()};
catInfo.floatParams = {params.getKeys<float>(), params.getValues<float>()};
catInfo.doubleParams = {params.getKeys<double>(), params.getValues<double>()};
catInfo.stringParams = {params.getKeys<std::string>(), params.getValues<std::string>()};
catInfo.intParams = params.getKeysAndValues<int>();
catInfo.floatParams = params.getKeysAndValues<float>();
catInfo.doubleParams = params.getKeysAndValues<double>();
catInfo.stringParams = params.getKeysAndValues<std::string>();
}

} // namespace podio

0 comments on commit 8850b63

Please sign in to comment.