parseExperimentalFeature(): Initialize atomically

This commit is contained in:
Eelco Dolstra 2021-10-26 14:29:48 +02:00
parent 9ce84c64c5
commit 0d9e050ba7

View file

@ -1,4 +1,6 @@
#include "experimental-features.hh" #include "experimental-features.hh"
#include "util.hh"
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
namespace nix { namespace nix {
@ -14,17 +16,19 @@ std::map<ExperimentalFeature, std::string> stringifiedXpFeatures = {
const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name) const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name)
{ {
using ReverseXpMap = std::map<std::string_view, ExperimentalFeature>; using ReverseXpMap = std::map<std::string_view, ExperimentalFeature>;
static ReverseXpMap * reverseXpMap;
if (!reverseXpMap) { static auto reverseXpMap = []()
reverseXpMap = new ReverseXpMap{}; {
auto reverseXpMap = std::make_unique<ReverseXpMap>();
for (auto & [feature, name] : stringifiedXpFeatures) for (auto & [feature, name] : stringifiedXpFeatures)
(*reverseXpMap)[name] = feature; (*reverseXpMap)[name] = feature;
} return reverseXpMap;
}();
auto featureIter = reverseXpMap->find(name); if (auto feature = get(*reverseXpMap, name))
if (featureIter == reverseXpMap->end()) return *feature;
else
return std::nullopt; return std::nullopt;
return {featureIter->second};
} }
std::string_view showExperimentalFeature(const ExperimentalFeature feature) std::string_view showExperimentalFeature(const ExperimentalFeature feature)
@ -45,7 +49,7 @@ std::set<ExperimentalFeature> parseFeatures(const std::set<std::string> & rawFea
MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature) MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature)
: Error("experimental Nix feature '%1%' is disabled; use '--extra-experimental-features %1%' to override", showExperimentalFeature(feature)) : Error("experimental Nix feature '%1%' is disabled; use '--extra-experimental-features %1%' to override", showExperimentalFeature(feature))
, missingFeature(feature) , missingFeature(feature)
{} {}
std::ostream & operator <<(std::ostream & str, const ExperimentalFeature & feature) std::ostream & operator <<(std::ostream & str, const ExperimentalFeature & feature)
{ {