#ifndef ANDROID_DVR_SERVICES_DISPLAYD_EPOLL_EVENT_DISPATCHER_H_ #define ANDROID_DVR_SERVICES_DISPLAYD_EPOLL_EVENT_DISPATCHER_H_ #include #include #include #include #include #include #include #include #include namespace android { namespace dvr { class EpollEventDispatcher { public: // Function type for event handlers. The handler receives a bitmask of the // epoll events that occurred on the file descriptor associated with the // handler. using Handler = std::function; EpollEventDispatcher(); ~EpollEventDispatcher(); // |handler| is called on the internal dispatch thread when |fd| is signaled // by events in |event_mask|. pdx::Status AddEventHandler(int fd, int event_mask, Handler handler); pdx::Status RemoveEventHandler(int fd); void Stop(); private: void EventThread(); std::thread thread_; std::atomic exit_thread_{false}; // Protects handlers_ and removed_handlers_ and serializes operations on // epoll_fd_ and event_fd_. std::mutex lock_; // Maintains a map of fds to event handlers. This is primarily to keep any // references alive that may be bound in the std::function instances. It is // not used at dispatch time to avoid performance problems with different // versions of std::unordered_map. std::unordered_map handlers_; // List of fds to be removed from the map. The actual removal is performed // by the event dispatch thread to avoid races. std::vector removed_handlers_; pdx::LocalHandle epoll_fd_; pdx::LocalHandle event_fd_; }; } // namespace dvr } // namespace android #endif // ANDROID_DVR_SERVICES_DISPLAYD_EPOLL_EVENT_DISPATCHER_H_