summaryrefslogtreecommitdiffstats
path: root/staging_vespalib
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 /staging_vespalib
parent56206b719c3936df7111593165bd34e45aa76ee5 (diff)
Use std::mutex for the hash lock
Diffstat (limited to 'staging_vespalib')
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/cache.h12
-rw-r--r--staging_vespalib/src/vespa/vespalib/stllike/cache.hpp35
2 files changed, 27 insertions, 20 deletions
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());
+}
+
}