From: Remi Gacogne Date: Tue, 14 Feb 2017 17:46:38 +0000 (+0100) Subject: StateHolder: Allocate (and copy if needed) before taking the lock X-Git-Tag: rec-4.0.5-rc1~7^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=651c0e90eda11145a091f55dc590fecbaab4d2f9;p=thirdparty%2Fpdns.git StateHolder: Allocate (and copy if needed) before taking the lock (cherry picked from commit 47678b827e9b20a23352e381b16312f1c29831de) --- diff --git a/pdns/sholder.hh b/pdns/sholder.hh index 457f337857..5a57ddc59c 100644 --- a/pdns/sholder.hh +++ b/pdns/sholder.hh @@ -93,9 +93,12 @@ public: void setState(T state) //!< Safely & slowly change the global state { - std::lock_guard l(d_lock); - d_state = std::make_shared(T(state)); - d_generation++; + std::shared_ptr newState = std::make_shared(state); + { + std::lock_guard l(d_lock); + d_state = newState; + d_generation++; + } } T getCopy() const //!< Safely & slowly get a copy of the global state @@ -110,7 +113,7 @@ public: 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(T(state)); + d_state = std::make_shared(state); ++d_generation; }