limit maximum timeout for select()
Stefan Schuermans

Stefan Schuermans commited on 2018-09-02 10:43:07
Showing 2 changed files, with 11 additions and 2 deletions.

... ...
@@ -21,6 +21,8 @@ namespace Blinker {
21 21
  */
22 22
 void Io::wait(Set &read, Set &write, const Time &timeout)
23 23
 {
24
+  static const Time maxTimeout(0.5); // maximum timeout for I/O wait
25
+
24 26
   // get maximum file descriptor, read set and write set
25 27
   int max = 0;
26 28
   fd_set fd_rd;
... ...
@@ -40,10 +42,13 @@ void Io::wait(Set &read, Set &write, const Time &timeout)
40 42
 
41 43
   // get timeout
42 44
   struct timeval to;
43
-  if (timeout < Time::zero) // don't use negaitve timeout
45
+  if (timeout < Time::zero) { // don't use negative timeout
44 46
     Time::zero.toTimeval(to);
45
-  else
47
+  } else if (timeout > maxTimeout) { // stay responsive -> no more than max
48
+    maxTimeout.toTimeval(to);
49
+  } else {
46 50
     timeout.toTimeval(to);
51
+  }
47 52
 
48 53
   // wait for I/O event
49 54
   fd_set fd_err;
... ...
@@ -21,6 +21,8 @@ namespace Blinker {
21 21
  */
22 22
 void Io::wait(Set &read, Set &write, const Time &timeout)
23 23
 {
24
+  static const Time maxTimeout(0.5); // maximum timeout for I/O wait
25
+
24 26
   // get read set and write set, also count entries
25 27
   unsigned int rds = 0;
26 28
   fd_set fd_rd;
... ...
@@ -54,6 +56,8 @@ void Io::wait(Set &read, Set &write, const Time &timeout)
54 56
   struct timeval to;
55 57
   if (timeout < Time::zero) { // don't use negative timeout
56 58
     Time::zero.toTimeval(to);
59
+  } else if (timeout > maxTimeout) { // stay responsive -> no more than max
60
+    maxTimeout.toTimeval(to);
57 61
   } else {
58 62
     timeout.toTimeval(to);
59 63
   }
60 64