]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
replace a bunch of static ints with AtomicCounters for threadsafety; make QType init...
authorPeter van Dijk <peter.van.dijk@netherlabs.nl>
Thu, 26 Apr 2012 11:28:04 +0000 (11:28 +0000)
committerPeter van Dijk <peter.van.dijk@netherlabs.nl>
Thu, 26 Apr 2012 11:28:04 +0000 (11:28 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2579 d19b8d6e-7fed-0310-83ef-9ca221ded41b

12 files changed:
pdns/common_startup.cc
pdns/dbdnsseckeeper.cc
pdns/distributor.hh
pdns/dnsseckeeper.hh
pdns/packetcache.cc
pdns/packetcache.hh
pdns/packethandler.cc
pdns/packethandler.hh
pdns/qtype.cc
pdns/qtype.hh
pdns/ueberbackend.cc
pdns/ueberbackend.hh

index cba697715de0b366e8f33a49d2dfd2eb6926918d..99c3affd5a9ed1731573aea718428bf0b882908b 100644 (file)
@@ -363,7 +363,8 @@ void mainthread()
   if(::arg().asNum("receiver-threads") > 1) {
     g_mustlockdistributor=true;
   }
-  for(int n=0; n < ::arg().asNum("receiver-threads"); ++n)
+  unsigned int max_rthreads= ::arg().asNum("receiver-threads");
+  for(int n=0; n < max_rthreads; ++n)
     pthread_create(&qtid,0,qthread, reinterpret_cast<void *>(n)); // receives packets
 
   void *p;
index c505dc85413d1a6e9fbda02985a0d9d94b5dad91..89a769c46687a827df6b43349d540cb70c72ce73 100644 (file)
@@ -42,7 +42,7 @@ DNSSECKeeper::keycache_t DNSSECKeeper::s_keycache;
 DNSSECKeeper::metacache_t DNSSECKeeper::s_metacache;
 pthread_mutex_t DNSSECKeeper::s_metacachelock = PTHREAD_MUTEX_INITIALIZER;
 pthread_mutex_t DNSSECKeeper::s_keycachelock = PTHREAD_MUTEX_INITIALIZER;
-unsigned int DNSSECKeeper::s_ops;
+AtomicCounter DNSSECKeeper::s_ops;
 time_t DNSSECKeeper::s_last_prune;
 
 bool DNSSECKeeper::isSecuredZone(const std::string& zone) 
@@ -50,7 +50,7 @@ bool DNSSECKeeper::isSecuredZone(const std::string& zone)
   if(isPresigned(zone))
     return true;
   
-  if(!((s_ops++) % 100000)) {
+  if(!((++s_ops) % 100000)) {
     cleanup();
   }
 
@@ -186,7 +186,7 @@ void DNSSECKeeper::getFromMeta(const std::string& zname, const std::string& key,
   value.clear();
   unsigned int now = time(0);
 
-  if(!((s_ops++) % 100000)) {
+  if(!((++s_ops) % 100000)) {
     cleanup();
   }
 
@@ -276,7 +276,7 @@ DNSSECKeeper::keyset_t DNSSECKeeper::getKeys(const std::string& zone, boost::tri
 {
   unsigned int now = time(0);
 
-  if(!((s_ops++) % 100000)) {
+  if(!((++s_ops) % 100000)) {
     cleanup();
   }
 
index fdd4f5c1051d539afd772f8ae9d3627261f9a17e..bdca89bb627d55cc7c4ebcde2317585e2257a1cf 100644 (file)
@@ -107,7 +107,7 @@ private:
   int nextid;
   time_t d_last_started;
   int d_num_threads;
-  int d_idle_threads;
+  AtomicCounter d_idle_threads;
   Backend *b;
 };
 
@@ -119,7 +119,7 @@ template<class Answer, class Question, class Backend>Distributor<Answer,Question
   b=0;
   d_overloaded = false;
   nextid=0;
-  d_idle_threads=0;
+  // d_idle_threads=0;
   d_last_started=time(0);
 //  sem_init(&numquestions,0,0);
   pthread_mutex_init(&q_lock,0);
@@ -160,13 +160,13 @@ template<class Answer, class Question, class Backend>void *Distributor<Answer,Qu
     // ick ick ick!
     static int overloadQueueLength=::arg().asNum("overload-queue-length");
     for(;;) {
-      us->d_idle_threads++;
+      ++(us->d_idle_threads);
 
       us->numquestions.getValue( &qcount );
 
       us->numquestions.wait();
 
-      us->d_idle_threads--;
+      --(us->d_idle_threads);
       pthread_mutex_lock(&us->q_lock);
 
       QuestionData QD=us->questions.front();
index f5409ddea1731598818367747d4f0f21b412a905..22b4b61be2ed36c60088724f225031bab3624735 100644 (file)
@@ -157,7 +157,7 @@ private:
   static metacache_t s_metacache;
   static pthread_mutex_t s_metacachelock;
   static pthread_mutex_t s_keycachelock;
-  static unsigned int s_ops;
+  static AtomicCounter s_ops;
   static time_t s_last_prune;
 };
 
index 3e611377a2842f7b181bf90b0fc8768f984f4c22..43f78ff5c9d3f7bf90404d2840953ce970065190 100644 (file)
@@ -28,7 +28,7 @@ extern StatBag S;
 PacketCache::PacketCache()
 {
   pthread_rwlock_init(&d_mut,0);
-  d_ops = 0;
+  // d_ops = 0;
 
   d_ttl=-1;
   d_recursivettl=-1;
@@ -54,7 +54,7 @@ int PacketCache::get(DNSPacket *p, DNSPacket *cached)
   if(d_ttl<0) 
     getTTLS();
 
-  if(!((d_ops++) % 300000)) {
+  if(!((++d_ops) % 300000)) {
     cleanup();
   }
 
@@ -135,7 +135,7 @@ void PacketCache::insert(DNSPacket *q, DNSPacket *r, unsigned int maxttl)
 void PacketCache::insert(const string &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID, 
   bool meritsRecursion, unsigned int maxReplyLen, bool dnssecOk)
 {
-  if(!((d_ops++) % 300000)) {
+  if(!((++d_ops) % 300000)) {
     cleanup();
   }
 
@@ -259,7 +259,7 @@ bool PacketCache::getEntry(const string &qname, const QType& qtype, CacheEntryTy
   if(d_ttl<0) 
     getTTLS();
 
-  if(!((d_ops++) % 300000)) {
+  if(!((++d_ops) % 300000)) {
     cleanup();
   }
 
index 0d5b179e2328421a406668007f4c47dc541f4bd8..72c37b42b261fa7053f276f9772f05591fe352df 100644 (file)
@@ -130,7 +130,7 @@ private:
 
   pthread_rwlock_t d_mut;
 
-  unsigned int d_ops;
+  AtomicCounter d_ops;
   int d_ttl;
   int d_recursivettl;
   bool d_doRecursion;
index 4972c25a8d8eee62ef1367f8fbe5ca41dfd275c9..fb3bf2ef4eab49c9d14ec114d8a123ec64a2ac36 100644 (file)
@@ -48,12 +48,12 @@ extern PacketCache PC;
 extern CommunicatorClass Communicator;
 extern DNSProxy *DP;
 
-int PacketHandler::s_count;
+AtomicCounter PacketHandler::s_count;
 extern string s_programname;
 
 PacketHandler::PacketHandler():B(s_programname)
 {
-  s_count++;
+  ++s_count;
   d_doFancyRecords = (::arg()["fancy-records"]!="no");
   d_doRecursion= ::arg().mustDo("recursor");
   d_logDNSDetails= ::arg().mustDo("log-dns-details");
index d1191ac886602a468b35567d5d344ecff389cf6a..c01aa8cefb0e729e8bae796ab2b8dbab4c88d48f 100644 (file)
@@ -116,7 +116,7 @@ private:
   bool addDSforNS(DNSPacket* p, DNSPacket* r, SOAData& sd, const string& dsname);
   void completeANYRecords(DNSPacket *p, DNSPacket*r, SOAData& sd, const string &target);
   
-  static int s_count;
+  static AtomicCounter s_count;
   bool d_doFancyRecords;
   bool d_doRecursion;
   bool d_doCNAME;
index 8a40e4054b1fa76edae7a310ecac7893f773a94f..6c0e106fabd832435a1d78223ce13d95502cc0cc 100644 (file)
 #include <sstream>
 #include "qtype.hh"
 #include "misc.hh"
+#include "lock.hh"
 
+
+pthread_mutex_t QType::uninitlock = PTHREAD_MUTEX_INITIALIZER;
 bool QType::uninit=true;
 vector<QType::namenum> QType::names;
 
@@ -37,6 +40,7 @@ void QType::insert(const char *p, int n)
 
 QType::QType()
 {
+  Lock l(&uninitlock);
   if(uninit)
     {
       uninit=false;
index b596ce1880544208ca5fffcc47fd4c0b2e682421..1eb328b740232c141f0e0307f0dd1133bf6895bf 100644 (file)
@@ -86,7 +86,7 @@ private:
   uint16_t code;
   void insert(const char *p, int n);
 
-
+  static pthread_mutex_t uninitlock;
   static bool uninit;
 };
 
index 05362583b347abf0e4cc7f4c2ab9962b0edc047b..0c925e6e5381ccc06ce18c7fadda5079687b896b 100644 (file)
@@ -477,17 +477,17 @@ bool UeberBackend::list(const string &target, int domain_id)
 }
 
 
-int UeberBackend::handle::instances=0;
+AtomicCounter UeberBackend::handle::instances(0);
 
 UeberBackend::handle::handle()
 {
   //  L<<Logger::Warning<<"Handle instances: "<<instances<<endl;
-  instances++;
+  ++instances;
 }
 
 UeberBackend::handle::~handle()
 {
-  instances--;
+  --instances;
 }
 
 bool UeberBackend::handle::get(DNSResourceRecord &r)
index 3f82abe9d7cdfc3fbb657b05913aa045603309e8..3c83a5364f95a921009277de96bceb97f8965ffc 100644 (file)
@@ -107,7 +107,7 @@ public:
     QType qtype;
   private:
 
-    static int instances;
+    static AtomicCounter instances;
   };
 
   void lookup(const QType &, const string &qdomain, DNSPacket *pkt_p=0,  int zoneId=-1);