1 /*
2 * Copyright (C) 2022 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 "host/commands/cvd/epoll_loop.h"
18
19 #include <android-base/errors.h>
20
21 #include "common/libs/fs/epoll.h"
22 #include "common/libs/fs/shared_fd.h"
23 #include "common/libs/utils/result.h"
24
25 namespace cuttlefish {
26
EpollPool(Epoll epoll)27 EpollPool::EpollPool(Epoll epoll) : epoll_(std::move(epoll)) {}
28
EpollPool(EpollPool && other)29 EpollPool::EpollPool(EpollPool&& other) {
30 std::unique_lock own_lock(instance_mutex_, std::defer_lock);
31 std::unique_lock other_lock(other.instance_mutex_, std::defer_lock);
32 std::unique_lock own_cb_lock(callbacks_mutex_, std::defer_lock);
33 std::unique_lock other_cb_lock(other.callbacks_mutex_, std::defer_lock);
34 std::lock(own_lock, other_lock, own_cb_lock, other_cb_lock);
35 epoll_ = std::move(other.epoll_);
36 callbacks_ = std::move(other.callbacks_);
37 }
38
operator =(EpollPool && other)39 EpollPool& EpollPool::operator=(EpollPool&& other) {
40 std::unique_lock own_lock(instance_mutex_, std::defer_lock);
41 std::unique_lock other_lock(other.instance_mutex_, std::defer_lock);
42 std::unique_lock own_cb_lock(callbacks_mutex_, std::defer_lock);
43 std::unique_lock other_cb_lock(other.callbacks_mutex_, std::defer_lock);
44 std::lock(own_lock, other_lock, own_cb_lock, other_cb_lock);
45 epoll_ = std::move(other.epoll_);
46 callbacks_ = std::move(other.callbacks_);
47
48 return *this;
49 }
50
Register(SharedFD fd,uint32_t events,EpollCallback callback)51 Result<void> EpollPool::Register(SharedFD fd, uint32_t events,
52 EpollCallback callback) {
53 std::shared_lock instance_lock(instance_mutex_, std::defer_lock);
54 std::unique_lock callbacks_lock(callbacks_mutex_, std::defer_lock);
55 std::lock(instance_lock, callbacks_lock);
56 if (callbacks_.find(fd) != callbacks_.end()) {
57 return CF_ERR("Already have a callback created");
58 }
59 CF_EXPECT(epoll_.AddOrModify(fd, events | EPOLLONESHOT));
60 callbacks_[fd] = std::move(callback);
61 return {};
62 }
63
HandleEvent()64 Result<void> EpollPool::HandleEvent() {
65 auto event = CF_EXPECT(epoll_.Wait());
66 if (!event) {
67 return {};
68 }
69 EpollCallback callback;
70 {
71 std::lock_guard lock(callbacks_mutex_);
72 auto it = callbacks_.find(event->fd);
73 CF_EXPECT(it != callbacks_.end(), "Could not find event callback");
74 callback = std::move(it->second);
75 callbacks_.erase(it);
76 }
77 CF_EXPECT(callback(*event));
78 return {};
79 }
80
Remove(SharedFD fd)81 Result<void> EpollPool::Remove(SharedFD fd) {
82 std::shared_lock instance_lock(instance_mutex_, std::defer_lock);
83 std::unique_lock callbacks_lock(callbacks_mutex_, std::defer_lock);
84 std::lock(instance_lock, callbacks_lock);
85 CF_EXPECT(epoll_.Delete(fd), "No callback registered with epoll");
86 callbacks_.erase(fd);
87 return {};
88 }
89
EpollLoopComponent()90 fruit::Component<EpollPool> EpollLoopComponent() {
91 return fruit::createComponent()
92 .registerProvider([]() -> EpollPool {
93 return EpollPool(OR_FATAL(Epoll::Create()));
94 });
95 }
96
97 } // namespace cuttlefish
98