]> granicus.if.org Git - pdns/commitdiff
Do not busy loop if we get lots of notifies. Also rewrite
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 7 May 2019 09:37:10 +0000 (11:37 +0200)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Tue, 7 May 2019 09:37:10 +0000 (11:37 +0200)
main slave loop to use a timed wait for the semaphore.

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

index 709b8d05fa449850ee53409207f7a9064db17541..3e2ce98bbe24169d1427dd7aa14f12dbb207ebb0 100644 (file)
@@ -113,37 +113,19 @@ void CommunicatorClass::mainloop(void)
     d_tickinterval=::arg().asNum("slave-cycle-interval");
     makeNotifySockets();
 
-    int rc;
-    time_t next, tick;
-
-    for(;;) {
+    for (;;) {
       slaveRefresh(&P);
       masterUpdateCheck(&P);
-      tick=doNotifications(); // this processes any notification acknowledgements and actually send out our own notifications
-      
-      tick = min (tick, d_tickinterval); 
-      
-      next=time(0)+tick;
 
-      while(time(0) < next) {
-        rc=d_any_sem.tryWait();
+      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};
+
+      // Wait for a post for a max time. We might get EINTR, oh well
+      d_any_sem.timedWait(abs_time);
 
-        if(rc) {
-          bool extraSlaveRefresh = false;
-          Utility::sleep(1);
-          {
-            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();
+      while (d_any_sem.tryWait() == 0) {
+       // eat up remaining posts, will do next iteration shortly
       }
     }
   }
index a4b25b6dd0a1ed87289ffb90bc2f1d4241930ee4..bac933c0ef6b7f0fe0ee792b53305c549fe85012 100644 (file)
@@ -168,6 +168,12 @@ 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 024fc089fb111a5b6f66f239e60d2f32ebb39cca..30ea9323099c15d01a1d478a2ad209258467465a 100644 (file)
@@ -78,6 +78,9 @@ 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 );