getDefaultSubstituters(): Simplify initialisation

As shlevy pointed out, static variables in C++11 have thread-safe
initialisation.
This commit is contained in:
Eelco Dolstra 2017-07-04 16:26:48 +02:00
parent 6cf23c3e8f
commit b7203e853e
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE

View file

@ -795,33 +795,27 @@ static RegisterStoreImplementation regStore([](
std::list<ref<Store>> getDefaultSubstituters() std::list<ref<Store>> getDefaultSubstituters()
{ {
struct State { static auto stores([]() {
bool done = false;
std::list<ref<Store>> stores; std::list<ref<Store>> stores;
};
static Sync<State> state_;
auto state(state_.lock()); StringSet done;
if (state->done) return state->stores; auto addStore = [&](const std::string & uri) {
if (done.count(uri)) return;
done.insert(uri);
stores.push_back(openStore(uri));
};
StringSet done; for (auto uri : settings.substituters.get())
addStore(uri);
auto addStore = [&](const std::string & uri) { for (auto uri : settings.extraSubstituters.get())
if (done.count(uri)) return; addStore(uri);
done.insert(uri);
state->stores.push_back(openStore(uri));
};
for (auto uri : settings.substituters.get()) return stores;
addStore(uri); } ());
for (auto uri : settings.extraSubstituters.get()) return stores;
addStore(uri);
state->done = true;
return state->stores;
} }