]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Merge pull request #9070 from rgacogne/boost-173 dnsdist-1.5.0-rc2
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 6 May 2020 08:46:57 +0000 (10:46 +0200)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 08:46:57 +0000 (10:46 +0200)
Boost 1.73 moved boost::bind placeholders to the placeholders namespace

74 files changed:
modules/bindbackend/bindbackend2.cc
modules/bindbackend/bindbackend2.hh
modules/geoipbackend/geoipbackend.cc
modules/geoipbackend/geoipbackend.hh
modules/gmysqlbackend/smysql.cc
modules/gmysqlbackend/smysql.hh
modules/ldapbackend/ldapauthenticator.cc
modules/ldapbackend/ldapauthenticator_p.hh
modules/tinydnsbackend/tinydnsbackend.cc
modules/tinydnsbackend/tinydnsbackend.hh
pdns/auth-packetcache.cc
pdns/auth-packetcache.hh
pdns/auth-querycache.cc
pdns/auth-querycache.hh
pdns/common_startup.cc
pdns/communicator.cc
pdns/communicator.hh
pdns/dbdnsseckeeper.cc
pdns/distributor.hh
pdns/dnsbackend.cc
pdns/dnscrypt.cc
pdns/dnscrypt.hh
pdns/dnsdist-cache.hh
pdns/dnsdist-web.cc
pdns/dnsdist.hh
pdns/dnsdistdist/dnsdist-backend.cc
pdns/dnsdistdist/dnsdist-kvs.cc
pdns/dnsdistdist/dnsdist-kvs.hh
pdns/dnsdistdist/dnsdist-rules.hh
pdns/dnsdistdist/libssl.cc
pdns/dnsdistdist/tcpiohandler.cc
pdns/dnsproxy.cc
pdns/dnsproxy.hh
pdns/dnsseckeeper.hh
pdns/dnssecsigner.cc
pdns/dnstcpbench.cc
pdns/dynlistener.cc
pdns/dynlistener.hh
pdns/filterpo.cc
pdns/iputils.hh
pdns/libssl.hh
pdns/lock.hh
pdns/logger.cc
pdns/logger.hh
pdns/lua-record.cc
pdns/mastercommunicator.cc
pdns/misc.cc
pdns/opensslsigners.cc
pdns/packethandler.hh
pdns/pdnsutil.cc
pdns/pkcs11signers.cc
pdns/rec-carbon.cc
pdns/rec_channel.hh
pdns/rec_channel_rec.cc
pdns/receiver.cc
pdns/recursordist/docs/settings.rst
pdns/recursordist/test-syncres_cc.cc
pdns/remote_logger.hh
pdns/rfc2136handler.cc
pdns/slavecommunicator.cc
pdns/speedtest.cc
pdns/ssqlite3.cc
pdns/stubresolver.cc
pdns/tcpreceiver.cc
pdns/tcpreceiver.hh
pdns/test-dnsdistpacketcache_cc.cc
pdns/test-lock_hh.cc
pdns/test-packetcache_cc.cc
pdns/test-signers.cc
pdns/test-statbag_cc.cc
pdns/ueberbackend.cc
pdns/ueberbackend.hh
pdns/ws-auth.cc
pdns/ws-auth.hh

index c61d33dc6c439bce2435fc26ffb5672544cdaf21..03e71e0fbaae1bff7ed126ca88fb4f9b2829bdb5 100644 (file)
@@ -77,9 +77,9 @@ Bind2Backend::state_t Bind2Backend::s_state;
 int Bind2Backend::s_first=1;
 bool Bind2Backend::s_ignore_broken_records=false;
 
-pthread_rwlock_t Bind2Backend::s_state_lock=PTHREAD_RWLOCK_INITIALIZER;
-pthread_mutex_t Bind2Backend::s_supermaster_config_lock=PTHREAD_MUTEX_INITIALIZER; // protects writes to config file
-pthread_mutex_t Bind2Backend::s_startup_lock=PTHREAD_MUTEX_INITIALIZER;
+ReadWriteLock Bind2Backend::s_state_lock;
+std::mutex Bind2Backend::s_supermaster_config_lock; // protects writes to config file
+std::mutex Bind2Backend::s_startup_lock;
 string Bind2Backend::s_binddirectory;  
 
 template <typename T>
@@ -463,7 +463,7 @@ void Bind2Backend::alsoNotifies(const DNSName& domain, set<string> *ips)
       (*ips).insert(str);
     }
   }
