From 33c2fd8af7b0f2968c7a78e9dc142b0cdd798a55 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 18 Nov 2019 10:11:58 +0100 Subject: [PATCH] Use move semantics when updating the content of the StateHolder --- pdns/sholder.hh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pdns/sholder.hh b/pdns/sholder.hh index 75837c30f1..ce5da43ce3 100644 --- a/pdns/sholder.hh +++ b/pdns/sholder.hh @@ -92,12 +92,22 @@ public: return LocalStateHolder(this); } - void setState(T state) //!< Safely & slowly change the global state + void setState(const T& state) //!< Safely & slowly change the global state { std::shared_ptr newState = std::make_shared(state); { std::lock_guard l(d_lock); - d_state = newState; + d_state = std::move(newState); + d_generation++; + } + } + + void setState(T&& state) //!< Safely & slowly change the global state + { + std::shared_ptr newState = std::make_shared(std::move(state)); + { + std::lock_guard l(d_lock); + d_state = std::move(newState); d_generation++; } } @@ -111,10 +121,10 @@ public: //! Safely & slowly modify the global state template void modify(F act) { - std::lock_guard l(d_lock); + 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(state); + d_state = std::make_shared(std::move(state)); ++d_generation; } -- 2.47.2