From a6d24b3ac1d0416847dddbfd686e953552ca68f6 Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Tue, 17 Oct 2023 15:51:30 +0000 Subject: Use `shared_mutex` to allow non-contending reads (common case) --- .../vespa/documentapi/messagebus/policies/contentpolicy.cpp | 10 +++++----- .../src/vespa/documentapi/messagebus/policies/contentpolicy.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'documentapi/src') diff --git a/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.cpp b/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.cpp index c13f32f2df5..ea27d42e790 100644 --- a/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.cpp +++ b/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.cpp @@ -103,7 +103,7 @@ void ContentPolicy::configure(std::unique_ptr config) { try { - std::lock_guard guard(_lock); + std::lock_guard guard(_rw_lock); _distribution = std::make_unique(*config); } catch (const std::exception& e) { LOG(warning, "Got exception when configuring distribution, config id was %s", _clusterConfigId.c_str()); @@ -235,7 +235,7 @@ void ContentPolicy::updateStateFromReply(WrongDistributionReply& wdr) { auto newState = std::make_unique(wdr.getSystemState()); - std::lock_guard guard(_lock); + std::lock_guard guard(_rw_lock); if (!_state || newState->getVersion() >= _state->getVersion()) { if (_state) { wdr.getTrace().trace(1, make_string("System state changed from version %u to %u", @@ -256,14 +256,14 @@ ContentPolicy::updateStateFromReply(WrongDistributionReply& wdr) ContentPolicy::StateSnapshot ContentPolicy::internal_state_snapshot() { - std::lock_guard guard(_lock); + std::shared_lock guard(_rw_lock); return {_state, _distribution}; } std::shared_ptr ContentPolicy::getSystemState() const noexcept { - std::lock_guard guard(_lock); + std::shared_lock guard(_rw_lock); return _state; } @@ -273,7 +273,7 @@ ContentPolicy::reset_state() // It's possible for the caller to race between checking and resetting the state, // but this should never lead to a worse outcome than sending to a random distributor // as if no state had been cached prior. - std::lock_guard guard(_lock); + std::lock_guard guard(_rw_lock); _state.reset(); } diff --git a/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.h b/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.h index 182b35a0e98..7a3675c3001 100644 --- a/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.h +++ b/documentapi/src/vespa/documentapi/messagebus/policies/contentpolicy.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include namespace config { class ICallback; @@ -24,7 +24,7 @@ class ContentPolicy : public ExternSlobrokPolicy { private: document::BucketIdFactory _bucketIdFactory; - mutable std::mutex _lock; + mutable std::shared_mutex _rw_lock; std::shared_ptr _state; string _clusterName; string _clusterConfigId; -- cgit v1.2.3