ClickHouse/src/Common/Epoll.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.0 KiB
C++
Raw Normal View History

2021-01-29 15:46:28 +00:00
#if defined(OS_LINUX)
2021-01-19 19:21:06 +00:00
#include "Epoll.h"
#include <Common/Exception.h>
#include <base/defines.h>
2021-01-19 19:21:06 +00:00
#include <unistd.h>
namespace DB
{
namespace ErrorCodes
{
extern const int EPOLL_ERROR;
2021-02-15 14:44:05 +00:00
extern const int LOGICAL_ERROR;
2021-01-19 19:21:06 +00:00
}
Epoll::Epoll() : events_count(0)
{
epoll_fd = epoll_create1(0);
if (epoll_fd == -1)
throwFromErrno("Cannot open epoll descriptor", DB::ErrorCodes::EPOLL_ERROR);
}
Epoll::Epoll(Epoll && other) noexcept : epoll_fd(other.epoll_fd), events_count(other.events_count.load())
2021-02-15 13:21:36 +00:00
{
other.epoll_fd = -1;
2021-02-15 19:54:23 +00:00
}
Epoll & Epoll::operator=(Epoll && other) noexcept
2021-02-15 19:54:23 +00:00
{
epoll_fd = other.epoll_fd;
other.epoll_fd = -1;
2021-02-17 17:34:52 +00:00
events_count.store(other.events_count.load());
2021-02-15 19:54:23 +00:00
return *this;
2021-02-15 13:21:36 +00:00
}
2023-03-03 19:30:43 +00:00
void Epoll::add(int fd, void * ptr, uint32_t events)
2021-01-19 19:21:06 +00:00
{
epoll_event event;
2023-03-03 19:30:43 +00:00
event.events = events | EPOLLPRI;
2021-01-19 19:21:06 +00:00
if (ptr)
event.data.ptr = ptr;
else
event.data.fd = fd;
2021-02-15 19:54:23 +00:00
++events_count;
2021-01-19 19:21:06 +00:00
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1)
throwFromErrno("Cannot add new descriptor to epoll", DB::ErrorCodes::EPOLL_ERROR);
}
void Epoll::remove(int fd)
{
2021-02-15 19:54:23 +00:00
--events_count;
2021-01-19 19:21:06 +00:00
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == -1)
throwFromErrno("Cannot remove descriptor from epoll", DB::ErrorCodes::EPOLL_ERROR);
}
2021-02-21 14:03:24 +00:00
size_t Epoll::getManyReady(int max_events, epoll_event * events_out, bool blocking) const
2021-01-19 19:21:06 +00:00
{
2021-02-15 13:21:36 +00:00
if (events_count == 0)
throw Exception(ErrorCodes::LOGICAL_ERROR, "There are no events in epoll");
2021-02-15 13:21:36 +00:00
2021-02-15 19:54:23 +00:00
int ready_size;
2021-02-21 14:03:24 +00:00
int timeout = blocking ? -1 : 0;
2021-02-06 00:54:27 +00:00
do
2021-01-19 19:21:06 +00:00
{
2021-02-06 00:54:27 +00:00
ready_size = epoll_wait(epoll_fd, events_out, max_events, timeout);
2021-01-19 19:21:06 +00:00
if (ready_size == -1 && errno != EINTR)
throwFromErrno("Error in epoll_wait", DB::ErrorCodes::EPOLL_ERROR);
}
2021-02-06 00:54:27 +00:00
while (ready_size <= 0 && (ready_size != 0 || blocking));
2021-01-19 19:21:06 +00:00
2021-02-06 00:54:27 +00:00
return ready_size;
2021-01-19 19:21:06 +00:00
}
Epoll::~Epoll()
{
2021-02-15 13:21:36 +00:00
if (epoll_fd != -1)
{
int err = close(epoll_fd);
chassert(!err || errno == EINTR);
}
2021-01-19 19:21:06 +00:00
}
}
2021-01-29 15:46:28 +00:00
#endif