]> granicus.if.org Git - pdns/commitdiff
auth statbag: move to std::mutex, avoid copies
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Fri, 27 Sep 2019 15:09:47 +0000 (17:09 +0200)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Fri, 27 Sep 2019 15:09:47 +0000 (17:09 +0200)
pdns/statbag.cc
pdns/statbag.hh

index be36420e10b6bf69d78a86d0931a8d945cfe54c3..4900ec56e652c7d3a244a681ffa1587d32c655b7 100644 (file)
@@ -164,27 +164,26 @@ template<typename T, typename Comp>
 StatRing<T,Comp>::StatRing(unsigned int size)
 {
   d_items.set_capacity(size);
-  pthread_mutex_init(&d_lock, 0);
 }
 
 template<typename T, typename Comp>
 void StatRing<T,Comp>::account(const T& t)
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   d_items.push_back(t);
 }
 
 template<typename T, typename Comp>
 unsigned int StatRing<T,Comp>::getSize()
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   return d_items.capacity();
 }
 
 template<typename T, typename Comp>
 void StatRing<T,Comp>::resize(unsigned int newsize)
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   d_items.set_capacity(newsize);
 }
 
@@ -205,7 +204,7 @@ string StatRing<T,Comp>::getHelp()
 template<typename T, typename Comp>
 vector<pair<T, unsigned int> >StatRing<T,Comp>::get() const
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   map<T,unsigned int, Comp> res;
   for(typename boost::circular_buffer<T>::const_iterator i=d_items.begin();i!=d_items.end();++i) {
     res[*i]++;
@@ -222,19 +221,19 @@ vector<pair<T, unsigned int> >StatRing<T,Comp>::get() const
 
 void StatBag::declareRing(const string &name, const string &help, unsigned int size)
 {
-  d_rings[name]=StatRing<string, CIStringCompare>(size);
+  d_rings.emplace(name, size);
   d_rings[name].setHelp(help);
 }
 
 void StatBag::declareComboRing(const string &name, const string &help, unsigned int size)
 {
-  d_comborings[name]=StatRing<SComboAddress>(size);
+  d_comborings.emplace(name, size);
   d_comborings[name].setHelp(help);
 }
 
 void StatBag::declareDNSNameQTypeRing(const string &name, const string &help, unsigned int size)
 {
-  d_dnsnameqtyperings[name] = StatRing<std::tuple<DNSName, QType> >(size);
+  d_dnsnameqtyperings.emplace(name, size);
   d_dnsnameqtyperings[name].setHelp(help);
 }
 
@@ -264,7 +263,7 @@ vector<pair<string, unsigned int> > StatBag::getRing(const string &name)
 template<typename T, typename Comp>
 void StatRing<T,Comp>::reset()
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   d_items.clear();
 }
 
index 0992a0946d3818ffeb8cc2824f0a7987f7aa0dec..a8f5663a59995c51c087316b92a7074ba0a534b9 100644 (file)
@@ -23,6 +23,7 @@
 #define STATBAG_HH
 #include <pthread.h>
 #include <map>
+#include <mutex>
 #include <functional>
 #include <string>
 #include <vector>
@@ -54,7 +55,7 @@ private:
   }
 
   boost::circular_buffer<T> d_items;
-  mutable pthread_mutex_t d_lock;
+  mutable std::mutex d_lock;
   string d_help;
 };