From: Remi Gacogne Date: Fri, 30 Apr 2021 13:53:00 +0000 (+0200) Subject: Convert the StateHolder to LockGuarded X-Git-Tag: dnsdist-1.7.0-alpha1~62^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a7f983e0329390229a2b9b99ad49f0e544abeb5;p=thirdparty%2Fpdns.git Convert the StateHolder to LockGuarded --- diff --git a/pdns/sholder.hh b/pdns/sholder.hh index ce5da43ce3..2a4f758c08 100644 --- a/pdns/sholder.hh +++ b/pdns/sholder.hh @@ -22,7 +22,9 @@ #pragma once #include #include -#include + +#include "lock.hh" + /** This is sort of a light-weight RCU idea. Suitable for when you frequently consult some "readonly" state, which infrequently gets changed. One way of dealing with this is fully locking access to the state, but @@ -96,8 +98,7 @@ public: { std::shared_ptr newState = std::make_shared(state); { - std::lock_guard l(d_lock); - d_state = std::move(newState); + *(d_state.lock()) = std::move(newState); d_generation++; } } @@ -106,29 +107,26 @@ public: { std::shared_ptr newState = std::make_shared(std::move(state)); { - std::lock_guard l(d_lock); - d_state = std::move(newState); + *(d_state.lock()) = std::move(newState); d_generation++; } } T getCopy() const //!< Safely & slowly get a copy of the global state { - std::lock_guard l(d_lock); - return *d_state; + return *(*(d_state.lock())); } //! Safely & slowly modify the global state template void modify(F act) { - std::lock_guard l(d_lock); - auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked! - act(state); - d_state = std::make_shared(std::move(state)); + auto state = d_state.lock(); + auto newState = *(*state); // and yes, these three steps are necessary, can't ever modify state in place, even when locked! + act(newState); + *state = std::make_shared(std::move(newState)); ++d_generation; } - typedef T value_type; private: unsigned int getGeneration() const @@ -137,12 +135,11 @@ private: } void getState(std::shared_ptr* state, unsigned int* generation) const { - std::lock_guard l(d_lock); - *state=d_state; + *state = *d_state.lock(); *generation = d_generation; } friend class LocalStateHolder; - mutable std::mutex d_lock; - std::shared_ptr d_state; + + mutable LockGuarded> d_state; std::atomic d_generation{1}; };