2021-01-19 19:21:06 +00:00
|
|
|
#pragma once
|
2021-01-29 15:46:28 +00:00
|
|
|
#if defined(OS_LINUX)
|
2021-01-19 19:21:06 +00:00
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <Poco/Logger.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
using AsyncCallback = std::function<void(int, const Poco::Timespan &, const std::string &)>;
|
|
|
|
|
2021-02-15 13:21:36 +00:00
|
|
|
class Epoll
|
2021-01-19 19:21:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Epoll();
|
|
|
|
|
2021-02-15 13:21:36 +00:00
|
|
|
Epoll(const Epoll & other) = delete;
|
|
|
|
Epoll & operator=(const Epoll & other) = delete;
|
|
|
|
|
|
|
|
Epoll(Epoll && other);
|
|
|
|
|
|
|
|
Epoll & operator=(Epoll && other) = default;
|
|
|
|
|
2021-02-06 00:54:27 +00:00
|
|
|
/// Add new file descriptor to epoll. If ptr set to nullptr, epoll_event.data.fd = fd,
|
|
|
|
/// otherwise epoll_event.data.ptr = ptr.
|
2021-01-19 19:21:06 +00:00
|
|
|
void add(int fd, void * ptr = nullptr);
|
|
|
|
|
|
|
|
/// Remove file descriptor to epoll.
|
|
|
|
void remove(int fd);
|
|
|
|
|
2021-02-06 00:54:27 +00:00
|
|
|
/// Get events from epoll. Events are written in events_out, this function returns an amount of ready events.
|
|
|
|
/// If blocking is false and there are no ready events,
|
2021-01-19 19:21:06 +00:00
|
|
|
/// return empty vector, otherwise wait for ready events. If blocking is true,
|
|
|
|
/// async_callback is given and there is no ready events, async_callback is called
|
|
|
|
/// with epoll file descriptor.
|
2021-02-06 00:54:27 +00:00
|
|
|
size_t getManyReady(int max_events, epoll_event * events_out, bool blocking, AsyncCallback async_callback = {}) const;
|
2021-01-19 19:21:06 +00:00
|
|
|
|
|
|
|
int getFileDescriptor() const { return epoll_fd; }
|
|
|
|
|
|
|
|
int size() const { return events_count; }
|
|
|
|
|
2021-01-29 15:46:28 +00:00
|
|
|
bool empty() const { return events_count == 0; }
|
|
|
|
|
2021-01-19 19:21:06 +00:00
|
|
|
~Epoll();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int epoll_fd;
|
|
|
|
int events_count;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2021-01-29 15:46:28 +00:00
|
|
|
#endif
|