X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=stats.cpp;h=f5916edf2fb696530fe081cad9eee503457c8a70;hp=c9af688327be6d1e2a8745e05b68a68f598baa16;hb=bb0f713d1bc0b41d6a22e9990816ab4d11e642ed;hpb=a0949bd6af9a604e7073354fc530353bbdb7871c diff --git a/stats.cpp b/stats.cpp index c9af688..f5916ed 100644 --- a/stats.cpp +++ b/stats.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #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; + } } }