| 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 |  | 
|  | 17 | #include "epoll.h" | 
|  | 18 |  | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 20 | #include <sys/epoll.h> | 
|  | 21 |  | 
|  | 22 | #include <chrono> | 
|  | 23 | #include <functional> | 
|  | 24 | #include <map> | 
|  | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 | namespace init { | 
|  | 28 |  | 
|  | 29 | Epoll::Epoll() {} | 
|  | 30 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 31 | Result<void> Epoll::Open() { | 
|  | 32 | if (epoll_fd_ >= 0) return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 33 | epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC)); | 
|  | 34 |  | 
|  | 35 | if (epoll_fd_ == -1) { | 
|  | 36 | return ErrnoError() << "epoll_create1 failed"; | 
|  | 37 | } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 38 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 41 | Result<void> Epoll::RegisterHandler(int fd, std::function<void()> handler, uint32_t events) { | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 42 | if (!events) { | 
|  | 43 | return Error() << "Must specify events"; | 
|  | 44 | } | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 45 | auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(handler)); | 
|  | 46 | if (!inserted) { | 
|  | 47 | return Error() << "Cannot specify two epoll handlers for a given FD"; | 
|  | 48 | } | 
|  | 49 | epoll_event ev; | 
| Mark Salyzyn | 37bbf80 | 2019-03-13 07:25:52 -0700 | [diff] [blame] | 50 | ev.events = events; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 51 | // std::map's iterators do not get invalidated until erased, so we use the | 
|  | 52 | // pointer to the std::function in the map directly for epoll_ctl. | 
|  | 53 | ev.data.ptr = reinterpret_cast<void*>(&it->second); | 
|  | 54 | if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) { | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 55 | Result<void> result = ErrnoError() << "epoll_ctl failed to add fd"; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 56 | epoll_handlers_.erase(fd); | 
|  | 57 | return result; | 
|  | 58 | } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 59 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 62 | Result<void> Epoll::UnregisterHandler(int fd) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 63 | if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) { | 
|  | 64 | return ErrnoError() << "epoll_ctl failed to remove fd"; | 
|  | 65 | } | 
|  | 66 | if (epoll_handlers_.erase(fd) != 1) { | 
|  | 67 | return Error() << "Attempting to remove epoll handler for FD without an existing handler"; | 
|  | 68 | } | 
| Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 69 | return {}; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 72 | Result<std::vector<std::function<void()>*>> Epoll::Wait( | 
|  | 73 | std::optional<std::chrono::milliseconds> timeout) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 74 | int timeout_ms = -1; | 
|  | 75 | if (timeout && timeout->count() < INT_MAX) { | 
|  | 76 | timeout_ms = timeout->count(); | 
|  | 77 | } | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 78 | const auto max_events = epoll_handlers_.size(); | 
|  | 79 | epoll_event ev[max_events]; | 
|  | 80 | auto num_events = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_, ev, max_events, timeout_ms)); | 
|  | 81 | if (num_events == -1) { | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 82 | return ErrnoError() << "epoll_wait failed"; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 83 | } | 
| Tom Cherry | 905a5df | 2019-08-30 14:12:56 -0700 | [diff] [blame] | 84 | std::vector<std::function<void()>*> pending_functions; | 
|  | 85 | for (int i = 0; i < num_events; ++i) { | 
|  | 86 | pending_functions.emplace_back(reinterpret_cast<std::function<void()>*>(ev[i].data.ptr)); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | return pending_functions; | 
| Mark Salyzyn | 6c6ec72 | 2015-10-24 16:20:18 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
|  | 92 | }  // namespace init | 
|  | 93 | }  // namespace android |