Merge "init: epoll: add events argument to RegisterHandler"

This commit is contained in:
Mark Salyzyn 2019-03-14 14:11:27 +00:00 committed by Gerrit Code Review
commit 873ee642f2
2 changed files with 11 additions and 3 deletions

View file

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

View file

@ -17,6 +17,9 @@
#ifndef _INIT_EPOLL_H #ifndef _INIT_EPOLL_H
#define _INIT_EPOLL_H #define _INIT_EPOLL_H
#include <stdint.h>
#include <sys/epoll.h>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <map> #include <map>
@ -34,7 +37,8 @@ class Epoll {
Epoll(); Epoll();
Result<Success> Open(); 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> UnregisterHandler(int fd);
Result<Success> Wait(std::optional<std::chrono::milliseconds> timeout); Result<Success> Wait(std::optional<std::chrono::milliseconds> timeout);