1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 12:06:01 +01:00

Add getConcurrent helper function

This commit is contained in:
Eelco Dolstra 2025-09-07 16:54:39 +02:00
parent 377b60ee9b
commit 7f9b5226af
2 changed files with 12 additions and 3 deletions

View file

@ -218,6 +218,17 @@ typename T::mapped_type * get(T & map, K & key)
template<class T> template<class T>
typename T::mapped_type * get(T && map, const typename T::key_type & key) = delete; typename T::mapped_type * get(T && map, const typename T::key_type & key) = delete;
/**
* Look up a value in a `boost::concurrent_flat_map`.
*/
template<class T>
std::optional<typename T::mapped_type> getConcurrent(const T & map, const typename T::key_type & key)
{
std::optional<typename T::mapped_type> res;
map.cvisit(key, [&](auto & x) { res = x.second; });
return res;
}
/** /**
* Get a value for the specified key from an associate container, or a default value if the key isn't present. * Get a value for the specified key from an associate container, or a default value if the key isn't present.
*/ */

View file

@ -95,9 +95,7 @@ std::optional<struct stat> PosixSourceAccessor::cachedLstat(const CanonPath & pa
// former is not hashable on libc++. // former is not hashable on libc++.
Path absPath = makeAbsPath(path).string(); Path absPath = makeAbsPath(path).string();
std::optional<Cache::mapped_type> res; if (auto res = getConcurrent(cache, absPath))
cache.cvisit(absPath, [&](auto & x) { res.emplace(x.second); });
if (res)
return *res; return *res;
auto st = nix::maybeLstat(absPath.c_str()); auto st = nix::maybeLstat(absPath.c_str());