]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Wrap pthread_ objects
authorRemi Gacogne <remi.gacogne@powerdns.com>
Fri, 24 Apr 2020 15:44:09 +0000 (17:44 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 28 Apr 2020 13:39:35 +0000 (15:39 +0200)
pdns/logger.cc
pdns/opensslsigners.cc
pdns/rec-carbon.cc
pdns/rec_channel.hh
pdns/rec_channel_rec.cc

index ad64c3108456251174287214fb91098c79933cba..61dcf28f5fe9d61b153d716f1038e762c4315217 100644 (file)
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+
+#include <mutex>
+
 #include "logger.hh"
 #include "misc.hh"
 #ifndef RECURSOR
 #include "statbag.hh"
 extern StatBag S;
 #endif
-#include "lock.hh"
 #include "namespaces.hh"
 
 thread_local Logger::PerThread Logger::t_perThread;
@@ -96,8 +98,8 @@ void Logger::log(const string &msg, Urgency u)
       }
     }
 
-    static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
-    Lock l(&m); // the C++-2011 spec says we need this, and OSX actually does
+    static std::mutex m;
+    std::lock_guard<std::mutex> l(m); // the C++-2011 spec says we need this, and OSX actually does
     clog << string(buffer) + prefix + msg <<endl;
 #ifndef RECURSOR
     mustAccount=true;
index d879d67e4db1b8c712cecfe0633b70b8e615414f..5fc8110a439a76d6d5bf863153414917a860a8f5 100644 (file)
 
 #if (OPENSSL_VERSION_NUMBER < 0x1010000fL || (defined LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2090100fL)
 /* OpenSSL < 1.1.0 needs support for threading/locking in the calling application. */
-static pthread_mutex_t *openssllocks;
+
+#include "lock.hh"
+static std::vector<std::mutex> openssllocks;
 
 extern "C" {
 void openssl_pthreads_locking_callback(int mode, int type, const char *file, int line)
 {
   if (mode & CRYPTO_LOCK) {
-    pthread_mutex_lock(&(openssllocks[type]));
+    openssllocks.at(type).lock();
 
-  }else {
-    pthread_mutex_unlock(&(openssllocks[type]));
+  } else {
+    openssllocks.at(type).unlock();
   }
 }
 
@@ -62,24 +64,15 @@ unsigned long openssl_pthreads_id_callback()
 
 void openssl_thread_setup()
 {
-  openssllocks = (pthread_mutex_t*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
-
-  for (int i = 0; i < CRYPTO_num_locks(); i++)
-    pthread_mutex_init(&(openssllocks[i]), NULL);
-
-  CRYPTO_set_id_callback(openssl_pthreads_id_callback);
-  CRYPTO_set_locking_callback(openssl_pthreads_locking_callback);
+  openssllocks = std::vector<std::mutex>(CRYPTO_num_locks());
+  CRYPTO_set_id_callback(&openssl_pthreads_id_callback);
+  CRYPTO_set_locking_callback(&openssl_pthreads_locking_callback);
 }
 
 void openssl_thread_cleanup()
 {
-  CRYPTO_set_locking_callback(NULL);
-
-  for (int i=0; i<CRYPTO_num_locks(); i++) {
-    pthread_mutex_destroy(&(openssllocks[i]));
-  }
-
-  OPENSSL_free(openssllocks);
+  CRYPTO_set_locking_callback(nullptr);
+  openssllocks.clear();
 }
 
 #ifndef HAVE_RSA_GET0_KEY
index 218ee83716972150ee34aaac46406c1dcf0fcdfd..3eba50bd8c5bb9d48cedce4c5e3bf6fce9687ba7 100644 (file)
@@ -19,7 +19,7 @@ try
   vector<string> carbonServers;
 
   {
-    Lock l(&g_carbon_config_lock);
+    std::lock_guard<std::mutex> l(g_carbon_config_lock);
     stringtok(carbonServers, arg()["carbon-server"], ", ");
     namespace_name=arg()["carbon-namespace"];
     hostname=arg()["carbon-ourname"];
index 732bd81eacd0ae1514971a710e00364cd6eb5516..09c8f1def596d48694a704401cc30b64a62a52f8 100644 (file)
@@ -73,7 +73,7 @@ public:
 enum class StatComponent { API, Carbon, RecControl, SNMP };
 
 std::map<std::string, std::string> getAllStatsMap(StatComponent component);
-extern pthread_mutex_t g_carbon_config_lock;
+extern std::mutex g_carbon_config_lock;
 std::vector<std::pair<DNSName, uint16_t> >* pleaseGetQueryRing();
 std::vector<std::pair<DNSName, uint16_t> >* pleaseGetServfailQueryRing();
 std::vector<std::pair<DNSName, uint16_t> >* pleaseGetBogusQueryRing();
index 5ecca53444735ff388d654d31811a31380b1164b..489e81083f5b230af119b0919a8f593736a9b572 100644 (file)
 #include "secpoll-recursor.hh"
 #include "pubsuffix.hh"
 #include "namespaces.hh"
-pthread_mutex_t g_carbon_config_lock=PTHREAD_MUTEX_INITIALIZER;
+std::mutex g_carbon_config_lock;
 
 static map<string, const uint32_t*> d_get32bitpointers;
 static map<string, const std::atomic<uint64_t>*> d_getatomics;
 static map<string, function< uint64_t() > >  d_get64bitmembers;
-static pthread_mutex_t d_dynmetricslock = PTHREAD_MUTEX_INITIALIZER;
+static std::mutex d_dynmetricslock;
 static map<string, std::atomic<unsigned long>* > d_dynmetrics;
 
 static std::map<StatComponent, std::set<std::string>> s_blacklistedStats;
@@ -84,7 +84,7 @@ static void addGetStat(const string& name, function<uint64_t ()> f )
 
 std::atomic<unsigned long>* getDynMetric(const std::string& str)
 {
-  Lock l(&d_dynmetricslock);
+  std::lock_guard<std::mutex> l(d_dynmetricslock);
   auto f = d_dynmetrics.find(str);
   if(f != d_dynmetrics.end())
     return f->second;
@@ -105,7 +105,7 @@ static optional<uint64_t> get(const string& name)
   if(d_get64bitmembers.count(name))
     return d_get64bitmembers.find(name)->second();
 
-  Lock l(&d_dynmetricslock);
+  std::lock_guard<std::mutex> l(d_dynmetricslock);
   auto f =rplookup(d_dynmetrics, name);
   if(f)
     return (*f)->load();
@@ -141,7 +141,7 @@ map<string,string> getAllStatsMap(StatComponent component)
   }
 
   {
-    Lock l(&d_dynmetricslock);
+    std::lock_guard<std::mutex> l(d_dynmetricslock);
     for(const auto& a : d_dynmetrics) {
       if (blacklistMap.count(a.first) == 0) {
         ret.insert({a.first, std::to_string(*a.second)});
@@ -446,7 +446,7 @@ static string doWipeCache(T begin, T end, uint16_t qtype)
 template<typename T>
 static string doSetCarbonServer(T begin, T end)
 {
-  Lock l(&g_carbon_config_lock);
+  std::lock_guard<std::mutex> l(g_carbon_config_lock);
   if(begin==end) {
     ::arg().set("carbon-server").clear();
     return "cleared carbon-server setting\n";