1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "event_demultiplexer.h"
17 #include "event_reactor.h"
18 #include "event_handler.h"
19 #include "common_timer_errors.h"
20 #include "utils_log.h"
21
22 #include <vector>
23 #include <cstdio>
24 #include <strings.h>
25 #include <unistd.h>
26 #include <sys/epoll.h>
27
28 namespace OHOS {
29 namespace Utils {
30
31 static const int EPOLL_MAX_EVENS_INIT = 8;
32 static const int HALF_OF_MAX_EVENT = 2;
33 static const int EPOLL_INVALID_FD = -1;
34
EventDemultiplexer()35 EventDemultiplexer::EventDemultiplexer()
36 : epollFd_(epoll_create1(EPOLL_CLOEXEC)), maxEvents_(EPOLL_MAX_EVENS_INIT), mutex_(), eventHandlers_()
37 {
38 }
39
~EventDemultiplexer()40 EventDemultiplexer::~EventDemultiplexer()
41 {
42 CleanUp();
43 }
44
StartUp()45 uint32_t EventDemultiplexer::StartUp()
46 {
47 if (epollFd_ < 0) {
48 epollFd_ = epoll_create1(EPOLL_CLOEXEC);
49 if (epollFd_ < 0) {
50 return TIMER_ERR_BADF;
51 }
52 }
53 return TIMER_ERR_OK;
54 }
55
CleanUp()56 void EventDemultiplexer::CleanUp()
57 {
58 if (epollFd_ != EPOLL_INVALID_FD) {
59 close(epollFd_);
60 epollFd_ = EPOLL_INVALID_FD;
61 }
62 }
63
UpdateEventHandler(EventHandler * handler)64 uint32_t EventDemultiplexer::UpdateEventHandler(EventHandler* handler)
65 {
66 if (handler == nullptr) {
67 return TIMER_ERR_INVALID_VALUE;
68 }
69
70 std::lock_guard<std::recursive_mutex> lock(mutex_);
71 auto itor = eventHandlers_.find(handler->GetHandle());
72 if (itor == eventHandlers_.end()) {
73 eventHandlers_.insert(std::make_pair(handler->GetHandle(), handler->shared_from_this()));
74 return Update(EPOLL_CTL_ADD, handler);
75 }
76
77 if (handler->Events() == EventReactor::NONE_EVENT) {
78 eventHandlers_.erase(itor);
79 return Update(EPOLL_CTL_DEL, handler);
80 }
81
82 if (handler != itor->second.get()) {
83 return TIMER_ERR_DEAL_FAILED;
84 }
85 return Update(EPOLL_CTL_MOD, handler);
86 }
87
Update(int operation,EventHandler * handler)88 uint32_t EventDemultiplexer::Update(int operation, EventHandler* handler)
89 {
90 struct epoll_event event;
91 bzero(&event, sizeof(event));
92 event.events = Reactor2Epoll(handler->Events());
93 event.data.fd = handler->GetHandle();
94
95 if (epoll_ctl(epollFd_, operation, handler->GetHandle(), &event) != 0) {
96 UTILS_LOGD("epoll_ctl %{public}d operation %{public}d on handle %{public}d failed",
97 epollFd_, operation, handler->GetHandle());
98 return TIMER_ERR_DEAL_FAILED;
99 }
100 return TIMER_ERR_OK;
101 }
102
Polling(int timeout)103 void EventDemultiplexer::Polling(int timeout /* ms */)
104 {
105 std::vector<struct epoll_event> epollEvents(maxEvents_);
106 std::vector<std::shared_ptr<EventHandler>> taskQue;
107 std::vector<uint32_t> eventQue;
108
109 int nfds = epoll_wait(epollFd_, &epollEvents[0], static_cast<int>(epollEvents.size()), timeout);
110 if (nfds == 0) {
111 return;
112 }
113 if (nfds == -1) {
114 UTILS_LOGE("epoll_wait failed.");
115 return;
116 }
117
118 {
119 std::lock_guard<std::recursive_mutex> lock(mutex_);
120 for (int idx = 0; idx < nfds; ++idx) {
121 int targetFd = epollEvents[idx].data.fd;
122 uint32_t events = epollEvents[idx].events;
123
124 auto itor = eventHandlers_.find(targetFd);
125 if (itor != eventHandlers_.end()) {
126 taskQue.emplace_back(itor->second);
127 eventQue.emplace_back(events);
128 }
129 }
130 }
131
132 for (size_t idx = 0u; idx < taskQue.size() && idx < eventQue.size(); idx++) {
133 taskQue[idx]->HandleEvents(eventQue[idx]);
134 }
135
136 if (nfds == maxEvents_) {
137 maxEvents_ *= HALF_OF_MAX_EVENT;
138 }
139 }
140
Epoll2Reactor(uint32_t epollEvents)141 uint32_t EventDemultiplexer::Epoll2Reactor(uint32_t epollEvents)
142 {
143 if (epollEvents & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)) {
144 return EventReactor::READ_EVENT;
145 }
146
147 return EventReactor::NONE_EVENT;
148 }
149
Reactor2Epoll(uint32_t reactorEvent)150 uint32_t EventDemultiplexer::Reactor2Epoll(uint32_t reactorEvent)
151 {
152 switch (reactorEvent) {
153 case EventReactor::NONE_EVENT:
154 return TIMER_ERR_OK;
155 case EventReactor::READ_EVENT:
156 return EPOLLIN | EPOLLPRI;
157 default:
158 UTILS_LOGD("invalid event %{public}u.", reactorEvent);
159 return TIMER_ERR_DEAL_FAILED;
160 }
161 }
162
163 }
164 }
165