blob: 74d8aac96ec8dcff305cb7b0532e6cc34d268780 [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#include "epoll.h"
18
Mark Salyzyn37bbf802019-03-13 07:25:52 -070019#include <stdint.h>
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070020#include <sys/epoll.h>
21
22#include <chrono>
23#include <functional>
24#include <map>
25
26namespace android {
27namespace init {
28
29Epoll::Epoll() {}
30
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070031Result<void> Epoll::Open() {
32 if (epoll_fd_ >= 0) return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070033 epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC));
34
35 if (epoll_fd_ == -1) {
36 return ErrnoError() << "epoll_create1 failed";
37 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070038 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070039}
40
David Anderson1de73842021-05-13 20:18:14 -070041Result<void> Epoll::RegisterHandler(int fd, Handler handler, uint32_t events) {
Mark Salyzyn37bbf802019-03-13 07:25:52 -070042 if (!events) {
43 return Error() << "Must specify events";
44 }
David Anderson1de73842021-05-13 20:18:14 -070045 auto sp = std::make_shared<decltype(handler)>(std::move(handler));
46 auto [it, inserted] = epoll_handlers_.emplace(fd, std::move(sp));
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070047 if (!inserted) {
48 return Error() << "Cannot specify two epoll handlers for a given FD";
49 }
50 epoll_event ev;
Mark Salyzyn37bbf802019-03-13 07:25:52 -070051 ev.events = events;
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070052 // std::map's iterators do not get invalidated until erased, so we use the
53 // pointer to the std::function in the map directly for epoll_ctl.
54 ev.data.ptr = reinterpret_cast<void*>(&it->second);
55 if (epoll_ctl(epoll_fd_, EPOLL_CTL_ADD, fd, &ev) == -1) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070056 Result<void> result = ErrnoError() << "epoll_ctl failed to add fd";
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070057 epoll_handlers_.erase(fd);
58 return result;
59 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070060 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070061}
62
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070063Result<void> Epoll::UnregisterHandler(int fd) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070064 if (epoll_ctl(epoll_fd_, EPOLL_CTL_DEL, fd, nullptr) == -1) {
65 return ErrnoError() << "epoll_ctl failed to remove fd";
66 }
67 if (epoll_handlers_.erase(fd) != 1) {
68 return Error() << "Attempting to remove epoll handler for FD without an existing handler";
69 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070070 return {};
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070071}
72
David Anderson1de73842021-05-13 20:18:14 -070073Result<std::vector<std::shared_ptr<Epoll::Handler>>> Epoll::Wait(
Tom Cherry905a5df2019-08-30 14:12:56 -070074 std::optional<std::chrono::milliseconds> timeout) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070075 int timeout_ms = -1;
76 if (timeout && timeout->count() < INT_MAX) {
77 timeout_ms = timeout->count();
78 }
Tom Cherry905a5df2019-08-30 14:12:56 -070079 const auto max_events = epoll_handlers_.size();
80 epoll_event ev[max_events];
81 auto num_events = TEMP_FAILURE_RETRY(epoll_wait(epoll_fd_, ev, max_events, timeout_ms));
82 if (num_events == -1) {
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070083 return ErrnoError() << "epoll_wait failed";
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070084 }
David Anderson1de73842021-05-13 20:18:14 -070085 std::vector<std::shared_ptr<Handler>> pending_functions;
Tom Cherry905a5df2019-08-30 14:12:56 -070086 for (int i = 0; i < num_events; ++i) {
David Anderson1de73842021-05-13 20:18:14 -070087 auto sp = *reinterpret_cast<std::shared_ptr<Handler>*>(ev[i].data.ptr);
88 pending_functions.emplace_back(std::move(sp));
Tom Cherry905a5df2019-08-30 14:12:56 -070089 }
90
91 return pending_functions;
Mark Salyzyn6c6ec722015-10-24 16:20:18 -070092}
93
94} // namespace init
95} // namespace android