Commit 7fe66f90 authored by Dominik Charousset's avatar Dominik Charousset

take some load off select() by reading chunks from pipe

parent 81634aa2
......@@ -555,11 +555,20 @@ class middleman_overseer : public network_channel {
bool continue_reading() {
DEBUG("middleman_overseer::continue_reading");
uint32_t dummy;
if (::read(read_handle(), &dummy, sizeof(dummy)) != sizeof(dummy)) {
static constexpr size_t num_dummies = 256;
uint8_t dummies[num_dummies];
auto read_result = ::read(read_handle(), dummies, num_dummies);
if (read_result < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// try again later
return true;
}
else {
CPPA_CRITICAL("cannot read from pipe");
}
}
atomic_thread_fence(memory_order_seq_cst);
for (int i = 0; i < read_result; ++i) {
unique_ptr<middleman_message> msg(m_queue.try_pop());
if (!msg) { CPPA_CRITICAL("nullptr dequeued"); }
switch (msg->type) {
......@@ -625,6 +634,7 @@ class middleman_overseer : public network_channel {
break;
}
}
}
return true;
}
......
......@@ -65,6 +65,14 @@ struct network_manager_impl : network_manager {
}
// store pipe read handle in local variables for lambda expression
int pipe_fd0 = pipe_fd[0];
// set read handle to nonblocking
auto flags = fcntl(pipe_fd0, F_GETFL, 0);
if (flags == -1) {
throw network_error("unable to read socket flags");
}
if (fcntl(pipe_fd0, F_SETFL, flags | O_NONBLOCK) < 0) {
CPPA_CRITICAL("unable to set pipe read handle to nonblock");
}
// start threads
m_middleman_thread = std::thread([this, pipe_fd0] {
middleman_loop(pipe_fd0, this->m_middleman_queue);
......@@ -82,7 +90,7 @@ struct network_manager_impl : network_manager {
void send_to_middleman(std::unique_ptr<middleman_message> msg) {
m_middleman_queue._push_back(msg.release());
std::atomic_thread_fence(std::memory_order_seq_cst);
std::uint32_t dummy = 0;
std::uint8_t dummy = 0;
if (write(pipe_fd[1], &dummy, sizeof(dummy)) != sizeof(dummy)) {
CPPA_CRITICAL("cannot write to pipe");
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment