Files
unison/src/core/model/unix-fd-reader.cc

226 lines
4.9 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2010 The Boeing Company
*
2024-06-17 16:17:10 +02:00
* SPDX-License-Identifier: GPL-2.0-only
*
* Author: Tom Goff <thomas.goff@boeing.com>
*/
#include "fatal-error.h"
#include "fd-reader.h"
2022-10-07 20:08:35 +00:00
#include "log.h"
#include "simple-ref-count.h"
#include "simulator.h"
2022-10-07 20:08:35 +00:00
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <sys/select.h>
#include <thread>
#include <unistd.h> // close()
2014-12-18 15:12:35 -08:00
/**
2024-11-08 18:05:46 +00:00
* @file
* @ingroup system
2014-12-18 15:12:35 -08:00
* ns3::FdReader implementation.
*/
2022-10-07 20:08:35 +00:00
namespace ns3
{
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("FdReader");
2022-10-07 20:08:35 +00:00
FdReader::FdReader()
: m_fd(-1),
m_stop(false),
m_destroyEvent()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
m_evpipe[0] = -1;
m_evpipe[1] = -1;
}
2022-10-07 20:08:35 +00:00
FdReader::~FdReader()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
Stop();
}
2022-10-07 20:08:35 +00:00
void
FdReader::Start(int fd, Callback<void, uint8_t*, ssize_t> readCallback)
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this << fd << &readCallback);
int tmp;
2022-10-07 20:08:35 +00:00
NS_ASSERT_MSG(!m_readThread.joinable(), "read thread already exists");
2022-10-07 20:08:35 +00:00
// create a pipe for inter-thread event notification
tmp = pipe(m_evpipe);
if (tmp == -1)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("pipe() failed: " << std::strerror(errno));
}
2022-10-07 20:08:35 +00:00
// make the read end non-blocking
tmp = fcntl(m_evpipe[0], F_GETFL);
if (tmp == -1)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
}
2022-10-07 20:08:35 +00:00
if (fcntl(m_evpipe[0], F_SETFL, tmp | O_NONBLOCK) == -1)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("fcntl() failed: " << std::strerror(errno));
}
2022-10-07 20:08:35 +00:00
m_fd = fd;
m_readCallback = readCallback;
2022-10-07 20:08:35 +00:00
//
// We're going to spin up a thread soon, so we need to make sure we have
// a way to tear down that thread when the simulation stops. Do this by
// scheduling a "destroy time" method to make sure the thread exits before
// proceeding.
//
if (!m_destroyEvent.IsPending())
{
2022-10-07 20:08:35 +00:00
// hold a reference to ensure that this object is not
// deallocated before the destroy-time event fires
this->Ref();
m_destroyEvent = Simulator::ScheduleDestroy(&FdReader::DestroyEvent, this);
}
2022-10-07 20:08:35 +00:00
//
// Now spin up a thread to read from the fd
//
NS_LOG_LOGIC("Spinning up read thread");
2022-10-07 20:08:35 +00:00
m_readThread = std::thread(&FdReader::Run, this);
}
2022-10-07 20:08:35 +00:00
void
FdReader::DestroyEvent()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
Stop();
this->Unref();
}
2022-10-07 20:08:35 +00:00
void
FdReader::Stop()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
m_stop = true;
2022-10-07 20:08:35 +00:00
// signal the read thread
if (m_evpipe[1] != -1)
{
2022-10-07 20:08:35 +00:00
char zero = 0;
ssize_t len = write(m_evpipe[1], &zero, sizeof(zero));
if (len != sizeof(zero))
2020-03-25 15:12:18 -07:00
{
2022-10-07 20:08:35 +00:00
NS_LOG_WARN("incomplete write(): " << std::strerror(errno));
2020-03-25 15:12:18 -07:00
}
}
2022-10-07 20:08:35 +00:00
// join the read thread
if (m_readThread.joinable())
{
2022-10-07 20:08:35 +00:00
m_readThread.join();
}
2022-10-07 20:08:35 +00:00
// close the write end of the event pipe
if (m_evpipe[1] != -1)
{
2022-10-07 20:08:35 +00:00
close(m_evpipe[1]);
m_evpipe[1] = -1;
}
2022-10-07 20:08:35 +00:00
// close the read end of the event pipe
if (m_evpipe[0] != -1)
{
2022-10-07 20:08:35 +00:00
close(m_evpipe[0]);
m_evpipe[0] = -1;
}
2022-10-07 20:08:35 +00:00
// reset everything else
m_fd = -1;
m_readCallback.Nullify();
m_stop = false;
}
// This runs in a separate thread
2022-10-07 20:08:35 +00:00
void
FdReader::Run()
{
2022-10-07 20:08:35 +00:00
NS_LOG_FUNCTION(this);
int nfds;
fd_set rfds;
2022-10-07 20:08:35 +00:00
nfds = (m_fd > m_evpipe[0] ? m_fd : m_evpipe[0]) + 1;
2022-10-07 20:08:35 +00:00
FD_ZERO(&rfds);
FD_SET(m_fd, &rfds);
FD_SET(m_evpipe[0], &rfds);
2022-10-07 20:08:35 +00:00
for (;;)
{
2022-10-07 20:08:35 +00:00
int r;
fd_set readfds = rfds;
2022-10-07 20:08:35 +00:00
r = select(nfds, &readfds, nullptr, nullptr, nullptr);
if (r == -1 && errno != EINTR)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("select() failed: " << std::strerror(errno));
2011-05-13 14:52:27 -04:00
}
2022-10-07 20:08:35 +00:00
if (FD_ISSET(m_evpipe[0], &readfds))
{
2022-10-07 20:08:35 +00:00
// drain the event pipe
for (;;)
{
2022-10-07 20:08:35 +00:00
char buf[1024];
ssize_t len = read(m_evpipe[0], buf, sizeof(buf));
if (len == 0)
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("event pipe closed");
}
2022-10-07 20:08:35 +00:00
if (len < 0)
{
2022-10-07 20:08:35 +00:00
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)
{
2022-10-07 20:08:35 +00:00
break;
}
2022-10-07 20:08:35 +00:00
else
{
2022-10-07 20:08:35 +00:00
NS_FATAL_ERROR("read() failed: " << std::strerror(errno));
}
}
}
2011-05-13 14:52:27 -04:00
}
2022-10-07 20:08:35 +00:00
if (m_stop)
{
2022-10-07 20:08:35 +00:00
// this thread is done
break;
}
2022-10-07 20:08:35 +00:00
if (FD_ISSET(m_fd, &readfds))
{
2023-05-13 19:49:26 +00:00
FdReader::Data data = DoRead();
2022-10-07 20:08:35 +00:00
// reading stops when m_len is zero
if (data.m_len == 0)
{
2022-10-07 20:08:35 +00:00
break;
}
2022-10-07 20:08:35 +00:00
// the callback is only called when m_len is positive (data
// is ignored if m_len is negative)
else if (data.m_len > 0)
{
2022-10-07 20:08:35 +00:00
m_readCallback(data.m_buf, data.m_len);
}
}
}
}
} // namespace ns3