blob: ce259280c26f88d3d3f1f85ed3b9af19467d320e [file] [log] [blame]
Yabin Cui3e4c5952016-07-26 15:03:27 -07001/*
2 * Copyright (C) 2016 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 "IOEventLoop.h"
18
19#include <event2/event.h>
20#include <fcntl.h>
21
22#include <android-base/logging.h>
23
24struct IOEvent {
25 IOEventLoop* loop;
26 event* e;
27 std::function<bool()> callback;
Yabin Cuia383c112016-10-19 11:04:56 -070028 bool enabled;
Yabin Cui3e4c5952016-07-26 15:03:27 -070029
30 IOEvent(IOEventLoop* loop, const std::function<bool()>& callback)
Yabin Cuia383c112016-10-19 11:04:56 -070031 : loop(loop), e(nullptr), callback(callback), enabled(false) {}
Yabin Cui3e4c5952016-07-26 15:03:27 -070032
33 ~IOEvent() {
34 if (e != nullptr) {
35 event_free(e);
36 }
37 }
38};
39
40IOEventLoop::IOEventLoop() : ebase_(nullptr), has_error_(false) {}
41
42IOEventLoop::~IOEventLoop() {
Yabin Cui26968e62017-01-30 11:34:24 -080043 events_.clear();
Yabin Cui3e4c5952016-07-26 15:03:27 -070044 if (ebase_ != nullptr) {
45 event_base_free(ebase_);
46 }
47}
48
49bool IOEventLoop::EnsureInit() {
50 if (ebase_ == nullptr) {
51 ebase_ = event_base_new();
52 if (ebase_ == nullptr) {
53 LOG(ERROR) << "failed to call event_base_new()";
54 return false;
55 }
56 }
57 return true;
58}
59
60void IOEventLoop::EventCallbackFn(int, short, void* arg) {
61 IOEvent* e = static_cast<IOEvent*>(arg);
62 if (!e->callback()) {
63 e->loop->has_error_ = true;
64 e->loop->ExitLoop();
65 }
66}
67
Yabin Cuidbbda302016-07-28 12:55:41 -070068static bool MakeFdNonBlocking(int fd) {
69 int flags = fcntl(fd, F_GETFL, 0);
70 if (flags == -1 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
71 PLOG(ERROR) << "fcntl() failed";
72 return false;
73 }
74 return true;
75}
76
Yabin Cui825e56b2016-08-26 18:25:21 -070077IOEventRef IOEventLoop::AddReadEvent(int fd,
78 const std::function<bool()>& callback) {
79 if (!MakeFdNonBlocking(fd)) {
80 return nullptr;
81 }
82 return AddEvent(fd, EV_READ | EV_PERSIST, nullptr, callback);
Yabin Cuidbbda302016-07-28 12:55:41 -070083}
84
Yabin Cuia383c112016-10-19 11:04:56 -070085IOEventRef IOEventLoop::AddWriteEvent(int fd,
86 const std::function<bool()>& callback) {
87 if (!MakeFdNonBlocking(fd)) {
88 return nullptr;
89 }
90 return AddEvent(fd, EV_WRITE | EV_PERSIST, nullptr, callback);
91}
92
Yabin Cui3e4c5952016-07-26 15:03:27 -070093bool IOEventLoop::AddSignalEvent(int sig,
94 const std::function<bool()>& callback) {
Yabin Cui825e56b2016-08-26 18:25:21 -070095 return AddEvent(sig, EV_SIGNAL | EV_PERSIST, nullptr, callback) != nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -070096}
97
98bool IOEventLoop::AddSignalEvents(std::vector<int> sigs,
99 const std::function<bool()>& callback) {
100 for (auto sig : sigs) {
101 if (!AddSignalEvent(sig, callback)) {
102 return false;
103 }
104 }
105 return true;
106}
107
108bool IOEventLoop::AddPeriodicEvent(timeval duration,
109 const std::function<bool()>& callback) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700110 return AddEvent(-1, EV_PERSIST, &duration, callback) != nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700111}
112
Yabin Cui825e56b2016-08-26 18:25:21 -0700113IOEventRef IOEventLoop::AddEvent(int fd_or_sig, short events, timeval* timeout,
114 const std::function<bool()>& callback) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700115 if (!EnsureInit()) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700116 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700117 }
118 std::unique_ptr<IOEvent> e(new IOEvent(this, callback));
119 e->e = event_new(ebase_, fd_or_sig, events, EventCallbackFn, e.get());
120 if (e->e == nullptr) {
121 LOG(ERROR) << "event_new() failed";
Yabin Cui825e56b2016-08-26 18:25:21 -0700122 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700123 }
124 if (event_add(e->e, timeout) != 0) {
125 LOG(ERROR) << "event_add() failed";
Yabin Cui825e56b2016-08-26 18:25:21 -0700126 return nullptr;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700127 }
Yabin Cuia383c112016-10-19 11:04:56 -0700128 e->enabled = true;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700129 events_.push_back(std::move(e));
Yabin Cui825e56b2016-08-26 18:25:21 -0700130 return events_.back().get();
Yabin Cui3e4c5952016-07-26 15:03:27 -0700131}
132
133bool IOEventLoop::RunLoop() {
134 if (event_base_dispatch(ebase_) == -1) {
135 LOG(ERROR) << "event_base_dispatch() failed";
136 return false;
137 }
138 if (has_error_) {
139 return false;
140 }
141 return true;
142}
143
144bool IOEventLoop::ExitLoop() {
145 if (event_base_loopbreak(ebase_) == -1) {
146 LOG(ERROR) << "event_base_loopbreak() failed";
147 return false;
148 }
149 return true;
150}
Yabin Cui825e56b2016-08-26 18:25:21 -0700151
Yabin Cuia383c112016-10-19 11:04:56 -0700152bool IOEventLoop::DisableEvent(IOEventRef ref) {
153 if (ref->enabled) {
154 if (event_del(ref->e) != 0) {
155 LOG(ERROR) << "event_del() failed";
156 return false;
157 }
158 ref->enabled = false;
159 }
160 return true;
161}
162
163bool IOEventLoop::EnableEvent(IOEventRef ref) {
164 if (!ref->enabled) {
165 if (event_add(ref->e, nullptr) != 0) {
166 LOG(ERROR) << "event_add() failed";
167 return false;
168 }
169 ref->enabled = true;
170 }
171 return true;
172}
173
Yabin Cui825e56b2016-08-26 18:25:21 -0700174bool IOEventLoop::DelEvent(IOEventRef ref) {
Yabin Cuia383c112016-10-19 11:04:56 -0700175 DisableEvent(ref);
Yabin Cui825e56b2016-08-26 18:25:21 -0700176 IOEventLoop* loop = ref->loop;
177 for (auto it = loop->events_.begin(); it != loop->events_.end(); ++it) {
178 if (it->get() == ref) {
Yabin Cui825e56b2016-08-26 18:25:21 -0700179 loop->events_.erase(it);
180 break;
181 }
182 }
183 return true;
184}