From 07c5e5661d1cf5717052ef5b92d50e1ecd178838 Mon Sep 17 00:00:00 2001 From: flamingcow Date: Sun, 12 May 2019 16:18:07 -0700 Subject: [PATCH] Tigher loop in KeepAlive; skip syscalls if overloaded --- firecgi | 2 +- keepalive.cc | 13 +++++++------ keepalive.h | 7 ++++++- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/firecgi b/firecgi index 0ba446b..a39ef8e 160000 --- a/firecgi +++ b/firecgi @@ -1 +1 @@ -Subproject commit 0ba446bacb1cedaacea0f8347c0a068f652e04cf +Subproject commit a39ef8e25dc2450b9219da67e37cfe158bd4a3ec diff --git a/keepalive.cc b/keepalive.cc index a4333c9..0c53128 100644 --- a/keepalive.cc +++ b/keepalive.cc @@ -8,12 +8,12 @@ namespace firesse { KeepAlive::KeepAlive(const std::chrono::nanoseconds& max_stale, Index* index) : max_stale_(max_stale), index_(index), - shutdown_(eventfd(0, 0)) { - PCHECK(shutdown_ >= 0) << "eventfd()"; + shutdown_fd_(eventfd(0, 0)) { + PCHECK(shutdown_fd_ >= 0) << "eventfd()"; } KeepAlive::~KeepAlive() { - PCHECK(close(shutdown_) == 0); + PCHECK(close(shutdown_fd_) == 0); } void KeepAlive::Start() { @@ -22,11 +22,11 @@ void KeepAlive::Start() { constexpr auto num_fds = 1; pollfd fds[num_fds] = { { - .fd = shutdown_, + .fd = shutdown_fd_, .events = POLLIN, }, }; - while (poll(fds, num_fds, timeout) <= 0) { + while (running_ && (timeout == 0 || poll(fds, num_fds, timeout) <= 0)) { auto sleep = index_->WithStalest([](Stream* stream) { stream->WriteRaw(":\n"); }, max_stale_); @@ -38,8 +38,9 @@ void KeepAlive::Start() { void KeepAlive::Stop() { CHECK(thread_.joinable()); + running_ = false; uint64_t shutdown = 1; - PCHECK(write(shutdown_, &shutdown, sizeof(shutdown)) == sizeof(shutdown)); + PCHECK(write(shutdown_fd_, &shutdown, sizeof(shutdown)) == sizeof(shutdown)); thread_.join(); } diff --git a/keepalive.h b/keepalive.h index fe75e4e..5396370 100644 --- a/keepalive.h +++ b/keepalive.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "index.h" @@ -17,7 +18,11 @@ class KeepAlive { private: const std::chrono::nanoseconds max_stale_; Index* index_; - int shutdown_; + + // Two shutdown mechanisms, one for if we're in a tight no-syscall loop, + // and one for if we're sleeping in poll() + int shutdown_fd_; + std::atomic running_ = true; std::thread thread_; };