Browse Source

add max-poll-cycle

David Rose 20 years ago
parent
commit
8390e00363
3 changed files with 43 additions and 3 deletions
  1. 19 0
      panda/src/net/config_net.cxx
  2. 1 0
      panda/src/net/config_net.h
  3. 23 3
      panda/src/net/connectionReader.cxx

+ 19 - 0
panda/src/net/config_net.cxx

@@ -101,3 +101,22 @@ get_net_error_abort() {
 
 
   return *net_error_abort;
   return *net_error_abort;
 }
 }
+
+double
+get_max_poll_cycle() {
+  static ConfigVariableDouble *max_poll_cycle = NULL;
+
+  if (max_poll_cycle == (ConfigVariableDouble *)NULL) {
+    max_poll_cycle = new ConfigVariableDouble
+      ("max-poll-cycle", 0.2,
+       PRC_DESC("Specifies the maximum amount of time, in seconds, to "
+		"continue to read data within one cycle of the poll() "
+		"call.  If this is negative, the program will wait as "
+		"long as data is available to be read.  Setting this to "
+		"a reasonable value prevents poll() from completely "
+		"starving the rest of the application when data is coming "
+		"in faster than it can be processed."));
+  }
+
+  return *max_poll_cycle;
+}

+ 1 - 0
panda/src/net/config_net.h

@@ -31,6 +31,7 @@ NotifyCategoryDecl(net, EXPCL_PANDA, EXPTP_PANDA);
 extern int get_net_max_write_queue();
 extern int get_net_max_write_queue();
 extern int get_net_max_response_queue();
 extern int get_net_max_response_queue();
 extern bool get_net_error_abort();
 extern bool get_net_error_abort();
+extern double get_max_poll_cycle();
 
 
 extern EXPCL_PANDA void init_libnet();
 extern EXPCL_PANDA void init_libnet();
 
 

+ 23 - 3
panda/src/net/connectionReader.cxx

@@ -23,6 +23,7 @@
 #include "datagramUDPHeader.h"
 #include "datagramUDPHeader.h"
 #include "pprerror.h"
 #include "pprerror.h"
 #include "config_net.h"
 #include "config_net.h"
+#include "clockObject.h"
 
 
 #include "notify.h"
 #include "notify.h"
 #include <prerror.h>
 #include <prerror.h>
@@ -299,9 +300,28 @@ poll() {
   }
   }
 
 
   SocketInfo *sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
   SocketInfo *sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
-  while (sinfo != (SocketInfo *)NULL) {
-    process_incoming_data(sinfo);
-    sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
+  if (sinfo != (SocketInfo *)NULL) {
+    double max_poll_cycle = get_max_poll_cycle();
+    if (max_poll_cycle < 0.0) {
+      // Continue to read all data.
+      while (sinfo != (SocketInfo *)NULL) {
+	process_incoming_data(sinfo);
+	sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
+      }
+
+    } else {
+      // Read only until a certain amount of time has elapsed.
+      ClockObject *global_clock = ClockObject::get_global_clock();
+      double stop = global_clock->get_real_time() + max_poll_cycle;
+
+      process_incoming_data(sinfo);
+      sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
+      while (sinfo != (SocketInfo *)NULL && 
+	     global_clock->get_real_time() < stop) {
+	process_incoming_data(sinfo);
+	sinfo = get_next_available_socket(PR_INTERVAL_NO_WAIT, -2);
+      }
+    }
   }
   }
 }
 }