]> granicus.if.org Git - pdns/commitdiff
Use a condition variable and restore the loop to be more like the original code.
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 10 May 2019 08:26:15 +0000 (10:26 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 10 May 2019 08:26:15 +0000 (10:26 +0200)
Separating slave and master comms threads can come later.

pdns/communicator.cc
pdns/communicator.hh
pdns/mastercommunicator.cc
pdns/slavecommunicator.cc
pdns/unix_semaphore.cc
pdns/utility.hh

index 3e2ce98bbe24169d1427dd7aa14f12dbb207ebb0..836f07e59a18e33096de30f763d3b6a856d22ae4 100644 (file)
@@ -113,19 +113,34 @@ void CommunicatorClass::mainloop(void)
     d_tickinterval=::arg().asNum("slave-cycle-interval");
     makeNotifySockets();
 
-    for (;;) {
+    for(;;) {
       slaveRefresh(&P);
       masterUpdateCheck(&P);
 
       time_t tick = doNotifications(); // this processes any notification acknowledgements and actually send out our own notifications
       tick = min(tick, d_tickinterval);
-      struct timespec abs_time = {.tv_sec = time(0) + tick, .tv_nsec = 0};
+      time_t next = time(0) + tick;
 
-      // Wait for a post for a max time. We might get EINTR, oh well
-      d_any_sem.timedWait(abs_time);
+      while (time(0) < next) {
+        std::unique_lock<std::mutex> lk(d_any_mutex);
+        auto rc = d_any_condvar.wait_for(lk, std::chrono::seconds(1));
+        lk.unlock();
 
-      while (d_any_sem.tryWait() == 0) {
-       // eat up remaining posts, will do next iteration shortly
+        if (rc == std::cv_status::timeout) {
+          bool extraSlaveRefresh = false;
+          {
+            Lock l(&d_lock);
+            if (d_tocheck.size())
+              extraSlaveRefresh = true;
+          }
+          if (extraSlaveRefresh)
+            slaveRefresh(&P);
+        }
+        else {
+          break; // something happened
+        }
+        // this gets executed at least once every second
+        doNotifications();
       }
     }
   }
index c63534dc232b5fa709eba136905c9bfcde208645..d7d7178a7fea0f0c5cc04a94d5ebb07ec5fd44c5 100644 (file)
@@ -25,6 +25,7 @@
 #include <pthread.h>
 #include <string>
 #include <semaphore.h>
+#include <condition_variable>
 #include <queue>
 #include <list>
 #include <limits>
@@ -205,7 +206,8 @@ private:
   set<DNSName> d_inprogress;
   
   Semaphore d_suck_sem;
-  Semaphore d_any_sem;
+  std::condition_variable d_any_condvar;
+  std::mutex d_any_mutex;
   time_t d_tickinterval;
   set<DomainInfo> d_tocheck;
   struct cmp {
index 04803474994a64e63d7294265b30260d6ed0a768..5d6c97459b4584da0285fdd46a3e3faf87dd9a68 100644 (file)
@@ -303,5 +303,5 @@ void CommunicatorClass::makeNotifySockets()
 void CommunicatorClass::notify(const DNSName &domain, const string &ip)
 {
   d_nq.add(domain, ip);
-  d_any_sem.post();
+  d_any_condvar.notify_one();
 }
index 190488faf5d74c2493ba6937597bd806c6695bb5..50cf01c0b36dff3886d76687282489682ca6f774 100644 (file)
@@ -727,7 +727,7 @@ void CommunicatorClass::addSlaveCheckRequest(const DomainInfo& di, const ComboAd
   }
   d_tocheck.erase(di);
   d_tocheck.insert(ours);
-  d_any_sem.post(); // kick the loop!
+  d_any_condvar.notify_one(); // kick the loop!
 }
 
 void CommunicatorClass::addTrySuperMasterRequest(DNSPacket *p)
@@ -735,7 +735,7 @@ void CommunicatorClass::addTrySuperMasterRequest(DNSPacket *p)
   Lock l(&d_lock);
   DNSPacket ours = *p;
   if(d_potentialsupermasters.insert(ours).second)
-    d_any_sem.post(); // kick the loop!
+    d_any_condvar.notify_one(); // kick the loop!
 }
 
 void CommunicatorClass::slaveRefresh(PacketHandler *P)
index bac933c0ef6b7f0fe0ee792b53305c549fe85012..a4b25b6dd0a1ed87289ffb90bc2f1d4241930ee4 100644 (file)
@@ -168,12 +168,6 @@ int Semaphore::wait()
   while (ret == -1 && errno == EINTR);
   return ret;
 }
-
-int Semaphore::timedWait(const struct timespec& abs_timeout)
-{
- return sem_timedwait(m_pSemaphore, &abs_timeout);
-}
-
 int Semaphore::tryWait()
 {
   return sem_trywait(m_pSemaphore);
index 30ea9323099c15d01a1d478a2ad209258467465a..024fc089fb111a5b6f66f239e60d2f32ebb39cca 100644 (file)
@@ -78,9 +78,6 @@ public:
   //! Waits for a semaphore.
   int wait( void );
 
-  //! Waits for a semaphore with a timeout
-  int timedWait(const struct timespec &abs_timeout);
-
   //! Tries to wait for a semaphore.
   int tryWait( void );