]> git.sesse.net Git - cubemap/blobdiff - stats.cpp
Signal thread stop through a pipe; fixes issues where the statistics thread would...
[cubemap] / stats.cpp
index c9af688327be6d1e2a8745e05b68a68f598baa16..f5916edf2fb696530fe081cad9eee503457c8a70 100644 (file)
--- a/stats.cpp
+++ b/stats.cpp
@@ -5,6 +5,8 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <sys/poll.h>
+#include <errno.h>
 #include <vector>
 
 #include "client.h"
@@ -71,9 +73,25 @@ void StatsThread::do_work()
                }
 
 sleep:
-               int left_to_sleep = stats_interval;
-               do {
-                       left_to_sleep = sleep(left_to_sleep);
-               } while (left_to_sleep > 0 && !should_stop);
+               // Wait until the stop_fd pipe is closed, stats_interval timeout,
+               // or a spurious signal. (The latter will cause us to write stats
+               // too often, but that's okay.)
+               pollfd pfd;
+               pfd.fd = stop_fd_read;
+               pfd.events = POLLIN | POLLRDHUP;
+
+               int nfds = poll(&pfd, 1, stats_interval * 1000);
+               if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
+                       continue;
+               }
+               if (nfds == 1) {
+                       // Should stop.
+                       break;
+               }
+               if (nfds == -1) {
+                       perror("poll");
+                       usleep(100000);
+                       continue;
+               }
        }
 }