-  ReadLock rl(&s_state_lock);  
+  ReadLock rl(&s_state_lock);
   for(state_t::const_iterator i = s_state.begin(); i != s_state.end() ; ++i) {
     if(i->d_name == domain) {
       for(set<string>::iterator it = i->d_also_notify.begin(); it != i->d_also_notify.end(); it++) {
@@ -730,7 +730,7 @@ Bind2Backend::Bind2Backend(const string &suffix, bool loadZones)
   if (!loadZones && d_hybrid)
     return;
 
-  Lock l(&s_startup_lock);
+  std::lock_guard<std::mutex> l(s_startup_lock);
   
   setupDNSSEC();
   if(!s_first) {
@@ -1375,7 +1375,7 @@ bool Bind2Backend::createSlaveDomain(const string &ip, const DNSName& domain, co
     << "' from supermaster " << ip << endl;
 
   {
-    Lock l2(&s_supermaster_config_lock);
+    std::lock_guard<std::mutex> l2(s_supermaster_config_lock);
         
     ofstream c_of(getArg("supermaster-config").c_str(),  std::ios::app);
     if (!c_of) {
index 1a33194326ef68b936f758e9f6822148ae31c138..aa38223bd0976ea739d047d813bb1ba40c7c1c5d 100644 (file)
@@ -187,7 +187,7 @@ public:
   void getAllDomains(vector<DomainInfo> *domains, bool include_disabled=false) override;
 
   static DNSBackend *maker();
-  static pthread_mutex_t s_startup_lock;
+  static std::mutex s_startup_lock;
 
   void setFresh(uint32_t domain_id) override;
   void setNotified(uint32_t id, uint32_t serial) override;
@@ -221,7 +221,7 @@ public:
                                               ordered_unique<tag<NameTag>, member<BB2DomainInfo, DNSName, &BB2DomainInfo::d_name> >
                                               > > state_t;
   static state_t s_state;
-  static pthread_rwlock_t s_state_lock;
+  static ReadWriteLock s_state_lock;
 
   void parseZoneFile(BB2DomainInfo *bbd);
   void rediscover(string *status=nullptr) override;
@@ -229,7 +229,7 @@ public:
 
   // for supermaster support
   bool superMasterBackend(const string &ip, const DNSName &domain, const vector<DNSResourceRecord>&nsset, string *nameserver, string *account, DNSBackend **db) override;
-  static pthread_mutex_t s_supermaster_config_lock;
+  static std::mutex s_supermaster_config_lock;
   bool createSlaveDomain(const string &ip, const DNSName &domain, const string &nameserver, const string &account) override;
 
 private:
index d812efa31504669740d53be5d90d650c17778e1f..612c9a5298a7605ef1bcce5d784f0ebb7ce092cd 100644 (file)
@@ -32,7 +32,7 @@
 #include <fstream>
 #include <yaml-cpp/yaml.h>
 
-pthread_rwlock_t GeoIPBackend::s_state_lock=PTHREAD_RWLOCK_INITIALIZER;
+ReadWriteLock GeoIPBackend::s_state_lock;
 
 struct GeoIPDNSResourceRecord: DNSResourceRecord {
   int weight;
index 1b885117bcd9a4cfae29b588cc3079229772beaa..a45bbda4e54ae0972b105cbbcbbca7fad200940a 100644 (file)
@@ -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);
index f792089a70605769119b398f48a5ee091d5d93dd..6f2cb0dab660a32f3c8cecf6a7e74b51ff9e853a 100644 (file)
@@ -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<std::mutex> l(s_myinitlock);
   if (d_threadCleanup) {
     threadcloser.enable();
   }
index 74c5a4bc302f2c0317a2f15f1ec807dd7f830d13..50780e9c51a1957a68fe00acfda103040f5f44d5 100644 (file)
@@ -20,6 +20,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #pragma once
+#include <mutex>
+
 #include <mysql.h>
 #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;
index 031d5fe0974545ca297095fa8c2a80a51ca3a172..1073e8031140606f27e9d8aa9580fc861fcbcaa7 100644 (file)
@@ -82,7 +82,7 @@ static int ldapGssapiAuthenticatorSaslInteractCallback( LDAP *conn, unsigned fla
 }
 
 LdapGssapiAuthenticator::LdapGssapiAuthenticator( const std::string& kt, const std::string &ccache, int tmout )
-  : d_logPrefix( "[LDAP GSSAPI] " ), d_keytabFile( kt ), d_cCacheFile( ccache ), d_timeout( tmout )
+  : d_logPrefix( "[LDAP GSSAPI] " ), d_keytabFile( kt ), d_cCacheFile( ccache )
 {
   krb5_error_code code;
 
index 5cf8d412008aa4ede5f91ae301cd55803dfb26d5..011549933a7f2dbcedb05cf95f75e77fd9e42b75 100644 (file)
@@ -45,7 +45,6 @@ class LdapGssapiAuthenticator : public LdapAuthenticator
     std::string d_logPrefix;
     std::string d_keytabFile;
     std::string d_cCacheFile;
-    int d_timeout;
     std::string d_lastError;
 
     krb5_context d_context;
index 72e31c61d29e50707802af721aa00f0b3b562d67..3f77b0e2f8a76816c771300278e6c21b73be1abe 100644 (file)
@@ -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<string> TinyDNSBackend::getLocations()
@@ -90,7 +90,7 @@ TinyDNSBackend::TinyDNSBackend(const string &suffix)
 }
 
 void TinyDNSBackend::getUpdatedMasters(vector<DomainInfo>* retDomains) {
-  Lock l(&s_domainInfoLock); //TODO: We could actually lock less if we do it per suffix.
+  std::lock_guard<std::mutex> 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<DomainInfo>* retDomains) {
 }
 
 void TinyDNSBackend::setNotified(uint32_t id, uint32_t serial) {
-  Lock l(&s_domainInfoLock);
+  std::lock_guard<std::mutex> l(s_domainInfoLock);
   if (!s_domainInfo.count(d_suffix)) {
     throw PDNSException("Can't get list of domains to set the serial.");
   }
index 5029546714523831585b75cd4374f8e556e2b247..ecf28e075ee27ad44240dd907388e3aba610c27b 100644 (file)
@@ -32,6 +32,7 @@
 #include <boost/multi_index_container.hpp>
 #include <boost/multi_index/hashed_index.hpp>
 #include <boost/multi_index/member.hpp>
+#include <mutex>
 
 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.
 };
index 71dbf134d50abdddf88086eb070266400d712b8a..759864e58888323b83981edccd7e210e9c004ad7 100644 (file)
@@ -46,13 +46,11 @@ AuthPacketCache::AuthPacketCache(size_t mapsCount): d_maps(mapsCount), d_lastcle
 AuthPacketCache::~AuthPacketCache()
 {
   try {
-    vector<WriteLock*> locks;
+    vector<WriteLock> locks;
     for(auto& mc : d_maps) {
-      locks.push_back(new WriteLock(&mc.d_mut));
-    }
-    for(auto wl : locks) {
-      delete wl;
+      locks.push_back(WriteLock(mc.d_mut));
     }
+    locks.clear();
   }
   catch(...) {
   }
index 0d3af47a251ef2cc7f85373a86001b424178f8e6..bceab073de59797980e75056d3bea546d68d5e41 100644 (file)
@@ -108,17 +108,15 @@ private:
   struct MapCombo
   {
     MapCombo() {
-      pthread_rwlock_init(&d_mut, nullptr);
     }
     ~MapCombo() {
-      pthread_rwlock_destroy(&d_mut);
     }
     MapCombo(const MapCombo&) = delete; 
     MapCombo& operator=(const MapCombo&) = delete;
 
     void reserve(size_t numberOfEntries);
 
-    pthread_rwlock_t d_mut;
+    ReadWriteLock d_mut;
     cmap_t d_map;
   };
 
index d4970bd137a8cd7d6851afebb295861ca00327a0..f98d9b133d875aa8d15222ac17c40d0831ae426a 100644 (file)
@@ -47,13 +47,11 @@ AuthQueryCache::AuthQueryCache(size_t mapsCount): d_maps(mapsCount), d_lastclean
 AuthQueryCache::~AuthQueryCache()
 {
   try {
-    vector<WriteLock*> locks;
+    vector<WriteLock> locks;
     for(auto& mc : d_maps) {
-      locks.push_back(new WriteLock(&mc.d_mut));
-    }
-    for(auto wl : locks) {
-      delete wl;
+      locks.push_back(WriteLock(mc.d_mut));
     }
+    locks.clear();
   }
   catch(...) {
   }
index c62230b2280f73f2984f0e9fd5e7d5e09bd7ffd9..f0c477e2f08ce59d0ab55d4c2457658f06399d26 100644 (file)
@@ -91,17 +91,15 @@ private:
   struct MapCombo
   {
     MapCombo() {
-      pthread_rwlock_init(&d_mut, nullptr);
     }
     ~MapCombo() {
-      pthread_rwlock_destroy(&d_mut);
     }
     MapCombo(const MapCombo &) = delete; 
     MapCombo & operator=(const MapCombo &) = delete;
 
     void reserve(size_t numberOfEntries);
 
-    pthread_rwlock_t d_mut;
+    ReadWriteLock d_mut;
     cmap_t d_map;
   };
 
index da3c8e337153e386f30219efe87c24e6bdd674d0..18553ed5082b68da79ece245d61eec04a35eecba 100644 (file)
@@ -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()
index 816b489673ddd05228c8fd71d22e22fc5a03b4ff..d5ff10eb653c148859a8736417ba521b34781e2a 100644 (file)
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+
+#include <set>
+#include <thread>
+#include <boost/utility.hpp>
+
 #include "packetcache.hh"
 #include "utility.hh"
 #include "communicator.hh"
-#include <set>
-#include <boost/utility.hpp>
 #include "dnsbackend.hh"
 #include "ueberbackend.hh"
 #include "packethandler.hh"
@@ -45,7 +48,7 @@ void CommunicatorClass::retrievalLoopThread(void)
     d_suck_sem.wait();
     SuckRequest sr;
     {
-      Lock l(&d_lock);
+      std::lock_guard<std::mutex> l(d_lock);
       if(d_suckdomains.empty()) 
         continue;
         
@@ -82,10 +85,13 @@ void CommunicatorClass::go()
     _exit(1);
   }
 
-  pthread_t tid;
-  pthread_create(&tid,0,&launchhelper,this); // Starts CommunicatorClass::mainloop()
-  for(int n=0; n < ::arg().asNum("retrieval-threads", 1); ++n)
-    pthread_create(&tid, 0, &retrieveLaunchhelper, this); // Starts CommunicatorClass::retrievalLoopThread()
+  std::thread mainT(std::bind(&CommunicatorClass::mainloop, this));
+  mainT.detach();
+
+  for(int n=0; n < ::arg().asNum("retrieval-threads", 1); ++n) {
+    std::thread retrieve(std::bind(&CommunicatorClass::retrievalLoopThread, this));
+    retrieve.detach();
+  }
 
   d_preventSelfNotification = ::arg().mustDo("prevent-self-notification");
 
@@ -131,7 +137,7 @@ void CommunicatorClass::mainloop(void)
           bool extraSlaveRefresh = false;
           Utility::sleep(1);
           {
-            Lock l(&d_lock);
+            std::lock_guard<std::mutex> l(d_lock);
             if (d_tocheck.size())
               extraSlaveRefresh = true;
           }
index 0c3d479b112ab69bf6ebeb9fc2276f387896314d..643145c34923767d1f1ed46020d29a43820fde4e 100644 (file)
@@ -149,9 +149,6 @@ class CommunicatorClass
 public:
   CommunicatorClass() 
   {
-    pthread_mutex_init(&d_lock,0);
-    pthread_mutex_init(&d_holelock,0);
-
     d_tickinterval=60;
     d_masterschanged=d_slaveschanged=true;
     d_nsock4 = -1;
@@ -171,17 +168,6 @@ public:
   void mainloop();
   void retrievalLoopThread();
   void sendNotification(int sock, const DNSName &domain, const ComboAddress& remote, uint16_t id, UeberBackend* B);
-
-  static void *launchhelper(void *p)
-  {
-    static_cast<CommunicatorClass *>(p)->mainloop();
-    return 0;
-  }
-  static void *retrieveLaunchhelper(void *p)
-  {
-    static_cast<CommunicatorClass *>(p)->retrievalLoopThread();
-    return 0;
-  }
   bool notifyDomain(const DNSName &domain, UeberBackend* B);
 private:
   void loadArgsIntoSet(const char *listname, set<string> &listset);
@@ -189,14 +175,14 @@ private:
   void queueNotifyDomain(const DomainInfo& di, UeberBackend* B);
   int d_nsock4, d_nsock6;
   map<pair<DNSName,string>,time_t>d_holes;
-  pthread_mutex_t d_holelock;
+  std::mutex d_holelock;
   void suck(const DNSName &domain, const ComboAddress& remote);
   void ixfrSuck(const DNSName &domain, const TSIGTriplet& tt, const ComboAddress& laddr, const ComboAddress& remote, std::unique_ptr<AuthLua4>& pdl,
                 ZoneStatus& zs, vector<DNSRecord>* axfr);
 
   void slaveRefresh(PacketHandler *P);
   void masterUpdateCheck(PacketHandler *P);
-  pthread_mutex_t d_lock;
+  std::mutex d_lock;
   
   UniQueue d_suckdomains;
   set<DNSName> d_inprogress;
@@ -232,7 +218,7 @@ private:
     ~RemoveSentinel()
     {
       try {
-        Lock l(&d_cc->d_lock);
+        std::lock_guard<std::mutex> l(d_cc->d_lock);
         d_cc->d_inprogress.erase(d_dn);
       }
       catch(...) {
index c2485628249d932353db032a9c54a126cd3dd490..65394273bd6e3115b4e1157fb881c6218d9d7cb8 100644 (file)
@@ -47,8 +47,8 @@ using namespace boost::assign;
 
 DNSSECKeeper::keycache_t DNSSECKeeper::s_keycache;
 DNSSECKeeper::metacache_t DNSSECKeeper::s_metacache;
-pthread_rwlock_t DNSSECKeeper::s_metacachelock = PTHREAD_RWLOCK_INITIALIZER;
-pthread_rwlock_t DNSSECKeeper::s_keycachelock = PTHREAD_RWLOCK_INITIALIZER;
+ReadWriteLock DNSSECKeeper::s_metacachelock;
+ReadWriteLock DNSSECKeeper::s_keycachelock;
 AtomicCounter DNSSECKeeper::s_ops;
 time_t DNSSECKeeper::s_last_prune;
 size_t DNSSECKeeper::s_maxEntries = 0;
@@ -276,7 +276,7 @@ void DNSSECKeeper::getSoaEdit(const DNSName& zname, std::string& value)
 uint64_t DNSSECKeeper::dbdnssecCacheSizes(const std::string& str)
 {
   if(str=="meta-cache-size") {
-    ReadLock l(&s_metacachelock); 
+    ReadLock l(&s_metacachelock);
     return s_metacache.size();
   }
   else if(str=="key-cache-size") {
index c025d897f5db4eecceb3bed22ec7406bd4d6cb40..38f08005ef58a2e61f45e658f64416fc87a9c758 100644 (file)
@@ -24,6 +24,7 @@
 #include <deque>
 #include <queue>
 #include <vector>
+#include <thread>
 #include <pthread.h>
 #include "threadname.hh"
 #include <unistd.h>
@@ -88,7 +89,7 @@ public:
   MultiThreadDistributor(int n);
   typedef std::function<void(std::unique_ptr<Answer>&)> callback_t;
   int question(Question&, callback_t callback) override; //!< Submit a question to the Distributor
-  static void* makeThread(void *); //!< helper function to create our n threads
+  void distribute(int n);
   int getQueueSize() override {
     return d_queued;
   }
@@ -114,7 +115,7 @@ private:
   time_t d_last_started;
   unsigned int d_overloadQueueLength, d_maxQueueLength;
   int d_num_threads;
-  std::atomic<unsigned int> d_queued{0}, d_running{0};
+  std::atomic<unsigned int> d_queued{0};
   std::vector<std::pair<int,int>> d_pipes;
 };
 
@@ -137,6 +138,10 @@ template<class Answer, class Question, class Backend>SingleThreadDistributor<Ans
     g_log<<Logger::Error<<"Distributor caught fatal exception: "<<AE.reason<<endl;
     _exit(1);
   }
+  catch(const std::exception& e) {
+    g_log<<Logger::Error<<"Distributor caught fatal exception: "<<e.what()<<endl;
+    _exit(1);
+  }
   catch(...) {
     g_log<<Logger::Error<<"Caught an unknown exception when creating backend, probably"<<endl;
     _exit(1);
@@ -151,9 +156,6 @@ template<class Answer, class Question, class Backend>MultiThreadDistributor<Answ
   nextid=0;
   d_last_started=time(0);
 
-  pthread_t tid;
-  
-
   for(int i=0; i < n; ++i) {
     int fds[2];
     if(pipe(fds) < 0)
@@ -168,7 +170,8 @@ template<class Answer, class Question, class Backend>MultiThreadDistributor<Answ
 
   g_log<<Logger::Warning<<"About to create "<<n<<" backend threads for UDP"<<endl;
   for(int i=0;i<n;i++) {
-    pthread_create(&tid,0,&makeThread,static_cast<void *>(this));
+    std::thread t(std::bind(&MultiThreadDistributor<Answer,Question,Backend>::distribute, this, i));
+    t.detach();
     Utility::usleep(50000); // we've overloaded mysql in the past :-)
   }
   g_log<<Logger::Warning<<"Done launching threads, ready to distribute questions"<<endl;
@@ -176,12 +179,9 @@ template<class Answer, class Question, class Backend>MultiThreadDistributor<Answ
 
 
 // start of a new thread
-template<class Answer, class Question, class Backend>void *MultiThreadDistributor<Answer,Question,Backend>::makeThread(void *p)
+template<class Answer, class Question, class Backend>void MultiThreadDistributor<Answer,Question,Backend>::distribute(int ournum)
 {
   setThreadName("pdns/distributo");
-  pthread_detach(pthread_self());
-  MultiThreadDistributor *us=static_cast<MultiThreadDistributor *>(p);
-  int ournum=us->d_running++;
 
   try {
     std::unique_ptr<Backend> b= make_unique<Backend>(); // this will answer our questions
@@ -190,9 +190,9 @@ template<class Answer, class Question, class Backend>void *MultiThreadDistributo
     for(;;) {
     
       QuestionData* tempQD = nullptr;
-      if(read(us->d_pipes[ournum].first, &tempQD, sizeof(tempQD)) != sizeof(tempQD))
+      if(read(d_pipes.at(ournum).first, &tempQD, sizeof(tempQD)) != sizeof(tempQD))
        unixDie("read");
-      --us->d_queued;
+      --d_queued;
       std::unique_ptr<QuestionData> QD = std::unique_ptr<QuestionData>(tempQD);
       tempQD = nullptr;
       std::unique_ptr<Answer> a = nullptr;
@@ -251,11 +251,14 @@ retry:
     g_log<<Logger::Error<<"Distributor caught fatal exception: "<<AE.reason<<endl;
     _exit(1);
   }
+  catch(const std::exception& e) {
+    g_log<<Logger::Error<<"Distributor caught fatal exception: "<<e.what()<<endl;
+    _exit(1);
+  }
   catch(...) {
     g_log<<Logger::Error<<"Caught an unknown exception when creating backend, probably"<<endl;
     _exit(1);
   }
-  return 0;
 }
 
 template<class Answer, class Question, class Backend>int SingleThreadDistributor<Answer,Question,Backend>::question(Question& q, callback_t callback)
@@ -312,7 +315,7 @@ template<class Answer, class Question, class Backend>int MultiThreadDistributor<
   QD->callback=callback;
 
   ++d_queued;
-  if(write(d_pipes[QD->id % d_pipes.size()].second, &QD, sizeof(QD)) != sizeof(QD)) {
+  if(write(d_pipes.at(QD->id % d_pipes.size()).second, &QD, sizeof(QD)) != sizeof(QD)) {
     --d_queued;
     delete QD;
     unixDie("write");
index 07fe9c6d42f4f443486437957658856395374334..5869df3be1ba45cb28a570e5b4c8de8df64646e2 100644 (file)
@@ -139,7 +139,7 @@ void BackendMakerClass::launch(const string &instr)
   vector<string> parts;
   stringtok(parts,instr,", ");
 
-  for (const auto part : parts)
+  for (const auto& part : parts)
     if (count(parts.begin(), parts.end(), part) > 1)
       throw ArgException("Refusing to launch multiple backends with the same name '" + part + "', verify all 'launch' statements in your configuration");
 
index db096659decac08d5b5c44a10d32b0f7b14c91f3..17eb99666fba4d7d13c309c92bf0f8bc41d1d8a3 100644 (file)
@@ -25,7 +25,6 @@
 #include "dolog.hh"
 #include "dnscrypt.hh"
 #include "dnswriter.hh"
-#include "lock.hh"
 
 DNSCryptPrivateKey::DNSCryptPrivateKey()
 {
@@ -125,20 +124,15 @@ DNSCryptQuery::~DNSCryptQuery()
 
 
 DNSCryptContext::~DNSCryptContext() {
-  pthread_rwlock_destroy(&d_lock);
 }
 
 DNSCryptContext::DNSCryptContext(const std::string& pName, const std::vector<CertKeyPaths>& certKeys): d_certKeyPaths(certKeys), providerName(pName)
 {
-  pthread_rwlock_init(&d_lock, 0);
-
   reloadCertificates();
 }
 
 DNSCryptContext::DNSCryptContext(const std::string& pName, const DNSCryptCert& certificate, const DNSCryptPrivateKey& pKey): providerName(pName)
 {
-  pthread_rwlock_init(&d_lock, 0);
-
   addNewCertificate(certificate, pKey);
 }
 
index 53b09f5fadf52c7b7b0baaed9a8ed4ab0ba2f44b..a010ebdc5a7a99bf64aed7ec37b5f02ef0222dae 100644 (file)
@@ -51,6 +51,7 @@ private:
 #include <sodium.h>
 
 #include "dnsname.hh"
+#include "lock.hh"
 
 #define DNSCRYPT_PROVIDER_PUBLIC_KEY_SIZE (crypto_sign_ed25519_PUBLICKEYBYTES)
 #define DNSCRYPT_PROVIDER_PRIVATE_KEY_SIZE (crypto_sign_ed25519_SECRETKEYBYTES)
@@ -285,7 +286,7 @@ private:
 
   void addNewCertificate(std::shared_ptr<DNSCryptCertificatePair>& newCert, bool reload=false);
 
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
   std::vector<std::shared_ptr<DNSCryptCertificatePair>> d_certs;
   std::vector<CertKeyPaths> d_certKeyPaths;
   DNSName providerName;
index 4b03ad4151aede5e3e748a6a47e90b36c79ce753..966f55cf50fa757ec58063a330078a43e5305c0e 100644 (file)
@@ -92,22 +92,18 @@ private:
   public:
     CacheShard(): d_entriesCount(0)
     {
-      pthread_rwlock_init(&d_lock, nullptr);
     }
     CacheShard(const CacheShard& old): d_entriesCount(0)
     {
-      pthread_rwlock_init(&d_lock, nullptr);
-    }
-    ~CacheShard() {
-      pthread_rwlock_destroy(&d_lock);
     }
+
     void setSize(size_t maxSize)
     {
       d_map.reserve(maxSize);
     }
 
     std::unordered_map<uint32_t,CacheValue> d_map;
-    pthread_rwlock_t d_lock;
+    ReadWriteLock d_lock;
     std::atomic<uint64_t> d_entriesCount;
   };
 
index 797a5730f432018f9146a0a6abf9c93357fe92ff..7347e448e507c27fc36ba9f1b51a729127b2d6bd 100644 (file)
@@ -500,7 +500,7 @@ static void connectionThread(int sock, ComboAddress remote)
         }
 
         // Latency histogram buckets
-        output << "# HELP dnsdist_latency Histogram of responses by latency\n";
+        output << "# HELP dnsdist_latency Histogram of responses by latency (in miliseconds)\n";
         output << "# TYPE dnsdist_latency histogram\n";
         uint64_t latency_amounts = g_stats.latency0_1;
         output << "dnsdist_latency_bucket{le=\"1\"} " << latency_amounts << "\n";
index 3785baffd89e398f576b92523f489da88f36a898..949e5a08c74086bbd5db85b483859dfaed3400da 100644 (file)
@@ -574,15 +574,13 @@ typedef std::function<std::tuple<bool, string>(const DNSQuestion* dq)> QueryCoun
 struct QueryCount {
   QueryCount()
   {
-    pthread_rwlock_init(&queryLock, nullptr);
   }
   ~QueryCount()
   {
-    pthread_rwlock_destroy(&queryLock);
   }
   QueryCountRecords records;
   QueryCountFilter filter;
-  pthread_rwlock_t queryLock;
+  ReadWriteLock queryLock;
   bool enabled{false};
 };
 
@@ -771,11 +769,10 @@ struct DownstreamState
         fd = -1;
       }
     }
-    pthread_rwlock_destroy(&d_lock);
   }
   boost::uuids::uuid id;
   std::vector<unsigned int> hashes;
-  mutable pthread_rwlock_t d_lock;
+  mutable ReadWriteLock d_lock;
   std::vector<int> sockets;
   const std::string sourceItfName;
   std::mutex socketsLock;
@@ -913,11 +910,9 @@ struct ServerPool
 {
   ServerPool()
   {
-    pthread_rwlock_init(&d_lock, nullptr);
   }
   ~ServerPool()
   {
-    pthread_rwlock_destroy(&d_lock);
   }
 
   const std::shared_ptr<DNSDistPacketCache> getCache() const { return packetCache; };
@@ -997,7 +992,7 @@ struct ServerPool
 
 private:
   ServerPolicy::NumberedServerVector d_servers;
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
   bool d_useECS{false};
 };
 
index fbce5dd708350464b63941ef0963cb68ab406d6a..2f7a7dbff822b531cbfd43495f434561d75a0c64 100644 (file)
@@ -137,7 +137,6 @@ void DownstreamState::setWeight(int newWeight)
 
 DownstreamState::DownstreamState(const ComboAddress& remote_, const ComboAddress& sourceAddr_, unsigned int sourceItf_, const std::string& sourceItfName_, size_t numberOfSockets, bool connect=true): sourceItfName(sourceItfName_), remote(remote_), sourceAddr(sourceAddr_), sourceItf(sourceItf_), name(remote_.toStringWithPort()), nameWithAddr(remote_.toStringWithPort())
 {
-  pthread_rwlock_init(&d_lock, nullptr);
   id = getUniqueID();
   threadStarted.clear();
 
index e4cc36b506087f457e72ce4c7be53473313600e2..090a17c2e2e3e95745dee345f210d9ddb1ba21bf 100644 (file)
@@ -117,7 +117,6 @@ bool LMDBKVStore::keyExists(const std::string& key)
 
 CDBKVStore::CDBKVStore(const std::string& fname, time_t refreshDelay): d_fname(fname), d_refreshDelay(refreshDelay)
 {
-  pthread_rwlock_init(&d_lock, nullptr);
   d_refreshing.clear();
 
   time_t now = time(nullptr);
@@ -129,7 +128,6 @@ CDBKVStore::CDBKVStore(const std::string& fname, time_t refreshDelay): d_fname(f
 }
 
 CDBKVStore::~CDBKVStore() {
-  pthread_rwlock_destroy(&d_lock);
 }
 
 bool CDBKVStore::reload(const struct stat& st)
index 63e18fdf5e507e94be14b45bfd79db8911fc190d..997de05a2b086e15358da49c4fba01f9fd29af9f 100644 (file)
@@ -193,7 +193,7 @@ private:
 
   std::unique_ptr<CDB> d_cdb{nullptr};
   std::string d_fname;
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
   time_t d_mtime{0};
   time_t d_nextCheck{0};
   time_t d_refreshDelay{0};
index 0474a1e1504b8d3c9acee069d7ee8426b40141ee..202e0b055364ca3f589fa2a2c5eeb5cf9a4f0e4f 100644 (file)
@@ -234,13 +234,9 @@ private:
 public:
   TimedIPSetRule()
   {
-    pthread_rwlock_init(&d_lock4, 0);
-    pthread_rwlock_init(&d_lock6, 0);
   }
   ~TimedIPSetRule()
   {
-    pthread_rwlock_destroy(&d_lock4);
-    pthread_rwlock_destroy(&d_lock6);
   }
   bool matches(const DNSQuestion* dq) const override
   {
@@ -302,7 +298,7 @@ public:
 
   void cleanup()
   {
-    time_t now=time(0);
+    time_t now = time(nullptr);
     {
       WriteLock rl(&d_lock4);
 
@@ -360,8 +356,8 @@ private:
   };
   std::unordered_map<IPv6, time_t, IPv6Hash> d_ip6s;
   std::unordered_map<uint32_t, time_t> d_ip4s;
-  mutable pthread_rwlock_t d_lock4;
-  mutable pthread_rwlock_t d_lock6;
+  mutable ReadWriteLock d_lock4;
+  mutable ReadWriteLock d_lock6;
 };
 
 
index 064c1b46d642ca00539158f4d39ab55ebdfaebc4..fa58e39eec68e0e3491d2f053eaf8b22a460f49a 100644 (file)
@@ -7,6 +7,7 @@
 #include <atomic>
 #include <fstream>
 #include <cstring>
+#include <mutex>
 #include <pthread.h>
 
 #include <openssl/conf.h>
 
 #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{nullptr};
+
+#include "lock.hh"
+static std::vector<std::mutex> openssllocks;
 
 extern "C" {
 static 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]));
+    openssllocks.at(type).unlock();
   }
 }
 
@@ -42,24 +45,15 @@ static unsigned long openssl_pthreads_id_callback()
 
 static 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);
 }
 
 static 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();
 }
 
 #endif /* (OPENSSL_VERSION_NUMBER < 0x1010000fL || (defined LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2090100fL) */
@@ -487,13 +481,11 @@ bool libssl_set_min_tls_version(std::unique_ptr<SSL_CTX, void(*)(SSL_CTX*)>& ctx
 
 OpenSSLTLSTicketKeysRing::OpenSSLTLSTicketKeysRing(size_t capacity)
 {
-  pthread_rwlock_init(&d_lock, nullptr);
   d_ticketKeys.set_capacity(capacity);
 }
 
 OpenSSLTLSTicketKeysRing::~OpenSSLTLSTicketKeysRing()
 {
-  pthread_rwlock_destroy(&d_lock);
 }
 
 void OpenSSLTLSTicketKeysRing::addKey(std::shared_ptr<OpenSSLTLSTicketKey> newKey)
index 9823ae569c35f20439c9a5c1aa5988c6641b3a23..b4ea4c96779b7be966dc7ef5e60b7831ec3424a1 100644 (file)
@@ -791,8 +791,6 @@ public:
       throw std::runtime_error("Error setting up TLS cipher preferences to '" + fe.d_tlsConfig.d_ciphers + "' (" + gnutls_strerror(rc) + ") on " + fe.d_addr.toStringWithPort());
     }
 
-    pthread_rwlock_init(&d_lock, nullptr);
-
     try {
       if (fe.d_tlsConfig.d_ticketKeyFile.empty()) {
         handleTicketsKeyRotation(time(nullptr));
@@ -802,15 +800,12 @@ public:
       }
     }
     catch(const std::runtime_error& e) {
-      pthread_rwlock_destroy(&d_lock);
       throw std::runtime_error("Error generating tickets key for TLS context on " + fe.d_addr.toStringWithPort() + ": " + e.what());
     }
   }
 
   virtual ~GnuTLSIOCtx() override
   {
-    pthread_rwlock_destroy(&d_lock);
-
     d_creds.reset();
 
     if (d_priorityCache) {
@@ -876,7 +871,7 @@ private:
   std::unique_ptr<gnutls_certificate_credentials_st, void(*)(gnutls_certificate_credentials_t)> d_creds;
   gnutls_priority_t d_priorityCache{nullptr};
   std::shared_ptr<GnuTLSTicketsKey> d_ticketsKey{nullptr};
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
   bool d_enableTickets{true};
 };
 
index decd631e4f5fa63d22909e53d5450d68cecc94d4..e2dd8e82034187921c2570ec1cb716c1de6d8295 100644 (file)
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
+
+#include <sys/types.h>
+#include <thread>
+
 #include "packetcache.hh"
 #include "utility.hh"
 #include "dnsproxy.hh"
 #include "pdnsexception.hh"
-#include <sys/types.h>
 #include "dns.hh"
 #include "logger.hh"
 #include "statbag.hh"
@@ -39,7 +42,6 @@ extern StatBag S;
 
 DNSProxy::DNSProxy(const string &remote)
 {
-  pthread_mutex_init(&d_lock,0);
   d_resanswers=S.getPointer("recursing-answers");
   d_resquestions=S.getPointer("recursing-questions");
   d_udpanswers=S.getPointer("udp-answers");
@@ -83,8 +85,8 @@ DNSProxy::DNSProxy(const string &remote)
 
 void DNSProxy::go()
 {
-  pthread_t tid;
-  pthread_create(&tid,0,&launchhelper,this);
+  std::thread t(std::bind(&DNSProxy::mainloop, this));
+  t.detach();
 }
 
 //! look up qname target with r->qtype, plonk it in the answer section of 'r' with name aname
@@ -129,7 +131,7 @@ bool DNSProxy::completePacket(std::unique_ptr<DNSPacket>& r, const DNSName& targ
   uint16_t id;
   uint16_t qtype = r->qtype.getCode();
   {
-    Lock l(&d_lock);
+    std::lock_guard<std::mutex> l(d_lock);
     id=getID_locked();
 
     ConntrackEntry ce;
@@ -216,7 +218,7 @@ void DNSProxy::mainloop(void)
       dnsheader d;
       memcpy(&d,buffer,sizeof(d));
       {
-        Lock l(&d_lock);
+        std::lock_guard<std::mutex> l(d_lock);
 #if BYTE_ORDER == BIG_ENDIAN
         // this is needed because spoof ID down below does not respect the native byteorder
         d.id = ( 256 * (uint16_t)buffer[1] ) + (uint16_t)buffer[0];  
index e9b5807af49832b362573137820fd20671100352..0adc07af9a4f769044fd2141b2adb58abcece9e2 100644 (file)
@@ -20,8 +20,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #pragma once
-#include <pthread.h>
 #include <map>
+#include <mutex>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -56,11 +56,6 @@ public:
   bool completePacket(std::unique_ptr<DNSPacket>& r, const DNSName& target,const DNSName& aname, const uint8_t scopeMask);
 
   void mainloop();                  //!< this is the main loop that receives reply packets and sends them out again
-  static void *launchhelper(void *p)
-  {
-    static_cast<DNSProxy *>(p)->mainloop();
-    return 0;
-  }
   bool recurseFor(DNSPacket* p);
 private:
   struct ConntrackEntry
@@ -84,7 +79,7 @@ private:
   AtomicCounter* d_resanswers;
   AtomicCounter* d_udpanswers;
   AtomicCounter* d_resquestions;
-  pthread_mutex_t d_lock;
+  std::mutex d_lock;
   map_t d_conntrack;
   int d_sock;
   int getID_locked();
index c2fa1c0ab631b72475da626931492002d9d93ac6..5ec297b0ffb439f07a6540ca30977f38e9b977ce 100644 (file)
@@ -33,6 +33,7 @@
 #include "dnssecinfra.hh"
 #include "dnsrecords.hh"
 #include "ueberbackend.hh"
+#include "lock.hh"
 
 using namespace ::boost::multi_index;
 
@@ -298,8 +299,8 @@ private:
 
   static keycache_t s_keycache;
   static metacache_t s_metacache;
-  static pthread_rwlock_t s_metacachelock;
-  static pthread_rwlock_t s_keycachelock;
+  static ReadWriteLock s_metacachelock;
+  static ReadWriteLock s_keycachelock;
   static AtomicCounter s_ops;
   static time_t s_last_prune;
   static size_t s_maxEntries;
index 7e6f485ee9fa269acfbab56353d7cb06cfd37577..3bb27930c8b183ac4c408ab731ea5b54825f27e0 100644 (file)
@@ -33,7 +33,7 @@
 #include "statbag.hh"
 extern StatBag S;
 
-static pthread_rwlock_t g_signatures_lock = PTHREAD_RWLOCK_INITIALIZER;
+static ReadWriteLock g_signatures_lock;
 typedef map<pair<string, string>, string> signaturecache_t;
 static signaturecache_t g_signatures;
 static int g_cacheweekno;
index 922c05ff226e8471856e0472b192ebd08a07c6db..4f36ff2e995442c33ea0cc8b3a46a7738230136b 100644 (file)
@@ -28,6 +28,8 @@
 
 #include <boost/accumulators/statistics.hpp>
 
+#include <thread>
+
 #include "dnsparser.hh"
 #include "sstuff.hh"
 #include "misc.hh"
@@ -172,7 +174,7 @@ AtomicCounter g_pos;
 
 vector<BenchQuery> g_queries;
 
-static void* worker(void*)
+static void worker()
 {
   setThreadName("dnstcpb/worker");
   for(;;) {
@@ -182,7 +184,6 @@ static void* worker(void*)
 
     doQuery(&g_queries[pos]); // this is safe as long as nobody *inserts* to g_queries
   }
-  return 0;
 }
 
 static void usage(po::options_description &desc) {
@@ -251,7 +252,8 @@ try
   }
 
 
-  std::vector<pthread_t> workers(numworkers);
+  std::vector<std::thread> workers;
+  workers.reserve(numworkers);
 
   FILE* fp;
   if(!g_vm.count("file"))
@@ -270,12 +272,11 @@ try
   }
   fclose(fp);
     
-  for(unsigned int n = 0; n < numworkers; ++n) {
-    pthread_create(&workers[n], 0, worker, 0);
+  for (unsigned int n = 0; n < numworkers; ++n) {
+    workers.push_back(std::thread(worker));
   }
-  for(unsigned int n = 0; n < numworkers; ++n) {
-    void* status;
-    pthread_join(workers[n], &status);
+  for (auto& w : workers) {
+    w.join();
   }
   
   using namespace boost::accumulators;
index 2deb2182b928a970e842e1b917cbd330792b03c3..ab10d114dadc0adb4ced1f138a6e11e8cc4364c0 100644 (file)
@@ -28,7 +28,6 @@
 #include <sys/types.h>
 #include <sys/un.h>
 #include <dlfcn.h>
-#include <pthread.h>
 #include <unistd.h>
 #include <boost/algorithm/string.hpp>
 
@@ -43,6 +42,8 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <boost/algorithm/string.hpp> 
+#include <thread>
+
 #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<DynListener *>(p);
-  us->theListener();
-  g_log<<Logger::Error<<"Control listener aborted, please file a bug!"<<endl;
-  return 0;
+  std::thread listener(std::bind(&DynListener::theListener,this));
+  listener.detach();
 }
 
 string DynListener::getLine()
@@ -331,6 +324,8 @@ void DynListener::registerRestFunc(g_funk_t *gf)
 
 void DynListener::theListener()
 {
+  setThreadName("pdns/ctrlListen");
+
   try {
     signal(SIGPIPE,SIG_IGN);
 
index da61661ccb4c8bbfa8852137b26383211e05bff4..cbe45c47b53ab1f03d6a69a7dcc5216597bd78b2 100644 (file)
@@ -22,7 +22,6 @@
 #pragma once
 #include <string>
 #include <vector>
-#include <pthread.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <iostream>
@@ -45,7 +44,6 @@ public:
   ~DynListener();
   void go();
   void theListener();
-  static void *theListenerHelper(void *p);
 
   typedef string g_funk_t(const vector<string> &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};
index 087be580a5d35091d1822a56fd35ce84b76b8fb3..2de0102ac21b383648b6a80eeb3170cc24df0229 100644 (file)
@@ -626,15 +626,15 @@ void DNSFilterEngine::Zone::dump(FILE* fp) const
     dumpNamedPolicy(fp, pair.first + DNSName("rpz-nsdname.") + d_domain, pair.second);
   }
 
-  for (const auto pair : d_qpolAddr) {
+  for (const auto& pair : d_qpolAddr) {
     dumpAddrPolicy(fp, pair.first, DNSName("rpz-client-ip.") + d_domain, pair.second);
   }
 
-  for (const auto pair : d_propolNSAddr) {
+  for (const auto& pair : d_propolNSAddr) {
     dumpAddrPolicy(fp, pair.first, DNSName("rpz-nsip.") + d_domain, pair.second);
   }
 
-  for (const auto pair : d_postpolAddr) {
+  for (const auto& pair : d_postpolAddr) {
     dumpAddrPolicy(fp, pair.first, DNSName("rpz-ip.") + d_domain, pair.second);
   }
 }
index 2fb2805055597cbbd5686c4fbf70d4568a65c835..968864100abab752fb406af488c02206d5838b51 100644 (file)
@@ -989,7 +989,7 @@ public:
 
     // we turn left on 0 and right on 1
     int bits = 0;
-    for(; node && bits < key.getBits(); bits++) {
+    for(; bits < key.getBits(); bits++) {
       bool vall = key.getBit(-1-bits);
 
       if (bits >= node->d_bits) {
index b113592555c6a3c5856c73a24de685c743cadf7a..bdca6eaa140b82d40a4969a6f3a05b84aae0fedf 100644 (file)
@@ -94,7 +94,7 @@ public:
 
 private:
   boost::circular_buffer<std::shared_ptr<OpenSSLTLSTicketKey> > d_ticketKeys;
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
 };
 
 void* libssl_get_ticket_key_callback_data(SSL* s);
index 7e9a11547b57cd2f14b848e567b77b522421ba7c..5cbc4404d7dc05ede533b1d529b00839cf28fb2d 100644 (file)
 #include "misc.hh"
 #include "pdnsexception.hh"
 
-extern bool g_singleThreaded;
-
-class Lock
+class ReadWriteLock
 {
-  pthread_mutex_t *d_lock;
 public:
-  Lock(const Lock& rhs) = delete;
-  Lock& operator=(const Lock& rhs) = delete;
-
-  Lock(pthread_mutex_t *lock) : d_lock(lock)
+  ReadWriteLock()
   {
-    if(g_singleThreaded)
-      return;
-
-    int err;
-    if((err = pthread_mutex_lock(d_lock))) {
-      errno = err;
-      throw PDNSException("error acquiring lock: "+stringerror());
+    if (pthread_rwlock_init(&d_lock, nullptr) != 0) {
+      throw std::runtime_error("Error creating a read-write lock: " + stringerror());
     }
   }
-  ~Lock()
-  {
-    if(g_singleThreaded)
-      return;
 
-    pthread_mutex_unlock(d_lock);
+  ~ReadWriteLock() {
+    /* might have been moved */
+    pthread_rwlock_destroy(&d_lock);
   }
+
+  ReadWriteLock(const ReadWriteLock& rhs) = delete;
+  ReadWriteLock& operator=(const ReadWriteLock& rhs) = delete;
+
+  pthread_rwlock_t* getLock()
+  {
+    return &d_lock;
+  }
+
+private:
+  pthread_rwlock_t d_lock;
 };
 
-class WriteLock
+class ReadLock
 {
-  pthread_rwlock_t *d_lock;
 public:
+  ReadLock(ReadWriteLock& lock): ReadLock(lock.getLock())
+  {
+  }
 
-  WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
+  ReadLock(ReadWriteLock* lock): ReadLock(lock->getLock())
   {
-    if(g_singleThreaded)
-      return;
+  }
 
+  ~ReadLock()
+  {
+    if(d_lock) // may have been moved
+      pthread_rwlock_unlock(d_lock);
+  }
+
+  ReadLock(ReadLock&& rhs)
+  {
+    d_lock = rhs.d_lock;
+    rhs.d_lock = nullptr;
+  }
+  ReadLock(const ReadLock& rhs) = delete;
+  ReadLock& operator=(const ReadLock& rhs) = delete;
+
+private:
+  ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
+  {
     int err;
-    if((err = pthread_rwlock_wrlock(d_lock))) {
-      throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
+    if((err = pthread_rwlock_rdlock(d_lock))) {
+      throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
     }
   }
-  ~WriteLock()
+
+ pthread_rwlock_t *d_lock;
+};
+
+class WriteLock
+{
+public:
+  WriteLock(ReadWriteLock& lock): WriteLock(lock.getLock())
+  {
+  }
+
+  WriteLock(ReadWriteLock* lock): WriteLock(lock->getLock())
   {
-    if(g_singleThreaded)
-      return;
-    if(d_lock) // might have been moved
-      pthread_rwlock_unlock(d_lock);
   }
 
   WriteLock(WriteLock&& rhs)
@@ -82,35 +105,40 @@ public:
     d_lock = rhs.d_lock;
     rhs.d_lock=0;
   }
+
+  ~WriteLock()
+  {
+    if(d_lock) // might have been moved
+      pthread_rwlock_unlock(d_lock);
+  }
+
   WriteLock(const WriteLock& rhs) = delete;
   WriteLock& operator=(const WriteLock& rhs) = delete;
 
+private:
+  WriteLock(pthread_rwlock_t *lock) : d_lock(lock)
+  {
+    int err;
+    if((err = pthread_rwlock_wrlock(d_lock))) {
+      throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
+    }
+  }
+
+  pthread_rwlock_t *d_lock;
 };
 
-class TryWriteLock
+class TryReadLock
 {
-  pthread_rwlock_t *d_lock;
-  bool d_havelock;
 public:
-  TryWriteLock(const TryWriteLock& rhs) = delete;
-  TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
-
-  TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
+  TryReadLock(ReadWriteLock& lock): TryReadLock(lock.getLock())
   {
-    if(g_singleThreaded) {
-      d_havelock=true;
-      return;
-    }
+  }
 
-    d_havelock=false;
-    int err;
-    if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
-      throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
-    }
-    d_havelock=(err==0);
+  TryReadLock(ReadWriteLock* lock): TryReadLock(lock->getLock())
+  {
   }
 
-  TryWriteLock(TryWriteLock&& rhs)
+  TryReadLock(TryReadLock&& rhs)
   {
     d_lock = rhs.d_lock;
     rhs.d_lock = nullptr;
@@ -118,46 +146,46 @@ public:
     rhs.d_havelock = false;
   }
 
-  
-  ~TryWriteLock()
+  ~TryReadLock()
   {
-    if(g_singleThreaded)
-      return;
-
-    if(d_havelock && d_lock) // we might be moved
+    if(d_havelock && d_lock)
       pthread_rwlock_unlock(d_lock);
   }
+
+  TryReadLock(const TryReadLock& rhs) = delete;
+  TryReadLock& operator=(const TryReadLock& rhs) = delete;
+
   bool gotIt()
   {
-    if(g_singleThreaded)
-      return true;
-
     return d_havelock;
   }
-};
-
-class TryReadLock
-{
-  pthread_rwlock_t *d_lock;
-  bool d_havelock;
-public:
-  TryReadLock(const TryReadLock& rhs) = delete;
-  TryReadLock& operator=(const TryReadLock& rhs) = delete;
 
+private:
   TryReadLock(pthread_rwlock_t *lock) : d_lock(lock)
   {
-    if(g_singleThreaded) {
-      d_havelock=true;
-      return;
-    }
-
     int err;
     if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
       throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
     }
     d_havelock=(err==0);
   }
-  TryReadLock(TryReadLock&& rhs)
+
+  pthread_rwlock_t *d_lock;
+  bool d_havelock;
+};
+
+class TryWriteLock
+{
+public:
+  TryWriteLock(ReadWriteLock& lock): TryWriteLock(lock.getLock())
+  {
+  }
+
+  TryWriteLock(ReadWriteLock* lock): TryWriteLock(lock->getLock())
+  {
+  }
+
+  TryWriteLock(TryWriteLock&& rhs)
   {
     d_lock = rhs.d_lock;
     rhs.d_lock = nullptr;
@@ -165,52 +193,32 @@ public:
     rhs.d_havelock = false;
   }
 
-  ~TryReadLock()
+  ~TryWriteLock()
   {
-    if(g_singleThreaded)
-      return;
-
-    if(d_havelock && d_lock)
+    if(d_havelock && d_lock) // we might be moved
       pthread_rwlock_unlock(d_lock);
   }
+
+  TryWriteLock(const TryWriteLock& rhs) = delete;
+  TryWriteLock& operator=(const TryWriteLock& rhs) = delete;
+
   bool gotIt()
   {
-    if(g_singleThreaded)
-      return true;
-
     return d_havelock;
   }
-};
 
-
-class ReadLock
-{
-  pthread_rwlock_t *d_lock;
-public:
-
-  ReadLock(pthread_rwlock_t *lock) : d_lock(lock)
+private:
+  TryWriteLock(pthread_rwlock_t *lock) : d_lock(lock)
   {
-    if(g_singleThreaded)
-      return;
-
+    d_havelock=false;
     int err;
-    if((err = pthread_rwlock_rdlock(d_lock))) {
-      throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
+    if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
+      throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
     }
-  }
-  ~ReadLock()
-  {
-    if(g_singleThreaded)
-      return;
-    if(d_lock) // may have been moved
-      pthread_rwlock_unlock(d_lock);
+    d_havelock=(err==0);
   }
 
-  ReadLock(ReadLock&& rhs)
-  {
-    d_lock = rhs.d_lock;
-    rhs.d_lock=0;
-  }
-  ReadLock(const ReadLock& rhs) = delete;
-  ReadLock& operator=(const ReadLock& rhs) = delete;
+  pthread_rwlock_t *d_lock;
+  bool d_havelock;
 };
+
index ad64c3108456251174287214fb91098c79933cba..6804b3997959e6b72eff16601cda998f0a48cb1b 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;
@@ -48,7 +50,7 @@ Logger& getLogger()
   return log;
 }
 
-void Logger::log(const string &msg, Urgency u)
+void Logger::log(const string &msg, Urgency u) noexcept
 {
 #ifndef RECURSOR
   bool mustAccount(false);
@@ -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;
@@ -111,8 +113,14 @@ void Logger::log(const string &msg, Urgency u)
   }
 
 #ifndef RECURSOR
-  if(mustAccount)
-    S.ringAccount("logmessages",msg);
+  if(mustAccount) {
+      try {
+        S.ringAccount("logmessages",msg);
+      }
+      catch (const runtime_error& e) {
+        cerr << e.what() << endl;
+      }
+  }
 #endif
 }
 
index 38689e2d5a88b16c51141cd84640c671b9110175..e8c4da7019f769c779d354f0d8377f87e5c18666 100644 (file)
@@ -45,7 +45,7 @@ public:
       \param msg Message you wish to log
       \param u Urgency of the message you wish to log
   */
-  void log(const string &msg, Urgency u=Notice);
+  void log(const string &msg, Urgency u=Notice) noexcept;
 
   void setFacility(int f){d_facility=f;open();} //!< Choose logging facility
   void setFlag(int f){flags|=f;open();} //!< set a syslog flag
index ea43dcef1cddab2385f55da6896d1aabe397d314..50bbf48e7422c2fb9a5c2bdf5a185c7005d7e175 100644 (file)
@@ -71,15 +71,13 @@ private:
     std::atomic<time_t> lastAccess{0};
   };
 
-  pthread_rwlock_t d_lock;
+  ReadWriteLock d_lock;
 public:
   IsUpOracle()
   {
-    pthread_rwlock_init(&d_lock, nullptr);
   }
   ~IsUpOracle()
   {
-    pthread_rwlock_destroy(&d_lock);
   }
   bool isUp(const ComboAddress& remote, const opts_t& opts);
   bool isUp(const ComboAddress& remote, const std::string& url, const opts_t& opts);
index 4219e1d356bd0a374ef1e665e4029e232418fd09..7693cec963710379955e84c3ffa791527c24272f 100644 (file)
@@ -280,13 +280,13 @@ void CommunicatorClass::sendNotification(int sock, const DNSName& domain, const
 
 void CommunicatorClass::drillHole(const DNSName &domain, const string &ip)
 {
-  Lock l(&d_holelock);
+  std::lock_guard<std::mutex> l(d_holelock);
   d_holes[make_pair(domain,ip)]=time(0);
 }
 
 bool CommunicatorClass::justNotified(const DNSName &domain, const string &ip)
 {
-  Lock l(&d_holelock);
+  std::lock_guard<std::mutex> l(d_holelock);
   if(d_holes.find(make_pair(domain,ip))==d_holes.end()) // no hole
     return false;
 
index a9839a229272709f7db8319d8b29a60f4340b0da..fb253e5f1c40d7e5563e36b827c2f201818c664b 100644 (file)
@@ -65,8 +65,6 @@
 #  include <sched.h>
 #endif
 
-bool g_singleThreaded;
-
 size_t writen2(int fd, const void *buf, size_t count)
 {
   const char *ptr = (char*)buf;
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 cd41c16259b19d6d8e659601d23f31b856854474..b196d984da35659819161ac071367ec351983bbd 100644 (file)
@@ -102,7 +102,7 @@ private:
   void tkeyHandler(const DNSPacket& p, std::unique_ptr<DNSPacket>& r); //<! process TKEY record, and adds TKEY record to (r)eply, or error code.
 
   static AtomicCounter s_count;
-  static pthread_mutex_t s_rfc2136lock;
+  static std::mutex s_rfc2136lock;
   bool d_logDNSDetails;
   bool d_doIPv6AdditionalProcessing;
   bool d_doDNAME;
index 406f5b074df398429ba1febd1e9ea984af8d29ac..fd5bcccfe91e13065d3e3e6284bf17e3b7bd0be4 100644 (file)
@@ -782,7 +782,7 @@ static int listKeys(const string &zname, DNSSECKeeper& dk){
     vector<DomainInfo> domainInfo;
     B.getAllDomains(&domainInfo);
     bool printHeader = true;
-    for (auto const di : domainInfo) {
+    for (const auto& di : domainInfo) {
       listKey(di, dk, printHeader);
       printHeader = false;
     }
@@ -1595,7 +1595,7 @@ static bool showZone(DNSSECKeeper& dk, const DNSName& zone, bool exportDS = fals
       cout<<endl;
 
       for(const auto& m : metamap) {
-        for(const auto i : m.second)
+        for(const auto& i : m.second)
           cout << '\t' << m.first<<'\t' << i <<endl;
       }
     }
@@ -2133,7 +2133,7 @@ try
     cout<<"DNSKEY algorithms supported by this installation of PowerDNS:"<<endl;
 
     auto algosWithBackend = DNSCryptoKeyEngine::listAllAlgosWithBackend();
-    for (auto const algoWithBackend : algosWithBackend){
+    for (const auto& algoWithBackend : algosWithBackend){
       string algoName = DNSSECKeeper::algorithm2name(algoWithBackend.first);
       cout<<std::to_string(algoWithBackend.first)<<" - "<<algoName;
       if (cmds.size() == 2 && cmds[1] == "with-backend")
@@ -3089,7 +3089,7 @@ try
     if (cmds.size() > 2) {
       keys.assign(cmds.begin() + 2, cmds.end());
       std::cout << "Metadata for '" << zone << "'" << endl;
-      for(const string kind :  keys) {
+      for(const auto& kind :  keys) {
         vector<string> meta;
         meta.clear();
         if (B.getDomainMetadata(zone, kind, meta)) {
index c999a573934de6c0d40acba6c401713b9c5ca8d6..ddb1506492f606845111878a724df3929885b070 100644 (file)
@@ -8,6 +8,8 @@
 #include <boost/format.hpp>
 #include <p11-kit/p11-kit.h>
 
+#include <mutex>
+
 #include "pdns/dnssecinfra.hh"
 #include "pdns/logger.hh"
 #include "pdns/pdnsexception.hh"
@@ -206,7 +208,7 @@ class Pkcs11Slot {
     CK_SESSION_HANDLE d_session;
     CK_SLOT_ID d_slot;
     CK_RV d_err;
-    pthread_mutex_t d_m;
+    std::mutex d_m;
 
     void logError(const std::string& operation) const {
       if (d_err) {
@@ -223,8 +225,7 @@ class Pkcs11Slot {
     d_err(0)
   {
       CK_TOKEN_INFO tokenInfo;
-      pthread_mutex_init(&(this->d_m), NULL);
-      Lock l(&d_m);
+      std::lock_guard<std::mutex> l(d_m);
 
       if ((d_err = d_functions->C_OpenSession(this->d_slot, CKF_SERIAL_SESSION|CKF_RW_SESSION, 0, 0, &(this->d_session)))) {
         logError("C_OpenSession");
@@ -261,7 +262,7 @@ class Pkcs11Slot {
 
     CK_FUNCTION_LIST* f() { return d_functions; }
 
-    pthread_mutex_t *m() { return &d_m; }
+    std::mutex& m() { return d_m; }
 
     static std::shared_ptr<Pkcs11Slot> GetSlot(const std::string& module, const string& tokenId);
     static CK_RV HuntSlot(const string& tokenId, CK_SLOT_ID &slotId, _CK_SLOT_INFO* info, CK_FUNCTION_LIST* functions);
@@ -342,7 +343,7 @@ class Pkcs11Token {
     }
 
     void LoadAttributes() {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
       std::vector<P11KitAttribute> attr;
       std::vector<CK_OBJECT_HANDLE> key;
       attr.push_back(P11KitAttribute(CKA_CLASS, (unsigned long)CKO_PRIVATE_KEY));
@@ -407,7 +408,7 @@ class Pkcs11Token {
 
     int GenerateKeyPair(CK_MECHANISM_PTR mechanism, std::vector<P11KitAttribute>& pubAttributes, std::vector<P11KitAttribute>& privAttributes, CK_OBJECT_HANDLE_PTR pubKey, CK_OBJECT_HANDLE_PTR privKey) {
       {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
 
       size_t k;
       std::unique_ptr<CK_ATTRIBUTE[]> pubAttr(new CK_ATTRIBUTE[pubAttributes.size()]);
@@ -435,7 +436,7 @@ class Pkcs11Token {
     }
 
     int Sign(const std::string& data, std::string& result, CK_MECHANISM_PTR mechanism) {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
 
       CK_BYTE buffer[1024];
       CK_ULONG buflen = sizeof buffer; // should be enough for most signatures.
@@ -454,7 +455,7 @@ class Pkcs11Token {
     }
 
     int Verify(const std::string& data, const std::string& signature, CK_MECHANISM_PTR mechanism) {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
 
       if ((d_err = this->d_slot->f()->C_VerifyInit(d_slot->Session(), mechanism, d_public_key))) { logError("C_VerifyInit"); return d_err; }
       d_err = this->d_slot->f()->C_Verify(d_slot->Session(), (unsigned char*)data.c_str(), data.size(), (unsigned char*)signature.c_str(), signature.size());
@@ -463,7 +464,7 @@ class Pkcs11Token {
     }
 
     int Digest(const std::string& data, std::string& result, CK_MECHANISM_PTR mechanism) {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
 
       CK_BYTE buffer[1024];
       CK_ULONG buflen = sizeof buffer; // should be enough for most digests
@@ -490,7 +491,7 @@ class Pkcs11Token {
     }
 
     int DigestKey(std::string& result) {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
       CK_MECHANISM mech;
       mech.mechanism = CKM_SHA_1;
 
@@ -521,7 +522,7 @@ class Pkcs11Token {
     }
 
     int FindObjects(const std::vector<P11KitAttribute>& attributes, std::vector<CK_OBJECT_HANDLE>& objects, int maxobjects) {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
       return FindObjects2(attributes, objects, maxobjects);
     }
 
@@ -567,7 +568,7 @@ class Pkcs11Token {
 
     int GetAttributeValue(const CK_OBJECT_HANDLE& object, std::vector<P11KitAttribute>& attributes) 
     {
-      Lock l(d_slot->m());
+      std::lock_guard<std::mutex> l(d_slot->m());
       return GetAttributeValue2(object, attributes);
     }
 
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";
index 42da510c58e50019997c2473046af64c6c4cfcc1..da65e8bf2591a9e3a5aaac613ec280a751b8c8a1 100644 (file)
@@ -154,7 +154,7 @@ static void writePid(void)
 
 int g_fd1[2], g_fd2[2];
 FILE *g_fp;
-pthread_mutex_t g_guardian_lock = PTHREAD_MUTEX_INITIALIZER;
+std::mutex g_guardian_lock;
 
 // The next two methods are not in dynhandler.cc because they use a few items declared in this file.
 static string DLCycleHandler(const vector<string>&parts, pid_t ppid)
@@ -176,7 +176,7 @@ static string DLRestHandler(const vector<string>&parts, pid_t ppid)
   }
   line.append(1,'\n');
   
-  Lock l(&g_guardian_lock);
+  std::lock_guard<std::mutex> l(g_guardian_lock);
 
   try {
     writen2(g_fd1[1],line.c_str(),line.size()+1);
@@ -216,7 +216,7 @@ static int guardian(int argc, char **argv)
   bool first=true;
   cpid=0;
 
-  pthread_mutex_lock(&g_guardian_lock);
+  g_guardian_lock.lock();
 
   for(;;) {
     int pid;
@@ -292,7 +292,7 @@ static int guardian(int argc, char **argv)
 
         writePid();
       }
-      pthread_mutex_unlock(&g_guardian_lock);  
+      g_guardian_lock.unlock();
       int status;
       cpid=pid;
       for(;;) {
@@ -314,7 +314,7 @@ static int guardian(int argc, char **argv)
         }
       }
 
-      pthread_mutex_lock(&g_guardian_lock);
+      g_guardian_lock.lock();
       close(g_fd1[1]);
       fclose(g_fp);
       g_fp=0;
@@ -494,7 +494,7 @@ int main(int argc, char **argv)
       }
       cerr<<" (";
       bool first = true;
-      for (auto const c : ::arg().getCommands()) {
+      for (const auto& c : ::arg().getCommands()) {
         if (!first) {
           cerr<<", ";
         }
index 1ecce73e9629b35df91733f471f70a4bc5df683f..bec850bd0b4a58657e50b5cdee0e2ea25378f181 100644 (file)
@@ -452,7 +452,7 @@ Queries to addresses for zones as configured in any of the settings `forward-zon
 .. versionadded:: 4.2.0
 
 -  Comma separated list of netmasks
--  Default: 0.0.0.0/0, ::, !127.0.0.0/8, !10.0.0.0/8, !100.64.0.0/10, !169.254.0.0/16, !192.168.0.0/16, !172.16.0.0/12, !::1/128, !fc00::/7, !fe80::/10
+-  Default: 0.0.0.0/0, ::/0, !127.0.0.0/8, !10.0.0.0/8, !100.64.0.0/10, !169.254.0.0/16, !192.168.0.0/16, !172.16.0.0/12, !::1/128, !fc00::/7, !fe80::/10
 
 List of requestor netmasks for which the requestor IP Address should be used as the :rfc:`EDNS Client Subnet <7871>` for outgoing queries. Outgoing queries for requestors that do not match this list will use the `ecs-scope-zero-address`_ instead.
 Valid incoming ECS values from `use-incoming-edns-subnet`_ are not replaced.
index f7a06bb1a4f9809c621675f5ce218483e2f88c7b..1eec4369cd65065f81fa54aa4e38716a094f56b9 100644 (file)
@@ -279,7 +279,7 @@ bool addRRSIG(const testkeysset_t& keys, std::vector<DNSRecord>& records, const
   const uint16_t type = records[recordsCount - 1].d_type;
 
   sortedRecords_t recordcontents;
-  for (const auto record : records) {
+  for (const auto& record : records) {
     if (record.d_name == name && record.d_type == type) {
       recordcontents.insert(record.d_content);
     }
index 70b7af35fdd2b09ac63b58691e49a985e6cbde85..d6cf953c63b1f197d269148201b08a713f6521ff 100644 (file)
@@ -25,8 +25,8 @@
 #endif
 
 #include <atomic>
-#include <condition_variable>
 #include <queue>
+#include <mutex>
 #include <thread>
 
 #include "iputils.hh"
index 50999d9c3d8b1a153804cf7a247f77ae309b6e07..069671a61f7fde70ea74d1afab86e7a1e8e1e50b 100644 (file)
@@ -20,7 +20,7 @@
 extern StatBag S;
 extern CommunicatorClass Communicator;
 
-pthread_mutex_t PacketHandler::s_rfc2136lock=PTHREAD_MUTEX_INITIALIZER;
+std::mutex PacketHandler::s_rfc2136lock;
 
 // Implement section 3.2.1 and 3.2.2 of RFC2136
 int PacketHandler::checkUpdatePrerequisites(const DNSRecord *rr, DomainInfo *di) {
@@ -823,7 +823,7 @@ int PacketHandler::processUpdate(DNSPacket& p) {
   }
 
 
-  Lock l(&s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
+  std::lock_guard<std::mutex> l(s_rfc2136lock); //TODO: i think this lock can be per zone, not for everything
   g_log<<Logger::Info<<msgPrefix<<"starting transaction."<<endl;
   if (!di.backend->startTransaction(p.qdomain, -1)) { // Not giving the domain_id means that we do not delete the existing records.
     g_log<<Logger::Error<<msgPrefix<<"Backend for domain "<<p.qdomain<<" does not support transaction. Can't do Update packet."<<endl;
index befe6286a6df00d20db40057e7b2908015064d1b..4b0ade2113344aee19d851613a3da1f5a31b307a 100644 (file)
@@ -49,7 +49,7 @@
 
 void CommunicatorClass::addSuckRequest(const DNSName &domain, const ComboAddress& master)
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   SuckRequest sr;
   sr.domain = domain;
   sr.master = master;
@@ -135,7 +135,7 @@ void CommunicatorClass::ixfrSuck(const DNSName &domain, const TSIGTriplet& tt, c
         grouped[{x.d_name, x.d_type}].second.push_back(x);
 
       di.backend->startTransaction(domain, -1);
-      for(const auto g : grouped) {
+      for(const auto& g : grouped) {
         vector<DNSRecord> rrset;
         {
           DNSZoneRecord zrr;
@@ -295,7 +295,7 @@ static vector<DNSResourceRecord> doAxfr(const ComboAddress& raddr, const DNSName
 void CommunicatorClass::suck(const DNSName &domain, const ComboAddress& remote)
 {
   {
-    Lock l(&d_lock);
+    std::lock_guard<std::mutex> l(d_lock);
     if(d_inprogress.count(domain)) {
       return; 
     }
@@ -634,7 +634,7 @@ void CommunicatorClass::suck(const DNSName &domain, const ComboAddress& remote)
   }
   catch(ResolverException &re) {
     {
-      Lock l(&d_lock);
+      std::lock_guard<std::mutex> l(d_lock);
       // The AXFR probably failed due to a problem on the master server. If SOA-checks against this master
       // still succeed, we would constantly try to AXFR the zone. To avoid this, we add the zone to the list of
       // failed slave-checks. This will suspend slave-checks (and subsequent AXFR) for this zone for some time.
@@ -729,7 +729,7 @@ struct SlaveSenderReceiver
 
 void CommunicatorClass::addSlaveCheckRequest(const DomainInfo& di, const ComboAddress& remote)
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   DomainInfo ours = di;
   ours.backend = 0;
 
@@ -750,7 +750,7 @@ void CommunicatorClass::addSlaveCheckRequest(const DomainInfo& di, const ComboAd
 
 void CommunicatorClass::addTrySuperMasterRequest(const DNSPacket& p)
 {
-  Lock l(&d_lock);
+  std::lock_guard<std::mutex> l(d_lock);
   DNSPacket ours = p;
   if(d_potentialsupermasters.insert(ours).second)
     d_any_sem.post(); // kick the loop!
@@ -766,7 +766,7 @@ void CommunicatorClass::slaveRefresh(PacketHandler *P)
   vector<DomainNotificationInfo> sdomains;
   set<DNSPacket, cmp> trysuperdomains;
   {
-    Lock l(&d_lock);
+    std::lock_guard<std::mutex> l(d_lock);
     set<DomainInfo> requeue;
     rdomains.reserve(d_tocheck.size());
     for(const auto& di: d_tocheck) {
@@ -806,7 +806,7 @@ void CommunicatorClass::slaveRefresh(PacketHandler *P)
   sdomains.reserve(rdomains.size());
   DNSSECKeeper dk(B); // NOW HEAR THIS! This DK uses our B backend, so no interleaved access!
   {
-    Lock l(&d_lock);
+    std::lock_guard<std::mutex> l(d_lock);
     domains_by_name_t& nameindex=boost::multi_index::get<IDTag>(d_suckdomains);
     time_t now = time(0);
 
@@ -867,14 +867,14 @@ void CommunicatorClass::slaveRefresh(PacketHandler *P)
   if(sdomains.empty())
   {
     if(d_slaveschanged) {
-      Lock l(&d_lock);
+      std::lock_guard<std::mutex> l(d_lock);
       g_log<<Logger::Warning<<"No new unfresh slave domains, "<<d_suckdomains.size()<<" queued for AXFR already, "<<d_inprogress.size()<<" in progress"<<endl;
     }
     d_slaveschanged = !rdomains.empty();
     return;
   }
   else {
-    Lock l(&d_lock);
+    std::lock_guard<std::mutex> l(d_lock);
     g_log<<Logger::Warning<<sdomains.size()<<" slave domain"<<(sdomains.size()>1 ? "s" : "")<<" need"<<
       (sdomains.size()>1 ? "" : "s")<<
       " checking, "<<d_suckdomains.size()<<" queued for AXFR"<<endl;
@@ -919,7 +919,7 @@ void CommunicatorClass::slaveRefresh(PacketHandler *P)
 
     if(!ssr.d_freshness.count(di.id)) { // If we don't have an answer for the domain
       uint64_t newCount = 1;
-      Lock l(&d_lock);
+      std::lock_guard<std::mutex> l(d_lock);
       const auto failedEntry = d_failedSlaveRefresh.find(di.zone);
       if (failedEntry != d_failedSlaveRefresh.end())
         newCount = d_failedSlaveRefresh[di.zone].first + 1;
@@ -936,7 +936,7 @@ void CommunicatorClass::slaveRefresh(PacketHandler *P)
     }
 
     {
-      Lock l(&d_lock);
+      std::lock_guard<std::mutex> l(d_lock);
       const auto wasFailedDomain = d_failedSlaveRefresh.find(di.zone);
       if (wasFailedDomain != d_failedSlaveRefresh.end())
         d_failedSlaveRefresh.erase(di.zone);
index 5d21667afee87b89f148aebc2150c167467e1d96..27205d5ffdfeba27ea81c29c8df72074c15ad12f 100644 (file)
@@ -105,7 +105,7 @@ struct GetTimeTest
   }
 };
 
-pthread_mutex_t s_testlock=PTHREAD_MUTEX_INITIALIZER;
+std::mutex s_testlock;
 
 struct GetLockUncontendedTest
 {
@@ -116,8 +116,8 @@ struct GetLockUncontendedTest
 
   void operator()() const
   {
-    pthread_mutex_lock(&s_testlock);
-    pthread_mutex_unlock(&s_testlock);
+    s_testlock.lock();
+    s_testlock.unlock();
   }
 };
 
index 34fc86da48dc59cdbe92c1e5d44a82dbb0414e90..8fc9466a436fdcc1ce2e10a89885efc95fe2d6e4 100644 (file)
@@ -234,19 +234,26 @@ std::unique_ptr<SSqlStatement> SSQLite3::prepare(const string& query, int nparam
 
 void SSQLite3::execute(const string& query) {
   char *errmsg;
-  int rc;
-  if (sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) == SQLITE_BUSY) {
+  std::string errstr1;
+  int rc = sqlite3_exec(m_pDB, query.c_str(), nullptr, nullptr, &errmsg);
+  if (rc != SQLITE_OK) {
+    errstr1 = errmsg;
+    sqlite3_free(errmsg);
+  }
+  if (rc == SQLITE_BUSY) {
     if (m_in_transaction) {
-      std::string errstr(errmsg);
-      sqlite3_free(errmsg);
-      throw("Failed to execute query: " + errstr);
+      throw SSqlException("Failed to execute query: " + errstr1);
     } else {
-      if ((rc = sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg) != SQLITE_OK) && rc != SQLITE_DONE && rc != SQLITE_ROW) {
-        std::string errstr(errmsg);
+      rc = sqlite3_exec(m_pDB, query.c_str(), NULL, NULL, &errmsg);
+      std::string errstr2;
+      if (rc != SQLITE_OK)  {
+        errstr2 = errmsg;
         sqlite3_free(errmsg);
-        throw("Failed to execute query: " + errstr);
+        throw SSqlException("Failed to execute query: " + errstr2);
       }
     }
+  } else if (rc != SQLITE_OK) {
+    throw SSqlException("Failed to execute query: " + errstr1);
   }
 }
 
index 985d96ea55004c909abbfa0145011ab0f6651d35..98cd70fe546d61fbf84fa5e30d0838b2d07bcd89 100644 (file)
@@ -23,7 +23,7 @@
 // s_resolversForStub contains the ComboAddresses that are used by
 // stubDoResolve
 static vector<ComboAddress> s_resolversForStub;
-static pthread_rwlock_t s_resolversForStubLock = PTHREAD_RWLOCK_INITIALIZER;
+static ReadWriteLock s_resolversForStubLock;
 static bool s_stubResolvConfigured = false;
 
 // /etc/resolv.conf last modification time
index d8449eb586378faf829f8b2d6b81d88c9afd7266..30314221331e077a11e79cacb6df1b62e81c4da2 100644 (file)
@@ -66,7 +66,7 @@ extern StatBag S;
 \brief This file implements the tcpreceiver that receives and answers questions over TCP/IP
 */
 
-pthread_mutex_t TCPNameserver::s_plock = PTHREAD_MUTEX_INITIALIZER;
+std::mutex TCPNameserver::s_plock;
 std::unique_ptr<Semaphore> TCPNameserver::d_connectionroom_sem{nullptr};
 std::unique_ptr<PacketHandler> TCPNameserver::s_P{nullptr};
 unsigned int TCPNameserver::d_maxTCPConnections = 0;
@@ -88,13 +88,9 @@ void TCPNameserver::go()
   catch(PDNSException &ae) {
     g_log<<Logger::Error<<"TCP server is unable to launch backends - will try again when questions come in: "<<ae.reason<<endl;
   }
-  pthread_create(&d_tid, 0, launcher, static_cast<void *>(this));
-}
 
-void *TCPNameserver::launcher(void *data)
-{
-  static_cast<TCPNameserver *>(data)->thread();
-  return 0;
+  std::thread th(std::bind(&TCPNameserver::thread, this));
+  th.detach();
 }
 
 // throws PDNSException if things didn't go according to plan, returns 0 if really 0 bytes were read
@@ -225,12 +221,10 @@ void TCPNameserver::decrementClientCount(const ComboAddress& remote)
   }
 }
 
-void *TCPNameserver::doConnection(void *data)
+void TCPNameserver::doConnection(int fd)
 {
   setThreadName("pdns/tcpConnect");
   std::unique_ptr<DNSPacket> packet;
-  // Fix gcc-4.0 error (on AMD64)
-  int fd=(int)(long)data; // gotta love C (generates a harmless warning on opteron)
   ComboAddress remote;
   socklen_t remotelen=sizeof(remote);
   size_t transactions = 0;
@@ -239,7 +233,6 @@ void *TCPNameserver::doConnection(void *data)
     start = time(NULL);
   }
 
-  pthread_detach(pthread_self());
   if(getpeername(fd, (struct sockaddr *)&remote, &remotelen) < 0) {
     g_log<<Logger::Warning<<"Received question from socket which had no remote address, dropping ("<<stringerror()<<")"<<endl;
     d_connectionroom_sem->post();
@@ -249,7 +242,7 @@ void *TCPNameserver::doConnection(void *data)
     catch(const PDNSException& e) {
       g_log<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
     }
-    return 0;
+    return;
   }
 
   setNonBlocking(fd);
@@ -352,7 +345,7 @@ void *TCPNameserver::doConnection(void *data)
         }
       }
       {
-        Lock l(&s_plock);
+        std::lock_guard<std::mutex> l(s_plock);
         if(!s_P) {
           g_log<<Logger::Error<<"TCP server is without backend connections, launching"<<endl;
           s_P=make_unique<PacketHandler>();
@@ -368,7 +361,7 @@ void *TCPNameserver::doConnection(void *data)
     }
   }
   catch(PDNSException &ae) {
-    Lock l(&s_plock);
+    std::lock_guard<std::mutex> l(s_plock);
     s_P.reset(); // on next call, backend will be recycled
     g_log<<Logger::Error<<"TCP nameserver had error, cycling backend: "<<ae.reason<<endl;
   }
@@ -392,8 +385,6 @@ void *TCPNameserver::doConnection(void *data)
     g_log<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
   }
   decrementClientCount(remote);
-
-  return 0;
 }
 
 
@@ -538,7 +529,7 @@ int TCPNameserver::doAXFR(const DNSName &target, std::unique_ptr<DNSPacket>& q,
   // determine if zone exists and AXFR is allowed using existing backend before spawning a new backend.
   SOAData sd;
   {
-    Lock l(&s_plock);
+    std::lock_guard<std::mutex> l(s_plock);
     DLOG(g_log<<"Looking for SOA"<<endl);    // find domain_id via SOA and list complete domain. No SOA, no AXFR
     if(!s_P) {
       g_log<<Logger::Error<<"TCP server is without backend connections in doAXFR, launching"<<endl;
@@ -1070,7 +1061,7 @@ int TCPNameserver::doIXFR(std::unique_ptr<DNSPacket>& q, int outsock)
   // determine if zone exists and AXFR is allowed using existing backend before spawning a new backend.
   SOAData sd;
   {
-    Lock l(&s_plock);
+    std::lock_guard<std::mutex> l(s_plock);
     DLOG(g_log<<"Looking for SOA"<<endl); // find domain_id via SOA and list complete domain. No SOA, no IXFR
     if(!s_P) {
       g_log<<Logger::Error<<"TCP server is without backend connections in doIXFR, launching"<<endl;
@@ -1124,7 +1115,7 @@ int TCPNameserver::doIXFR(std::unique_ptr<DNSPacket>& q, int outsock)
       DNSName algorithm=trc.d_algoName; // FIXME400: was toLowerCanonic, compare output
       if (algorithm == DNSName("hmac-md5.sig-alg.reg.int"))
         algorithm = DNSName("hmac-md5");
-      Lock l(&s_plock);
+      std::lock_guard<std::mutex> l(s_plock);
       if(!s_P->getBackend()->getTSIGKey(tsigkeyname, &algorithm, &tsig64)) {
         g_log<<Logger::Error<<"TSIG key '"<<tsigkeyname<<"' for domain '"<<target<<"' not found"<<endl;
         return 0;
@@ -1175,7 +1166,6 @@ TCPNameserver::TCPNameserver()
 //  sem_init(&d_connectionroom_sem,0,::arg().asNum("max-tcp-connections"));
   d_connectionroom_sem = make_unique<Semaphore>( ::arg().asNum( "max-tcp-connections" ));
   d_maxTCPConnections = ::arg().asNum( "max-tcp-connections" );
-  d_tid=0;
 
   vector<string>locals;
   stringtok(locals,::arg()["local-ipv6"]," ,");
@@ -1284,7 +1274,6 @@ void TCPNameserver::thread()
               s_clientsCount[remote]++;
             }
 
-            pthread_t tid;
             d_connectionroom_sem->wait(); // blocks if no connections are available
 
             int room;
@@ -1292,9 +1281,12 @@ void TCPNameserver::thread()
             if(room<1)
               g_log<<Logger::Warning<<"Limit of simultaneous TCP connections reached - raise max-tcp-connections"<<endl;
 
-            int err;
-            if((err = pthread_create(&tid, 0, &doConnection, reinterpret_cast<void*>(fd)))) {
-              g_log<<Logger::Error<<"Error creating thread: "<<stringerror(err)<<endl;
+            try {
+              std::thread connThread(doConnection, fd);
+              connThread.detach();
+            }
+            catch (std::exception& e) {
+              g_log<<Logger::Error<<"Error creating thread: "<<e.what()<<endl;
               d_connectionroom_sem->post();
               close(fd);
               decrementClientCount(remote);
index cafe7943312eab80c44ac3138220526dbf09fed7..5220d5b5f2ef8e78cad0641375159506b8657aaf 100644 (file)
@@ -54,15 +54,13 @@ private:
   static int doAXFR(const DNSName &target, std::unique_ptr<DNSPacket>& q, int outsock);
   static int doIXFR(std::unique_ptr<DNSPacket>& q, int outsock);
   static bool canDoAXFR(std::unique_ptr<DNSPacket>& q);
-  static void *doConnection(void *data);
-  static void *launcher(void *data);
+  static void doConnection(int fd);
   static void decrementClientCount(const ComboAddress& remote);
   void thread(void);
-  static pthread_mutex_t s_plock;
+  static std::mutex s_plock;
   static std::mutex s_clientsCountMutex;
   static std::map<ComboAddress,size_t,ComboAddress::addressOnlyLessThan> s_clientsCount;
   static std::unique_ptr<PacketHandler> s_P;
-  pthread_t d_tid;
   static std::unique_ptr<Semaphore> d_connectionroom_sem;
   static unsigned int d_maxTCPConnections;
   static NetmaskGroup d_ng;
index 8e41798cb99d13f56045a8692a45d30a41471119..becd1ec2e77e9fe719c0f71465dc2df8ba564218 100644 (file)
@@ -297,14 +297,13 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheNXDomainTTL) {
 
 static DNSDistPacketCache g_PC(500000);
 
-static void *threadMangler(void* off)
+static void threadMangler(unsigned int offset)
 {
   struct timespec queryTime;
   gettime(&queryTime);  // does not have to be accurate ("realTime") in tests
   try {
     ComboAddress remote;
     bool dnssecOK = false;
-    unsigned int offset=(unsigned int)(unsigned long)off;
     for(unsigned int counter=0; counter < 100000; ++counter) {
       DNSName a=DNSName("hello ")+DNSName(std::to_string(counter+offset));
       vector<uint8_t> query;
@@ -337,19 +336,17 @@ static void *threadMangler(void* off)
     cerr<<"Had error: "<<e.reason<<endl;
     throw;
   }
-  return 0;
 }
 
 AtomicCounter g_missing;
 
-static void *threadReader(void* off)
+static void threadReader(unsigned int offset)
 {
   bool dnssecOK = false;
   struct timespec queryTime;
   gettime(&queryTime);  // does not have to be accurate ("realTime") in tests
   try
   {
-    unsigned int offset=(unsigned int)(unsigned long)off;
     vector<DNSResourceRecord> entry;
     ComboAddress remote;
     for(unsigned int counter=0; counter < 100000; ++counter) {
@@ -373,25 +370,31 @@ static void *threadReader(void* off)
     cerr<<"Had error in threadReader: "<<e.reason<<endl;
     throw;
   }
-  return 0;
 }
 
 BOOST_AUTO_TEST_CASE(test_PacketCacheThreaded) {
   try {
-    pthread_t tid[4];
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadMangler, (void*)(i*1000000UL));
-    void* res;
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    std::vector<std::thread> threads;
+    for (int i = 0; i < 4; ++i) {
+      threads.push_back(std::thread(threadMangler, i*1000000UL));
+    }
+
+    for (auto& t : threads) {
+      t.join();
+    }
+
+    threads.clear();
 
     BOOST_CHECK_EQUAL(g_PC.getSize() + g_PC.getDeferredInserts() + g_PC.getInsertCollisions(), 400000U);
     BOOST_CHECK_SMALL(1.0*g_PC.getInsertCollisions(), 10000.0);
 
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadReader, (void*)(i*1000000UL));
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    for (int i = 0; i < 4; ++i) {
+      threads.push_back(std::thread(threadReader, i*1000000UL));
+    }
+
+    for (auto& t : threads) {
+      t.join();
+    }
 
     BOOST_CHECK((g_PC.getDeferredInserts() + g_PC.getDeferredLookups() + g_PC.getInsertCollisions()) >= g_missing);
   }
index bdd3697504c735a5c8425f10df709d774e2c641d..46e844c60aa20905b8ae4258d8c9a3ce201865c1 100644 (file)
@@ -11,26 +11,20 @@ using namespace boost;
 
 BOOST_AUTO_TEST_SUITE(test_lock_hh)
 
-static std::vector<std::unique_ptr<pthread_rwlock_t> > g_locks;
+static std::vector<ReadWriteLock> g_locks(1000);
 
 static void lthread()
 {
   std::vector<ReadLock> 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_t>();
-    pthread_rwlock_init(p.get(), 0);
-    g_locks.emplace_back(std::move(p));
-  }
-
   std::vector<ReadLock> 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<WriteLock> 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()
index 39f5911998ff5d868320b4183132fb5ce0a60366..5fcfff8216aab30b4db38b773d2fb44a5f95c3b0 100644 (file)
@@ -12,6 +12,8 @@
 #include "auth-querycache.hh"
 #include "arguments.hh"
 #include <utility>
+#include <thread>
+
 extern StatBag S;
 
 BOOST_AUTO_TEST_SUITE(test_packetcache_cc)
@@ -71,30 +73,26 @@ BOOST_AUTO_TEST_CASE(test_AuthQueryCacheSimple) {
 static AuthQueryCache* g_QC;
 static AtomicCounter g_QCmissing;
 
-static void *threadQCMangler(void* a)
+static void threadQCMangler(unsigned int offset)
 try
 {
   vector<DNSZoneRecord> records;
-  unsigned int offset=(unsigned int)(unsigned long)a;
   for(unsigned int counter=0; counter < 100000; ++counter)
     g_QC->insert(DNSName("hello ")+DNSName(std::to_string(counter+offset)), QType(QType::A), vector<DNSZoneRecord>(records), 3600, 1);
-  return 0;
 }
  catch(PDNSException& e) {
    cerr<<"Had error: "<<e.reason<<endl;
    throw;
  }
 
-static void *threadQCReader(void* a)
+static void threadQCReader(unsigned int offset)
 try
 {
-  unsigned int offset=(unsigned int)(unsigned long)a;
   vector<DNSZoneRecord> entry;
   for(unsigned int counter=0; counter < 100000; ++counter)
     if(!g_QC->getEntry(DNSName("hello ")+DNSName(std::to_string(counter+offset)), QType(QType::A), entry, 1)) {
       g_QCmissing++;
     }
-  return 0;
 }
 catch(PDNSException& e) {
   cerr<<"Had error in threadQCReader: "<<e.reason<<endl;
@@ -107,20 +105,28 @@ BOOST_AUTO_TEST_CASE(test_QueryCacheThreaded) {
     AuthQueryCache QC;
     QC.setMaxEntries(1000000);
     g_QC=&QC;
-    pthread_t tid[4];
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadQCMangler, (void*)(i*1000000UL));
-    void* res;
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    std::vector<std::thread> manglers;
+    for (int i=0; i < 4; ++i) {
+      manglers.push_back(std::thread(threadQCMangler, i*1000000UL));
+    }
+
+    for (auto& t : manglers) {
+      t.join();
+    }
+    manglers.clear();
 
     BOOST_CHECK_EQUAL(QC.size() + S.read("deferred-cache-inserts"), 400000U);
     BOOST_CHECK_SMALL(1.0*S.read("deferred-cache-inserts"), 10000.0);
 
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadQCReader, (void*)(i*1000000UL));
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    std::vector<std::thread> readers;
+    for (int i=0; i < 4; ++i) {
+      readers.push_back(std::thread(threadQCReader, i*1000000UL));
+    }
+
+    for (auto& t : readers) {
+      t.join();
+    }
+    readers.clear();
 
     BOOST_CHECK(S.read("deferred-cache-inserts") + S.read("deferred-cache-lookup") >= g_QCmissing);
     //    BOOST_CHECK_EQUAL(S.read("deferred-cache-lookup"), 0); // cache cleaning invalidates this
@@ -135,10 +141,9 @@ BOOST_AUTO_TEST_CASE(test_QueryCacheThreaded) {
 static AuthPacketCache* g_PC;
 static AtomicCounter g_PCmissing;
 
-static void *threadPCMangler(void* a)
+static void threadPCMangler(unsigned int offset)
 try
 {
-  unsigned int offset=(unsigned int)(unsigned long)a;
   for(unsigned int counter=0; counter < 100000; ++counter) {
     vector<uint8_t> pak;
     DNSName qname = DNSName("hello ")+DNSName(std::to_string(counter+offset));
@@ -165,18 +170,15 @@ try
     const unsigned int maxTTL = 3600;
     g_PC->insert(q, r, maxTTL);
   }
-
-  return 0;
 }
  catch(PDNSException& e) {
    cerr<<"Had error: "<<e.reason<<endl;
    throw;
  }
 
-static void *threadPCReader(void* a)
+static void threadPCReader(unsigned int offset)
 try
 {
-  unsigned int offset=(unsigned int)(unsigned long)a;
   vector<DNSZoneRecord> entry;
   for(unsigned int counter=0; counter < 100000; ++counter) {
     vector<uint8_t> pak;
@@ -191,8 +193,6 @@ try
       g_PCmissing++;
     }
   }
-
-  return 0;
 }
 catch(PDNSException& e) {
   cerr<<"Had error in threadPCReader: "<<e.reason<<endl;
@@ -207,21 +207,29 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheThreaded) {
 
     g_PC=&PC;
     g_PCmissing = 0;
-    pthread_t tid[4];
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadPCMangler, (void*)(i*1000000UL));
-    void* res;
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    std::vector<std::thread> manglers;
+    for (int i=0; i < 4; ++i) {
+      manglers.push_back(std::thread(threadPCMangler, i*1000000UL));
+    }
+
+    for (auto& t : manglers) {
+      t.join();
+    }
+    manglers.clear();
 
     BOOST_CHECK_EQUAL(PC.size() + S.read("deferred-packetcache-inserts"), 400000UL);
     BOOST_CHECK_EQUAL(S.read("deferred-packetcache-lookup"), 0UL);
     BOOST_CHECK_SMALL(1.0*S.read("deferred-packetcache-inserts"), 10000.0);
 
-    for(int i=0; i < 4; ++i)
-      pthread_create(&tid[i], 0, threadPCReader, (void*)(i*1000000UL));
-    for(int i=0; i < 4 ; ++i)
-      pthread_join(tid[i], &res);
+    std::vector<std::thread> readers;
+    for (int i=0; i < 4; ++i) {
+      readers.push_back(std::thread(threadPCReader, i*1000000UL));
+    }
+
+    for (auto& t : readers) {
+      t.join();
+    }
+    readers.clear();
 
 /*
     cerr<<"Misses: "<<S.read("packetcache-miss")<<endl;
@@ -243,14 +251,12 @@ BOOST_AUTO_TEST_CASE(test_PacketCacheThreaded) {
 }
 
 bool g_stopCleaning;
-static void *cacheCleaner(void*)
+static void cacheCleaner()
 try
 {
   while(!g_stopCleaning) {
     g_QC->cleanup();
   }
-
-  return 0;
 }
 catch(PDNSException& e) {
   cerr<<"Had error in cacheCleaner: "<<e.reason<<endl;
@@ -270,19 +276,24 @@ BOOST_AUTO_TEST_CASE(test_QueryCacheClean) {
     sleep(1);
 
     g_QC=&QC;
-    pthread_t tid[4];
+    std::vector<std::thread> readers;
+    for (int i=0; i < 4; ++i) {
+      if (i < 3) {
+        readers.push_back(std::thread(threadQCReader, i*1000000UL));
+      }
+      else {
+        readers.push_back(std::thread(cacheCleaner));
+      }
+    }
 
-    pthread_create(&tid[0], 0, threadQCReader, (void*)(0*1000000UL));
-    pthread_create(&tid[1], 0, threadQCReader, (void*)(1*1000000UL));
-    pthread_create(&tid[2], 0, threadQCReader, (void*)(2*1000000UL));
-    //    pthread_create(&tid[2], 0, threadMangler, (void*)(0*1000000UL));
-    pthread_create(&tid[3], 0, cacheCleaner, 0);
+    for (int i = 0; i < 3 ; ++i) {
+      readers.at(i).join();
+    }
 
-    void *res;
-    for(int i=0; i < 3 ; ++i)
-      pthread_join(tid[i], &res);
     g_stopCleaning=true;
-    pthread_join(tid[3], &res);
+    readers.at(3).join();
+
+    readers.clear();
   }
   catch(PDNSException& e) {
     cerr<<"Had error in test_QueryCacheClean: "<<e.reason<<endl;
index da21db062a16d40576322225309f15da3f28f1b7..135dbf665a7119bb791ee0c83fbcacda6bfd00eb 100644 (file)
@@ -161,7 +161,7 @@ static void checkRR(const signerParams& signer)
 
 BOOST_AUTO_TEST_CASE(test_generic_signers)
 {
-  for (const auto signer : signers) {
+  for (const auto& signer : signers) {
     DNSKEYRecordContent drc;
     auto dcke = DNSCryptoKeyEngine::makeFromISCString(drc, signer.iscMap);
 
index a33978540a78b267254a690bd2573c5e142b32de..d551ca6546172327bee860374d36d51f6aec66a2 100644 (file)
@@ -9,26 +9,23 @@
 
 #include <boost/tuple/tuple.hpp>
 #include <stdint.h>
+#include <thread>
 #include "misc.hh"
 #include "dns.hh"
 #include "statbag.hh"
 
 using std::string;
 
-static void *threadMangler(void* a)
+static void threadMangler(AtomicCounter* ac)
 {
-  AtomicCounter* ac=(AtomicCounter*)a;
   for(unsigned int n=0; n < 1000000; ++n)
     (*ac)++;
-  return 0;
 }
 
-static void *threadMangler2(void* a)
+static void threadMangler2(StatBag* S)
 {
-  StatBag* S = (StatBag*)a;
   for(unsigned int n=0; n < 1000000; ++n)
     S->inc("c");
-  return 0;
 }
 
 
@@ -56,22 +53,28 @@ BOOST_AUTO_TEST_CASE(test_StatBagBasic) {
   BOOST_CHECK_EQUAL(s.read("a"), n+1);
 
   AtomicCounter* acc = s.getPointer("c");
-  pthread_t tid[4];
-  for(int i=0; i < 4; ++i) 
-    pthread_create(&tid[i], 0, threadMangler, (void*)acc);
-  void* res;
-  for(int i=0; i < 4 ; ++i)
-    pthread_join(tid[i], &res);
+  std::vector<std::thread> manglers;
+  for (int i=0; i < 4; ++i) {
+    manglers.push_back(std::thread(threadMangler, acc));
+  }
+
+  for (auto& t : manglers) {
+    t.join();
+  }
+  manglers.clear();
 
   BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
  
   s.set("c", 0);
 
-  for(int i=0; i < 4; ++i) 
-    pthread_create(&tid[i], 0, threadMangler2, (void*)&s);
+  for (int i=0; i < 4; ++i) {
+    manglers.push_back(std::thread(threadMangler2, &s));
+  }
 
-  for(int i=0; i < 4 ; ++i)
-    pthread_join(tid[i], &res);
+  for (auto& t : manglers) {
+    t.join();
+  }
+  manglers.clear();
 
   BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
 
index 4d3e4795d6623bdbaeb212ab145339166d10271e..45cbe9aab13176fdacdb869d5c58c1bfe7e4eed7 100644 (file)
 extern StatBag S;
 
 vector<UeberBackend *>UeberBackend::instances;
-pthread_mutex_t UeberBackend::instances_lock=PTHREAD_MUTEX_INITIALIZER;
+std::mutex UeberBackend::instances_lock;
 
 // initially we are blocked
 bool UeberBackend::d_go=false;
-pthread_mutex_t  UeberBackend::d_mut = PTHREAD_MUTEX_INITIALIZER;
-pthread_cond_t UeberBackend::d_cond = PTHREAD_COND_INITIALIZER;
+std::mutex UeberBackend::d_mut;
+std::condition_variable UeberBackend::d_cond;
 
 //! Loads a module and reports it to all UeberBackend threads
 bool UeberBackend::loadmodule(const string &name)
@@ -94,10 +94,11 @@ bool UeberBackend::loadModules(const vector<string>& modules, const string& path
 
 void UeberBackend::go(void)
 {
-  pthread_mutex_lock(&d_mut);
-  d_go=true;
-  pthread_cond_broadcast(&d_cond);
-  pthread_mutex_unlock(&d_mut);
+  {
+    std::unique_lock<std::mutex> l(d_mut);
+    d_go = true;
+  }
+  d_cond.notify_all();
 }
 
 bool UeberBackend::getDomainInfo(const DNSName &domain, DomainInfo &di, bool getSerial)
@@ -463,9 +464,10 @@ bool UeberBackend::superMasterBackend(const string &ip, const DNSName &domain, c
 
 UeberBackend::UeberBackend(const string &pname)
 {
-  pthread_mutex_lock(&instances_lock);
-  instances.push_back(this); // report to the static list of ourself
-  pthread_mutex_unlock(&instances_lock);
+  {
+    std::lock_guard<std::mutex> l(instances_lock);
+    instances.push_back(this); // report to the static list of ourself
+  }
 
   d_negcached=0;
   d_ancount=0;
@@ -474,7 +476,6 @@ UeberBackend::UeberBackend(const string &pname)
   d_cache_ttl = ::arg().asNum("query-cache-ttl");
   d_negcache_ttl = ::arg().asNum("negquery-cache-ttl");
 
-  d_tid=pthread_self();
   d_stale=false;
 
   backends=BackendMakers().all(pname=="key-only");
@@ -487,12 +488,11 @@ static void del(DNSBackend* d)
 
 void UeberBackend::cleanup()
 {
-  pthread_mutex_lock(&instances_lock);
-
-  remove(instances.begin(),instances.end(),this);
-  instances.resize(instances.size()-1);
-
-  pthread_mutex_unlock(&instances_lock);
+  {
+    std::lock_guard<std::mutex> l(instances_lock);
+    remove(instances.begin(),instances.end(),this);
+    instances.resize(instances.size()-1);
+  }
 
   for_each(backends.begin(),backends.end(),del);
 }
@@ -567,14 +567,11 @@ void UeberBackend::lookup(const QType &qtype,const DNSName &qname, int zoneId, D
   }
 
   DLOG(g_log<<"UeberBackend received question for "<<qtype.getName()<<" of "<<qname<<endl);
-  if(!d_go) {
-    pthread_mutex_lock(&d_mut);
-    while (d_go==false) {
-      g_log<<Logger::Error<<"UeberBackend is blocked, waiting for 'go'"<<endl;
-      pthread_cond_wait(&d_cond, &d_mut);
-      g_log<<Logger::Error<<"Broadcast received, unblocked"<<endl;
-    }
-    pthread_mutex_unlock(&d_mut);
+  if (!d_go) {
+    g_log<<Logger::Error<<"UeberBackend is blocked, waiting for 'go'"<<endl;
+    std::unique_lock<std::mutex> l(d_mut);
+    d_cond.wait(l, []{ return d_go == true; });
+    g_log<<Logger::Error<<"Broadcast received, unblocked"<<endl;
   }
 
   d_domain_id=zoneId;
index 09c3b451b15b585c1e1ed07dca095d11431f5c0a..48db857d9904b85fa37725e07bab6bbb794f5bfa 100644 (file)
@@ -24,8 +24,9 @@
 #include <map>
 #include <string>
 #include <algorithm>
-#include <pthread.h>
 #include <semaphore.h>
+#include <mutex>
+#include <condition_variable>
 
 #include <unistd.h>
 #include <sys/stat.h>
@@ -56,7 +57,7 @@ public:
       existing threads of new modules 
   */
   static vector<UeberBackend *>instances;
-  static pthread_mutex_t instances_lock;
+  static std::mutex instances_lock;
 
   static bool loadmodule(const string &name);
   static bool loadModules(const vector<string>& modules, const string& path);
@@ -134,13 +135,12 @@ public:
   bool searchRecords(const string &pattern, int maxResults, vector<DNSResourceRecord>& result);
   bool searchComments(const string &pattern, int maxResults, vector<Comment>& result);
 private:
-  pthread_t d_tid;
   handle d_handle;
   vector<DNSZoneRecord> d_answers;
   vector<DNSZoneRecord>::const_iterator d_cachehandleiter;
 
-  static pthread_mutex_t d_mut;
-  static pthread_cond_t d_cond;
+  static std::mutex d_mut;
+  static std::condition_variable d_cond;
 
   struct Question
   {
index c3ca5c29aec277b24ad13995b7ae6c832b5e9ae8..de5fa008405e1a019be87da67208bb0fec535081 100644 (file)
@@ -62,7 +62,6 @@ static const std::set<uint16_t> onlyOneEntryTypes = { QType::CNAME, QType::DNAME
 static const std::set<uint16_t> exclusiveEntryTypes = { QType::CNAME };
 
 AuthWebServer::AuthWebServer() :
-  d_tid(0),
   d_start(time(nullptr)),
   d_min10(0),
   d_min5(0),
@@ -87,8 +86,10 @@ AuthWebServer::AuthWebServer() :
 void AuthWebServer::go()
 {
   S.doRings();
-  pthread_create(&d_tid, 0, webThreadHelper, this);
-  pthread_create(&d_tid, 0, statThreadHelper, this);
+  std::thread webT(std::bind(&AuthWebServer::webThread, this));
+  webT.detach();
+  std::thread statT(std::bind(&AuthWebServer::statThread, this));
+  statT.detach();
 }
 
 void AuthWebServer::statThread()
@@ -110,20 +111,6 @@ void AuthWebServer::statThread()
   }
 }
 
-void *AuthWebServer::statThreadHelper(void *p)
-{
-  AuthWebServer *self=static_cast<AuthWebServer *>(p);
-  self->statThread();
-  return 0; // never reached
-}
-
-void *AuthWebServer::webThreadHelper(void *p)
-{
-  AuthWebServer *self=static_cast<AuthWebServer *>(p);
-  self->webThread();
-  return 0; // never reached
-}
-
 static string htmlescape(const string &s) {
   string result;
   for(string::const_iterator it=s.begin(); it!=s.end(); ++it) {
@@ -2197,7 +2184,7 @@ static void apiServerSearchData(HttpRequest* req, HttpResponse* resp) {
 
   B.getAllDomains(&domains, true);
 
-  for(const DomainInfo di: domains)
+  for(const DomainInfo& di: domains)
   {
     if ((objectType == ObjectType::ALL || objectType == ObjectType::ZONE) && ents < maxEnts && sm.match(di.zone)) {
       doc.push_back(Json::object {
index b613174f6216a9fbfdab7450e656515eeb80e43b..79f8f2f6b6fd38af83a687ced2ff21aa43b2a710 100644 (file)
@@ -78,8 +78,6 @@ public:
   static string makePercentage(const double& val);
 
 private:
-  static void *webThreadHelper(void *);
-  static void *statThreadHelper(void *p);
   void indexfunction(HttpRequest* req, HttpResponse* resp);
   void cssfunction(HttpRequest* req, HttpResponse* resp);
   void jsonstat(HttpRequest* req, HttpResponse* resp);
@@ -88,7 +86,6 @@ private:
   void printargs(ostringstream &ret);
   void webThread();
   void statThread();
-  pthread_t d_tid;
 
   time_t d_start;
   double d_min10, d_min5, d_min1;