blob: ca8426634e6666184b81c8d57e1046643c50d82c [file] [log] [blame]
Mark Salyzyn6c6ec722015-10-24 16:20:18 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _INIT_EPOLL_H
18#define _INIT_EPOLL_H
19
Mark Salyzyn37bbf802019-03-13 07:25:52 -070020#include <stdint.h>
21#include <sys/epoll.h>
22
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070023#include <chrono>
24#include <functional>
25#include <map>
26#include <optional>
27
28#include <android-base/unique_fd.h>
29
30#include "result.h"
31
32namespace android {
33namespace init {
34
35class Epoll {
36 public:
37 Epoll();
38
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070039 Result<void> Open();
40 Result<void> RegisterHandler(int fd, std::function<void()> handler, uint32_t events = EPOLLIN);
41 Result<void> UnregisterHandler(int fd);
42 Result<void> Wait(std::optional<std::chrono::milliseconds> timeout);
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070043
44 private:
45 android::base::unique_fd epoll_fd_;
46 std::map<int, std::function<void()>> epoll_handlers_;
47};
48
49} // namespace init
50} // namespace android
51
52#endif