X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=stats.cpp;fp=stats.cpp;h=f5916edf2fb696530fe081cad9eee503457c8a70;hp=c9af688327be6d1e2a8745e05b68a68f598baa16;hb=76d52250fd5d7a84d9ae535fd7725d3a466bbb36;hpb=dad4769b3bf91bb45f577625c753994131df8a58 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; + } } }