aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2020-10-08 19:29:36 +0000
committerHenning Baldersheim <balder@yahoo-inc.com>2020-10-08 21:56:34 +0000
commita4d0cd759eed68318afebbaddbc4baddc70416e2 (patch)
treeec9e220d3f14e81e14a046e449b0bb6478edce38
parent56206b719c3936df7111593165bd34e45aa76ee5 (diff)
Use std::mutex for the hash lock
-rw-r--r--searchlib/src/vespa/searchlib/docstore/visitcache.cpp4
-rw-r--r--searchlib/src/vespa/searchlib/docstore/visitcache.h4
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/cache.h12
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/cache.hpp35
4 files changed, 31 insertions, 24 deletions
diff --git a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
index 6990a0a3ed7..c3f4214fb60 100644
--- a/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
+++ b/searchlib/src/vespa/searchlib/docstore/visitcache.cpp
@@ -167,7 +167,7 @@ VisitCache::reconfigure(size_t cacheSize, const CompressionConfig &compression)
VisitCache::Cache::IdSet
-VisitCache::Cache::findSetsContaining(const LockGuard &, const KeySet & keys) const {
+VisitCache::Cache::findSetsContaining(const UniqueLock &, const KeySet & keys) const {
IdSet found;
for (uint32_t subKey : keys.getKeys()) {
const auto foundLid = _lid2Id.find(subKey);
@@ -194,7 +194,7 @@ VisitCache::Cache::readSet(const KeySet & key)
}
void
-VisitCache::Cache::locateAndInvalidateOtherSubsets(const LockGuard & cacheGuard, const KeySet & keys)
+VisitCache::Cache::locateAndInvalidateOtherSubsets(const UniqueLock & cacheGuard, const KeySet & keys)
{
// Due to the implementation of insert where the global lock is released and the fact
// that 2 overlapping keysets kan have different keys and use different ValueLock
diff --git a/searchlib/src/vespa/searchlib/docstore/visitcache.h b/searchlib/src/vespa/searchlib/docstore/visitcache.h
index 7aa264bda02..430481acd4c 100644
--- a/searchlib/src/vespa/searchlib/docstore/visitcache.h
+++ b/searchlib/src/vespa/searchlib/docstore/visitcache.h
@@ -150,12 +150,12 @@ private:
CompressedBlobSet readSet(const KeySet & keys);
void removeKey(uint32_t key);
private:
- void locateAndInvalidateOtherSubsets(const vespalib::LockGuard & cacheGuard, const KeySet & keys);
+ void locateAndInvalidateOtherSubsets(const UniqueLock & cacheGuard, const KeySet & keys);
using IdSet = vespalib::hash_set<uint64_t>;
using Parent = vespalib::cache<CacheParams>;
using LidUniqueKeySetId = vespalib::hash_map<uint32_t, uint64_t>;
using IdKeySetMap = vespalib::hash_map<uint64_t, KeySet>;
- IdSet findSetsContaining(const vespalib::LockGuard &, const KeySet & keys) const;
+ IdSet findSetsContaining(const UniqueLock &, const KeySet & keys) const;
void onInsert(const K & key) override;
void onRemove(const K & key) override;
LidUniqueKeySetId _lid2Id;
diff --git a/staging_vespalib/src/vespa/vespalib/stllike/cache.h b/staging_vespalib/src/vespa/vespalib/stllike/cache.h
index 2c7722724cf..d0491e4d246 100644
--- a/staging_vespalib/src/vespa/vespalib/stllike/cache.h
+++ b/staging_vespalib/src/vespa/vespalib/stllike/cache.h
@@ -42,7 +42,7 @@ struct CacheParam : public P
template< typename P >
class cache : private lrucache_map<P>
{
- typedef lrucache_map<P> Lru;
+ using Lru = lrucache_map<P>;
protected:
typedef typename P::BackingStore BackingStore;
typedef typename P::Hash Hash;
@@ -120,10 +120,12 @@ public:
size_t getlookup() const { return _lookup; }
protected:
- vespalib::LockGuard getGuard();
- void invalidate(const vespalib::LockGuard & guard, const K & key);
- bool hasKey(const vespalib::LockGuard & guard, const K & key) const;
+ using UniqueLock = std::unique_lock<std::mutex>;
+ UniqueLock getGuard();
+ void invalidate(const UniqueLock & guard, const K & key);
+ bool hasKey(const UniqueLock & guard, const K & key) const;
private:
+ void verifyHashLock(const UniqueLock & guard) const;
/**
* Called when an object is inserted, to see if the LRU should be removed.
* Default is to obey the maxsize given in constructor.
@@ -152,7 +154,7 @@ private:
mutable size_t _invalidate;
mutable size_t _lookup;
BackingStore & _store;
- vespalib::Lock _hashLock;
+ mutable std::mutex _hashLock;
/// Striped locks that can be used for having a locked access to the backing store.
vespalib::Lock _addLocks[113];
};
diff --git a/staging_vespalib/src/vespa/vespalib/stllike/cache.hpp b/staging_vespalib/src/vespa/vespalib/stllike/cache.hpp
index ebad3ef6a09..652abc6672b 100644
--- a/staging_vespalib/src/vespa/vespalib/stllike/cache.hpp
+++ b/staging_vespalib/src/vespa/vespalib/stllike/cache.hpp
@@ -30,14 +30,14 @@ cache<P>::setCapacityBytes(size_t sz) {
template< typename P >
void
cache<P>::invalidate(const K & key) {
- vespalib::LockGuard guard(_hashLock);
+ UniqueLock guard(_hashLock);
invalidate(guard, key);
}
template< typename P >
bool
cache<P>::hasKey(const K & key) const {
- vespalib::LockGuard guard(_hashLock);
+ UniqueLock guard(_hashLock);
return hasKey(guard, key);
}
@@ -73,9 +73,9 @@ cache<P>::removeOldest(const value_type & v) {
}
template< typename P >
-vespalib::LockGuard
+std::unique_lock<std::mutex>
cache<P>::getGuard() {
- return vespalib::LockGuard(_hashLock);
+ return UniqueLock(_hashLock);
}
template< typename P >
@@ -83,7 +83,7 @@ typename P::Value
cache<P>::read(const K & key)
{
{
- vespalib::LockGuard guard(_hashLock);
+ std::lock_guard guard(_hashLock);
if (Lru::hasKey(key)) {
_hit++;
return (*this)[key];
@@ -94,7 +94,7 @@ cache<P>::read(const K & key)
vespalib::LockGuard storeGuard(getLock(key));
{
- vespalib::LockGuard guard(_hashLock);
+ std::lock_guard guard(_hashLock);
if (Lru::hasKey(key)) {
// Somebody else just fetched it ahead of me.
_race++;
@@ -103,7 +103,7 @@ cache<P>::read(const K & key)
}
V value;
if (_store.read(key, value)) {
- vespalib::LockGuard guard(_hashLock);
+ std::lock_guard guard(_hashLock);
Lru::insert(key, value);
_sizeBytes += calcSize(key, value);
_insert++;
@@ -120,7 +120,7 @@ cache<P>::write(const K & key, V value)
size_t newSize = calcSize(key, value);
vespalib::LockGuard storeGuard(getLock(key));
{
- vespalib::LockGuard guard(_hashLock);
+ std::lock_guard guard(_hashLock);
if (Lru::hasKey(key)) {
_sizeBytes -= calcSize(key, (*this)[key]);
_update++;
@@ -129,7 +129,7 @@ cache<P>::write(const K & key, V value)
_store.write(key, value);
{
- vespalib::LockGuard guard(_hashLock);
+ std::lock_guard guard(_hashLock);
(*this)[key] = std::move(value);
_sizeBytes += newSize;
_write++;
@@ -147,10 +147,9 @@ cache<P>::erase(const K & key)
template< typename P >
void
-cache<P>::invalidate(const vespalib::LockGuard & guard, const K & key)
+cache<P>::invalidate(const UniqueLock & guard, const K & key)
{
- assert(guard.locks(_hashLock));
- (void) guard;
+ verifyHashLock(guard);
if (Lru::hasKey(key)) {
_sizeBytes -= calcSize(key, (*this)[key]);
_invalidate++;
@@ -160,13 +159,19 @@ cache<P>::invalidate(const vespalib::LockGuard & guard, const K & key)
template< typename P >
bool
-cache<P>::hasKey(const vespalib::LockGuard & guard, const K & key) const
+cache<P>::hasKey(const UniqueLock & guard, const K & key) const
{
- (void) guard;
- assert(guard.locks(_hashLock));
+ verifyHashLock(guard);
_lookup++;
return Lru::hasKey(key);
}
+template< typename P >
+void
+cache<P>::verifyHashLock(const UniqueLock & guard) const {
+ assert(guard.mutex() == & _hashLock);
+ assert(guard.owns_lock());
+}
+
}