From: Remi Gacogne Date: Fri, 24 Apr 2020 15:44:09 +0000 (+0200) Subject: rec: Wrap pthread_ objects X-Git-Tag: dnsdist-1.5.0-rc2~7^2~5 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fpdns.git;a=commitdiff_plain;h=69b8131b51746192b9ce3209827676e19e0a14ed rec: Wrap pthread_ objects --- diff --git a/pdns/logger.cc b/pdns/logger.cc index ad64c31084..61dcf28f5f 100644 --- a/pdns/logger.cc +++ b/pdns/logger.cc @@ -22,13 +22,15 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif + +#include + #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 l(m); // the C++-2011 spec says we need this, and OSX actually does clog << string(buffer) + prefix + msg < 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(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 carbonServers; { - Lock l(&g_carbon_config_lock); + std::lock_guard l(g_carbon_config_lock); stringtok(carbonServers, arg()["carbon-server"], ", "); namespace_name=arg()["carbon-namespace"]; hostname=arg()["carbon-ourname"]; diff --git a/pdns/rec_channel.hh b/pdns/rec_channel.hh index 732bd81eac..09c8f1def5 100644 --- a/pdns/rec_channel.hh +++ b/pdns/rec_channel.hh @@ -73,7 +73,7 @@ public: enum class StatComponent { API, Carbon, RecControl, SNMP }; std::map getAllStatsMap(StatComponent component); -extern pthread_mutex_t g_carbon_config_lock; +extern std::mutex g_carbon_config_lock; std::vector >* pleaseGetQueryRing(); std::vector >* pleaseGetServfailQueryRing(); std::vector >* pleaseGetBogusQueryRing(); diff --git a/pdns/rec_channel_rec.cc b/pdns/rec_channel_rec.cc index 5ecca53444..489e81083f 100644 --- a/pdns/rec_channel_rec.cc +++ b/pdns/rec_channel_rec.cc @@ -37,12 +37,12 @@ #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 d_get32bitpointers; static map*> d_getatomics; static map > d_get64bitmembers; -static pthread_mutex_t d_dynmetricslock = PTHREAD_MUTEX_INITIALIZER; +static std::mutex d_dynmetricslock; static map* > d_dynmetrics; static std::map> s_blacklistedStats; @@ -84,7 +84,7 @@ static void addGetStat(const string& name, function f ) std::atomic* getDynMetric(const std::string& str) { - Lock l(&d_dynmetricslock); + std::lock_guard l(d_dynmetricslock); auto f = d_dynmetrics.find(str); if(f != d_dynmetrics.end()) return f->second; @@ -105,7 +105,7 @@ static optional get(const string& name) if(d_get64bitmembers.count(name)) return d_get64bitmembers.find(name)->second(); - Lock l(&d_dynmetricslock); + std::lock_guard l(d_dynmetricslock); auto f =rplookup(d_dynmetrics, name); if(f) return (*f)->load(); @@ -141,7 +141,7 @@ map getAllStatsMap(StatComponent component) } { - Lock l(&d_dynmetricslock); + std::lock_guard 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 static string doSetCarbonServer(T begin, T end) { - Lock l(&g_carbon_config_lock); + std::lock_guard l(g_carbon_config_lock); if(begin==end) { ::arg().set("carbon-server").clear(); return "cleared carbon-server setting\n";