ClickHouse/src/Common/Epoll.h

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

56 lines
1.5 KiB
C++
Raw Normal View History

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 <functional>
2021-01-19 19:21:06 +00:00
#include <boost/noncopyable.hpp>
#include <Poco/Logger.h>
namespace DB
{
2021-02-15 13:21:36 +00:00
class Epoll
2021-01-19 19:21:06 +00:00
{
public:
Epoll();
2021-02-15 19:54:23 +00:00
Epoll(const Epoll &) = delete;
Epoll & operator=(const Epoll &) = delete;
2021-02-15 13:21:36 +00:00
Epoll & operator=(Epoll && other) noexcept;
Epoll(Epoll && other) noexcept;
2021-02-15 13:21:36 +00:00
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.
2023-03-03 19:30:43 +00:00
/// Default events are for reading from fd and for errors.
void add(int fd, void * ptr = nullptr, uint32_t events = EPOLLIN | EPOLLERR);
void add(int fd, uint32_t events) { add(fd, nullptr, events); }
2021-01-19 19:21:06 +00:00
/// 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-02-21 14:03:24 +00:00
/// return empty vector, otherwise wait for ready events.
size_t getManyReady(int max_events, epoll_event * events_out, bool blocking) 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-02-21 14:03:24 +00:00
const std::string & getDescription() const { return fd_description; }
2021-01-19 19:21:06 +00:00
~Epoll();
private:
int epoll_fd;
2021-02-15 19:54:23 +00:00
std::atomic<int> events_count;
2021-02-21 14:03:24 +00:00
const std::string fd_description = "epoll";
2021-01-19 19:21:06 +00:00
};
}
2021-01-29 15:46:28 +00:00
#endif