| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 |  | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 17 | #pragma once | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 18 |  | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 | #include <sys/epoll.h> | 
|  | 21 |  | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 22 | #include <chrono> | 
|  | 23 | #include <functional> | 
|  | 24 | #include <map> | 
| David Anderson | 1de7384 | 2021-05-13 20:18:14 -0700 | [diff] [blame] | 25 | #include <memory> | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 26 | #include <optional> | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 27 | #include <unordered_set> | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 28 | #include <vector> | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include <android-base/unique_fd.h> | 
|  | 31 |  | 
|  | 32 | #include "result.h" | 
|  | 33 |  | 
|  | 34 | namespace android { | 
|  | 35 | namespace init { | 
|  | 36 |  | 
|  | 37 | class Epoll { | 
|  | 38 | public: | 
|  | 39 | Epoll(); | 
|  | 40 |  | 
| David Anderson | 1de7384 | 2021-05-13 20:18:14 -0700 | [diff] [blame] | 41 | typedef std::function<void()> Handler; | 
|  | 42 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 43 | Result<void> Open(); | 
| David Anderson | 1de7384 | 2021-05-13 20:18:14 -0700 | [diff] [blame] | 44 | Result<void> RegisterHandler(int fd, Handler handler, uint32_t events = EPOLLIN); | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 45 | Result<void> UnregisterHandler(int fd); | 
| Bart Van Assche | a2c1604 | 2022-10-14 09:35:35 -0700 | [diff] [blame] | 46 | void SetFirstCallback(std::function<void()> first_callback); | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 47 | Result<int> Wait(std::optional<std::chrono::milliseconds> timeout); | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | private: | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 50 | struct Info { | 
| Bart Van Assche | dcd23df | 2022-10-14 12:55:35 -0700 | [diff] [blame] | 51 | Handler handler; | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 52 | uint32_t events; | 
|  | 53 | }; | 
|  | 54 |  | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 55 | android::base::unique_fd epoll_fd_; | 
| David Anderson | 0fa7c40 | 2022-03-07 19:10:57 -0800 | [diff] [blame] | 56 | std::map<int, Info> epoll_handlers_; | 
| Bart Van Assche | a2c1604 | 2022-10-14 09:35:35 -0700 | [diff] [blame] | 57 | std::function<void()> first_callback_; | 
| Bart Van Assche | bc5c4a4 | 2022-10-14 09:13:19 -0700 | [diff] [blame] | 58 | std::unordered_set<int> to_remove_; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 59 | }; | 
|  | 60 |  | 
|  | 61 | }  // namespace init | 
|  | 62 | }  // namespace android |