]> granicus.if.org Git - pdns/commitdiff
StateHolder: Allocate (and copy if needed) before taking the lock
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 14 Feb 2017 17:46:38 +0000 (18:46 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 16 Mar 2017 17:28:13 +0000 (18:28 +0100)
(cherry picked from commit 47678b827e9b20a23352e381b16312f1c29831de)

pdns/sholder.hh

index 457f3378578d92f5271c8c90dfe22a2f9f0cc803..5a57ddc59c9e723eb7ba911a46bab71df5351bb2 100644 (file)
@@ -93,9 +93,12 @@ public:
 
   void setState(T state) //!< Safely & slowly change the global state
   {
-    std::lock_guard<std::mutex> l(d_lock);
-    d_state = std::make_shared<T>(T(state));
-    d_generation++;
+    std::shared_ptr<T> newState = std::make_shared<T>(state);
+    {
+      std::lock_guard<std::mutex> 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<std::mutex> 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>(T(state));
+    d_state = std::make_shared<T>(state);
     ++d_generation;
   }