#pragma once #if defined(OS_LINUX) #include #include #include #include namespace DB { using AsyncCallback = std::function; class Epoll { public: Epoll(); Epoll(const Epoll & other) = delete; Epoll & operator=(const Epoll & other) = delete; Epoll(Epoll && other); Epoll & operator=(Epoll && other) = default; /// Add new file descriptor to epoll. If ptr set to nullptr, epoll_event.data.fd = fd, /// otherwise epoll_event.data.ptr = ptr. void add(int fd, void * ptr = nullptr); /// Remove file descriptor to epoll. void remove(int fd); /// 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, /// 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. size_t getManyReady(int max_events, epoll_event * events_out, bool blocking, AsyncCallback async_callback = {}) const; int getFileDescriptor() const { return epoll_fd; } int size() const { return events_count; } bool empty() const { return events_count == 0; } ~Epoll(); private: int epoll_fd; int events_count; }; } #endif