From: Remi Gacogne Date: Tue, 28 Apr 2020 13:33:47 +0000 (+0200) Subject: auth: Wrap a few more missed pthread_ objects X-Git-Tag: dnsdist-1.5.0-rc2~7^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9092419ebe8b68d9124f6e6d9607f2af75cdf149;p=thirdparty%2Fpdns.git auth: Wrap a few more missed pthread_ objects --- diff --git a/modules/geoipbackend/geoipbackend.cc b/modules/geoipbackend/geoipbackend.cc index d812efa315..612c9a5298 100644 --- a/modules/geoipbackend/geoipbackend.cc +++ b/modules/geoipbackend/geoipbackend.cc @@ -32,7 +32,7 @@ #include #include -pthread_rwlock_t GeoIPBackend::s_state_lock=PTHREAD_RWLOCK_INITIALIZER; +ReadWriteLock GeoIPBackend::s_state_lock; struct GeoIPDNSResourceRecord: DNSResourceRecord { int weight; diff --git a/modules/geoipbackend/geoipbackend.hh b/modules/geoipbackend/geoipbackend.hh index 1b885117bc..a45bbda4e5 100644 --- a/modules/geoipbackend/geoipbackend.hh +++ b/modules/geoipbackend/geoipbackend.hh @@ -67,7 +67,7 @@ public: bool unpublishDomainKey(const DNSName& name, unsigned int id) override; private: - static pthread_rwlock_t s_state_lock; + static ReadWriteLock s_state_lock; void initialize(); string format2str(string format, const Netmask &addr, GeoIPNetmask& gl); diff --git a/modules/gmysqlbackend/smysql.cc b/modules/gmysqlbackend/smysql.cc index f792089a70..6f2cb0dab6 100644 --- a/modules/gmysqlbackend/smysql.cc +++ b/modules/gmysqlbackend/smysql.cc @@ -65,7 +65,7 @@ private: static thread_local MySQLThreadCloser threadcloser; bool SMySQL::s_dolog; -pthread_mutex_t SMySQL::s_myinitlock = PTHREAD_MUTEX_INITIALIZER; +std::mutex SMySQL::s_myinitlock; class SMySQLStatement: public SSqlStatement { @@ -440,7 +440,7 @@ void SMySQL::connect() { int retry=1; - Lock l(&s_myinitlock); + std::lock_guard l(s_myinitlock); if (d_threadCleanup) { threadcloser.enable(); } diff --git a/modules/gmysqlbackend/smysql.hh b/modules/gmysqlbackend/smysql.hh index 74c5a4bc30..50780e9c51 100644 --- a/modules/gmysqlbackend/smysql.hh +++ b/modules/gmysqlbackend/smysql.hh @@ -20,6 +20,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once +#include + #include #include "pdns/backends/gsql/ssql.hh" #include "pdns/utility.hh" @@ -48,7 +50,7 @@ private: void connect(); static bool s_dolog; - static pthread_mutex_t s_myinitlock; + static std::mutex s_myinitlock; MYSQL d_db; std::string d_database; diff --git a/modules/tinydnsbackend/tinydnsbackend.cc b/modules/tinydnsbackend/tinydnsbackend.cc index 72e31c61d2..3f77b0e2f8 100644 --- a/modules/tinydnsbackend/tinydnsbackend.cc +++ b/modules/tinydnsbackend/tinydnsbackend.cc @@ -29,7 +29,7 @@ static string backendname="[TinyDNSBackend] "; uint32_t TinyDNSBackend::s_lastId; -pthread_mutex_t TinyDNSBackend::s_domainInfoLock=PTHREAD_MUTEX_INITIALIZER; +std::mutex TinyDNSBackend::s_domainInfoLock; TinyDNSBackend::TDI_suffix_t TinyDNSBackend::s_domainInfo; vector TinyDNSBackend::getLocations() @@ -90,7 +90,7 @@ TinyDNSBackend::TinyDNSBackend(const string &suffix) } void TinyDNSBackend::getUpdatedMasters(vector* retDomains) { - Lock l(&s_domainInfoLock); //TODO: We could actually lock less if we do it per suffix. + std::lock_guard l(s_domainInfoLock); //TODO: We could actually lock less if we do it per suffix. if (! s_domainInfo.count(d_suffix)) { TDI_t tmp; @@ -133,7 +133,7 @@ void TinyDNSBackend::getUpdatedMasters(vector* retDomains) { } void TinyDNSBackend::setNotified(uint32_t id, uint32_t serial) { - Lock l(&s_domainInfoLock); + std::lock_guard l(s_domainInfoLock); if (!s_domainInfo.count(d_suffix)) { throw PDNSException("Can't get list of domains to set the serial."); } diff --git a/modules/tinydnsbackend/tinydnsbackend.hh b/modules/tinydnsbackend/tinydnsbackend.hh index 5029546714..ecf28e075e 100644 --- a/modules/tinydnsbackend/tinydnsbackend.hh +++ b/modules/tinydnsbackend/tinydnsbackend.hh @@ -32,6 +32,7 @@ #include #include #include +#include using namespace ::boost; using namespace ::boost::multi_index; @@ -102,7 +103,7 @@ private: string d_suffix; // Statics - static pthread_mutex_t s_domainInfoLock; + static std::mutex s_domainInfoLock; static TDI_suffix_t s_domainInfo; static uint32_t s_lastId; // used to give a domain an id. }; diff --git a/pdns/common_startup.cc b/pdns/common_startup.cc index da3c8e3371..18553ed508 100644 --- a/pdns/common_startup.cc +++ b/pdns/common_startup.cc @@ -506,18 +506,14 @@ catch(PDNSException& pe) _exit(1); } -static void* dummyThread(void *) +static void dummyThread() { - void* ignore=0; - pthread_exit(ignore); } static void triggerLoadOfLibraries() { - pthread_t tid; - pthread_create(&tid, 0, dummyThread, 0); - void* res; - pthread_join(tid, &res); + std::thread dummy(dummyThread); + dummy.join(); } void mainthread() diff --git a/pdns/dynlistener.cc b/pdns/dynlistener.cc index 2deb2182b9..ab10d114da 100644 --- a/pdns/dynlistener.cc +++ b/pdns/dynlistener.cc @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -43,6 +42,8 @@ #include #include #include +#include + #include "misc.hh" #include "dns.hh" #include "arguments.hh" @@ -203,16 +204,8 @@ DynListener::DynListener(const string &progname) void DynListener::go() { d_ppid=getpid(); - pthread_create(&d_tid,0,&DynListener::theListenerHelper,this); -} - -void *DynListener::theListenerHelper(void *p) -{ - setThreadName("pdns/ctrlListen"); - DynListener *us=static_cast(p); - us->theListener(); - g_log< #include -#include #include #include #include @@ -45,7 +44,6 @@ public: ~DynListener(); void go(); void theListener(); - static void *theListenerHelper(void *p); typedef string g_funk_t(const vector &parts, Utility::pid_t ppid); // guido! typedef struct { g_funk_t *func; string args; string usage; } g_funkwithusage_t; @@ -66,7 +64,6 @@ private: NetmaskGroup d_tcprange; int d_s{-1}; int d_client{-1}; - pthread_t d_tid{0}; bool d_nonlocal; bool d_tcp{false}; pid_t d_ppid{0}; diff --git a/pdns/test-lock_hh.cc b/pdns/test-lock_hh.cc index bdd3697504..46e844c60a 100644 --- a/pdns/test-lock_hh.cc +++ b/pdns/test-lock_hh.cc @@ -11,26 +11,20 @@ using namespace boost; BOOST_AUTO_TEST_SUITE(test_lock_hh) -static std::vector > g_locks; +static std::vector g_locks(1000); static void lthread() { std::vector rlocks; for(auto& pp : g_locks) - rlocks.emplace_back(&*pp); + rlocks.emplace_back(pp); } BOOST_AUTO_TEST_CASE(test_pdns_lock) { - for(unsigned int n=0; n < 1000; ++n) { - auto p = make_unique(); - pthread_rwlock_init(p.get(), 0); - g_locks.emplace_back(std::move(p)); - } - std::vector rlocks; for(auto& pp : g_locks) - rlocks.emplace_back(&*pp); + rlocks.emplace_back(pp); std::thread thr(lthread); thr.join(); @@ -38,13 +32,13 @@ BOOST_AUTO_TEST_CASE(test_pdns_lock) std::vector wlocks; for(auto& pp : g_locks) - wlocks.emplace_back(&*pp); + wlocks.emplace_back(pp); // on macOS, this TryReadLock throws (EDEADLK) instead of simply failing // so we catch the exception and consider that success for this test bool gotit = false; try { - TryReadLock trl(&*g_locks[0]); + TryReadLock trl(g_locks.at(0)); gotit = trl.gotIt(); } catch(const PDNSException &e) { @@ -55,13 +49,11 @@ BOOST_AUTO_TEST_CASE(test_pdns_lock) wlocks.clear(); { - TryReadLock trl2(&*g_locks[0]); + TryReadLock trl2(g_locks.at(0)); BOOST_CHECK(trl2.gotIt()); } - for(auto& pp : g_locks) { - pthread_rwlock_destroy(pp.get()); - } + g_locks.clear(); } BOOST_AUTO_TEST_SUITE_END()