init: epoll: add events argument to RegisterHandler

Allow caller to specify events other than EPOLLIN default.

Test: boot
Bug: 124072565
Change-Id: Id4e582a6abc74c5fdb26fea7dcbd3ba2150dadd6
This commit is contained in:
Mark Salyzyn 2019-03-13 07:25:52 -07:00
parent 92dcfb3187
commit 37bbf800fa
2 changed files with 11 additions and 3 deletions

View File

@ -16,6 +16,7 @@
#include "epoll.h"
#include <stdint.h>
#include <sys/epoll.h>
#include <chrono>
@ -37,13 +38,16 @@ Result<Success> Epoll::Open() {
return Success();
}
Result<Success> Epoll::RegisterHandler(int fd, std::function<void()> handler) {
Result<Success> Epoll::RegisterHandler(int fd, std::function<void()> handler, uint32_t events) {
if (!events) {
return Error() << "Must specify events";
}
auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(handler));
if (!inserted) {
return Error() << "Cannot specify two epoll handlers for a given FD";
}
epoll_event ev;
ev.events = EPOLLIN;
ev.events = events;
// std::map's iterators do not get invalidated until erased, so we use the
// pointer to the std::function in the map directly for epoll_ctl.
ev.data.ptr = reinterpret_cast<void*>(&it->second);

View File

@ -17,6 +17,9 @@
#ifndef _INIT_EPOLL_H
#define _INIT_EPOLL_H
#include <stdint.h>
#include <sys/epoll.h>
#include <chrono>
#include <functional>
#include <map>
@ -34,7 +37,8 @@ class Epoll {
Epoll();
Result<Success> Open();
Result<Success> RegisterHandler(int fd, std::function<void()> handler);
Result<Success> RegisterHandler(int fd, std::function<void()> handler,
uint32_t events = EPOLLIN);
Result<Success> UnregisterHandler(int fd);
Result<Success> Wait(std::optional<std::chrono::milliseconds> timeout);