From dd5f6afa332fb95b936899159206a480a2724026 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 2 Jan 2021 15:34:55 +0000 Subject: Fix EntCache locking Scope for improvements in getEntry regarding multiple threads calling fillCache --- netfs/lib/entCache.impl.h | 17 +++++++++-------- netfs/unittests/testLib.cpp | 1 - 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/netfs/lib/entCache.impl.h b/netfs/lib/entCache.impl.h index 13263af..37aecfd 100644 --- a/netfs/lib/entCache.impl.h +++ b/netfs/lib/entCache.impl.h @@ -57,15 +57,19 @@ template typename EntCache::Resolution EntCache::getEntry(const key_t & key) const { - if (fillTime + 60 > time(nullptr)) { + SharedScopeLock(lock) { + if (fillTime + 60 > time(nullptr)) { + if (auto ent = getEntryNoFill(key)) { + return ent; + } + } + } + ScopeLock(lock) { + fillCache(); if (auto ent = getEntryNoFill(key)) { return ent; } } - fillCache(); - if (auto ent = getEntryNoFill(key)) { - return ent; - } if (fallback) { return {fallback, ResolvedAs::Fallback}; } @@ -77,7 +81,6 @@ template typename EntCache::entry_ptr EntCache::getEntryNoFill(const key_t & key) const { - SharedLock(lock); auto & collection = idcache->template get(); auto i = collection.find(key); if (i != collection.end()) { @@ -106,7 +109,6 @@ template<> void EntCache::fillCache() const { - Lock(lock); setpwent(); idcache->clear(); std::array buf {}; @@ -124,7 +126,6 @@ template<> void EntCache::fillCache() const { - Lock(lock); setgrent(); std::array buf {}; idcache->clear(); diff --git a/netfs/unittests/testLib.cpp b/netfs/unittests/testLib.cpp index d3ce2fe..ff82040 100644 --- a/netfs/unittests/testLib.cpp +++ b/netfs/unittests/testLib.cpp @@ -27,7 +27,6 @@ template<> void EntCache::fillCache() const { - Lock(lock); idcache->insert(std::make_shared(1, "user1")); idcache->insert(std::make_shared(2, "user2")); idcache->insert(std::make_shared(3, "user3")); -- cgit v1.2.3