Tigher loop in KeepAlive; skip syscalls if overloaded

This commit is contained in:
flamingcow
2019-05-12 16:18:07 -07:00
parent 6efb19f255
commit 07c5e5661d
3 changed files with 14 additions and 8 deletions

Submodule firecgi updated: 0ba446bacb...a39ef8e25d

View File

@@ -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();
}

View File

@@ -1,5 +1,6 @@
#pragma once
#include <atomic>
#include <thread>
#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<bool> running_ = true;
std::thread thread_;
};