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 <unistd.h>
|
|
|
|
#include <common/logger_useful.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);
|
|
|
|
}
|
|
|
|
|
2021-02-15 19:54:23 +00:00
|
|
|
Epoll::Epoll(Epoll && other)
|
2021-02-15 13:21:36 +00:00
|
|
|
{
|
2021-02-15 19:54:23 +00:00
|
|
|
epoll_fd = other.epoll_fd;
|
2021-02-15 13:21:36 +00:00
|
|
|
other.epoll_fd = -1;
|
2021-02-15 19:54:23 +00:00
|
|
|
int count = other.events_count;
|
|
|
|
events_count = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Epoll & Epoll::operator=(Epoll && other)
|
|
|
|
{
|
|
|
|
epoll_fd = other.epoll_fd;
|
|
|
|
other.epoll_fd = -1;
|
|
|
|
int count = other.events_count;
|
|
|
|
events_count = count;
|
|
|
|
return *this;
|
2021-02-15 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 19:21:06 +00:00
|
|
|
void Epoll::add(int fd, void * ptr)
|
|
|
|
{
|
|
|
|
epoll_event event;
|
|
|
|
event.events = EPOLLIN | EPOLLPRI;
|
|
|
|
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-06 00:54:27 +00:00
|
|
|
size_t Epoll::getManyReady(int max_events, epoll_event * events_out, bool blocking, AsyncCallback async_callback) const
|
2021-01-19 19:21:06 +00:00
|
|
|
{
|
2021-02-15 13:21:36 +00:00
|
|
|
if (events_count == 0)
|
|
|
|
throw Exception("There is no events in epoll", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2021-02-15 19:54:23 +00:00
|
|
|
int ready_size;
|
2021-01-19 19:21:06 +00:00
|
|
|
int timeout = blocking && !async_callback ? -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);
|
|
|
|
|
|
|
|
if (ready_size == 0 && blocking && async_callback)
|
|
|
|
async_callback(epoll_fd, 0, "epoll");
|
|
|
|
}
|
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)
|
|
|
|
close(epoll_fd);
|
2021-01-19 19:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-01-29 15:46:28 +00:00
|
|
|
#endif
|